os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/unix/tclLoadShl.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/tclLoadShl.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,208 @@
     1.4 +/* 
     1.5 + * tclLoadShl.c --
     1.6 + *
     1.7 + *	This procedure provides a version of the TclLoadFile that works
     1.8 + *	with the "shl_load" and "shl_findsym" library procedures for
     1.9 + *	dynamic loading (e.g. for HP machines).
    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: tclLoadShl.c,v 1.13.2.1 2005/10/05 04:23:56 hobbs Exp $
    1.17 + */
    1.18 +
    1.19 +#include <dl.h>
    1.20 +
    1.21 +/*
    1.22 + * On some HP machines, dl.h defines EXTERN; remove that definition.
    1.23 + */
    1.24 +
    1.25 +#ifdef EXTERN
    1.26 +#   undef EXTERN
    1.27 +#endif
    1.28 +
    1.29 +#include "tclInt.h"
    1.30 +
    1.31 +/*
    1.32 + *----------------------------------------------------------------------
    1.33 + *
    1.34 + * TclpDlopen --
    1.35 + *
    1.36 + *	Dynamically loads a binary code file into memory and returns
    1.37 + *	a handle to the new code.
    1.38 + *
    1.39 + * Results:
    1.40 + *	A standard Tcl completion code.  If an error occurs, an error
    1.41 + *	message is left in the interp's result.
    1.42 + *
    1.43 + * Side effects:
    1.44 + *	New code suddenly appears in memory.
    1.45 + *
    1.46 + *----------------------------------------------------------------------
    1.47 + */
    1.48 +
    1.49 +int
    1.50 +TclpDlopen(interp, pathPtr, loadHandle, unloadProcPtr)
    1.51 +    Tcl_Interp *interp;		/* Used for error reporting. */
    1.52 +    Tcl_Obj *pathPtr;		/* Name of the file containing the desired
    1.53 +				 * code (UTF-8). */
    1.54 +    Tcl_LoadHandle *loadHandle;	/* Filled with token for dynamically loaded
    1.55 +				 * file which will be passed back to 
    1.56 +				 * (*unloadProcPtr)() to unload the file. */
    1.57 +    Tcl_FSUnloadFileProc **unloadProcPtr;	
    1.58 +				/* Filled with address of Tcl_FSUnloadFileProc
    1.59 +				 * function which should be used for
    1.60 +				 * this file. */
    1.61 +{
    1.62 +    shl_t handle;
    1.63 +    CONST char *native;
    1.64 +    char *fileName = Tcl_GetString(pathPtr);
    1.65 +
    1.66 +    /*
    1.67 +     * The flags below used to be BIND_IMMEDIATE; they were changed at
    1.68 +     * the suggestion of Wolfgang Kechel (wolfgang@prs.de): "This
    1.69 +     * enables verbosity for missing symbols when loading a shared lib
    1.70 +     * and allows to load libtk8.0.sl into tclsh8.0 without problems.
    1.71 +     * In general, this delays resolving symbols until they are actually
    1.72 +     * needed.  Shared libs do no longer need all libraries linked in
    1.73 +     * when they are build."
    1.74 +     */
    1.75 +
    1.76 +
    1.77 +    /*
    1.78 +     * First try the full path the user gave us.  This is particularly
    1.79 +     * important if the cwd is inside a vfs, and we are trying to load
    1.80 +     * using a relative path.
    1.81 +     */
    1.82 +    native = Tcl_FSGetNativePath(pathPtr);
    1.83 +    handle = shl_load(native, BIND_DEFERRED|BIND_VERBOSE, 0L);
    1.84 +
    1.85 +    if (handle == NULL) {
    1.86 +	/*
    1.87 +	 * Let the OS loader examine the binary search path for
    1.88 +	 * whatever string the user gave us which hopefully refers
    1.89 +	 * to a file on the binary path
    1.90 +	 */
    1.91 +	Tcl_DString ds;
    1.92 +	native = Tcl_UtfToExternalDString(NULL, fileName, -1, &ds);
    1.93 +	handle = shl_load(native,
    1.94 +			  BIND_DEFERRED|BIND_VERBOSE|DYNAMIC_PATH, 0L);
    1.95 +	Tcl_DStringFree(&ds);
    1.96 +    }
    1.97 +
    1.98 +    if (handle == NULL) {
    1.99 +	Tcl_AppendResult(interp, "couldn't load file \"", fileName,
   1.100 +		"\": ", Tcl_PosixError(interp), (char *) NULL);
   1.101 +	return TCL_ERROR;
   1.102 +    }
   1.103 +    *loadHandle = (Tcl_LoadHandle) handle;
   1.104 +    *unloadProcPtr = &TclpUnloadFile;
   1.105 +    return TCL_OK;
   1.106 +}
   1.107 +
   1.108 +/*
   1.109 + *----------------------------------------------------------------------
   1.110 + *
   1.111 + * TclpFindSymbol --
   1.112 + *
   1.113 + *	Looks up a symbol, by name, through a handle associated with
   1.114 + *	a previously loaded piece of code (shared library).
   1.115 + *
   1.116 + * Results:
   1.117 + *	Returns a pointer to the function associated with 'symbol' if
   1.118 + *	it is found.  Otherwise returns NULL and may leave an error
   1.119 + *	message in the interp's result.
   1.120 + *
   1.121 + *----------------------------------------------------------------------
   1.122 + */
   1.123 +Tcl_PackageInitProc*
   1.124 +TclpFindSymbol(interp, loadHandle, symbol) 
   1.125 +    Tcl_Interp *interp;
   1.126 +    Tcl_LoadHandle loadHandle;
   1.127 +    CONST char *symbol;
   1.128 +{
   1.129 +    Tcl_DString newName;
   1.130 +    Tcl_PackageInitProc *proc=NULL;
   1.131 +    shl_t handle = (shl_t)loadHandle;
   1.132 +    /*
   1.133 +     * Some versions of the HP system software still use "_" at the
   1.134 +     * beginning of exported symbols while others don't;  try both
   1.135 +     * forms of each name.
   1.136 +     */
   1.137 +
   1.138 +    if (shl_findsym(&handle, symbol, (short) TYPE_PROCEDURE, (void *) &proc)
   1.139 +	    != 0) {
   1.140 +	Tcl_DStringInit(&newName);
   1.141 +	Tcl_DStringAppend(&newName, "_", 1);
   1.142 +	Tcl_DStringAppend(&newName, symbol, -1);
   1.143 +	if (shl_findsym(&handle, Tcl_DStringValue(&newName),
   1.144 +		(short) TYPE_PROCEDURE, (void *) &proc) != 0) {
   1.145 +	    proc = NULL;
   1.146 +	}
   1.147 +	Tcl_DStringFree(&newName);
   1.148 +    }
   1.149 +    return proc;
   1.150 +}
   1.151 +
   1.152 +/*
   1.153 + *----------------------------------------------------------------------
   1.154 + *
   1.155 + * TclpUnloadFile --
   1.156 + *
   1.157 + *	Unloads a dynamically loaded binary code file from memory.
   1.158 + *	Code pointers in the formerly loaded file are no longer valid
   1.159 + *	after calling this function.
   1.160 + *
   1.161 + * Results:
   1.162 + *	None.
   1.163 + *
   1.164 + * Side effects:
   1.165 + *	Code removed from memory.
   1.166 + *
   1.167 + *----------------------------------------------------------------------
   1.168 + */
   1.169 +
   1.170 +void
   1.171 +TclpUnloadFile(loadHandle)
   1.172 +    Tcl_LoadHandle loadHandle;	/* loadHandle returned by a previous call
   1.173 +				 * to TclpDlopen().  The loadHandle is 
   1.174 +				 * a token that represents the loaded 
   1.175 +				 * file. */
   1.176 +{
   1.177 +    shl_t handle;
   1.178 +
   1.179 +    handle = (shl_t) loadHandle;
   1.180 +    shl_unload(handle);
   1.181 +}
   1.182 +
   1.183 +/*
   1.184 + *----------------------------------------------------------------------
   1.185 + *
   1.186 + * TclGuessPackageName --
   1.187 + *
   1.188 + *	If the "load" command is invoked without providing a package
   1.189 + *	name, this procedure is invoked to try to figure it out.
   1.190 + *
   1.191 + * Results:
   1.192 + *	Always returns 0 to indicate that we couldn't figure out a
   1.193 + *	package name;  generic code will then try to guess the package
   1.194 + *	from the file name.  A return value of 1 would have meant that
   1.195 + *	we figured out the package name and put it in bufPtr.
   1.196 + *
   1.197 + * Side effects:
   1.198 + *	None.
   1.199 + *
   1.200 + *----------------------------------------------------------------------
   1.201 + */
   1.202 +
   1.203 +int
   1.204 +TclGuessPackageName(fileName, bufPtr)
   1.205 +    CONST char *fileName;	/* Name of file containing package (already
   1.206 +				 * translated to local form if needed). */
   1.207 +    Tcl_DString *bufPtr;	/* Initialized empty dstring.  Append
   1.208 +				 * package name to this if possible. */
   1.209 +{
   1.210 +    return 0;
   1.211 +}