1 |
578 |
markom |
/*
|
2 |
|
|
* tclMacLoad.c --
|
3 |
|
|
*
|
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.
|
7 |
|
|
*
|
8 |
|
|
* Copyright (c) 1995-1996 Sun Microsystems, Inc.
|
9 |
|
|
*
|
10 |
|
|
* See the file "license.terms" for information on usage and redistribution
|
11 |
|
|
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
12 |
|
|
*
|
13 |
|
|
* RCS: @(#) $Id: tclMacLoad.c,v 1.1.1.1 2002-01-16 10:25:30 markom Exp $
|
14 |
|
|
*/
|
15 |
|
|
|
16 |
|
|
#include <CodeFragments.h>
|
17 |
|
|
#include <Errors.h>
|
18 |
|
|
#include <Resources.h>
|
19 |
|
|
#include <Strings.h>
|
20 |
|
|
#include <FSpCompat.h>
|
21 |
|
|
|
22 |
|
|
/*
|
23 |
|
|
* Seems that the 3.0.1 Universal headers leave this define out. So we
|
24 |
|
|
* define it here...
|
25 |
|
|
*/
|
26 |
|
|
|
27 |
|
|
#ifndef fragNoErr
|
28 |
|
|
#define fragNoErr noErr
|
29 |
|
|
#endif
|
30 |
|
|
|
31 |
|
|
#include "tclPort.h"
|
32 |
|
|
#include "tclInt.h"
|
33 |
|
|
#include "tclMacInt.h"
|
34 |
|
|
|
35 |
|
|
#if GENERATINGPOWERPC
|
36 |
|
|
#define OUR_ARCH_TYPE kPowerPCCFragArch
|
37 |
|
|
#else
|
38 |
|
|
#define OUR_ARCH_TYPE kMotorola68KCFragArch
|
39 |
|
|
#endif
|
40 |
|
|
|
41 |
|
|
/*
|
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.
|
45 |
|
|
*/
|
46 |
|
|
struct CfrgHeader {
|
47 |
|
|
long res1;
|
48 |
|
|
long res2;
|
49 |
|
|
long version;
|
50 |
|
|
long res3;
|
51 |
|
|
long res4;
|
52 |
|
|
long filler1;
|
53 |
|
|
long filler2;
|
54 |
|
|
long itemCount;
|
55 |
|
|
char arrayStart; /* Array of externalItems begins here. */
|
56 |
|
|
};
|
57 |
|
|
typedef struct CfrgHeader CfrgHeader, *CfrgHeaderPtr, **CfrgHeaderPtrHand;
|
58 |
|
|
|
59 |
|
|
/*
|
60 |
|
|
* The below structure defines a cfrag item within the cfrag resource.
|
61 |
|
|
*/
|
62 |
|
|
struct CfrgItem {
|
63 |
|
|
OSType archType;
|
64 |
|
|
long updateLevel;
|
65 |
|
|
long currVersion;
|
66 |
|
|
long oldDefVersion;
|
67 |
|
|
long appStackSize;
|
68 |
|
|
short appSubFolder;
|
69 |
|
|
char usage;
|
70 |
|
|
char location;
|
71 |
|
|
long codeOffset;
|
72 |
|
|
long codeLength;
|
73 |
|
|
long res1;
|
74 |
|
|
long res2;
|
75 |
|
|
short itemSize;
|
76 |
|
|
Str255 name; /* This is actually variable sized. */
|
77 |
|
|
};
|
78 |
|
|
typedef struct CfrgItem CfrgItem;
|
79 |
|
|
|
80 |
|
|
/*
|
81 |
|
|
*----------------------------------------------------------------------
|
82 |
|
|
*
|
83 |
|
|
* TclLoadFile --
|
84 |
|
|
*
|
85 |
|
|
* This procedure is called to carry out dynamic loading of binary
|
86 |
|
|
* code for the Macintosh. This implementation is based on the
|
87 |
|
|
* Code Fragment Manager & will not work on other systems.
|
88 |
|
|
*
|
89 |
|
|
* Results:
|
90 |
|
|
* The result is TCL_ERROR, and an error message is left in
|
91 |
|
|
* interp->result.
|
92 |
|
|
*
|
93 |
|
|
* Side effects:
|
94 |
|
|
* New binary code is loaded.
|
95 |
|
|
*
|
96 |
|
|
*----------------------------------------------------------------------
|
97 |
|
|
*/
|
98 |
|
|
|
99 |
|
|
int
|
100 |
|
|
TclLoadFile(
|
101 |
|
|
Tcl_Interp *interp, /* Used for error reporting. */
|
102 |
|
|
char *fileName, /* Name of the file containing the desired
|
103 |
|
|
* code. */
|
104 |
|
|
char *sym1, char *sym2, /* Names of two procedures to look up in
|
105 |
|
|
* the file's symbol table. */
|
106 |
|
|
Tcl_PackageInitProc **proc1Ptr,
|
107 |
|
|
Tcl_PackageInitProc **proc2Ptr)
|
108 |
|
|
/* Where to return the addresses corresponding
|
109 |
|
|
* to sym1 and sym2. */
|
110 |
|
|
{
|
111 |
|
|
CFragConnectionID connID;
|
112 |
|
|
Ptr dummy;
|
113 |
|
|
OSErr err;
|
114 |
|
|
CFragSymbolClass symClass;
|
115 |
|
|
FSSpec fileSpec;
|
116 |
|
|
short fragFileRef, saveFileRef;
|
117 |
|
|
Handle fragResource;
|
118 |
|
|
UInt32 offset = 0;
|
119 |
|
|
UInt32 length = kCFragGoesToEOF;
|
120 |
|
|
char packageName[255];
|
121 |
|
|
Str255 errName;
|
122 |
|
|
|
123 |
|
|
/*
|
124 |
|
|
* First thing we must do is infer the package name from the sym1
|
125 |
|
|
* variable. This is kind of dumb since the caller actually knows
|
126 |
|
|
* this value, it just doesn't give it to us.
|
127 |
|
|
*/
|
128 |
|
|
strcpy(packageName, sym1);
|
129 |
|
|
*packageName = (char) tolower(*packageName);
|
130 |
|
|
packageName[strlen(packageName) - 5] = NULL;
|
131 |
|
|
|
132 |
|
|
err = FSpLocationFromPath(strlen(fileName), fileName, &fileSpec);
|
133 |
|
|
if (err != noErr) {
|
134 |
|
|
interp->result = "could not locate shared library";
|
135 |
|
|
return TCL_ERROR;
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
/*
|
139 |
|
|
* See if this fragment has a 'cfrg' resource. It will tell us were
|
140 |
|
|
* to look for the fragment in the file. If it doesn't exist we will
|
141 |
|
|
* assume we have a ppc frag using the whole data fork. If it does
|
142 |
|
|
* exist we find the frag that matches the one we are looking for and
|
143 |
|
|
* get the offset and size from the resource.
|
144 |
|
|
*/
|
145 |
|
|
saveFileRef = CurResFile();
|
146 |
|
|
SetResLoad(false);
|
147 |
|
|
fragFileRef = FSpOpenResFile(&fileSpec, fsRdPerm);
|
148 |
|
|
SetResLoad(true);
|
149 |
|
|
if (fragFileRef != -1) {
|
150 |
|
|
UseResFile(fragFileRef);
|
151 |
|
|
fragResource = Get1Resource(kCFragResourceType, kCFragResourceID);
|
152 |
|
|
HLock(fragResource);
|
153 |
|
|
if (ResError() == noErr) {
|
154 |
|
|
CfrgItem* srcItem;
|
155 |
|
|
long itemCount, index;
|
156 |
|
|
Ptr itemStart;
|
157 |
|
|
|
158 |
|
|
itemCount = (*(CfrgHeaderPtrHand)fragResource)->itemCount;
|
159 |
|
|
itemStart = &(*(CfrgHeaderPtrHand)fragResource)->arrayStart;
|
160 |
|
|
for (index = 0; index < itemCount;
|
161 |
|
|
index++, itemStart += srcItem->itemSize) {
|
162 |
|
|
srcItem = (CfrgItem*)itemStart;
|
163 |
|
|
if (srcItem->archType != OUR_ARCH_TYPE) continue;
|
164 |
|
|
if (!strncasecmp(packageName, (char *) srcItem->name + 1,
|
165 |
|
|
srcItem->name[0])) {
|
166 |
|
|
offset = srcItem->codeOffset;
|
167 |
|
|
length = srcItem->codeLength;
|
168 |
|
|
}
|
169 |
|
|
}
|
170 |
|
|
}
|
171 |
|
|
/*
|
172 |
|
|
* Close the resource file. If the extension wants to reopen the
|
173 |
|
|
* resource fork it should use the tclMacLibrary.c file during it's
|
174 |
|
|
* construction.
|
175 |
|
|
*/
|
176 |
|
|
HUnlock(fragResource);
|
177 |
|
|
ReleaseResource(fragResource);
|
178 |
|
|
CloseResFile(fragFileRef);
|
179 |
|
|
UseResFile(saveFileRef);
|
180 |
|
|
}
|
181 |
|
|
|
182 |
|
|
/*
|
183 |
|
|
* Now we can attempt to load the fragement using the offset & length
|
184 |
|
|
* obtained from the resource. We don't worry about the main entry point
|
185 |
|
|
* as we are going to search for specific entry points passed to us.
|
186 |
|
|
*/
|
187 |
|
|
|
188 |
|
|
c2pstr(packageName);
|
189 |
|
|
err = GetDiskFragment(&fileSpec, offset, length, (StringPtr) packageName,
|
190 |
|
|
kLoadCFrag, &connID, &dummy, errName);
|
191 |
|
|
if (err != fragNoErr) {
|
192 |
|
|
p2cstr(errName);
|
193 |
|
|
Tcl_AppendResult(interp, "couldn't load file \"", fileName,
|
194 |
|
|
"\": ", errName, (char *) NULL);
|
195 |
|
|
return TCL_ERROR;
|
196 |
|
|
}
|
197 |
|
|
|
198 |
|
|
c2pstr(sym1);
|
199 |
|
|
err = FindSymbol(connID, (StringPtr) sym1, (Ptr *) proc1Ptr, &symClass);
|
200 |
|
|
p2cstr((StringPtr) sym1);
|
201 |
|
|
if (err != fragNoErr || symClass == kDataCFragSymbol) {
|
202 |
|
|
interp->result =
|
203 |
|
|
"could not find Initialization routine in library";
|
204 |
|
|
return TCL_ERROR;
|
205 |
|
|
}
|
206 |
|
|
|
207 |
|
|
c2pstr(sym2);
|
208 |
|
|
err = FindSymbol(connID, (StringPtr) sym2, (Ptr *) proc2Ptr, &symClass);
|
209 |
|
|
p2cstr((StringPtr) sym2);
|
210 |
|
|
if (err != fragNoErr || symClass == kDataCFragSymbol) {
|
211 |
|
|
*proc2Ptr = NULL;
|
212 |
|
|
}
|
213 |
|
|
|
214 |
|
|
return TCL_OK;
|
215 |
|
|
}
|
216 |
|
|
|
217 |
|
|
/*
|
218 |
|
|
*----------------------------------------------------------------------
|
219 |
|
|
*
|
220 |
|
|
* TclGuessPackageName --
|
221 |
|
|
*
|
222 |
|
|
* If the "load" command is invoked without providing a package
|
223 |
|
|
* name, this procedure is invoked to try to figure it out.
|
224 |
|
|
*
|
225 |
|
|
* Results:
|
226 |
|
|
* Always returns 0 to indicate that we couldn't figure out a
|
227 |
|
|
* package name; generic code will then try to guess the package
|
228 |
|
|
* from the file name. A return value of 1 would have meant that
|
229 |
|
|
* we figured out the package name and put it in bufPtr.
|
230 |
|
|
*
|
231 |
|
|
* Side effects:
|
232 |
|
|
* None.
|
233 |
|
|
*
|
234 |
|
|
*----------------------------------------------------------------------
|
235 |
|
|
*/
|
236 |
|
|
|
237 |
|
|
int
|
238 |
|
|
TclGuessPackageName(
|
239 |
|
|
char *fileName, /* Name of file containing package (already
|
240 |
|
|
* translated to local form if needed). */
|
241 |
|
|
Tcl_DString *bufPtr) /* Initialized empty dstring. Append
|
242 |
|
|
* package name to this if possible. */
|
243 |
|
|
{
|
244 |
|
|
return 0;
|
245 |
|
|
}
|