Debian Codegraph @

pTk/mTk/tclUnix/tclUnixInit.c

raw symbols SCIP
1 /*
2 * tclUnixInit.c --
3 *
4 * Contains the Unix-specific interpreter initialization functions.
5 *
6 * Copyright (c) 1995-1997 Sun Microsystems, Inc.
7 * Copyright (c) 1999 by Scriptics Corporation.
8 * All rights reserved.
9 *
10 * RCS: @(#) $Id: tclUnixInit.c,v 1.34.2.2 2003/11/10 20:32:34 dgp Exp $
11 */
12
13 #if defined(HAVE_CFBUNDLE)
14 #include <CoreFoundation/CoreFoundation.h>
15 #endif
16 #define NO_UNAME
17
18 #include "Lang.h"
19 #include "tclInt.h"
20 #include "tclPort.h"
21 #include <locale.h>
22 #ifdef HAVE_LANGINFO
23 #include <langinfo.h>
24 #endif
25 #if defined(__FreeBSD__)
26 # include <floatingpoint.h>
27 #endif
28 #if defined(__bsdi__)
29 # include <sys/param.h>
30 # if _BSDI_VERSION > 199501
31 # include <dlfcn.h>
32 # endif
33 #endif
34
35 /*
36 * The Init script (common to Windows and Unix platforms) is
37 * defined in tkInitScript.h
38 */
39 #include "tclInitScript.h"
40
41 /* Used to store the encoding used for binary files */
42 static Tcl_Encoding binaryEncoding = NULL;
43 /* Has the basic library path encoding issue been fixed */
44 static int libraryPathEncodingFixed = 0;
45
46 #if 0
47 /*
48 * Tcl tries to use standard and homebrew methods to guess the right
49 * encoding on the platform. However, there is always a final fallback,
50 * and this value is it. Make sure it is a real Tcl encoding.
51 */
52
53 #ifndef TCL_DEFAULT_ENCODING
54 #define TCL_DEFAULT_ENCODING "iso8859-1"
55 #endif
56
57 /*
58 * Default directory in which to look for Tcl library scripts. The
59 * symbol is defined by Makefile.
60 */
61
62 static char defaultLibraryDir[sizeof(TCL_LIBRARY)+200] = TCL_LIBRARY;
63
64 /*
65 * Directory in which to look for packages (each package is typically
66 * installed as a subdirectory of this directory). The symbol is
67 * defined by Makefile.
68 */
69
70 static char pkgPath[sizeof(TCL_PACKAGE_PATH)+200] = TCL_PACKAGE_PATH;
71
72 /*
73 * The following table is used to map from Unix locale strings to
74 * encoding files. If HAVE_LANGINFO is defined, then this is a fallback
75 * table when the result from nl_langinfo isn't a recognized encoding.
76 * Otherwise this is the first list checked for a mapping from env
77 * encoding to Tcl encoding name.
78 */
79
80 typedef struct LocaleTable {
81 CONST char *lang;
82 CONST char *encoding;
83 } LocaleTable;
84
85 static CONST LocaleTable localeTable[] = {
86 #ifdef HAVE_LANGINFO
87 {"gb2312-1980", "gb2312"},
88 #ifdef __hpux
89 {"SJIS", "shiftjis"},
90 {"eucjp", "euc-jp"},
91 {"euckr", "euc-kr"},
92 {"euctw", "euc-cn"},
93 {"greek8", "cp869"},
94 {"iso88591", "iso8859-1"},
95 {"iso88592", "iso8859-2"},
96 {"iso88595", "iso8859-5"},
97 {"iso88596", "iso8859-6"},
98 {"iso88597", "iso8859-7"},
99 {"iso88598", "iso8859-8"},
100 {"iso88599", "iso8859-9"},
101 {"iso885915", "iso8859-15"},
102 {"roman8", "iso8859-1"},
103 {"tis620", "tis-620"},
104 {"turkish8", "cp857"},
105 {"utf8", "utf-8"},
106 #endif /* __hpux */
107 #endif /* HAVE_LANGINFO */
108
109 {"ja_JP.SJIS", "shiftjis"},
110 {"ja_JP.EUC", "euc-jp"},
111 {"ja_JP.eucJP", "euc-jp"},
112 {"ja_JP.JIS", "iso2022-jp"},
113 {"ja_JP.mscode", "shiftjis"},
114 {"ja_JP.ujis", "euc-jp"},
115 {"ja_JP", "euc-jp"},
116 {"Ja_JP", "shiftjis"},
117 {"Jp_JP", "shiftjis"},
118 {"japan", "euc-jp"},
119 #ifdef hpux
120 {"japanese", "shiftjis"},
121 {"ja", "shiftjis"},
122 #else
123 {"japanese", "euc-jp"},
124 {"ja", "euc-jp"},
125 #endif
126 {"japanese.sjis", "shiftjis"},
127 {"japanese.euc", "euc-jp"},
128 {"japanese-sjis", "shiftjis"},
129 {"japanese-ujis", "euc-jp"},
130
131 {"ko", "euc-kr"},
132 {"ko_KR", "euc-kr"},
133 {"ko_KR.EUC", "euc-kr"},
134 {"ko_KR.euc", "euc-kr"},
135 {"ko_KR.eucKR", "euc-kr"},
136 {"korean", "euc-kr"},
137
138 {"ru", "iso8859-5"},
139 {"ru_RU", "iso8859-5"},
140 {"ru_SU", "iso8859-5"},
141
142 {"zh", "cp936"},
143
144 {NULL, NULL}
145 };
146
147 #ifdef HAVE_CFBUNDLE
148 static int Tcl_MacOSXGetLibraryPath(Tcl_Interp *interp, int maxPathLen, char *tclLibPath);
149 #endif /* HAVE_CFBUNDLE */
150
151
152 /*
153 *---------------------------------------------------------------------------
154 *
155 * TclpInitPlatform --
156 *
157 * Initialize all the platform-dependant things like signals and
158 * floating-point error handling.
159 *
160 * Called at process initialization time.
161 *
162 * Results:
163 * None.
164 *
165 * Side effects:
166 * None.
167 *
168 *---------------------------------------------------------------------------
169 */
170
171 void
172 TclpInitPlatform()
173 {
174 tclPlatform = TCL_PLATFORM_UNIX;
175
176 /*
177 * The code below causes SIGPIPE (broken pipe) errors to
178 * be ignored. This is needed so that Tcl processes don't
179 * die if they create child processes (e.g. using "exec" or
180 * "open") that terminate prematurely. The signal handler
181 * is only set up when the first interpreter is created;
182 * after this the application can override the handler with
183 * a different one of its own, if it wants.
184 */
185
186 #ifdef SIGPIPE
187 (void) signal(SIGPIPE, SIG_IGN);
188 #endif /* SIGPIPE */
189
190 #ifdef __FreeBSD__
191 fpsetround(FP_RN);
192 fpsetmask(0L);
193 #endif
194
195 #if defined(__bsdi__) && (_BSDI_VERSION > 199501)
196 /*
197 * Find local symbols. Don't report an error if we fail.
198 */
199 (void) dlopen (NULL, RTLD_NOW); /* INTL: Native. */
200 #endif
201 }
202
203 /*
204 *---------------------------------------------------------------------------
205 *
206 * TclpInitLibraryPath --
207 *
208 * Initialize the library path at startup. We have a minor
209 * metacircular problem that we don't know the encoding of the
210 * operating system but we may need to talk to operating system
211 * to find the library directories so that we know how to talk to
212 * the operating system.
213 *
214 * We do not know the encoding of the operating system.
215 * We do know that the encoding is some multibyte encoding.
216 * In that multibyte encoding, the characters 0..127 are equivalent
217 * to ascii.
218 *
219 * So although we don't know the encoding, it's safe:
220 * to look for the last slash character in a path in the encoding.
221 * to append an ascii string to a path.
222 * to pass those strings back to the operating system.
223 *
224 * But any strings that we remembered before we knew the encoding of
225 * the operating system must be translated to UTF-8 once we know the
226 * encoding so that the rest of Tcl can use those strings.
227 *
228 * This call sets the library path to strings in the unknown native
229 * encoding. TclpSetInitialEncodings() will translate the library
230 * path from the native encoding to UTF-8 as soon as it determines
231 * what the native encoding actually is.
232 *
233 * Called at process initialization time.
234 *
235 * Results:
236 * None.
237 *
238 * Side effects:
239 * None.
240 *
241 *---------------------------------------------------------------------------
242 */
243
244 void
245 TclpInitLibraryPath(path)
246 CONST char *path; /* Path to the executable in native
247 * multi-byte encoding. */
248 {
249 #define LIBRARY_SIZE 32
250 Tcl_Obj *pathPtr, *objPtr;
251 CONST char *str;
252 Tcl_DString buffer, ds;
253 int pathc;
254 CONST char **pathv;
255 char installLib[LIBRARY_SIZE], developLib[LIBRARY_SIZE];
256
257 Tcl_DStringInit(&ds);
258 pathPtr = Tcl_NewObj();
259
260 /*
261 * Initialize the substrings used when locating an executable. The
262 * installLib variable computes the path as though the executable
263 * is installed. The developLib computes the path as though the
264 * executable is run from a develpment directory.
265 */
266
267 sprintf(installLib, "lib/tcl%s", TCL_VERSION);
268 sprintf(developLib, "tcl%s/library", TCL_PATCH_LEVEL);
269
270 /*
271 * Look for the library relative to default encoding dir.
272 */
273
274 str = Tcl_GetDefaultEncodingDir();
275 if ((str != NULL) && (str[0] != '\0')) {
276 objPtr = Tcl_NewStringObj(str, -1);
277 Tcl_ListObjAppendElement(NULL, pathPtr, objPtr);
278 }
279
280 /*
281 * Look for the library relative to the TCL_LIBRARY env variable.
282 * If the last dirname in the TCL_LIBRARY path does not match the
283 * last dirname in the installLib variable, use the last dir name
284 * of installLib in addition to the orginal TCL_LIBRARY path.
285 */
286
287 str = getenv("TCL_LIBRARY"); /* INTL: Native. */
288 Tcl_ExternalToUtfDString(NULL, str, -1, &buffer);
289 str = Tcl_DStringValue(&buffer);
290
291 if ((str != NULL) && (str[0] != '\0')) {
292 /*
293 * If TCL_LIBRARY is set, search there.
294 */
295
296 objPtr = Tcl_NewStringObj(str, -1);
297 Tcl_ListObjAppendElement(NULL, pathPtr, objPtr);
298
299 Tcl_SplitPath(str, &pathc, &pathv);
300 if ((pathc > 0) && (strcasecmp(installLib + 4, pathv[pathc-1]) != 0)) {
301 /*
302 * If TCL_LIBRARY is set but refers to a different tcl
303 * installation than the current version, try fiddling with the
304 * specified directory to make it refer to this installation by
305 * removing the old "tclX.Y" and substituting the current
306 * version string.
307 */
308
309 pathv[pathc - 1] = installLib + 4;
310 str = Tcl_JoinPath(pathc, pathv, &ds);
311 objPtr = Tcl_NewStringObj(str, Tcl_DStringLength(&ds));
312 Tcl_ListObjAppendElement(NULL, pathPtr, objPtr);
313 Tcl_DStringFree(&ds);
314 }
315 ckfree((char *) pathv);
316 }
317
318 /*
319 * Look for the library relative to the executable. This algorithm
320 * should be the same as the one in the tcl_findLibrary procedure.
321 *
322 * This code looks in the following directories:
323 *
324 * <bindir>/../<installLib>
325 * (e.g. /usr/local/bin/../lib/tcl8.4)
326 * <bindir>/../../<installLib>
327 * (e.g. /usr/local/TclPro/solaris-sparc/bin/../../lib/tcl8.4)
328 * <bindir>/../library
329 * (e.g. /usr/src/tcl8.4.0/unix/../library)
330 * <bindir>/../../library
331 * (e.g. /usr/src/tcl8.4.0/unix/solaris-sparc/../../library)
332 * <bindir>/../../<developLib>
333 * (e.g. /usr/src/tcl8.4.0/unix/../../tcl8.4.0/library)
334 * <bindir>/../../../<developLib>
335 * (e.g. /usr/src/tcl8.4.0/unix/solaris-sparc/../../../tcl8.4.0/library)
336 */
337
338
339 /*
340 * The variable path holds an absolute path. Take care not to
341 * overwrite pathv[0] since that might produce a relative path.
342 */
343
344 if (path != NULL) {
345 int i, origc;
346 CONST char **origv;
347
348 Tcl_SplitPath(path, &origc, &origv);
349 pathc = 0;
350 pathv = (CONST char **) ckalloc((unsigned int)(origc * sizeof(char *)));
351 for (i=0; i< origc; i++) {
352 if (origv[i][0] == '.') {
353 if (strcmp(origv[i], ".") == 0) {
354 /* do nothing */
355 } else if (strcmp(origv[i], "..") == 0) {
356 pathc--;
357 } else {
358 pathv[pathc++] = origv[i];
359 }
360 } else {
361 pathv[pathc++] = origv[i];
362 }
363 }
364 if (pathc > 2) {
365 str = pathv[pathc - 2];
366 pathv[pathc - 2] = installLib;
367 path = Tcl_JoinPath(pathc - 1, pathv, &ds);
368 pathv[pathc - 2] = str;
369 objPtr = Tcl_NewStringObj(path, Tcl_DStringLength(&ds));
370 Tcl_ListObjAppendElement(NULL, pathPtr, objPtr);
371 Tcl_DStringFree(&ds);
372 }
373 if (pathc > 3) {
374 str = pathv[pathc - 3];
375 pathv[pathc - 3] = installLib;
376 path = Tcl_JoinPath(pathc - 2, pathv, &ds);
377 pathv[pathc - 3] = str;
378 objPtr = Tcl_NewStringObj(path, Tcl_DStringLength(&ds));
379 Tcl_ListObjAppendElement(NULL, pathPtr, objPtr);
380 Tcl_DStringFree(&ds);
381 }
382 if (pathc > 2) {
383 str = pathv[pathc - 2];
384 pathv[pathc - 2] = "library";
385 path = Tcl_JoinPath(pathc - 1, pathv, &ds);
386 pathv[pathc - 2] = str;
387 objPtr = Tcl_NewStringObj(path, Tcl_DStringLength(&ds));
388 Tcl_ListObjAppendElement(NULL, pathPtr, objPtr);
389 Tcl_DStringFree(&ds);
390 }
391 if (pathc > 3) {
392 str = pathv[pathc - 3];
393 pathv[pathc - 3] = "library";
394 path = Tcl_JoinPath(pathc - 2, pathv, &ds);
395 pathv[pathc - 3] = str;
396 objPtr = Tcl_NewStringObj(path, Tcl_DStringLength(&ds));
397 Tcl_ListObjAppendElement(NULL, pathPtr, objPtr);
398 Tcl_DStringFree(&ds);
399 }
400 if (pathc > 3) {
401 str = pathv[pathc - 3];
402 pathv[pathc - 3] = developLib;
403 path = Tcl_JoinPath(pathc - 2, pathv, &ds);
404 pathv[pathc - 3] = str;
405 objPtr = Tcl_NewStringObj(path, Tcl_DStringLength(&ds));
406 Tcl_ListObjAppendElement(NULL, pathPtr, objPtr);
407 Tcl_DStringFree(&ds);
408 }
409 if (pathc > 4) {
410 str = pathv[pathc - 4];
411 pathv[pathc - 4] = developLib;
412 path = Tcl_JoinPath(pathc - 3, pathv, &ds);
413 pathv[pathc - 4] = str;
414 objPtr = Tcl_NewStringObj(path, Tcl_DStringLength(&ds));
415 Tcl_ListObjAppendElement(NULL, pathPtr, objPtr);
416 Tcl_DStringFree(&ds);
417 }
418 ckfree((char *) origv);
419 ckfree((char *) pathv);
420 }
421
422 /*
423 * Finally, look for the library relative to the compiled-in path.
424 * This is needed when users install Tcl with an exec-prefix that
425 * is different from the prtefix.
426 */
427
428 {
429 #ifdef HAVE_CFBUNDLE
430 char tclLibPath[MAXPATHLEN + 1];
431
432 if (Tcl_MacOSXGetLibraryPath(NULL, MAXPATHLEN, tclLibPath) == TCL_OK) {
433 str = tclLibPath;
434 } else
435 #endif /* HAVE_CFBUNDLE */
436 {
437 str = defaultLibraryDir;
438 }
439 if (str[0] != '\0') {
440 objPtr = Tcl_NewStringObj(str, -1);
441 Tcl_ListObjAppendElement(NULL, pathPtr, objPtr);
442 }
443 }
444
445 TclSetLibraryPath(pathPtr);
446 Tcl_DStringFree(&buffer);
447 }
448
449 /*
450 *---------------------------------------------------------------------------
451 *
452 * TclpSetInitialEncodings --
453 *
454 * Based on the locale, determine the encoding of the operating
455 * system and the default encoding for newly opened files.
456 *
457 * Called at process initialization time, and part way through
458 * startup, we verify that the initial encodings were correctly
459 * setup. Depending on Tcl's environment, there may not have been
460 * enough information first time through (above).
461 *
462 * Results:
463 * None.
464 *
465 * Side effects:
466 * The Tcl library path is converted from native encoding to UTF-8,
467 * on the first call, and the encodings may be changed on first or
468 * second call.
469 *
470 *---------------------------------------------------------------------------
471 */
472
473 void
474 TclpSetInitialEncodings()
475 {
476 if (libraryPathEncodingFixed == 0) {
477 CONST char *encoding = NULL;
478 int i, setSysEncCode = TCL_ERROR;
479 Tcl_Obj *pathPtr;
480
481 /*
482 * Determine the current encoding from the LC_* or LANG environment
483 * variables. We previously used setlocale() to determine the locale,
484 * but this does not work on some systems (e.g. Linux/i386 RH 5.0).
485 */
486 #ifdef HAVE_LANGINFO
487 if (setlocale(LC_CTYPE, "") != NULL) {
488 Tcl_DString ds;
489
490 /*
491 * Use a DString so we can overwrite it in name compatability
492 * checks below.
493 */
494
495 Tcl_DStringInit(&ds);
496 encoding = Tcl_DStringAppend(&ds, nl_langinfo(CODESET), -1);
497
498 Tcl_UtfToLower(Tcl_DStringValue(&ds));
499 #ifdef HAVE_LANGINFO_DEBUG
500 fprintf(stderr, "encoding '%s'", encoding);
501 #endif
502 if (encoding[0] == 'i' && encoding[1] == 's' && encoding[2] == 'o'
503 && encoding[3] == '-') {
504 char *p, *q;
505 /* need to strip '-' from iso-* encoding */
506 for(p = Tcl_DStringValue(&ds)+3, q = Tcl_DStringValue(&ds)+4;
507 *p; *p++ = *q++);
508 } else if (encoding[0] == 'i' && encoding[1] == 'b'
509 && encoding[2] == 'm' && encoding[3] >= '0'
510 && encoding[3] <= '9') {
511 char *p, *q;
512 /* if langinfo reports "ibm*" we should use "cp*" */
513 p = Tcl_DStringValue(&ds);
514 *p++ = 'c'; *p++ = 'p';
515 for(q = p+1; *p ; *p++ = *q++);
516 } else if ((*encoding == '\0')
517 || !strcmp(encoding, "ansi_x3.4-1968")) {
518 /* Use iso8859-1 for empty or 'ansi_x3.4-1968' encoding */
519 encoding = "iso8859-1";
520 }
521 #ifdef HAVE_LANGINFO_DEBUG
522 fprintf(stderr, " ?%s?", encoding);
523 #endif
524 setSysEncCode = Tcl_SetSystemEncoding(NULL, encoding);
525 if (setSysEncCode != TCL_OK) {
526 /*
527 * If this doesn't return TCL_OK, the encoding returned by
528 * nl_langinfo or as we translated it wasn't accepted. Do
529 * this fallback check. If this fails, we will enter the
530 * old fallback below.
531 */
532
533 for (i = 0; localeTable[i].lang != NULL; i++) {
534 if (strcmp(localeTable[i].lang, encoding) == 0) {
535 setSysEncCode = Tcl_SetSystemEncoding(NULL,
536 localeTable[i].encoding);
537 break;
538 }
539 }
540 }
541 #ifdef HAVE_LANGINFO_DEBUG
542 fprintf(stderr, " => '%s'\n", encoding);
543 #endif
544 Tcl_DStringFree(&ds);
545 }
546 #ifdef HAVE_LANGINFO_DEBUG
547 else {
548 fprintf(stderr, "setlocale returned NULL\n");
549 }
550 #endif
551 #endif /* HAVE_LANGINFO */
552
553 if (setSysEncCode != TCL_OK) {
554 /*
555 * Classic fallback check. This tries a homebrew algorithm to
556 * determine what encoding should be used based on env vars.
557 */
558 char *langEnv = getenv("LC_ALL");
559 encoding = NULL;
560
561 if (langEnv == NULL || langEnv[0] == '\0') {
562 langEnv = getenv("LC_CTYPE");
563 }
564 if (langEnv == NULL || langEnv[0] == '\0') {
565 langEnv = getenv("LANG");
566 }
567 if (langEnv == NULL || langEnv[0] == '\0') {
568 langEnv = NULL;
569 }
570
571 if (langEnv != NULL) {
572 for (i = 0; localeTable[i].lang != NULL; i++) {
573 if (strcmp(localeTable[i].lang, langEnv) == 0) {
574 encoding = localeTable[i].encoding;
575 break;
576 }
577 }
578 /*
579 * There was no mapping in the locale table. If there is an
580 * encoding subfield, we can try to guess from that.
581 */
582
583 if (encoding == NULL) {
584 char *p;
585 for (p = langEnv; *p != '\0'; p++) {
586 if (*p == '.') {
587 p++;
588 break;
589 }
590 }
591 if (*p != '\0') {
592 Tcl_DString ds;
593 Tcl_DStringInit(&ds);
594 encoding = Tcl_DStringAppend(&ds, p, -1);
595
596 Tcl_UtfToLower(Tcl_DStringValue(&ds));
597 setSysEncCode = Tcl_SetSystemEncoding(NULL, encoding);
598 if (setSysEncCode != TCL_OK) {
599 encoding = NULL;
600 }
601 Tcl_DStringFree(&ds);
602 }
603 }
604 #ifdef HAVE_LANGINFO_DEBUG
605 fprintf(stderr, "encoding fallback check '%s' => '%s'\n",
606 langEnv, encoding);
607 #endif
608 }
609 if (setSysEncCode != TCL_OK) {
610 if (encoding == NULL) {
611 encoding = TCL_DEFAULT_ENCODING;
612 }
613
614 Tcl_SetSystemEncoding(NULL, encoding);
615 }
616
617 /*
618 * Initialize the C library's locale subsystem. This is required
619 * for input methods to work properly on X11. We only do this for
620 * LC_CTYPE because that's the necessary one, and we don't want to
621 * affect LC_TIME here. The side effect of setting the default
622 * locale should be to load any locale specific modules that are
623 * needed by X. [BUG: 5422 3345 4236 2522 2521].
624 * In HAVE_LANGINFO, this call is already done above.
625 */
626 #ifndef HAVE_LANGINFO
627 setlocale(LC_CTYPE, "");
628 #endif
629 }
630
631 /*
632 * In case the initial locale is not "C", ensure that the numeric
633 * processing is done in "C" locale regardless. This is needed because
634 * Tcl relies on routines like strtod, but should not have locale
635 * dependent behavior.
636 */
637
638 setlocale(LC_NUMERIC, "C");
639
640 /*
641 * Until the system encoding was actually set, the library path was
642 * actually in the native multi-byte encoding, and not really UTF-8
643 * as advertised. We cheated as follows:
644 *
645 * 1. It was safe to allow the Tcl_SetSystemEncoding() call to
646 * append the ASCII chars that make up the encoding's filename to
647 * the names (in the native encoding) of directories in the library
648 * path, since all Unix multi-byte encodings have ASCII in the
649 * beginning.
650 *
651 * 2. To open the encoding file, the native bytes in the file name
652 * were passed to the OS, without translating from UTF-8 to native,
653 * because the name was already in the native encoding.
654 *
655 * Now that the system encoding was actually successfully set,
656 * translate all the names in the library path to UTF-8. That way,
657 * next time we search the library path, we'll translate the names
658 * from UTF-8 to the system encoding which will be the native
659 * encoding.
660 */
661
662 pathPtr = TclGetLibraryPath();
663 if (pathPtr != NULL) {
664 int objc;
665 Tcl_Obj **objv;
666
667 objc = 0;
668 Tcl_ListObjGetElements(NULL, pathPtr, &objc, &objv);
669 for (i = 0; i < objc; i++) {
670 int length;
671 char *string;
672 Tcl_DString ds;
673
674 string = Tcl_GetStringFromObj(objv[i], &length);
675 Tcl_ExternalToUtfDString(NULL, string, length, &ds);
676 Tcl_SetStringObj(objv[i], Tcl_DStringValue(&ds),
677 Tcl_DStringLength(&ds));
678 Tcl_DStringFree(&ds);
679 }
680 }
681
682 libraryPathEncodingFixed = 1;
683 }
684
685 /* This is only ever called from the startup thread */
686 if (binaryEncoding == NULL) {
687 /*
688 * Keep the iso8859-1 encoding preloaded. The IO package uses
689 * it for gets on a binary channel.
690 */
691 binaryEncoding = Tcl_GetEncoding(NULL, "iso8859-1");
692 }
693 }
694
695 /*
696 *---------------------------------------------------------------------------
697 *
698 * TclpSetVariables --
699 *
700 * Performs platform-specific interpreter initialization related to
701 * the tcl_library and tcl_platform variables, and other platform-
702 * specific things.
703 *
704 * Results:
705 * None.
706 *
707 * Side effects:
708 * Sets "tclDefaultLibrary", "tcl_pkgPath", and "tcl_platform" Tcl
709 * variables.
710 *
711 *----------------------------------------------------------------------
712 */
713
714 void
715 TclpSetVariables(interp)
716 Tcl_Interp *interp;
717 {
718 #ifndef NO_UNAME
719 struct utsname name;
720 #endif
721 int unameOK;
722 CONST char *user;
723 Tcl_DString ds;
724
725 #ifdef HAVE_CFBUNDLE
726 char tclLibPath[MAXPATHLEN + 1];
727
728 if (Tcl_MacOSXGetLibraryPath(interp, MAXPATHLEN, tclLibPath) == TCL_OK) {
729 CONST char *str;
730 Tcl_DString ds;
731 CFBundleRef bundleRef;
732
733 Tcl_SetVar(interp, "tclDefaultLibrary", tclLibPath,
734 TCL_GLOBAL_ONLY);
735 Tcl_SetVar(interp, "tcl_pkgPath", tclLibPath,
736 TCL_GLOBAL_ONLY);
737 Tcl_SetVar(interp, "tcl_pkgPath", " ",
738 TCL_GLOBAL_ONLY | TCL_APPEND_VALUE);
739 str = TclGetEnv("DYLD_FRAMEWORK_PATH", &ds);
740 if ((str != NULL) && (str[0] != '\0')) {
741 char *p = Tcl_DStringValue(&ds);
742 /* convert DYLD_FRAMEWORK_PATH from colon to space separated */
743 do {
744 if(*p == ':') *p = ' ';
745 } while (*p++);
746 Tcl_SetVar(interp, "tcl_pkgPath", Tcl_DStringValue(&ds),
747 TCL_GLOBAL_ONLY | TCL_APPEND_VALUE);
748 Tcl_SetVar(interp, "tcl_pkgPath", " ",
749 TCL_GLOBAL_ONLY | TCL_APPEND_VALUE);
750 Tcl_DStringFree(&ds);
751 }
752 if ((bundleRef = CFBundleGetMainBundle())) {
753 CFURLRef frameworksURL;
754 Tcl_StatBuf statBuf;
755 if((frameworksURL = CFBundleCopyPrivateFrameworksURL(bundleRef))) {
756 if(CFURLGetFileSystemRepresentation(frameworksURL, TRUE,
757 tclLibPath, MAXPATHLEN) &&
758 ! TclOSstat(tclLibPath, &statBuf) &&
759 S_ISDIR(statBuf.st_mode)) {
760 Tcl_SetVar(interp, "tcl_pkgPath", tclLibPath,
761 TCL_GLOBAL_ONLY | TCL_APPEND_VALUE);
762 Tcl_SetVar(interp, "tcl_pkgPath", " ",
763 TCL_GLOBAL_ONLY | TCL_APPEND_VALUE);
764 }
765 CFRelease(frameworksURL);
766 }
767 if((frameworksURL = CFBundleCopySharedFrameworksURL(bundleRef))) {
768 if(CFURLGetFileSystemRepresentation(frameworksURL, TRUE,
769 tclLibPath, MAXPATHLEN) &&
770 ! TclOSstat(tclLibPath, &statBuf) &&
771 S_ISDIR(statBuf.st_mode)) {
772 Tcl_SetVar(interp, "tcl_pkgPath", tclLibPath,
773 TCL_GLOBAL_ONLY | TCL_APPEND_VALUE);
774 Tcl_SetVar(interp, "tcl_pkgPath", " ",
775 TCL_GLOBAL_ONLY | TCL_APPEND_VALUE);
776 }
777 CFRelease(frameworksURL);
778 }
779 }
780 Tcl_SetVar(interp, "tcl_pkgPath", pkgPath,
781 TCL_GLOBAL_ONLY | TCL_APPEND_VALUE);
782 } else
783 #endif /* HAVE_CFBUNDLE */
784 {
785 Tcl_SetVar(interp, "tclDefaultLibrary", defaultLibraryDir,
786 TCL_GLOBAL_ONLY);
787 Tcl_SetVar(interp, "tcl_pkgPath", pkgPath, TCL_GLOBAL_ONLY);
788 }
789
790 #ifdef DJGPP
791 Tcl_SetVar2(interp, "tcl_platform", "platform", "dos", TCL_GLOBAL_ONLY);
792 #else
793 Tcl_SetVar2(interp, "tcl_platform", "platform", "unix", TCL_GLOBAL_ONLY);
794 #endif
795 unameOK = 0;
796 #ifndef NO_UNAME
797 if (uname(&name) >= 0) {
798 CONST char *native;
799
800 unameOK = 1;
801
802 native = Tcl_ExternalToUtfDString(NULL, name.sysname, -1, &ds);
803 Tcl_SetVar2(interp, "tcl_platform", "os", native, TCL_GLOBAL_ONLY);
804 Tcl_DStringFree(&ds);
805
806 /*
807 * The following code is a special hack to handle differences in
808 * the way version information is returned by uname. On most
809 * systems the full version number is available in name.release.
810 * However, under AIX the major version number is in
811 * name.version and the minor version number is in name.release.
812 */
813
814 if ((strchr(name.release, '.') != NULL)
815 || !isdigit(UCHAR(name.version[0]))) { /* INTL: digit */
816 Tcl_SetVar2(interp, "tcl_platform", "osVersion", name.release,
817 TCL_GLOBAL_ONLY);
818 } else {
819 Tcl_SetVar2(interp, "tcl_platform", "osVersion", name.version,
820 TCL_GLOBAL_ONLY);
821 Tcl_SetVar2(interp, "tcl_platform", "osVersion", ".",
822 TCL_GLOBAL_ONLY|TCL_APPEND_VALUE);
823 Tcl_SetVar2(interp, "tcl_platform", "osVersion", name.release,
824 TCL_GLOBAL_ONLY|TCL_APPEND_VALUE);
825 }
826 Tcl_SetVar2(interp, "tcl_platform", "machine", name.machine,
827 TCL_GLOBAL_ONLY);
828 }
829 #endif
830 if (!unameOK) {
831 Tcl_SetVar2(interp, "tcl_platform", "os", "", TCL_GLOBAL_ONLY);
832 Tcl_SetVar2(interp, "tcl_platform", "osVersion", "", TCL_GLOBAL_ONLY);
833 Tcl_SetVar2(interp, "tcl_platform", "machine", "", TCL_GLOBAL_ONLY);
834 }
835
836 /*
837 * Copy USER or LOGNAME environment variable into tcl_platform(user)
838 */
839
840 Tcl_DStringInit(&ds);
841 user = TclGetEnv("USER", &ds);
842 if (user == NULL) {
843 user = TclGetEnv("LOGNAME", &ds);
844 if (user == NULL) {
845 user = "";
846 }
847 }
848 Tcl_SetVar2(interp, "tcl_platform", "user", user, TCL_GLOBAL_ONLY);
849 Tcl_DStringFree(&ds);
850
851 }
852
853 /*
854 *----------------------------------------------------------------------
855 *
856 * TclpFindVariable --
857 *
858 * Locate the entry in environ for a given name. On Unix this
859 * routine is case sensetive, on Windows this matches mixed case.
860 *
861 * Results:
862 * The return value is the index in environ of an entry with the
863 * name "name", or -1 if there is no such entry. The integer at
864 * *lengthPtr is filled in with the length of name (if a matching
865 * entry is found) or the length of the environ array (if no matching
866 * entry is found).
867 *
868 * Side effects:
869 * None.
870 *
871 *----------------------------------------------------------------------
872 */
873
874 int
875 TclpFindVariable(name, lengthPtr)
876 CONST char *name; /* Name of desired environment variable
877 * (native). */
878 int *lengthPtr; /* Used to return length of name (for
879 * successful searches) or number of non-NULL
880 * entries in environ (for unsuccessful
881 * searches). */
882 {
883 int i, result = -1;
884 register CONST char *env, *p1, *p2;
885 Tcl_DString envString;
886
887 Tcl_DStringInit(&envString);
888 for (i = 0, env = environ[i]; env != NULL; i++, env = environ[i]) {
889 p1 = Tcl_ExternalToUtfDString(NULL, env, -1, &envString);
890 p2 = name;
891
892 for (; *p2 == *p1; p1++, p2++) {
893 /* NULL loop body. */
894 }
895 if ((*p1 == '=') && (*p2 == '\0')) {
896 *lengthPtr = p2 - name;
897 result = i;
898 goto done;
899 }
900
901 Tcl_DStringFree(&envString);
902 }
903
904 *lengthPtr = i;
905
906 done:
907 Tcl_DStringFree(&envString);
908 return result;
909 }
910
911 /*
912 *----------------------------------------------------------------------
913 *
914 * Tcl_Init --
915 *
916 * This procedure is typically invoked by Tcl_AppInit procedures
917 * to find and source the "init.tcl" script, which should exist
918 * somewhere on the Tcl library path.
919 *
920 * Results:
921 * Returns a standard Tcl completion code and sets the interp's
922 * result if there is an error.
923 *
924 * Side effects:
925 * Depends on what's in the init.tcl script.
926 *
927 *----------------------------------------------------------------------
928 */
929
930 int
931 Tcl_Init(interp)
932 Tcl_Interp *interp; /* Interpreter to initialize. */
933 {
934 Tcl_Obj *pathPtr;
935
936 if (tclPreInitScript != NULL) {
937 if (Tcl_Eval(interp, tclPreInitScript) == TCL_ERROR) {
938 return (TCL_ERROR);
939 };
940 }
941
942 pathPtr = TclGetLibraryPath();
943 if (pathPtr == NULL) {
944 pathPtr = Tcl_NewObj();
945 }
946 Tcl_SetVar2Ex(interp, "tcl_libPath", NULL, pathPtr, TCL_GLOBAL_ONLY);
947 return Tcl_Eval(interp, initScript);
948 }
949
950 /*
951 *----------------------------------------------------------------------
952 *
953 * Tcl_SourceRCFile --
954 *
955 * This procedure is typically invoked by Tcl_Main of Tk_Main
956 * procedure to source an application specific rc file into the
957 * interpreter at startup time.
958 *
959 * Results:
960 * None.
961 *
962 * Side effects:
963 * Depends on what's in the rc script.
964 *
965 *----------------------------------------------------------------------
966 */
967
968 void
969 Tcl_SourceRCFile(interp)
970 Tcl_Interp *interp; /* Interpreter to source rc file into. */
971 {
972 Tcl_DString temp;
973 CONST char *fileName;
974 Tcl_Channel errChannel;
975
976 fileName = Tcl_GetVar(interp, "tcl_rcFileName", TCL_GLOBAL_ONLY);
977
978 if (fileName != NULL) {
979 Tcl_Channel c;
980 CONST char *fullName;
981
982 Tcl_DStringInit(&temp);
983 fullName = Tcl_TranslateFileName(interp, fileName, &temp);
984 if (fullName == NULL) {
985 /*
986 * Couldn't translate the file name (e.g. it referred to a
987 * bogus user or there was no HOME environment variable).
988 * Just do nothing.
989 */
990 } else {
991
992 /*
993 * Test for the existence of the rc file before trying to read it.
994 */
995
996 c = Tcl_OpenFileChannel(NULL, fullName, "r", 0);
997 if (c != (Tcl_Channel) NULL) {
998 Tcl_Close(NULL, c);
999 if (Tcl_EvalFile(interp, fullName) != TCL_OK) {
1000 errChannel = Tcl_GetStdChannel(TCL_STDERR);
1001 if (errChannel) {
1002 Tcl_WriteObj(errChannel, Tcl_GetObjResult(interp));
1003 Tcl_WriteChars(errChannel, "\n", 1);
1004 }
1005 }
1006 }
1007 }
1008 Tcl_DStringFree(&temp);
1009 }
1010 }
1011
1012 /*
1013 *----------------------------------------------------------------------
1014 *
1015 * TclpCheckStackSpace --
1016 *
1017 * Detect if we are about to blow the stack. Called before an
1018 * evaluation can happen when nesting depth is checked.
1019 *
1020 * Results:
1021 * 1 if there is enough stack space to continue; 0 if not.
1022 *
1023 * Side effects:
1024 * None.
1025 *
1026 *----------------------------------------------------------------------
1027 */
1028
1029 int
1030 TclpCheckStackSpace()
1031 {
1032 /*
1033 * This function is unimplemented on Unix platforms.
1034 */
1035
1036 return 1;
1037 }
1038
1039 #ifdef HAVE_CFBUNDLE
1040 /*
1041 *----------------------------------------------------------------------
1042 *
1043 * Tcl_MacOSXGetLibraryPath --
1044 *
1045 * If we have a bundle structure for the Tcl installation,
1046 * then check there first to see if we can find the libraries
1047 * there.
1048 *
1049 * Results:
1050 * TCL_OK if we have found the tcl library; TCL_ERROR otherwise.
1051 *
1052 * Side effects:
1053 * Same as for Tcl_MacOSXOpenVersionedBundleResources.
1054 *
1055 *----------------------------------------------------------------------
1056 */
1057 static int Tcl_MacOSXGetLibraryPath(Tcl_Interp *interp, int maxPathLen, char *tclLibPath)
1058 {
1059 int foundInFramework = TCL_ERROR;
1060 if (strcmp(defaultLibraryDir, "@TCL_IN_FRAMEWORK@") == 0) {
1061 foundInFramework = Tcl_MacOSXOpenVersionedBundleResources(interp,
1062 "com.tcltk.tcllibrary", TCL_VERSION, 0, maxPathLen, tclLibPath);
1063 }
1064 return foundInFramework;
1065 }
1066 #endif /* HAVE_CFBUNDLE */
1067
1068