Debian Codegraph @

pTk/mTk/tclGeneric/tclPkg.c

raw symbols SCIP
1 /*
2 * tclPkg.c --
3 *
4 * This file implements package and version control for Tcl via
5 * the "package" command and a few C APIs.
6 *
7 * Copyright (c) 1996 Sun Microsystems, Inc.
8 *
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 *
12 * RCS: @(#) $Id: tclPkg.c,v 1.9 2002/02/22 22:36:09 dgp Exp $
13 */
14
15 #include "tclInt.h"
16
17 /*
18 * Each invocation of the "package ifneeded" command creates a structure
19 * of the following type, which is used to load the package into the
20 * interpreter if it is requested with a "package require" command.
21 */
22
23 typedef struct PkgAvail {
24 char *version; /* Version string; malloc'ed. */
25 char *script; /* Script to invoke to provide this version
26 * of the package. Malloc'ed and protected
27 * by Tcl_Preserve and Tcl_Release. */
28 struct PkgAvail *nextPtr; /* Next in list of available versions of
29 * the same package. */
30 } PkgAvail;
31
32 /*
33 * For each package that is known in any way to an interpreter, there
34 * is one record of the following type. These records are stored in
35 * the "packageTable" hash table in the interpreter, keyed by
36 * package name such as "Tk" (no version number).
37 */
38
39 typedef struct Package {
40 char *version; /* Version that has been supplied in this
41 * interpreter via "package provide"
42 * (malloc'ed). NULL means the package doesn't
43 * exist in this interpreter yet. */
44 PkgAvail *availPtr; /* First in list of all available versions
45 * of this package. */
46 ClientData clientData; /* Client data. */
47 } Package;
48
49 /*
50 * Prototypes for procedures defined in this file:
51 */
52
53 static int CheckVersion _ANSI_ARGS_((Tcl_Interp *interp,
54 CONST char *string));
55 static int ComparePkgVersions _ANSI_ARGS_((CONST char *v1,
56 CONST char *v2,
57 int *satPtr));
58 static Package * FindPackage _ANSI_ARGS_((Tcl_Interp *interp,
59 CONST char *name));
60
61 /*
62 *----------------------------------------------------------------------
63 *
64 * Tcl_PkgProvide / Tcl_PkgProvideEx --
65 *
66 * This procedure is invoked to declare that a particular version
67 * of a particular package is now present in an interpreter. There
68 * must not be any other version of this package already
69 * provided in the interpreter.
70 *
71 * Results:
72 * Normally returns TCL_OK; if there is already another version
73 * of the package loaded then TCL_ERROR is returned and an error
74 * message is left in the interp's result.
75 *
76 * Side effects:
77 * The interpreter remembers that this package is available,
78 * so that no other version of the package may be provided for
79 * the interpreter.
80 *
81 *----------------------------------------------------------------------
82 */
83
84 int
85 Tcl_PkgProvide(interp, name, version)
86 Tcl_Interp *interp; /* Interpreter in which package is now
87 * available. */
88 CONST char *name; /* Name of package. */
89 CONST char *version; /* Version string for package. */
90 {
91 return Tcl_PkgProvideEx(interp, name, version, (ClientData) NULL);
92 }
93
94 int
95 Tcl_PkgProvideEx(interp, name, version, clientData)
96 Tcl_Interp *interp; /* Interpreter in which package is now
97 * available. */
98 CONST char *name; /* Name of package. */
99 CONST char *version; /* Version string for package. */
100 ClientData clientData; /* clientdata for this package (normally
101 * used for C callback function table) */
102 {
103 Package *pkgPtr;
104
105 pkgPtr = FindPackage(interp, name);
106 if (pkgPtr->version == NULL) {
107 pkgPtr->version = ckalloc((unsigned) (strlen(version) + 1));
108 strcpy(pkgPtr->version, version);
109 pkgPtr->clientData = clientData;
110 return TCL_OK;
111 }
112 if (ComparePkgVersions(pkgPtr->version, version, (int *) NULL) == 0) {
113 if (clientData != NULL) {
114 pkgPtr->clientData = clientData;
115 }
116 return TCL_OK;
117 }
118 Tcl_AppendResult(interp, "conflicting versions provided for package \"",
119 name, "\": ", pkgPtr->version, ", then ", version, (char *) NULL);
120 return TCL_ERROR;
121 }
122
123 /*
124 *----------------------------------------------------------------------
125 *
126 * Tcl_PkgRequire / Tcl_PkgRequireEx --
127 *
128 * This procedure is called by code that depends on a particular
129 * version of a particular package. If the package is not already
130 * provided in the interpreter, this procedure invokes a Tcl script
131 * to provide it. If the package is already provided, this
132 * procedure makes sure that the caller's needs don't conflict with
133 * the version that is present.
134 *
135 * Results:
136 * If successful, returns the version string for the currently
137 * provided version of the package, which may be different from
138 * the "version" argument. If the caller's requirements
139 * cannot be met (e.g. the version requested conflicts with
140 * a currently provided version, or the required version cannot
141 * be found, or the script to provide the required version
142 * generates an error), NULL is returned and an error
143 * message is left in the interp's result.
144 *
145 * Side effects:
146 * The script from some previous "package ifneeded" command may
147 * be invoked to provide the package.
148 *
149 *----------------------------------------------------------------------
150 */
151
152 CONST char *
153 Tcl_PkgRequire(interp, name, version, exact)
154 Tcl_Interp *interp; /* Interpreter in which package is now
155 * available. */
156 CONST char *name; /* Name of desired package. */
157 CONST char *version; /* Version string for desired version;
158 * NULL means use the latest version
159 * available. */
160 int exact; /* Non-zero means that only the particular
161 * version given is acceptable. Zero means
162 * use the latest compatible version. */
163 {
164 return Tcl_PkgRequireEx(interp, name, version, exact, (ClientData *) NULL);
165 }
166
167 CONST char *
168 Tcl_PkgRequireEx(interp, name, version, exact, clientDataPtr)
169 Tcl_Interp *interp; /* Interpreter in which package is now
170 * available. */
171 CONST char *name; /* Name of desired package. */
172 CONST char *version; /* Version string for desired version;
173 * NULL means use the latest version
174 * available. */
175 int exact; /* Non-zero means that only the particular
176 * version given is acceptable. Zero means
177 * use the latest compatible version. */
178 ClientData *clientDataPtr; /* Used to return the client data for this
179 * package. If it is NULL then the client
180 * data is not returned. This is unchanged
181 * if this call fails for any reason. */
182 {
183 Package *pkgPtr;
184 PkgAvail *availPtr, *bestPtr;
185 char *script;
186 int code, satisfies, result, pass;
187 Tcl_DString command;
188
189 /*
190 * If an attempt is being made to load this into a standalone executable
191 * on a platform where backlinking is not supported then this must be
192 * a shared version of Tcl (Otherwise the load would have failed).
193 * Detect this situation by checking that this library has been correctly
194 * initialised. If it has not been then return immediately as nothing will
195 * work.
196 */
197
198 if (tclEmptyStringRep == NULL) {
199
200 /*
201 * OK, so what's going on here?
202 *
203 * First, what are we doing? We are performing a check on behalf of
204 * one particular caller, Tcl_InitStubs(). When a package is
205 * stub-enabled, it is statically linked to libtclstub.a, which
206 * contains a copy of Tcl_InitStubs(). When a stub-enabled package
207 * is loaded, its *_Init() function is supposed to call
208 * Tcl_InitStubs() before calling any other functions in the Tcl
209 * library. The first Tcl function called by Tcl_InitStubs() through
210 * the stub table is Tcl_PkgRequireEx(), so this code right here is
211 * the first code that is part of the original Tcl library in the
212 * executable that gets executed on behalf of a newly loaded
213 * stub-enabled package.
214 *
215 * One easy error for the developer/builder of a stub-enabled package
216 * to make is to forget to define USE_TCL_STUBS when compiling the
217 * package. When that happens, the package will contain symbols
218 * that are references to the Tcl library, rather than function
219 * pointers referencing the stub table. On platforms that lack
220 * backlinking, those unresolved references may cause the loading
221 * of the package to also load a second copy of the Tcl library,
222 * leading to all kinds of trouble. We would like to catch that
223 * error and report a useful message back to the user. That's
224 * what we're doing.
225 *
226 * Second, how does this work? If we reach this point, then the
227 * global variable tclEmptyStringRep has the value NULL. Compare
228 * that with the definition of tclEmptyStringRep near the top of
229 * the file generic/tclObj.c. It clearly should not have the value
230 * NULL; it should point to the char tclEmptyString. If we see it
231 * having the value NULL, then somehow we are seeing a Tcl library
232 * that isn't completely initialized, and that's an indicator for the
233 * error condition described above. (Further explanation is welcome.)
234 *
235 * Third, so what do we do about it? This situation indicates
236 * the package we just loaded wasn't properly compiled to be
237 * stub-enabled, yet it thinks it is stub-enabled (it called
238 * Tcl_InitStubs()). We want to report that the package just
239 * loaded is broken, so we want to place an error message in
240 * the interpreter result and return NULL to indicate failure
241 * to Tcl_InitStubs() so that it will also fail. (Further
242 * explanation why we don't want to Tcl_Panic() is welcome.
243 * After all, two Tcl libraries can't be a good thing!)
244 *
245 * Trouble is that's going to be tricky. We're now using a Tcl
246 * library that's not fully initialized. In particular, it
247 * doesn't have a proper value for tclEmptyStringRep. The
248 * Tcl_Obj system heavily depends on the value of tclEmptyStringRep
249 * and all of Tcl depends (increasingly) on the Tcl_Obj system, we
250 * need to correct that flaw before making the calls to set the
251 * interpreter result to the error message. That's the only flaw
252 * corrected; other problems with initialization of the Tcl library
253 * are not remedied, so be very careful about adding any other calls
254 * here without checking how they behave when initialization is
255 * incomplete.
256 */
257
258 tclEmptyStringRep = &tclEmptyString;
259 Tcl_AppendResult(interp, "Cannot load package \"", name,
260 "\" in standalone executable: This package is not ",
261 "compiled with stub support", NULL);
262 return NULL;
263 }
264
265 /*
266 * It can take up to three passes to find the package: one pass to
267 * run the "package unknown" script, one to run the "package ifneeded"
268 * script for a specific version, and a final pass to lookup the
269 * package loaded by the "package ifneeded" script.
270 */
271
272 for (pass = 1; ; pass++) {
273 pkgPtr = FindPackage(interp, name);
274 if (pkgPtr->version != NULL) {
275 break;
276 }
277
278 /*
279 * The package isn't yet present. Search the list of available
280 * versions and invoke the script for the best available version.
281 */
282
283 bestPtr = NULL;
284 for (availPtr = pkgPtr->availPtr; availPtr != NULL;
285 availPtr = availPtr->nextPtr) {
286 if ((bestPtr != NULL) && (ComparePkgVersions(availPtr->version,
287 bestPtr->version, (int *) NULL) <= 0)) {
288 continue;
289 }
290 if (version != NULL) {
291 result = ComparePkgVersions(availPtr->version, version,
292 &satisfies);
293 if ((result != 0) && exact) {
294 continue;
295 }
296 if (!satisfies) {
297 continue;
298 }
299 }
300 bestPtr = availPtr;
301 }
302 if (bestPtr != NULL) {
303 /*
304 * We found an ifneeded script for the package. Be careful while
305 * executing it: this could cause reentrancy, so (a) protect the
306 * script itself from deletion and (b) don't assume that bestPtr
307 * will still exist when the script completes.
308 */
309
310 script = bestPtr->script;
311 Tcl_Preserve((ClientData) script);
312 code = Tcl_GlobalEval(interp, script);
313 Tcl_Release((ClientData) script);
314 if (code != TCL_OK) {
315 if (code == TCL_ERROR) {
316 Tcl_AddErrorInfo(interp,
317 "\n (\"package ifneeded\" script)");
318 }
319 return NULL;
320 }
321 Tcl_ResetResult(interp);
322 pkgPtr = FindPackage(interp, name);
323 break;
324 }
325
326 /*
327 * Package not in the database. If there is a "package unknown"
328 * command, invoke it (but only on the first pass; after that,
329 * we should not get here in the first place).
330 */
331
332 if (pass > 1) {
333 break;
334 }
335 script = ((Interp *) interp)->packageUnknown;
336 if (script != NULL) {
337 Tcl_DStringInit(&command);
338 Tcl_DStringAppend(&command, script, -1);
339 Tcl_DStringAppendElement(&command, name);
340 Tcl_DStringAppend(&command, " ", 1);
341 Tcl_DStringAppend(&command, (version != NULL) ? version : "{}",
342 -1);
343 if (exact) {
344 Tcl_DStringAppend(&command, " -exact", 7);
345 }
346 code = Tcl_GlobalEval(interp, Tcl_DStringValue(&command));
347 Tcl_DStringFree(&command);
348 if (code != TCL_OK) {
349 if (code == TCL_ERROR) {
350 Tcl_AddErrorInfo(interp,
351 "\n (\"package unknown\" script)");
352 }
353 return NULL;
354 }
355 Tcl_ResetResult(interp);
356 }
357 }
358
359 if (pkgPtr->version == NULL) {
360 Tcl_AppendResult(interp, "can't find package ", name,
361 (char *) NULL);
362 if (version != NULL) {
363 Tcl_AppendResult(interp, " ", version, (char *) NULL);
364 }
365 return NULL;
366 }
367
368 /*
369 * At this point we know that the package is present. Make sure that the
370 * provided version meets the current requirement.
371 */
372
373 if (version == NULL) {
374 if (clientDataPtr) {
375 *clientDataPtr = pkgPtr->clientData;
376 }
377 return pkgPtr->version;
378 }
379 result = ComparePkgVersions(pkgPtr->version, version, &satisfies);
380 if ((satisfies && !exact) || (result == 0)) {
381 if (clientDataPtr) {
382 *clientDataPtr = pkgPtr->clientData;
383 }
384 return pkgPtr->version;
385 }
386 Tcl_AppendResult(interp, "version conflict for package \"",
387 name, "\": have ", pkgPtr->version, ", need ", version,
388 (char *) NULL);
389 return NULL;
390 }
391
392 /*
393 *----------------------------------------------------------------------
394 *
395 * Tcl_PkgPresent / Tcl_PkgPresentEx --
396 *
397 * Checks to see whether the specified package is present. If it
398 * is not then no additional action is taken.
399 *
400 * Results:
401 * If successful, returns the version string for the currently
402 * provided version of the package, which may be different from
403 * the "version" argument. If the caller's requirements
404 * cannot be met (e.g. the version requested conflicts with
405 * a currently provided version), NULL is returned and an error
406 * message is left in interp->result.
407 *
408 * Side effects:
409 * None.
410 *
411 *----------------------------------------------------------------------
412 */
413
414 CONST char *
415 Tcl_PkgPresent(interp, name, version, exact)
416 Tcl_Interp *interp; /* Interpreter in which package is now
417 * available. */
418 CONST char *name; /* Name of desired package. */
419 CONST char *version; /* Version string for desired version;
420 * NULL means use the latest version
421 * available. */
422 int exact; /* Non-zero means that only the particular
423 * version given is acceptable. Zero means
424 * use the latest compatible version. */
425 {
426 return Tcl_PkgPresentEx(interp, name, version, exact, (ClientData *) NULL);
427 }
428
429 CONST char *
430 Tcl_PkgPresentEx(interp, name, version, exact, clientDataPtr)
431 Tcl_Interp *interp; /* Interpreter in which package is now
432 * available. */
433 CONST char *name; /* Name of desired package. */
434 CONST char *version; /* Version string for desired version;
435 * NULL means use the latest version
436 * available. */
437 int exact; /* Non-zero means that only the particular
438 * version given is acceptable. Zero means
439 * use the latest compatible version. */
440 ClientData *clientDataPtr; /* Used to return the client data for this
441 * package. If it is NULL then the client
442 * data is not returned. This is unchanged
443 * if this call fails for any reason. */
444 {
445 Interp *iPtr = (Interp *) interp;
446 Tcl_HashEntry *hPtr;
447 Package *pkgPtr;
448 int satisfies, result;
449
450 hPtr = Tcl_FindHashEntry(&iPtr->packageTable, name);
451 if (hPtr) {
452 pkgPtr = (Package *) Tcl_GetHashValue(hPtr);
453 if (pkgPtr->version != NULL) {
454
455 /*
456 * At this point we know that the package is present. Make sure
457 * that the provided version meets the current requirement.
458 */
459
460 if (version == NULL) {
461 if (clientDataPtr) {
462 *clientDataPtr = pkgPtr->clientData;
463 }
464
465 return pkgPtr->version;
466 }
467 result = ComparePkgVersions(pkgPtr->version, version, &satisfies);
468 if ((satisfies && !exact) || (result == 0)) {
469 if (clientDataPtr) {
470 *clientDataPtr = pkgPtr->clientData;
471 }
472
473 return pkgPtr->version;
474 }
475 Tcl_AppendResult(interp, "version conflict for package \"",
476 name, "\": have ", pkgPtr->version,
477 ", need ", version, (char *) NULL);
478 return NULL;
479 }
480 }
481
482 if (version != NULL) {
483 Tcl_AppendResult(interp, "package ", name, " ", version,
484 " is not present", (char *) NULL);
485 } else {
486 Tcl_AppendResult(interp, "package ", name, " is not present",
487 (char *) NULL);
488 }
489 return NULL;
490 }
491
492 /*
493 *----------------------------------------------------------------------
494 *
495 * Tcl_PackageObjCmd --
496 *
497 * This procedure is invoked to process the "package" Tcl command.
498 * See the user documentation for details on what it does.
499 *
500 * Results:
501 * A standard Tcl result.
502 *
503 * Side effects:
504 * See the user documentation.
505 *
506 *----------------------------------------------------------------------
507 */
508
509 /* ARGSUSED */
510 int
511 Tcl_PackageObjCmd(dummy, interp, objc, objv)
512 ClientData dummy; /* Not used. */
513 Tcl_Interp *interp; /* Current interpreter. */
514 int objc; /* Number of arguments. */
515 Tcl_Obj *CONST objv[]; /* Argument objects. */
516 {
517 static CONST char *pkgOptions[] = {
518 "forget", "ifneeded", "names", "present", "provide", "require",
519 "unknown", "vcompare", "versions", "vsatisfies", (char *) NULL
520 };
521 enum pkgOptions {
522 PKG_FORGET, PKG_IFNEEDED, PKG_NAMES, PKG_PRESENT,
523 PKG_PROVIDE, PKG_REQUIRE, PKG_UNKNOWN, PKG_VCOMPARE,
524 PKG_VERSIONS, PKG_VSATISFIES
525 };
526 Interp *iPtr = (Interp *) interp;
527 int optionIndex, exact, i, satisfies;
528 PkgAvail *availPtr, *prevPtr;
529 Package *pkgPtr;
530 Tcl_HashEntry *hPtr;
531 Tcl_HashSearch search;
532 Tcl_HashTable *tablePtr;
533 CONST char *version;
534 char *argv2, *argv3, *argv4;
535
536 if (objc < 2) {
537 Tcl_WrongNumArgs(interp, 1, objv, "option ?arg arg ...?");
538 return TCL_ERROR;
539 }
540
541 if (Tcl_GetIndexFromObj(interp, objv[1], pkgOptions, "option", 0,
542 &optionIndex) != TCL_OK) {
543 return TCL_ERROR;
544 }
545 switch ((enum pkgOptions) optionIndex) {
546 case PKG_FORGET: {
547 char *keyString;
548 for (i = 2; i < objc; i++) {
549 keyString = Tcl_GetString(objv[i]);
550 hPtr = Tcl_FindHashEntry(&iPtr->packageTable, keyString);
551 if (hPtr == NULL) {
552 continue;
553 }
554 pkgPtr = (Package *) Tcl_GetHashValue(hPtr);
555 Tcl_DeleteHashEntry(hPtr);
556 if (pkgPtr->version != NULL) {
557 ckfree(pkgPtr->version);
558 }
559 while (pkgPtr->availPtr != NULL) {
560 availPtr = pkgPtr->availPtr;
561 pkgPtr->availPtr = availPtr->nextPtr;
562 ckfree(availPtr->version);
563 Tcl_EventuallyFree((ClientData)availPtr->script, TCL_DYNAMIC);
564 ckfree((char *) availPtr);
565 }
566 ckfree((char *) pkgPtr);
567 }
568 break;
569 }
570 case PKG_IFNEEDED: {
571 int length;
572 if ((objc != 4) && (objc != 5)) {
573 Tcl_WrongNumArgs(interp, 2, objv, "package version ?script?");
574 return TCL_ERROR;
575 }
576 argv3 = Tcl_GetString(objv[3]);
577 if (CheckVersion(interp, argv3) != TCL_OK) {
578 return TCL_ERROR;
579 }
580 argv2 = Tcl_GetString(objv[2]);
581 if (objc == 4) {
582 hPtr = Tcl_FindHashEntry(&iPtr->packageTable, argv2);
583 if (hPtr == NULL) {
584 return TCL_OK;
585 }
586 pkgPtr = (Package *) Tcl_GetHashValue(hPtr);
587 } else {
588 pkgPtr = FindPackage(interp, argv2);
589 }
590 argv3 = Tcl_GetStringFromObj(objv[3], &length);
591 for (availPtr = pkgPtr->availPtr, prevPtr = NULL; availPtr != NULL;
592 prevPtr = availPtr, availPtr = availPtr->nextPtr) {
593 if (ComparePkgVersions(availPtr->version, argv3, (int *) NULL)
594 == 0) {
595 if (objc == 4) {
596 Tcl_SetResult(interp, availPtr->script, TCL_VOLATILE);
597 return TCL_OK;
598 }
599 Tcl_EventuallyFree((ClientData)availPtr->script, TCL_DYNAMIC);
600 break;
601 }
602 }
603 if (objc == 4) {
604 return TCL_OK;
605 }
606 if (availPtr == NULL) {
607 availPtr = (PkgAvail *) ckalloc(sizeof(PkgAvail));
608 availPtr->version = ckalloc((unsigned) (length + 1));
609 strcpy(availPtr->version, argv3);
610 if (prevPtr == NULL) {
611 availPtr->nextPtr = pkgPtr->availPtr;
612 pkgPtr->availPtr = availPtr;
613 } else {
614 availPtr->nextPtr = prevPtr->nextPtr;
615 prevPtr->nextPtr = availPtr;
616 }
617 }
618 argv4 = Tcl_GetStringFromObj(objv[4], &length);
619 availPtr->script = ckalloc((unsigned) (length + 1));
620 strcpy(availPtr->script, argv4);
621 break;
622 }
623 case PKG_NAMES: {
624 if (objc != 2) {
625 Tcl_WrongNumArgs(interp, 2, objv, NULL);
626 return TCL_ERROR;
627 }
628 tablePtr = &iPtr->packageTable;
629 for (hPtr = Tcl_FirstHashEntry(tablePtr, &search); hPtr != NULL;
630 hPtr = Tcl_NextHashEntry(&search)) {
631 pkgPtr = (Package *) Tcl_GetHashValue(hPtr);
632 if ((pkgPtr->version != NULL) || (pkgPtr->availPtr != NULL)) {
633 Tcl_AppendElement(interp, Tcl_GetHashKey(tablePtr, hPtr));
634 }
635 }
636 break;
637 }
638 case PKG_PRESENT: {
639 if (objc < 3) {
640 presentSyntax:
641 Tcl_WrongNumArgs(interp, 2, objv, "?-exact? package ?version?");
642 return TCL_ERROR;
643 }
644 argv2 = Tcl_GetString(objv[2]);
645 if ((argv2[0] == '-') && (strcmp(argv2, "-exact") == 0)) {
646 exact = 1;
647 } else {
648 exact = 0;
649 }
650 version = NULL;
651 if (objc == (4 + exact)) {
652 version = Tcl_GetString(objv[3 + exact]);
653 if (CheckVersion(interp, version) != TCL_OK) {
654 return TCL_ERROR;
655 }
656 } else if ((objc != 3) || exact) {
657 goto presentSyntax;
658 }
659 if (exact) {
660 argv3 = Tcl_GetString(objv[3]);
661 version = Tcl_PkgPresent(interp, argv3, version, exact);
662 } else {
663 version = Tcl_PkgPresent(interp, argv2, version, exact);
664 }
665 if (version == NULL) {
666 return TCL_ERROR;
667 }
668 Tcl_SetObjResult( interp, Tcl_NewStringObj( version, -1 ) );
669 break;
670 }
671 case PKG_PROVIDE: {
672 if ((objc != 3) && (objc != 4)) {
673 Tcl_WrongNumArgs(interp, 2, objv, "package ?version?");
674 return TCL_ERROR;
675 }
676 argv2 = Tcl_GetString(objv[2]);
677 if (objc == 3) {
678 hPtr = Tcl_FindHashEntry(&iPtr->packageTable, argv2);
679 if (hPtr != NULL) {
680 pkgPtr = (Package *) Tcl_GetHashValue(hPtr);
681 if (pkgPtr->version != NULL) {
682 Tcl_SetResult(interp, pkgPtr->version, TCL_VOLATILE);
683 }
684 }
685 return TCL_OK;
686 }
687 argv3 = Tcl_GetString(objv[3]);
688 if (CheckVersion(interp, argv3) != TCL_OK) {
689 return TCL_ERROR;
690 }
691 return Tcl_PkgProvide(interp, argv2, argv3);
692 }
693 case PKG_REQUIRE: {
694 if (objc < 3) {
695 requireSyntax:
696 Tcl_WrongNumArgs(interp, 2, objv, "?-exact? package ?version?");
697 return TCL_ERROR;
698 }
699 argv2 = Tcl_GetString(objv[2]);
700 if ((argv2[0] == '-') && (strcmp(argv2, "-exact") == 0)) {
701 exact = 1;
702 } else {
703 exact = 0;
704 }
705 version = NULL;
706 if (objc == (4 + exact)) {
707 version = Tcl_GetString(objv[3 + exact]);
708 if (CheckVersion(interp, version) != TCL_OK) {
709 return TCL_ERROR;
710 }
711 } else if ((objc != 3) || exact) {
712 goto requireSyntax;
713 }
714 if (exact) {
715 argv3 = Tcl_GetString(objv[3]);
716 version = Tcl_PkgRequire(interp, argv3, version, exact);
717 } else {
718 version = Tcl_PkgRequire(interp, argv2, version, exact);
719 }
720 if (version == NULL) {
721 return TCL_ERROR;
722 }
723 Tcl_SetObjResult( interp, Tcl_NewStringObj( version, -1 ) );
724 break;
725 }
726 case PKG_UNKNOWN: {
727 int length;
728 if (objc == 2) {
729 if (iPtr->packageUnknown != NULL) {
730 Tcl_SetResult(interp, iPtr->packageUnknown, TCL_VOLATILE);
731 }
732 } else if (objc == 3) {
733 if (iPtr->packageUnknown != NULL) {
734 ckfree(iPtr->packageUnknown);
735 }
736 argv2 = Tcl_GetStringFromObj(objv[2], &length);
737 if (argv2[0] == 0) {
738 iPtr->packageUnknown = NULL;
739 } else {
740 iPtr->packageUnknown = (char *) ckalloc((unsigned)
741 (length + 1));
742 strcpy(iPtr->packageUnknown, argv2);
743 }
744 } else {
745 Tcl_WrongNumArgs(interp, 2, objv, "?command?");
746 return TCL_ERROR;
747 }
748 break;
749 }
750 case PKG_VCOMPARE: {
751 if (objc != 4) {
752 Tcl_WrongNumArgs(interp, 2, objv, "version1 version2");
753 return TCL_ERROR;
754 }
755 argv3 = Tcl_GetString(objv[3]);
756 argv2 = Tcl_GetString(objv[2]);
757 if ((CheckVersion(interp, argv2) != TCL_OK)
758 || (CheckVersion(interp, argv3) != TCL_OK)) {
759 return TCL_ERROR;
760 }
761 Tcl_SetIntObj(Tcl_GetObjResult(interp),
762 ComparePkgVersions(argv2, argv3, (int *) NULL));
763 break;
764 }
765 case PKG_VERSIONS: {
766 if (objc != 3) {
767 Tcl_WrongNumArgs(interp, 2, objv, "package");
768 return TCL_ERROR;
769 }
770 argv2 = Tcl_GetString(objv[2]);
771 hPtr = Tcl_FindHashEntry(&iPtr->packageTable, argv2);
772 if (hPtr != NULL) {
773 pkgPtr = (Package *) Tcl_GetHashValue(hPtr);
774 for (availPtr = pkgPtr->availPtr; availPtr != NULL;
775 availPtr = availPtr->nextPtr) {
776 Tcl_AppendElement(interp, availPtr->version);
777 }
778 }
779 break;
780 }
781 case PKG_VSATISFIES: {
782 if (objc != 4) {
783 Tcl_WrongNumArgs(interp, 2, objv, "version1 version2");
784 return TCL_ERROR;
785 }
786 argv3 = Tcl_GetString(objv[3]);
787 argv2 = Tcl_GetString(objv[2]);
788 if ((CheckVersion(interp, argv2) != TCL_OK)
789 || (CheckVersion(interp, argv3) != TCL_OK)) {
790 return TCL_ERROR;
791 }
792 ComparePkgVersions(argv2, argv3, &satisfies);
793 Tcl_SetIntObj(Tcl_GetObjResult(interp), satisfies);
794 break;
795 }
796 default: {
797 panic("Tcl_PackageObjCmd: bad option index to pkgOptions");
798 }
799 }
800 return TCL_OK;
801 }
802
803 /*
804 *----------------------------------------------------------------------
805 *
806 * FindPackage --
807 *
808 * This procedure finds the Package record for a particular package
809 * in a particular interpreter, creating a record if one doesn't
810 * already exist.
811 *
812 * Results:
813 * The return value is a pointer to the Package record for the
814 * package.
815 *
816 * Side effects:
817 * A new Package record may be created.
818 *
819 *----------------------------------------------------------------------
820 */
821
822 static Package *
823 FindPackage(interp, name)
824 Tcl_Interp *interp; /* Interpreter to use for package lookup. */
825 CONST char *name; /* Name of package to fine. */
826 {
827 Interp *iPtr = (Interp *) interp;
828 Tcl_HashEntry *hPtr;
829 int new;
830 Package *pkgPtr;
831
832 hPtr = Tcl_CreateHashEntry(&iPtr->packageTable, name, &new);
833 if (new) {
834 pkgPtr = (Package *) ckalloc(sizeof(Package));
835 pkgPtr->version = NULL;
836 pkgPtr->availPtr = NULL;
837 pkgPtr->clientData = NULL;
838 Tcl_SetHashValue(hPtr, pkgPtr);
839 } else {
840 pkgPtr = (Package *) Tcl_GetHashValue(hPtr);
841 }
842 return pkgPtr;
843 }
844
845 /*
846 *----------------------------------------------------------------------
847 *
848 * TclFreePackageInfo --
849 *
850 * This procedure is called during interpreter deletion to
851 * free all of the package-related information for the
852 * interpreter.
853 *
854 * Results:
855 * None.
856 *
857 * Side effects:
858 * Memory is freed.
859 *
860 *----------------------------------------------------------------------
861 */
862
863 void
864 TclFreePackageInfo(iPtr)
865 Interp *iPtr; /* Interpereter that is being deleted. */
866 {
867 Package *pkgPtr;
868 Tcl_HashSearch search;
869 Tcl_HashEntry *hPtr;
870 PkgAvail *availPtr;
871
872 for (hPtr = Tcl_FirstHashEntry(&iPtr->packageTable, &search);
873 hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
874 pkgPtr = (Package *) Tcl_GetHashValue(hPtr);
875 if (pkgPtr->version != NULL) {
876 ckfree(pkgPtr->version);
877 }
878 while (pkgPtr->availPtr != NULL) {
879 availPtr = pkgPtr->availPtr;
880 pkgPtr->availPtr = availPtr->nextPtr;
881 ckfree(availPtr->version);
882 Tcl_EventuallyFree((ClientData)availPtr->script, TCL_DYNAMIC);
883 ckfree((char *) availPtr);
884 }
885 ckfree((char *) pkgPtr);
886 }
887 Tcl_DeleteHashTable(&iPtr->packageTable);
888 if (iPtr->packageUnknown != NULL) {
889 ckfree(iPtr->packageUnknown);
890 }
891 }
892
893 /*
894 *----------------------------------------------------------------------
895 *
896 * CheckVersion --
897 *
898 * This procedure checks to see whether a version number has
899 * valid syntax.
900 *
901 * Results:
902 * If string is a properly formed version number the TCL_OK
903 * is returned. Otherwise TCL_ERROR is returned and an error
904 * message is left in the interp's result.
905 *
906 * Side effects:
907 * None.
908 *
909 *----------------------------------------------------------------------
910 */
911
912 static int
913 CheckVersion(interp, string)
914 Tcl_Interp *interp; /* Used for error reporting. */
915 CONST char *string; /* Supposedly a version number, which is
916 * groups of decimal digits separated
917 * by dots. */
918 {
919 CONST char *p = string;
920 char prevChar;
921
922 if (!isdigit(UCHAR(*p))) { /* INTL: digit */
923 goto error;
924 }
925 for (prevChar = *p, p++; *p != 0; p++) {
926 if (!isdigit(UCHAR(*p)) &&
927 ((*p != '.') || (prevChar == '.'))) { /* INTL: digit */
928 goto error;
929 }
930 prevChar = *p;
931 }
932 if (prevChar != '.') {
933 return TCL_OK;
934 }
935
936 error:
937 Tcl_AppendResult(interp, "expected version number but got \"",
938 string, "\"", (char *) NULL);
939 return TCL_ERROR;
940 }
941
942 /*
943 *----------------------------------------------------------------------
944 *
945 * ComparePkgVersions --
946 *
947 * This procedure compares two version numbers.
948 *
949 * Results:
950 * The return value is -1 if v1 is less than v2, 0 if the two
951 * version numbers are the same, and 1 if v1 is greater than v2.
952 * If *satPtr is non-NULL, the word it points to is filled in
953 * with 1 if v2 >= v1 and both numbers have the same major number
954 * or 0 otherwise.
955 *
956 * Side effects:
957 * None.
958 *
959 *----------------------------------------------------------------------
960 */
961
962 static int
963 ComparePkgVersions(v1, v2, satPtr)
964 CONST char *v1;
965 CONST char *v2; /* Versions strings, of form 2.1.3 (any
966 * number of version numbers). */
967 int *satPtr; /* If non-null, the word pointed to is
968 * filled in with a 0/1 value. 1 means
969 * v1 "satisfies" v2: v1 is greater than
970 * or equal to v2 and both version numbers
971 * have the same major number. */
972 {
973 int thisIsMajor, n1, n2;
974
975 /*
976 * Each iteration of the following loop processes one number from
977 * each string, terminated by a ".". If those numbers don't match
978 * then the comparison is over; otherwise, we loop back for the
979 * next number.
980 */
981
982 thisIsMajor = 1;
983 while (1) {
984 /*
985 * Parse one decimal number from the front of each string.
986 */
987
988 n1 = n2 = 0;
989 while ((*v1 != 0) && (*v1 != '.')) {
990 n1 = 10*n1 + (*v1 - '0');
991 v1++;
992 }
993 while ((*v2 != 0) && (*v2 != '.')) {
994 n2 = 10*n2 + (*v2 - '0');
995 v2++;
996 }
997
998 /*
999 * Compare and go on to the next version number if the
1000 * current numbers match.
1001 */
1002
1003 if (n1 != n2) {
1004 break;
1005 }
1006 if (*v1 != 0) {
1007 v1++;
1008 } else if (*v2 == 0) {
1009 break;
1010 }
1011 if (*v2 != 0) {
1012 v2++;
1013 }
1014 thisIsMajor = 0;
1015 }
1016 if (satPtr != NULL) {
1017 *satPtr = (n1 == n2) || ((n1 > n2) && !thisIsMajor);
1018 }
1019 if (n1 > n2) {
1020 return 1;
1021 } else if (n1 == n2) {
1022 return 0;
1023 } else {
1024 return -1;
1025 }
1026 }