os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/unix/tclLoadDl.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/unix/tclLoadDl.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,216 @@
     1.4 +/* 
     1.5 + * tclLoadDl.c --
     1.6 + *
     1.7 + *	This procedure provides a version of the TclLoadFile that
     1.8 + *	works with the "dlopen" and "dlsym" library procedures for
     1.9 + *	dynamic loading.
    1.10 + *
    1.11 + * Copyright (c) 1995-1997 Sun Microsystems, Inc.
    1.12 + *
    1.13 + * See the file "license.terms" for information on usage and redistribution
    1.14 + * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
    1.15 + *
    1.16 + * RCS: @(#) $Id: tclLoadDl.c,v 1.13.2.1 2006/06/13 22:54:01 dkf Exp $
    1.17 + */
    1.18 +
    1.19 +#include "tclInt.h"
    1.20 +#ifdef NO_DLFCN_H
    1.21 +#   include "../compat/dlfcn.h"
    1.22 +#else
    1.23 +#   include <dlfcn.h>
    1.24 +#endif
    1.25 +
    1.26 +/*
    1.27 + * In some systems, like SunOS 4.1.3, the RTLD_NOW flag isn't defined
    1.28 + * and this argument to dlopen must always be 1.  The RTLD_GLOBAL
    1.29 + * flag is needed on some systems (e.g. SCO and UnixWare) but doesn't
    1.30 + * exist on others;  if it doesn't exist, set it to 0 so it has no effect.
    1.31 + */
    1.32 +
    1.33 +#ifndef RTLD_NOW
    1.34 +#   define RTLD_NOW 1
    1.35 +#endif
    1.36 +
    1.37 +#ifndef RTLD_GLOBAL
    1.38 +#   define RTLD_GLOBAL 0
    1.39 +#endif
    1.40 +
    1.41 +/*
    1.42 + *---------------------------------------------------------------------------
    1.43 + *
    1.44 + * TclpDlopen --
    1.45 + *
    1.46 + *	Dynamically loads a binary code file into memory and returns
    1.47 + *	a handle to the new code.
    1.48 + *
    1.49 + * Results:
    1.50 + *	A standard Tcl completion code.  If an error occurs, an error
    1.51 + *	message is left in the interp's result. 
    1.52 + *
    1.53 + * Side effects:
    1.54 + *	New code suddenly appears in memory.
    1.55 + *
    1.56 + *---------------------------------------------------------------------------
    1.57 + */
    1.58 +
    1.59 +int
    1.60 +TclpDlopen(interp, pathPtr, loadHandle, unloadProcPtr)
    1.61 +    Tcl_Interp *interp;		/* Used for error reporting. */
    1.62 +    Tcl_Obj *pathPtr;		/* Name of the file containing the desired
    1.63 +				 * code (UTF-8). */
    1.64 +    Tcl_LoadHandle *loadHandle;	/* Filled with token for dynamically loaded
    1.65 +				 * file which will be passed back to 
    1.66 +				 * (*unloadProcPtr)() to unload the file. */
    1.67 +    Tcl_FSUnloadFileProc **unloadProcPtr;	
    1.68 +				/* Filled with address of Tcl_FSUnloadFileProc
    1.69 +				 * function which should be used for
    1.70 +				 * this file. */
    1.71 +{
    1.72 +    VOID *handle;
    1.73 +    CONST char *native;
    1.74 +
    1.75 +    /* 
    1.76 +     * First try the full path the user gave us.  This is particularly
    1.77 +     * important if the cwd is inside a vfs, and we are trying to load
    1.78 +     * using a relative path.
    1.79 +     */
    1.80 +    native = Tcl_FSGetNativePath(pathPtr);
    1.81 +    handle = dlopen(native, RTLD_NOW | RTLD_GLOBAL);
    1.82 +    if (handle == NULL) {
    1.83 +	/* 
    1.84 +	 * Let the OS loader examine the binary search path for
    1.85 +	 * whatever string the user gave us which hopefully refers
    1.86 +	 * to a file on the binary path
    1.87 +	 */
    1.88 +	Tcl_DString ds;
    1.89 +	char *fileName = Tcl_GetString(pathPtr);
    1.90 +	native = Tcl_UtfToExternalDString(NULL, fileName, -1, &ds);
    1.91 +	handle = dlopen(native, RTLD_NOW | RTLD_GLOBAL);
    1.92 +	Tcl_DStringFree(&ds);
    1.93 +    }
    1.94 +    
    1.95 +    if (handle == NULL) {
    1.96 +	/*
    1.97 +	 * Write the string to a variable first to work around a compiler bug
    1.98 +	 * in the Sun Forte 6 compiler. [Bug 1503729]
    1.99 +	 */
   1.100 +
   1.101 +	CONST char *errorStr = dlerror();
   1.102 +
   1.103 +	Tcl_AppendResult(interp, "couldn't load file \"", 
   1.104 +		Tcl_GetString(pathPtr), "\": ", errorStr, (char *) NULL);
   1.105 +	return TCL_ERROR;
   1.106 +    }
   1.107 +
   1.108 +    *unloadProcPtr = &TclpUnloadFile;
   1.109 +    *loadHandle = (Tcl_LoadHandle)handle;
   1.110 +    return TCL_OK;
   1.111 +}
   1.112 +
   1.113 +/*
   1.114 + *----------------------------------------------------------------------
   1.115 + *
   1.116 + * TclpFindSymbol --
   1.117 + *
   1.118 + *	Looks up a symbol, by name, through a handle associated with
   1.119 + *	a previously loaded piece of code (shared library).
   1.120 + *
   1.121 + * Results:
   1.122 + *	Returns a pointer to the function associated with 'symbol' if
   1.123 + *	it is found.  Otherwise returns NULL and may leave an error
   1.124 + *	message in the interp's result.
   1.125 + *
   1.126 + *----------------------------------------------------------------------
   1.127 + */
   1.128 +Tcl_PackageInitProc*
   1.129 +TclpFindSymbol(interp, loadHandle, symbol) 
   1.130 +    Tcl_Interp *interp;
   1.131 +    Tcl_LoadHandle loadHandle;
   1.132 +    CONST char *symbol;
   1.133 +{
   1.134 +    CONST char *native;
   1.135 +    Tcl_DString newName, ds;
   1.136 +    VOID *handle = (VOID*)loadHandle;
   1.137 +    Tcl_PackageInitProc *proc;
   1.138 +    /* 
   1.139 +     * Some platforms still add an underscore to the beginning of symbol
   1.140 +     * names.  If we can't find a name without an underscore, try again
   1.141 +     * with the underscore.
   1.142 +     */
   1.143 +
   1.144 +    native = Tcl_UtfToExternalDString(NULL, symbol, -1, &ds);
   1.145 +    proc = (Tcl_PackageInitProc *) dlsym(handle,	/* INTL: Native. */
   1.146 +	    native);	
   1.147 +    if (proc == NULL) {
   1.148 +	Tcl_DStringInit(&newName);
   1.149 +	Tcl_DStringAppend(&newName, "_", 1);
   1.150 +	native = Tcl_DStringAppend(&newName, native, -1);
   1.151 +	proc = (Tcl_PackageInitProc *) dlsym(handle, /* INTL: Native. */
   1.152 +		native);
   1.153 +	Tcl_DStringFree(&newName);
   1.154 +    }
   1.155 +    Tcl_DStringFree(&ds);
   1.156 +
   1.157 +    return proc;
   1.158 +}
   1.159 +
   1.160 +/*
   1.161 + *----------------------------------------------------------------------
   1.162 + *
   1.163 + * TclpUnloadFile --
   1.164 + *
   1.165 + *	Unloads a dynamically loaded binary code file from memory.
   1.166 + *	Code pointers in the formerly loaded file are no longer valid
   1.167 + *	after calling this function.
   1.168 + *
   1.169 + * Results:
   1.170 + *	None.
   1.171 + *
   1.172 + * Side effects:
   1.173 + *	Code removed from memory.
   1.174 + *
   1.175 + *----------------------------------------------------------------------
   1.176 + */
   1.177 +
   1.178 +void
   1.179 +TclpUnloadFile(loadHandle)
   1.180 +    Tcl_LoadHandle loadHandle;	/* loadHandle returned by a previous call
   1.181 +				 * to TclpDlopen().  The loadHandle is 
   1.182 +				 * a token that represents the loaded 
   1.183 +				 * file. */
   1.184 +{
   1.185 +    VOID *handle;
   1.186 +
   1.187 +    handle = (VOID *) loadHandle;
   1.188 +    dlclose(handle);
   1.189 +}
   1.190 +
   1.191 +/*
   1.192 + *----------------------------------------------------------------------
   1.193 + *
   1.194 + * TclGuessPackageName --
   1.195 + *
   1.196 + *	If the "load" command is invoked without providing a package
   1.197 + *	name, this procedure is invoked to try to figure it out.
   1.198 + *
   1.199 + * Results:
   1.200 + *	Always returns 0 to indicate that we couldn't figure out a
   1.201 + *	package name;  generic code will then try to guess the package
   1.202 + *	from the file name.  A return value of 1 would have meant that
   1.203 + *	we figured out the package name and put it in bufPtr.
   1.204 + *
   1.205 + * Side effects:
   1.206 + *	None.
   1.207 + *
   1.208 + *----------------------------------------------------------------------
   1.209 + */
   1.210 +
   1.211 +int
   1.212 +TclGuessPackageName(fileName, bufPtr)
   1.213 +    CONST char *fileName;	/* Name of file containing package (already
   1.214 +				 * translated to local form if needed). */
   1.215 +    Tcl_DString *bufPtr;	/* Initialized empty dstring.  Append
   1.216 +				 * package name to this if possible. */
   1.217 +{
   1.218 +    return 0;
   1.219 +}