Added 'list_only' option (and modified 'run()' to respect it).
[python/dscho.git] / Mac / Python / macgetargv.c
bloba0efa17306382ac6cfe4f0e3f8d0ae0e71a2cf04
1 /***********************************************************
2 Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
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 */
28 #include <stdlib.h>
30 #ifndef SystemSevenOrLater
31 #define SystemSevenOrLater 1
32 #endif
34 #include <Types.h>
35 #include <Files.h>
36 #include <Events.h>
37 #include <Memory.h>
38 #include <Processes.h>
39 #include <Errors.h>
40 #include <AppleEvents.h>
41 #include <AEObjects.h>
42 #include <Fonts.h>
43 #include <TextEdit.h>
44 #include <Menus.h>
45 #include <Dialogs.h>
46 #include <Windows.h>
48 #include "Python.h"
49 #include "macglue.h"
51 #ifdef GENERATINGCFM /* Defined to 0 or 1 in Universal headers */
52 #define HAVE_UNIVERSAL_HEADERS
53 #endif
55 #ifdef SYMANTEC__CFM68K__
56 #pragma lib_export on
57 #endif
59 #ifndef HAVE_UNIVERSAL_HEADERS
60 #define NewAEEventHandlerProc(x) (x)
61 #define AEEventHandlerUPP EventHandlerProcPtr
62 #endif
64 static int arg_count;
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
71 ** and others use it
74 char *
75 strdup(char *src)
77 char *dst = malloc(strlen(src) + 1);
78 if (dst)
79 strcpy(dst, src);
80 return dst;
83 /* Initialize FSSpec and full name of current application */
85 OSErr
86 PyMac_init_process_location()
88 ProcessSerialNumber currentPSN;
89 ProcessInfoRec info;
90 OSErr err;
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(&currentPSN, &info))
99 return err;
100 if ( err=PyMac_GetFullPath(&PyMac_ApplicationFSSpec, PyMac_ApplicationPath) )
101 return err;
102 applocation_inited = 1;
103 return 0;
106 /* Given an FSSpec, return the FSSpec of the parent folder */
108 static OSErr
109 get_folder_parent (FSSpec * fss, FSSpec * parent)
111 CInfoPBRec rec;
112 short err;
114 * parent = * fss;
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))
121 return err;
122 parent->parID = rec.dirInfo.ioDrParID;
123 /* parent->name[0] = 0; */
124 return 0;
127 /* Given an FSSpec return a full, colon-separated pathname */
129 OSErr
130 PyMac_GetFullPath (FSSpec *fss, char *buf)
132 short err;
133 FSSpec fss_parent, fss_current;
134 char tmpbuf[256];
135 int plen;
137 fss_current = *fss;
138 plen = fss_current.name[0];
139 memcpy(buf, &fss_current.name[1], plen);
140 buf[plen] = 0;
141 /* Special case for disk names */
142 if ( fss_current.parID <= 1 ) {
143 buf[plen++] = ':';
144 buf[plen] = 0;
145 return 0;
147 while (fss_current.parID > 1) {
148 /* Get parent folder name */
149 if (err = get_folder_parent(&fss_current, &fss_parent))
150 return err;
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) */
156 *buf = 0;
157 return -1;
159 memcpy(tmpbuf, &fss_current.name[1], plen);
160 tmpbuf[plen] = ':';
161 strcpy(&tmpbuf[plen+1], buf);
162 strcpy(buf, tmpbuf);
164 return 0;
167 /* Check that there aren't any args remaining in the event */
169 static OSErr
170 get_missing_params(AppleEvent *theAppleEvent)
172 DescType theType;
173 Size actualSize;
174 OSErr err;
176 err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
177 &theType, nil, 0, &actualSize);
178 if (err == errAEDescNotFound)
179 return noErr;
180 else
181 return errAEEventNotHandled;
184 static int got_one; /* Flag that we can stop getting events */
186 /* Handle the Print or Quit events (by failing) */
188 static pascal OSErr
189 handle_not(AppleEvent *theAppleEvent, AppleEvent *reply, long refCon)
191 #pragma unused (reply, refCon)
192 got_one = 1;
193 return errAEEventNotHandled;
196 /* Handle the Open Application event (by ignoring it) */
198 static pascal OSErr
199 handle_open_app(AppleEvent *theAppleEvent, AppleEvent *reply, long refCon)
201 #pragma unused (reply, refCon)
202 #if 0
203 /* Test by Jack: would removing this facilitate debugging? */
204 got_one = 1;
205 #endif
206 return get_missing_params(theAppleEvent);
209 /* Handle the Open Document event, by adding an argument */
211 static pascal OSErr
212 handle_open_doc(AppleEvent *theAppleEvent, AppleEvent *reply, long refCon)
214 #pragma unused (reply, refCon)
215 OSErr err;
216 AEDescList doclist;
217 AEKeyword keywd;
218 DescType rttype;
219 long i, ndocs, size;
220 FSSpec fss;
221 char path[256];
223 got_one = 1;
224 if (err = AEGetParamDesc(theAppleEvent,
225 keyDirectObject, typeAEList, &doclist))
226 return err;
227 if (err = get_missing_params(theAppleEvent))
228 return err;
229 if (err = AECountItems(&doclist, &ndocs))
230 return err;
231 for(i = 1; i <= ndocs; i++) {
232 err = AEGetNthPtr(&doclist, i, typeFSS,
233 &keywd, &rttype, &fss, sizeof(fss), &size);
234 if (err)
235 break;
236 PyMac_GetFullPath(&fss, path);
237 arg_vector[arg_count++] = strdup(path);
239 return err;
242 /* Install standard core event handlers */
243 AEEventHandlerUPP open_doc_upp;
244 AEEventHandlerUPP open_app_upp;
245 AEEventHandlerUPP not_upp;
247 static void
248 set_ae_handlers()
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,
259 not_upp, 0L, false);
260 AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
261 not_upp, 0L, false);
264 /* Uninstall standard core event handlers */
266 static void
267 reset_ae_handlers()
269 AERemoveEventHandler(kCoreEventClass, kAEOpenApplication,
270 open_app_upp, false);
271 AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments,
272 open_doc_upp, false);
273 AERemoveEventHandler(kCoreEventClass, kAEPrintDocuments,
274 not_upp, false);
275 AERemoveEventHandler(kCoreEventClass, kAEQuitApplication,
276 not_upp, false);
279 /* Wait for events until a core event has been handled */
281 static void
282 event_loop()
284 EventRecord event;
285 int n;
286 int ok;
288 got_one = 0;
289 for (n = 0; n < 100 && !got_one; n++) {
290 SystemTask();
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)
302 char ***pargv;
303 int noevents;
306 arg_count = 0;
307 (void)PyMac_init_process_location();
308 arg_vector[arg_count++] = strdup(PyMac_ApplicationPath);
310 if( !noevents ) {
311 set_ae_handlers();
312 event_loop();
313 reset_ae_handlers();
316 arg_vector[arg_count] = NULL;
318 *pargv = arg_vector;
319 return arg_count;