1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
/*
* tclMacExit.c --
*
* This file contains routines that deal with cleaning up various state
* when Tcl/Tk applications quit. Unfortunantly, not all state is cleaned
* up by the process when an application quites or crashes. Also you
* need to do different things depending on wether you are running as
* 68k code, PowerPC, or a code resource. The Exit handler code was
* adapted from code posted on alt.sources.mac by Dave Nebinger.
*
* Copyright (c) 1995 Dave Nebinger.
* Copyright (c) 1995-1996 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tclMacExit.c,v 1.4 1999/04/16 00:47:19 stanton Exp $
*/
#include "tclInt.h"
#include "tclMacInt.h"
#include <SegLoad.h>
#include <Traps.h>
#include <Processes.h>
/*
* Various typedefs and defines needed to patch ExitToShell.
*/
enum {
uppExitToShellProcInfo = kPascalStackBased
};
#if GENERATINGCFM
typedef UniversalProcPtr ExitToShellUPP;
#define CallExitToShellProc(userRoutine) \
CallUniversalProc((UniversalProcPtr)(userRoutine),uppExitToShellProcInfo)
#define NewExitToShellProc(userRoutine) \
(ExitToShellUPP)NewRoutineDescriptor((ProcPtr)(userRoutine), \
uppExitToShellProcInfo, GetCurrentArchitecture())
#else
typedef ExitToShellProcPtr ExitToShellUPP;
#define CallExitToShellProc(userRoutine) \
(*(userRoutine))()
#define NewExitToShellProc(userRoutine) \
(ExitToShellUPP)(userRoutine)
#endif
#define DisposeExitToShellProc(userRoutine) \
DisposeRoutineDescriptor(userRoutine)
#if defined(powerc)||defined(__powerc)
#pragma options align=mac68k
#endif
struct ExitToShellUPPList{
struct ExitToShellUPPList* nextProc;
ExitToShellUPP userProc;
};
#if defined(powerc)||defined(__powerc)
#pragma options align=reset
#endif
typedef struct ExitToShellDataStruct ExitToShellDataRec,* ExitToShellDataPtr,** ExitToShellDataHdl;
typedef struct ExitToShellUPPList ExitToShellUPPList,* ExitToShellUPPListPtr,** ExitToShellUPPHdl;
#if defined(powerc)||defined(__powerc)
#pragma options align=mac68k
#endif
struct ExitToShellDataStruct{
unsigned long a5;
ExitToShellUPPList* userProcs;
ExitToShellUPP oldProc;
};
#if defined(powerc)||defined(__powerc)
#pragma options align=reset
#endif
/*
* Static globals used within this file.
*/
static ExitToShellDataPtr gExitToShellData = (ExitToShellDataPtr) NULL;
/*
*----------------------------------------------------------------------
*
* TclPlatformExit --
*
* This procedure implements the Macintosh specific exit routine.
* We explicitly callthe ExitHandler function to do various clean
* up.
*
* Results:
* None.
*
* Side effects:
* We exit the process.
*
*----------------------------------------------------------------------
*/
void
TclpExit(
int status) /* Ignored. */
{
TclMacExitHandler();
/*
* If we are using the Metrowerks Standard Library, then we will call its exit so that it
* will get a chance to clean up temp files, and so forth. It always calls the standard
* ExitToShell, so the Tcl handlers will also get called.
*
* If you have another exit, make sure that it does not patch ExitToShell, and does
* call it. If so, it will probably work as well.
*
*/
#ifdef __MSL__
exit(status);
#else
ExitToShell();
#endif
}
/*
*----------------------------------------------------------------------
*
* TclMacExitHandler --
*
* This procedure is invoked after Tcl at the last possible moment
* to clean up any state Tcl has left around that may cause other
* applications to crash. For example, this function can be used
* as the termination routine for CFM applications.
*
* Results:
* None.
*
* Side effects:
* Various cleanup occurs.
*
*----------------------------------------------------------------------
*/
void
TclMacExitHandler()
{
ExitToShellUPPListPtr curProc;
/*
* Loop through all installed Exit handlers
* and call them. Always make sure we are in
* a clean state in case we are recursivly called.
*/
if ((gExitToShellData) != NULL && (gExitToShellData->userProcs != NULL)){
/*
* Call the installed exit to shell routines.
*/
curProc = gExitToShellData->userProcs;
do {
gExitToShellData->userProcs = curProc->nextProc;
CallExitToShellProc(curProc->userProc);
DisposeExitToShellProc(curProc->userProc);
DisposePtr((Ptr) curProc);
curProc = gExitToShellData->userProcs;
} while (curProc != (ExitToShellUPPListPtr) NULL);
}
return;
}
/*
*----------------------------------------------------------------------
*
* TclMacInstallExitToShellPatch --
*
* This procedure installs a way to clean up state at the latest
* possible moment before we exit. These are things that must
* be cleaned up or the system will crash. The exact way in which
* this is implemented depends on the architecture in which we are
* running. For 68k applications we patch the ExitToShell call.
* For PowerPC applications we just create a list of procs to call.
* The function ExitHandler should be installed in the Code
* Fragments terminiation routine.
*
* Results:
* None.
*
* Side effects:
* Installs the new routine.
*
*----------------------------------------------------------------------
*/
OSErr
TclMacInstallExitToShellPatch(
ExitToShellProcPtr newProc) /* Function pointer. */
{
ExitToShellUPP exitHandler;
ExitToShellUPPListPtr listPtr;
if (gExitToShellData == (ExitToShellDataPtr) NULL){
TclMacInitExitToShell(true);
}
/*
* Add the passed in function pointer to the list of functions
* to be called when ExitToShell is called.
*/
exitHandler = NewExitToShellProc(newProc);
listPtr = (ExitToShellUPPListPtr) NewPtrClear(sizeof(ExitToShellUPPList));
listPtr->userProc = exitHandler;
listPtr->nextProc = gExitToShellData->userProcs;
gExitToShellData->userProcs = listPtr;
return noErr;
}
/*
*----------------------------------------------------------------------
*
* ExitToShellPatchRoutine --
*
* This procedure is invoked when someone calls ExitToShell for
* this application. This function performs some last miniute
* clean up and then calls the real ExitToShell routine.
*
* Results:
* None.
*
* Side effects:
* Various cleanup occurs.
*
*----------------------------------------------------------------------
*/
static pascal void
ExitToShellPatchRoutine()
{
ExitToShellUPP oldETS;
long oldA5;
/*
* Set up our A5 world. This allows us to have
* access to our global variables in the 68k world.
*/
oldA5 = SetCurrentA5();
SetA5(gExitToShellData->a5);
/*
* Call the function that invokes all
* of the handlers.
*/
TclMacExitHandler();
/*
* Call the origional ExitToShell routine.
*/
oldETS = gExitToShellData->oldProc;
DisposePtr((Ptr) gExitToShellData);
SetA5(oldA5);
CallExitToShellProc(oldETS);
return;
}
/*
*----------------------------------------------------------------------
*
* TclMacInitExitToShell --
*
* This procedure initializes the ExitToShell clean up machanism.
* Generally, this is handled automatically when users make a call
* to InstallExitToShellPatch. However, it can be called
* explicitly at startup time to turn off the patching mechanism.
* This can be used by code resources which could be removed from
* the application before ExitToShell is called.
*
* Note, if we are running from CFM code we never install the
* patch. Instead, the function ExitHandler should be installed
* as the terminiation routine for the code fragment.
*
* Results:
* None.
*
* Side effects:
* Creates global state.
*
*----------------------------------------------------------------------
*/
void
TclMacInitExitToShell(
int usePatch) /* True if on 68k. */
{
if (gExitToShellData == (ExitToShellDataPtr) NULL){
#if GENERATINGCFM
gExitToShellData = (ExitToShellDataPtr)
NewPtr(sizeof(ExitToShellDataRec));
gExitToShellData->a5 = SetCurrentA5();
gExitToShellData->userProcs = (ExitToShellUPPList*) NULL;
#else
ExitToShellUPP oldExitToShell, newExitToShellPatch;
short exitToShellTrap;
/*
* Initialize patch mechanism.
*/
gExitToShellData = (ExitToShellDataPtr) NewPtr(sizeof(ExitToShellDataRec));
gExitToShellData->a5 = SetCurrentA5();
gExitToShellData->userProcs = (ExitToShellUPPList*) NULL;
/*
* Save state needed to call origional ExitToShell routine. Install
* the new ExitToShell code in it's place.
*/
if (usePatch) {
exitToShellTrap = _ExitToShell & 0x3ff;
newExitToShellPatch = NewExitToShellProc(ExitToShellPatchRoutine);
oldExitToShell = (ExitToShellUPP)
NGetTrapAddress(exitToShellTrap, ToolTrap);
NSetTrapAddress((UniversalProcPtr) newExitToShellPatch,
exitToShellTrap, ToolTrap);
gExitToShellData->oldProc = oldExitToShell;
}
#endif
}
}
|