os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/unix/tclLoadNext.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/tclLoadNext.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,186 @@
     1.4 +/* 
     1.5 + * tclLoadNext.c --
     1.6 + *
     1.7 + *	This procedure provides a version of the TclLoadFile that
     1.8 + *	works with NeXTs rld_* dynamic loading.  This file provided
     1.9 + *	by Pedja Bogdanovich.
    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: tclLoadNext.c,v 1.11 2002/10/10 12:25:53 vincentdarley Exp $
    1.17 + */
    1.18 +
    1.19 +#include "tclInt.h"
    1.20 +#include <mach-o/rld.h>
    1.21 +#include <streams/streams.h>
    1.22 +
    1.23 +/*
    1.24 + *----------------------------------------------------------------------
    1.25 + *
    1.26 + * TclpDlopen --
    1.27 + *
    1.28 + *	Dynamically loads a binary code file into memory and returns
    1.29 + *	a handle to the new code.
    1.30 + *
    1.31 + * Results:
    1.32 + *	A standard Tcl completion code.  If an error occurs, an error
    1.33 + *	message is left in the interp's result.
    1.34 + *
    1.35 + * Side effects:
    1.36 + *	New code suddenly appears in memory.
    1.37 + *
    1.38 + *----------------------------------------------------------------------
    1.39 + */
    1.40 +
    1.41 +int
    1.42 +TclpDlopen(interp, pathPtr, loadHandle, unloadProcPtr)
    1.43 +    Tcl_Interp *interp;		/* Used for error reporting. */
    1.44 +    Tcl_Obj *pathPtr;		/* Name of the file containing the desired
    1.45 +				 * code (UTF-8). */
    1.46 +    Tcl_LoadHandle *loadHandle;	/* Filled with token for dynamically loaded
    1.47 +				 * file which will be passed back to 
    1.48 +				 * (*unloadProcPtr)() to unload the file. */
    1.49 +    Tcl_FSUnloadFileProc **unloadProcPtr;	
    1.50 +				/* Filled with address of Tcl_FSUnloadFileProc
    1.51 +				 * function which should be used for
    1.52 +				 * this file. */
    1.53 +{
    1.54 +    struct mach_header *header;
    1.55 +    char *fileName;
    1.56 +    char *files[2];
    1.57 +    CONST char *native;
    1.58 +    int result = 1;
    1.59 +    
    1.60 +    NXStream *errorStream = NXOpenMemory(0,0,NX_READWRITE);
    1.61 +    
    1.62 +    fileName = Tcl_GetString(pathPtr);
    1.63 +
    1.64 +    /* 
    1.65 +     * First try the full path the user gave us.  This is particularly
    1.66 +     * important if the cwd is inside a vfs, and we are trying to load
    1.67 +     * using a relative path.
    1.68 +     */
    1.69 +    native = Tcl_FSGetNativePath(pathPtr);
    1.70 +    files = {native,NULL};
    1.71 +
    1.72 +    result = rld_load(errorStream, &header, files, NULL);
    1.73 +    
    1.74 +    if (!result) {
    1.75 +	/* 
    1.76 +	 * Let the OS loader examine the binary search path for
    1.77 +	 * whatever string the user gave us which hopefully refers
    1.78 +	 * to a file on the binary path
    1.79 +	 */
    1.80 +	Tcl_DString ds;
    1.81 +	native = Tcl_UtfToExternalDString(NULL, fileName, -1, &ds);
    1.82 +	files = {native,NULL};
    1.83 +	result = rld_load(errorStream, &header, files, NULL);
    1.84 +	Tcl_DStringFree(&ds);
    1.85 +    }
    1.86 +    
    1.87 +    if (!result) {
    1.88 +	char *data;
    1.89 +	int len, maxlen;
    1.90 +	NXGetMemoryBuffer(errorStream,&data,&len,&maxlen);
    1.91 +	Tcl_AppendResult(interp, "couldn't load file \"",
    1.92 +			 fileName, "\": ", data, NULL);
    1.93 +	NXCloseMemory(errorStream, NX_FREEBUFFER);
    1.94 +	return TCL_ERROR;
    1.95 +    }
    1.96 +    NXCloseMemory(errorStream, NX_FREEBUFFER);
    1.97 +    
    1.98 +    *loadHandle = (Tcl_LoadHandle)1; /* A dummy non-NULL value */
    1.99 +    *unloadProcPtr = &TclpUnloadFile;
   1.100 +    
   1.101 +    return TCL_OK;
   1.102 +}
   1.103 +
   1.104 +/*
   1.105 + *----------------------------------------------------------------------
   1.106 + *
   1.107 + * TclpFindSymbol --
   1.108 + *
   1.109 + *	Looks up a symbol, by name, through a handle associated with
   1.110 + *	a previously loaded piece of code (shared library).
   1.111 + *
   1.112 + * Results:
   1.113 + *	Returns a pointer to the function associated with 'symbol' if
   1.114 + *	it is found.  Otherwise returns NULL and may leave an error
   1.115 + *	message in the interp's result.
   1.116 + *
   1.117 + *----------------------------------------------------------------------
   1.118 + */
   1.119 +Tcl_PackageInitProc*
   1.120 +TclpFindSymbol(interp, loadHandle, symbol) 
   1.121 +    Tcl_Interp *interp;
   1.122 +    Tcl_LoadHandle loadHandle;
   1.123 +    CONST char *symbol;
   1.124 +{
   1.125 +    Tcl_PackageInitProc *proc=NULL;
   1.126 +    if(symbol) {
   1.127 +	char sym[strlen(symbol)+2];
   1.128 +	sym[0]='_'; sym[1]=0; strcat(sym,symbol);
   1.129 +	rld_lookup(NULL,sym,(unsigned long *)&proc);
   1.130 +    }
   1.131 +    return proc;
   1.132 +}
   1.133 +
   1.134 +/*
   1.135 + *----------------------------------------------------------------------
   1.136 + *
   1.137 + * TclpUnloadFile --
   1.138 + *
   1.139 + *	Unloads a dynamically loaded binary code file from memory.
   1.140 + *	Code pointers in the formerly loaded file are no longer valid
   1.141 + *	after calling this function.
   1.142 + *
   1.143 + * Results:
   1.144 + *	None.
   1.145 + *
   1.146 + * Side effects:
   1.147 + *	Does nothing.  Can anything be done?
   1.148 + *
   1.149 + *----------------------------------------------------------------------
   1.150 + */
   1.151 +
   1.152 +void
   1.153 +TclpUnloadFile(loadHandle)
   1.154 +    Tcl_LoadHandle loadHandle;	/* loadHandle returned by a previous call
   1.155 +				 * to TclpDlopen().  The loadHandle is 
   1.156 +				 * a token that represents the loaded 
   1.157 +				 * file. */
   1.158 +{
   1.159 +}
   1.160 +
   1.161 +/*
   1.162 + *----------------------------------------------------------------------
   1.163 + *
   1.164 + * TclGuessPackageName --
   1.165 + *
   1.166 + *	If the "load" command is invoked without providing a package
   1.167 + *	name, this procedure is invoked to try to figure it out.
   1.168 + *
   1.169 + * Results:
   1.170 + *	Always returns 0 to indicate that we couldn't figure out a
   1.171 + *	package name;  generic code will then try to guess the package
   1.172 + *	from the file name.  A return value of 1 would have meant that
   1.173 + *	we figured out the package name and put it in bufPtr.
   1.174 + *
   1.175 + * Side effects:
   1.176 + *	None.
   1.177 + *
   1.178 + *----------------------------------------------------------------------
   1.179 + */
   1.180 +
   1.181 +int
   1.182 +TclGuessPackageName(fileName, bufPtr)
   1.183 +    CONST char *fileName;	/* Name of file containing package (already
   1.184 +				 * translated to local form if needed). */
   1.185 +    Tcl_DString *bufPtr;	/* Initialized empty dstring.  Append
   1.186 +				 * package name to this if possible. */
   1.187 +{
   1.188 +    return 0;
   1.189 +}