py-cvs-rel2_1 (Rev 1.2) merge
[python/dscho.git] / Mac / Python / macgetargv.c
blob1bd57ad5606813bcf324819177b41968d6e2be8f
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 #include <Types.h>
31 #include <Files.h>
32 #include <Events.h>
33 #include <Memory.h>
34 #include <Processes.h>
35 #include <Errors.h>
36 #include <AppleEvents.h>
37 #include <AEObjects.h>
38 #include <Fonts.h>
39 #include <TextEdit.h>
40 #include <Menus.h>
41 #include <Dialogs.h>
42 #include <Windows.h>
44 #if UNIVERSAL_INTERFACES_VERSION >= 0x0340
45 typedef long refcontype;
46 #else
47 typedef unsigned long refcontype;
48 #endif
50 #include "Python.h"
51 #include "macglue.h"
53 static int arg_count;
54 static char *arg_vector[256];
55 FSSpec PyMac_ApplicationFSSpec;
56 char PyMac_ApplicationPath[256];
57 static int applocation_inited;
59 /* Duplicate a string to the heap. We also export this since it isn't standard
60 ** and others use it
62 #ifndef HAVE_STRDUP
63 char *
64 strdup(const char *src)
66 char *dst = malloc(strlen(src) + 1);
67 if (dst)
68 strcpy(dst, src);
69 return dst;
71 #endif
73 /* Initialize FSSpec and full name of current application */
75 OSErr
76 PyMac_init_process_location()
78 ProcessSerialNumber currentPSN;
79 ProcessInfoRec info;
80 OSErr err;
82 if ( applocation_inited ) return 0;
83 currentPSN.highLongOfPSN = 0;
84 currentPSN.lowLongOfPSN = kCurrentProcess;
85 info.processInfoLength = sizeof(ProcessInfoRec);
86 info.processName = NULL;
87 info.processAppSpec = &PyMac_ApplicationFSSpec;
88 if ( err=GetProcessInformation(&currentPSN, &info))
89 return err;
90 if ( err=PyMac_GetFullPath(&PyMac_ApplicationFSSpec, PyMac_ApplicationPath) )
91 return err;
92 applocation_inited = 1;
93 return 0;
96 /* Check that there aren't any args remaining in the event */
98 static OSErr
99 get_missing_params(const AppleEvent *theAppleEvent)
101 DescType theType;
102 Size actualSize;
103 OSErr err;
105 err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
106 &theType, nil, 0, &actualSize);
107 if (err == errAEDescNotFound)
108 return noErr;
109 else
110 return errAEEventNotHandled;
113 static int got_one; /* Flag that we can stop getting events */
115 /* Handle the Print or Quit events (by failing) */
117 static pascal OSErr
118 handle_not(const AppleEvent *theAppleEvent, AppleEvent *reply, refcontype refCon)
120 #pragma unused (reply, refCon)
121 got_one = 1;
122 return errAEEventNotHandled;
125 /* Handle the Open Application event (by ignoring it) */
127 static pascal OSErr
128 handle_open_app(const AppleEvent *theAppleEvent, AppleEvent *reply, refcontype refCon)
130 #pragma unused (reply, refCon)
131 #if 0
132 /* Test by Jack: would removing this facilitate debugging? */
133 got_one = 1;
134 #endif
135 return get_missing_params(theAppleEvent);
138 /* Handle the Open Document event, by adding an argument */
140 static pascal OSErr
141 handle_open_doc(const AppleEvent *theAppleEvent, AppleEvent *reply, refcontype refCon)
143 #pragma unused (reply, refCon)
144 OSErr err;
145 AEDescList doclist;
146 AEKeyword keywd;
147 DescType rttype;
148 long i, ndocs, size;
149 FSSpec fss;
150 char path[256];
152 got_one = 1;
153 if (err = AEGetParamDesc(theAppleEvent,
154 keyDirectObject, typeAEList, &doclist))
155 return err;
156 if (err = get_missing_params(theAppleEvent))
157 return err;
158 if (err = AECountItems(&doclist, &ndocs))
159 return err;
160 for(i = 1; i <= ndocs; i++) {
161 err = AEGetNthPtr(&doclist, i, typeFSS,
162 &keywd, &rttype, &fss, sizeof(fss), &size);
163 if (err)
164 break;
165 PyMac_GetFullPath(&fss, path);
166 arg_vector[arg_count++] = strdup(path);
168 return err;
171 /* Install standard core event handlers */
172 AEEventHandlerUPP open_doc_upp;
173 AEEventHandlerUPP open_app_upp;
174 AEEventHandlerUPP not_upp;
176 static void
177 set_ae_handlers()
179 open_doc_upp = NewAEEventHandlerUPP(&handle_open_doc);
180 open_app_upp = NewAEEventHandlerUPP(&handle_open_app);
181 not_upp = NewAEEventHandlerUPP(&handle_not);
183 AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
184 open_app_upp, 0L, false);
185 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
186 open_doc_upp, 0L, false);
187 AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
188 not_upp, 0L, false);
189 AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
190 not_upp, 0L, false);
193 /* Uninstall standard core event handlers */
195 static void
196 reset_ae_handlers()
198 AERemoveEventHandler(kCoreEventClass, kAEOpenApplication,
199 open_app_upp, false);
200 AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments,
201 open_doc_upp, false);
202 AERemoveEventHandler(kCoreEventClass, kAEPrintDocuments,
203 not_upp, false);
204 AERemoveEventHandler(kCoreEventClass, kAEQuitApplication,
205 not_upp, false);
208 /* Wait for events until a core event has been handled */
210 static void
211 event_loop()
213 EventRecord event;
214 int n;
215 int ok;
217 got_one = 0;
218 for (n = 0; n < 100 && !got_one; n++) {
219 #if !TARGET_API_MAC_CARBON
220 SystemTask();
221 #endif
222 ok = GetNextEvent(everyEvent, &event);
223 if (ok && event.what == kHighLevelEvent) {
224 AEProcessAppleEvent(&event);
229 /* Get the argv vector, return argc */
232 PyMac_GetArgv(pargv, noevents)
233 char ***pargv;
234 int noevents;
237 arg_count = 0;
238 (void)PyMac_init_process_location();
239 arg_vector[arg_count++] = strdup(PyMac_ApplicationPath);
241 if( !noevents ) {
242 set_ae_handlers();
243 event_loop();
244 reset_ae_handlers();
247 arg_vector[arg_count] = NULL;
249 *pargv = arg_vector;
250 return arg_count;