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 */
34 #include <Processes.h>
36 #include <AppleEvents.h>
37 #include <AEObjects.h>
48 static char *arg_vector
[256];
49 FSSpec PyMac_ApplicationFSSpec
;
50 char PyMac_ApplicationPath
[256];
51 static int applocation_inited
;
53 /* Duplicate a string to the heap. We also export this since it isn't standard
58 strdup(const char *src
)
60 char *dst
= malloc(strlen(src
) + 1);
67 /* Initialize FSSpec and full name of current application */
70 PyMac_init_process_location()
72 ProcessSerialNumber currentPSN
;
76 if ( applocation_inited
) return 0;
77 currentPSN
.highLongOfPSN
= 0;
78 currentPSN
.lowLongOfPSN
= kCurrentProcess
;
79 info
.processInfoLength
= sizeof(ProcessInfoRec
);
80 info
.processName
= NULL
;
81 info
.processAppSpec
= &PyMac_ApplicationFSSpec
;
82 if ( err
=GetProcessInformation(¤tPSN
, &info
))
84 if ( err
=PyMac_GetFullPath(&PyMac_ApplicationFSSpec
, PyMac_ApplicationPath
) )
86 applocation_inited
= 1;
90 /* Given an FSSpec, return the FSSpec of the parent folder */
93 get_folder_parent (FSSpec
* fss
, FSSpec
* parent
)
99 rec
.hFileInfo
.ioNamePtr
= parent
->name
;
100 rec
.hFileInfo
.ioVRefNum
= parent
->vRefNum
;
101 rec
.hFileInfo
.ioDirID
= parent
->parID
;
102 rec
.hFileInfo
.ioFDirIndex
= -1;
103 rec
.hFileInfo
.ioFVersNum
= 0;
104 if (err
= PBGetCatInfoSync (& rec
))
106 parent
->parID
= rec
.dirInfo
.ioDrParID
;
107 /* parent->name[0] = 0; */
111 /* Given an FSSpec return a full, colon-separated pathname */
114 PyMac_GetFullPath (FSSpec
*fss
, char *buf
)
117 FSSpec fss_parent
, fss_current
;
122 plen
= fss_current
.name
[0];
123 memcpy(buf
, &fss_current
.name
[1], plen
);
125 /* Special case for disk names */
126 if ( fss_current
.parID
<= 1 ) {
131 while (fss_current
.parID
> 1) {
132 /* Get parent folder name */
133 if (err
= get_folder_parent(&fss_current
, &fss_parent
))
135 fss_current
= fss_parent
;
136 /* Prepend path component just found to buf */
137 plen
= fss_current
.name
[0];
138 if (strlen(buf
) + plen
+ 1 > 1024) {
139 /* Oops... Not enough space (shouldn't happen) */
143 memcpy(tmpbuf
, &fss_current
.name
[1], plen
);
145 strcpy(&tmpbuf
[plen
+1], buf
);
151 /* Check that there aren't any args remaining in the event */
154 get_missing_params(const AppleEvent
*theAppleEvent
)
160 err
= AEGetAttributePtr(theAppleEvent
, keyMissedKeywordAttr
, typeWildCard
,
161 &theType
, nil
, 0, &actualSize
);
162 if (err
== errAEDescNotFound
)
165 return errAEEventNotHandled
;
168 static int got_one
; /* Flag that we can stop getting events */
170 /* Handle the Print or Quit events (by failing) */
173 handle_not(const AppleEvent
*theAppleEvent
, AppleEvent
*reply
, unsigned long refCon
)
175 #pragma unused (reply, refCon)
177 return errAEEventNotHandled
;
180 /* Handle the Open Application event (by ignoring it) */
183 handle_open_app(const AppleEvent
*theAppleEvent
, AppleEvent
*reply
, unsigned long refCon
)
185 #pragma unused (reply, refCon)
187 /* Test by Jack: would removing this facilitate debugging? */
190 return get_missing_params(theAppleEvent
);
193 /* Handle the Open Document event, by adding an argument */
196 handle_open_doc(const AppleEvent
*theAppleEvent
, AppleEvent
*reply
, unsigned long refCon
)
198 #pragma unused (reply, refCon)
208 if (err
= AEGetParamDesc(theAppleEvent
,
209 keyDirectObject
, typeAEList
, &doclist
))
211 if (err
= get_missing_params(theAppleEvent
))
213 if (err
= AECountItems(&doclist
, &ndocs
))
215 for(i
= 1; i
<= ndocs
; i
++) {
216 err
= AEGetNthPtr(&doclist
, i
, typeFSS
,
217 &keywd
, &rttype
, &fss
, sizeof(fss
), &size
);
220 PyMac_GetFullPath(&fss
, path
);
221 arg_vector
[arg_count
++] = strdup(path
);
226 /* Install standard core event handlers */
227 AEEventHandlerUPP open_doc_upp
;
228 AEEventHandlerUPP open_app_upp
;
229 AEEventHandlerUPP not_upp
;
234 open_doc_upp
= NewAEEventHandlerProc(handle_open_doc
);
235 open_app_upp
= NewAEEventHandlerProc(handle_open_app
);
236 not_upp
= NewAEEventHandlerProc(handle_not
);
238 AEInstallEventHandler(kCoreEventClass
, kAEOpenApplication
,
239 open_app_upp
, 0L, false);
240 AEInstallEventHandler(kCoreEventClass
, kAEOpenDocuments
,
241 open_doc_upp
, 0L, false);
242 AEInstallEventHandler(kCoreEventClass
, kAEPrintDocuments
,
244 AEInstallEventHandler(kCoreEventClass
, kAEQuitApplication
,
248 /* Uninstall standard core event handlers */
253 AERemoveEventHandler(kCoreEventClass
, kAEOpenApplication
,
254 open_app_upp
, false);
255 AERemoveEventHandler(kCoreEventClass
, kAEOpenDocuments
,
256 open_doc_upp
, false);
257 AERemoveEventHandler(kCoreEventClass
, kAEPrintDocuments
,
259 AERemoveEventHandler(kCoreEventClass
, kAEQuitApplication
,
263 /* Wait for events until a core event has been handled */
273 for (n
= 0; n
< 100 && !got_one
; n
++) {
274 #if !TARGET_API_MAC_CARBON
277 ok
= GetNextEvent(everyEvent
, &event
);
278 if (ok
&& event
.what
== kHighLevelEvent
) {
279 AEProcessAppleEvent(&event
);
284 /* Get the argv vector, return argc */
287 PyMac_GetArgv(pargv
, noevents
)
293 (void)PyMac_init_process_location();
294 arg_vector
[arg_count
++] = strdup(PyMac_ApplicationPath
);
302 arg_vector
[arg_count
] = NULL
;