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 librararies 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 *----------------------------------------------------------------------
106 Tcl_PkgProvide(interp, name, version)
107 Tcl_Interp *interp; /* Interpreter in which package is now
109 char *name; /* Name of package. */
110 char *version; /* Version string for package. */
114 pkgPtr = FindPackage(interp, name);
115 if (pkgPtr->version == NULL) {
116 pkgPtr->version = ckalloc((unsigned) (strlen(version) + 1));
117 strcpy(pkgPtr->version, version);
120 if (ComparePkgVersions(pkgPtr->version, version, (int *) NULL) == 0) {
123 Tcl_AppendResult(interp, "conflicting versions provided for package \"",
124 name, "\": ", pkgPtr->version, ", then ", version, (char *) NULL);
129 *----------------------------------------------------------------------
133 * This procedure is called by code that depends on a particular
134 * version of a particular package. If the package is not already
135 * provided in the interpreter, this procedure invokes a Tcl script
136 * to provide it. If the package is already provided, this
137 * procedure makes sure that the caller's needs don't conflict with
138 * the version that is present.
141 * If successful, returns the version string for the currently
142 * provided version of the package, which may be different from
143 * the "version" argument. If the caller's requirements
144 * cannot be met (e.g. the version requested conflicts with
145 * a currently provided version, or the required version cannot
146 * be found, or the script to provide the required version
147 * generates an error), NULL is returned and an error
148 * message is left in interp->result.
151 * The script from some previous "package ifneeded" command may
152 * be invoked to provide the package.
154 *----------------------------------------------------------------------
158 Tcl_PkgRequire(interp, name, version, exact)
159 Tcl_Interp *interp; /* Interpreter in which package is now
161 char *name; /* Name of desired package. */
162 char *version; /* Version string for desired version;
163 * NULL means use the latest version
165 int exact; /* Non-zero means that only the particular
166 * version given is acceptable. Zero means
167 * use the latest compatible version. */
170 PkgAvail *availPtr, *bestPtr;
172 int code, satisfies, result, pass;
176 * It can take up to three passes to find the package: one pass to
177 * run the "package unknown" script, one to run the "package ifneeded"
178 * script for a specific version, and a final pass to lookup the
179 * package loaded by the "package ifneeded" script.
182 for (pass = 1; ; pass++) {
183 pkgPtr = FindPackage(interp, name);
184 if (pkgPtr->version != NULL) {
189 * The package isn't yet present. Search the list of available
190 * versions and invoke the script for the best available version.
194 for (availPtr = pkgPtr->availPtr; availPtr != NULL;
195 availPtr = availPtr->nextPtr) {
196 if ((bestPtr != NULL) && (ComparePkgVersions(availPtr->version,
197 bestPtr->version, (int *) NULL) <= 0)) {
200 if (version != NULL) {
201 result = ComparePkgVersions(availPtr->version, version,
203 if ((result != 0) && exact) {
212 if (bestPtr != NULL) {
214 * We found an ifneeded script for the package. Be careful while
215 * executing it: this could cause reentrancy, so (a) protect the
216 * script itself from deletion and (b) don't assume that bestPtr
217 * will still exist when the script completes.
220 script = bestPtr->script;
221 Tcl_Preserve((ClientData) script);
222 code = Tcl_GlobalEval(interp, script);
223 Tcl_Release((ClientData) script);
224 if (code != TCL_OK) {
225 if (code == TCL_ERROR) {
226 Tcl_AddErrorInfo(interp,
227 "\n (\"package ifneeded\" script)");
231 Tcl_ResetResult(interp);
232 pkgPtr = FindPackage(interp, name);
237 * Package not in the database. If there is a "package unknown"
238 * command, invoke it (but only on the first pass; after that,
239 * we should not get here in the first place).
245 script = ((Interp *) interp)->packageUnknown;
246 if (script != NULL) {
247 Tcl_DStringInit(&command);
248 Tcl_DStringAppend(&command, script, -1);
249 Tcl_DStringAppendElement(&command, name);
250 Tcl_DStringAppend(&command, " ", 1);
251 Tcl_DStringAppend(&command, (version != NULL) ? version : "{}",
254 Tcl_DStringAppend(&command, " -exact", 7);
256 code = Tcl_GlobalEval(interp, Tcl_DStringValue(&command));
257 Tcl_DStringFree(&command);
258 if (code != TCL_OK) {
259 if (code == TCL_ERROR) {
260 Tcl_AddErrorInfo(interp,
261 "\n (\"package unknown\" script)");
265 Tcl_ResetResult(interp);
269 if (pkgPtr->version == NULL) {
270 Tcl_AppendResult(interp, "can't find package ", name,
272 if (version != NULL) {
273 Tcl_AppendResult(interp, " ", version, (char *) NULL);
279 * At this point we now that the package is present. Make sure that the
280 * provided version meets the current requirement.
283 if (version == NULL) {
284 return pkgPtr->version;
286 result = ComparePkgVersions(pkgPtr->version, version, &satisfies);
287 if ((satisfies && !exact) || (result == 0)) {
288 return pkgPtr->version;
290 Tcl_AppendResult(interp, "version conflict for package \"",
291 name, "\": have ", pkgPtr->version, ", need ", version,
297 *----------------------------------------------------------------------
301 * This procedure is invoked to process the "package" Tcl command.
302 * See the user documentation for details on what it does.
305 * A standard Tcl result.
308 * See the user documentation.
310 *----------------------------------------------------------------------
315 Tcl_PackageCmd(dummy, interp, argc, argv)
316 ClientData dummy; /* Not used. */
317 Tcl_Interp *interp; /* Current interpreter. */
318 int argc; /* Number of arguments. */
319 char **argv; /* Argument strings. */
321 Interp *iPtr = (Interp *) interp;
323 int c, exact, i, satisfies;
324 PkgAvail *availPtr, *prevPtr;
327 Tcl_HashSearch search;
328 Tcl_HashTable *tablePtr;
332 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
333 " option ?arg arg ...?\"", (char *) NULL);
337 length = strlen(argv[1]);
338 if ((c == 'f') && (strncmp(argv[1], "forget", length) == 0)) {
339 for (i = 2; i < argc; i++) {
340 hPtr = Tcl_FindHashEntry(&iPtr->packageTable, argv[i]);
344 pkgPtr = (Package *) Tcl_GetHashValue(hPtr);
345 Tcl_DeleteHashEntry(hPtr);
346 if (pkgPtr->version != NULL) {
347 ckfree(pkgPtr->version);
349 while (pkgPtr->availPtr != NULL) {
350 availPtr = pkgPtr->availPtr;
351 pkgPtr->availPtr = availPtr->nextPtr;
352 ckfree(availPtr->version);
353 Tcl_EventuallyFree((ClientData)availPtr->script, TCL_DYNAMIC);
354 ckfree((char *) availPtr);
356 ckfree((char *) pkgPtr);
358 } else if ((c == 'i') && (strncmp(argv[1], "ifneeded", length) == 0)) {
359 if ((argc != 4) && (argc != 5)) {
360 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
361 " ifneeded package version ?script?\"", (char *) NULL);
364 if (CheckVersion(interp, argv[3]) != TCL_OK) {
368 hPtr = Tcl_FindHashEntry(&iPtr->packageTable, argv[2]);
372 pkgPtr = (Package *) Tcl_GetHashValue(hPtr);
374 pkgPtr = FindPackage(interp, argv[2]);
376 for (availPtr = pkgPtr->availPtr, prevPtr = NULL; availPtr != NULL;
377 prevPtr = availPtr, availPtr = availPtr->nextPtr) {
378 if (ComparePkgVersions(availPtr->version, argv[3], (int *) NULL)
381 interp->result = availPtr->script;
384 Tcl_EventuallyFree((ClientData)availPtr->script, TCL_DYNAMIC);
391 if (availPtr == NULL) {
392 availPtr = (PkgAvail *) ckalloc(sizeof(PkgAvail));
393 availPtr->version = ckalloc((unsigned) (strlen(argv[3]) + 1));
394 strcpy(availPtr->version, argv[3]);
395 if (prevPtr == NULL) {
396 availPtr->nextPtr = pkgPtr->availPtr;
397 pkgPtr->availPtr = availPtr;
399 availPtr->nextPtr = prevPtr->nextPtr;
400 prevPtr->nextPtr = availPtr;
403 availPtr->script = ckalloc((unsigned) (strlen(argv[4]) + 1));
404 strcpy(availPtr->script, argv[4]);
405 } else if ((c == 'n') && (strncmp(argv[1], "names", length) == 0)) {
407 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
408 " names\"", (char *) NULL);
411 tablePtr = &iPtr->packageTable;
412 for (hPtr = Tcl_FirstHashEntry(tablePtr, &search); hPtr != NULL;
413 hPtr = Tcl_NextHashEntry(&search)) {
414 pkgPtr = (Package *) Tcl_GetHashValue(hPtr);
415 if ((pkgPtr->version != NULL) || (pkgPtr->availPtr != NULL)) {
416 Tcl_AppendElement(interp, Tcl_GetHashKey(tablePtr, hPtr));
419 } else if ((c == 'p') && (strncmp(argv[1], "provide", length) == 0)) {
420 if ((argc != 3) && (argc != 4)) {
421 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
422 " provide package ?version?\"", (char *) NULL);
426 hPtr = Tcl_FindHashEntry(&iPtr->packageTable, argv[2]);
428 pkgPtr = (Package *) Tcl_GetHashValue(hPtr);
429 if (pkgPtr->version != NULL) {
430 interp->result = pkgPtr->version;
435 if (CheckVersion(interp, argv[3]) != TCL_OK) {
438 return Tcl_PkgProvide(interp, argv[2], argv[3]);
439 } else if ((c == 'r') && (strncmp(argv[1], "require", length) == 0)) {
442 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
443 " require ?-exact? package ?version?\"", (char *) NULL);
446 if ((argv[2][0] == '-') && (strcmp(argv[2], "-exact") == 0)) {
452 if (argc == (4+exact)) {
453 version = argv[3+exact];
454 if (CheckVersion(interp, version) != TCL_OK) {
457 } else if ((argc != 3) || exact) {
460 version = Tcl_PkgRequire(interp, argv[2+exact], version, exact);
461 if (version == NULL) {
464 interp->result = version;
465 } else if ((c == 'u') && (strncmp(argv[1], "unknown", length) == 0)) {
467 if (iPtr->packageUnknown != NULL) {
468 iPtr->result = iPtr->packageUnknown;
470 } else if (argc == 3) {
471 if (iPtr->packageUnknown != NULL) {
472 ckfree(iPtr->packageUnknown);
474 if (argv[2][0] == 0) {
475 iPtr->packageUnknown = NULL;
477 iPtr->packageUnknown = (char *) ckalloc((unsigned)
478 (strlen(argv[2]) + 1));
479 strcpy(iPtr->packageUnknown, argv[2]);
482 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
483 " unknown ?command?\"", (char *) NULL);
486 } else if ((c == 'v') && (strncmp(argv[1], "vcompare", length) == 0)
489 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
490 " vcompare version1 version2\"", (char *) NULL);
493 if ((CheckVersion(interp, argv[2]) != TCL_OK)
494 || (CheckVersion(interp, argv[3]) != TCL_OK)) {
497 sprintf(interp->result, "%d", ComparePkgVersions(argv[2], argv[3],
499 } else if ((c == 'v') && (strncmp(argv[1], "versions", length) == 0)
502 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
503 " versions package\"", (char *) NULL);
506 hPtr = Tcl_FindHashEntry(&iPtr->packageTable, argv[2]);
508 pkgPtr = (Package *) Tcl_GetHashValue(hPtr);
509 for (availPtr = pkgPtr->availPtr; availPtr != NULL;
510 availPtr = availPtr->nextPtr) {
511 Tcl_AppendElement(interp, availPtr->version);
514 } else if ((c == 'v') && (strncmp(argv[1], "vsatisfies", length) == 0)
517 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
518 " vsatisfies version1 version2\"", (char *) NULL);
521 if ((CheckVersion(interp, argv[2]) != TCL_OK)
522 || (CheckVersion(interp, argv[3]) != TCL_OK)) {
525 ComparePkgVersions(argv[2], argv[3], &satisfies);
526 sprintf(interp->result, "%d", satisfies);
528 Tcl_AppendResult(interp, "bad option \"", argv[1],
529 "\": should be forget, ifneeded, names, ",
530 "provide, require, unknown, vcompare, ",
531 "versions, or vsatisfies", (char *) NULL);
538 *----------------------------------------------------------------------
542 * This procedure finds the Package record for a particular package
543 * in a particular interpreter, creating a record if one doesn't
547 * The return value is a pointer to the Package record for the
551 * A new Package record may be created.
553 *----------------------------------------------------------------------
557 FindPackage(interp, name)
558 Tcl_Interp *interp; /* Interpreter to use for package lookup. */
559 char *name; /* Name of package to fine. */
561 Interp *iPtr = (Interp *) interp;
566 hPtr = Tcl_CreateHashEntry(&iPtr->packageTable, name, &new);
568 pkgPtr = (Package *) ckalloc(sizeof(Package));
569 pkgPtr->version = NULL;
570 pkgPtr->availPtr = NULL;
571 Tcl_SetHashValue(hPtr, pkgPtr);
573 pkgPtr = (Package *) Tcl_GetHashValue(hPtr);
579 *----------------------------------------------------------------------
581 * TclFreePackageInfo --
583 * This procedure is called during interpreter deletion to
584 * free all of the package-related information for the
593 *----------------------------------------------------------------------
597 TclFreePackageInfo(iPtr)
598 Interp *iPtr; /* Interpereter that is being deleted. */
601 Tcl_HashSearch search;
605 for (hPtr = Tcl_FirstHashEntry(&iPtr->packageTable, &search);
606 hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
607 pkgPtr = (Package *) Tcl_GetHashValue(hPtr);
608 if (pkgPtr->version != NULL) {
609 ckfree(pkgPtr->version);
611 while (pkgPtr->availPtr != NULL) {
612 availPtr = pkgPtr->availPtr;
613 pkgPtr->availPtr = availPtr->nextPtr;
614 ckfree(availPtr->version);
615 Tcl_EventuallyFree((ClientData)availPtr->script, TCL_DYNAMIC);
616 ckfree((char *) availPtr);
618 ckfree((char *) pkgPtr);
620 Tcl_DeleteHashTable(&iPtr->packageTable);
621 if (iPtr->packageUnknown != NULL) {
622 ckfree(iPtr->packageUnknown);
627 *----------------------------------------------------------------------
631 * This procedure checks to see whether a version number has
635 * If string is a properly formed version number the TCL_OK
636 * is returned. Otherwise TCL_ERROR is returned and an error
637 * message is left in interp->result.
642 *----------------------------------------------------------------------
646 CheckVersion(interp, string)
647 Tcl_Interp *interp; /* Used for error reporting. */
648 char *string; /* Supposedly a version number, which is
649 * groups of decimal digits separated
657 for (p++; *p != 0; p++) {
658 if (!isdigit(*p) && (*p != '.')) {
667 Tcl_AppendResult(interp, "expected version number but got \"",
668 string, "\"", (char *) NULL);
673 *----------------------------------------------------------------------
675 * ComparePkgVersions --
677 * This procedure compares two version numbers.
680 * The return value is -1 if v1 is less than v2, 0 if the two
681 * version numbers are the same, and 1 if v1 is greater than v2.
682 * If *satPtr is non-NULL, the word it points to is filled in
683 * with 1 if v2 >= v1 and both numbers have the same major number
689 *----------------------------------------------------------------------
693 ComparePkgVersions(v1, v2, satPtr)
694 char *v1, *v2; /* Versions strings, of form 2.1.3 (any
695 * number of version numbers). */
696 int *satPtr; /* If non-null, the word pointed to is
697 * filled in with a 0/1 value. 1 means
698 * v1 "satisfies" v2: v1 is greater than
699 * or equal to v2 and both version numbers
700 * have the same major number. */
702 int thisIsMajor, n1, n2;
705 * Each iteration of the following loop processes one number from
706 * each string, terminated by a ".". If those numbers don't match
707 * then the comparison is over; otherwise, we loop back for the
714 * Parse one decimal number from the front of each string.
718 while ((*v1 != 0) && (*v1 != '.')) {
719 n1 = 10*n1 + (*v1 - '0');
722 while ((*v2 != 0) && (*v2 != '.')) {
723 n2 = 10*n2 + (*v2 - '0');
728 * Compare and go on to the next version number if the
729 * current numbers match.
737 } else if (*v2 == 0) {
745 if (satPtr != NULL) {
746 *satPtr = (n1 == n2) || ((n1 > n2) && !thisIsMajor);
750 } else if (n1 == n2) {