diff options
Diffstat (limited to 'generic/tclEnv.c')
-rw-r--r-- | generic/tclEnv.c | 395 |
1 files changed, 215 insertions, 180 deletions
diff --git a/generic/tclEnv.c b/generic/tclEnv.c index 287a3a8..cd1a954 100644 --- a/generic/tclEnv.c +++ b/generic/tclEnv.c @@ -11,50 +11,42 @@ * * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. - * - * RCS: @(#) $Id: tclEnv.c,v 1.25 2005/07/21 14:38:31 dkf Exp $ */ #include "tclInt.h" -TCL_DECLARE_MUTEX(envMutex) /* To serialize access to environ */ +TCL_DECLARE_MUTEX(envMutex) /* To serialize access to environ. */ -static int cacheSize = 0; /* Number of env strings in environCache. */ -static char **environCache = NULL; - /* Array containing all of the environment +static struct { + int cacheSize; /* Number of env strings in cache. */ + char **cache; /* Array containing all of the environment * strings that Tcl has allocated. */ - #ifndef USE_PUTENV -static int environSize = 0; /* Non-zero means that the environ array was + char **ourEnviron; /* Cache of the array that we allocate. We + * need to track this in case another + * subsystem swaps around the environ array + * like we do. */ + int ourEnvironSize; /* Non-zero means that the environ array was * malloced and has this many total entries * allocated to it (not all may be in use at - * once). Zero means that the environment + * once). Zero means that the environment * array is in its original static state. */ #endif - -/* - * For MacOS X - */ -#if defined(__APPLE__) && defined(__DYNAMIC__) -#include <crt_externs.h> -char **environ = NULL; -#endif +} env; /* * Declarations for local functions defined in this file: */ -static char * EnvTraceProc _ANSI_ARGS_((ClientData clientData, - Tcl_Interp *interp, CONST char *name1, - CONST char *name2, int flags)); -static void ReplaceString _ANSI_ARGS_((CONST char *oldStr, - char *newStr)); -void TclSetEnv _ANSI_ARGS_((CONST char *name, - CONST char *value)); -void TclUnsetEnv _ANSI_ARGS_((CONST char *name)); - -#if defined (__CYGWIN__) && defined(__WIN32__) -static void TclCygwinPutenv _ANSI_ARGS_((CONST char *string)); +static char * EnvTraceProc(ClientData clientData, Tcl_Interp *interp, + const char *name1, const char *name2, int flags); +static void ReplaceString(const char *oldStr, char *newStr); +MODULE_SCOPE void TclSetEnv(const char *name, const char *value); +MODULE_SCOPE void TclUnsetEnv(const char *name); + +#if defined(__CYGWIN__) + static void TclCygwinPutenv(char *string); +# define putenv TclCygwinPutenv #endif /* @@ -72,7 +64,7 @@ static void TclCygwinPutenv _ANSI_ARGS_((CONST char *string)); * Side effects: * The interpreter is added to a list of interpreters managed by us, so * that its view of envariables can be kept consistent with the view in - * other interpreters. If this is the first call to TclSetupEnv, then + * other interpreters. If this is the first call to TclSetupEnv, then * additional initialization happens, such as copying the environment to * dynamically-allocated space for ease of management. * @@ -80,70 +72,111 @@ static void TclCygwinPutenv _ANSI_ARGS_((CONST char *string)); */ void -TclSetupEnv(interp) - Tcl_Interp *interp; /* Interpreter whose "env" array is to be +TclSetupEnv( + Tcl_Interp *interp) /* Interpreter whose "env" array is to be * managed. */ { + Var *varPtr, *arrayPtr; + Tcl_Obj *varNamePtr; Tcl_DString envString; - char *p1, *p2; - int i; + Tcl_HashTable namesHash; + Tcl_HashEntry *hPtr; + Tcl_HashSearch search; /* - * For MacOS X + * Synchronize the values in the environ array with the contents of the + * Tcl "env" variable. To do this: + * 1) Remove the trace that fires when the "env" var is updated. + * 2) Find the existing contents of the "env", storing in a hash table. + * 3) Create/update elements for each environ variable, removing + * elements from the hash table as we go. + * 4) Remove the elements for each remaining entry in the hash table, + * which must have existed before yet have no analog in the environ + * variable. + * 5) Add a trace that synchronizes the "env" array. */ -#if defined(__APPLE__) && defined(__DYNAMIC__) - environ = *_NSGetEnviron(); -#endif + + Tcl_UntraceVar2(interp, "env", NULL, + TCL_GLOBAL_ONLY | TCL_TRACE_WRITES | TCL_TRACE_UNSETS | + TCL_TRACE_READS | TCL_TRACE_ARRAY, EnvTraceProc, NULL); /* - * Synchronize the values in the environ array with the contents of the - * Tcl "env" variable. To do this: - * 1) Remove the trace that fires when the "env" var is unset. - * 2) Unset the "env" variable. - * 3) If there are no environ variables, create an empty "env" array. - * Otherwise populate the array with current values. - * 4) Add a trace that synchronizes the "env" array. + * Find out what elements are currently in the global env array. */ - Tcl_UntraceVar2(interp, "env", (char *) NULL, - TCL_GLOBAL_ONLY | TCL_TRACE_WRITES | TCL_TRACE_UNSETS | - TCL_TRACE_READS | TCL_TRACE_ARRAY, EnvTraceProc, - (ClientData) NULL); + TclNewLiteralStringObj(varNamePtr, "env"); + Tcl_IncrRefCount(varNamePtr); + Tcl_InitObjHashTable(&namesHash); + varPtr = TclObjLookupVarEx(interp, varNamePtr, NULL, TCL_GLOBAL_ONLY, + /*msg*/ 0, /*createPart1*/ 0, /*createPart2*/ 0, &arrayPtr); + TclFindArrayPtrElements(varPtr, &namesHash); - Tcl_UnsetVar2(interp, "env", (char *) NULL, TCL_GLOBAL_ONLY); + /* + * Go through the environment array and transfer its values into Tcl. At + * the same time, remove those elements we add/update from the hash table + * of existing elements, so that after this part processes, that table + * will hold just the parts to remove. + */ - if (environ[0] == NULL) { - Tcl_Obj *varNamePtr; + if (environ[0] != NULL) { + int i; - varNamePtr = Tcl_NewStringObj("env", -1); - Tcl_IncrRefCount(varNamePtr); - TclArraySet(interp, varNamePtr, NULL); - Tcl_DecrRefCount(varNamePtr); - } else { Tcl_MutexLock(&envMutex); for (i = 0; environ[i] != NULL; i++) { + Tcl_Obj *obj1, *obj2; + char *p1, *p2; + p1 = Tcl_ExternalToUtfDString(NULL, environ[i], -1, &envString); p2 = strchr(p1, '='); if (p2 == NULL) { /* * This condition seem to happen occasionally under some - * versions of Solaris; ignore the entry. + * versions of Solaris, or when encoding accidents swallow the + * '='; ignore the entry. */ continue; } p2++; p2[-1] = '\0'; - Tcl_SetVar2(interp, "env", p1, p2, TCL_GLOBAL_ONLY); + obj1 = Tcl_NewStringObj(p1, -1); + obj2 = Tcl_NewStringObj(p2, -1); Tcl_DStringFree(&envString); + + Tcl_IncrRefCount(obj1); + Tcl_IncrRefCount(obj2); + Tcl_ObjSetVar2(interp, varNamePtr, obj1, obj2, TCL_GLOBAL_ONLY); + hPtr = Tcl_FindHashEntry(&namesHash, obj1); + if (hPtr != NULL) { + Tcl_DeleteHashEntry(hPtr); + } + Tcl_DecrRefCount(obj1); + Tcl_DecrRefCount(obj2); } Tcl_MutexUnlock(&envMutex); } - Tcl_TraceVar2(interp, "env", (char *) NULL, + /* + * Delete those elements that existed in the array but which had no + * counterparts in the environment array. + */ + + for (hPtr=Tcl_FirstHashEntry(&namesHash, &search); hPtr!=NULL; + hPtr=Tcl_NextHashEntry(&search)) { + Tcl_Obj *elemName = Tcl_GetHashValue(hPtr); + + TclObjUnsetVar2(interp, varNamePtr, elemName, TCL_GLOBAL_ONLY); + } + Tcl_DeleteHashTable(&namesHash); + Tcl_DecrRefCount(varNamePtr); + + /* + * Re-establish the trace. + */ + + Tcl_TraceVar2(interp, "env", NULL, TCL_GLOBAL_ONLY | TCL_TRACE_WRITES | TCL_TRACE_UNSETS | - TCL_TRACE_READS | TCL_TRACE_ARRAY, EnvTraceProc, - (ClientData) NULL); + TCL_TRACE_READS | TCL_TRACE_ARRAY, EnvTraceProc, NULL); } /* @@ -152,11 +185,11 @@ TclSetupEnv(interp) * TclSetEnv -- * * Set an environment variable, replacing an existing value or creating a - * new variable if there doesn't exist a variable by the given name. - * This function is intended to be a stand-in for the UNIX "setenv" - * function so that applications using that function will interface - * properly to Tcl. To make it a stand-in, the Makefile must define - * "TclSetEnv" to "setenv". + * new variable if there doesn't exist a variable by the given name. This + * function is intended to be a stand-in for the UNIX "setenv" function + * so that applications using that function will interface properly to + * Tcl. To make it a stand-in, the Makefile must define "TclSetEnv" to + * "setenv". * * Results: * None. @@ -168,15 +201,16 @@ TclSetupEnv(interp) */ void -TclSetEnv(name, value) - CONST char *name; /* Name of variable whose value is to be set +TclSetEnv( + const char *name, /* Name of variable whose value is to be set * (UTF-8). */ - CONST char *value; /* New value for variable (UTF-8). */ + const char *value) /* New value for variable (UTF-8). */ { Tcl_DString envString; - int index, length, nameLength; + unsigned nameLength, valueLength; + int index, length; char *p, *oldValue; - CONST char *p2; + const char *p2; /* * Figure out where the entry is going to go. If the name doesn't already @@ -189,24 +223,21 @@ TclSetEnv(name, value) if (index == -1) { #ifndef USE_PUTENV - if ((length + 2) > environSize) { - char **newEnviron; - - newEnviron = (char **) ckalloc((unsigned) - ((length + 5) * sizeof(char *))); - memcpy((VOID *) newEnviron, (VOID *) environ, - length*sizeof(char *)); - if (environSize != 0) { - ckfree((char *) environ); - } - environ = newEnviron; - environSize = length + 5; -#if defined(__APPLE__) && defined(__DYNAMIC__) - { - char ***e = _NSGetEnviron(); - *e = environ; + /* + * We need to handle the case where the environment may be changed + * outside our control. ourEnvironSize is only valid if the current + * environment is the one we allocated. [Bug 979640] + */ + + if ((env.ourEnviron != environ) || (length+2 > env.ourEnvironSize)) { + char **newEnviron = ckalloc((length + 5) * sizeof(char *)); + + memcpy(newEnviron, environ, length * sizeof(char *)); + if ((env.ourEnvironSize != 0) && (env.ourEnviron != NULL)) { + ckfree(env.ourEnviron); } -#endif /* __APPLE__ && __DYNAMIC__ */ + environ = env.ourEnviron = newEnviron; + env.ourEnvironSize = length + 5; } index = length; environ[index + 1] = NULL; @@ -214,7 +245,7 @@ TclSetEnv(name, value) oldValue = NULL; nameLength = strlen(name); } else { - CONST char *env; + const char *env; /* * Compare the new value to the existing value. If they're the same @@ -225,7 +256,7 @@ TclSetEnv(name, value) */ env = Tcl_ExternalToUtfDString(NULL, environ[index], -1, &envString); - if (strcmp(value, (env + length + 1)) == 0) { + if (strcmp(value, env + (length + 1)) == 0) { Tcl_DStringFree(&envString); Tcl_MutexUnlock(&envMutex); return; @@ -233,7 +264,7 @@ TclSetEnv(name, value) Tcl_DStringFree(&envString); oldValue = environ[index]; - nameLength = length; + nameLength = (unsigned) length; } /* @@ -242,18 +273,19 @@ TclSetEnv(name, value) * and set the environ array value. */ - p = (char *) ckalloc((unsigned) (nameLength + strlen(value) + 2)); - strcpy(p, name); + valueLength = strlen(value); + p = ckalloc(nameLength + valueLength + 2); + memcpy(p, name, nameLength); p[nameLength] = '='; - strcpy(p+nameLength+1, value); + memcpy(p+nameLength+1, value, valueLength+1); p2 = Tcl_UtfToExternalDString(NULL, p, -1, &envString); /* * Copy the native string to heap memory. */ - p = (char *) ckrealloc(p, (unsigned) (strlen(p2) + 1)); - strcpy(p, p2); + p = ckrealloc(p, Tcl_DStringLength(&envString) + 1); + memcpy(p, p2, (unsigned) Tcl_DStringLength(&envString) + 1); Tcl_DStringFree(&envString); #ifdef USE_PUTENV @@ -265,7 +297,7 @@ TclSetEnv(name, value) index = TclpFindVariable(name, &length); #else environ[index] = p; -#endif +#endif /* USE_PUTENV */ /* * Watch out for versions of putenv that copy the string (e.g. VC++). In @@ -282,7 +314,7 @@ TclSetEnv(name, value) */ ckfree(p); -#endif +#endif /* HAVE_PUTENV_THAT_COPIES */ } Tcl_MutexUnlock(&envMutex); @@ -320,12 +352,12 @@ TclSetEnv(name, value) */ int -Tcl_PutEnv(assignment) - CONST char *assignment; /* Info about environment variable in the form +Tcl_PutEnv( + const char *assignment) /* Info about environment variable in the form * NAME=value. (native) */ { Tcl_DString nameString; - CONST char *name; + const char *name; char *value; if (assignment == NULL) { @@ -369,18 +401,18 @@ Tcl_PutEnv(assignment) */ void -TclUnsetEnv(name) - CONST char *name; /* Name of variable to remove (UTF-8). */ +TclUnsetEnv( + const char *name) /* Name of variable to remove (UTF-8). */ { char *oldValue; int length; int index; -#ifdef USE_PUTENV +#ifdef USE_PUTENV_FOR_UNSET Tcl_DString envString; char *string; #else char **envPtr; -#endif +#endif /* USE_PUTENV_FOR_UNSET */ Tcl_MutexLock(&envMutex); index = TclpFindVariable(name, &length); @@ -394,6 +426,7 @@ TclUnsetEnv(name) Tcl_MutexUnlock(&envMutex); return; } + /* * Remember the old value so we can free it if Tcl created the string. */ @@ -401,19 +434,31 @@ TclUnsetEnv(name) oldValue = environ[index]; /* - * Update the system environment. This must be done before we update the + * Update the system environment. This must be done before we update the * interpreters or we will recurse. */ -#ifdef USE_PUTENV - string = ckalloc((unsigned int) length+2); - memcpy((VOID *) string, (VOID *) name, (size_t) length); +#ifdef USE_PUTENV_FOR_UNSET + /* + * For those platforms that support putenv to unset, Linux indicates + * that no = should be included, and Windows requires it. + */ + +#if defined(_WIN32) || defined(__CYGWIN__) + string = ckalloc(length + 2); + memcpy(string, name, (size_t) length); string[length] = '='; string[length+1] = '\0'; +#else + string = ckalloc(length + 1); + memcpy(string, name, (size_t) length); + string[length] = '\0'; +#endif /* _WIN32 */ Tcl_UtfToExternalDString(NULL, string, -1, &envString); - string = ckrealloc(string, (unsigned) (Tcl_DStringLength(&envString)+1)); - strcpy(string, Tcl_DStringValue(&envString)); + string = ckrealloc(string, Tcl_DStringLength(&envString) + 1); + memcpy(string, Tcl_DStringValue(&envString), + (unsigned) Tcl_DStringLength(&envString)+1); Tcl_DStringFree(&envString); putenv(string); @@ -433,9 +478,9 @@ TclUnsetEnv(name) */ ckfree(string); -#endif +#endif /* HAVE_PUTENV_THAT_COPIES */ } -#else +#else /* !USE_PUTENV_FOR_UNSET */ for (envPtr = environ+index+1; ; envPtr++) { envPtr[-1] = *envPtr; if (*envPtr == NULL) { @@ -443,7 +488,7 @@ TclUnsetEnv(name) } } ReplaceString(oldValue, NULL); -#endif +#endif /* USE_PUTENV_FOR_UNSET */ Tcl_MutexUnlock(&envMutex); } @@ -468,16 +513,16 @@ TclUnsetEnv(name) *---------------------------------------------------------------------- */ -CONST char * -TclGetEnv(name, valuePtr) - CONST char *name; /* Name of environment variable to find +const char * +TclGetEnv( + const char *name, /* Name of environment variable to find * (UTF-8). */ - Tcl_DString *valuePtr; /* Uninitialized or free DString in which the + Tcl_DString *valuePtr) /* Uninitialized or free DString in which the * value of the environment variable is * stored. */ { int length, index; - CONST char *result; + const char *result; Tcl_MutexLock(&envMutex); index = TclpFindVariable(name, &length); @@ -523,14 +568,14 @@ TclGetEnv(name, valuePtr) /* ARGSUSED */ static char * -EnvTraceProc(clientData, interp, name1, name2, flags) - ClientData clientData; /* Not used. */ - Tcl_Interp *interp; /* Interpreter whose "env" variable is being +EnvTraceProc( + ClientData clientData, /* Not used. */ + Tcl_Interp *interp, /* Interpreter whose "env" variable is being * modified. */ - CONST char *name1; /* Better be "env". */ - CONST char *name2; /* Name of variable being modified, or NULL if + const char *name1, /* Better be "env". */ + const char *name2, /* Name of variable being modified, or NULL if * whole array is being deleted (UTF-8). */ - int flags; /* Indicates what's happening. */ + int flags) /* Indicates what's happening. */ { /* * For array traces, let TclSetupEnv do all the work. @@ -554,7 +599,7 @@ EnvTraceProc(clientData, interp, name1, name2, flags) */ if (flags & TCL_TRACE_WRITES) { - CONST char *value; + const char *value; value = Tcl_GetVar2(interp, "env", name2, TCL_GLOBAL_ONLY); TclSetEnv(name2, value); @@ -566,11 +611,11 @@ EnvTraceProc(clientData, interp, name1, name2, flags) if (flags & TCL_TRACE_READS) { Tcl_DString valueString; - CONST char *value; + const char *value = TclGetEnv(name2, &valueString); - value = TclGetEnv(name2, &valueString); if (value == NULL) { - return "no such variable"; + Tcl_UnsetVar2(interp, name1, name2, 0); + return NULL; } Tcl_SetVar2(interp, name1, name2, value, 0); Tcl_DStringFree(&valueString); @@ -591,8 +636,8 @@ EnvTraceProc(clientData, interp, name1, name2, flags) * * ReplaceString -- * - * Replace one string with another in the environment variable cache. - * The cache keeps track of all of the environment variables that Tcl has + * Replace one string with another in the environment variable cache. The + * cache keeps track of all of the environment variables that Tcl has * modified so they can be freed later. * * Results: @@ -605,12 +650,11 @@ EnvTraceProc(clientData, interp, name1, name2, flags) */ static void -ReplaceString(oldStr, newStr) - CONST char *oldStr; /* Old environment string. */ - char *newStr; /* New environment string. */ +ReplaceString( + const char *oldStr, /* Old environment string. */ + char *newStr) /* New environment string. */ { int i; - char **newCache; /* * Check to see if the old value was allocated by Tcl. If so, it needs to @@ -619,47 +663,41 @@ ReplaceString(oldStr, newStr) * changes are being made. */ - for (i = 0; i < cacheSize; i++) { - if ((environCache[i] == oldStr) || (environCache[i] == NULL)) { + for (i = 0; i < env.cacheSize; i++) { + if (env.cache[i]==oldStr || env.cache[i]==NULL) { break; } } - if (i < cacheSize) { + if (i < env.cacheSize) { /* * Replace or delete the old value. */ - if (environCache[i]) { - ckfree(environCache[i]); + if (env.cache[i]) { + ckfree(env.cache[i]); } if (newStr) { - environCache[i] = newStr; + env.cache[i] = newStr; } else { - for (; i < cacheSize-1; i++) { - environCache[i] = environCache[i+1]; + for (; i < env.cacheSize-1; i++) { + env.cache[i] = env.cache[i+1]; } - environCache[cacheSize-1] = NULL; + env.cache[env.cacheSize-1] = NULL; } } else { - int allocatedSize = (cacheSize + 5) * sizeof(char *); - /* * We need to grow the cache in order to hold the new string. */ - newCache = (char **) ckalloc((unsigned) allocatedSize); - (VOID *) memset(newCache, (int) 0, (size_t) allocatedSize); + const int growth = 5; - if (environCache) { - memcpy((VOID *) newCache, (VOID *) environCache, - (size_t) (cacheSize * sizeof(char*))); - ckfree((char *) environCache); - } - environCache = newCache; - environCache[cacheSize] = newStr; - environCache[cacheSize+1] = NULL; - cacheSize += 5; + env.cache = ckrealloc(env.cache, + (env.cacheSize + growth) * sizeof(char *)); + env.cache[env.cacheSize] = newStr; + (void) memset(env.cache+env.cacheSize+1, 0, + (size_t) (growth-1) * sizeof(char *)); + env.cacheSize += growth; } } @@ -682,7 +720,7 @@ ReplaceString(oldStr, newStr) */ void -TclFinalizeEnvironment() +TclFinalizeEnvironment(void) { /* * For now we just deallocate the cache array and none of the environment @@ -692,19 +730,17 @@ TclFinalizeEnvironment() * unlikely, so we don't bother. */ - if (environCache) { - ckfree((char *) environCache); - environCache = NULL; - cacheSize = 0; + if (env.cache) { + ckfree(env.cache); + env.cache = NULL; + env.cacheSize = 0; #ifndef USE_PUTENV - environSize = 0; + env.ourEnvironSize = 0; #endif } } -#if defined(__CYGWIN__) && defined(__WIN32__) - -#include <windows.h> +#if defined(__CYGWIN__) /* * When using cygwin, when an environment variable changes, we need to synch @@ -712,10 +748,11 @@ TclFinalizeEnvironment() * fork) and the Windows environment (in case the application TCL code calls * exec, which calls the Windows CreateProcess function). */ +DLLIMPORT extern void __stdcall SetEnvironmentVariableA(const char*, const char *); static void -TclCygwinPutenv(str) - const char *str; +TclCygwinPutenv( + char *str) { char *name, *value; @@ -724,7 +761,7 @@ TclCygwinPutenv(str) * for Windows. */ - name = (char *) alloca(strlen(str) + 1); + name = alloca(strlen(str) + 1); strcpy(name, str); for (value=name ; *value!='=' && *value!='\0' ; ++value) { /* Empty body */ @@ -733,8 +770,7 @@ TclCygwinPutenv(str) /* Can't happen. */ return; } - *value = '\0'; - ++value; + *(value++) = '\0'; if (*value == '\0') { value = NULL; } @@ -766,11 +802,11 @@ TclCygwinPutenv(str) */ if (strcmp(name, "Path") == 0) { - SetEnvironmentVariable("PATH", (char *) NULL); + SetEnvironmentVariableA("PATH", NULL); unsetenv("PATH"); } - SetEnvironmentVariable(name, value); + SetEnvironmentVariableA(name, value); } else { char *buf; @@ -778,7 +814,7 @@ TclCygwinPutenv(str) * Eliminate any Path variable, to prevent any confusion. */ - SetEnvironmentVariable("Path", (char *) NULL); + SetEnvironmentVariableA("Path", NULL); unsetenv("Path"); if (value == NULL) { @@ -786,16 +822,15 @@ TclCygwinPutenv(str) } else { int size; - size = cygwin_posix_to_win32_path_list_buf_size(value); - buf = (char *) alloca(size + 1); - cygwin_posix_to_win32_path_list(value, buf); + size = cygwin_conv_path_list(0, value, NULL, 0); + buf = alloca(size + 1); + cygwin_conv_path_list(0, value, buf, size); } - SetEnvironmentVariable(name, buf); + SetEnvironmentVariableA(name, buf); } } - -#endif /* __CYGWIN__ && __WIN32__ */ +#endif /* __CYGWIN__ */ /* * Local Variables: |