| 1 |
/* |
| 2 |
* tclWinInit.c -- |
| 3 |
* |
| 4 |
* Contains the Windows-specific interpreter initialization functions. |
| 5 |
* |
| 6 |
* Copyright (c) 1994-1997 Sun Microsystems, Inc. |
| 7 |
* Copyright (c) 1998-1999 by Scriptics Corporation. |
| 8 |
* All rights reserved. |
| 9 |
* |
| 10 |
* RCS: @(#) $Id: tclWinInit.c,v 1.40.2.2 2003/11/10 20:32:34 dgp Exp $ |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "tclWinInt.h" |
| 14 |
#include <winnt.h> |
| 15 |
#include <winbase.h> |
| 16 |
#include <lmcons.h> |
| 17 |
|
| 18 |
/* |
| 19 |
* The following declaration is a workaround for some Microsoft brain damage. |
| 20 |
* The SYSTEM_INFO structure is different in various releases, even though the |
| 21 |
* layout is the same. So we overlay our own structure on top of it so we |
| 22 |
* can access the interesting slots in a uniform way. |
| 23 |
*/ |
| 24 |
|
| 25 |
typedef struct { |
| 26 |
WORD wProcessorArchitecture; |
| 27 |
WORD wReserved; |
| 28 |
} OemId; |
| 29 |
|
| 30 |
/* |
| 31 |
* The following macros are missing from some versions of winnt.h. |
| 32 |
*/ |
| 33 |
|
| 34 |
#ifndef PROCESSOR_ARCHITECTURE_INTEL |
| 35 |
#define PROCESSOR_ARCHITECTURE_INTEL 0 |
| 36 |
#endif |
| 37 |
#ifndef PROCESSOR_ARCHITECTURE_MIPS |
| 38 |
#define PROCESSOR_ARCHITECTURE_MIPS 1 |
| 39 |
#endif |
| 40 |
#ifndef PROCESSOR_ARCHITECTURE_ALPHA |
| 41 |
#define PROCESSOR_ARCHITECTURE_ALPHA 2 |
| 42 |
#endif |
| 43 |
#ifndef PROCESSOR_ARCHITECTURE_PPC |
| 44 |
#define PROCESSOR_ARCHITECTURE_PPC 3 |
| 45 |
#endif |
| 46 |
#ifndef PROCESSOR_ARCHITECTURE_SHX |
| 47 |
#define PROCESSOR_ARCHITECTURE_SHX 4 |
| 48 |
#endif |
| 49 |
#ifndef PROCESSOR_ARCHITECTURE_ARM |
| 50 |
#define PROCESSOR_ARCHITECTURE_ARM 5 |
| 51 |
#endif |
| 52 |
#ifndef PROCESSOR_ARCHITECTURE_IA64 |
| 53 |
#define PROCESSOR_ARCHITECTURE_IA64 6 |
| 54 |
#endif |
| 55 |
#ifndef PROCESSOR_ARCHITECTURE_ALPHA64 |
| 56 |
#define PROCESSOR_ARCHITECTURE_ALPHA64 7 |
| 57 |
#endif |
| 58 |
#ifndef PROCESSOR_ARCHITECTURE_MSIL |
| 59 |
#define PROCESSOR_ARCHITECTURE_MSIL 8 |
| 60 |
#endif |
| 61 |
#ifndef PROCESSOR_ARCHITECTURE_AMD64 |
| 62 |
#define PROCESSOR_ARCHITECTURE_AMD64 9 |
| 63 |
#endif |
| 64 |
#ifndef PROCESSOR_ARCHITECTURE_IA32_ON_WIN64 |
| 65 |
#define PROCESSOR_ARCHITECTURE_IA32_ON_WIN64 10 |
| 66 |
#endif |
| 67 |
#ifndef PROCESSOR_ARCHITECTURE_UNKNOWN |
| 68 |
#define PROCESSOR_ARCHITECTURE_UNKNOWN 0xFFFF |
| 69 |
#endif |
| 70 |
|
| 71 |
/* |
| 72 |
* The following arrays contain the human readable strings for the Windows |
| 73 |
* platform and processor values. |
| 74 |
*/ |
| 75 |
|
| 76 |
|
| 77 |
#define NUMPLATFORMS 4 |
| 78 |
static char* platforms[NUMPLATFORMS] = { |
| 79 |
"Win32s", "Windows 95", "Windows NT", "Windows CE" |
| 80 |
}; |
| 81 |
|
| 82 |
#define NUMPROCESSORS 11 |
| 83 |
static char* processors[NUMPROCESSORS] = { |
| 84 |
"intel", "mips", "alpha", "ppc", "shx", "arm", "ia64", "alpha64", "msil", |
| 85 |
"amd64", "ia32_on_win64" |
| 86 |
}; |
| 87 |
|
| 88 |
/* Used to store the encoding used for binary files */ |
| 89 |
static Tcl_Encoding binaryEncoding = NULL; |
| 90 |
/* Has the basic library path encoding issue been fixed */ |
| 91 |
static int libraryPathEncodingFixed = 0; |
| 92 |
|
| 93 |
/* |
| 94 |
* The Init script (common to Windows and Unix platforms) is |
| 95 |
* defined in tkInitScript.h |
| 96 |
*/ |
| 97 |
|
| 98 |
#include "tclInitScript.h" |
| 99 |
|
| 100 |
static void AppendEnvironment(Tcl_Obj *listPtr, CONST char *lib); |
| 101 |
static void AppendDllPath(Tcl_Obj *listPtr, HMODULE hModule, |
| 102 |
CONST char *lib); |
| 103 |
static int ToUtf(CONST WCHAR *wSrc, char *dst); |
| 104 |
|
| 105 |
/* |
| 106 |
*--------------------------------------------------------------------------- |
| 107 |
* |
| 108 |
* TclpInitPlatform -- |
| 109 |
* |
| 110 |
* Initialize all the platform-dependant things like signals and |
| 111 |
* floating-point error handling. |
| 112 |
* |
| 113 |
* Called at process initialization time. |
| 114 |
* |
| 115 |
* Results: |
| 116 |
* None. |
| 117 |
* |
| 118 |
* Side effects: |
| 119 |
* None. |
| 120 |
* |
| 121 |
*--------------------------------------------------------------------------- |
| 122 |
*/ |
| 123 |
|
| 124 |
void |
| 125 |
TclpInitPlatform() |
| 126 |
{ |
| 127 |
tclPlatform = TCL_PLATFORM_WINDOWS; |
| 128 |
|
| 129 |
/* |
| 130 |
* The following code stops Windows 3.X and Windows NT 3.51 from |
| 131 |
* automatically putting up Sharing Violation dialogs, e.g, when |
| 132 |
* someone tries to access a file that is locked or a drive with no |
| 133 |
* disk in it. Tcl already returns the appropriate error to the |
| 134 |
* caller, and they can decide to put up their own dialog in response |
| 135 |
* to that failure. |
| 136 |
* |
| 137 |
* Under 95 and NT 4.0, this is a NOOP because the system doesn't |
| 138 |
* automatically put up dialogs when the above operations fail. |
| 139 |
*/ |
| 140 |
|
| 141 |
SetErrorMode(SetErrorMode(0) | SEM_FAILCRITICALERRORS); |
| 142 |
|
| 143 |
#ifdef STATIC_BUILD |
| 144 |
/* |
| 145 |
* If we are in a statically linked executable, then we need to |
| 146 |
* explicitly initialize the Windows function tables here since |
| 147 |
* DllMain() will not be invoked. |
| 148 |
*/ |
| 149 |
|
| 150 |
TclWinInit(GetModuleHandle(NULL)); |
| 151 |
#endif |
| 152 |
} |
| 153 |
|
| 154 |
/* |
| 155 |
*--------------------------------------------------------------------------- |
| 156 |
* |
| 157 |
* TclpInitLibraryPath -- |
| 158 |
* |
| 159 |
* Initialize the library path at startup. |
| 160 |
* |
| 161 |
* This call sets the library path to strings in UTF-8. Any |
| 162 |
* pre-existing library path information is assumed to have been |
| 163 |
* in the native multibyte encoding. |
| 164 |
* |
| 165 |
* Called at process initialization time. |
| 166 |
* |
| 167 |
* Results: |
| 168 |
* None. |
| 169 |
* |
| 170 |
* Side effects: |
| 171 |
* None. |
| 172 |
* |
| 173 |
*--------------------------------------------------------------------------- |
| 174 |
*/ |
| 175 |
|
| 176 |
void |
| 177 |
TclpInitLibraryPath(path) |
| 178 |
CONST char *path; /* Potentially dirty UTF string that is */ |
| 179 |
/* the path to the executable name. */ |
| 180 |
{ |
| 181 |
#define LIBRARY_SIZE 32 |
| 182 |
Tcl_Obj *pathPtr, *objPtr; |
| 183 |
CONST char *str; |
| 184 |
Tcl_DString ds; |
| 185 |
int pathc; |
| 186 |
CONST char **pathv; |
| 187 |
char installLib[LIBRARY_SIZE], developLib[LIBRARY_SIZE]; |
| 188 |
|
| 189 |
Tcl_DStringInit(&ds); |
| 190 |
pathPtr = Tcl_NewObj(); |
| 191 |
|
| 192 |
/* |
| 193 |
* Initialize the substrings used when locating an executable. The |
| 194 |
* installLib variable computes the path as though the executable |
| 195 |
* is installed. The developLib computes the path as though the |
| 196 |
* executable is run from a develpment directory. |
| 197 |
*/ |
| 198 |
|
| 199 |
sprintf(installLib, "lib/tcl%s", TCL_VERSION); |
| 200 |
sprintf(developLib, "tcl%s/library", TCL_PATCH_LEVEL); |
| 201 |
|
| 202 |
/* |
| 203 |
* Look for the library relative to default encoding dir. |
| 204 |
*/ |
| 205 |
|
| 206 |
str = Tcl_GetDefaultEncodingDir(); |
| 207 |
if ((str != NULL) && (str[0] != '\0')) { |
| 208 |
objPtr = Tcl_NewStringObj(str, -1); |
| 209 |
Tcl_ListObjAppendElement(NULL, pathPtr, objPtr); |
| 210 |
} |
| 211 |
|
| 212 |
/* |
| 213 |
* Look for the library relative to the TCL_LIBRARY env variable. |
| 214 |
* If the last dirname in the TCL_LIBRARY path does not match the |
| 215 |
* last dirname in the installLib variable, use the last dir name |
| 216 |
* of installLib in addition to the orginal TCL_LIBRARY path. |
| 217 |
*/ |
| 218 |
|
| 219 |
AppendEnvironment(pathPtr, installLib); |
| 220 |
|
| 221 |
/* |
| 222 |
* Look for the library relative to the DLL. Only use the installLib |
| 223 |
* because in practice, the DLL is always installed. |
| 224 |
*/ |
| 225 |
|
| 226 |
AppendDllPath(pathPtr, TclWinGetTclInstance(), installLib); |
| 227 |
|
| 228 |
|
| 229 |
/* |
| 230 |
* Look for the library relative to the executable. This algorithm |
| 231 |
* should be the same as the one in the tcl_findLibrary procedure. |
| 232 |
* |
| 233 |
* This code looks in the following directories: |
| 234 |
* |
| 235 |
* <bindir>/../<installLib> |
| 236 |
* (e.g. /usr/local/bin/../lib/tcl8.4) |
| 237 |
* <bindir>/../../<installLib> |
| 238 |
* (e.g. /usr/local/TclPro/solaris-sparc/bin/../../lib/tcl8.4) |
| 239 |
* <bindir>/../library |
| 240 |
* (e.g. /usr/src/tcl8.4.0/unix/../library) |
| 241 |
* <bindir>/../../library |
| 242 |
* (e.g. /usr/src/tcl8.4.0/unix/solaris-sparc/../../library) |
| 243 |
* <bindir>/../../<developLib> |
| 244 |
* (e.g. /usr/src/tcl8.4.0/unix/../../tcl8.4.0/library) |
| 245 |
* <bindir>/../../../<developLib> |
| 246 |
* (e.g. /usr/src/tcl8.4.0/unix/solaris-sparc/../../../tcl8.4.0/library) |
| 247 |
*/ |
| 248 |
|
| 249 |
/* |
| 250 |
* The variable path holds an absolute path. Take care not to |
| 251 |
* overwrite pathv[0] since that might produce a relative path. |
| 252 |
*/ |
| 253 |
|
| 254 |
if (path != NULL) { |
| 255 |
int i, origc; |
| 256 |
CONST char **origv; |
| 257 |
|
| 258 |
Tcl_SplitPath(path, &origc, &origv); |
| 259 |
pathc = 0; |
| 260 |
pathv = (CONST char **) ckalloc((unsigned int)(origc * sizeof(char *))); |
| 261 |
for (i=0; i< origc; i++) { |
| 262 |
if (origv[i][0] == '.') { |
| 263 |
if (strcmp(origv[i], ".") == 0) { |
| 264 |
/* do nothing */ |
| 265 |
} else if (strcmp(origv[i], "..") == 0) { |
| 266 |
pathc--; |
| 267 |
} else { |
| 268 |
pathv[pathc++] = origv[i]; |
| 269 |
} |
| 270 |
} else { |
| 271 |
pathv[pathc++] = origv[i]; |
| 272 |
} |
| 273 |
} |
| 274 |
if (pathc > 2) { |
| 275 |
str = pathv[pathc - 2]; |
| 276 |
pathv[pathc - 2] = installLib; |
| 277 |
path = Tcl_JoinPath(pathc - 1, pathv, &ds); |
| 278 |
pathv[pathc - 2] = str; |
| 279 |
objPtr = Tcl_NewStringObj(path, Tcl_DStringLength(&ds)); |
| 280 |
Tcl_ListObjAppendElement(NULL, pathPtr, objPtr); |
| 281 |
Tcl_DStringFree(&ds); |
| 282 |
} |
| 283 |
if (pathc > 3) { |
| 284 |
str = pathv[pathc - 3]; |
| 285 |
pathv[pathc - 3] = installLib; |
| 286 |
path = Tcl_JoinPath(pathc - 2, pathv, &ds); |
| 287 |
pathv[pathc - 3] = str; |
| 288 |
objPtr = Tcl_NewStringObj(path, Tcl_DStringLength(&ds)); |
| 289 |
Tcl_ListObjAppendElement(NULL, pathPtr, objPtr); |
| 290 |
Tcl_DStringFree(&ds); |
| 291 |
} |
| 292 |
if (pathc > 2) { |
| 293 |
str = pathv[pathc - 2]; |
| 294 |
pathv[pathc - 2] = "library"; |
| 295 |
path = Tcl_JoinPath(pathc - 1, pathv, &ds); |
| 296 |
pathv[pathc - 2] = str; |
| 297 |
objPtr = Tcl_NewStringObj(path, Tcl_DStringLength(&ds)); |
| 298 |
Tcl_ListObjAppendElement(NULL, pathPtr, objPtr); |
| 299 |
Tcl_DStringFree(&ds); |
| 300 |
} |
| 301 |
if (pathc > 3) { |
| 302 |
str = pathv[pathc - 3]; |
| 303 |
pathv[pathc - 3] = "library"; |
| 304 |
path = Tcl_JoinPath(pathc - 2, pathv, &ds); |
| 305 |
pathv[pathc - 3] = str; |
| 306 |
objPtr = Tcl_NewStringObj(path, Tcl_DStringLength(&ds)); |
| 307 |
Tcl_ListObjAppendElement(NULL, pathPtr, objPtr); |
| 308 |
Tcl_DStringFree(&ds); |
| 309 |
} |
| 310 |
if (pathc > 3) { |
| 311 |
str = pathv[pathc - 3]; |
| 312 |
pathv[pathc - 3] = developLib; |
| 313 |
path = Tcl_JoinPath(pathc - 2, pathv, &ds); |
| 314 |
pathv[pathc - 3] = str; |
| 315 |
objPtr = Tcl_NewStringObj(path, Tcl_DStringLength(&ds)); |
| 316 |
Tcl_ListObjAppendElement(NULL, pathPtr, objPtr); |
| 317 |
Tcl_DStringFree(&ds); |
| 318 |
} |
| 319 |
if (pathc > 4) { |
| 320 |
str = pathv[pathc - 4]; |
| 321 |
pathv[pathc - 4] = developLib; |
| 322 |
path = Tcl_JoinPath(pathc - 3, pathv, &ds); |
| 323 |
pathv[pathc - 4] = str; |
| 324 |
objPtr = Tcl_NewStringObj(path, Tcl_DStringLength(&ds)); |
| 325 |
Tcl_ListObjAppendElement(NULL, pathPtr, objPtr); |
| 326 |
Tcl_DStringFree(&ds); |
| 327 |
} |
| 328 |
ckfree((char *) origv); |
| 329 |
ckfree((char *) pathv); |
| 330 |
} |
| 331 |
|
| 332 |
TclSetLibraryPath(pathPtr); |
| 333 |
} |
| 334 |
|
| 335 |
/* |
| 336 |
*--------------------------------------------------------------------------- |
| 337 |
* |
| 338 |
* AppendEnvironment -- |
| 339 |
* |
| 340 |
* Append the value of the TCL_LIBRARY environment variable onto the |
| 341 |
* path pointer. If the env variable points to another version of |
| 342 |
* tcl (e.g. "tcl7.6") also append the path to this version (e.g., |
| 343 |
* "tcl7.6/../tcl8.2") |
| 344 |
* |
| 345 |
* Results: |
| 346 |
* None. |
| 347 |
* |
| 348 |
* Side effects: |
| 349 |
* None. |
| 350 |
* |
| 351 |
*--------------------------------------------------------------------------- |
| 352 |
*/ |
| 353 |
|
| 354 |
static void |
| 355 |
AppendEnvironment( |
| 356 |
Tcl_Obj *pathPtr, |
| 357 |
CONST char *lib) |
| 358 |
{ |
| 359 |
int pathc; |
| 360 |
WCHAR wBuf[MAX_PATH]; |
| 361 |
char buf[MAX_PATH * TCL_UTF_MAX]; |
| 362 |
Tcl_Obj *objPtr; |
| 363 |
Tcl_DString ds; |
| 364 |
CONST char **pathv; |
| 365 |
|
| 366 |
/* |
| 367 |
* The "L" preceeding the TCL_LIBRARY string is used to tell VC++ |
| 368 |
* that this is a unicode string. |
| 369 |
*/ |
| 370 |
|
| 371 |
if (GetEnvironmentVariableW(L"TCL_LIBRARY", wBuf, MAX_PATH) == 0) { |
| 372 |
buf[0] = '\0'; |
| 373 |
GetEnvironmentVariableA("TCL_LIBRARY", buf, MAX_PATH); |
| 374 |
} else { |
| 375 |
ToUtf(wBuf, buf); |
| 376 |
} |
| 377 |
|
| 378 |
if (buf[0] != '\0') { |
| 379 |
objPtr = Tcl_NewStringObj(buf, -1); |
| 380 |
Tcl_ListObjAppendElement(NULL, pathPtr, objPtr); |
| 381 |
|
| 382 |
TclWinNoBackslash(buf); |
| 383 |
Tcl_SplitPath(buf, &pathc, &pathv); |
| 384 |
|
| 385 |
/* |
| 386 |
* The lstrcmpi() will work even if pathv[pathc - 1] is random |
| 387 |
* UTF-8 chars because I know lib is ascii. |
| 388 |
*/ |
| 389 |
|
| 390 |
if ((pathc > 0) && (lstrcmpiA(lib + 4, pathv[pathc - 1]) != 0)) { |
| 391 |
CONST char *str; |
| 392 |
/* |
| 393 |
* TCL_LIBRARY is set but refers to a different tcl |
| 394 |
* installation than the current version. Try fiddling with the |
| 395 |
* specified directory to make it refer to this installation by |
| 396 |
* removing the old "tclX.Y" and substituting the current |
| 397 |
* version string. |
| 398 |
*/ |
| 399 |
|
| 400 |
pathv[pathc - 1] = (lib + 4); |
| 401 |
Tcl_DStringInit(&ds); |
| 402 |
str = Tcl_JoinPath(pathc, pathv, &ds); |
| 403 |
objPtr = Tcl_NewStringObj(str, Tcl_DStringLength(&ds)); |
| 404 |
Tcl_DStringFree(&ds); |
| 405 |
} else { |
| 406 |
objPtr = Tcl_NewStringObj(buf, -1); |
| 407 |
} |
| 408 |
Tcl_ListObjAppendElement(NULL, pathPtr, objPtr); |
| 409 |
ckfree((char *) pathv); |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
/* |
| 414 |
*--------------------------------------------------------------------------- |
| 415 |
* |
| 416 |
* AppendDllPath -- |
| 417 |
* |
| 418 |
* Append a path onto the path pointer that tries to locate the Tcl |
| 419 |
* library relative to the location of the Tcl DLL. |
| 420 |
* |
| 421 |
* Results: |
| 422 |
* None. |
| 423 |
* |
| 424 |
* Side effects: |
| 425 |
* None. |
| 426 |
* |
| 427 |
*--------------------------------------------------------------------------- |
| 428 |
*/ |
| 429 |
|
| 430 |
static void |
| 431 |
AppendDllPath( |
| 432 |
Tcl_Obj *pathPtr, |
| 433 |
HMODULE hModule, |
| 434 |
CONST char *lib) |
| 435 |
{ |
| 436 |
WCHAR wName[MAX_PATH + LIBRARY_SIZE]; |
| 437 |
char name[(MAX_PATH + LIBRARY_SIZE) * TCL_UTF_MAX]; |
| 438 |
|
| 439 |
if (GetModuleFileNameW(hModule, wName, MAX_PATH) == 0) { |
| 440 |
GetModuleFileNameA(hModule, name, MAX_PATH); |
| 441 |
} else { |
| 442 |
ToUtf(wName, name); |
| 443 |
} |
| 444 |
if (lib != NULL) { |
| 445 |
char *end, *p; |
| 446 |
|
| 447 |
end = strrchr(name, '\\'); |
| 448 |
*end = '\0'; |
| 449 |
p = strrchr(name, '\\'); |
| 450 |
if (p != NULL) { |
| 451 |
end = p; |
| 452 |
} |
| 453 |
*end = '\\'; |
| 454 |
strcpy(end + 1, lib); |
| 455 |
} |
| 456 |
TclWinNoBackslash(name); |
| 457 |
Tcl_ListObjAppendElement(NULL, pathPtr, Tcl_NewStringObj(name, -1)); |
| 458 |
} |
| 459 |
|
| 460 |
/* |
| 461 |
*--------------------------------------------------------------------------- |
| 462 |
* |
| 463 |
* ToUtf -- |
| 464 |
* |
| 465 |
* Convert a char string to a UTF string. |
| 466 |
* |
| 467 |
* Results: |
| 468 |
* None. |
| 469 |
* |
| 470 |
* Side effects: |
| 471 |
* None. |
| 472 |
* |
| 473 |
*--------------------------------------------------------------------------- |
| 474 |
*/ |
| 475 |
|
| 476 |
static int |
| 477 |
ToUtf( |
| 478 |
CONST WCHAR *wSrc, |
| 479 |
char *dst) |
| 480 |
{ |
| 481 |
char *start; |
| 482 |
|
| 483 |
start = dst; |
| 484 |
while (*wSrc != '\0') { |
| 485 |
dst += Tcl_UniCharToUtf(*wSrc, dst); |
| 486 |
wSrc++; |
| 487 |
} |
| 488 |
*dst = '\0'; |
| 489 |
return (int) (dst - start); |
| 490 |
} |
| 491 |
|
| 492 |
/* |
| 493 |
*--------------------------------------------------------------------------- |
| 494 |
* |
| 495 |
* TclWinEncodingsCleanup -- |
| 496 |
* |
| 497 |
* Reset information to its original state in finalization to |
| 498 |
* allow for reinitialization to be possible. This must not |
| 499 |
* be called until after the filesystem has been finalised, or |
| 500 |
* exit crashes may occur when using virtual filesystems. |
| 501 |
* |
| 502 |
* Results: |
| 503 |
* None. |
| 504 |
* |
| 505 |
* Side effects: |
| 506 |
* Static information reset to startup state. |
| 507 |
* |
| 508 |
*--------------------------------------------------------------------------- |
| 509 |
*/ |
| 510 |
|
| 511 |
void |
| 512 |
TclWinEncodingsCleanup() |
| 513 |
{ |
| 514 |
TclWinResetInterfaceEncodings(); |
| 515 |
libraryPathEncodingFixed = 0; |
| 516 |
if (binaryEncoding != NULL) { |
| 517 |
Tcl_FreeEncoding(binaryEncoding); |
| 518 |
binaryEncoding = NULL; |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
/* |
| 523 |
*--------------------------------------------------------------------------- |
| 524 |
* |
| 525 |
* TclpSetInitialEncodings -- |
| 526 |
* |
| 527 |
* Based on the locale, determine the encoding of the operating |
| 528 |
* system and the default encoding for newly opened files. |
| 529 |
* |
| 530 |
* Called at process initialization time, and part way through |
| 531 |
* startup, we verify that the initial encodings were correctly |
| 532 |
* setup. Depending on Tcl's environment, there may not have been |
| 533 |
* enough information first time through (above). |
| 534 |
* |
| 535 |
* Results: |
| 536 |
* None. |
| 537 |
* |
| 538 |
* Side effects: |
| 539 |
* The Tcl library path is converted from native encoding to UTF-8, |
| 540 |
* on the first call, and the encodings may be changed on first or |
| 541 |
* second call. |
| 542 |
* |
| 543 |
*--------------------------------------------------------------------------- |
| 544 |
*/ |
| 545 |
|
| 546 |
void |
| 547 |
TclpSetInitialEncodings() |
| 548 |
{ |
| 549 |
CONST char *encoding; |
| 550 |
char buf[4 + TCL_INTEGER_SPACE]; |
| 551 |
|
| 552 |
if (libraryPathEncodingFixed == 0) { |
| 553 |
int platformId; |
| 554 |
platformId = TclWinGetPlatformId(); |
| 555 |
TclWinSetInterfaces(platformId == VER_PLATFORM_WIN32_NT); |
| 556 |
|
| 557 |
wsprintfA(buf, "cp%d", GetACP()); |
| 558 |
Tcl_SetSystemEncoding(NULL, buf); |
| 559 |
|
| 560 |
if (platformId != VER_PLATFORM_WIN32_NT) { |
| 561 |
Tcl_Obj *pathPtr = TclGetLibraryPath(); |
| 562 |
if (pathPtr != NULL) { |
| 563 |
int i, objc; |
| 564 |
Tcl_Obj **objv; |
| 565 |
|
| 566 |
objc = 0; |
| 567 |
Tcl_ListObjGetElements(NULL, pathPtr, &objc, &objv); |
| 568 |
for (i = 0; i < objc; i++) { |
| 569 |
int length; |
| 570 |
char *string; |
| 571 |
Tcl_DString ds; |
| 572 |
|
| 573 |
string = Tcl_GetStringFromObj(objv[i], &length); |
| 574 |
Tcl_ExternalToUtfDString(NULL, string, length, &ds); |
| 575 |
Tcl_SetStringObj(objv[i], Tcl_DStringValue(&ds), |
| 576 |
Tcl_DStringLength(&ds)); |
| 577 |
Tcl_DStringFree(&ds); |
| 578 |
} |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
libraryPathEncodingFixed = 1; |
| 583 |
} else { |
| 584 |
wsprintfA(buf, "cp%d", GetACP()); |
| 585 |
Tcl_SetSystemEncoding(NULL, buf); |
| 586 |
} |
| 587 |
|
| 588 |
/* This is only ever called from the startup thread */ |
| 589 |
if (binaryEncoding == NULL) { |
| 590 |
/* |
| 591 |
* Keep this encoding preloaded. The IO package uses it for |
| 592 |
* gets on a binary channel. |
| 593 |
*/ |
| 594 |
encoding = "iso8859-1"; |
| 595 |
binaryEncoding = Tcl_GetEncoding(NULL, encoding); |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
/* |
| 600 |
*--------------------------------------------------------------------------- |
| 601 |
* |
| 602 |
* TclpSetVariables -- |
| 603 |
* |
| 604 |
* Performs platform-specific interpreter initialization related to |
| 605 |
* the tcl_platform and env variables, and other platform-specific |
| 606 |
* things. |
| 607 |
* |
| 608 |
* Results: |
| 609 |
* None. |
| 610 |
* |
| 611 |
* Side effects: |
| 612 |
* Sets "tcl_platform", and "env(HOME)" Tcl variables. |
| 613 |
* |
| 614 |
*---------------------------------------------------------------------- |
| 615 |
*/ |
| 616 |
|
| 617 |
void |
| 618 |
TclpSetVariables(interp) |
| 619 |
Tcl_Interp *interp; /* Interp to initialize. */ |
| 620 |
{ |
| 621 |
CONST char *ptr; |
| 622 |
char buffer[TCL_INTEGER_SPACE * 2]; |
| 623 |
SYSTEM_INFO sysInfo; |
| 624 |
OemId *oemId; |
| 625 |
OSVERSIONINFOA osInfo; |
| 626 |
Tcl_DString ds; |
| 627 |
TCHAR szUserName[ UNLEN+1 ]; |
| 628 |
DWORD dwUserNameLen = sizeof(szUserName); |
| 629 |
|
| 630 |
osInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA); |
| 631 |
GetVersionExA(&osInfo); |
| 632 |
|
| 633 |
oemId = (OemId *) &sysInfo; |
| 634 |
GetSystemInfo(&sysInfo); |
| 635 |
|
| 636 |
/* |
| 637 |
* Define the tcl_platform array. |
| 638 |
*/ |
| 639 |
|
| 640 |
Tcl_SetVar2(interp, "tcl_platform", "platform", "windows", |
| 641 |
TCL_GLOBAL_ONLY); |
| 642 |
if (osInfo.dwPlatformId < NUMPLATFORMS) { |
| 643 |
Tcl_SetVar2(interp, "tcl_platform", "os", |
| 644 |
platforms[osInfo.dwPlatformId], TCL_GLOBAL_ONLY); |
| 645 |
} |
| 646 |
wsprintfA(buffer, "%d.%d", osInfo.dwMajorVersion, osInfo.dwMinorVersion); |
| 647 |
Tcl_SetVar2(interp, "tcl_platform", "osVersion", buffer, TCL_GLOBAL_ONLY); |
| 648 |
if (oemId->wProcessorArchitecture < NUMPROCESSORS) { |
| 649 |
Tcl_SetVar2(interp, "tcl_platform", "machine", |
| 650 |
processors[oemId->wProcessorArchitecture], |
| 651 |
TCL_GLOBAL_ONLY); |
| 652 |
} |
| 653 |
|
| 654 |
#ifdef _DEBUG |
| 655 |
/* |
| 656 |
* The existence of the "debug" element of the tcl_platform array indicates |
| 657 |
* that this particular Tcl shell has been compiled with debug information. |
| 658 |
* Using "info exists tcl_platform(debug)" a Tcl script can direct the |
| 659 |
* interpreter to load debug versions of DLLs with the load command. |
| 660 |
*/ |
| 661 |
|
| 662 |
Tcl_SetVar2(interp, "tcl_platform", "debug", "1", |
| 663 |
TCL_GLOBAL_ONLY); |
| 664 |
#endif |
| 665 |
|
| 666 |
/* |
| 667 |
* Set up the HOME environment variable from the HOMEDRIVE & HOMEPATH |
| 668 |
* environment variables, if necessary. |
| 669 |
*/ |
| 670 |
|
| 671 |
Tcl_DStringInit(&ds); |
| 672 |
ptr = Tcl_GetVar2(interp, "env", "HOME", TCL_GLOBAL_ONLY); |
| 673 |
if (ptr == NULL) { |
| 674 |
ptr = Tcl_GetVar2(interp, "env", "HOMEDRIVE", TCL_GLOBAL_ONLY); |
| 675 |
if (ptr != NULL) { |
| 676 |
Tcl_DStringAppend(&ds, ptr, -1); |
| 677 |
} |
| 678 |
ptr = Tcl_GetVar2(interp, "env", "HOMEPATH", TCL_GLOBAL_ONLY); |
| 679 |
if (ptr != NULL) { |
| 680 |
Tcl_DStringAppend(&ds, ptr, -1); |
| 681 |
} |
| 682 |
if (Tcl_DStringLength(&ds) > 0) { |
| 683 |
Tcl_SetVar2(interp, "env", "HOME", Tcl_DStringValue(&ds), |
| 684 |
TCL_GLOBAL_ONLY); |
| 685 |
} else { |
| 686 |
Tcl_SetVar2(interp, "env", "HOME", "c:\\", TCL_GLOBAL_ONLY); |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
/* |
| 691 |
* Initialize the user name from the environment first, since this is much |
| 692 |
* faster than asking the system. |
| 693 |
*/ |
| 694 |
|
| 695 |
Tcl_DStringInit( &ds ); |
| 696 |
if (TclGetEnv("USERNAME", &ds) == NULL) { |
| 697 |
|
| 698 |
if ( GetUserName( szUserName, &dwUserNameLen ) != 0 ) { |
| 699 |
Tcl_WinTCharToUtf( szUserName, dwUserNameLen, &ds ); |
| 700 |
} |
| 701 |
} |
| 702 |
Tcl_SetVar2(interp, "tcl_platform", "user", Tcl_DStringValue(&ds), |
| 703 |
TCL_GLOBAL_ONLY); |
| 704 |
Tcl_DStringFree(&ds); |
| 705 |
} |
| 706 |
|
| 707 |
/* |
| 708 |
*---------------------------------------------------------------------- |
| 709 |
* |
| 710 |
* TclpFindVariable -- |
| 711 |
* |
| 712 |
* Locate the entry in environ for a given name. On Unix this |
| 713 |
* routine is case sensetive, on Windows this matches mioxed case. |
| 714 |
* |
| 715 |
* Results: |
| 716 |
* The return value is the index in environ of an entry with the |
| 717 |
* name "name", or -1 if there is no such entry. The integer at |
| 718 |
* *lengthPtr is filled in with the length of name (if a matching |
| 719 |
* entry is found) or the length of the environ array (if no matching |
| 720 |
* entry is found). |
| 721 |
* |
| 722 |
* Side effects: |
| 723 |
* None. |
| 724 |
* |
| 725 |
*---------------------------------------------------------------------- |
| 726 |
*/ |
| 727 |
|
| 728 |
int |
| 729 |
TclpFindVariable(name, lengthPtr) |
| 730 |
CONST char *name; /* Name of desired environment variable |
| 731 |
* (UTF-8). */ |
| 732 |
int *lengthPtr; /* Used to return length of name (for |
| 733 |
* successful searches) or number of non-NULL |
| 734 |
* entries in environ (for unsuccessful |
| 735 |
* searches). */ |
| 736 |
{ |
| 737 |
int i, length, result = -1; |
| 738 |
register CONST char *env, *p1, *p2; |
| 739 |
char *envUpper, *nameUpper; |
| 740 |
Tcl_DString envString; |
| 741 |
|
| 742 |
/* |
| 743 |
* Convert the name to all upper case for the case insensitive |
| 744 |
* comparison. |
| 745 |
*/ |
| 746 |
|
| 747 |
length = strlen(name); |
| 748 |
nameUpper = (char *) ckalloc((unsigned) length+1); |
| 749 |
memcpy((VOID *) nameUpper, (VOID *) name, (size_t) length+1); |
| 750 |
Tcl_UtfToUpper(nameUpper); |
| 751 |
|
| 752 |
Tcl_DStringInit(&envString); |
| 753 |
for (i = 0, env = environ[i]; env != NULL; i++, env = environ[i]) { |
| 754 |
/* |
| 755 |
* Chop the env string off after the equal sign, then Convert |
| 756 |
* the name to all upper case, so we do not have to convert |
| 757 |
* all the characters after the equal sign. |
| 758 |
*/ |
| 759 |
|
| 760 |
envUpper = Tcl_ExternalToUtfDString(NULL, env, -1, &envString); |
| 761 |
p1 = strchr(envUpper, '='); |
| 762 |
if (p1 == NULL) { |
| 763 |
continue; |
| 764 |
} |
| 765 |
length = (int) (p1 - envUpper); |
| 766 |
Tcl_DStringSetLength(&envString, length+1); |
| 767 |
Tcl_UtfToUpper(envUpper); |
| 768 |
|
| 769 |
p1 = envUpper; |
| 770 |
p2 = nameUpper; |
| 771 |
for (; *p2 == *p1; p1++, p2++) { |
| 772 |
/* NULL loop body. */ |
| 773 |
} |
| 774 |
if ((*p1 == '=') && (*p2 == '\0')) { |
| 775 |
*lengthPtr = length; |
| 776 |
result = i; |
| 777 |
goto done; |
| 778 |
} |
| 779 |
|
| 780 |
Tcl_DStringFree(&envString); |
| 781 |
} |
| 782 |
|
| 783 |
*lengthPtr = i; |
| 784 |
|
| 785 |
done: |
| 786 |
Tcl_DStringFree(&envString); |
| 787 |
ckfree(nameUpper); |
| 788 |
return result; |
| 789 |
} |
| 790 |
|
| 791 |
/* |
| 792 |
*---------------------------------------------------------------------- |
| 793 |
* |
| 794 |
* Tcl_Init -- |
| 795 |
* |
| 796 |
* This procedure is typically invoked by Tcl_AppInit procedures |
| 797 |
* to perform additional initialization for a Tcl interpreter, |
| 798 |
* such as sourcing the "init.tcl" script. |
| 799 |
* |
| 800 |
* Results: |
| 801 |
* Returns a standard Tcl completion code and sets the interp's |
| 802 |
* result if there is an error. |
| 803 |
* |
| 804 |
* Side effects: |
| 805 |
* Depends on what's in the init.tcl script. |
| 806 |
* |
| 807 |
*---------------------------------------------------------------------- |
| 808 |
*/ |
| 809 |
|
| 810 |
int |
| 811 |
Tcl_Init(interp) |
| 812 |
Tcl_Interp *interp; /* Interpreter to initialize. */ |
| 813 |
{ |
| 814 |
Tcl_Obj *pathPtr; |
| 815 |
|
| 816 |
if (tclPreInitScript != NULL) { |
| 817 |
if (Tcl_Eval(interp, tclPreInitScript) == TCL_ERROR) { |
| 818 |
return (TCL_ERROR); |
| 819 |
}; |
| 820 |
} |
| 821 |
|
| 822 |
pathPtr = TclGetLibraryPath(); |
| 823 |
if (pathPtr == NULL) { |
| 824 |
pathPtr = Tcl_NewObj(); |
| 825 |
} |
| 826 |
Tcl_SetVar2Ex(interp, "tcl_libPath", NULL, pathPtr, TCL_GLOBAL_ONLY); |
| 827 |
return Tcl_Eval(interp, initScript); |
| 828 |
} |
| 829 |
|
| 830 |
/* |
| 831 |
*---------------------------------------------------------------------- |
| 832 |
* |
| 833 |
* Tcl_SourceRCFile -- |
| 834 |
* |
| 835 |
* This procedure is typically invoked by Tcl_Main of Tk_Main |
| 836 |
* procedure to source an application specific rc file into the |
| 837 |
* interpreter at startup time. |
| 838 |
* |
| 839 |
* Results: |
| 840 |
* None. |
| 841 |
* |
| 842 |
* Side effects: |
| 843 |
* Depends on what's in the rc script. |
| 844 |
* |
| 845 |
*---------------------------------------------------------------------- |
| 846 |
*/ |
| 847 |
|
| 848 |
void |
| 849 |
Tcl_SourceRCFile(interp) |
| 850 |
Tcl_Interp *interp; /* Interpreter to source rc file into. */ |
| 851 |
{ |
| 852 |
Tcl_DString temp; |
| 853 |
CONST char *fileName; |
| 854 |
Tcl_Channel errChannel; |
| 855 |
|
| 856 |
fileName = Tcl_GetVar(interp, "tcl_rcFileName", TCL_GLOBAL_ONLY); |
| 857 |
|
| 858 |
if (fileName != NULL) { |
| 859 |
Tcl_Channel c; |
| 860 |
CONST char *fullName; |
| 861 |
|
| 862 |
Tcl_DStringInit(&temp); |
| 863 |
fullName = Tcl_TranslateFileName(interp, fileName, &temp); |
| 864 |
if (fullName == NULL) { |
| 865 |
/* |
| 866 |
* Couldn't translate the file name (e.g. it referred to a |
| 867 |
* bogus user or there was no HOME environment variable). |
| 868 |
* Just do nothing. |
| 869 |
*/ |
| 870 |
} else { |
| 871 |
|
| 872 |
/* |
| 873 |
* Test for the existence of the rc file before trying to read it. |
| 874 |
*/ |
| 875 |
|
| 876 |
c = Tcl_OpenFileChannel(NULL, fullName, "r", 0); |
| 877 |
if (c != (Tcl_Channel) NULL) { |
| 878 |
Tcl_Close(NULL, c); |
| 879 |
if (Tcl_EvalFile(interp, fullName) != TCL_OK) { |
| 880 |
errChannel = Tcl_GetStdChannel(TCL_STDERR); |
| 881 |
if (errChannel) { |
| 882 |
Tcl_WriteObj(errChannel, Tcl_GetObjResult(interp)); |
| 883 |
Tcl_WriteChars(errChannel, "\n", 1); |
| 884 |
} |
| 885 |
} |
| 886 |
} |
| 887 |
} |
| 888 |
Tcl_DStringFree(&temp); |
| 889 |
} |
| 890 |
} |