Debian Codegraph @

generic/tclPkg.c

raw
1 /*
2 * tclPkg.c --
3 *
4 * This file implements package and version control for Tcl via the
5 * "package" command and a few C APIs.
6 *
7 * Copyright (c) 1996 Sun Microsystems, Inc.
8 * Copyright (c) 2006 Andreas Kupries <[email protected]>
9 *
10 * See the file "license.terms" for information on usage and redistribution of
11 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 *
13 * TIP #268.
14 * Heavily rewritten to handle the extend version numbers, and extended
15 * package requirements.
16 */
17
18 #include "tclInt.h"
19
20 /*
21 * Each invocation of the "package ifneeded" command creates a structure of
22 * the following type, which is used to load the package into the interpreter
23 * if it is requested with a "package require" command.
24 */
25
26 typedef struct PkgAvail {
27 char *version; /* Version string; malloc'ed. */
28 char *script; /* Script to invoke to provide this version of
29 * the package. Malloc'ed and protected by
30 * Tcl_Preserve and Tcl_Release. */
31 struct PkgAvail *nextPtr; /* Next in list of available versions of the
32 * same package. */
33 } PkgAvail;
34
35 /*
36 * For each package that is known in any way to an interpreter, there is one
37 * record of the following type. These records are stored in the
38 * "packageTable" hash table in the interpreter, keyed by package name such as
39 * "Tk" (no version number).
40 */
41
42 typedef struct Package {
43 Tcl_Obj *version;
44 PkgAvail *availPtr; /* First in list of all available versions of
45 * this package. */
46 const void *clientData; /* Client data. */
47 } Package;
48
49 typedef struct Require {
50 void * clientDataPtr;
51 const char *name;
52 Package *pkgPtr;
53 char *versionToProvide;
54 } Require;
55
56 typedef struct RequireProcArgs {
57 const char *name;
58 void *clientDataPtr;
59 } RequireProcArgs;
60
61 /*
62 * Prototypes for functions defined in this file:
63 */
64
65 static int CheckVersionAndConvert(Tcl_Interp *interp,
66 const char *string, char **internal, int *stable);
67 static int CompareVersions(char *v1i, char *v2i,
68 int *isMajorPtr);
69 static int CheckRequirement(Tcl_Interp *interp,
70 const char *string);
71 static int CheckAllRequirements(Tcl_Interp *interp, int reqc,
72 Tcl_Obj *const reqv[]);
73 static int RequirementSatisfied(char *havei, const char *req);
74 static int SomeRequirementSatisfied(char *havei, int reqc,
75 Tcl_Obj *const reqv[]);
76 static void AddRequirementsToResult(Tcl_Interp *interp, int reqc,
77 Tcl_Obj *const reqv[]);
78 static void AddRequirementsToDString(Tcl_DString *dstring,
79 int reqc, Tcl_Obj *const reqv[]);
80 static Package * FindPackage(Tcl_Interp *interp, const char *name);
81 static int PkgRequireCore(ClientData data[], Tcl_Interp *interp, int result);
82 static int PkgRequireCoreFinal(ClientData data[], Tcl_Interp *interp, int result);
83 static int PkgRequireCoreCleanup(ClientData data[], Tcl_Interp *interp, int result);
84 static int PkgRequireCoreStep1(ClientData data[], Tcl_Interp *interp, int result);
85 static int PkgRequireCoreStep2(ClientData data[], Tcl_Interp *interp, int result);
86 static int TclNRPkgRequireProc(ClientData clientData, Tcl_Interp *interp, int reqc, Tcl_Obj *const reqv[]);
87 static int SelectPackage(ClientData data[], Tcl_Interp *interp, int result);
88 static int SelectPackageFinal(ClientData data[], Tcl_Interp *interp, int result);
89 static int TclNRPackageObjCmdCleanup(ClientData data[], Tcl_Interp *interp, int result);
90
91 /*
92 * Helper macros.
93 */
94
95 #define DupBlock(v,s,len) \
96 ((v) = ckalloc(len), memcpy((v),(s),(len)))
97 #define DupString(v,s) \
98 do { \
99 unsigned local__len = (unsigned) (strlen(s) + 1); \
100 DupBlock((v),(s),local__len); \
101 } while (0)
102
103 /*
104 *----------------------------------------------------------------------
105 *
106 * Tcl_PkgProvide / Tcl_PkgProvideEx --
107 *
108 * This function is invoked to declare that a particular version of a
109 * particular package is now present in an interpreter. There must not be
110 * any other version of this package already provided in the interpreter.
111 *
112 * Results:
113 * Normally returns TCL_OK; if there is already another version of the
114 * package loaded then TCL_ERROR is returned and an error message is left
115 * in the interp's result.
116 *
117 * Side effects:
118 * The interpreter remembers that this package is available, so that no
119 * other version of the package may be provided for the interpreter.
120 *
121 *----------------------------------------------------------------------
122 */
123
124 #undef Tcl_PkgProvide
125 int
126 Tcl_PkgProvide(
127 Tcl_Interp *interp, /* Interpreter in which package is now
128 * available. */
129 const char *name, /* Name of package. */
130 const char *version) /* Version string for package. */
131 {
132 return Tcl_PkgProvideEx(interp, name, version, NULL);
133 }
134
135 int
136 Tcl_PkgProvideEx(
137 Tcl_Interp *interp, /* Interpreter in which package is now
138 * available. */
139 const char *name, /* Name of package. */
140 const char *version, /* Version string for package. */
141 const void *clientData) /* clientdata for this package (normally used
142 * for C callback function table) */
143 {
144 Package *pkgPtr;
145 char *pvi, *vi;
146 int res;
147
148 pkgPtr = FindPackage(interp, name);
149 if (pkgPtr->version == NULL) {
150 pkgPtr->version = Tcl_NewStringObj(version, -1);
151 Tcl_IncrRefCount(pkgPtr->version);
152 pkgPtr->clientData = clientData;
153 return TCL_OK;
154 }
155
156 if (CheckVersionAndConvert(interp, TclGetString(pkgPtr->version), &pvi,
157 NULL) != TCL_OK) {
158 return TCL_ERROR;
159 } else if (CheckVersionAndConvert(interp, version, &vi, NULL) != TCL_OK) {
160 ckfree(pvi);
161 return TCL_ERROR;
162 }
163
164 res = CompareVersions(pvi, vi, NULL);
165 ckfree(pvi);
166 ckfree(vi);
167
168 if (res == 0) {
169 if (clientData != NULL) {
170 pkgPtr->clientData = clientData;
171 }
172 return TCL_OK;
173 }
174 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
175 "conflicting versions provided for package \"%s\": %s, then %s",
176 name, TclGetString(pkgPtr->version), version));
177 Tcl_SetErrorCode(interp, "TCL", "PACKAGE", "VERSIONCONFLICT", (char *)NULL);
178 return TCL_ERROR;
179 }
180
181 /*
182 *----------------------------------------------------------------------
183 *
184 * Tcl_PkgRequire / Tcl_PkgRequireEx / Tcl_PkgRequireProc --
185 *
186 * This function is called by code that depends on a particular version
187 * of a particular package. If the package is not already provided in the
188 * interpreter, this function invokes a Tcl script to provide it. If the
189 * package is already provided, this function makes sure that the
190 * caller's needs don't conflict with the version that is present.
191 *
192 * Results:
193 * If successful, returns the version string for the currently provided
194 * version of the package, which may be different from the "version"
195 * argument. If the caller's requirements cannot be met (e.g. the version
196 * requested conflicts with a currently provided version, or the required
197 * version cannot be found, or the script to provide the required version
198 * generates an error), NULL is returned and an error message is left in
199 * the interp's result.
200 *
201 * Side effects:
202 * The script from some previous "package ifneeded" command may be
203 * invoked to provide the package.
204 *
205 *----------------------------------------------------------------------
206 */
207
208 #undef Tcl_PkgRequire
209 const char *
210 Tcl_PkgRequire(
211 Tcl_Interp *interp, /* Interpreter in which package is now
212 * available. */
213 const char *name, /* Name of desired package. */
214 const char *version, /* Version string for desired version; NULL
215 * means use the latest version available. */
216 int exact) /* Non-zero means that only the particular
217 * version given is acceptable. Zero means use
218 * the latest compatible version. */
219 {
220 return Tcl_PkgRequireEx(interp, name, version, exact, NULL);
221 }
222
223 const char *
224 Tcl_PkgRequireEx(
225 Tcl_Interp *interp, /* Interpreter in which package is now
226 * available. */
227 const char *name, /* Name of desired package. */
228 const char *version, /* Version string for desired version; NULL
229 * means use the latest version available. */
230 int exact, /* Non-zero means that only the particular
231 * version given is acceptable. Zero means use
232 * the latest compatible version. */
233 void *clientDataPtr) /* Used to return the client data for this
234 * package. If it is NULL then the client data
235 * is not returned. This is unchanged if this
236 * call fails for any reason. */
237 {
238 Tcl_Obj *ov;
239 const char *result = NULL;
240
241 /*
242 * If an attempt is being made to load this into a standalone executable
243 * on a platform where backlinking is not supported then this must be a
244 * shared version of Tcl (Otherwise the load would have failed). Detect
245 * this situation by checking that this library has been correctly
246 * initialised. If it has not been then return immediately as nothing will
247 * work.
248 */
249
250 if (tclEmptyStringRep == NULL) {
251 /*
252 * OK, so what's going on here?
253 *
254 * First, what are we doing? We are performing a check on behalf of
255 * one particular caller, Tcl_InitStubs(). When a package is stub-
256 * enabled, it is statically linked to libtclstub.a, which contains a
257 * copy of Tcl_InitStubs(). When a stub-enabled package is loaded, its
258 * *_Init() function is supposed to call Tcl_InitStubs() before
259 * calling any other functions in the Tcl library. The first Tcl
260 * function called by Tcl_InitStubs() through the stub table is
261 * Tcl_PkgRequireEx(), so this code right here is the first code that
262 * is part of the original Tcl library in the executable that gets
263 * executed on behalf of a newly loaded stub-enabled package.
264 *
265 * One easy error for the developer/builder of a stub-enabled package
266 * to make is to forget to define USE_TCL_STUBS when compiling the
267 * package. When that happens, the package will contain symbols that
268 * are references to the Tcl library, rather than function pointers
269 * referencing the stub table. On platforms that lack backlinking,
270 * those unresolved references may cause the loading of the package to
271 * also load a second copy of the Tcl library, leading to all kinds of
272 * trouble. We would like to catch that error and report a useful
273 * message back to the user. That's what we're doing.
274 *
275 * Second, how does this work? If we reach this point, then the global
276 * variable tclEmptyStringRep has the value NULL. Compare that with
277 * the definition of tclEmptyStringRep near the top of the file
278 * generic/tclObj.c. It clearly should not have the value NULL; it
279 * should point to the char tclEmptyString. If we see it having the
280 * value NULL, then somehow we are seeing a Tcl library that isn't
281 * completely initialized, and that's an indicator for the error
282 * condition described above. (Further explanation is welcome.)
283 *
284 * Third, so what do we do about it? This situation indicates the
285 * package we just loaded wasn't properly compiled to be stub-enabled,
286 * yet it thinks it is stub-enabled (it called Tcl_InitStubs()). We
287 * want to report that the package just loaded is broken, so we want
288 * to place an error message in the interpreter result and return NULL
289 * to indicate failure to Tcl_InitStubs() so that it will also fail.
290 * (Further explanation why we don't want to Tcl_Panic() is welcome.
291 * After all, two Tcl libraries can't be a good thing!)
292 *
293 * Trouble is that's going to be tricky. We're now using a Tcl library
294 * that's not fully initialized. In particular, it doesn't have a
295 * proper value for tclEmptyStringRep. The Tcl_Obj system heavily
296 * depends on the value of tclEmptyStringRep and all of Tcl depends
297 * (increasingly) on the Tcl_Obj system, we need to correct that flaw
298 * before making the calls to set the interpreter result to the error
299 * message. That's the only flaw corrected; other problems with
300 * initialization of the Tcl library are not remedied, so be very
301 * careful about adding any other calls here without checking how they
302 * behave when initialization is incomplete.
303 */
304
305 tclEmptyStringRep = &tclEmptyString;
306 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
307 "Cannot load package \"%s\" in standalone executable:"
308 " This package is not compiled with stub support", name));
309 Tcl_SetErrorCode(interp, "TCL", "PACKAGE", "UNSTUBBED", (char *)NULL);
310 return NULL;
311 }
312
313 /*
314 * Translate between old and new API, and defer to the new function.
315 */
316
317 if (version == NULL) {
318 if (Tcl_PkgRequireProc(interp, name, 0, NULL, clientDataPtr) == TCL_OK) {
319 result = Tcl_GetString(Tcl_GetObjResult(interp));
320 Tcl_ResetResult(interp);
321 }
322 } else {
323 if (exact && TCL_OK
324 != CheckVersionAndConvert(interp, version, NULL, NULL)) {
325 return NULL;
326 }
327 ov = Tcl_NewStringObj(version, -1);
328 if (exact) {
329 Tcl_AppendStringsToObj(ov, "-", version, (char *)NULL);
330 }
331 Tcl_IncrRefCount(ov);
332 if (Tcl_PkgRequireProc(interp, name, 1, &ov, clientDataPtr) == TCL_OK) {
333 result = Tcl_GetString(Tcl_GetObjResult(interp));
334 Tcl_ResetResult(interp);
335 }
336 TclDecrRefCount(ov);
337 }
338 return result;
339 }
340
341 int
342 Tcl_PkgRequireProc(
343 Tcl_Interp *interp, /* Interpreter in which package is now
344 * available. */
345 const char *name, /* Name of desired package. */
346 int reqc, /* Requirements constraining the desired
347 * version. */
348 Tcl_Obj *const reqv[], /* 0 means to use the latest version
349 * available. */
350 void *clientDataPtr)
351 {
352 RequireProcArgs args;
353 args.name = name;
354 args.clientDataPtr = clientDataPtr;
355 return Tcl_NRCallObjProc(interp, TclNRPkgRequireProc, (void *)&args, reqc, reqv);
356 }
357
358 static int
359 TclNRPkgRequireProc(
360 ClientData clientData,
361 Tcl_Interp *interp,
362 int reqc,
363 Tcl_Obj *const reqv[]) {
364 RequireProcArgs *args = clientData;
365 Tcl_NRAddCallback(interp, PkgRequireCore, (void *)args->name, INT2PTR(reqc), (void *)reqv, args->clientDataPtr);
366 return TCL_OK;
367 }
368
369 static int
370 PkgRequireCore(ClientData data[], Tcl_Interp *interp, int result)
371 {
372 const char *name = data[0];
373 int reqc = PTR2INT(data[1]);
374 Tcl_Obj *const *reqv = data[2];
375 int code = CheckAllRequirements(interp, reqc, reqv);
376 Require *reqPtr;
377 if (code != TCL_OK) {
378 return code;
379 }
380 reqPtr = ckalloc(sizeof(Require));
381 Tcl_NRAddCallback(interp, PkgRequireCoreCleanup, reqPtr, NULL, NULL, NULL);
382 reqPtr->clientDataPtr = data[3];
383 reqPtr->name = name;
384 reqPtr->pkgPtr = FindPackage(interp, name);
385 if (reqPtr->pkgPtr->version == NULL) {
386 Tcl_NRAddCallback(interp, SelectPackage, reqPtr, INT2PTR(reqc), (void *)reqv, PkgRequireCoreStep1);
387 } else {
388 Tcl_NRAddCallback(interp, PkgRequireCoreFinal, reqPtr, INT2PTR(reqc), (void *)reqv, NULL);
389 }
390 return TCL_OK;
391 }
392
393 static int
394 PkgRequireCoreStep1(ClientData data[], Tcl_Interp *interp, int result) {
395 Tcl_DString command;
396 char *script;
397 Require *reqPtr = data[0];
398 int reqc = PTR2INT(data[1]);
399 Tcl_Obj **const reqv = data[2];
400 const char *name = reqPtr->name /* Name of desired package. */;
401 if (reqPtr->pkgPtr->version == NULL) {
402 /*
403 * The package is not in the database. If there is a "package unknown"
404 * command, invoke it.
405 */
406
407 script = ((Interp *) interp)->packageUnknown;
408 if (script == NULL) {
409 Tcl_NRAddCallback(interp, PkgRequireCoreFinal, reqPtr, INT2PTR(reqc), (void *)reqv, NULL);
410 } else {
411 Tcl_DStringInit(&command);
412 Tcl_DStringAppend(&command, script, -1);
413 Tcl_DStringAppendElement(&command, name);
414 AddRequirementsToDString(&command, reqc, reqv);
415
416 Tcl_NRAddCallback(interp, PkgRequireCoreStep2, reqPtr, INT2PTR(reqc), (void *)reqv, NULL);
417 Tcl_NREvalObj(interp, TclDStringToObj(&command), TCL_EVAL_GLOBAL);
418 }
419 return TCL_OK;
420 } else {
421 Tcl_NRAddCallback(interp, PkgRequireCoreFinal, reqPtr, INT2PTR(reqc), (void *)reqv, NULL);
422 }
423 return TCL_OK;
424 }
425
426 static int
427 PkgRequireCoreStep2(ClientData data[], Tcl_Interp *interp, int result) {
428 Require *reqPtr = data[0];
429 int reqc = PTR2INT(data[1]);
430 Tcl_Obj **const reqv = data[2];
431 const char *name = reqPtr->name /* Name of desired package. */;
432 if ((result != TCL_OK) && (result != TCL_ERROR)) {
433 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
434 "bad return code: %d", result));
435 Tcl_SetErrorCode(interp, "TCL", "PACKAGE", "BADRESULT", (char *)NULL);
436 result = TCL_ERROR;
437 }
438 if (result == TCL_ERROR) {
439 Tcl_AddErrorInfo(interp,
440 "\n (\"package unknown\" script)");
441 return result;
442 }
443 Tcl_ResetResult(interp);
444 /* pkgPtr may now be invalid, so refresh it. */
445 reqPtr->pkgPtr = FindPackage(interp, name);
446 Tcl_NRAddCallback(interp, SelectPackage, reqPtr, INT2PTR(reqc), (void *)reqv, PkgRequireCoreFinal);
447 return TCL_OK;
448 }
449
450 static int
451 PkgRequireCoreFinal(ClientData data[], Tcl_Interp *interp, int result) {
452 Require *reqPtr = data[0];
453 int reqc = PTR2INT(data[1]), satisfies;
454 Tcl_Obj **const reqv = data[2];
455 char *pkgVersionI;
456 void *clientDataPtr = reqPtr->clientDataPtr;
457 const char *name = reqPtr->name /* Name of desired package. */;
458 if (reqPtr->pkgPtr->version == NULL) {
459 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
460 "can't find package %s", name));
461 Tcl_SetErrorCode(interp, "TCL", "PACKAGE", "UNFOUND", (char *)NULL);
462 AddRequirementsToResult(interp, reqc, reqv);
463 return TCL_ERROR;
464 }
465
466 /*
467 * Ensure that the provided version meets the current requirements.
468 */
469
470 if (reqc != 0) {
471 CheckVersionAndConvert(interp, TclGetString(reqPtr->pkgPtr->version),
472 &pkgVersionI, NULL);
473 satisfies = SomeRequirementSatisfied(pkgVersionI, reqc, reqv);
474
475 ckfree(pkgVersionI);
476
477 if (!satisfies) {
478 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
479 "version conflict for package \"%s\": have %s, need",
480 name, TclGetString(reqPtr->pkgPtr->version)));
481 Tcl_SetErrorCode(interp, "TCL", "PACKAGE", "VERSIONCONFLICT",
482 (char *)NULL);
483 AddRequirementsToResult(interp, reqc, reqv);
484 return TCL_ERROR;
485 }
486 }
487
488 if (clientDataPtr) {
489 const void **ptr = (const void **) clientDataPtr;
490
491 *ptr = reqPtr->pkgPtr->clientData;
492 }
493 Tcl_SetObjResult(interp, reqPtr->pkgPtr->version);
494 return TCL_OK;
495 }
496
497 static int
498 PkgRequireCoreCleanup(ClientData data[], Tcl_Interp *interp, int result) {
499 ckfree(data[0]);
500 return result;
501 }
502
503
504 static int
505 SelectPackage(ClientData data[], Tcl_Interp *interp, int result) {
506 PkgAvail *availPtr, *bestPtr, *bestStablePtr;
507 char *availVersion, *bestVersion, *bestStableVersion;
508 /* Internal rep. of versions */
509 int availStable, satisfies;
510 Require *reqPtr = data[0];
511 int reqc = PTR2INT(data[1]);
512 Tcl_Obj **const reqv = data[2];
513 const char *name = reqPtr->name;
514 Package *pkgPtr = reqPtr->pkgPtr;
515 Interp *iPtr = (Interp *) interp;
516
517 /*
518 * Check whether we're already attempting to load some version of this
519 * package (circular dependency detection).
520 */
521
522 if (pkgPtr->clientData != NULL) {
523 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
524 "circular package dependency:"
525 " attempt to provide %s %s requires %s",
526 name, (char *) pkgPtr->clientData, name));
527 AddRequirementsToResult(interp, reqc, reqv);
528 Tcl_SetErrorCode(interp, "TCL", "PACKAGE", "CIRCULARITY", (char *)NULL);
529 return TCL_ERROR;
530 }
531
532 /*
533 * The package isn't yet present. Search the list of available
534 * versions and invoke the script for the best available version. We
535 * are actually locating the best, and the best stable version. One of
536 * them is then chosen based on the selection mode.
537 */
538
539 bestPtr = NULL;
540 bestStablePtr = NULL;
541 bestVersion = NULL;
542 bestStableVersion = NULL;
543
544 for (availPtr = pkgPtr->availPtr; availPtr != NULL;
545 availPtr = availPtr->nextPtr) {
546 if (CheckVersionAndConvert(interp, availPtr->version,
547 &availVersion, &availStable) != TCL_OK) {
548 /*
549 * The provided version number has invalid syntax. This
550 * should not happen. This should have been caught by the
551 * 'package ifneeded' registering the package.
552 */
553
554 continue;
555 }
556
557 /* Check satisfaction of requirements before considering the current version further. */
558 if (reqc > 0) {
559 satisfies = SomeRequirementSatisfied(availVersion, reqc, reqv);
560 if (!satisfies) {
561 ckfree(availVersion);
562 availVersion = NULL;
563 continue;
564 }
565 }
566
567 if (bestPtr != NULL) {
568 int res = CompareVersions(availVersion, bestVersion, NULL);
569
570 /*
571 * Note: Used internal reps in the comparison!
572 */
573
574 if (res > 0) {
575 /*
576 * The version of the package sought is better than the
577 * currently selected version.
578 */
579 ckfree(bestVersion);
580 bestVersion = NULL;
581 goto newbest;
582 }
583 } else {
584 newbest:
585 /* We have found a version which is better than our max. */
586
587 bestPtr = availPtr;
588 CheckVersionAndConvert(interp, bestPtr->version, &bestVersion, NULL);
589 }
590
591 if (!availStable) {
592 ckfree(availVersion);
593 availVersion = NULL;
594 continue;
595 }
596
597 if (bestStablePtr != NULL) {
598 int res = CompareVersions(availVersion, bestStableVersion, NULL);
599
600 /*
601 * Note: Used internal reps in the comparison!
602 */
603
604 if (res > 0) {
605 /*
606 * This stable version of the package sought is better
607 * than the currently selected stable version.
608 */
609 ckfree(bestStableVersion);
610 bestStableVersion = NULL;
611 goto newstable;
612 }
613 } else {
614 newstable:
615 /* We have found a stable version which is better than our max stable. */
616 bestStablePtr = availPtr;
617 CheckVersionAndConvert(interp, bestStablePtr->version, &bestStableVersion, NULL);
618 }
619
620 ckfree(availVersion);
621 availVersion = NULL;
622 } /* end for */
623
624 /*
625 * Clean up memorized internal reps, if any.
626 */
627
628 if (bestVersion != NULL) {
629 ckfree(bestVersion);
630 bestVersion = NULL;
631 }
632
633 if (bestStableVersion != NULL) {
634 ckfree(bestStableVersion);
635 bestStableVersion = NULL;
636 }
637
638 /*
639 * Now choose a version among the two best. For 'latest' we simply
640 * take (actually keep) the best. For 'stable' we take the best
641 * stable, if there is any, or the best if there is nothing stable.
642 */
643
644 if ((iPtr->packagePrefer == PKG_PREFER_STABLE)
645 && (bestStablePtr != NULL)) {
646 bestPtr = bestStablePtr;
647 }
648
649 if (bestPtr == NULL) {
650 Tcl_NRAddCallback(interp, data[3], reqPtr, INT2PTR(reqc), (void *)reqv, NULL);
651 } else {
652 /*
653 * We found an ifneeded script for the package. Be careful while
654 * executing it: this could cause reentrancy, so (a) protect the
655 * script itself from deletion and (b) don't assume that bestPtr
656 * will still exist when the script completes.
657 */
658
659 char *versionToProvide = bestPtr->version;
660
661 pkgPtr->clientData = versionToProvide;
662 Tcl_Preserve(versionToProvide);
663 reqPtr->versionToProvide = versionToProvide;
664 Tcl_NRAddCallback(interp, SelectPackageFinal, reqPtr, INT2PTR(reqc), (void *)reqv, data[3]);
665 Tcl_NREvalObj(interp, Tcl_NewStringObj(bestPtr->script, -1), TCL_EVAL_GLOBAL);
666 }
667 return TCL_OK;
668 }
669
670 static int
671 SelectPackageFinal(ClientData data[], Tcl_Interp *interp, int result) {
672 Require *reqPtr = data[0];
673 int reqc = PTR2INT(data[1]);
674 Tcl_Obj **const reqv = data[2];
675 const char *name = reqPtr->name;
676 char *versionToProvide = reqPtr->versionToProvide;
677
678 reqPtr->pkgPtr = FindPackage(interp, name);
679 if (result == TCL_OK) {
680 Tcl_ResetResult(interp);
681 if (reqPtr->pkgPtr->version == NULL) {
682 result = TCL_ERROR;
683 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
684 "attempt to provide package %s %s failed:"
685 " no version of package %s provided",
686 name, versionToProvide, name));
687 Tcl_SetErrorCode(interp, "TCL", "PACKAGE", "UNPROVIDED",
688 (char *)NULL);
689 } else {
690 char *pvi, *vi;
691
692 if (TCL_OK != CheckVersionAndConvert(interp,
693 TclGetString(reqPtr->pkgPtr->version), &pvi, NULL)) {
694 result = TCL_ERROR;
695 } else if (CheckVersionAndConvert(interp,
696 versionToProvide, &vi, NULL) != TCL_OK) {
697 ckfree(pvi);
698 result = TCL_ERROR;
699 } else {
700 int res = CompareVersions(pvi, vi, NULL);
701
702 ckfree(pvi);
703 ckfree(vi);
704 if (res != 0) {
705 result = TCL_ERROR;
706 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
707 "attempt to provide package %s %s failed:"
708 " package %s %s provided instead",
709 name, versionToProvide,
710 name, TclGetString(reqPtr->pkgPtr->version)));
711 Tcl_SetErrorCode(interp, "TCL", "PACKAGE",
712 "WRONGPROVIDE", (char *)NULL);
713 }
714 }
715 }
716 } else if (result != TCL_ERROR) {
717 Tcl_Obj *codePtr;
718
719 TclNewIntObj(codePtr, result);
720 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
721 "attempt to provide package %s %s failed:"
722 " bad return code: %s",
723 name, versionToProvide, TclGetString(codePtr)));
724 Tcl_SetErrorCode(interp, "TCL", "PACKAGE", "BADRESULT", (char *)NULL);
725 TclDecrRefCount(codePtr);
726 result = TCL_ERROR;
727 }
728
729 if (result == TCL_ERROR) {
730 Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf(
731 "\n (\"package ifneeded %s %s\" script)",
732 name, versionToProvide));
733 }
734 Tcl_Release(versionToProvide);
735
736 if (result != TCL_OK) {
737 /*
738 * Take a non-TCL_OK code from the script as an indication the
739 * package wasn't loaded properly, so the package system
740 * should not remember an improper load.
741 *
742 * This is consistent with our returning NULL. If we're not
743 * willing to tell our caller we got a particular version, we
744 * shouldn't store that version for telling future callers
745 * either.
746 */
747
748 if (reqPtr->pkgPtr->version != NULL) {
749 Tcl_DecrRefCount(reqPtr->pkgPtr->version);
750 reqPtr->pkgPtr->version = NULL;
751 }
752 reqPtr->pkgPtr->clientData = NULL;
753 return result;
754 }
755
756 Tcl_NRAddCallback(interp, data[3], reqPtr, INT2PTR(reqc), (void *)reqv, NULL);
757 return TCL_OK;
758 }
759
760 /*
761 *----------------------------------------------------------------------
762 *
763 * Tcl_PkgPresent / Tcl_PkgPresentEx --
764 *
765 * Checks to see whether the specified package is present. If it is not
766 * then no additional action is taken.
767 *
768 * Results:
769 * If successful, returns the version string for the currently provided
770 * version of the package, which may be different from the "version"
771 * argument. If the caller's requirements cannot be met (e.g. the version
772 * requested conflicts with a currently provided version), NULL is
773 * returned and an error message is left in interp->result.
774 *
775 * Side effects:
776 * None.
777 *
778 *----------------------------------------------------------------------
779 */
780
781 #undef Tcl_PkgPresent
782 const char *
783 Tcl_PkgPresent(
784 Tcl_Interp *interp, /* Interpreter in which package is now
785 * available. */
786 const char *name, /* Name of desired package. */
787 const char *version, /* Version string for desired version; NULL
788 * means use the latest version available. */
789 int exact) /* Non-zero means that only the particular
790 * version given is acceptable. Zero means use
791 * the latest compatible version. */
792 {
793 return Tcl_PkgPresentEx(interp, name, version, exact, NULL);
794 }
795
796 const char *
797 Tcl_PkgPresentEx(
798 Tcl_Interp *interp, /* Interpreter in which package is now
799 * available. */
800 const char *name, /* Name of desired package. */
801 const char *version, /* Version string for desired version; NULL
802 * means use the latest version available. */
803 int exact, /* Non-zero means that only the particular
804 * version given is acceptable. Zero means use
805 * the latest compatible version. */
806 void *clientDataPtr) /* Used to return the client data for this
807 * package. If it is NULL then the client data
808 * is not returned. This is unchanged if this
809 * call fails for any reason. */
810 {
811 Interp *iPtr = (Interp *) interp;
812 Tcl_HashEntry *hPtr;
813 Package *pkgPtr;
814
815 hPtr = Tcl_FindHashEntry(&iPtr->packageTable, name);
816 if (hPtr) {
817 pkgPtr = Tcl_GetHashValue(hPtr);
818 if (pkgPtr->version != NULL) {
819 /*
820 * At this point we know that the package is present. Make sure
821 * that the provided version meets the current requirement by
822 * calling Tcl_PkgRequireEx() to check for us.
823 */
824
825 const char *foundVersion = Tcl_PkgRequireEx(interp, name, version,
826 exact, clientDataPtr);
827
828 if (foundVersion == NULL) {
829 Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "PACKAGE", name,
830 (char *)NULL);
831 }
832 return foundVersion;
833 }
834 }
835
836 if (version != NULL) {
837 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
838 "package %s %s is not present", name, version));
839 } else {
840 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
841 "package %s is not present", name));
842 }
843 Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "PACKAGE", name, (char *)NULL);
844 return NULL;
845 }
846
847 /*
848 *----------------------------------------------------------------------
849 *
850 * Tcl_PackageObjCmd --
851 *
852 * This function is invoked to process the "package" Tcl command. See the
853 * user documentation for details on what it does.
854 *
855 * Results:
856 * A standard Tcl result.
857 *
858 * Side effects:
859 * See the user documentation.
860 *
861 *----------------------------------------------------------------------
862 */
863 int
864 Tcl_PackageObjCmd(
865 ClientData dummy, /* Not used. */
866 Tcl_Interp *interp, /* Current interpreter. */
867 int objc, /* Number of arguments. */
868 Tcl_Obj *const objv[]) /* Argument objects. */
869 {
870 return Tcl_NRCallObjProc(interp, TclNRPackageObjCmd, NULL, objc, objv);
871 }
872
873 /* ARGSUSED */
874 int
875 TclNRPackageObjCmd(
876 ClientData dummy, /* Not used. */
877 Tcl_Interp *interp, /* Current interpreter. */
878 int objc, /* Number of arguments. */
879 Tcl_Obj *const objv[]) /* Argument objects. */
880 {
881 static const char *const pkgOptions[] = {
882 "forget", "ifneeded", "names", "prefer", "present",
883 "provide", "require", "unknown", "vcompare", "versions",
884 "vsatisfies", NULL
885 };
886 enum pkgOptions {
887 PKG_FORGET, PKG_IFNEEDED, PKG_NAMES, PKG_PREFER, PKG_PRESENT,
888 PKG_PROVIDE, PKG_REQUIRE, PKG_UNKNOWN, PKG_VCOMPARE, PKG_VERSIONS,
889 PKG_VSATISFIES
890 };
891 Interp *iPtr = (Interp *) interp;
892 int optionIndex, exact, i, newobjc, satisfies;
893 PkgAvail *availPtr, *prevPtr;
894 Package *pkgPtr;
895 Tcl_HashEntry *hPtr;
896 Tcl_HashSearch search;
897 Tcl_HashTable *tablePtr;
898 const char *version;
899 const char *argv2, *argv3, *argv4;
900 char *iva = NULL, *ivb = NULL;
901 Tcl_Obj *objvListPtr, **newObjvPtr;
902
903 if (objc < 2) {
904 Tcl_WrongNumArgs(interp, 1, objv, "option ?arg ...?");
905 return TCL_ERROR;
906 }
907
908 if (Tcl_GetIndexFromObj(interp, objv[1], pkgOptions, "option", 0,
909 &optionIndex) != TCL_OK) {
910 return TCL_ERROR;
911 }
912 switch ((enum pkgOptions) optionIndex) {
913 case PKG_FORGET: {
914 const char *keyString;
915
916 for (i = 2; i < objc; i++) {
917 keyString = TclGetString(objv[i]);
918 hPtr = Tcl_FindHashEntry(&iPtr->packageTable, keyString);
919 if (hPtr == NULL) {
920 continue;
921 }
922 pkgPtr = Tcl_GetHashValue(hPtr);
923 Tcl_DeleteHashEntry(hPtr);
924 if (pkgPtr->version != NULL) {
925 Tcl_DecrRefCount(pkgPtr->version);
926 }
927 while (pkgPtr->availPtr != NULL) {
928 availPtr = pkgPtr->availPtr;
929 pkgPtr->availPtr = availPtr->nextPtr;
930 Tcl_EventuallyFree(availPtr->version, TCL_DYNAMIC);
931 Tcl_EventuallyFree(availPtr->script, TCL_DYNAMIC);
932 ckfree(availPtr);
933 }
934 ckfree(pkgPtr);
935 }
936 break;
937 }
938 case PKG_IFNEEDED: {
939 int length, res;
940 char *argv3i, *avi;
941
942 if ((objc != 4) && (objc != 5)) {
943 Tcl_WrongNumArgs(interp, 2, objv, "package version ?script?");
944 return TCL_ERROR;
945 }
946 argv3 = TclGetString(objv[3]);
947 if (CheckVersionAndConvert(interp, argv3, &argv3i, NULL) != TCL_OK) {
948 return TCL_ERROR;
949 }
950 argv2 = TclGetString(objv[2]);
951 if (objc == 4) {
952 hPtr = Tcl_FindHashEntry(&iPtr->packageTable, argv2);
953 if (hPtr == NULL) {
954 ckfree(argv3i);
955 return TCL_OK;
956 }
957 pkgPtr = Tcl_GetHashValue(hPtr);
958 } else {
959 pkgPtr = FindPackage(interp, argv2);
960 }
961 argv3 = TclGetStringFromObj(objv[3], &length);
962
963 for (availPtr = pkgPtr->availPtr, prevPtr = NULL; availPtr != NULL;
964 prevPtr = availPtr, availPtr = availPtr->nextPtr) {
965 if (CheckVersionAndConvert(interp, availPtr->version, &avi,
966 NULL) != TCL_OK) {
967 ckfree(argv3i);
968 return TCL_ERROR;
969 }
970
971 res = CompareVersions(avi, argv3i, NULL);
972 ckfree(avi);
973
974 if (res == 0){
975 if (objc == 4) {
976 ckfree(argv3i);
977 Tcl_SetObjResult(interp,
978 Tcl_NewStringObj(availPtr->script, -1));
979 return TCL_OK;
980 }
981 Tcl_EventuallyFree(availPtr->script, TCL_DYNAMIC);
982 break;
983 }
984 }
985 ckfree(argv3i);
986
987 if (objc == 4) {
988 return TCL_OK;
989 }
990 if (availPtr == NULL) {
991 availPtr = ckalloc(sizeof(PkgAvail));
992 DupBlock(availPtr->version, argv3, (unsigned) length + 1);
993
994 if (prevPtr == NULL) {
995 availPtr->nextPtr = pkgPtr->availPtr;
996 pkgPtr->availPtr = availPtr;
997 } else {
998 availPtr->nextPtr = prevPtr->nextPtr;
999 prevPtr->nextPtr = availPtr;
1000 }
1001 }
1002 argv4 = TclGetStringFromObj(objv[4], &length);
1003 DupBlock(availPtr->script, argv4, (unsigned) length + 1);
1004 break;
1005 }
1006 case PKG_NAMES:
1007 if (objc != 2) {
1008 Tcl_WrongNumArgs(interp, 2, objv, NULL);
1009 return TCL_ERROR;
1010 } else {
1011 Tcl_Obj *resultObj;
1012
1013 TclNewObj(resultObj);
1014 tablePtr = &iPtr->packageTable;
1015 for (hPtr = Tcl_FirstHashEntry(tablePtr, &search); hPtr != NULL;
1016 hPtr = Tcl_NextHashEntry(&search)) {
1017 pkgPtr = Tcl_GetHashValue(hPtr);
1018 if ((pkgPtr->version != NULL) || (pkgPtr->availPtr != NULL)) {
1019 Tcl_ListObjAppendElement(NULL,resultObj, Tcl_NewStringObj(
1020 Tcl_GetHashKey(tablePtr, hPtr), -1));
1021 }
1022 }
1023 Tcl_SetObjResult(interp, resultObj);
1024 }
1025 break;
1026 case PKG_PRESENT: {
1027 const char *name;
1028
1029 if (objc < 3) {
1030 goto require;
1031 }
1032 argv2 = TclGetString(objv[2]);
1033 if ((argv2[0] == '-') && (strcmp(argv2, "-exact") == 0)) {
1034 if (objc != 5) {
1035 goto requireSyntax;
1036 }
1037 exact = 1;
1038 name = TclGetString(objv[3]);
1039 } else {
1040 exact = 0;
1041 name = argv2;
1042 }
1043
1044 hPtr = Tcl_FindHashEntry(&iPtr->packageTable, name);
1045 if (hPtr != NULL) {
1046 pkgPtr = Tcl_GetHashValue(hPtr);
1047 if (pkgPtr->version != NULL) {
1048 goto require;
1049 }
1050 }
1051
1052 version = NULL;
1053 if (exact) {
1054 version = TclGetString(objv[4]);
1055 if (CheckVersionAndConvert(interp, version, NULL,
1056 NULL) != TCL_OK) {
1057 return TCL_ERROR;
1058 }
1059 } else {
1060 if (CheckAllRequirements(interp, objc-3, objv+3) != TCL_OK) {
1061 return TCL_ERROR;
1062 }
1063 if ((objc > 3) && (CheckVersionAndConvert(interp,
1064 TclGetString(objv[3]), NULL, NULL) == TCL_OK)) {
1065 version = TclGetString(objv[3]);
1066 }
1067 }
1068 Tcl_PkgPresentEx(interp, name, version, exact, NULL);
1069 return TCL_ERROR;
1070 break;
1071 }
1072 case PKG_PROVIDE:
1073 if ((objc != 3) && (objc != 4)) {
1074 Tcl_WrongNumArgs(interp, 2, objv, "package ?version?");
1075 return TCL_ERROR;
1076 }
1077 argv2 = TclGetString(objv[2]);
1078 if (objc == 3) {
1079 hPtr = Tcl_FindHashEntry(&iPtr->packageTable, argv2);
1080 if (hPtr != NULL) {
1081 pkgPtr = Tcl_GetHashValue(hPtr);
1082 if (pkgPtr->version != NULL) {
1083 Tcl_SetObjResult(interp, pkgPtr->version);
1084 }
1085 }
1086 return TCL_OK;
1087 }
1088 argv3 = TclGetString(objv[3]);
1089 if (CheckVersionAndConvert(interp, argv3, NULL, NULL) != TCL_OK) {
1090 return TCL_ERROR;
1091 }
1092 return Tcl_PkgProvideEx(interp, argv2, argv3, NULL);
1093 case PKG_REQUIRE:
1094 require:
1095 if (objc < 3) {
1096 requireSyntax:
1097 Tcl_WrongNumArgs(interp, 2, objv,
1098 "?-exact? package ?requirement ...?");
1099 return TCL_ERROR;
1100 }
1101
1102 version = NULL;
1103
1104 argv2 = TclGetString(objv[2]);
1105 if ((argv2[0] == '-') && (strcmp(argv2, "-exact") == 0)) {
1106 Tcl_Obj *ov;
1107
1108 if (objc != 5) {
1109 goto requireSyntax;
1110 }
1111
1112 version = TclGetString(objv[4]);
1113 if (CheckVersionAndConvert(interp, version, NULL,
1114 NULL) != TCL_OK) {
1115 return TCL_ERROR;
1116 }
1117
1118 /*
1119 * Create a new-style requirement for the exact version.
1120 */
1121
1122 ov = Tcl_NewStringObj(version, -1);
1123 Tcl_AppendStringsToObj(ov, "-", version, NULL);
1124 version = NULL;
1125 argv3 = TclGetString(objv[3]);
1126 Tcl_IncrRefCount(objv[3]);
1127
1128 objvListPtr = Tcl_NewListObj(0, NULL);
1129 Tcl_IncrRefCount(objvListPtr);
1130 Tcl_ListObjAppendElement(interp, objvListPtr, ov);
1131 TclListObjGetElements(interp, objvListPtr, &newobjc, &newObjvPtr);
1132
1133 Tcl_NRAddCallback(interp, TclNRPackageObjCmdCleanup, objv[3], objvListPtr, NULL, NULL);
1134 Tcl_NRAddCallback(interp, PkgRequireCore, (void *)argv3, INT2PTR(newobjc), newObjvPtr, NULL);
1135 return TCL_OK;
1136 } else {
1137 Tcl_Obj *const *newobjv = objv + 3;
1138 newobjc = objc - 3;
1139
1140 if (CheckAllRequirements(interp, objc - 3, objv + 3) != TCL_OK) {
1141 return TCL_ERROR;
1142 }
1143 objvListPtr = Tcl_NewListObj(0, NULL);
1144 Tcl_IncrRefCount(objvListPtr);
1145 Tcl_IncrRefCount(objv[2]);
1146 for (i = 0; i < newobjc; i++) {
1147
1148 /*
1149 * Tcl_Obj structures may have come from another interpreter,
1150 * so duplicate them.
1151 */
1152
1153 Tcl_ListObjAppendElement(interp, objvListPtr, Tcl_DuplicateObj(newobjv[i]));
1154 }
1155 TclListObjGetElements(interp, objvListPtr, &newobjc, &newObjvPtr);
1156 Tcl_NRAddCallback(interp, TclNRPackageObjCmdCleanup, objv[2], objvListPtr, NULL, NULL);
1157 Tcl_NRAddCallback(interp, PkgRequireCore, (void *)argv2, INT2PTR(newobjc), newObjvPtr, NULL);
1158 return TCL_OK;
1159 }
1160 break;
1161 case PKG_UNKNOWN: {
1162 int length;
1163
1164 if (objc == 2) {
1165 if (iPtr->packageUnknown != NULL) {
1166 Tcl_SetObjResult(interp,
1167 Tcl_NewStringObj(iPtr->packageUnknown, -1));
1168 }
1169 } else if (objc == 3) {
1170 if (iPtr->packageUnknown != NULL) {
1171 ckfree(iPtr->packageUnknown);
1172 }
1173 argv2 = TclGetStringFromObj(objv[2], &length);
1174 if (argv2[0] == 0) {
1175 iPtr->packageUnknown = NULL;
1176 } else {
1177 DupBlock(iPtr->packageUnknown, argv2, (unsigned) length+1);
1178 }
1179 } else {
1180 Tcl_WrongNumArgs(interp, 2, objv, "?command?");
1181 return TCL_ERROR;
1182 }
1183 break;
1184 }
1185 case PKG_PREFER: {
1186 static const char *const pkgPreferOptions[] = {
1187 "latest", "stable", NULL
1188 };
1189
1190 /*
1191 * See tclInt.h for the enum, just before Interp.
1192 */
1193
1194 if (objc > 3) {
1195 Tcl_WrongNumArgs(interp, 2, objv, "?latest|stable?");
1196 return TCL_ERROR;
1197 } else if (objc == 3) {
1198 /*
1199 * Seting the value.
1200 */
1201
1202 int newPref;
1203
1204 if (Tcl_GetIndexFromObj(interp, objv[2], pkgPreferOptions,
1205 "preference", 0, &newPref) != TCL_OK) {
1206 return TCL_ERROR;
1207 }
1208
1209 if (newPref < iPtr->packagePrefer) {
1210 iPtr->packagePrefer = newPref;
1211 }
1212 }
1213
1214 /*
1215 * Always return current value.
1216 */
1217
1218 Tcl_SetObjResult(interp,
1219 Tcl_NewStringObj(pkgPreferOptions[iPtr->packagePrefer], -1));
1220 break;
1221 }
1222 case PKG_VCOMPARE:
1223 if (objc != 4) {
1224 Tcl_WrongNumArgs(interp, 2, objv, "version1 version2");
1225 return TCL_ERROR;
1226 }
1227 argv3 = TclGetString(objv[3]);
1228 argv2 = TclGetString(objv[2]);
1229 if (CheckVersionAndConvert(interp, argv2, &iva, NULL) != TCL_OK ||
1230 CheckVersionAndConvert(interp, argv3, &ivb, NULL) != TCL_OK) {
1231 if (iva != NULL) {
1232 ckfree(iva);
1233 }
1234
1235 /*
1236 * ivb cannot be set in this branch.
1237 */
1238
1239 return TCL_ERROR;
1240 }
1241
1242 /*
1243 * Comparison is done on the internal representation.
1244 */
1245
1246 Tcl_SetObjResult(interp,
1247 Tcl_NewIntObj(CompareVersions(iva, ivb, NULL)));
1248 ckfree(iva);
1249 ckfree(ivb);
1250 break;
1251 case PKG_VERSIONS:
1252 if (objc != 3) {
1253 Tcl_WrongNumArgs(interp, 2, objv, "package");
1254 return TCL_ERROR;
1255 } else {
1256 Tcl_Obj *resultObj;
1257
1258 TclNewObj(resultObj);
1259 argv2 = TclGetString(objv[2]);
1260 hPtr = Tcl_FindHashEntry(&iPtr->packageTable, argv2);
1261 if (hPtr != NULL) {
1262 pkgPtr = Tcl_GetHashValue(hPtr);
1263 for (availPtr = pkgPtr->availPtr; availPtr != NULL;
1264 availPtr = availPtr->nextPtr) {
1265 Tcl_ListObjAppendElement(NULL, resultObj,
1266 Tcl_NewStringObj(availPtr->version, -1));
1267 }
1268 }
1269 Tcl_SetObjResult(interp, resultObj);
1270 }
1271 break;
1272 case PKG_VSATISFIES: {
1273 char *argv2i = NULL;
1274
1275 if (objc < 4) {
1276 Tcl_WrongNumArgs(interp, 2, objv, "version ?requirement ...?");
1277 return TCL_ERROR;
1278 }
1279
1280 argv2 = TclGetString(objv[2]);
1281 if (CheckVersionAndConvert(interp, argv2, &argv2i, NULL) != TCL_OK) {
1282 return TCL_ERROR;
1283 } else if (CheckAllRequirements(interp, objc-3, objv+3) != TCL_OK) {
1284 ckfree(argv2i);
1285 return TCL_ERROR;
1286 }
1287
1288 satisfies = SomeRequirementSatisfied(argv2i, objc-3, objv+3);
1289 ckfree(argv2i);
1290
1291 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(satisfies));
1292 break;
1293 }
1294 default:
1295 Tcl_Panic("Tcl_PackageObjCmd: bad option index to pkgOptions");
1296 }
1297 return TCL_OK;
1298 }
1299
1300 static int
1301 TclNRPackageObjCmdCleanup(ClientData data[], Tcl_Interp *interp, int result) {
1302 TclDecrRefCount((Tcl_Obj *)data[0]);
1303 TclDecrRefCount((Tcl_Obj *)data[1]);
1304 return result;
1305 }
1306
1307 /*
1308 *----------------------------------------------------------------------
1309 *
1310 * FindPackage --
1311 *
1312 * This function finds the Package record for a particular package in a
1313 * particular interpreter, creating a record if one doesn't already
1314 * exist.
1315 *
1316 * Results:
1317 * The return value is a pointer to the Package record for the package.
1318 *
1319 * Side effects:
1320 * A new Package record may be created.
1321 *
1322 *----------------------------------------------------------------------
1323 */
1324
1325 static Package *
1326 FindPackage(
1327 Tcl_Interp *interp, /* Interpreter to use for package lookup. */
1328 const char *name) /* Name of package to fine. */
1329 {
1330 Interp *iPtr = (Interp *) interp;
1331 Tcl_HashEntry *hPtr;
1332 int isNew;
1333 Package *pkgPtr;
1334
1335 hPtr = Tcl_CreateHashEntry(&iPtr->packageTable, name, &isNew);
1336 if (isNew) {
1337 pkgPtr = ckalloc(sizeof(Package));
1338 pkgPtr->version = NULL;
1339 pkgPtr->availPtr = NULL;
1340 pkgPtr->clientData = NULL;
1341 Tcl_SetHashValue(hPtr, pkgPtr);
1342 } else {
1343 pkgPtr = Tcl_GetHashValue(hPtr);
1344 }
1345 return pkgPtr;
1346 }
1347
1348 /*
1349 *----------------------------------------------------------------------
1350 *
1351 * TclFreePackageInfo --
1352 *
1353 * This function is called during interpreter deletion to free all of the
1354 * package-related information for the interpreter.
1355 *
1356 * Results:
1357 * None.
1358 *
1359 * Side effects:
1360 * Memory is freed.
1361 *
1362 *----------------------------------------------------------------------
1363 */
1364
1365 void
1366 TclFreePackageInfo(
1367 Interp *iPtr) /* Interpereter that is being deleted. */
1368 {
1369 Package *pkgPtr;
1370 Tcl_HashSearch search;
1371 Tcl_HashEntry *hPtr;
1372 PkgAvail *availPtr;
1373
1374 for (hPtr = Tcl_FirstHashEntry(&iPtr->packageTable, &search);
1375 hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
1376 pkgPtr = Tcl_GetHashValue(hPtr);
1377 if (pkgPtr->version != NULL) {
1378 Tcl_DecrRefCount(pkgPtr->version);
1379 }
1380 while (pkgPtr->availPtr != NULL) {
1381 availPtr = pkgPtr->availPtr;
1382 pkgPtr->availPtr = availPtr->nextPtr;
1383 Tcl_EventuallyFree(availPtr->version, TCL_DYNAMIC);
1384 Tcl_EventuallyFree(availPtr->script, TCL_DYNAMIC);
1385 ckfree(availPtr);
1386 }
1387 ckfree(pkgPtr);
1388 }
1389 Tcl_DeleteHashTable(&iPtr->packageTable);
1390 if (iPtr->packageUnknown != NULL) {
1391 ckfree(iPtr->packageUnknown);
1392 }
1393 }
1394
1395 /*
1396 *----------------------------------------------------------------------
1397 *
1398 * CheckVersionAndConvert --
1399 *
1400 * This function checks to see whether a version number has valid syntax.
1401 * It also generates a semi-internal representation (string rep of a list
1402 * of numbers).
1403 *
1404 * Results:
1405 * If string is a properly formed version number the TCL_OK is returned.
1406 * Otherwise TCL_ERROR is returned and an error message is left in the
1407 * interp's result.
1408 *
1409 * Side effects:
1410 * None.
1411 *
1412 *----------------------------------------------------------------------
1413 */
1414
1415 static int
1416 CheckVersionAndConvert(
1417 Tcl_Interp *interp, /* Used for error reporting. */
1418 const char *string, /* Supposedly a version number, which is
1419 * groups of decimal digits separated by
1420 * dots. */
1421 char **internal, /* Internal normalized representation */
1422 int *stable) /* Flag: Version is (un)stable. */
1423 {
1424 const char *p = string;
1425 char prevChar;
1426 int hasunstable = 0;
1427 /*
1428 * 4* assuming that each char is a separator (a,b become ' -x ').
1429 * 4+ to have space for an additional -2 at the end
1430 */
1431 char *ibuf = ckalloc(4 + 4*strlen(string));
1432 char *ip = ibuf;
1433
1434 /*
1435 * Basic rules
1436 * (1) First character has to be a digit.
1437 * (2) All other characters have to be a digit or '.'
1438 * (3) Two '.'s may not follow each other.
1439 *
1440 * TIP 268, Modified rules
1441 * (1) s.a.
1442 * (2) All other characters have to be a digit, 'a', 'b', or '.'
1443 * (3) s.a.
1444 * (4) Only one of 'a' or 'b' may occur.
1445 * (5) Neither 'a', nor 'b' may occur before or after a '.'
1446 */
1447
1448 if (!isdigit(UCHAR(*p))) { /* INTL: digit */
1449 goto error;
1450 }
1451
1452 *ip++ = *p;
1453
1454 for (prevChar = *p, p++; *p != 0; p++) {
1455 if (!isdigit(UCHAR(*p)) && /* INTL: digit */
1456 ((*p!='.' && *p!='a' && *p!='b') ||
1457 ((hasunstable && (*p=='a' || *p=='b')) ||
1458 ((prevChar=='a' || prevChar=='b' || prevChar=='.')
1459 && (*p=='.')) ||
1460 ((*p=='a' || *p=='b' || *p=='.') && prevChar=='.')))) {
1461 goto error;
1462 }
1463
1464 if (*p == 'a' || *p == 'b') {
1465 hasunstable = 1;
1466 }
1467
1468 /*
1469 * Translation to the internal rep. Regular version chars are copied
1470 * as is. The separators are translated to numerics. The new separator
1471 * for all parts is space.
1472 */
1473
1474 if (*p == '.') {
1475 *ip++ = ' ';
1476 *ip++ = '0';
1477 *ip++ = ' ';
1478 } else if (*p == 'a') {
1479 *ip++ = ' ';
1480 *ip++ = '-';
1481 *ip++ = '2';
1482 *ip++ = ' ';
1483 } else if (*p == 'b') {
1484 *ip++ = ' ';
1485 *ip++ = '-';
1486 *ip++ = '1';
1487 *ip++ = ' ';
1488 } else {
1489 *ip++ = *p;
1490 }
1491
1492 prevChar = *p;
1493 }
1494 if (prevChar!='.' && prevChar!='a' && prevChar!='b') {
1495 *ip = '\0';
1496 if (internal != NULL) {
1497 *internal = ibuf;
1498 } else {
1499 ckfree(ibuf);
1500 }
1501 if (stable != NULL) {
1502 *stable = !hasunstable;
1503 }
1504 return TCL_OK;
1505 }
1506
1507 error:
1508 ckfree(ibuf);
1509 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
1510 "expected version number but got \"%s\"", string));
1511 Tcl_SetErrorCode(interp, "TCL", "VALUE", "VERSION", (char *)NULL);
1512 return TCL_ERROR;
1513 }
1514
1515 /*
1516 *----------------------------------------------------------------------
1517 *
1518 * CompareVersions --
1519 *
1520 * This function compares two version numbers (in internal rep).
1521 *
1522 * Results:
1523 * The return value is -1 if v1 is less than v2, 0 if the two version
1524 * numbers are the same, and 1 if v1 is greater than v2. If *satPtr is
1525 * non-NULL, the word it points to is filled in with 1 if v2 >= v1 and
1526 * both numbers have the same major number or 0 otherwise.
1527 *
1528 * Side effects:
1529 * None.
1530 *
1531 *----------------------------------------------------------------------
1532 */
1533
1534 static int
1535 CompareVersions(
1536 char *v1, char *v2, /* Versions strings, of form 2.1.3 (any number
1537 * of version numbers). */
1538 int *isMajorPtr) /* If non-null, the word pointed to is filled
1539 * in with a 0/1 value. 1 means that the
1540 * difference occurred in the first element. */
1541 {
1542 int thisIsMajor, res, flip;
1543 char *s1, *e1, *s2, *e2, o1, o2;
1544
1545 /*
1546 * Each iteration of the following loop processes one number from each
1547 * string, terminated by a " " (space). If those numbers don't match then
1548 * the comparison is over; otherwise, we loop back for the next number.
1549 *
1550 * TIP 268.
1551 * This is identical the function 'ComparePkgVersion', but using the new
1552 * space separator as used by the internal rep of version numbers. The
1553 * special separators 'a' and 'b' have already been dealt with in
1554 * 'CheckVersionAndConvert', they were translated into numbers as well.
1555 * This keeps the comparison sane. Otherwise we would have to compare
1556 * numerics, the separators, and also deal with the special case of
1557 * end-of-string compared to separators. The semi-list rep we get here is
1558 * much easier to handle, as it is still regular.
1559 *
1560 * Rewritten to not compute a numeric value for the extracted version
1561 * number, but do string comparison. Skip any leading zeros for that to
1562 * work. This change breaks through the 32bit-limit on version numbers.
1563 */
1564
1565 thisIsMajor = 1;
1566 s1 = v1;
1567 s2 = v2;
1568
1569 while (1) {
1570 /*
1571 * Parse one decimal number from the front of each string. Skip
1572 * leading zeros. Terminate found number for upcoming string-wise
1573 * comparison, if needed.
1574 */
1575
1576 while ((*s1 != 0) && (*s1 == '0')) {
1577 s1++;
1578 }
1579 while ((*s2 != 0) && (*s2 == '0')) {
1580 s2++;
1581 }
1582
1583 /*
1584 * s1, s2 now point to the beginnings of the numbers to compare. Test
1585 * for their signs first, as shortcut to the result (different signs),
1586 * or determines if result has to be flipped (both negative). If there
1587 * is no shortcut we have to insert terminators later to limit the
1588 * strcmp.
1589 */
1590
1591 if ((*s1 == '-') && (*s2 != '-')) {
1592 /* s1 < 0, s2 >= 0 => s1 < s2 */
1593 res = -1;
1594 break;
1595 }
1596 if ((*s1 != '-') && (*s2 == '-')) {
1597 /* s1 >= 0, s2 < 0 => s1 > s2 */
1598 res = 1;
1599 break;
1600 }
1601
1602 if ((*s1 == '-') && (*s2 == '-')) {
1603 /* a < b => -a > -b, etc. */
1604 s1++;
1605 s2++;
1606 flip = 1;
1607 } else {
1608 flip = 0;
1609 }
1610
1611 /*
1612 * The string comparison is needed, so now we determine where the
1613 * numbers end.
1614 */
1615
1616 e1 = s1;
1617 while ((*e1 != 0) && (*e1 != ' ')) {
1618 e1++;
1619 }
1620 e2 = s2;
1621 while ((*e2 != 0) && (*e2 != ' ')) {
1622 e2++;
1623 }
1624
1625 /*
1626 * s1 .. e1 and s2 .. e2 now bracket the numbers to compare. Insert
1627 * terminators, compare, and restore actual contents. First however
1628 * another shortcut. Compare lengths. Shorter string is smaller
1629 * number! Thus we strcmp only strings of identical length.
1630 */
1631
1632 if ((e1-s1) < (e2-s2)) {
1633 res = -1;
1634 } else if ((e2-s2) < (e1-s1)) {
1635 res = 1;
1636 } else {
1637 o1 = *e1;
1638 *e1 = '\0';
1639 o2 = *e2;
1640 *e2 = '\0';
1641
1642 res = strcmp(s1, s2);
1643 res = (res < 0) ? -1 : (res ? 1 : 0);
1644
1645 *e1 = o1;
1646 *e2 = o2;
1647 }
1648
1649 /*
1650 * Stop comparing segments when a difference has been found. Here we
1651 * may have to flip the result to account for signs.
1652 */
1653
1654 if (res != 0) {
1655 if (flip) {
1656 res = -res;
1657 }
1658 break;
1659 }
1660
1661 /*
1662 * Go on to the next version number if the current numbers match.
1663 * However stop processing if the end of both numbers has been
1664 * reached.
1665 */
1666
1667 s1 = e1;
1668 s2 = e2;
1669
1670 if (*s1 != 0) {
1671 s1++;
1672 } else if (*s2 == 0) {
1673 /*
1674 * s1, s2 both at the end => identical
1675 */
1676
1677 res = 0;
1678 break;
1679 }
1680 if (*s2 != 0) {
1681 s2++;
1682 }
1683 thisIsMajor = 0;
1684 }
1685
1686 if (isMajorPtr != NULL) {
1687 *isMajorPtr = thisIsMajor;
1688 }
1689
1690 return res;
1691 }
1692
1693 /*
1694 *----------------------------------------------------------------------
1695 *
1696 * CheckAllRequirements --
1697 *
1698 * This function checks to see whether all requirements in a set have
1699 * valid syntax.
1700 *
1701 * Results:
1702 * TCL_OK is returned if all requirements are valid. Otherwise TCL_ERROR
1703 * is returned and an error message is left in the interp's result.
1704 *
1705 * Side effects:
1706 * May modify the interpreter result.
1707 *
1708 *----------------------------------------------------------------------
1709 */
1710
1711 static int
1712 CheckAllRequirements(
1713 Tcl_Interp *interp,
1714 int reqc, /* Requirements to check. */
1715 Tcl_Obj *const reqv[])
1716 {
1717 int i;
1718
1719 for (i = 0; i < reqc; i++) {
1720 if ((CheckRequirement(interp, TclGetString(reqv[i])) != TCL_OK)) {
1721 return TCL_ERROR;
1722 }
1723 }
1724 return TCL_OK;
1725 }
1726
1727 /*
1728 *----------------------------------------------------------------------
1729 *
1730 * CheckRequirement --
1731 *
1732 * This function checks to see whether a requirement has valid syntax.
1733 *
1734 * Results:
1735 * If string is a properly formed requirement then TCL_OK is returned.
1736 * Otherwise TCL_ERROR is returned and an error message is left in the
1737 * interp's result.
1738 *
1739 * Side effects:
1740 * None.
1741 *
1742 *----------------------------------------------------------------------
1743 */
1744
1745 static int
1746 CheckRequirement(
1747 Tcl_Interp *interp, /* Used for error reporting. */
1748 const char *string) /* Supposedly a requirement. */
1749 {
1750 /*
1751 * Syntax of requirement = version
1752 * = version-version
1753 * = version-
1754 */
1755
1756 char *dash = NULL, *buf;
1757
1758 dash = strchr(string, '-');
1759 if (dash == NULL) {
1760 /*
1761 * No dash found, has to be a simple version.
1762 */
1763
1764 return CheckVersionAndConvert(interp, string, NULL, NULL);
1765 }
1766
1767 if (strchr(dash+1, '-') != NULL) {
1768 /*
1769 * More dashes found after the first. This is wrong.
1770 */
1771
1772 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
1773 "expected versionMin-versionMax but got \"%s\"", string));
1774 Tcl_SetErrorCode(interp, "TCL", "VALUE", "VERSIONRANGE", (char *)NULL);
1775 return TCL_ERROR;
1776 }
1777
1778 /*
1779 * Exactly one dash is present. Copy the string, split at the location of
1780 * dash and check that both parts are versions. Note that the max part can
1781 * be empty. Also note that the string allocated with strdup() must be
1782 * freed with free() and not ckfree().
1783 */
1784
1785 DupString(buf, string);
1786 dash = buf + (dash - string);
1787 *dash = '\0'; /* buf now <=> min part */
1788 dash++; /* dash now <=> max part */
1789
1790 if ((CheckVersionAndConvert(interp, buf, NULL, NULL) != TCL_OK) ||
1791 ((*dash != '\0') &&
1792 (CheckVersionAndConvert(interp, dash, NULL, NULL) != TCL_OK))) {
1793 ckfree(buf);
1794 return TCL_ERROR;
1795 }
1796
1797 ckfree(buf);
1798 return TCL_OK;
1799 }
1800
1801 /*
1802 *----------------------------------------------------------------------
1803 *
1804 * AddRequirementsToResult --
1805 *
1806 * This function accumulates requirements in the interpreter result.
1807 *
1808 * Results:
1809 * None.
1810 *
1811 * Side effects:
1812 * The interpreter result is extended.
1813 *
1814 *----------------------------------------------------------------------
1815 */
1816
1817 static void
1818 AddRequirementsToResult(
1819 Tcl_Interp *interp,
1820 int reqc, /* Requirements constraining the desired
1821 * version. */
1822 Tcl_Obj *const reqv[]) /* 0 means to use the latest version
1823 * available. */
1824 {
1825 Tcl_Obj *result = Tcl_GetObjResult(interp);
1826 int i, length;
1827
1828 for (i = 0; i < reqc; i++) {
1829 const char *v = TclGetStringFromObj(reqv[i], &length);
1830
1831 if ((length & 0x1) && (v[length/2] == '-')
1832 && (strncmp(v, v+((length+1)/2), length/2) == 0)) {
1833 Tcl_AppendPrintfToObj(result, " exactly %s", v+((length+1)/2));
1834 } else {
1835 Tcl_AppendPrintfToObj(result, " %s", v);
1836 }
1837 }
1838 }
1839
1840 /*
1841 *----------------------------------------------------------------------
1842 *
1843 * AddRequirementsToDString --
1844 *
1845 * This function accumulates requirements in a DString.
1846 *
1847 * Results:
1848 * None.
1849 *
1850 * Side effects:
1851 * The DString argument is extended.
1852 *
1853 *----------------------------------------------------------------------
1854 */
1855
1856 static void
1857 AddRequirementsToDString(
1858 Tcl_DString *dsPtr,
1859 int reqc, /* Requirements constraining the desired
1860 * version. */
1861 Tcl_Obj *const reqv[]) /* 0 means to use the latest version
1862 * available. */
1863 {
1864 int i;
1865
1866 if (reqc > 0) {
1867 for (i = 0; i < reqc; i++) {
1868 TclDStringAppendLiteral(dsPtr, " ");
1869 TclDStringAppendObj(dsPtr, reqv[i]);
1870 }
1871 } else {
1872 TclDStringAppendLiteral(dsPtr, " 0-");
1873 }
1874 }
1875
1876 /*
1877 *----------------------------------------------------------------------
1878 *
1879 * SomeRequirementSatisfied --
1880 *
1881 * This function checks to see whether a version satisfies at least one
1882 * of a set of requirements.
1883 *
1884 * Results:
1885 * If the requirements are satisfied 1 is returned. Otherwise 0 is
1886 * returned. The function assumes that all pieces have valid syntax. And
1887 * is allowed to make that assumption.
1888 *
1889 * Side effects:
1890 * None.
1891 *
1892 *----------------------------------------------------------------------
1893 */
1894
1895 static int
1896 SomeRequirementSatisfied(
1897 char *availVersionI, /* Candidate version to check against the
1898 * requirements. */
1899 int reqc, /* Requirements constraining the desired
1900 * version. */
1901 Tcl_Obj *const reqv[]) /* 0 means to use the latest version
1902 * available. */
1903 {
1904 int i;
1905
1906 for (i = 0; i < reqc; i++) {
1907 if (RequirementSatisfied(availVersionI, TclGetString(reqv[i]))) {
1908 return 1;
1909 }
1910 }
1911 return 0;
1912 }
1913
1914 /*
1915 *----------------------------------------------------------------------
1916 *
1917 * RequirementSatisfied --
1918 *
1919 * This function checks to see whether a version satisfies a requirement.
1920 *
1921 * Results:
1922 * If the requirement is satisfied 1 is returned. Otherwise 0 is
1923 * returned. The function assumes that all pieces have valid syntax, and
1924 * is allowed to make that assumption.
1925 *
1926 * Side effects:
1927 * None.
1928 *
1929 *----------------------------------------------------------------------
1930 */
1931
1932 static int
1933 RequirementSatisfied(
1934 char *havei, /* Version string, of candidate package we
1935 * have. */
1936 const char *req) /* Requirement string the candidate has to
1937 * satisfy. */
1938 {
1939 /*
1940 * The have candidate is already in internal rep.
1941 */
1942
1943 int satisfied, res;
1944 char *dash = NULL, *buf, *min, *max;
1945
1946 dash = strchr(req, '-');
1947 if (dash == NULL) {
1948 /*
1949 * No dash found, is a simple version, fallback to regular check. The
1950 * 'CheckVersionAndConvert' cannot fail. We pad the requirement with
1951 * 'a0', i.e '-2' before doing the comparison to properly accept
1952 * unstables as well.
1953 */
1954
1955 char *reqi = NULL;
1956 int thisIsMajor;
1957
1958 CheckVersionAndConvert(NULL, req, &reqi, NULL);
1959 strcat(reqi, " -2");
1960 res = CompareVersions(havei, reqi, &thisIsMajor);
1961 satisfied = (res == 0) || ((res == 1) && !thisIsMajor);
1962 ckfree(reqi);
1963 return satisfied;
1964 }
1965
1966 /*
1967 * Exactly one dash is present (Assumption of valid syntax). Copy the req,
1968 * split at the location of dash and check that both parts are versions.
1969 * Note that the max part can be empty.
1970 */
1971
1972 DupString(buf, req);
1973 dash = buf + (dash - req);
1974 *dash = '\0'; /* buf now <=> min part */
1975 dash++; /* dash now <=> max part */
1976
1977 if (*dash == '\0') {
1978 /*
1979 * We have a min, but no max. For the comparison we generate the
1980 * internal rep, padded with 'a0' i.e. '-2'.
1981 */
1982
1983 CheckVersionAndConvert(NULL, buf, &min, NULL);
1984 strcat(min, " -2");
1985 satisfied = (CompareVersions(havei, min, NULL) >= 0);
1986 ckfree(min);
1987 ckfree(buf);
1988 return satisfied;
1989 }
1990
1991 /*
1992 * We have both min and max, and generate their internal reps. When
1993 * identical we compare as is, otherwise we pad with 'a0' to over the range
1994 * a bit.
1995 */
1996
1997 CheckVersionAndConvert(NULL, buf, &min, NULL);
1998 CheckVersionAndConvert(NULL, dash, &max, NULL);
1999
2000 if (CompareVersions(min, max, NULL) == 0) {
2001 satisfied = (CompareVersions(min, havei, NULL) == 0);
2002 } else {
2003 strcat(min, " -2");
2004 strcat(max, " -2");
2005 satisfied = ((CompareVersions(min, havei, NULL) <= 0) &&
2006 (CompareVersions(havei, max, NULL) < 0));
2007 }
2008
2009 ckfree(min);
2010 ckfree(max);
2011 ckfree(buf);
2012 return satisfied;
2013 }
2014
2015 /*
2016 *----------------------------------------------------------------------
2017 *
2018 * Tcl_PkgInitStubsCheck --
2019 *
2020 * This is a replacement routine for Tcl_InitStubs() that is called
2021 * from code where -DUSE_TCL_STUBS has not been enabled.
2022 *
2023 * Results:
2024 * Returns the version of a conforming stubs table, or NULL, if
2025 * the table version doesn't satisfy the requested requirements,
2026 * according to historical practice.
2027 *
2028 * Side effects:
2029 * None.
2030 *
2031 *----------------------------------------------------------------------
2032 */
2033
2034 const char *
2035 Tcl_PkgInitStubsCheck(
2036 Tcl_Interp *interp,
2037 const char * version,
2038 int exact)
2039 {
2040 const char *actualVersion = Tcl_PkgPresent(interp, "Tcl", version, 0);
2041
2042 if (exact && actualVersion) {
2043 const char *p = version;
2044 int count = 0;
2045
2046 while (*p) {
2047 count += !isdigit(UCHAR(*p++));
2048 }
2049 if (count == 1) {
2050 if (0 != strncmp(version, actualVersion, strlen(version))) {
2051 /* Construct error message */
2052 Tcl_PkgPresent(interp, "Tcl", version, 1);
2053 return NULL;
2054 }
2055 } else {
2056 return Tcl_PkgPresent(interp, "Tcl", version, 1);
2057 }
2058 }
2059 return actualVersion;
2060 }
2061 /*
2062 * Local Variables:
2063 * mode: c
2064 * c-basic-offset: 4
2065 * fill-column: 78
2066 * End:
2067 */