os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/mac/tclMacLoad.c
Update contrib.
4 * This procedure provides a version of the TclLoadFile for use
5 * on the Macintosh. This procedure will only work with systems
6 * that use the Code Fragment Manager.
8 * Copyright (c) 1995-1997 Sun Microsystems, Inc.
10 * See the file "license.terms" for information on usage and redistribution
11 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 * RCS: @(#) $Id: tclMacLoad.c,v 1.16 2002/10/09 11:54:26 das Exp $
16 #include <CodeFragments.h>
18 #include <Resources.h>
20 #include <FSpCompat.h>
23 * Seems that the 3.0.1 Universal headers leave this define out. So we
28 #define fragNoErr noErr
33 #include "tclMacInt.h"
36 #define OUR_ARCH_TYPE kPowerPCCFragArch
38 #define OUR_ARCH_TYPE kMotorola68KCFragArch
42 * The following data structure defines the structure of a code fragment
43 * resource. We can cast the resource to be of this type to access
44 * any fields we need to see.
55 char arrayStart; /* Array of externalItems begins here. */
57 typedef struct CfrgHeader CfrgHeader, *CfrgHeaderPtr, **CfrgHeaderPtrHand;
60 * The below structure defines a cfrag item within the cfrag resource.
76 Str255 name; /* This is actually variable sized. */
78 typedef struct CfrgItem CfrgItem;
81 * On MacOS, old shared libraries which contain many code fragments
82 * cannot, it seems, be loaded in one go. We need to look provide
83 * the name of a code fragment while we load. Since with the
84 * separation of the 'load' and 'findsymbol' be do not necessarily
85 * know a symbol name at load time, we have to store some further
86 * information in a structure like this so we can ensure we load
87 * properly in 'findsymbol' if the first attempts didn't work.
89 typedef struct TclMacLoadInfo {
91 CFragConnectionID connID;
95 static int TryToLoad(Tcl_Interp *interp, TclMacLoadInfo *loadInfo, Tcl_Obj *pathPtr,
96 CONST char *sym /* native */);
100 *----------------------------------------------------------------------
104 * This procedure is called to carry out dynamic loading of binary
105 * code for the Macintosh. This implementation is based on the
106 * Code Fragment Manager & will not work on other systems.
109 * The result is TCL_ERROR, and an error message is left in
110 * the interp's result.
113 * New binary code is loaded.
115 *----------------------------------------------------------------------
119 TclpDlopen(interp, pathPtr, loadHandle, unloadProcPtr)
120 Tcl_Interp *interp; /* Used for error reporting. */
121 Tcl_Obj *pathPtr; /* Name of the file containing the desired
123 Tcl_LoadHandle *loadHandle; /* Filled with token for dynamically loaded
124 * file which will be passed back to
125 * (*unloadProcPtr)() to unload the file. */
126 Tcl_FSUnloadFileProc **unloadProcPtr;
127 /* Filled with address of Tcl_FSUnloadFileProc
128 * function which should be used for
134 TclMacLoadInfo *loadInfo;
136 native = Tcl_FSGetNativePath(pathPtr);
137 err = FSpLocationFromPath(strlen(native), native, &fileSpec);
140 Tcl_SetResult(interp, "could not locate shared library", TCL_STATIC);
144 loadInfo = (TclMacLoadInfo *) ckalloc(sizeof(TclMacLoadInfo));
145 loadInfo->loaded = 0;
146 loadInfo->fileSpec = fileSpec;
147 loadInfo->connID = NULL;
149 if (TryToLoad(interp, loadInfo, pathPtr, NULL) != TCL_OK) {
150 ckfree((char*) loadInfo);
154 *loadHandle = (Tcl_LoadHandle)loadInfo;
155 *unloadProcPtr = &TclpUnloadFile;
160 * See the comments about 'struct TclMacLoadInfo' above. This
161 * function ensures the appropriate library or symbol is
165 TryToLoad(Tcl_Interp *interp, TclMacLoadInfo *loadInfo, Tcl_Obj *pathPtr,
166 CONST char *sym /* native */)
169 CFragConnectionID connID;
171 short fragFileRef, saveFileRef;
174 UInt32 length = kCFragGoesToEOF;
176 StringPtr fragName=NULL;
178 if (loadInfo->loaded == 1) {
183 * See if this fragment has a 'cfrg' resource. It will tell us where
184 * to look for the fragment in the file. If it doesn't exist we will
185 * assume we have a ppc frag using the whole data fork. If it does
186 * exist we find the frag that matches the one we are looking for and
187 * get the offset and size from the resource.
190 saveFileRef = CurResFile();
192 fragFileRef = FSpOpenResFile(&loadInfo->fileSpec, fsRdPerm);
194 if (fragFileRef != -1) {
196 UseResFile(fragFileRef);
197 fragResource = Get1Resource(kCFragResourceType, kCFragResourceID);
199 if (ResError() == noErr) {
201 long itemCount, index;
204 itemCount = (*(CfrgHeaderPtrHand)fragResource)->itemCount;
205 itemStart = &(*(CfrgHeaderPtrHand)fragResource)->arrayStart;
206 for (index = 0; index < itemCount;
207 index++, itemStart += srcItem->itemSize) {
208 srcItem = (CfrgItem*)itemStart;
209 if (srcItem->archType != OUR_ARCH_TYPE) continue;
210 if (!strncasecmp(sym, (char *) srcItem->name + 1,
212 offset = srcItem->codeOffset;
213 length = srcItem->codeLength;
214 fragName=srcItem->name;
220 * Close the resource file. If the extension wants to reopen the
221 * resource fork it should use the tclMacLibrary.c file during it's
224 HUnlock(fragResource);
225 ReleaseResource(fragResource);
226 CloseResFile(fragFileRef);
227 UseResFile(saveFileRef);
235 * Now we can attempt to load the fragment using the offset & length
236 * obtained from the resource. We don't worry about the main entry point
237 * as we are going to search for specific entry points passed to us.
240 err = GetDiskFragment(&loadInfo->fileSpec, offset, length, fragName,
241 kLoadCFrag, &connID, &dummy, errName);
243 if (err != fragNoErr) {
246 Tcl_AppendResult(interp, "couldn't load file \"",
247 Tcl_GetString(pathPtr),
248 "\": ", errName, (char *) NULL);
250 Tcl_AppendResult(interp, "couldn't load library \"",
252 "\": ", errName, (char *) NULL);
257 loadInfo->connID = connID;
258 loadInfo->loaded = 1;
264 *----------------------------------------------------------------------
268 * Looks up a symbol, by name, through a handle associated with
269 * a previously loaded piece of code (shared library).
272 * Returns a pointer to the function associated with 'symbol' if
273 * it is found. Otherwise returns NULL and may leave an error
274 * message in the interp's result.
276 *----------------------------------------------------------------------
279 TclpFindSymbol(interp, loadHandle, symbol)
281 Tcl_LoadHandle loadHandle;
285 Tcl_PackageInitProc *proc=NULL;
286 TclMacLoadInfo *loadInfo = (TclMacLoadInfo *)loadHandle;
288 CFragSymbolClass symClass;
291 if (loadInfo->loaded == 0) {
294 * First thing we must do is infer the package name from the
295 * sym variable. We do this by removing the '_Init'.
297 Tcl_UtfToExternalDString(NULL, symbol, -1, &ds);
298 Tcl_DStringSetLength(&ds, Tcl_DStringLength(&ds) - 5);
299 res = TryToLoad(interp, loadInfo, NULL, Tcl_DStringValue(&ds));
300 Tcl_DStringFree(&ds);
306 Tcl_UtfToExternalDString(NULL, symbol, -1, &ds);
307 strcpy((char *) symbolName + 1, Tcl_DStringValue(&ds));
308 symbolName[0] = (unsigned) Tcl_DStringLength(&ds);
309 err = FindSymbol(loadInfo->connID, symbolName, (Ptr *) &proc, &symClass);
310 Tcl_DStringFree(&ds);
311 if (err != fragNoErr || symClass == kDataCFragSymbol) {
312 Tcl_SetResult(interp,
313 "could not find Initialization routine in library",
321 *----------------------------------------------------------------------
325 * Unloads a dynamically loaded binary code file from memory.
326 * Code pointers in the formerly loaded file are no longer valid
327 * after calling this function.
333 * Does nothing. Can anything be done?
335 *----------------------------------------------------------------------
339 TclpUnloadFile(loadHandle)
340 Tcl_LoadHandle loadHandle; /* loadHandle returned by a previous call
341 * to TclpDlopen(). The loadHandle is
342 * a token that represents the loaded
345 TclMacLoadInfo *loadInfo = (TclMacLoadInfo *)loadHandle;
346 if (loadInfo->loaded) {
347 CloseConnection((CFragConnectionID*) &(loadInfo->connID));
349 ckfree((char*)loadInfo);
353 *----------------------------------------------------------------------
355 * TclGuessPackageName --
357 * If the "load" command is invoked without providing a package
358 * name, this procedure is invoked to try to figure it out.
361 * Always returns 0 to indicate that we couldn't figure out a
362 * package name; generic code will then try to guess the package
363 * from the file name. A return value of 1 would have meant that
364 * we figured out the package name and put it in bufPtr.
369 *----------------------------------------------------------------------
374 CONST char *fileName, /* Name of file containing package (already
375 * translated to local form if needed). */
376 Tcl_DString *bufPtr) /* Initialized empty dstring. Append
377 * package name to this if possible. */