/*
* tclLoad.c --
*
* This file provides the generic portion (those that are the same
* on all platforms) of Tcl's dynamic loading facilities.
*
* Copyright (c) 1995 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* SCCS: @(#) tclLoad.c 1.15 96/10/12 17:05:58
*/
#include "tclInt.h"
/*
* The following structure describes a package that has been loaded
* either dynamically (with the "load" command) or statically (as
* indicated by a call to Tcl_PackageLoaded). All such packages
* are linked together into a single list for the process. Packages
* are never unloaded, so these structures are never freed.
*/
typedef struct LoadedPackage {
* package was loaded. An empty string
* means the package is loaded statically.
* Malloc-ed. */
* properly capitalized (first letter UC,
* others LC), no "_", as in "Net".
* Malloc-ed. */
/* Initialization procedure to call to
* incorporate this package into a trusted
* interpreter. */
/* Initialization procedure to call to
* incorporate this package into a safe
* interpreter (one that will execute
* untrusted scripts). NULL means the
* package can't be used in unsafe
* interpreters. */
/* Next in list of all packages loaded into
* this application process. NULL means
* end of list. */
/* First in list of all packages loaded into
* this process. */
/*
* The following structure represents a particular package that has
* been incorporated into a particular interpreter (by calling its
* initialization procedure). There is a list of these structures for
* each interpreter, with an AssocData value (key "load") for the
* interpreter that points to the first package (if any).
*/
typedef struct InterpPackage {
* package. */
/* Next package in this interpreter, or
* NULL for end of list. */
/*
* Prototypes for procedures that are private to this file:
*/
Tcl_Interp *interp));
/*
*----------------------------------------------------------------------
*
* Tcl_LoadCmd --
*
* This procedure is invoked to process the "load" Tcl command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*----------------------------------------------------------------------
*/
int
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
" fileName ?packageName? ?interp?\"", (char *) NULL);
return TCL_ERROR;
}
if (fullFileName == NULL) {
return TCL_ERROR;
}
gotPkgName = 1;
} else {
gotPkgName = 0;
}
if ((fullFileName[0] == 0) && !gotPkgName) {
goto done;
}
/*
* Figure out which interpreter we're going to load the package into.
*/
if (argc == 4) {
return TCL_ERROR;
}
}
/*
* Scan through the packages that are currently loaded to see if the
* package we want is already loaded. We'll use a loaded package if
* it meets any of the following conditions:
* - Its name and file match the once we're looking for.
* - Its file matches, and we weren't given a name.
* - Its name matches, the file name was specified as empty, and there
* is only no statically loaded package with the same name.
*/
defaultPtr = NULL;
if (!gotPkgName) {
namesMatch = 0;
} else {
namesMatch = 1;
namesMatch = 0;
break;
}
if (*p1 == 0) {
break;
}
}
}
break;
}
if (namesMatch && (fullFileName[0] == 0)) {
defaultPtr = pkgPtr;
}
/*
* Can't have two different packages loaded from the same
* file.
*/
"\" is already loaded for package \"",
goto done;
}
}
pkgPtr = defaultPtr;
}
/*
* Scan through the list of packages already loaded in the target
* interpreter. If the package we want is already loaded there,
* then there's nothing for us to to.
*/
(Tcl_InterpDeleteProc **) NULL);
goto done;
}
}
}
/*
* The desired file isn't currently loaded, so load it. It's an
* error if the desired package is a static one.
*/
if (fullFileName[0] == 0) {
"\" isn't loaded statically", (char *) NULL);
goto done;
}
/*
* Figure out the module name if it wasn't provided explicitly.
*/
if (gotPkgName) {
} else {
int pargc;
/*
* The platform-specific code couldn't figure out the
* module name. Make a guess by taking the last element
* of the file name, stripping off any leading "lib",
* and then using all of the alphabetic and underline
* characters that follow that.
*/
pkgGuess += 3;
}
/* Empty loop body. */
}
if (p == pkgGuess) {
"couldn't figure out package name for ",
fullFileName, (char *) NULL);
goto done;
}
}
}
/*
* Fix the capitalization in the package name so that the first
* character is in caps but the others are all lower-case.
*/
p = Tcl_DStringValue(&pkgName);
c = UCHAR(*p);
if (c != 0) {
if (islower(c)) {
*p = (char) toupper(c);
}
p++;
while (1) {
c = UCHAR(*p);
if (c == 0) {
break;
}
if (isupper(c)) {
*p = (char) tolower(c);
}
p++;
}
}
/*
* Compute the names of the two initialization procedures,
* based on the package name.
*/
/*
* Call platform-specific code to load the package and find the
* two initialization procedures.
*/
goto done;
}
goto done;
}
/*
* Create a new record to describe this package.
*/
if (firstPackagePtr == NULL) {
}
}
/*
* Invoke the package's initialization procedure (either the
* normal one or the safe one, depending on whether or not the
* interpreter is safe).
*/
if (Tcl_IsSafe(target)) {
} else {
"can't use package in a safe interpreter: ",
(char *) NULL);
goto done;
}
} else {
}
/*
* An error occurred, so transfer error information from the
* destination interpreter back to our interpreter. Must clear
* interp's result before calling Tcl_AddErrorInfo, since
* Tcl_AddErrorInfo will store the interp's result in errorInfo
* before appending target's $errorInfo; we've already got
* everything we need in target's $errorInfo.
*/
}
/*
* Record the fact that the package has been loaded in the
* target interpreter.
*/
/*
* Refetch ipFirstPtr: loading the package may have introduced
* additional static packages at the head of the linked list!
*/
(Tcl_InterpDeleteProc **) NULL);
(ClientData) ipPtr);
}
done:
return code;
}
/*
*----------------------------------------------------------------------
*
* Tcl_StaticPackage --
*
* This procedure is invoked to indicate that a particular
* package has been linked statically with an application.
*
* Results:
* None.
*
* Side effects:
* Once this procedure completes, the package becomes loadable
* via the "load" command with an empty file name.
*
*----------------------------------------------------------------------
*/
void
* package has already been loaded
* into the given interpreter by
* calling the appropriate init proc. */
char *pkgName; /* Name of package (must be properly
* capitalized: first letter upper
* case, others lower case). */
* this package into a trusted
* interpreter. */
* this package into a safe interpreter
* (one that will execute untrusted
* scripts). NULL means the package
* can't be used in safe
* interpreters. */
{
/*
* Check to see if someone else has already reported this package as
* statically loaded. If this call is redundant then just return.
*/
return;
}
}
if (firstPackagePtr == NULL) {
}
(Tcl_InterpDeleteProc **) NULL);
(ClientData) ipPtr);
}
}
/*
*----------------------------------------------------------------------
*
* TclGetLoadedPackages --
*
* This procedure returns information about all of the files
* that are loaded (either in a particular intepreter, or
* for all interpreters).
*
* Results:
* The return value is a standard Tcl completion code. If
* successful, a list of lists is placed in interp->result.
* Each sublist corresponds to one loaded file; its first
* element is the name of the file (or an empty string for
* something that's statically loaded) and the second element
* is the name of the package in that file.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
* information or error message. */
char *targetName; /* Name of target interpreter or NULL.
* If NULL, return info about all interps;
* otherwise, just return info about this
* interpreter. */
{
char *prefix;
if (targetName == NULL) {
/*
* Return information about all of the available packages.
*/
prefix = "{";
prefix = " {";
}
return TCL_OK;
}
/*
* Return information about only the packages that are loaded in
* a given interpreter.
*/
return TCL_ERROR;
}
(Tcl_InterpDeleteProc **) NULL);
prefix = "{";
prefix = " {";
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* LoadCleanupProc --
*
* This procedure is called to delete all of the InterpPackage
* structures for an interpreter when the interpreter is deleted.
* It gets invoked via the Tcl AssocData mechanism.
*
* Results:
* None.
*
* Side effects:
* Storage for all of the InterpPackage procedures for interp
* get deleted.
*
*----------------------------------------------------------------------
*/
static void
* for interp. */
{
}
}
/*
*----------------------------------------------------------------------
*
* LoadExitProc --
*
* This procedure is invoked just before the application exits.
* It frees all of the LoadedPackage structures.
*
* Results:
* None.
*
* Side effects:
* Memory is freed.
*
*----------------------------------------------------------------------
*/
static void
{
while (firstPackagePtr != NULL) {
}
}