os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/unix/tclLoadDld.c
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/unix/tclLoadDld.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,201 @@
1.4 +/*
1.5 + * tclLoadDld.c --
1.6 + *
1.7 + * This procedure provides a version of the TclLoadFile that
1.8 + * works with the "dld_link" and "dld_get_func" library procedures
1.9 + * for dynamic loading. It has been tested on Linux 1.1.95 and
1.10 + * dld-3.2.7. This file probably isn't needed anymore, since it
1.11 + * makes more sense to use "dl_open" etc.
1.12 + *
1.13 + * Copyright (c) 1995-1997 Sun Microsystems, Inc.
1.14 + *
1.15 + * See the file "license.terms" for information on usage and redistribution
1.16 + * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
1.17 + *
1.18 + * RCS: @(#) $Id: tclLoadDld.c,v 1.12 2002/10/10 12:25:53 vincentdarley Exp $
1.19 + */
1.20 +
1.21 +#include "tclInt.h"
1.22 +#include "dld.h"
1.23 +
1.24 +/*
1.25 + * In some systems, like SunOS 4.1.3, the RTLD_NOW flag isn't defined
1.26 + * and this argument to dlopen must always be 1.
1.27 + */
1.28 +
1.29 +#ifndef RTLD_NOW
1.30 +# define RTLD_NOW 1
1.31 +#endif
1.32 +
1.33 +/*
1.34 + *----------------------------------------------------------------------
1.35 + *
1.36 + * TclpDlopen --
1.37 + *
1.38 + * Dynamically loads a binary code file into memory and returns
1.39 + * a handle to the new code.
1.40 + *
1.41 + * Results:
1.42 + * A standard Tcl completion code. If an error occurs, an error
1.43 + * message is left in the interp's result.
1.44 + *
1.45 + * Side effects:
1.46 + * New code suddenly appears in memory.
1.47 + *
1.48 + *----------------------------------------------------------------------
1.49 + */
1.50 +
1.51 +int
1.52 +TclpDlopen(interp, pathPtr, loadHandle, unloadProcPtr)
1.53 + Tcl_Interp *interp; /* Used for error reporting. */
1.54 + Tcl_Obj *pathPtr; /* Name of the file containing the desired
1.55 + * code (UTF-8). */
1.56 + Tcl_LoadHandle *loadHandle; /* Filled with token for dynamically loaded
1.57 + * file which will be passed back to
1.58 + * (*unloadProcPtr)() to unload the file. */
1.59 + Tcl_FSUnloadFileProc **unloadProcPtr;
1.60 + /* Filled with address of Tcl_FSUnloadFileProc
1.61 + * function which should be used for
1.62 + * this file. */
1.63 +{
1.64 + static int firstTime = 1;
1.65 + int returnCode;
1.66 + char *fileName;
1.67 + CONST char *native;
1.68 +
1.69 + /*
1.70 + * The dld package needs to know the pathname to the tcl binary.
1.71 + * If that's not known, return an error.
1.72 + */
1.73 +
1.74 + if (firstTime) {
1.75 + if (tclExecutableName == NULL) {
1.76 + Tcl_SetResult(interp,
1.77 + "don't know name of application binary file, so can't initialize dynamic loader",
1.78 + TCL_STATIC);
1.79 + return TCL_ERROR;
1.80 + }
1.81 + returnCode = dld_init(tclExecutableName);
1.82 + if (returnCode != 0) {
1.83 + Tcl_AppendResult(interp,
1.84 + "initialization failed for dynamic loader: ",
1.85 + dld_strerror(returnCode), (char *) NULL);
1.86 + return TCL_ERROR;
1.87 + }
1.88 + firstTime = 0;
1.89 + }
1.90 +
1.91 + fileName = Tcl_GetString(pathPtr);
1.92 +
1.93 + /*
1.94 + * First try the full path the user gave us. This is particularly
1.95 + * important if the cwd is inside a vfs, and we are trying to load
1.96 + * using a relative path.
1.97 + */
1.98 + native = Tcl_FSGetNativePath(pathPtr);
1.99 + returnCode = dld_link(native);
1.100 +
1.101 + if (returnCode != 0) {
1.102 + Tcl_DString ds;
1.103 + native = Tcl_UtfToExternalDString(NULL, fileName, -1, &ds);
1.104 + returnCode = dld_link(native);
1.105 + Tcl_DStringFree(&ds);
1.106 + }
1.107 +
1.108 + if (returnCode != 0) {
1.109 + Tcl_AppendResult(interp, "couldn't load file \"",
1.110 + fileName, "\": ",
1.111 + dld_strerror(returnCode), (char *) NULL);
1.112 + return TCL_ERROR;
1.113 + }
1.114 + *loadHandle = (Tcl_LoadHandle) strcpy(
1.115 + (char *) ckalloc((unsigned) (strlen(fileName) + 1)), fileName);
1.116 + *unloadProcPtr = &TclpUnloadFile;
1.117 + return TCL_OK;
1.118 +}
1.119 +
1.120 +/*
1.121 + *----------------------------------------------------------------------
1.122 + *
1.123 + * TclpFindSymbol --
1.124 + *
1.125 + * Looks up a symbol, by name, through a handle associated with
1.126 + * a previously loaded piece of code (shared library).
1.127 + *
1.128 + * Results:
1.129 + * Returns a pointer to the function associated with 'symbol' if
1.130 + * it is found. Otherwise returns NULL and may leave an error
1.131 + * message in the interp's result.
1.132 + *
1.133 + *----------------------------------------------------------------------
1.134 + */
1.135 +Tcl_PackageInitProc*
1.136 +TclpFindSymbol(interp, loadHandle, symbol)
1.137 + Tcl_Interp *interp;
1.138 + Tcl_LoadHandle loadHandle;
1.139 + CONST char *symbol;
1.140 +{
1.141 + return (Tcl_PackageInitProc *) dld_get_func(symbol);
1.142 +}
1.143 +
1.144 +/*
1.145 + *----------------------------------------------------------------------
1.146 + *
1.147 + * TclpUnloadFile --
1.148 + *
1.149 + * Unloads a dynamically loaded binary code file from memory.
1.150 + * Code pointers in the formerly loaded file are no longer valid
1.151 + * after calling this function.
1.152 + *
1.153 + * Results:
1.154 + * None.
1.155 + *
1.156 + * Side effects:
1.157 + * Code removed from memory.
1.158 + *
1.159 + *----------------------------------------------------------------------
1.160 + */
1.161 +
1.162 +void
1.163 +TclpUnloadFile(loadHandle)
1.164 + Tcl_LoadHandle loadHandle; /* loadHandle returned by a previous call
1.165 + * to TclpDlopen(). The loadHandle is
1.166 + * a token that represents the loaded
1.167 + * file. */
1.168 +{
1.169 + char *fileName;
1.170 +
1.171 + handle = (char *) loadHandle;
1.172 + dld_unlink_by_file(handle, 0);
1.173 + ckfree(handle);
1.174 +}
1.175 +
1.176 +/*
1.177 + *----------------------------------------------------------------------
1.178 + *
1.179 + * TclGuessPackageName --
1.180 + *
1.181 + * If the "load" command is invoked without providing a package
1.182 + * name, this procedure is invoked to try to figure it out.
1.183 + *
1.184 + * Results:
1.185 + * Always returns 0 to indicate that we couldn't figure out a
1.186 + * package name; generic code will then try to guess the package
1.187 + * from the file name. A return value of 1 would have meant that
1.188 + * we figured out the package name and put it in bufPtr.
1.189 + *
1.190 + * Side effects:
1.191 + * None.
1.192 + *
1.193 + *----------------------------------------------------------------------
1.194 + */
1.195 +
1.196 +int
1.197 +TclGuessPackageName(fileName, bufPtr)
1.198 + CONST char *fileName; /* Name of file containing package (already
1.199 + * translated to local form if needed). */
1.200 + Tcl_DString *bufPtr; /* Initialized empty dstring. Append
1.201 + * package name to this if possible. */
1.202 +{
1.203 + return 0;
1.204 +}