Move setting of ioready 'wait' earlier in call chain, to
[python/dscho.git] / Mac / Python / macgetargv.c
blobf301bab0746da9b76059d79b63509b682bcc501c
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 #ifdef WITHOUT_FRAMEWORKS
31 #include <Types.h>
32 #include <Files.h>
33 #include <Events.h>
34 #include <Memory.h>
35 #include <Processes.h>
36 #include <Errors.h>
37 #include <AppleEvents.h>
38 #include <AEObjects.h>
39 #include <Fonts.h>
40 #include <TextEdit.h>
41 #include <Menus.h>
42 #include <Dialogs.h>
43 #include <Windows.h>
44 #else
45 #include <Carbon/Carbon.h>
46 #endif /* WITHOUT_FRAMEWORKS */
48 typedef long refcontype;
50 #include "Python.h"
51 #include "macglue.h"
53 #define PATHNAMELEN 256
55 static int arg_count;
56 static char *arg_vector[256];
57 FSSpec PyMac_ApplicationFSSpec;
58 char PyMac_ApplicationPath[PATHNAMELEN];
60 /* Duplicate a string to the heap. We also export this since it isn't standard
61 ** and others use it
63 #ifndef HAVE_STRDUP
64 char *
65 strdup(const char *src)
67 char *dst = malloc(strlen(src) + 1);
68 if (dst)
69 strcpy(dst, src);
70 return dst;
72 #endif
74 /* Initialize FSSpec and full name of current application */
76 OSErr
77 PyMac_init_process_location(void)
79 ProcessSerialNumber currentPSN;
80 ProcessInfoRec info;
81 OSErr err;
82 static int applocation_inited;
84 if ( applocation_inited ) return 0;
85 currentPSN.highLongOfPSN = 0;
86 currentPSN.lowLongOfPSN = kCurrentProcess;
87 info.processInfoLength = sizeof(ProcessInfoRec);
88 info.processName = NULL;
89 info.processAppSpec = &PyMac_ApplicationFSSpec;
90 if ( err=GetProcessInformation(&currentPSN, &info))
91 return err;
92 if ( err=PyMac_GetFullPathname(&PyMac_ApplicationFSSpec, PyMac_ApplicationPath, PATHNAMELEN) )
93 return err;
94 applocation_inited = 1;
95 return 0;
98 /* Check that there aren't any args remaining in the event */
100 static OSErr
101 get_missing_params(const AppleEvent *theAppleEvent)
103 DescType theType;
104 Size actualSize;
105 OSErr err;
107 err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
108 &theType, nil, 0, &actualSize);
109 if (err == errAEDescNotFound)
110 return noErr;
111 else
112 return errAEEventNotHandled;
115 static int got_one; /* Flag that we can stop getting events */
117 /* Handle the Print or Quit events (by failing) */
119 static pascal OSErr
120 handle_not(const AppleEvent *theAppleEvent, AppleEvent *reply, refcontype refCon)
122 #pragma unused (reply, refCon)
123 got_one = 1;
124 return errAEEventNotHandled;
127 /* Handle the Open Application event (by ignoring it) */
129 static pascal OSErr
130 handle_open_app(const AppleEvent *theAppleEvent, AppleEvent *reply, refcontype refCon)
132 #pragma unused (reply, refCon)
133 #if 0
134 /* Test by Jack: would removing this facilitate debugging? */
135 got_one = 1;
136 #endif
137 return get_missing_params(theAppleEvent);
140 /* Handle the Open Document event, by adding an argument */
142 static pascal OSErr
143 handle_open_doc(const AppleEvent *theAppleEvent, AppleEvent *reply, refcontype refCon)
145 #pragma unused (reply, refCon)
146 OSErr err;
147 AEDescList doclist;
148 AEKeyword keywd;
149 DescType rttype;
150 long i, ndocs, size;
151 FSSpec fss;
152 char path[PATHNAMELEN];
154 got_one = 1;
155 if ((err = AEGetParamDesc(theAppleEvent,
156 keyDirectObject, typeAEList, &doclist)))
157 return err;
158 if ((err = get_missing_params(theAppleEvent)))
159 return err;
160 if ((err = AECountItems(&doclist, &ndocs)))
161 return err;
162 for(i = 1; i <= ndocs; i++) {
163 err = AEGetNthPtr(&doclist, i, typeFSS,
164 &keywd, &rttype, &fss, sizeof(fss), &size);
165 if (err)
166 break;
167 PyMac_GetFullPathname(&fss, path, PATHNAMELEN);
168 arg_vector[arg_count++] = strdup(path);
170 return err;
173 /* Install standard core event handlers */
174 AEEventHandlerUPP open_doc_upp;
175 AEEventHandlerUPP open_app_upp;
176 AEEventHandlerUPP not_upp;
178 static void
179 set_ae_handlers(void)
181 open_doc_upp = NewAEEventHandlerUPP(&handle_open_doc);
182 open_app_upp = NewAEEventHandlerUPP(&handle_open_app);
183 not_upp = NewAEEventHandlerUPP(&handle_not);
185 AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
186 open_app_upp, 0L, false);
187 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
188 open_doc_upp, 0L, false);
189 AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
190 not_upp, 0L, false);
191 AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
192 not_upp, 0L, false);
195 /* Uninstall standard core event handlers */
197 static void
198 reset_ae_handlers(void)
200 AERemoveEventHandler(kCoreEventClass, kAEOpenApplication,
201 open_app_upp, false);
202 AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments,
203 open_doc_upp, false);
204 AERemoveEventHandler(kCoreEventClass, kAEPrintDocuments,
205 not_upp, false);
206 AERemoveEventHandler(kCoreEventClass, kAEQuitApplication,
207 not_upp, false);
210 /* Wait for events until a core event has been handled */
212 static void
213 event_loop(void)
215 EventRecord event;
216 int n;
217 int ok;
219 got_one = 0;
220 for (n = 0; n < 100 && !got_one; n++) {
221 ok = GetNextEvent(everyEvent, &event);
222 if (ok && event.what == kHighLevelEvent) {
223 AEProcessAppleEvent(&event);
228 /* Get the argv vector, return argc */
231 PyMac_GetArgv(char ***pargv, int noevents)
233 arg_count = 0;
234 (void)PyMac_init_process_location();
235 arg_vector[arg_count++] = strdup(PyMac_ApplicationPath);
237 if( !noevents ) {
238 set_ae_handlers();
239 event_loop();
240 reset_ae_handlers();
243 arg_vector[arg_count] = NULL;
245 *pargv = arg_vector;
246 return arg_count;