1 |
578 |
markom |
/*
|
2 |
|
|
* tclMacInit.c --
|
3 |
|
|
*
|
4 |
|
|
* Contains the Mac-specific interpreter initialization functions.
|
5 |
|
|
*
|
6 |
|
|
* Copyright (c) 1995-1997 Sun Microsystems, Inc.
|
7 |
|
|
*
|
8 |
|
|
* See the file "license.terms" for information on usage and redistribution
|
9 |
|
|
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
10 |
|
|
*
|
11 |
|
|
* RCS: @(#) $Id: tclMacInit.c,v 1.1.1.1 2002-01-16 10:25:30 markom Exp $
|
12 |
|
|
*/
|
13 |
|
|
|
14 |
|
|
#include <Files.h>
|
15 |
|
|
#include <Gestalt.h>
|
16 |
|
|
#include <TextUtils.h>
|
17 |
|
|
#include <Resources.h>
|
18 |
|
|
#include <Strings.h>
|
19 |
|
|
#include "tclInt.h"
|
20 |
|
|
#include "tclMacInt.h"
|
21 |
|
|
|
22 |
|
|
/*
|
23 |
|
|
*----------------------------------------------------------------------
|
24 |
|
|
*
|
25 |
|
|
* TclPlatformInit --
|
26 |
|
|
*
|
27 |
|
|
* Performs Mac-specific interpreter initialization related to the
|
28 |
|
|
* tcl_platform and tcl_library variables.
|
29 |
|
|
*
|
30 |
|
|
* Results:
|
31 |
|
|
* None.
|
32 |
|
|
*
|
33 |
|
|
* Side effects:
|
34 |
|
|
* Sets "tcl_library" & "tcl_platfrom" Tcl variable
|
35 |
|
|
*
|
36 |
|
|
*----------------------------------------------------------------------
|
37 |
|
|
*/
|
38 |
|
|
|
39 |
|
|
void
|
40 |
|
|
TclPlatformInit(
|
41 |
|
|
Tcl_Interp *interp) /* Tcl interpreter to initialize. */
|
42 |
|
|
{
|
43 |
|
|
char *libDir;
|
44 |
|
|
Tcl_DString path, libPath;
|
45 |
|
|
long int gestaltResult;
|
46 |
|
|
int minor, major;
|
47 |
|
|
char versStr[10];
|
48 |
|
|
|
49 |
|
|
/*
|
50 |
|
|
* Set runtime C variable that tells cross platform C functions
|
51 |
|
|
* what platform they are running on. This can change at
|
52 |
|
|
* runtime for testing purposes.
|
53 |
|
|
*/
|
54 |
|
|
tclPlatform = TCL_PLATFORM_MAC;
|
55 |
|
|
|
56 |
|
|
/*
|
57 |
|
|
* Define the tcl_platfrom variable.
|
58 |
|
|
*/
|
59 |
|
|
Tcl_SetVar2(interp, "tcl_platform", "platform", "macintosh",
|
60 |
|
|
TCL_GLOBAL_ONLY);
|
61 |
|
|
Tcl_SetVar2(interp, "tcl_platform", "os", "MacOS", TCL_GLOBAL_ONLY);
|
62 |
|
|
Gestalt(gestaltSystemVersion, &gestaltResult);
|
63 |
|
|
major = (gestaltResult & 0x0000FF00) >> 8;
|
64 |
|
|
minor = (gestaltResult & 0x000000F0) >> 4;
|
65 |
|
|
sprintf(versStr, "%d.%d", major, minor);
|
66 |
|
|
Tcl_SetVar2(interp, "tcl_platform", "osVersion", versStr, TCL_GLOBAL_ONLY);
|
67 |
|
|
#if GENERATINGPOWERPC
|
68 |
|
|
Tcl_SetVar2(interp, "tcl_platform", "machine", "ppc", TCL_GLOBAL_ONLY);
|
69 |
|
|
#else
|
70 |
|
|
Tcl_SetVar2(interp, "tcl_platform", "machine", "68k", TCL_GLOBAL_ONLY);
|
71 |
|
|
#endif
|
72 |
|
|
|
73 |
|
|
/*
|
74 |
|
|
* The tcl_library path can be found in one of two places. As an element
|
75 |
|
|
* in the env array. Or the default which is to a folder in side the
|
76 |
|
|
* Extensions folder of your system.
|
77 |
|
|
*/
|
78 |
|
|
|
79 |
|
|
Tcl_DStringInit(&path);
|
80 |
|
|
libDir = Tcl_GetVar2(interp, "env", "TCL_LIBRARY", TCL_GLOBAL_ONLY);
|
81 |
|
|
if (libDir != NULL) {
|
82 |
|
|
Tcl_SetVar(interp, "tcl_library", libDir, TCL_GLOBAL_ONLY);
|
83 |
|
|
} else {
|
84 |
|
|
libDir = Tcl_GetVar2(interp, "env", "EXT_FOLDER", TCL_GLOBAL_ONLY);
|
85 |
|
|
if (libDir != NULL) {
|
86 |
|
|
Tcl_JoinPath(1, &libDir, &path);
|
87 |
|
|
|
88 |
|
|
Tcl_DStringInit(&libPath);
|
89 |
|
|
Tcl_DStringAppend(&libPath, ":Tool Command Language:tcl", -1);
|
90 |
|
|
Tcl_DStringAppend(&libPath, TCL_VERSION, -1);
|
91 |
|
|
Tcl_JoinPath(1, &libPath.string, &path);
|
92 |
|
|
Tcl_DStringFree(&libPath);
|
93 |
|
|
Tcl_SetVar(interp, "tcl_library", path.string, TCL_GLOBAL_ONLY);
|
94 |
|
|
} else {
|
95 |
|
|
Tcl_SetVar(interp, "tcl_library", "no library", TCL_GLOBAL_ONLY);
|
96 |
|
|
}
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
/*
|
100 |
|
|
* Now create the tcl_pkgPath variable.
|
101 |
|
|
*/
|
102 |
|
|
Tcl_DStringSetLength(&path, 0);
|
103 |
|
|
libDir = Tcl_GetVar2(interp, "env", "EXT_FOLDER", TCL_GLOBAL_ONLY);
|
104 |
|
|
if (libDir != NULL) {
|
105 |
|
|
Tcl_JoinPath(1, &libDir, &path);
|
106 |
|
|
libDir = ":Tool Command Language:";
|
107 |
|
|
Tcl_JoinPath(1, &libDir, &path);
|
108 |
|
|
Tcl_SetVar(interp, "tcl_pkgPath", path.string,
|
109 |
|
|
TCL_GLOBAL_ONLY|TCL_LIST_ELEMENT);
|
110 |
|
|
} else {
|
111 |
|
|
Tcl_SetVar(interp, "tcl_pkgPath", "no extension folder",
|
112 |
|
|
TCL_GLOBAL_ONLY|TCL_LIST_ELEMENT);
|
113 |
|
|
}
|
114 |
|
|
Tcl_DStringFree(&path);
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
/*
|
118 |
|
|
*----------------------------------------------------------------------
|
119 |
|
|
*
|
120 |
|
|
* TclpCheckStackSpace --
|
121 |
|
|
*
|
122 |
|
|
* On a 68K Mac, we can detect if we are about to blow the stack.
|
123 |
|
|
* Called before an evaluation can happen when nesting depth is
|
124 |
|
|
* checked.
|
125 |
|
|
*
|
126 |
|
|
* Results:
|
127 |
|
|
* 1 if there is enough stack space to continue; 0 if not.
|
128 |
|
|
*
|
129 |
|
|
* Side effects:
|
130 |
|
|
* None.
|
131 |
|
|
*
|
132 |
|
|
*----------------------------------------------------------------------
|
133 |
|
|
*/
|
134 |
|
|
|
135 |
|
|
int
|
136 |
|
|
TclpCheckStackSpace()
|
137 |
|
|
{
|
138 |
|
|
return StackSpace() > TCL_MAC_STACK_THRESHOLD;
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
/*
|
142 |
|
|
*----------------------------------------------------------------------
|
143 |
|
|
*
|
144 |
|
|
* Tcl_Init --
|
145 |
|
|
*
|
146 |
|
|
* This procedure is typically invoked by Tcl_AppInit procedures
|
147 |
|
|
* to perform additional initialization for a Tcl interpreter,
|
148 |
|
|
* such as sourcing the "init.tcl" script.
|
149 |
|
|
*
|
150 |
|
|
* Results:
|
151 |
|
|
* Returns a standard Tcl completion code and sets interp->result
|
152 |
|
|
* if there is an error.
|
153 |
|
|
*
|
154 |
|
|
* Side effects:
|
155 |
|
|
* Depends on what's in the init.tcl script.
|
156 |
|
|
*
|
157 |
|
|
*----------------------------------------------------------------------
|
158 |
|
|
*/
|
159 |
|
|
|
160 |
|
|
int
|
161 |
|
|
Tcl_Init(
|
162 |
|
|
Tcl_Interp *interp) /* Interpreter to initialize. */
|
163 |
|
|
{
|
164 |
|
|
static char initCmd[] =
|
165 |
|
|
"if {[catch {source -rsrc Init}] != 0} {\n\
|
166 |
|
|
if [file exists [info library]:init.tcl] {\n\
|
167 |
|
|
source [info library]:init.tcl\n\
|
168 |
|
|
} else {\n\
|
169 |
|
|
set msg \"can't find Init resource or [info library]:init.tcl;\"\n\
|
170 |
|
|
append msg \" perhaps you need to\\ninstall Tcl or set your \"\n\
|
171 |
|
|
append msg \"TCL_LIBRARY environment variable?\"\n\
|
172 |
|
|
error $msg\n\
|
173 |
|
|
}\n}\n\
|
174 |
|
|
if {[catch {source -rsrc History}] != 0} {\n\
|
175 |
|
|
if [file exists [info library]:history.tcl] {\n\
|
176 |
|
|
source [info library]:history.tcl\n\
|
177 |
|
|
} else {\n\
|
178 |
|
|
set msg \"can't find History resource or [info library]:history.tcl;\"\n\
|
179 |
|
|
append msg \" perhaps you need to\\ninstall Tcl or set your \"\n\
|
180 |
|
|
append msg \"TCL_LIBRARY environment variable?\"\n\
|
181 |
|
|
error $msg\n\
|
182 |
|
|
}\n}\n\
|
183 |
|
|
if {[catch {source -rsrc Word}] != 0} {\n\
|
184 |
|
|
if [file exists [info library]:word.tcl] {\n\
|
185 |
|
|
source [info library]:word.tcl\n\
|
186 |
|
|
} else {\n\
|
187 |
|
|
set msg \"can't find Word resource or [info library]:word.tcl;\"\n\
|
188 |
|
|
append msg \" perhaps you need to\\ninstall Tcl or set your \"\n\
|
189 |
|
|
append msg \"TCL_LIBRARY environment variable?\"\n\
|
190 |
|
|
error $msg\n\
|
191 |
|
|
}\n}";
|
192 |
|
|
|
193 |
|
|
/*
|
194 |
|
|
* For Macintosh applications the Init function may be contained in
|
195 |
|
|
* the application resources. If it exists we use it - otherwise we
|
196 |
|
|
* look in the tcl_library directory. Ditto for the history command.
|
197 |
|
|
*/
|
198 |
|
|
|
199 |
|
|
return Tcl_Eval(interp, initCmd);
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
/*
|
203 |
|
|
*----------------------------------------------------------------------
|
204 |
|
|
*
|
205 |
|
|
* Tcl_SourceRCFile --
|
206 |
|
|
*
|
207 |
|
|
* This procedure is typically invoked by Tcl_Main or Tk_Main
|
208 |
|
|
* procedure to source an application specific rc file into the
|
209 |
|
|
* interpreter at startup time. This will either source a file
|
210 |
|
|
* in the "tcl_rcFileName" variable or a TEXT resource in the
|
211 |
|
|
* "tcl_rcRsrcName" variable.
|
212 |
|
|
*
|
213 |
|
|
* Results:
|
214 |
|
|
* None.
|
215 |
|
|
*
|
216 |
|
|
* Side effects:
|
217 |
|
|
* Depends on what's in the rc script.
|
218 |
|
|
*
|
219 |
|
|
*----------------------------------------------------------------------
|
220 |
|
|
*/
|
221 |
|
|
|
222 |
|
|
void
|
223 |
|
|
Tcl_SourceRCFile(
|
224 |
|
|
Tcl_Interp *interp) /* Interpreter to source rc file into. */
|
225 |
|
|
{
|
226 |
|
|
Tcl_DString temp;
|
227 |
|
|
char *fileName;
|
228 |
|
|
Tcl_Channel errChannel;
|
229 |
|
|
Handle h;
|
230 |
|
|
|
231 |
|
|
fileName = Tcl_GetVar(interp, "tcl_rcFileName", TCL_GLOBAL_ONLY);
|
232 |
|
|
|
233 |
|
|
if (fileName != NULL) {
|
234 |
|
|
Tcl_Channel c;
|
235 |
|
|
char *fullName;
|
236 |
|
|
|
237 |
|
|
Tcl_DStringInit(&temp);
|
238 |
|
|
fullName = Tcl_TranslateFileName(interp, fileName, &temp);
|
239 |
|
|
if (fullName == NULL) {
|
240 |
|
|
/*
|
241 |
|
|
* Couldn't translate the file name (e.g. it referred to a
|
242 |
|
|
* bogus user or there was no HOME environment variable).
|
243 |
|
|
* Just do nothing.
|
244 |
|
|
*/
|
245 |
|
|
} else {
|
246 |
|
|
|
247 |
|
|
/*
|
248 |
|
|
* Test for the existence of the rc file before trying to read it.
|
249 |
|
|
*/
|
250 |
|
|
|
251 |
|
|
c = Tcl_OpenFileChannel(NULL, fullName, "r", 0);
|
252 |
|
|
if (c != (Tcl_Channel) NULL) {
|
253 |
|
|
Tcl_Close(NULL, c);
|
254 |
|
|
if (Tcl_EvalFile(interp, fullName) != TCL_OK) {
|
255 |
|
|
errChannel = Tcl_GetStdChannel(TCL_STDERR);
|
256 |
|
|
if (errChannel) {
|
257 |
|
|
Tcl_Write(errChannel, interp->result, -1);
|
258 |
|
|
Tcl_Write(errChannel, "\n", 1);
|
259 |
|
|
}
|
260 |
|
|
}
|
261 |
|
|
}
|
262 |
|
|
}
|
263 |
|
|
Tcl_DStringFree(&temp);
|
264 |
|
|
}
|
265 |
|
|
|
266 |
|
|
fileName = Tcl_GetVar(interp, "tcl_rcRsrcName", TCL_GLOBAL_ONLY);
|
267 |
|
|
|
268 |
|
|
if (fileName != NULL) {
|
269 |
|
|
c2pstr(fileName);
|
270 |
|
|
h = GetNamedResource('TEXT', (StringPtr) fileName);
|
271 |
|
|
p2cstr((StringPtr) fileName);
|
272 |
|
|
if (h != NULL) {
|
273 |
|
|
if (Tcl_MacEvalResource(interp, fileName, 0, NULL) != TCL_OK) {
|
274 |
|
|
errChannel = Tcl_GetStdChannel(TCL_STDERR);
|
275 |
|
|
if (errChannel) {
|
276 |
|
|
Tcl_Write(errChannel, interp->result, -1);
|
277 |
|
|
Tcl_Write(errChannel, "\n", 1);
|
278 |
|
|
}
|
279 |
|
|
}
|
280 |
|
|
Tcl_ResetResult(interp);
|
281 |
|
|
ReleaseResource(h);
|
282 |
|
|
}
|
283 |
|
|
}
|
284 |
|
|
}
|