1 /***********************************************************
2 Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* Construct argc and argv for main() by using Apple Events */
26 /* From Jack's implementation for STDWIN */
30 #ifndef SystemSevenOrLater
31 #define SystemSevenOrLater 1
38 #include <Processes.h>
40 #include <AppleEvents.h>
41 #include <AEObjects.h>
51 #ifdef GENERATINGCFM /* Defined to 0 or 1 in Universal headers */
52 #define HAVE_UNIVERSAL_HEADERS
55 #ifdef SYMANTEC__CFM68K__
59 #ifndef HAVE_UNIVERSAL_HEADERS
60 #define NewAEEventHandlerProc(x) (x)
61 #define AEEventHandlerUPP EventHandlerProcPtr
65 static char *arg_vector
[256];
66 FSSpec PyMac_ApplicationFSSpec
;
67 char PyMac_ApplicationPath
[256];
68 static int applocation_inited
;
70 /* Duplicate a string to the heap. We also export this since it isn't standard
77 char *dst
= malloc(strlen(src
) + 1);
83 /* Initialize FSSpec and full name of current application */
86 PyMac_init_process_location()
88 ProcessSerialNumber currentPSN
;
92 if ( applocation_inited
) return 0;
93 currentPSN
.highLongOfPSN
= 0;
94 currentPSN
.lowLongOfPSN
= kCurrentProcess
;
95 info
.processInfoLength
= sizeof(ProcessInfoRec
);
96 info
.processName
= NULL
;
97 info
.processAppSpec
= &PyMac_ApplicationFSSpec
;
98 if ( err
=GetProcessInformation(¤tPSN
, &info
))
100 if ( err
=PyMac_GetFullPath(&PyMac_ApplicationFSSpec
, PyMac_ApplicationPath
) )
102 applocation_inited
= 1;
106 /* Given an FSSpec, return the FSSpec of the parent folder */
109 get_folder_parent (FSSpec
* fss
, FSSpec
* parent
)
115 rec
.hFileInfo
.ioNamePtr
= parent
->name
;
116 rec
.hFileInfo
.ioVRefNum
= parent
->vRefNum
;
117 rec
.hFileInfo
.ioDirID
= parent
->parID
;
118 rec
.hFileInfo
.ioFDirIndex
= -1;
119 rec
.hFileInfo
.ioFVersNum
= 0;
120 if (err
= PBGetCatInfoSync (& rec
))
122 parent
->parID
= rec
.dirInfo
.ioDrParID
;
123 /* parent->name[0] = 0; */
127 /* Given an FSSpec return a full, colon-separated pathname */
130 PyMac_GetFullPath (FSSpec
*fss
, char *buf
)
133 FSSpec fss_parent
, fss_current
;
138 plen
= fss_current
.name
[0];
139 memcpy(buf
, &fss_current
.name
[1], plen
);
141 /* Special case for disk names */
142 if ( fss_current
.parID
<= 1 ) {
147 while (fss_current
.parID
> 1) {
148 /* Get parent folder name */
149 if (err
= get_folder_parent(&fss_current
, &fss_parent
))
151 fss_current
= fss_parent
;
152 /* Prepend path component just found to buf */
153 plen
= fss_current
.name
[0];
154 if (strlen(buf
) + plen
+ 1 > 256) {
155 /* Oops... Not enough space (shouldn't happen) */
159 memcpy(tmpbuf
, &fss_current
.name
[1], plen
);
161 strcpy(&tmpbuf
[plen
+1], buf
);
167 /* Check that there aren't any args remaining in the event */
170 get_missing_params(AppleEvent
*theAppleEvent
)
176 err
= AEGetAttributePtr(theAppleEvent
, keyMissedKeywordAttr
, typeWildCard
,
177 &theType
, nil
, 0, &actualSize
);
178 if (err
== errAEDescNotFound
)
181 return errAEEventNotHandled
;
184 static int got_one
; /* Flag that we can stop getting events */
186 /* Handle the Print or Quit events (by failing) */
189 handle_not(AppleEvent
*theAppleEvent
, AppleEvent
*reply
, long refCon
)
191 #pragma unused (reply, refCon)
193 return errAEEventNotHandled
;
196 /* Handle the Open Application event (by ignoring it) */
199 handle_open_app(AppleEvent
*theAppleEvent
, AppleEvent
*reply
, long refCon
)
201 #pragma unused (reply, refCon)
203 /* Test by Jack: would removing this facilitate debugging? */
206 return get_missing_params(theAppleEvent
);
209 /* Handle the Open Document event, by adding an argument */
212 handle_open_doc(AppleEvent
*theAppleEvent
, AppleEvent
*reply
, long refCon
)
214 #pragma unused (reply, refCon)
224 if (err
= AEGetParamDesc(theAppleEvent
,
225 keyDirectObject
, typeAEList
, &doclist
))
227 if (err
= get_missing_params(theAppleEvent
))
229 if (err
= AECountItems(&doclist
, &ndocs
))
231 for(i
= 1; i
<= ndocs
; i
++) {
232 err
= AEGetNthPtr(&doclist
, i
, typeFSS
,
233 &keywd
, &rttype
, &fss
, sizeof(fss
), &size
);
236 PyMac_GetFullPath(&fss
, path
);
237 arg_vector
[arg_count
++] = strdup(path
);
242 /* Install standard core event handlers */
243 AEEventHandlerUPP open_doc_upp
;
244 AEEventHandlerUPP open_app_upp
;
245 AEEventHandlerUPP not_upp
;
250 open_doc_upp
= NewAEEventHandlerProc(handle_open_doc
);
251 open_app_upp
= NewAEEventHandlerProc(handle_open_app
);
252 not_upp
= NewAEEventHandlerProc(handle_not
);
254 AEInstallEventHandler(kCoreEventClass
, kAEOpenApplication
,
255 open_app_upp
, 0L, false);
256 AEInstallEventHandler(kCoreEventClass
, kAEOpenDocuments
,
257 open_doc_upp
, 0L, false);
258 AEInstallEventHandler(kCoreEventClass
, kAEPrintDocuments
,
260 AEInstallEventHandler(kCoreEventClass
, kAEQuitApplication
,
264 /* Uninstall standard core event handlers */
269 AERemoveEventHandler(kCoreEventClass
, kAEOpenApplication
,
270 open_app_upp
, false);
271 AERemoveEventHandler(kCoreEventClass
, kAEOpenDocuments
,
272 open_doc_upp
, false);
273 AERemoveEventHandler(kCoreEventClass
, kAEPrintDocuments
,
275 AERemoveEventHandler(kCoreEventClass
, kAEQuitApplication
,
279 /* Wait for events until a core event has been handled */
289 for (n
= 0; n
< 100 && !got_one
; n
++) {
291 ok
= GetNextEvent(everyEvent
, &event
);
292 if (ok
&& event
.what
== kHighLevelEvent
) {
293 AEProcessAppleEvent(&event
);
298 /* Get the argv vector, return argc */
301 PyMac_GetArgv(pargv
, noevents
)
307 (void)PyMac_init_process_location();
308 arg_vector
[arg_count
++] = strdup(PyMac_ApplicationPath
);
316 arg_vector
[arg_count
] = NULL
;