os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/win/tclWinLoad.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/win/tclWinLoad.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,230 @@
     1.4 +/* 
     1.5 + * tclWinLoad.c --
     1.6 + *
     1.7 + *	This procedure provides a version of the TclLoadFile that
     1.8 + *	works with the Windows "LoadLibrary" and "GetProcAddress"
     1.9 + *	API for 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: tclWinLoad.c,v 1.15 2002/10/10 12:25:53 vincentdarley Exp $
    1.17 + */
    1.18 +
    1.19 +#include "tclWinInt.h"
    1.20 +
    1.21 +
    1.22 +/*
    1.23 + *----------------------------------------------------------------------
    1.24 + *
    1.25 + * TclpDlopen --
    1.26 + *
    1.27 + *	Dynamically loads a binary code file into memory and returns
    1.28 + *	a handle to the new code.
    1.29 + *
    1.30 + * Results:
    1.31 + *	A standard Tcl completion code.  If an error occurs, an error
    1.32 + *	message is left in the interp's result.
    1.33 + *
    1.34 + * Side effects:
    1.35 + *	New code suddenly appears in memory.
    1.36 + *
    1.37 + *----------------------------------------------------------------------
    1.38 + */
    1.39 +
    1.40 +int
    1.41 +TclpDlopen(interp, pathPtr, loadHandle, unloadProcPtr)
    1.42 +    Tcl_Interp *interp;		/* Used for error reporting. */
    1.43 +    Tcl_Obj *pathPtr;		/* Name of the file containing the desired
    1.44 +				 * code (UTF-8). */
    1.45 +    Tcl_LoadHandle *loadHandle;	/* Filled with token for dynamically loaded
    1.46 +				 * file which will be passed back to 
    1.47 +				 * (*unloadProcPtr)() to unload the file. */
    1.48 +    Tcl_FSUnloadFileProc **unloadProcPtr;	
    1.49 +				/* Filled with address of Tcl_FSUnloadFileProc
    1.50 +				 * function which should be used for
    1.51 +				 * this file. */
    1.52 +{
    1.53 +    HINSTANCE handle;
    1.54 +    CONST TCHAR *nativeName;
    1.55 +
    1.56 +    /* 
    1.57 +     * First try the full path the user gave us.  This is particularly
    1.58 +     * important if the cwd is inside a vfs, and we are trying to load
    1.59 +     * using a relative path.
    1.60 +     */
    1.61 +    nativeName = Tcl_FSGetNativePath(pathPtr);
    1.62 +    handle = (*tclWinProcs->loadLibraryProc)(nativeName);
    1.63 +    if (handle == NULL) {
    1.64 +	/* 
    1.65 +	 * Let the OS loader examine the binary search path for
    1.66 +	 * whatever string the user gave us which hopefully refers
    1.67 +	 * to a file on the binary path
    1.68 +	 */
    1.69 +	Tcl_DString ds;
    1.70 +        char *fileName = Tcl_GetString(pathPtr);
    1.71 +	nativeName = Tcl_WinUtfToTChar(fileName, -1, &ds);
    1.72 +	handle = (*tclWinProcs->loadLibraryProc)(nativeName);
    1.73 +	Tcl_DStringFree(&ds);
    1.74 +    }
    1.75 +
    1.76 +    *loadHandle = (Tcl_LoadHandle) handle;
    1.77 +    
    1.78 +    if (handle == NULL) {
    1.79 +	DWORD lastError = GetLastError();
    1.80 +#if 0
    1.81 +	/*
    1.82 +	 * It would be ideal if the FormatMessage stuff worked better,
    1.83 +	 * but unfortunately it doesn't seem to want to...
    1.84 +	 */
    1.85 +	LPTSTR lpMsgBuf;
    1.86 +	char *buf;
    1.87 +	int size;
    1.88 +	size = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
    1.89 +		FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, lastError, 0,
    1.90 +		(LPTSTR) &lpMsgBuf, 0, NULL);
    1.91 +	buf = (char *) ckalloc((unsigned) TCL_INTEGER_SPACE + size + 1);
    1.92 +	sprintf(buf, "%d %s", lastError, (char *)lpMsgBuf);
    1.93 +#endif
    1.94 +	Tcl_AppendResult(interp, "couldn't load library \"",
    1.95 +			 Tcl_GetString(pathPtr), "\": ", (char *) NULL);
    1.96 +	/*
    1.97 +	 * Check for possible DLL errors.  This doesn't work quite right,
    1.98 +	 * because Windows seems to only return ERROR_MOD_NOT_FOUND for
    1.99 +	 * just about any problem, but it's better than nothing.  It'd be
   1.100 +	 * even better if there was a way to get what DLLs
   1.101 +	 */
   1.102 +	switch (lastError) {
   1.103 +	    case ERROR_MOD_NOT_FOUND:
   1.104 +	    case ERROR_DLL_NOT_FOUND:
   1.105 +		Tcl_AppendResult(interp, "this library or a dependent library",
   1.106 +			" could not be found in library path",
   1.107 +			(char *) NULL);
   1.108 +		break;
   1.109 +	    case ERROR_PROC_NOT_FOUND:
   1.110 +		Tcl_AppendResult(interp, "could not find specified procedure",
   1.111 +			(char *) NULL);
   1.112 +		break;
   1.113 +	    case ERROR_INVALID_DLL:
   1.114 +		Tcl_AppendResult(interp, "this library or a dependent library",
   1.115 +			" is damaged", (char *) NULL);
   1.116 +		break;
   1.117 +	    case ERROR_DLL_INIT_FAILED:
   1.118 +		Tcl_AppendResult(interp, "the library initialization",
   1.119 +			" routine failed", (char *) NULL);
   1.120 +		break;
   1.121 +	    default:
   1.122 +		TclWinConvertError(lastError);
   1.123 +		Tcl_AppendResult(interp, Tcl_PosixError(interp),
   1.124 +			(char *) NULL);
   1.125 +	}
   1.126 +	return TCL_ERROR;
   1.127 +    } else {
   1.128 +	*unloadProcPtr = &TclpUnloadFile;
   1.129 +    }
   1.130 +    return TCL_OK;
   1.131 +}
   1.132 +
   1.133 +/*
   1.134 + *----------------------------------------------------------------------
   1.135 + *
   1.136 + * TclpFindSymbol --
   1.137 + *
   1.138 + *	Looks up a symbol, by name, through a handle associated with
   1.139 + *	a previously loaded piece of code (shared library).
   1.140 + *
   1.141 + * Results:
   1.142 + *	Returns a pointer to the function associated with 'symbol' if
   1.143 + *	it is found.  Otherwise returns NULL and may leave an error
   1.144 + *	message in the interp's result.
   1.145 + *
   1.146 + *----------------------------------------------------------------------
   1.147 + */
   1.148 +Tcl_PackageInitProc*
   1.149 +TclpFindSymbol(interp, loadHandle, symbol) 
   1.150 +    Tcl_Interp *interp;
   1.151 +    Tcl_LoadHandle loadHandle;
   1.152 +    CONST char *symbol;
   1.153 +{
   1.154 +    Tcl_PackageInitProc *proc = NULL;
   1.155 +    HINSTANCE handle = (HINSTANCE)loadHandle;
   1.156 +
   1.157 +    /*
   1.158 +     * For each symbol, check for both Symbol and _Symbol, since Borland
   1.159 +     * generates C symbols with a leading '_' by default.
   1.160 +     */
   1.161 +
   1.162 +    proc = (Tcl_PackageInitProc *) GetProcAddress(handle, symbol);
   1.163 +    if (proc == NULL) {
   1.164 +	Tcl_DString ds;
   1.165 +	Tcl_DStringInit(&ds);
   1.166 +	Tcl_DStringAppend(&ds, "_", 1);
   1.167 +	symbol = Tcl_DStringAppend(&ds, symbol, -1);
   1.168 +	proc = (Tcl_PackageInitProc *) GetProcAddress(handle, symbol);
   1.169 +	Tcl_DStringFree(&ds);
   1.170 +    }
   1.171 +    return proc;
   1.172 +}
   1.173 +
   1.174 +/*
   1.175 + *----------------------------------------------------------------------
   1.176 + *
   1.177 + * TclpUnloadFile --
   1.178 + *
   1.179 + *	Unloads a dynamically loaded binary code file from memory.
   1.180 + *	Code pointers in the formerly loaded file are no longer valid
   1.181 + *	after calling this function.
   1.182 + *
   1.183 + * Results:
   1.184 + *	None.
   1.185 + *
   1.186 + * Side effects:
   1.187 + *	Code removed from memory.
   1.188 + *
   1.189 + *----------------------------------------------------------------------
   1.190 + */
   1.191 +
   1.192 +void
   1.193 +TclpUnloadFile(loadHandle)
   1.194 +    Tcl_LoadHandle loadHandle;	/* loadHandle returned by a previous call
   1.195 +				 * to TclpDlopen().  The loadHandle is 
   1.196 +				 * a token that represents the loaded 
   1.197 +				 * file. */
   1.198 +{
   1.199 +    HINSTANCE handle;
   1.200 +
   1.201 +    handle = (HINSTANCE) loadHandle;
   1.202 +    FreeLibrary(handle);
   1.203 +}
   1.204 +
   1.205 +/*
   1.206 + *----------------------------------------------------------------------
   1.207 + *
   1.208 + * TclGuessPackageName --
   1.209 + *
   1.210 + *	If the "load" command is invoked without providing a package
   1.211 + *	name, this procedure is invoked to try to figure it out.
   1.212 + *
   1.213 + * Results:
   1.214 + *	Always returns 0 to indicate that we couldn't figure out a
   1.215 + *	package name;  generic code will then try to guess the package
   1.216 + *	from the file name.  A return value of 1 would have meant that
   1.217 + *	we figured out the package name and put it in bufPtr.
   1.218 + *
   1.219 + * Side effects:
   1.220 + *	None.
   1.221 + *
   1.222 + *----------------------------------------------------------------------
   1.223 + */
   1.224 +
   1.225 +int
   1.226 +TclGuessPackageName(fileName, bufPtr)
   1.227 +    CONST char *fileName;	/* Name of file containing package (already
   1.228 +				 * translated to local form if needed). */
   1.229 +    Tcl_DString *bufPtr;	/* Initialized empty dstring.  Append
   1.230 +				 * package name to this if possible. */
   1.231 +{
   1.232 +    return 0;
   1.233 +}