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