| 1 |
/* |
| 2 |
* tclLoad.c -- |
| 3 |
* |
| 4 |
* This file provides the generic portion (those that are the same on all |
| 5 |
* platforms) of Tcl's dynamic loading facilities. |
| 6 |
* |
| 7 |
* Copyright (c) 1995-1997 Sun Microsystems, Inc. |
| 8 |
* |
| 9 |
* See the file "license.terms" for information on usage and redistribution of |
| 10 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "tclInt.h" |
| 14 |
|
| 15 |
/* |
| 16 |
* The following structure describes a package that has been loaded either |
| 17 |
* dynamically (with the "load" command) or statically (as indicated by a call |
| 18 |
* to TclGetLoadedPackages). All such packages are linked together into a |
| 19 |
* single list for the process. Packages are never unloaded, until the |
| 20 |
* application exits, when TclFinalizeLoad is called, and these structures are |
| 21 |
* freed. |
| 22 |
*/ |
| 23 |
|
| 24 |
typedef struct LoadedPackage { |
| 25 |
char *fileName; /* Name of the file from which the package was |
| 26 |
* loaded. An empty string means the package |
| 27 |
* is loaded statically. Malloc-ed. */ |
| 28 |
char *packageName; /* Name of package prefix for the package, |
| 29 |
* properly capitalized (first letter UC, |
| 30 |
* others LC), as in "Net". |
| 31 |
* Malloc-ed. */ |
| 32 |
Tcl_LoadHandle loadHandle; /* Token for the loaded file which should be |
| 33 |
* passed to (*unLoadProcPtr)() when the file |
| 34 |
* is no longer needed. If fileName is NULL, |
| 35 |
* then this field is irrelevant. */ |
| 36 |
Tcl_PackageInitProc *initProc; |
| 37 |
/* Initialization function to call to |
| 38 |
* incorporate this package into a trusted |
| 39 |
* interpreter. */ |
| 40 |
Tcl_PackageInitProc *safeInitProc; |
| 41 |
/* Initialization function to call to |
| 42 |
* incorporate this package into a safe |
| 43 |
* interpreter (one that will execute |
| 44 |
* untrusted scripts). NULL means the package |
| 45 |
* can't be used in unsafe interpreters. */ |
| 46 |
Tcl_PackageUnloadProc *unloadProc; |
| 47 |
/* Finalisation function to unload a package |
| 48 |
* from a trusted interpreter. NULL means that |
| 49 |
* the package cannot be unloaded. */ |
| 50 |
Tcl_PackageUnloadProc *safeUnloadProc; |
| 51 |
/* Finalisation function to unload a package |
| 52 |
* from a safe interpreter. NULL means that |
| 53 |
* the package cannot be unloaded. */ |
| 54 |
int interpRefCount; /* How many times the package has been loaded |
| 55 |
* in trusted interpreters. */ |
| 56 |
int safeInterpRefCount; /* How many times the package has been loaded |
| 57 |
* in safe interpreters. */ |
| 58 |
struct LoadedPackage *nextPtr; |
| 59 |
/* Next in list of all packages loaded into |
| 60 |
* this application process. NULL means end of |
| 61 |
* list. */ |
| 62 |
} LoadedPackage; |
| 63 |
|
| 64 |
/* |
| 65 |
* TCL_THREADS |
| 66 |
* There is a global list of packages that is anchored at firstPackagePtr. |
| 67 |
* Access to this list is governed by a mutex. |
| 68 |
*/ |
| 69 |
|
| 70 |
static LoadedPackage *firstPackagePtr = NULL; |
| 71 |
/* First in list of all packages loaded into |
| 72 |
* this process. */ |
| 73 |
|
| 74 |
TCL_DECLARE_MUTEX(packageMutex) |
| 75 |
|
| 76 |
/* |
| 77 |
* The following structure represents a particular package that has been |
| 78 |
* incorporated into a particular interpreter (by calling its initialization |
| 79 |
* function). There is a list of these structures for each interpreter, with |
| 80 |
* an AssocData value (key "load") for the interpreter that points to the |
| 81 |
* first package (if any). |
| 82 |
*/ |
| 83 |
|
| 84 |
typedef struct InterpPackage { |
| 85 |
LoadedPackage *pkgPtr; /* Points to detailed information about |
| 86 |
* package. */ |
| 87 |
struct InterpPackage *nextPtr; |
| 88 |
/* Next package in this interpreter, or NULL |
| 89 |
* for end of list. */ |
| 90 |
} InterpPackage; |
| 91 |
|
| 92 |
/* |
| 93 |
* Prototypes for functions that are private to this file: |
| 94 |
*/ |
| 95 |
|
| 96 |
static void LoadCleanupProc(ClientData clientData, |
| 97 |
Tcl_Interp *interp); |
| 98 |
|
| 99 |
/* |
| 100 |
*---------------------------------------------------------------------- |
| 101 |
* |
| 102 |
* Tcl_LoadObjCmd -- |
| 103 |
* |
| 104 |
* This function is invoked to process the "load" Tcl command. See the |
| 105 |
* user documentation for details on what it does. |
| 106 |
* |
| 107 |
* Results: |
| 108 |
* A standard Tcl result. |
| 109 |
* |
| 110 |
* Side effects: |
| 111 |
* See the user documentation. |
| 112 |
* |
| 113 |
*---------------------------------------------------------------------- |
| 114 |
*/ |
| 115 |
|
| 116 |
int |
| 117 |
Tcl_LoadObjCmd( |
| 118 |
ClientData dummy, /* Not used. */ |
| 119 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 120 |
int objc, /* Number of arguments. */ |
| 121 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 122 |
{ |
| 123 |
Tcl_Interp *target; |
| 124 |
LoadedPackage *pkgPtr, *defaultPtr; |
| 125 |
Tcl_DString prefix, tmp, initName, safeInitName; |
| 126 |
Tcl_DString unloadName, safeUnloadName; |
| 127 |
InterpPackage *ipFirstPtr, *ipPtr; |
| 128 |
int code, namesMatch, filesMatch, offset; |
| 129 |
const char *symbols[2]; |
| 130 |
Tcl_PackageInitProc *initProc; |
| 131 |
const char *p, *fullFileName, *packageName; |
| 132 |
Tcl_LoadHandle loadHandle; |
| 133 |
Tcl_UniChar ch = 0; |
| 134 |
unsigned len; |
| 135 |
int index, flags = 0; |
| 136 |
Tcl_Obj *const *savedobjv = objv; |
| 137 |
static const char *const options[] = { |
| 138 |
"-global", "-lazy", "--", NULL |
| 139 |
}; |
| 140 |
enum options { |
| 141 |
LOAD_GLOBAL, LOAD_LAZY, LOAD_LAST |
| 142 |
}; |
| 143 |
|
| 144 |
while (objc > 2) { |
| 145 |
if (TclGetString(objv[1])[0] != '-') { |
| 146 |
break; |
| 147 |
} |
| 148 |
if (Tcl_GetIndexFromObj(interp, objv[1], options, "option", 0, |
| 149 |
&index) != TCL_OK) { |
| 150 |
return TCL_ERROR; |
| 151 |
} |
| 152 |
++objv; --objc; |
| 153 |
if (LOAD_GLOBAL == (enum options) index) { |
| 154 |
flags |= TCL_LOAD_GLOBAL; |
| 155 |
} else if (LOAD_LAZY == (enum options) index) { |
| 156 |
flags |= TCL_LOAD_LAZY; |
| 157 |
} else { |
| 158 |
break; |
| 159 |
} |
| 160 |
} |
| 161 |
if ((objc < 2) || (objc > 4)) { |
| 162 |
Tcl_WrongNumArgs(interp, 1, savedobjv, "?-global? ?-lazy? ?--? fileName ?packageName? ?interp?"); |
| 163 |
return TCL_ERROR; |
| 164 |
} |
| 165 |
if (Tcl_FSConvertToPathType(interp, objv[1]) != TCL_OK) { |
| 166 |
return TCL_ERROR; |
| 167 |
} |
| 168 |
fullFileName = TclGetString(objv[1]); |
| 169 |
|
| 170 |
Tcl_DStringInit(&prefix); |
| 171 |
Tcl_DStringInit(&initName); |
| 172 |
Tcl_DStringInit(&safeInitName); |
| 173 |
Tcl_DStringInit(&unloadName); |
| 174 |
Tcl_DStringInit(&safeUnloadName); |
| 175 |
Tcl_DStringInit(&tmp); |
| 176 |
|
| 177 |
packageName = NULL; |
| 178 |
if (objc >= 3) { |
| 179 |
packageName = TclGetString(objv[2]); |
| 180 |
if (packageName[0] == '\0') { |
| 181 |
packageName = NULL; |
| 182 |
} |
| 183 |
} |
| 184 |
if ((fullFileName[0] == 0) && (packageName == NULL)) { |
| 185 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 186 |
"must specify either file name or package name", -1)); |
| 187 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "LOAD", "NOLIBRARY", |
| 188 |
(char *)NULL); |
| 189 |
code = TCL_ERROR; |
| 190 |
goto done; |
| 191 |
} |
| 192 |
|
| 193 |
/* |
| 194 |
* Figure out which interpreter we're going to load the package into. |
| 195 |
*/ |
| 196 |
|
| 197 |
target = interp; |
| 198 |
if (objc == 4) { |
| 199 |
const char *childIntName = TclGetString(objv[3]); |
| 200 |
|
| 201 |
target = Tcl_GetChild(interp, childIntName); |
| 202 |
if (target == NULL) { |
| 203 |
code = TCL_ERROR; |
| 204 |
goto done; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/* |
| 209 |
* Scan through the packages that are currently loaded to see if the |
| 210 |
* package we want is already loaded. We'll use a loaded package if it |
| 211 |
* meets any of the following conditions: |
| 212 |
* - Its name and file match the once we're looking for. |
| 213 |
* - Its file matches, and we weren't given a name. |
| 214 |
* - Its name matches, the file name was specified as empty, and there is |
| 215 |
* only no statically loaded package with the same name. |
| 216 |
*/ |
| 217 |
|
| 218 |
Tcl_MutexLock(&packageMutex); |
| 219 |
|
| 220 |
defaultPtr = NULL; |
| 221 |
for (pkgPtr = firstPackagePtr; pkgPtr != NULL; pkgPtr = pkgPtr->nextPtr) { |
| 222 |
if (packageName == NULL) { |
| 223 |
namesMatch = 0; |
| 224 |
} else { |
| 225 |
TclDStringClear(&prefix); |
| 226 |
Tcl_DStringAppend(&prefix, packageName, -1); |
| 227 |
TclDStringClear(&tmp); |
| 228 |
Tcl_DStringAppend(&tmp, pkgPtr->packageName, -1); |
| 229 |
Tcl_UtfToLower(Tcl_DStringValue(&prefix)); |
| 230 |
Tcl_UtfToLower(Tcl_DStringValue(&tmp)); |
| 231 |
if (strcmp(Tcl_DStringValue(&tmp), |
| 232 |
Tcl_DStringValue(&prefix)) == 0) { |
| 233 |
namesMatch = 1; |
| 234 |
} else { |
| 235 |
namesMatch = 0; |
| 236 |
} |
| 237 |
} |
| 238 |
TclDStringClear(&prefix); |
| 239 |
|
| 240 |
filesMatch = (strcmp(pkgPtr->fileName, fullFileName) == 0); |
| 241 |
if (filesMatch && (namesMatch || (packageName == NULL))) { |
| 242 |
break; |
| 243 |
} |
| 244 |
if (namesMatch && (fullFileName[0] == 0)) { |
| 245 |
defaultPtr = pkgPtr; |
| 246 |
} |
| 247 |
if (filesMatch && !namesMatch && (fullFileName[0] != 0)) { |
| 248 |
/* |
| 249 |
* Can't have two different packages loaded from the same file. |
| 250 |
*/ |
| 251 |
|
| 252 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 253 |
"file \"%s\" is already loaded for package \"%s\"", |
| 254 |
fullFileName, pkgPtr->packageName)); |
| 255 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "LOAD", |
| 256 |
"SPLITPERSONALITY", (char *)NULL); |
| 257 |
code = TCL_ERROR; |
| 258 |
Tcl_MutexUnlock(&packageMutex); |
| 259 |
goto done; |
| 260 |
} |
| 261 |
} |
| 262 |
Tcl_MutexUnlock(&packageMutex); |
| 263 |
if (pkgPtr == NULL) { |
| 264 |
pkgPtr = defaultPtr; |
| 265 |
} |
| 266 |
|
| 267 |
/* |
| 268 |
* Scan through the list of packages already loaded in the target |
| 269 |
* interpreter. If the package we want is already loaded there, then |
| 270 |
* there's nothing for us to do. |
| 271 |
*/ |
| 272 |
|
| 273 |
if (pkgPtr != NULL) { |
| 274 |
ipFirstPtr = Tcl_GetAssocData(target, "tclLoad", NULL); |
| 275 |
for (ipPtr = ipFirstPtr; ipPtr != NULL; ipPtr = ipPtr->nextPtr) { |
| 276 |
if (ipPtr->pkgPtr == pkgPtr) { |
| 277 |
code = TCL_OK; |
| 278 |
goto done; |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
if (pkgPtr == NULL) { |
| 284 |
/* |
| 285 |
* The desired file isn't currently loaded, so load it. It's an error |
| 286 |
* if the desired package is a static one. |
| 287 |
*/ |
| 288 |
|
| 289 |
if (fullFileName[0] == 0) { |
| 290 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 291 |
"package \"%s\" isn't loaded statically", packageName)); |
| 292 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "LOAD", "NOTSTATIC", |
| 293 |
(char *)NULL); |
| 294 |
code = TCL_ERROR; |
| 295 |
goto done; |
| 296 |
} |
| 297 |
|
| 298 |
/* |
| 299 |
* Figure out the module name if it wasn't provided explicitly. |
| 300 |
*/ |
| 301 |
|
| 302 |
if (packageName != NULL) { |
| 303 |
Tcl_DStringAppend(&prefix, packageName, -1); |
| 304 |
} else { |
| 305 |
int retc; |
| 306 |
|
| 307 |
/* |
| 308 |
* Threading note - this call used to be protected by a mutex. |
| 309 |
*/ |
| 310 |
|
| 311 |
retc = TclGuessPackageName(fullFileName, &prefix); |
| 312 |
if (!retc) { |
| 313 |
Tcl_Obj *splitPtr, *pkgGuessPtr; |
| 314 |
int pElements; |
| 315 |
const char *pkgGuess; |
| 316 |
|
| 317 |
/* |
| 318 |
* The platform-specific code couldn't figure out the module |
| 319 |
* name. Make a guess by taking the last element of the file |
| 320 |
* name, stripping off any leading "lib", and then using all |
| 321 |
* of the alphabetic and underline characters that follow |
| 322 |
* that. |
| 323 |
*/ |
| 324 |
|
| 325 |
splitPtr = Tcl_FSSplitPath(objv[1], &pElements); |
| 326 |
Tcl_ListObjIndex(NULL, splitPtr, pElements -1, &pkgGuessPtr); |
| 327 |
pkgGuess = TclGetString(pkgGuessPtr); |
| 328 |
if ((pkgGuess[0] == 'l') && (pkgGuess[1] == 'i') |
| 329 |
&& (pkgGuess[2] == 'b')) { |
| 330 |
pkgGuess += 3; |
| 331 |
} |
| 332 |
#ifdef __CYGWIN__ |
| 333 |
if ((pkgGuess[0] == 'c') && (pkgGuess[1] == 'y') |
| 334 |
&& (pkgGuess[2] == 'g')) { |
| 335 |
pkgGuess += 3; |
| 336 |
} |
| 337 |
#endif /* __CYGWIN__ */ |
| 338 |
for (p = pkgGuess; *p != 0; p += offset) { |
| 339 |
offset = TclUtfToUniChar(p, &ch); |
| 340 |
if ((ch > 0x100) |
| 341 |
|| !(isalpha(UCHAR(ch)) /* INTL: ISO only */ |
| 342 |
|| (UCHAR(ch) == '_'))) { |
| 343 |
break; |
| 344 |
} |
| 345 |
} |
| 346 |
if (p == pkgGuess) { |
| 347 |
Tcl_DecrRefCount(splitPtr); |
| 348 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 349 |
"couldn't figure out package name for %s", |
| 350 |
fullFileName)); |
| 351 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "LOAD", |
| 352 |
"WHATPACKAGE", (char *)NULL); |
| 353 |
code = TCL_ERROR; |
| 354 |
goto done; |
| 355 |
} |
| 356 |
Tcl_DStringAppend(&prefix, pkgGuess, p - pkgGuess); |
| 357 |
Tcl_DecrRefCount(splitPtr); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
/* |
| 362 |
* Fix the capitalization in the package name so that the first |
| 363 |
* character is in caps (or title case) but the others are all |
| 364 |
* lower-case. |
| 365 |
*/ |
| 366 |
|
| 367 |
Tcl_DStringSetLength(&prefix, |
| 368 |
Tcl_UtfToTitle(Tcl_DStringValue(&prefix))); |
| 369 |
|
| 370 |
/* |
| 371 |
* Compute the names of the two initialization functions, based on the |
| 372 |
* package name. |
| 373 |
*/ |
| 374 |
|
| 375 |
TclDStringAppendDString(&initName, &prefix); |
| 376 |
TclDStringAppendLiteral(&initName, "_Init"); |
| 377 |
TclDStringAppendDString(&safeInitName, &prefix); |
| 378 |
TclDStringAppendLiteral(&safeInitName, "_SafeInit"); |
| 379 |
TclDStringAppendDString(&unloadName, &prefix); |
| 380 |
TclDStringAppendLiteral(&unloadName, "_Unload"); |
| 381 |
TclDStringAppendDString(&safeUnloadName, &prefix); |
| 382 |
TclDStringAppendLiteral(&safeUnloadName, "_SafeUnload"); |
| 383 |
|
| 384 |
/* |
| 385 |
* Call platform-specific code to load the package and find the two |
| 386 |
* initialization functions. |
| 387 |
*/ |
| 388 |
|
| 389 |
symbols[0] = Tcl_DStringValue(&initName); |
| 390 |
symbols[1] = NULL; |
| 391 |
|
| 392 |
Tcl_MutexLock(&packageMutex); |
| 393 |
code = Tcl_LoadFile(interp, objv[1], symbols, flags, &initProc, |
| 394 |
&loadHandle); |
| 395 |
Tcl_MutexUnlock(&packageMutex); |
| 396 |
if (code != TCL_OK) { |
| 397 |
goto done; |
| 398 |
} |
| 399 |
|
| 400 |
/* |
| 401 |
* Create a new record to describe this package. |
| 402 |
*/ |
| 403 |
|
| 404 |
pkgPtr = ckalloc(sizeof(LoadedPackage)); |
| 405 |
len = strlen(fullFileName) + 1; |
| 406 |
pkgPtr->fileName = ckalloc(len); |
| 407 |
memcpy(pkgPtr->fileName, fullFileName, len); |
| 408 |
len = (unsigned) Tcl_DStringLength(&prefix) + 1; |
| 409 |
pkgPtr->packageName = ckalloc(len); |
| 410 |
memcpy(pkgPtr->packageName, Tcl_DStringValue(&prefix), len); |
| 411 |
pkgPtr->loadHandle = loadHandle; |
| 412 |
pkgPtr->initProc = initProc; |
| 413 |
pkgPtr->safeInitProc = (Tcl_PackageInitProc *) |
| 414 |
Tcl_FindSymbol(interp, loadHandle, |
| 415 |
Tcl_DStringValue(&safeInitName)); |
| 416 |
pkgPtr->unloadProc = (Tcl_PackageUnloadProc *) |
| 417 |
Tcl_FindSymbol(interp, loadHandle, |
| 418 |
Tcl_DStringValue(&unloadName)); |
| 419 |
pkgPtr->safeUnloadProc = (Tcl_PackageUnloadProc *) |
| 420 |
Tcl_FindSymbol(interp, loadHandle, |
| 421 |
Tcl_DStringValue(&safeUnloadName)); |
| 422 |
pkgPtr->interpRefCount = 0; |
| 423 |
pkgPtr->safeInterpRefCount = 0; |
| 424 |
|
| 425 |
Tcl_MutexLock(&packageMutex); |
| 426 |
pkgPtr->nextPtr = firstPackagePtr; |
| 427 |
firstPackagePtr = pkgPtr; |
| 428 |
Tcl_MutexUnlock(&packageMutex); |
| 429 |
|
| 430 |
/* |
| 431 |
* The Tcl_FindSymbol calls may have left a spurious error message in |
| 432 |
* the interpreter result. |
| 433 |
*/ |
| 434 |
|
| 435 |
Tcl_ResetResult(interp); |
| 436 |
} |
| 437 |
|
| 438 |
/* |
| 439 |
* Invoke the package's initialization function (either the normal one or |
| 440 |
* the safe one, depending on whether or not the interpreter is safe). |
| 441 |
*/ |
| 442 |
|
| 443 |
if (Tcl_IsSafe(target)) { |
| 444 |
if (pkgPtr->safeInitProc == NULL) { |
| 445 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 446 |
"can't use package in a safe interpreter: no" |
| 447 |
" %s_SafeInit procedure", pkgPtr->packageName)); |
| 448 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "LOAD", "UNSAFE", |
| 449 |
(char *)NULL); |
| 450 |
code = TCL_ERROR; |
| 451 |
goto done; |
| 452 |
} |
| 453 |
code = pkgPtr->safeInitProc(target); |
| 454 |
} else { |
| 455 |
if (pkgPtr->initProc == NULL) { |
| 456 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 457 |
"can't attach package to interpreter: no %s_Init procedure", |
| 458 |
pkgPtr->packageName)); |
| 459 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "LOAD", "ENTRYPOINT", |
| 460 |
(char *)NULL); |
| 461 |
code = TCL_ERROR; |
| 462 |
goto done; |
| 463 |
} |
| 464 |
code = pkgPtr->initProc(target); |
| 465 |
} |
| 466 |
|
| 467 |
/* |
| 468 |
* Test for whether the initialization failed. If so, transfer the error |
| 469 |
* from the target interpreter to the originating one. |
| 470 |
*/ |
| 471 |
|
| 472 |
if (code != TCL_OK) { |
| 473 |
Tcl_TransferResult(target, code, interp); |
| 474 |
goto done; |
| 475 |
} |
| 476 |
|
| 477 |
/* |
| 478 |
* Record the fact that the package has been loaded in the target |
| 479 |
* interpreter. |
| 480 |
* |
| 481 |
* Update the proper reference count. |
| 482 |
*/ |
| 483 |
|
| 484 |
Tcl_MutexLock(&packageMutex); |
| 485 |
if (Tcl_IsSafe(target)) { |
| 486 |
pkgPtr->safeInterpRefCount++; |
| 487 |
} else { |
| 488 |
pkgPtr->interpRefCount++; |
| 489 |
} |
| 490 |
Tcl_MutexUnlock(&packageMutex); |
| 491 |
|
| 492 |
/* |
| 493 |
* Refetch ipFirstPtr: loading the package may have introduced additional |
| 494 |
* static packages at the head of the linked list! |
| 495 |
*/ |
| 496 |
|
| 497 |
ipFirstPtr = Tcl_GetAssocData(target, "tclLoad", NULL); |
| 498 |
ipPtr = ckalloc(sizeof(InterpPackage)); |
| 499 |
ipPtr->pkgPtr = pkgPtr; |
| 500 |
ipPtr->nextPtr = ipFirstPtr; |
| 501 |
Tcl_SetAssocData(target, "tclLoad", LoadCleanupProc, ipPtr); |
| 502 |
|
| 503 |
done: |
| 504 |
Tcl_DStringFree(&prefix); |
| 505 |
Tcl_DStringFree(&initName); |
| 506 |
Tcl_DStringFree(&safeInitName); |
| 507 |
Tcl_DStringFree(&unloadName); |
| 508 |
Tcl_DStringFree(&safeUnloadName); |
| 509 |
Tcl_DStringFree(&tmp); |
| 510 |
return code; |
| 511 |
} |
| 512 |
|
| 513 |
/* |
| 514 |
*---------------------------------------------------------------------- |
| 515 |
* |
| 516 |
* Tcl_UnloadObjCmd -- |
| 517 |
* |
| 518 |
* This function is invoked to process the "unload" Tcl command. See the |
| 519 |
* user documentation for details on what it does. |
| 520 |
* |
| 521 |
* Results: |
| 522 |
* A standard Tcl result. |
| 523 |
* |
| 524 |
* Side effects: |
| 525 |
* See the user documentation. |
| 526 |
* |
| 527 |
*---------------------------------------------------------------------- |
| 528 |
*/ |
| 529 |
|
| 530 |
int |
| 531 |
Tcl_UnloadObjCmd( |
| 532 |
ClientData dummy, /* Not used. */ |
| 533 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 534 |
int objc, /* Number of arguments. */ |
| 535 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 536 |
{ |
| 537 |
Tcl_Interp *target; /* Which interpreter to unload from. */ |
| 538 |
LoadedPackage *pkgPtr, *defaultPtr; |
| 539 |
Tcl_DString prefix, tmp; |
| 540 |
Tcl_PackageUnloadProc *unloadProc; |
| 541 |
InterpPackage *ipFirstPtr, *ipPtr; |
| 542 |
int i, index, code, complain = 1, keepLibrary = 0; |
| 543 |
int trustedRefCount = -1, safeRefCount = -1; |
| 544 |
const char *fullFileName = ""; |
| 545 |
const char *packageName; |
| 546 |
static const char *const options[] = { |
| 547 |
"-nocomplain", "-keeplibrary", "--", NULL |
| 548 |
}; |
| 549 |
enum options { |
| 550 |
UNLOAD_NOCOMPLAIN, UNLOAD_KEEPLIB, UNLOAD_LAST |
| 551 |
}; |
| 552 |
|
| 553 |
for (i = 1; i < objc; i++) { |
| 554 |
if (Tcl_GetIndexFromObj(interp, objv[i], options, "option", 0, |
| 555 |
&index) != TCL_OK) { |
| 556 |
fullFileName = TclGetString(objv[i]); |
| 557 |
if (fullFileName[0] == '-') { |
| 558 |
/* |
| 559 |
* It looks like the command contains an option so signal an |
| 560 |
* error |
| 561 |
*/ |
| 562 |
|
| 563 |
return TCL_ERROR; |
| 564 |
} else { |
| 565 |
/* |
| 566 |
* This clearly isn't an option; assume it's the filename. We |
| 567 |
* must clear the error. |
| 568 |
*/ |
| 569 |
|
| 570 |
Tcl_ResetResult(interp); |
| 571 |
break; |
| 572 |
} |
| 573 |
} |
| 574 |
switch (index) { |
| 575 |
case UNLOAD_NOCOMPLAIN: /* -nocomplain */ |
| 576 |
complain = 0; |
| 577 |
break; |
| 578 |
case UNLOAD_KEEPLIB: /* -keeplibrary */ |
| 579 |
keepLibrary = 1; |
| 580 |
break; |
| 581 |
case UNLOAD_LAST: /* -- */ |
| 582 |
i++; |
| 583 |
goto endOfForLoop; |
| 584 |
} |
| 585 |
} |
| 586 |
endOfForLoop: |
| 587 |
if ((objc-i < 1) || (objc-i > 3)) { |
| 588 |
Tcl_WrongNumArgs(interp, 1, objv, |
| 589 |
"?-switch ...? fileName ?packageName? ?interp?"); |
| 590 |
return TCL_ERROR; |
| 591 |
} |
| 592 |
if (Tcl_FSConvertToPathType(interp, objv[i]) != TCL_OK) { |
| 593 |
return TCL_ERROR; |
| 594 |
} |
| 595 |
|
| 596 |
fullFileName = TclGetString(objv[i]); |
| 597 |
Tcl_DStringInit(&prefix); |
| 598 |
Tcl_DStringInit(&tmp); |
| 599 |
|
| 600 |
packageName = NULL; |
| 601 |
if (objc - i >= 2) { |
| 602 |
packageName = TclGetString(objv[i+1]); |
| 603 |
if (packageName[0] == '\0') { |
| 604 |
packageName = NULL; |
| 605 |
} |
| 606 |
} |
| 607 |
if ((fullFileName[0] == 0) && (packageName == NULL)) { |
| 608 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 609 |
"must specify either file name or package name", -1)); |
| 610 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "UNLOAD", "NOLIBRARY", |
| 611 |
(char *)NULL); |
| 612 |
code = TCL_ERROR; |
| 613 |
goto done; |
| 614 |
} |
| 615 |
|
| 616 |
/* |
| 617 |
* Figure out which interpreter we're going to load the package into. |
| 618 |
*/ |
| 619 |
|
| 620 |
target = interp; |
| 621 |
if (objc - i == 3) { |
| 622 |
const char *childIntName = TclGetString(objv[i + 2]); |
| 623 |
|
| 624 |
target = Tcl_GetChild(interp, childIntName); |
| 625 |
if (target == NULL) { |
| 626 |
return TCL_ERROR; |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
/* |
| 631 |
* Scan through the packages that are currently loaded to see if the |
| 632 |
* package we want is already loaded. We'll use a loaded package if it |
| 633 |
* meets any of the following conditions: |
| 634 |
* - Its name and file match the once we're looking for. |
| 635 |
* - Its file matches, and we weren't given a name. |
| 636 |
* - Its name matches, the file name was specified as empty, and there is |
| 637 |
* only no statically loaded package with the same name. |
| 638 |
*/ |
| 639 |
|
| 640 |
Tcl_MutexLock(&packageMutex); |
| 641 |
|
| 642 |
defaultPtr = NULL; |
| 643 |
for (pkgPtr = firstPackagePtr; pkgPtr != NULL; pkgPtr = pkgPtr->nextPtr) { |
| 644 |
int namesMatch, filesMatch; |
| 645 |
|
| 646 |
if (packageName == NULL) { |
| 647 |
namesMatch = 0; |
| 648 |
} else { |
| 649 |
TclDStringClear(&prefix); |
| 650 |
Tcl_DStringAppend(&prefix, packageName, -1); |
| 651 |
TclDStringClear(&tmp); |
| 652 |
Tcl_DStringAppend(&tmp, pkgPtr->packageName, -1); |
| 653 |
Tcl_UtfToLower(Tcl_DStringValue(&prefix)); |
| 654 |
Tcl_UtfToLower(Tcl_DStringValue(&tmp)); |
| 655 |
if (strcmp(Tcl_DStringValue(&tmp), |
| 656 |
Tcl_DStringValue(&prefix)) == 0) { |
| 657 |
namesMatch = 1; |
| 658 |
} else { |
| 659 |
namesMatch = 0; |
| 660 |
} |
| 661 |
} |
| 662 |
TclDStringClear(&prefix); |
| 663 |
|
| 664 |
filesMatch = (strcmp(pkgPtr->fileName, fullFileName) == 0); |
| 665 |
if (filesMatch && (namesMatch || (packageName == NULL))) { |
| 666 |
break; |
| 667 |
} |
| 668 |
if (namesMatch && (fullFileName[0] == 0)) { |
| 669 |
defaultPtr = pkgPtr; |
| 670 |
} |
| 671 |
if (filesMatch && !namesMatch && (fullFileName[0] != 0)) { |
| 672 |
break; |
| 673 |
} |
| 674 |
} |
| 675 |
Tcl_MutexUnlock(&packageMutex); |
| 676 |
if (fullFileName[0] == 0) { |
| 677 |
/* |
| 678 |
* It's an error to try unload a static package. |
| 679 |
*/ |
| 680 |
|
| 681 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 682 |
"package \"%s\" is loaded statically and cannot be unloaded", |
| 683 |
packageName)); |
| 684 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "UNLOAD", "STATIC", |
| 685 |
(char *)NULL); |
| 686 |
code = TCL_ERROR; |
| 687 |
goto done; |
| 688 |
} |
| 689 |
if (pkgPtr == NULL) { |
| 690 |
/* |
| 691 |
* The DLL pointed by the provided filename has never been loaded. |
| 692 |
*/ |
| 693 |
|
| 694 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 695 |
"file \"%s\" has never been loaded", fullFileName)); |
| 696 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "UNLOAD", "NEVERLOADED", |
| 697 |
(char *)NULL); |
| 698 |
code = TCL_ERROR; |
| 699 |
goto done; |
| 700 |
} |
| 701 |
|
| 702 |
/* |
| 703 |
* Scan through the list of packages already loaded in the target |
| 704 |
* interpreter. If the package we want is already loaded there, then we |
| 705 |
* should proceed with unloading. |
| 706 |
*/ |
| 707 |
|
| 708 |
code = TCL_ERROR; |
| 709 |
if (pkgPtr != NULL) { |
| 710 |
ipFirstPtr = Tcl_GetAssocData(target, "tclLoad", NULL); |
| 711 |
for (ipPtr = ipFirstPtr; ipPtr != NULL; ipPtr = ipPtr->nextPtr) { |
| 712 |
if (ipPtr->pkgPtr == pkgPtr) { |
| 713 |
code = TCL_OK; |
| 714 |
break; |
| 715 |
} |
| 716 |
} |
| 717 |
} |
| 718 |
if (code != TCL_OK) { |
| 719 |
/* |
| 720 |
* The package has not been loaded in this interpreter. |
| 721 |
*/ |
| 722 |
|
| 723 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 724 |
"file \"%s\" has never been loaded in this interpreter", |
| 725 |
fullFileName)); |
| 726 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "UNLOAD", "NEVERLOADED", |
| 727 |
(char *)NULL); |
| 728 |
code = TCL_ERROR; |
| 729 |
goto done; |
| 730 |
} |
| 731 |
|
| 732 |
/* |
| 733 |
* Ensure that the DLL can be unloaded. If it is a trusted interpreter, |
| 734 |
* pkgPtr->unloadProc must not be NULL for the DLL to be unloadable. If |
| 735 |
* the interpreter is a safe one, pkgPtr->safeUnloadProc must be non-NULL. |
| 736 |
*/ |
| 737 |
|
| 738 |
if (Tcl_IsSafe(target)) { |
| 739 |
if (pkgPtr->safeUnloadProc == NULL) { |
| 740 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 741 |
"file \"%s\" cannot be unloaded under a safe interpreter", |
| 742 |
fullFileName)); |
| 743 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "UNLOAD", "CANNOT", |
| 744 |
(char *)NULL); |
| 745 |
code = TCL_ERROR; |
| 746 |
goto done; |
| 747 |
} |
| 748 |
unloadProc = pkgPtr->safeUnloadProc; |
| 749 |
} else { |
| 750 |
if (pkgPtr->unloadProc == NULL) { |
| 751 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 752 |
"file \"%s\" cannot be unloaded under a trusted interpreter", |
| 753 |
fullFileName)); |
| 754 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "UNLOAD", "CANNOT", |
| 755 |
(char *)NULL); |
| 756 |
code = TCL_ERROR; |
| 757 |
goto done; |
| 758 |
} |
| 759 |
unloadProc = pkgPtr->unloadProc; |
| 760 |
} |
| 761 |
|
| 762 |
/* |
| 763 |
* We are ready to unload the package. First, evaluate the unload |
| 764 |
* function. If this fails, we cannot proceed with unload. Also, we must |
| 765 |
* specify the proper flag to pass to the unload callback. |
| 766 |
* TCL_UNLOAD_DETACH_FROM_INTERPRETER is defined when the callback should |
| 767 |
* only remove itself from the interpreter; the library will be unloaded |
| 768 |
* in a future call of unload. In case the library will be unloaded just |
| 769 |
* after the callback returns, TCL_UNLOAD_DETACH_FROM_PROCESS is passed. |
| 770 |
*/ |
| 771 |
|
| 772 |
code = TCL_UNLOAD_DETACH_FROM_INTERPRETER; |
| 773 |
if (!keepLibrary) { |
| 774 |
Tcl_MutexLock(&packageMutex); |
| 775 |
trustedRefCount = pkgPtr->interpRefCount; |
| 776 |
safeRefCount = pkgPtr->safeInterpRefCount; |
| 777 |
Tcl_MutexUnlock(&packageMutex); |
| 778 |
|
| 779 |
if (Tcl_IsSafe(target)) { |
| 780 |
safeRefCount--; |
| 781 |
} else { |
| 782 |
trustedRefCount--; |
| 783 |
} |
| 784 |
|
| 785 |
if (safeRefCount <= 0 && trustedRefCount <= 0) { |
| 786 |
code = TCL_UNLOAD_DETACH_FROM_PROCESS; |
| 787 |
} |
| 788 |
} |
| 789 |
code = unloadProc(target, code); |
| 790 |
if (code != TCL_OK) { |
| 791 |
Tcl_TransferResult(target, code, interp); |
| 792 |
goto done; |
| 793 |
} |
| 794 |
|
| 795 |
/* |
| 796 |
* The unload function executed fine. Examine the reference count to see |
| 797 |
* if we unload the DLL. |
| 798 |
*/ |
| 799 |
|
| 800 |
Tcl_MutexLock(&packageMutex); |
| 801 |
if (Tcl_IsSafe(target)) { |
| 802 |
pkgPtr->safeInterpRefCount--; |
| 803 |
|
| 804 |
/* |
| 805 |
* Do not let counter get negative. |
| 806 |
*/ |
| 807 |
|
| 808 |
if (pkgPtr->safeInterpRefCount < 0) { |
| 809 |
pkgPtr->safeInterpRefCount = 0; |
| 810 |
} |
| 811 |
} else { |
| 812 |
pkgPtr->interpRefCount--; |
| 813 |
|
| 814 |
/* |
| 815 |
* Do not let counter get negative. |
| 816 |
*/ |
| 817 |
|
| 818 |
if (pkgPtr->interpRefCount < 0) { |
| 819 |
pkgPtr->interpRefCount = 0; |
| 820 |
} |
| 821 |
} |
| 822 |
trustedRefCount = pkgPtr->interpRefCount; |
| 823 |
safeRefCount = pkgPtr->safeInterpRefCount; |
| 824 |
Tcl_MutexUnlock(&packageMutex); |
| 825 |
|
| 826 |
code = TCL_OK; |
| 827 |
if (pkgPtr->safeInterpRefCount <= 0 && pkgPtr->interpRefCount <= 0 |
| 828 |
&& !keepLibrary) { |
| 829 |
/* |
| 830 |
* Unload the shared library from the application memory... |
| 831 |
*/ |
| 832 |
|
| 833 |
#if defined(TCL_UNLOAD_DLLS) || defined(_WIN32) |
| 834 |
/* |
| 835 |
* Some Unix dlls are poorly behaved - registering things like atexit |
| 836 |
* calls that can't be unregistered. If you unload such dlls, you get |
| 837 |
* a core on exit because it wants to call a function in the dll after |
| 838 |
* it's been unloaded. |
| 839 |
*/ |
| 840 |
|
| 841 |
if (pkgPtr->fileName[0] != '\0') { |
| 842 |
Tcl_MutexLock(&packageMutex); |
| 843 |
if (Tcl_FSUnloadFile(interp, pkgPtr->loadHandle) == TCL_OK) { |
| 844 |
/* |
| 845 |
* Remove this library from the loaded library cache. |
| 846 |
*/ |
| 847 |
|
| 848 |
defaultPtr = pkgPtr; |
| 849 |
if (defaultPtr == firstPackagePtr) { |
| 850 |
firstPackagePtr = pkgPtr->nextPtr; |
| 851 |
} else { |
| 852 |
for (pkgPtr = firstPackagePtr; pkgPtr != NULL; |
| 853 |
pkgPtr = pkgPtr->nextPtr) { |
| 854 |
if (pkgPtr->nextPtr == defaultPtr) { |
| 855 |
pkgPtr->nextPtr = defaultPtr->nextPtr; |
| 856 |
break; |
| 857 |
} |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
/* |
| 862 |
* Remove this library from the interpreter's library cache. |
| 863 |
*/ |
| 864 |
|
| 865 |
ipFirstPtr = Tcl_GetAssocData(target, "tclLoad", NULL); |
| 866 |
ipPtr = ipFirstPtr; |
| 867 |
if (ipPtr->pkgPtr == defaultPtr) { |
| 868 |
ipFirstPtr = ipFirstPtr->nextPtr; |
| 869 |
} else { |
| 870 |
InterpPackage *ipPrevPtr; |
| 871 |
|
| 872 |
for (ipPrevPtr = ipPtr; ipPtr != NULL; |
| 873 |
ipPrevPtr = ipPtr, ipPtr = ipPtr->nextPtr) { |
| 874 |
if (ipPtr->pkgPtr == defaultPtr) { |
| 875 |
ipPrevPtr->nextPtr = ipPtr->nextPtr; |
| 876 |
break; |
| 877 |
} |
| 878 |
} |
| 879 |
} |
| 880 |
Tcl_SetAssocData(target, "tclLoad", LoadCleanupProc, |
| 881 |
ipFirstPtr); |
| 882 |
ckfree(defaultPtr->fileName); |
| 883 |
ckfree(defaultPtr->packageName); |
| 884 |
ckfree(defaultPtr); |
| 885 |
ckfree(ipPtr); |
| 886 |
Tcl_MutexUnlock(&packageMutex); |
| 887 |
} else { |
| 888 |
code = TCL_ERROR; |
| 889 |
} |
| 890 |
} |
| 891 |
#else |
| 892 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 893 |
"file \"%s\" cannot be unloaded: unloading disabled", |
| 894 |
fullFileName)); |
| 895 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "UNLOAD", "DISABLED", |
| 896 |
(char *)NULL); |
| 897 |
code = TCL_ERROR; |
| 898 |
#endif |
| 899 |
} |
| 900 |
|
| 901 |
done: |
| 902 |
Tcl_DStringFree(&prefix); |
| 903 |
Tcl_DStringFree(&tmp); |
| 904 |
if (!complain && (code != TCL_OK)) { |
| 905 |
code = TCL_OK; |
| 906 |
Tcl_ResetResult(interp); |
| 907 |
} |
| 908 |
return code; |
| 909 |
} |
| 910 |
|
| 911 |
/* |
| 912 |
*---------------------------------------------------------------------- |
| 913 |
* |
| 914 |
* Tcl_StaticPackage -- |
| 915 |
* |
| 916 |
* This function is invoked to indicate that a particular package has |
| 917 |
* been linked statically with an application. |
| 918 |
* |
| 919 |
* Results: |
| 920 |
* None. |
| 921 |
* |
| 922 |
* Side effects: |
| 923 |
* Once this function completes, the package becomes loadable via the |
| 924 |
* "load" command with an empty file name. |
| 925 |
* |
| 926 |
*---------------------------------------------------------------------- |
| 927 |
*/ |
| 928 |
|
| 929 |
void |
| 930 |
Tcl_StaticPackage( |
| 931 |
Tcl_Interp *interp, /* If not NULL, it means that the package has |
| 932 |
* already been loaded into the given |
| 933 |
* interpreter by calling the appropriate init |
| 934 |
* proc. */ |
| 935 |
const char *prefix, /* Prefix (must be properly |
| 936 |
* capitalized: first letter upper case, |
| 937 |
* others lower case). */ |
| 938 |
Tcl_PackageInitProc *initProc, |
| 939 |
/* Function to call to incorporate this |
| 940 |
* package into a trusted interpreter. */ |
| 941 |
Tcl_PackageInitProc *safeInitProc) |
| 942 |
/* Function to call to incorporate this |
| 943 |
* package into a safe interpreter (one that |
| 944 |
* will execute untrusted scripts). NULL means |
| 945 |
* the package can't be used in safe |
| 946 |
* interpreters. */ |
| 947 |
{ |
| 948 |
LoadedPackage *pkgPtr; |
| 949 |
InterpPackage *ipPtr, *ipFirstPtr; |
| 950 |
|
| 951 |
/* |
| 952 |
* Check to see if someone else has already reported this package as |
| 953 |
* statically loaded in the process. |
| 954 |
*/ |
| 955 |
|
| 956 |
Tcl_MutexLock(&packageMutex); |
| 957 |
for (pkgPtr = firstPackagePtr; pkgPtr != NULL; pkgPtr = pkgPtr->nextPtr) { |
| 958 |
if ((pkgPtr->initProc == initProc) |
| 959 |
&& (pkgPtr->safeInitProc == safeInitProc) |
| 960 |
&& (strcmp(pkgPtr->packageName, prefix) == 0)) { |
| 961 |
break; |
| 962 |
} |
| 963 |
} |
| 964 |
Tcl_MutexUnlock(&packageMutex); |
| 965 |
|
| 966 |
/* |
| 967 |
* If the package is not yet recorded as being loaded statically, add it |
| 968 |
* to the list now. |
| 969 |
*/ |
| 970 |
|
| 971 |
if (pkgPtr == NULL) { |
| 972 |
pkgPtr = ckalloc(sizeof(LoadedPackage)); |
| 973 |
pkgPtr->fileName = ckalloc(1); |
| 974 |
pkgPtr->fileName[0] = 0; |
| 975 |
pkgPtr->packageName = ckalloc(strlen(prefix) + 1); |
| 976 |
strcpy(pkgPtr->packageName, prefix); |
| 977 |
pkgPtr->loadHandle = NULL; |
| 978 |
pkgPtr->initProc = initProc; |
| 979 |
pkgPtr->safeInitProc = safeInitProc; |
| 980 |
Tcl_MutexLock(&packageMutex); |
| 981 |
pkgPtr->nextPtr = firstPackagePtr; |
| 982 |
firstPackagePtr = pkgPtr; |
| 983 |
Tcl_MutexUnlock(&packageMutex); |
| 984 |
} |
| 985 |
|
| 986 |
if (interp != NULL) { |
| 987 |
|
| 988 |
/* |
| 989 |
* If we're loading the package into an interpreter, determine whether |
| 990 |
* it's already loaded. |
| 991 |
*/ |
| 992 |
|
| 993 |
ipFirstPtr = Tcl_GetAssocData(interp, "tclLoad", NULL); |
| 994 |
for (ipPtr = ipFirstPtr; ipPtr != NULL; ipPtr = ipPtr->nextPtr) { |
| 995 |
if (ipPtr->pkgPtr == pkgPtr) { |
| 996 |
return; |
| 997 |
} |
| 998 |
} |
| 999 |
|
| 1000 |
/* |
| 1001 |
* Package isn't loade in the current interp yet. Mark it as now being |
| 1002 |
* loaded. |
| 1003 |
*/ |
| 1004 |
|
| 1005 |
ipPtr = ckalloc(sizeof(InterpPackage)); |
| 1006 |
ipPtr->pkgPtr = pkgPtr; |
| 1007 |
ipPtr->nextPtr = ipFirstPtr; |
| 1008 |
Tcl_SetAssocData(interp, "tclLoad", LoadCleanupProc, ipPtr); |
| 1009 |
} |
| 1010 |
} |
| 1011 |
|
| 1012 |
/* |
| 1013 |
*---------------------------------------------------------------------- |
| 1014 |
* |
| 1015 |
* TclGetLoadedPackages -- |
| 1016 |
* |
| 1017 |
* This function returns information about all of the files that are |
| 1018 |
* loaded (either in a particular interpreter, or for all interpreters). |
| 1019 |
* |
| 1020 |
* Results: |
| 1021 |
* The return value is a standard Tcl completion code. If successful, a |
| 1022 |
* list of lists is placed in the interp's result. Each sublist |
| 1023 |
* corresponds to one loaded file; its first element is the name of the |
| 1024 |
* file (or an empty string for something that's statically loaded) and |
| 1025 |
* the second element is the name of the package in that file. |
| 1026 |
* |
| 1027 |
* Side effects: |
| 1028 |
* None. |
| 1029 |
* |
| 1030 |
*---------------------------------------------------------------------- |
| 1031 |
*/ |
| 1032 |
|
| 1033 |
int |
| 1034 |
TclGetLoadedPackages( |
| 1035 |
Tcl_Interp *interp, /* Interpreter in which to return information |
| 1036 |
* or error message. */ |
| 1037 |
const char *targetName) /* Name of target interpreter or NULL. If |
| 1038 |
* NULL, return info about all interps; |
| 1039 |
* otherwise, just return info about this |
| 1040 |
* interpreter. */ |
| 1041 |
{ |
| 1042 |
Tcl_Interp *target; |
| 1043 |
LoadedPackage *pkgPtr; |
| 1044 |
InterpPackage *ipPtr; |
| 1045 |
Tcl_Obj *resultObj, *pkgDesc[2]; |
| 1046 |
|
| 1047 |
if (targetName == NULL) { |
| 1048 |
/* |
| 1049 |
* Return information about all of the available packages. |
| 1050 |
*/ |
| 1051 |
|
| 1052 |
TclNewObj(resultObj); |
| 1053 |
Tcl_MutexLock(&packageMutex); |
| 1054 |
for (pkgPtr = firstPackagePtr; pkgPtr != NULL; |
| 1055 |
pkgPtr = pkgPtr->nextPtr) { |
| 1056 |
pkgDesc[0] = Tcl_NewStringObj(pkgPtr->fileName, -1); |
| 1057 |
pkgDesc[1] = Tcl_NewStringObj(pkgPtr->packageName, -1); |
| 1058 |
Tcl_ListObjAppendElement(NULL, resultObj, |
| 1059 |
Tcl_NewListObj(2, pkgDesc)); |
| 1060 |
} |
| 1061 |
Tcl_MutexUnlock(&packageMutex); |
| 1062 |
Tcl_SetObjResult(interp, resultObj); |
| 1063 |
return TCL_OK; |
| 1064 |
} |
| 1065 |
|
| 1066 |
/* |
| 1067 |
* Return information about only the packages that are loaded in a given |
| 1068 |
* interpreter. |
| 1069 |
*/ |
| 1070 |
|
| 1071 |
target = Tcl_GetChild(interp, targetName); |
| 1072 |
if (target == NULL) { |
| 1073 |
return TCL_ERROR; |
| 1074 |
} |
| 1075 |
ipPtr = Tcl_GetAssocData(target, "tclLoad", NULL); |
| 1076 |
TclNewObj(resultObj); |
| 1077 |
for (; ipPtr != NULL; ipPtr = ipPtr->nextPtr) { |
| 1078 |
pkgPtr = ipPtr->pkgPtr; |
| 1079 |
pkgDesc[0] = Tcl_NewStringObj(pkgPtr->fileName, -1); |
| 1080 |
pkgDesc[1] = Tcl_NewStringObj(pkgPtr->packageName, -1); |
| 1081 |
Tcl_ListObjAppendElement(NULL, resultObj, Tcl_NewListObj(2, pkgDesc)); |
| 1082 |
} |
| 1083 |
Tcl_SetObjResult(interp, resultObj); |
| 1084 |
return TCL_OK; |
| 1085 |
} |
| 1086 |
|
| 1087 |
/* |
| 1088 |
*---------------------------------------------------------------------- |
| 1089 |
* |
| 1090 |
* LoadCleanupProc -- |
| 1091 |
* |
| 1092 |
* This function is called to delete all of the InterpPackage structures |
| 1093 |
* for an interpreter when the interpreter is deleted. It gets invoked |
| 1094 |
* via the Tcl AssocData mechanism. |
| 1095 |
* |
| 1096 |
* Results: |
| 1097 |
* None. |
| 1098 |
* |
| 1099 |
* Side effects: |
| 1100 |
* Storage for all of the InterpPackage functions for interp get deleted. |
| 1101 |
* |
| 1102 |
*---------------------------------------------------------------------- |
| 1103 |
*/ |
| 1104 |
|
| 1105 |
static void |
| 1106 |
LoadCleanupProc( |
| 1107 |
ClientData clientData, /* Pointer to first InterpPackage structure |
| 1108 |
* for interp. */ |
| 1109 |
Tcl_Interp *interp) /* Interpreter that is being deleted. */ |
| 1110 |
{ |
| 1111 |
InterpPackage *ipPtr, *nextPtr; |
| 1112 |
|
| 1113 |
ipPtr = clientData; |
| 1114 |
while (ipPtr != NULL) { |
| 1115 |
nextPtr = ipPtr->nextPtr; |
| 1116 |
ckfree(ipPtr); |
| 1117 |
ipPtr = nextPtr; |
| 1118 |
} |
| 1119 |
} |
| 1120 |
|
| 1121 |
/* |
| 1122 |
*---------------------------------------------------------------------- |
| 1123 |
* |
| 1124 |
* TclFinalizeLoad -- |
| 1125 |
* |
| 1126 |
* This function is invoked just before the application exits. It frees |
| 1127 |
* all of the LoadedPackage structures. |
| 1128 |
* |
| 1129 |
* Results: |
| 1130 |
* None. |
| 1131 |
* |
| 1132 |
* Side effects: |
| 1133 |
* Memory is freed. |
| 1134 |
* |
| 1135 |
*---------------------------------------------------------------------- |
| 1136 |
*/ |
| 1137 |
|
| 1138 |
void |
| 1139 |
TclFinalizeLoad(void) |
| 1140 |
{ |
| 1141 |
LoadedPackage *pkgPtr; |
| 1142 |
|
| 1143 |
/* |
| 1144 |
* No synchronization here because there should just be one thread alive |
| 1145 |
* at this point. Logically, packageMutex should be grabbed at this point, |
| 1146 |
* but the Mutexes get finalized before the call to this routine. The only |
| 1147 |
* subsystem left alive at this point is the memory allocator. |
| 1148 |
*/ |
| 1149 |
|
| 1150 |
while (firstPackagePtr != NULL) { |
| 1151 |
pkgPtr = firstPackagePtr; |
| 1152 |
firstPackagePtr = pkgPtr->nextPtr; |
| 1153 |
|
| 1154 |
#if defined(TCL_UNLOAD_DLLS) || defined(_WIN32) |
| 1155 |
/* |
| 1156 |
* Some Unix dlls are poorly behaved - registering things like atexit |
| 1157 |
* calls that can't be unregistered. If you unload such dlls, you get |
| 1158 |
* a core on exit because it wants to call a function in the dll after |
| 1159 |
* it has been unloaded. |
| 1160 |
*/ |
| 1161 |
|
| 1162 |
if (pkgPtr->fileName[0] != '\0') { |
| 1163 |
Tcl_FSUnloadFile(NULL, pkgPtr->loadHandle); |
| 1164 |
} |
| 1165 |
#endif |
| 1166 |
|
| 1167 |
ckfree(pkgPtr->fileName); |
| 1168 |
ckfree(pkgPtr->packageName); |
| 1169 |
ckfree(pkgPtr); |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
/* |
| 1174 |
* Local Variables: |
| 1175 |
* mode: c |
| 1176 |
* c-basic-offset: 4 |
| 1177 |
* fill-column: 78 |
| 1178 |
* End: |
| 1179 |
*/ |