os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/unix/tclLoadShl.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* 
sl@0
     2
 * tclLoadShl.c --
sl@0
     3
 *
sl@0
     4
 *	This procedure provides a version of the TclLoadFile that works
sl@0
     5
 *	with the "shl_load" and "shl_findsym" library procedures for
sl@0
     6
 *	dynamic loading (e.g. for HP machines).
sl@0
     7
 *
sl@0
     8
 * Copyright (c) 1995-1997 Sun Microsystems, Inc.
sl@0
     9
 *
sl@0
    10
 * See the file "license.terms" for information on usage and redistribution
sl@0
    11
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
sl@0
    12
 *
sl@0
    13
 * RCS: @(#) $Id: tclLoadShl.c,v 1.13.2.1 2005/10/05 04:23:56 hobbs Exp $
sl@0
    14
 */
sl@0
    15
sl@0
    16
#include <dl.h>
sl@0
    17
sl@0
    18
/*
sl@0
    19
 * On some HP machines, dl.h defines EXTERN; remove that definition.
sl@0
    20
 */
sl@0
    21
sl@0
    22
#ifdef EXTERN
sl@0
    23
#   undef EXTERN
sl@0
    24
#endif
sl@0
    25
sl@0
    26
#include "tclInt.h"
sl@0
    27

sl@0
    28
/*
sl@0
    29
 *----------------------------------------------------------------------
sl@0
    30
 *
sl@0
    31
 * TclpDlopen --
sl@0
    32
 *
sl@0
    33
 *	Dynamically loads a binary code file into memory and returns
sl@0
    34
 *	a handle to the new code.
sl@0
    35
 *
sl@0
    36
 * Results:
sl@0
    37
 *	A standard Tcl completion code.  If an error occurs, an error
sl@0
    38
 *	message is left in the interp's result.
sl@0
    39
 *
sl@0
    40
 * Side effects:
sl@0
    41
 *	New code suddenly appears in memory.
sl@0
    42
 *
sl@0
    43
 *----------------------------------------------------------------------
sl@0
    44
 */
sl@0
    45
sl@0
    46
int
sl@0
    47
TclpDlopen(interp, pathPtr, loadHandle, unloadProcPtr)
sl@0
    48
    Tcl_Interp *interp;		/* Used for error reporting. */
sl@0
    49
    Tcl_Obj *pathPtr;		/* Name of the file containing the desired
sl@0
    50
				 * code (UTF-8). */
sl@0
    51
    Tcl_LoadHandle *loadHandle;	/* Filled with token for dynamically loaded
sl@0
    52
				 * file which will be passed back to 
sl@0
    53
				 * (*unloadProcPtr)() to unload the file. */
sl@0
    54
    Tcl_FSUnloadFileProc **unloadProcPtr;	
sl@0
    55
				/* Filled with address of Tcl_FSUnloadFileProc
sl@0
    56
				 * function which should be used for
sl@0
    57
				 * this file. */
sl@0
    58
{
sl@0
    59
    shl_t handle;
sl@0
    60
    CONST char *native;
sl@0
    61
    char *fileName = Tcl_GetString(pathPtr);
sl@0
    62
sl@0
    63
    /*
sl@0
    64
     * The flags below used to be BIND_IMMEDIATE; they were changed at
sl@0
    65
     * the suggestion of Wolfgang Kechel (wolfgang@prs.de): "This
sl@0
    66
     * enables verbosity for missing symbols when loading a shared lib
sl@0
    67
     * and allows to load libtk8.0.sl into tclsh8.0 without problems.
sl@0
    68
     * In general, this delays resolving symbols until they are actually
sl@0
    69
     * needed.  Shared libs do no longer need all libraries linked in
sl@0
    70
     * when they are build."
sl@0
    71
     */
sl@0
    72
sl@0
    73
sl@0
    74
    /*
sl@0
    75
     * First try the full path the user gave us.  This is particularly
sl@0
    76
     * important if the cwd is inside a vfs, and we are trying to load
sl@0
    77
     * using a relative path.
sl@0
    78
     */
sl@0
    79
    native = Tcl_FSGetNativePath(pathPtr);
sl@0
    80
    handle = shl_load(native, BIND_DEFERRED|BIND_VERBOSE, 0L);
sl@0
    81
sl@0
    82
    if (handle == NULL) {
sl@0
    83
	/*
sl@0
    84
	 * Let the OS loader examine the binary search path for
sl@0
    85
	 * whatever string the user gave us which hopefully refers
sl@0
    86
	 * to a file on the binary path
sl@0
    87
	 */
sl@0
    88
	Tcl_DString ds;
sl@0
    89
	native = Tcl_UtfToExternalDString(NULL, fileName, -1, &ds);
sl@0
    90
	handle = shl_load(native,
sl@0
    91
			  BIND_DEFERRED|BIND_VERBOSE|DYNAMIC_PATH, 0L);
sl@0
    92
	Tcl_DStringFree(&ds);
sl@0
    93
    }
sl@0
    94
sl@0
    95
    if (handle == NULL) {
sl@0
    96
	Tcl_AppendResult(interp, "couldn't load file \"", fileName,
sl@0
    97
		"\": ", Tcl_PosixError(interp), (char *) NULL);
sl@0
    98
	return TCL_ERROR;
sl@0
    99
    }
sl@0
   100
    *loadHandle = (Tcl_LoadHandle) handle;
sl@0
   101
    *unloadProcPtr = &TclpUnloadFile;
sl@0
   102
    return TCL_OK;
sl@0
   103
}
sl@0
   104

sl@0
   105
/*
sl@0
   106
 *----------------------------------------------------------------------
sl@0
   107
 *
sl@0
   108
 * TclpFindSymbol --
sl@0
   109
 *
sl@0
   110
 *	Looks up a symbol, by name, through a handle associated with
sl@0
   111
 *	a previously loaded piece of code (shared library).
sl@0
   112
 *
sl@0
   113
 * Results:
sl@0
   114
 *	Returns a pointer to the function associated with 'symbol' if
sl@0
   115
 *	it is found.  Otherwise returns NULL and may leave an error
sl@0
   116
 *	message in the interp's result.
sl@0
   117
 *
sl@0
   118
 *----------------------------------------------------------------------
sl@0
   119
 */
sl@0
   120
Tcl_PackageInitProc*
sl@0
   121
TclpFindSymbol(interp, loadHandle, symbol) 
sl@0
   122
    Tcl_Interp *interp;
sl@0
   123
    Tcl_LoadHandle loadHandle;
sl@0
   124
    CONST char *symbol;
sl@0
   125
{
sl@0
   126
    Tcl_DString newName;
sl@0
   127
    Tcl_PackageInitProc *proc=NULL;
sl@0
   128
    shl_t handle = (shl_t)loadHandle;
sl@0
   129
    /*
sl@0
   130
     * Some versions of the HP system software still use "_" at the
sl@0
   131
     * beginning of exported symbols while others don't;  try both
sl@0
   132
     * forms of each name.
sl@0
   133
     */
sl@0
   134
sl@0
   135
    if (shl_findsym(&handle, symbol, (short) TYPE_PROCEDURE, (void *) &proc)
sl@0
   136
	    != 0) {
sl@0
   137
	Tcl_DStringInit(&newName);
sl@0
   138
	Tcl_DStringAppend(&newName, "_", 1);
sl@0
   139
	Tcl_DStringAppend(&newName, symbol, -1);
sl@0
   140
	if (shl_findsym(&handle, Tcl_DStringValue(&newName),
sl@0
   141
		(short) TYPE_PROCEDURE, (void *) &proc) != 0) {
sl@0
   142
	    proc = NULL;
sl@0
   143
	}
sl@0
   144
	Tcl_DStringFree(&newName);
sl@0
   145
    }
sl@0
   146
    return proc;
sl@0
   147
}
sl@0
   148

sl@0
   149
/*
sl@0
   150
 *----------------------------------------------------------------------
sl@0
   151
 *
sl@0
   152
 * TclpUnloadFile --
sl@0
   153
 *
sl@0
   154
 *	Unloads a dynamically loaded binary code file from memory.
sl@0
   155
 *	Code pointers in the formerly loaded file are no longer valid
sl@0
   156
 *	after calling this function.
sl@0
   157
 *
sl@0
   158
 * Results:
sl@0
   159
 *	None.
sl@0
   160
 *
sl@0
   161
 * Side effects:
sl@0
   162
 *	Code removed from memory.
sl@0
   163
 *
sl@0
   164
 *----------------------------------------------------------------------
sl@0
   165
 */
sl@0
   166
sl@0
   167
void
sl@0
   168
TclpUnloadFile(loadHandle)
sl@0
   169
    Tcl_LoadHandle loadHandle;	/* loadHandle returned by a previous call
sl@0
   170
				 * to TclpDlopen().  The loadHandle is 
sl@0
   171
				 * a token that represents the loaded 
sl@0
   172
				 * file. */
sl@0
   173
{
sl@0
   174
    shl_t handle;
sl@0
   175
sl@0
   176
    handle = (shl_t) loadHandle;
sl@0
   177
    shl_unload(handle);
sl@0
   178
}
sl@0
   179

sl@0
   180
/*
sl@0
   181
 *----------------------------------------------------------------------
sl@0
   182
 *
sl@0
   183
 * TclGuessPackageName --
sl@0
   184
 *
sl@0
   185
 *	If the "load" command is invoked without providing a package
sl@0
   186
 *	name, this procedure is invoked to try to figure it out.
sl@0
   187
 *
sl@0
   188
 * Results:
sl@0
   189
 *	Always returns 0 to indicate that we couldn't figure out a
sl@0
   190
 *	package name;  generic code will then try to guess the package
sl@0
   191
 *	from the file name.  A return value of 1 would have meant that
sl@0
   192
 *	we figured out the package name and put it in bufPtr.
sl@0
   193
 *
sl@0
   194
 * Side effects:
sl@0
   195
 *	None.
sl@0
   196
 *
sl@0
   197
 *----------------------------------------------------------------------
sl@0
   198
 */
sl@0
   199
sl@0
   200
int
sl@0
   201
TclGuessPackageName(fileName, bufPtr)
sl@0
   202
    CONST char *fileName;	/* Name of file containing package (already
sl@0
   203
				 * translated to local form if needed). */
sl@0
   204
    Tcl_DString *bufPtr;	/* Initialized empty dstring.  Append
sl@0
   205
				 * package name to this if possible. */
sl@0
   206
{
sl@0
   207
    return 0;
sl@0
   208
}