2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: tclPkg.c /main/2 1996/08/08 14:45:54 cde-hp $ */
27 * This file implements package and version control for Tcl via
28 * the "package" command and a few C APIs.
30 * Copyright (c) 1996 Sun Microsystems, Inc.
32 * See the file "license.terms" for information on usage and redistribution
33 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
35 * SCCS: @(#) tclPkg.c 1.6 96/02/15 11:43:16
41 * Each invocation of the "package ifneeded" command creates a structure
42 * of the following type, which is used to load the package into the
43 * interpreter if it is requested with a "package require" command.
46 typedef struct PkgAvail {
47 char *version; /* Version string; malloc'ed. */
48 char *script; /* Script to invoke to provide this version
49 * of the package. Malloc'ed and protected
50 * by Tcl_Preserve and Tcl_Release. */
51 struct PkgAvail *nextPtr; /* Next in list of available versions of
52 * the same package. */
56 * For each package that is known in any way to an interpreter, there
57 * is one record of the following type. These records are stored in
58 * the "packageTable" hash table in the interpreter, keyed by
59 * package name such as "Tk" (no version number).
62 typedef struct Package {
63 char *version; /* Version that has been supplied in this
64 * interpreter via "package provide"
65 * (malloc'ed). NULL means the package doesn't
66 * exist in this interpreter yet. */
67 PkgAvail *availPtr; /* First in list of all available versions
72 * Prototypes for procedures defined in this file:
75 static int CheckVersion _ANSI_ARGS_((Tcl_Interp *interp,
77 static int ComparePkgVersions _ANSI_ARGS_((char *v1, char *v2,
79 static Package * FindPackage _ANSI_ARGS_((Tcl_Interp *interp,
83 *----------------------------------------------------------------------
87 * This procedure is invoked to declare that a particular version
88 * of a particular package is now present in an interpreter. There
89 * must not be any other version of this package already
90 * provided in the interpreter.
93 * Normally returns TCL_OK; if there is already another version
94 * of the package loaded then TCL_ERROR is returned and an error
95 * message is left in interp->result.
98 * The interpreter remembers that this package is available,
99 * so that no other version of the package may be provided for
102 *----------------------------------------------------------------------
107 Tcl_Interp *interp, /* Interpreter in which package is now
109 char *name, /* Name of package. */
110 char *version /* Version string for package. */
115 pkgPtr = FindPackage(interp, name);
116 if (pkgPtr->version == NULL) {
117 pkgPtr->version = ckalloc((unsigned) (strlen(version) + 1));
118 strcpy(pkgPtr->version, version);
121 if (ComparePkgVersions(pkgPtr->version, version, (int *) NULL) == 0) {
124 Tcl_AppendResult(interp, "conflicting versions provided for package \"",
125 name, "\": ", pkgPtr->version, ", then ", version, (char *) NULL);
130 *----------------------------------------------------------------------
134 * This procedure is called by code that depends on a particular
135 * version of a particular package. If the package is not already
136 * provided in the interpreter, this procedure invokes a Tcl script
137 * to provide it. If the package is already provided, this
138 * procedure makes sure that the caller's needs don't conflict with
139 * the version that is present.
142 * If successful, returns the version string for the currently
143 * provided version of the package, which may be different from
144 * the "version" argument. If the caller's requirements
145 * cannot be met (e.g. the version requested conflicts with
146 * a currently provided version, or the required version cannot
147 * be found, or the script to provide the required version
148 * generates an error), NULL is returned and an error
149 * message is left in interp->result.
152 * The script from some previous "package ifneeded" command may
153 * be invoked to provide the package.
155 *----------------------------------------------------------------------
160 Tcl_Interp *interp, /* Interpreter in which package is now
162 char *name, /* Name of desired package. */
163 char *version, /* Version string for desired version;
164 * NULL means use the latest version
166 int exact /* Non-zero means that only the particular
167 * version given is acceptable. Zero means
168 * use the latest compatible version. */
172 PkgAvail *availPtr, *bestPtr;
174 int code, satisfies, result, pass;
178 * It can take up to three passes to find the package: one pass to
179 * run the "package unknown" script, one to run the "package ifneeded"
180 * script for a specific version, and a final pass to lookup the
181 * package loaded by the "package ifneeded" script.
184 for (pass = 1; ; pass++) {
185 pkgPtr = FindPackage(interp, name);
186 if (pkgPtr->version != NULL) {
191 * The package isn't yet present. Search the list of available
192 * versions and invoke the script for the best available version.
196 for (availPtr = pkgPtr->availPtr; availPtr != NULL;
197 availPtr = availPtr->nextPtr) {
198 if ((bestPtr != NULL) && (ComparePkgVersions(availPtr->version,
199 bestPtr->version, (int *) NULL) <= 0)) {
202 if (version != NULL) {
203 result = ComparePkgVersions(availPtr->version, version,
205 if ((result != 0) && exact) {
214 if (bestPtr != NULL) {
216 * We found an ifneeded script for the package. Be careful while
217 * executing it: this could cause reentrancy, so (a) protect the
218 * script itself from deletion and (b) don't assume that bestPtr
219 * will still exist when the script completes.
222 script = bestPtr->script;
223 Tcl_Preserve((ClientData) script);
224 code = Tcl_GlobalEval(interp, script);
225 Tcl_Release((ClientData) script);
226 if (code != TCL_OK) {
227 if (code == TCL_ERROR) {
228 Tcl_AddErrorInfo(interp,
229 "\n (\"package ifneeded\" script)");
233 Tcl_ResetResult(interp);
234 pkgPtr = FindPackage(interp, name);
239 * Package not in the database. If there is a "package unknown"
240 * command, invoke it (but only on the first pass; after that,
241 * we should not get here in the first place).
247 script = ((Interp *) interp)->packageUnknown;
248 if (script != NULL) {
249 Tcl_DStringInit(&command);
250 Tcl_DStringAppend(&command, script, -1);
251 Tcl_DStringAppendElement(&command, name);
252 Tcl_DStringAppend(&command, " ", 1);
253 Tcl_DStringAppend(&command, (version != NULL) ? version : "{}",
256 Tcl_DStringAppend(&command, " -exact", 7);
258 code = Tcl_GlobalEval(interp, Tcl_DStringValue(&command));
259 Tcl_DStringFree(&command);
260 if (code != TCL_OK) {
261 if (code == TCL_ERROR) {
262 Tcl_AddErrorInfo(interp,
263 "\n (\"package unknown\" script)");
267 Tcl_ResetResult(interp);
271 if (pkgPtr->version == NULL) {
272 Tcl_AppendResult(interp, "can't find package ", name,
274 if (version != NULL) {
275 Tcl_AppendResult(interp, " ", version, (char *) NULL);
281 * At this point we now that the package is present. Make sure that the
282 * provided version meets the current requirement.
285 if (version == NULL) {
286 return pkgPtr->version;
288 result = ComparePkgVersions(pkgPtr->version, version, &satisfies);
289 if ((satisfies && !exact) || (result == 0)) {
290 return pkgPtr->version;
292 Tcl_AppendResult(interp, "version conflict for package \"",
293 name, "\": have ", pkgPtr->version, ", need ", version,
299 *----------------------------------------------------------------------
303 * This procedure is invoked to process the "package" Tcl command.
304 * See the user documentation for details on what it does.
307 * A standard Tcl result.
310 * See the user documentation.
312 *----------------------------------------------------------------------
318 ClientData dummy, /* Not used. */
319 Tcl_Interp *interp, /* Current interpreter. */
320 int argc, /* Number of arguments. */
321 char **argv /* Argument strings. */
324 Interp *iPtr = (Interp *) interp;
326 int c, exact, i, satisfies;
327 PkgAvail *availPtr, *prevPtr;
330 Tcl_HashSearch search;
331 Tcl_HashTable *tablePtr;
335 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
336 " option ?arg arg ...?\"", (char *) NULL);
340 length = strlen(argv[1]);
341 if ((c == 'f') && (strncmp(argv[1], "forget", length) == 0)) {
342 for (i = 2; i < argc; i++) {
343 hPtr = Tcl_FindHashEntry(&iPtr->packageTable, argv[i]);
347 pkgPtr = (Package *) Tcl_GetHashValue(hPtr);
348 Tcl_DeleteHashEntry(hPtr);
349 if (pkgPtr->version != NULL) {
350 ckfree(pkgPtr->version);
352 while (pkgPtr->availPtr != NULL) {
353 availPtr = pkgPtr->availPtr;
354 pkgPtr->availPtr = availPtr->nextPtr;
355 ckfree(availPtr->version);
356 Tcl_EventuallyFree((ClientData)availPtr->script, TCL_DYNAMIC);
357 ckfree((char *) availPtr);
359 ckfree((char *) pkgPtr);
361 } else if ((c == 'i') && (strncmp(argv[1], "ifneeded", length) == 0)) {
362 if ((argc != 4) && (argc != 5)) {
363 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
364 " ifneeded package version ?script?\"", (char *) NULL);
367 if (CheckVersion(interp, argv[3]) != TCL_OK) {
371 hPtr = Tcl_FindHashEntry(&iPtr->packageTable, argv[2]);
375 pkgPtr = (Package *) Tcl_GetHashValue(hPtr);
377 pkgPtr = FindPackage(interp, argv[2]);
379 for (availPtr = pkgPtr->availPtr, prevPtr = NULL; availPtr != NULL;
380 prevPtr = availPtr, availPtr = availPtr->nextPtr) {
381 if (ComparePkgVersions(availPtr->version, argv[3], (int *) NULL)
384 interp->result = availPtr->script;
387 Tcl_EventuallyFree((ClientData)availPtr->script, TCL_DYNAMIC);
394 if (availPtr == NULL) {
395 availPtr = (PkgAvail *) ckalloc(sizeof(PkgAvail));
396 availPtr->version = ckalloc((unsigned) (strlen(argv[3]) + 1));
397 strcpy(availPtr->version, argv[3]);
398 if (prevPtr == NULL) {
399 availPtr->nextPtr = pkgPtr->availPtr;
400 pkgPtr->availPtr = availPtr;
402 availPtr->nextPtr = prevPtr->nextPtr;
403 prevPtr->nextPtr = availPtr;
406 availPtr->script = ckalloc((unsigned) (strlen(argv[4]) + 1));
407 strcpy(availPtr->script, argv[4]);
408 } else if ((c == 'n') && (strncmp(argv[1], "names", length) == 0)) {
410 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
411 " names\"", (char *) NULL);
414 tablePtr = &iPtr->packageTable;
415 for (hPtr = Tcl_FirstHashEntry(tablePtr, &search); hPtr != NULL;
416 hPtr = Tcl_NextHashEntry(&search)) {
417 pkgPtr = (Package *) Tcl_GetHashValue(hPtr);
418 if ((pkgPtr->version != NULL) || (pkgPtr->availPtr != NULL)) {
419 Tcl_AppendElement(interp, Tcl_GetHashKey(tablePtr, hPtr));
422 } else if ((c == 'p') && (strncmp(argv[1], "provide", length) == 0)) {
423 if ((argc != 3) && (argc != 4)) {
424 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
425 " provide package ?version?\"", (char *) NULL);
429 hPtr = Tcl_FindHashEntry(&iPtr->packageTable, argv[2]);
431 pkgPtr = (Package *) Tcl_GetHashValue(hPtr);
432 if (pkgPtr->version != NULL) {
433 interp->result = pkgPtr->version;
438 if (CheckVersion(interp, argv[3]) != TCL_OK) {
441 return Tcl_PkgProvide(interp, argv[2], argv[3]);
442 } else if ((c == 'r') && (strncmp(argv[1], "require", length) == 0)) {
445 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
446 " require ?-exact? package ?version?\"", (char *) NULL);
449 if ((argv[2][0] == '-') && (strcmp(argv[2], "-exact") == 0)) {
455 if (argc == (4+exact)) {
456 version = argv[3+exact];
457 if (CheckVersion(interp, version) != TCL_OK) {
460 } else if ((argc != 3) || exact) {
463 version = Tcl_PkgRequire(interp, argv[2+exact], version, exact);
464 if (version == NULL) {
467 interp->result = version;
468 } else if ((c == 'u') && (strncmp(argv[1], "unknown", length) == 0)) {
470 if (iPtr->packageUnknown != NULL) {
471 iPtr->result = iPtr->packageUnknown;
473 } else if (argc == 3) {
474 if (iPtr->packageUnknown != NULL) {
475 ckfree(iPtr->packageUnknown);
477 if (argv[2][0] == 0) {
478 iPtr->packageUnknown = NULL;
480 iPtr->packageUnknown = (char *) ckalloc((unsigned)
481 (strlen(argv[2]) + 1));
482 strcpy(iPtr->packageUnknown, argv[2]);
485 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
486 " unknown ?command?\"", (char *) NULL);
489 } else if ((c == 'v') && (strncmp(argv[1], "vcompare", length) == 0)
492 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
493 " vcompare version1 version2\"", (char *) NULL);
496 if ((CheckVersion(interp, argv[2]) != TCL_OK)
497 || (CheckVersion(interp, argv[3]) != TCL_OK)) {
500 sprintf(interp->result, "%d", ComparePkgVersions(argv[2], argv[3],
502 } else if ((c == 'v') && (strncmp(argv[1], "versions", length) == 0)
505 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
506 " versions package\"", (char *) NULL);
509 hPtr = Tcl_FindHashEntry(&iPtr->packageTable, argv[2]);
511 pkgPtr = (Package *) Tcl_GetHashValue(hPtr);
512 for (availPtr = pkgPtr->availPtr; availPtr != NULL;
513 availPtr = availPtr->nextPtr) {
514 Tcl_AppendElement(interp, availPtr->version);
517 } else if ((c == 'v') && (strncmp(argv[1], "vsatisfies", length) == 0)
520 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
521 " vsatisfies version1 version2\"", (char *) NULL);
524 if ((CheckVersion(interp, argv[2]) != TCL_OK)
525 || (CheckVersion(interp, argv[3]) != TCL_OK)) {
528 ComparePkgVersions(argv[2], argv[3], &satisfies);
529 sprintf(interp->result, "%d", satisfies);
531 Tcl_AppendResult(interp, "bad option \"", argv[1],
532 "\": should be forget, ifneeded, names, ",
533 "provide, require, unknown, vcompare, ",
534 "versions, or vsatisfies", (char *) NULL);
541 *----------------------------------------------------------------------
545 * This procedure finds the Package record for a particular package
546 * in a particular interpreter, creating a record if one doesn't
550 * The return value is a pointer to the Package record for the
554 * A new Package record may be created.
556 *----------------------------------------------------------------------
561 Tcl_Interp *interp, /* Interpreter to use for package lookup. */
562 char *name /* Name of package to fine. */
565 Interp *iPtr = (Interp *) interp;
570 hPtr = Tcl_CreateHashEntry(&iPtr->packageTable, name, &new);
572 pkgPtr = (Package *) ckalloc(sizeof(Package));
573 pkgPtr->version = NULL;
574 pkgPtr->availPtr = NULL;
575 Tcl_SetHashValue(hPtr, pkgPtr);
577 pkgPtr = (Package *) Tcl_GetHashValue(hPtr);
583 *----------------------------------------------------------------------
585 * TclFreePackageInfo --
587 * This procedure is called during interpreter deletion to
588 * free all of the package-related information for the
597 *----------------------------------------------------------------------
602 Interp *iPtr /* Interpereter that is being deleted. */
606 Tcl_HashSearch search;
610 for (hPtr = Tcl_FirstHashEntry(&iPtr->packageTable, &search);
611 hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
612 pkgPtr = (Package *) Tcl_GetHashValue(hPtr);
613 if (pkgPtr->version != NULL) {
614 ckfree(pkgPtr->version);
616 while (pkgPtr->availPtr != NULL) {
617 availPtr = pkgPtr->availPtr;
618 pkgPtr->availPtr = availPtr->nextPtr;
619 ckfree(availPtr->version);
620 Tcl_EventuallyFree((ClientData)availPtr->script, TCL_DYNAMIC);
621 ckfree((char *) availPtr);
623 ckfree((char *) pkgPtr);
625 Tcl_DeleteHashTable(&iPtr->packageTable);
626 if (iPtr->packageUnknown != NULL) {
627 ckfree(iPtr->packageUnknown);
632 *----------------------------------------------------------------------
636 * This procedure checks to see whether a version number has
640 * If string is a properly formed version number the TCL_OK
641 * is returned. Otherwise TCL_ERROR is returned and an error
642 * message is left in interp->result.
647 *----------------------------------------------------------------------
652 Tcl_Interp *interp, /* Used for error reporting. */
653 char *string /* Supposedly a version number, which is
654 * groups of decimal digits separated
663 for (p++; *p != 0; p++) {
664 if (!isdigit(*p) && (*p != '.')) {
673 Tcl_AppendResult(interp, "expected version number but got \"",
674 string, "\"", (char *) NULL);
679 *----------------------------------------------------------------------
681 * ComparePkgVersions --
683 * This procedure compares two version numbers.
686 * The return value is -1 if v1 is less than v2, 0 if the two
687 * version numbers are the same, and 1 if v1 is greater than v2.
688 * If *satPtr is non-NULL, the word it points to is filled in
689 * with 1 if v2 >= v1 and both numbers have the same major number
695 *----------------------------------------------------------------------
700 char *v1, char *v2, /* Versions strings, of form 2.1.3 (any
701 * number of version numbers). */
702 int *satPtr /* If non-null, the word pointed to is
703 * filled in with a 0/1 value. 1 means
704 * v1 "satisfies" v2: v1 is greater than
705 * or equal to v2 and both version numbers
706 * have the same major number. */
709 int thisIsMajor, n1, n2;
712 * Each iteration of the following loop processes one number from
713 * each string, terminated by a ".". If those numbers don't match
714 * then the comparison is over; otherwise, we loop back for the
721 * Parse one decimal number from the front of each string.
725 while ((*v1 != 0) && (*v1 != '.')) {
726 n1 = 10*n1 + (*v1 - '0');
729 while ((*v2 != 0) && (*v2 != '.')) {
730 n2 = 10*n2 + (*v2 - '0');
735 * Compare and go on to the next version number if the
736 * current numbers match.
744 } else if (*v2 == 0) {
752 if (satPtr != NULL) {
753 *satPtr = (n1 == n2) || ((n1 > n2) && !thisIsMajor);
757 } else if (n1 == n2) {