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