Debian Codegraph @

unix/tclUnixInit.c

raw
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 Scriptics Corporation.
8 * All rights reserved.
9 */
10
11 #include "tclInt.h"
12 #include <stddef.h>
13 #include <locale.h>
14 #ifdef HAVE_LANGINFO
15 # include <langinfo.h>
16 # ifdef __APPLE__
17 # if defined(HAVE_WEAK_IMPORT) && MAC_OS_X_VERSION_MIN_REQUIRED < 1030
18 /* Support for weakly importing nl_langinfo on Darwin. */
19 # define WEAK_IMPORT_NL_LANGINFO
20 extern char *nl_langinfo(nl_item) WEAK_IMPORT_ATTRIBUTE;
21 # endif
22 # endif
23 #endif
24 #include <sys/resource.h>
25 #if defined(__FreeBSD__) && defined(__GNUC__)
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 #ifdef __CYGWIN__
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 DLLIMPORT extern unsigned char GetVersionExW(void *);
40 DLLIMPORT extern void *GetModuleHandleW(const void *);
41 DLLIMPORT extern void FreeLibrary(void *);
42 DLLIMPORT extern void *GetProcAddress(void *, const char *);
43 DLLIMPORT extern void GetSystemInfo(void *);
44 #ifdef __cplusplus
45 }
46 #endif
47
48 #define NUMPROCESSORS 15
49 static const char *const processors[NUMPROCESSORS] = {
50 "i686", "mips", "alpha", "ppc", "shx", "arm", "ia64", "alpha64", "msil",
51 "x86_64", "ia32_on_win64", "neutral", "arm64", "arm32_on_win64", "ia32_on_arm64"
52 };
53
54 typedef struct {
55 union {
56 unsigned int dwOemId;
57 struct {
58 int wProcessorArchitecture;
59 int wReserved;
60 };
61 };
62 unsigned int dwPageSize;
63 void *lpMinimumApplicationAddress;
64 void *lpMaximumApplicationAddress;
65 void *dwActiveProcessorMask;
66 unsigned int dwNumberOfProcessors;
67 unsigned int dwProcessorType;
68 unsigned int dwAllocationGranularity;
69 int wProcessorLevel;
70 int wProcessorRevision;
71 } SYSTEM_INFO;
72
73 typedef struct {
74 unsigned int dwOSVersionInfoSize;
75 unsigned int dwMajorVersion;
76 unsigned int dwMinorVersion;
77 unsigned int dwBuildNumber;
78 unsigned int dwPlatformId;
79 wchar_t szCSDVersion[128];
80 } OSVERSIONINFOW;
81 #endif
82
83 #ifdef HAVE_COREFOUNDATION
84 #include <CoreFoundation/CoreFoundation.h>
85 #endif
86
87 /*
88 * Tcl tries to use standard and homebrew methods to guess the right encoding
89 * on the platform. However, there is always a final fallback, and this value
90 * is it. Make sure it is a real Tcl encoding.
91 */
92
93 #ifndef TCL_DEFAULT_ENCODING
94 #define TCL_DEFAULT_ENCODING "iso8859-1"
95 #endif
96
97 /*
98 * Default directory in which to look for Tcl library scripts. The symbol is
99 * defined by Makefile.
100 */
101
102 static char defaultLibraryDir[sizeof(TCL_LIBRARY)+200] = TCL_LIBRARY;
103
104 /*
105 * Directory in which to look for packages (each package is typically
106 * installed as a subdirectory of this directory). The symbol is defined by
107 * Makefile.
108 */
109
110 static char pkgPath[sizeof(TCL_PACKAGE_PATH)+200] = TCL_PACKAGE_PATH;
111
112 /*
113 * The following table is used to map from Unix locale strings to encoding
114 * files. If HAVE_LANGINFO is defined, then this is a fallback table when the
115 * result from nl_langinfo isn't a recognized encoding. Otherwise this is the
116 * first list checked for a mapping from env encoding to Tcl encoding name.
117 */
118
119 typedef struct {
120 const char *lang;
121 const char *encoding;
122 } LocaleTable;
123
124 /*
125 * The table below is sorted for the sake of doing binary searches on it. The
126 * indenting reflects different categories of data. The leftmost data
127 * represent the encoding names directly implemented by data files in Tcl's
128 * default encoding directory. Indented by one TAB are the encoding names that
129 * are common alternative spellings. Indented by two TABs are the accumulated
130 * "bug fixes" that have been added to deal with the wide variability seen
131 * among existing platforms.
132 */
133
134 static const LocaleTable localeTable[] = {
135 {"", "iso8859-1"},
136 {"ansi-1251", "cp1251"},
137 {"ansi_x3.4-1968", "iso8859-1"},
138 {"ascii", "ascii"},
139 {"big5", "big5"},
140 {"cp1250", "cp1250"},
141 {"cp1251", "cp1251"},
142 {"cp1252", "cp1252"},
143 {"cp1253", "cp1253"},
144 {"cp1254", "cp1254"},
145 {"cp1255", "cp1255"},
146 {"cp1256", "cp1256"},
147 {"cp1257", "cp1257"},
148 {"cp1258", "cp1258"},
149 {"cp437", "cp437"},
150 {"cp737", "cp737"},
151 {"cp775", "cp775"},
152 {"cp850", "cp850"},
153 {"cp852", "cp852"},
154 {"cp855", "cp855"},
155 {"cp857", "cp857"},
156 {"cp860", "cp860"},
157 {"cp861", "cp861"},
158 {"cp862", "cp862"},
159 {"cp863", "cp863"},
160 {"cp864", "cp864"},
161 {"cp865", "cp865"},
162 {"cp866", "cp866"},
163 {"cp869", "cp869"},
164 {"cp874", "cp874"},
165 {"cp932", "cp932"},
166 {"cp936", "cp936"},
167 {"cp949", "cp949"},
168 {"cp950", "cp950"},
169 {"dingbats", "dingbats"},
170 {"ebcdic", "ebcdic"},
171 {"euc-cn", "euc-cn"},
172 {"euc-jp", "euc-jp"},
173 {"euc-kr", "euc-kr"},
174 {"eucjp", "euc-jp"},
175 {"euckr", "euc-kr"},
176 {"euctw", "euc-cn"},
177 {"gb12345", "gb12345"},
178 {"gb1988", "gb1988"},
179 {"gb2312", "gb2312"},
180 {"gb2312-1980", "gb2312"},
181 {"gb2312-raw", "gb2312-raw"},
182 {"greek8", "cp869"},
183 {"ibm1250", "cp1250"},
184 {"ibm1251", "cp1251"},
185 {"ibm1252", "cp1252"},
186 {"ibm1253", "cp1253"},
187 {"ibm1254", "cp1254"},
188 {"ibm1255", "cp1255"},
189 {"ibm1256", "cp1256"},
190 {"ibm1257", "cp1257"},
191 {"ibm1258", "cp1258"},
192 {"ibm437", "cp437"},
193 {"ibm737", "cp737"},
194 {"ibm775", "cp775"},
195 {"ibm850", "cp850"},
196 {"ibm852", "cp852"},
197 {"ibm855", "cp855"},
198 {"ibm857", "cp857"},
199 {"ibm860", "cp860"},
200 {"ibm861", "cp861"},
201 {"ibm862", "cp862"},
202 {"ibm863", "cp863"},
203 {"ibm864", "cp864"},
204 {"ibm865", "cp865"},
205 {"ibm866", "cp866"},
206 {"ibm869", "cp869"},
207 {"ibm874", "cp874"},
208 {"ibm932", "cp932"},
209 {"ibm936", "cp936"},
210 {"ibm949", "cp949"},
211 {"ibm950", "cp950"},
212 {"iso-2022", "iso2022"},
213 {"iso-2022-jp", "iso2022-jp"},
214 {"iso-2022-kr", "iso2022-kr"},
215 {"iso-8859-1", "iso8859-1"},
216 {"iso-8859-10", "iso8859-10"},
217 {"iso-8859-13", "iso8859-13"},
218 {"iso-8859-14", "iso8859-14"},
219 {"iso-8859-15", "iso8859-15"},
220 {"iso-8859-16", "iso8859-16"},
221 {"iso-8859-2", "iso8859-2"},
222 {"iso-8859-3", "iso8859-3"},
223 {"iso-8859-4", "iso8859-4"},
224 {"iso-8859-5", "iso8859-5"},
225 {"iso-8859-6", "iso8859-6"},
226 {"iso-8859-7", "iso8859-7"},
227 {"iso-8859-8", "iso8859-8"},
228 {"iso-8859-9", "iso8859-9"},
229 {"iso2022", "iso2022"},
230 {"iso2022-jp", "iso2022-jp"},
231 {"iso2022-kr", "iso2022-kr"},
232 {"iso8859-1", "iso8859-1"},
233 {"iso8859-10", "iso8859-10"},
234 {"iso8859-13", "iso8859-13"},
235 {"iso8859-14", "iso8859-14"},
236 {"iso8859-15", "iso8859-15"},
237 {"iso8859-16", "iso8859-16"},
238 {"iso8859-2", "iso8859-2"},
239 {"iso8859-3", "iso8859-3"},
240 {"iso8859-4", "iso8859-4"},
241 {"iso8859-5", "iso8859-5"},
242 {"iso8859-6", "iso8859-6"},
243 {"iso8859-7", "iso8859-7"},
244 {"iso8859-8", "iso8859-8"},
245 {"iso8859-9", "iso8859-9"},
246 {"iso88591", "iso8859-1"},
247 {"iso885915", "iso8859-15"},
248 {"iso88592", "iso8859-2"},
249 {"iso88595", "iso8859-5"},
250 {"iso88596", "iso8859-6"},
251 {"iso88597", "iso8859-7"},
252 {"iso88598", "iso8859-8"},
253 {"iso88599", "iso8859-9"},
254 #ifdef hpux
255 {"ja", "shiftjis"},
256 #else
257 {"ja", "euc-jp"},
258 #endif
259 {"ja_jp", "euc-jp"},
260 {"ja_jp.euc", "euc-jp"},
261 {"ja_jp.eucjp", "euc-jp"},
262 {"ja_jp.jis", "iso2022-jp"},
263 {"ja_jp.mscode", "shiftjis"},
264 {"ja_jp.sjis", "shiftjis"},
265 {"ja_jp.ujis", "euc-jp"},
266 {"japan", "euc-jp"},
267 #ifdef hpux
268 {"japanese", "shiftjis"},
269 #else
270 {"japanese", "euc-jp"},
271 #endif
272 {"japanese-sjis", "shiftjis"},
273 {"japanese-ujis", "euc-jp"},
274 {"japanese.euc", "euc-jp"},
275 {"japanese.sjis", "shiftjis"},
276 {"jis0201", "jis0201"},
277 {"jis0208", "jis0208"},
278 {"jis0212", "jis0212"},
279 {"jp_jp", "shiftjis"},
280 {"ko", "euc-kr"},
281 {"ko_kr", "euc-kr"},
282 {"ko_kr.euc", "euc-kr"},
283 {"ko_kw.euckw", "euc-kr"},
284 {"koi8-r", "koi8-r"},
285 {"koi8-u", "koi8-u"},
286 {"korean", "euc-kr"},
287 {"ksc5601", "ksc5601"},
288 {"maccenteuro", "macCentEuro"},
289 {"maccroatian", "macCroatian"},
290 {"maccyrillic", "macCyrillic"},
291 {"macdingbats", "macDingbats"},
292 {"macgreek", "macGreek"},
293 {"maciceland", "macIceland"},
294 {"macjapan", "macJapan"},
295 {"macroman", "macRoman"},
296 {"macromania", "macRomania"},
297 {"macthai", "macThai"},
298 {"macturkish", "macTurkish"},
299 {"macukraine", "macUkraine"},
300 {"roman8", "iso8859-1"},
301 {"ru", "iso8859-5"},
302 {"ru_ru", "iso8859-5"},
303 {"ru_su", "iso8859-5"},
304 {"shiftjis", "shiftjis"},
305 {"sjis", "shiftjis"},
306 {"symbol", "symbol"},
307 {"tis-620", "tis-620"},
308 {"tis620", "tis-620"},
309 {"turkish8", "cp857"},
310 {"utf8", "utf-8"},
311 {"zh", "cp936"},
312 {"zh_cn.gb2312", "euc-cn"},
313 {"zh_cn.gbk", "euc-cn"},
314 {"zh_cz.gb2312", "euc-cn"},
315 {"zh_tw", "euc-tw"},
316 {"zh_tw.big5", "big5"},
317 };
318
319 #ifdef HAVE_COREFOUNDATION
320 static int MacOSXGetLibraryPath(Tcl_Interp *interp,
321 int maxPathLen, char *tclLibPath);
322 #endif /* HAVE_COREFOUNDATION */
323 #if defined(__APPLE__) && (defined(TCL_LOAD_FROM_MEMORY) || ( \
324 defined(MAC_OS_X_VERSION_MIN_REQUIRED) && ( \
325 (defined(TCL_THREADS) && MAC_OS_X_VERSION_MIN_REQUIRED < 1030) || \
326 (defined(__LP64__) && MAC_OS_X_VERSION_MIN_REQUIRED < 1050) || \
327 (defined(HAVE_COREFOUNDATION) && MAC_OS_X_VERSION_MIN_REQUIRED < 1050)\
328 )))
329 /*
330 * Need to check Darwin release at runtime in tclUnixFCmd.c and tclLoadDyld.c:
331 * initialize release global at startup from uname().
332 */
333 #define GET_DARWIN_RELEASE 1
334 MODULE_SCOPE long tclMacOSXDarwinRelease;
335 long tclMacOSXDarwinRelease = 0;
336 #endif
337
338 /*
339 *---------------------------------------------------------------------------
340 *
341 * TclpInitPlatform --
342 *
343 * Initialize all the platform-dependent things like signals and
344 * floating-point error handling.
345 *
346 * Called at process initialization time.
347 *
348 * Results:
349 * None.
350 *
351 * Side effects:
352 * None.
353 *
354 *---------------------------------------------------------------------------
355 */
356
357 void
358 TclpInitPlatform(void)
359 {
360 #ifdef DJGPP
361 tclPlatform = TCL_PLATFORM_WINDOWS;
362 #else
363 tclPlatform = TCL_PLATFORM_UNIX;
364 #endif
365
366 /*
367 * Make sure, that the standard FDs exist. [Bug 772288]
368 */
369
370 if (TclOSseek(0, 0, SEEK_CUR) == -1 && errno == EBADF) {
371 open("/dev/null", O_RDONLY);
372 }
373 if (TclOSseek(1, 0, SEEK_CUR) == -1 && errno == EBADF) {
374 open("/dev/null", O_WRONLY);
375 }
376 if (TclOSseek(2, 0, SEEK_CUR) == -1 && errno == EBADF) {
377 open("/dev/null", O_WRONLY);
378 }
379
380 /*
381 * The code below causes SIGPIPE (broken pipe) errors to be ignored. This
382 * is needed so that Tcl processes don't die if they create child
383 * processes (e.g. using "exec" or "open") that terminate prematurely.
384 * The signal handler is only set up when the first interpreter is
385 * created; after this the application can override the handler with a
386 * different one of its own, if it wants.
387 */
388
389 #ifdef SIGPIPE
390 (void) signal(SIGPIPE, SIG_IGN);
391 #endif /* SIGPIPE */
392
393 #if defined(__FreeBSD__) && defined(__GNUC__)
394 /*
395 * Adjust the rounding mode to be more conventional. Note that FreeBSD
396 * only provides the __fpsetreg() used by the following two for the GNU
397 * Compiler. When using, say, Intel's icc they break. (Partially based on
398 * patch in BSD ports system from [email protected])
399 */
400
401 fpsetround(FP_RN);
402 (void) fpsetmask(0L);
403 #endif
404
405 #if defined(__bsdi__) && (_BSDI_VERSION > 199501)
406 /*
407 * Find local symbols. Don't report an error if we fail.
408 */
409
410 (void) dlopen(NULL, RTLD_NOW); /* INTL: Native. */
411 #endif
412
413 /*
414 * Initialize the C library's locale subsystem. This is required for input
415 * methods to work properly on X11. We only do this for LC_CTYPE because
416 * that's the necessary one, and we don't want to affect LC_TIME here.
417 * The side effect of setting the default locale should be to load any
418 * locale specific modules that are needed by X. [BUG: 5422 3345 4236 2522
419 * 2521].
420 */
421
422 setlocale(LC_CTYPE, "");
423
424 /*
425 * In case the initial locale is not "C", ensure that the numeric
426 * processing is done in "C" locale regardless. This is needed because Tcl
427 * relies on routines like strtod, but should not have locale dependent
428 * behavior.
429 */
430
431 setlocale(LC_NUMERIC, "C");
432
433 #ifdef GET_DARWIN_RELEASE
434 {
435 struct utsname name;
436
437 if (!uname(&name)) {
438 tclMacOSXDarwinRelease = strtol(name.release, NULL, 10);
439 }
440 }
441 #endif
442 }
443
444 /*
445 *---------------------------------------------------------------------------
446 *
447 * TclpInitLibraryPath --
448 *
449 * This is the fallback routine that sets the library path if the
450 * application has not set one by the first time it is needed.
451 *
452 * Results:
453 * None.
454 *
455 * Side effects:
456 * Sets the library path to an initial value.
457 *
458 *-------------------------------------------------------------------------
459 */
460
461 void
462 TclpInitLibraryPath(
463 char **valuePtr,
464 int *lengthPtr,
465 Tcl_Encoding *encodingPtr)
466 {
467 #define LIBRARY_SIZE 32
468 Tcl_Obj *pathPtr, *objPtr;
469 const char *str;
470 Tcl_DString buffer;
471
472 TclNewObj(pathPtr);
473
474 /*
475 * Look for the library relative to the TCL_LIBRARY env variable. If the
476 * last dirname in the TCL_LIBRARY path does not match the last dirname in
477 * the installLib variable, use the last dir name of installLib in
478 * addition to the original TCL_LIBRARY path.
479 */
480
481 str = getenv("TCL_LIBRARY"); /* INTL: Native. */
482 if (str != NULL) {
483 Tcl_ExternalToUtfDString(NULL, str, -1, &buffer);
484 str = Tcl_DStringValue(&buffer);
485 } else {
486 Tcl_DStringInit(&buffer);
487 }
488
489 if ((str != NULL) && (str[0] != '\0')) {
490 Tcl_DString ds;
491 int pathc;
492 const char **pathv;
493 char installLib[LIBRARY_SIZE];
494
495 Tcl_DStringInit(&ds);
496
497 /*
498 * Initialize the substrings used when locating an executable. The
499 * installLib variable computes the path as though the executable is
500 * installed.
501 */
502
503 snprintf(installLib, sizeof(installLib), "lib/tcl%s", TCL_VERSION);
504
505 /*
506 * If TCL_LIBRARY is set, search there.
507 */
508
509 Tcl_ListObjAppendElement(NULL, pathPtr, Tcl_NewStringObj(str, -1));
510
511 Tcl_SplitPath(str, &pathc, &pathv);
512 if ((pathc > 0) && (strcasecmp(installLib + 4, pathv[pathc-1]) != 0)) {
513 /*
514 * If TCL_LIBRARY is set but refers to a different tcl
515 * installation than the current version, try fiddling with the
516 * specified directory to make it refer to this installation by
517 * removing the old "tclX.Y" and substituting the current version
518 * string.
519 */
520
521 pathv[pathc - 1] = installLib + 4;
522 str = Tcl_JoinPath(pathc, pathv, &ds);
523 Tcl_ListObjAppendElement(NULL, pathPtr, TclDStringToObj(&ds));
524 }
525 ckfree(pathv);
526 }
527
528 /*
529 * Finally, look for the library relative to the compiled-in path. This is
530 * needed when users install Tcl with an exec-prefix that is different
531 * from the prefix.
532 */
533
534 {
535 #ifdef HAVE_COREFOUNDATION
536 char tclLibPath[MAXPATHLEN + 1];
537
538 if (MacOSXGetLibraryPath(NULL, MAXPATHLEN, tclLibPath) == TCL_OK) {
539 str = tclLibPath;
540 } else
541 #endif /* HAVE_COREFOUNDATION */
542 {
543 /*
544 * TODO: Pull this value from the TIP 59 table.
545 */
546
547 str = defaultLibraryDir;
548 }
549 if (str[0] != '\0') {
550 objPtr = Tcl_NewStringObj(str, -1);
551 Tcl_ListObjAppendElement(NULL, pathPtr, objPtr);
552 }
553 }
554 Tcl_DStringFree(&buffer);
555
556 *encodingPtr = Tcl_GetEncoding(NULL, NULL);
557 str = Tcl_GetStringFromObj(pathPtr, lengthPtr);
558 *valuePtr = (char *)ckalloc(*lengthPtr + 1);
559 memcpy(*valuePtr, str, *lengthPtr + 1);
560 Tcl_DecrRefCount(pathPtr);
561 }
562
563 /*
564 *---------------------------------------------------------------------------
565 *
566 * TclpSetInitialEncodings --
567 *
568 * Based on the locale, determine the encoding of the operating system
569 * and the default encoding for newly opened files.
570 *
571 * Called at process initialization time, and part way through startup,
572 * we verify that the initial encodings were correctly setup. Depending
573 * on Tcl's environment, there may not have been enough information first
574 * time through (above).
575 *
576 * Results:
577 * None.
578 *
579 * Side effects:
580 * The Tcl library path is converted from native encoding to UTF-8, on
581 * the first call, and the encodings may be changed on first or second
582 * call.
583 *
584 *---------------------------------------------------------------------------
585 */
586
587 void
588 TclpSetInitialEncodings(void)
589 {
590 Tcl_DString encodingName;
591 Tcl_SetSystemEncoding(NULL,
592 Tcl_GetEncodingNameFromEnvironment(&encodingName));
593 Tcl_DStringFree(&encodingName);
594 }
595
596 static const char *
597 SearchKnownEncodings(
598 const char *encoding)
599 {
600 int left = 0;
601 int right = sizeof(localeTable)/sizeof(LocaleTable);
602
603 /* Here, search for i in the interval left <= i < right. */
604 while (left < right) {
605 int test = (left + right)/2;
606 int code = strcmp(localeTable[test].lang, encoding);
607
608 if (code == 0) {
609 /* Found it at i == test. */
610 return localeTable[test].encoding;
611 }
612 if (code < 0) {
613 /* Restrict the search to the interval test < i < right. */
614 left = test+1;
615 } else {
616 /* Restrict the search to the interval left <= i < test. */
617 right = test;
618 }
619 }
620 return NULL;
621 }
622
623 const char *
624 Tcl_GetEncodingNameFromEnvironment(
625 Tcl_DString *bufPtr)
626 {
627 const char *encoding;
628 const char *knownEncoding;
629
630 Tcl_DStringInit(bufPtr);
631
632 /*
633 * Determine the current encoding from the LC_* or LANG environment
634 * variables. We previously used setlocale() to determine the locale, but
635 * this does not work on some systems (e.g. Linux/i386 RH 5.0).
636 */
637
638 #ifdef HAVE_LANGINFO
639 if (
640 #ifdef WEAK_IMPORT_NL_LANGINFO
641 nl_langinfo != NULL &&
642 #endif
643 setlocale(LC_CTYPE, "") != NULL) {
644 Tcl_DString ds;
645
646 /*
647 * Use a DString so we can modify case.
648 */
649
650 Tcl_DStringInit(&ds);
651 encoding = Tcl_DStringAppend(&ds, nl_langinfo(CODESET), -1);
652 Tcl_UtfToLower(Tcl_DStringValue(&ds));
653 knownEncoding = SearchKnownEncodings(encoding);
654 if (knownEncoding != NULL) {
655 Tcl_DStringAppend(bufPtr, knownEncoding, -1);
656 } else if (NULL != Tcl_GetEncoding(NULL, encoding)) {
657 Tcl_DStringAppend(bufPtr, encoding, -1);
658 }
659 Tcl_DStringFree(&ds);
660 if (Tcl_DStringLength(bufPtr)) {
661 return Tcl_DStringValue(bufPtr);
662 }
663 }
664 #endif /* HAVE_LANGINFO */
665
666 /*
667 * Classic fallback check. This tries a homebrew algorithm to determine
668 * what encoding should be used based on env vars.
669 */
670
671 encoding = getenv("LC_ALL");
672
673 if (encoding == NULL || encoding[0] == '\0') {
674 encoding = getenv("LC_CTYPE");
675 }
676 if (encoding == NULL || encoding[0] == '\0') {
677 encoding = getenv("LANG");
678 }
679 if (encoding == NULL || encoding[0] == '\0') {
680 encoding = NULL;
681 }
682
683 if (encoding != NULL) {
684 const char *p;
685 Tcl_DString ds;
686
687 Tcl_DStringInit(&ds);
688 p = encoding;
689 encoding = Tcl_DStringAppend(&ds, p, -1);
690 Tcl_UtfToLower(Tcl_DStringValue(&ds));
691
692 knownEncoding = SearchKnownEncodings(encoding);
693 if (knownEncoding != NULL) {
694 Tcl_DStringAppend(bufPtr, knownEncoding, -1);
695 } else if (NULL != Tcl_GetEncoding(NULL, encoding)) {
696 Tcl_DStringAppend(bufPtr, encoding, -1);
697 }
698 if (Tcl_DStringLength(bufPtr)) {
699 Tcl_DStringFree(&ds);
700 return Tcl_DStringValue(bufPtr);
701 }
702
703 /*
704 * We didn't recognize the full value as an encoding name. If there is
705 * an encoding subfield, we can try to guess from that.
706 */
707
708 for (p = encoding; *p != '\0'; p++) {
709 if (*p == '.') {
710 p++;
711 break;
712 }
713 }
714 if (*p != '\0') {
715 knownEncoding = SearchKnownEncodings(p);
716 if (knownEncoding != NULL) {
717 Tcl_DStringAppend(bufPtr, knownEncoding, -1);
718 } else if (NULL != Tcl_GetEncoding(NULL, p)) {
719 Tcl_DStringAppend(bufPtr, p, -1);
720 }
721 }
722 Tcl_DStringFree(&ds);
723 if (Tcl_DStringLength(bufPtr)) {
724 return Tcl_DStringValue(bufPtr);
725 }
726 }
727 return Tcl_DStringAppend(bufPtr, TCL_DEFAULT_ENCODING, -1);
728 }
729
730 /*
731 *---------------------------------------------------------------------------
732 *
733 * TclpSetVariables --
734 *
735 * Performs platform-specific interpreter initialization related to the
736 * tcl_library and tcl_platform variables, and other platform-specific
737 * things.
738 *
739 * Results:
740 * None.
741 *
742 * Side effects:
743 * Sets "tclDefaultLibrary", "tcl_pkgPath", and "tcl_platform" Tcl
744 * variables.
745 *
746 *----------------------------------------------------------------------
747 */
748
749 #if defined(HAVE_COREFOUNDATION) && MAC_OS_X_VERSION_MAX_ALLOWED > 1020
750 /*
751 * Helper because whether CFLocaleCopyCurrent and CFLocaleGetIdentifier are
752 * strongly or weakly bound varies by version of OSX, triggering warnings.
753 */
754
755 static inline void
756 InitMacLocaleInfoVar(
757 CFLocaleRef (*localeCopyCurrent)(void),
758 CFStringRef (*localeGetIdentifier)(CFLocaleRef),
759 Tcl_Interp *interp)
760 {
761 CFLocaleRef localeRef;
762 CFStringRef locale;
763 char loc[256];
764
765 if (localeCopyCurrent == NULL || localeGetIdentifier == NULL) {
766 return;
767 }
768
769 localeRef = localeCopyCurrent();
770 if (!localeRef) {
771 return;
772 }
773
774 locale = localeGetIdentifier(localeRef);
775 if (locale && CFStringGetCString(locale, loc, 256,
776 kCFStringEncodingUTF8)) {
777 if (!Tcl_CreateNamespace(interp, "::tcl::mac", NULL, NULL)) {
778 Tcl_ResetResult(interp);
779 }
780 Tcl_SetVar2(interp, "::tcl::mac::locale", NULL, loc, TCL_GLOBAL_ONLY);
781 }
782 CFRelease(localeRef);
783 }
784 #endif /*defined(HAVE_COREFOUNDATION) && MAC_OS_X_VERSION_MAX_ALLOWED > 1020*/
785
786 void
787 TclpSetVariables(
788 Tcl_Interp *interp)
789 {
790 #ifdef __CYGWIN__
791 SYSTEM_INFO sysInfo;
792 static OSVERSIONINFOW osInfo;
793 static int osInfoInitialized = 0;
794 char buffer[TCL_INTEGER_SPACE * 2];
795 #elif !defined(NO_UNAME)
796 struct utsname name;
797 #endif
798 int unameOK;
799 const char *p, *q;
800 Tcl_Obj *pkgListObj = Tcl_NewObj();
801
802 #ifdef HAVE_COREFOUNDATION
803 char tclLibPath[MAXPATHLEN + 1];
804
805 /*
806 * Set msgcat fallback locale to current CFLocale identifier.
807 */
808
809 #if MAC_OS_X_VERSION_MAX_ALLOWED > 1020
810 InitMacLocaleInfoVar(CFLocaleCopyCurrent, CFLocaleGetIdentifier, interp);
811 #endif /* MAC_OS_X_VERSION_MAX_ALLOWED > 1020 */
812
813 if (MacOSXGetLibraryPath(interp, MAXPATHLEN, tclLibPath) == TCL_OK) {
814 const char *str;
815 CFBundleRef bundleRef;
816 Tcl_DString ds;
817
818 Tcl_SetVar2(interp, "tclDefaultLibrary", NULL, tclLibPath, TCL_GLOBAL_ONLY);
819 Tcl_ListObjAppendElement(NULL, pkgListObj, Tcl_NewStringObj(tclLibPath, -1));
820 str = TclGetEnv("DYLD_FRAMEWORK_PATH", &ds);
821 if ((str != NULL) && (str[0] != '\0')) {
822 p = Tcl_DStringValue(&ds);
823 while ((q = strchr(p, ':')) != NULL) {
824 Tcl_ListObjAppendElement(NULL, pkgListObj, Tcl_NewStringObj(p, q-p));
825 p = q+1;
826 }
827 if (*p) {
828 Tcl_ListObjAppendElement(NULL, pkgListObj, Tcl_NewStringObj(p, -1));
829 }
830 Tcl_DStringFree(&ds);
831 }
832 bundleRef = CFBundleGetMainBundle();
833 if (bundleRef) {
834 CFURLRef frameworksURL;
835 Tcl_StatBuf statBuf;
836
837 frameworksURL = CFBundleCopyPrivateFrameworksURL(bundleRef);
838 if (frameworksURL) {
839 if (CFURLGetFileSystemRepresentation(frameworksURL, TRUE,
840 (unsigned char*) tclLibPath, MAXPATHLEN) &&
841 ! TclOSstat(tclLibPath, &statBuf) &&
842 S_ISDIR(statBuf.st_mode)) {
843 Tcl_ListObjAppendElement(NULL, pkgListObj, Tcl_NewStringObj(tclLibPath, -1));
844 }
845 CFRelease(frameworksURL);
846 }
847 frameworksURL = CFBundleCopySharedFrameworksURL(bundleRef);
848 if (frameworksURL) {
849 if (CFURLGetFileSystemRepresentation(frameworksURL, TRUE,
850 (unsigned char*) tclLibPath, MAXPATHLEN) &&
851 ! TclOSstat(tclLibPath, &statBuf) &&
852 S_ISDIR(statBuf.st_mode)) {
853 Tcl_ListObjAppendElement(NULL, pkgListObj, Tcl_NewStringObj(tclLibPath, -1));
854 }
855 CFRelease(frameworksURL);
856 }
857 }
858 }
859 #endif /* HAVE_COREFOUNDATION */
860 p = pkgPath;
861 while ((q = strchr(p, ':')) != NULL) {
862 Tcl_ListObjAppendElement(NULL, pkgListObj, Tcl_NewStringObj(p, q-p));
863 p = q+1;
864 }
865 if (*p) {
866 Tcl_ListObjAppendElement(NULL, pkgListObj, Tcl_NewStringObj(p, -1));
867 }
868 Tcl_ObjSetVar2(interp, Tcl_NewStringObj("tcl_pkgPath", -1), NULL, pkgListObj, TCL_GLOBAL_ONLY);
869
870 #ifdef DJGPP
871 Tcl_SetVar2(interp, "tcl_platform", "platform", "dos", TCL_GLOBAL_ONLY);
872 #else
873 Tcl_SetVar2(interp, "tcl_platform", "platform", "unix", TCL_GLOBAL_ONLY);
874 #endif
875
876 unameOK = 0;
877 #ifdef __CYGWIN__
878 unameOK = 1;
879 if (!osInfoInitialized) {
880 void *handle = GetModuleHandleW(L"NTDLL");
881 int(*getversion)(void *) =
882 (int(*)(void *))GetProcAddress(handle, "RtlGetVersion");
883 osInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
884 if (!getversion || getversion(&osInfo)) {
885 GetVersionExW(&osInfo);
886 }
887 osInfoInitialized = 1;
888 }
889
890 GetSystemInfo(&sysInfo);
891
892 if (osInfo.dwMajorVersion == 10 && osInfo.dwBuildNumber >= 22000) {
893 osInfo.dwMajorVersion = 11;
894 }
895 Tcl_SetVar2(interp, "tcl_platform", "os", "Windows NT", TCL_GLOBAL_ONLY);
896 snprintf(buffer, sizeof(buffer), "%d.%d", osInfo.dwMajorVersion, osInfo.dwMinorVersion);
897 Tcl_SetVar2(interp, "tcl_platform", "osVersion", buffer, TCL_GLOBAL_ONLY);
898 if (sysInfo.wProcessorArchitecture < NUMPROCESSORS) {
899 Tcl_SetVar2(interp, "tcl_platform", "machine",
900 processors[sysInfo.wProcessorArchitecture],
901 TCL_GLOBAL_ONLY);
902 }
903
904 #elif !defined NO_UNAME
905 if (uname(&name) >= 0) {
906 const char *native;
907 Tcl_DString ds;
908
909 unameOK = 1;
910
911 native = Tcl_ExternalToUtfDString(NULL, name.sysname, -1, &ds);
912 Tcl_SetVar2(interp, "tcl_platform", "os", native, TCL_GLOBAL_ONLY);
913 Tcl_DStringFree(&ds);
914
915 /*
916 * The following code is a special hack to handle differences in the
917 * way version information is returned by uname. On most systems the
918 * full version number is available in name.release. However, under
919 * AIX the major version number is in name.version and the minor
920 * version number is in name.release.
921 */
922
923 if ((strchr(name.release, '.') != NULL)
924 || !isdigit(UCHAR(name.version[0]))) { /* INTL: digit */
925 Tcl_SetVar2(interp, "tcl_platform", "osVersion", name.release,
926 TCL_GLOBAL_ONLY);
927 } else {
928 #ifdef DJGPP
929 /*
930 * For some obscure reason DJGPP puts major version into
931 * name.release and minor into name.version. As of DJGPP 2.04 this
932 * is documented in djgpp libc.info file.
933 */
934
935 Tcl_SetVar2(interp, "tcl_platform", "osVersion", name.release,
936 TCL_GLOBAL_ONLY);
937 Tcl_SetVar2(interp, "tcl_platform", "osVersion", ".",
938 TCL_GLOBAL_ONLY|TCL_APPEND_VALUE);
939 Tcl_SetVar2(interp, "tcl_platform", "osVersion", name.version,
940 TCL_GLOBAL_ONLY|TCL_APPEND_VALUE);
941 #else
942 Tcl_SetVar2(interp, "tcl_platform", "osVersion", name.version,
943 TCL_GLOBAL_ONLY);
944 Tcl_SetVar2(interp, "tcl_platform", "osVersion", ".",
945 TCL_GLOBAL_ONLY|TCL_APPEND_VALUE);
946 Tcl_SetVar2(interp, "tcl_platform", "osVersion", name.release,
947 TCL_GLOBAL_ONLY|TCL_APPEND_VALUE);
948
949 #endif /* DJGPP */
950 }
951 Tcl_SetVar2(interp, "tcl_platform", "machine", name.machine,
952 TCL_GLOBAL_ONLY);
953 }
954 #endif /* !NO_UNAME */
955 if (!unameOK) {
956 Tcl_SetVar2(interp, "tcl_platform", "os", "", TCL_GLOBAL_ONLY);
957 Tcl_SetVar2(interp, "tcl_platform", "osVersion", "", TCL_GLOBAL_ONLY);
958 Tcl_SetVar2(interp, "tcl_platform", "machine", "", TCL_GLOBAL_ONLY);
959 }
960
961 /*
962 * Copy the username of the real user (according to getuid()) into
963 * tcl_platform(user).
964 */
965
966 {
967 struct passwd *pwEnt = TclpGetPwUid(getuid());
968 const char *user;
969 Tcl_DString ds;
970
971 if (pwEnt == NULL) {
972 user = "";
973 Tcl_DStringInit(&ds); /* ensure cleanliness */
974 } else {
975 user = Tcl_ExternalToUtfDString(NULL, pwEnt->pw_name, -1, &ds);
976 }
977
978 Tcl_SetVar2(interp, "tcl_platform", "user", user, TCL_GLOBAL_ONLY);
979 Tcl_DStringFree(&ds);
980 }
981
982 /*
983 * Define what the platform PATH separator is. [TIP #315]
984 */
985
986 Tcl_SetVar2(interp, "tcl_platform","pathSeparator", ":", TCL_GLOBAL_ONLY);
987 }
988
989 /*
990 *----------------------------------------------------------------------
991 *
992 * TclpFindVariable --
993 *
994 * Locate the entry in environ for a given name. On Unix this routine is
995 * case sensitive, on Windows this matches mixed case.
996 *
997 * Results:
998 * The return value is the index in environ of an entry with the name
999 * "name", or -1 if there is no such entry. The integer at *lengthPtr is
1000 * filled in with the length of name (if a matching entry is found) or
1001 * the length of the environ array (if no matching entry is found).
1002 *
1003 * Side effects:
1004 * None.
1005 *
1006 *----------------------------------------------------------------------
1007 */
1008
1009 int
1010 TclpFindVariable(
1011 const char *name, /* Name of desired environment variable
1012 * (native). */
1013 int *lengthPtr) /* Used to return length of name (for
1014 * successful searches) or number of non-NULL
1015 * entries in environ (for unsuccessful
1016 * searches). */
1017 {
1018 int i, result = -1;
1019 const char *env, *p1, *p2;
1020 Tcl_DString envString;
1021
1022 Tcl_DStringInit(&envString);
1023 for (i = 0, env = environ[i]; env != NULL; i++, env = environ[i]) {
1024 p1 = Tcl_ExternalToUtfDString(NULL, env, -1, &envString);
1025 p2 = name;
1026
1027 for (; *p2 == *p1; p1++, p2++) {
1028 /* NULL loop body. */
1029 }
1030 if ((*p1 == '=') && (*p2 == '\0')) {
1031 *lengthPtr = p2 - name;
1032 result = i;
1033 goto done;
1034 }
1035
1036 Tcl_DStringFree(&envString);
1037 }
1038
1039 *lengthPtr = i;
1040
1041 done:
1042 Tcl_DStringFree(&envString);
1043 return result;
1044 }
1045
1046 /*
1047 *----------------------------------------------------------------------
1048 *
1049 * MacOSXGetLibraryPath --
1050 *
1051 * If we have a bundle structure for the Tcl installation, then check
1052 * there first to see if we can find the libraries there.
1053 *
1054 * Results:
1055 * TCL_OK if we have found the tcl library; TCL_ERROR otherwise.
1056 *
1057 * Side effects:
1058 * Same as for Tcl_MacOSXOpenVersionedBundleResources.
1059 *
1060 *----------------------------------------------------------------------
1061 */
1062
1063 #ifdef HAVE_COREFOUNDATION
1064 static int
1065 MacOSXGetLibraryPath(
1066 Tcl_Interp *interp,
1067 int maxPathLen,
1068 char *tclLibPath)
1069 {
1070 int foundInFramework = TCL_ERROR;
1071
1072 #ifdef TCL_FRAMEWORK
1073 foundInFramework = Tcl_MacOSXOpenVersionedBundleResources(interp,
1074 "com.tcltk.tcllibrary", TCL_FRAMEWORK_VERSION, 0, maxPathLen,
1075 tclLibPath);
1076 #endif
1077
1078 return foundInFramework;
1079 }
1080 #endif /* HAVE_COREFOUNDATION */
1081
1082 /*
1083 * Local Variables:
1084 * mode: c
1085 * c-basic-offset: 4
1086 * fill-column: 78
1087 * End:
1088 */