This commit was manufactured by cvs2svn to create tag 'r22a4-fork'.
[python/dscho.git] / Mac / Python / macgetargv.c
blob8fdbad508726f26ca36abe556aae7e84b9db0ad9
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 #if UNIVERSAL_INTERFACES_VERSION >= 0x0340
49 typedef long refcontype;
50 #else
51 typedef unsigned long refcontype;
52 #endif
54 #include "Python.h"
55 #include "macglue.h"
57 #ifdef TARGET_API_MAC_OSX
58 #define PATHNAMELEN 1024
59 #else
60 #define PATHNAMELEN 256
61 #endif
63 static int arg_count;
64 static char *arg_vector[256];
65 FSSpec PyMac_ApplicationFSSpec;
66 char PyMac_ApplicationPath[PATHNAMELEN];
68 /* Duplicate a string to the heap. We also export this since it isn't standard
69 ** and others use it
71 #ifndef HAVE_STRDUP
72 char *
73 strdup(const char *src)
75 char *dst = malloc(strlen(src) + 1);
76 if (dst)
77 strcpy(dst, src);
78 return dst;
80 #endif
83 #if !TARGET_API_MAC_OSX
84 /* Initialize FSSpec and full name of current application */
86 OSErr
87 PyMac_init_process_location(void)
89 ProcessSerialNumber currentPSN;
90 ProcessInfoRec info;
91 OSErr err;
92 static int applocation_inited;
94 if ( applocation_inited ) return 0;
95 currentPSN.highLongOfPSN = 0;
96 currentPSN.lowLongOfPSN = kCurrentProcess;
97 info.processInfoLength = sizeof(ProcessInfoRec);
98 info.processName = NULL;
99 info.processAppSpec = &PyMac_ApplicationFSSpec;
100 if ( err=GetProcessInformation(&currentPSN, &info))
101 return err;
102 if ( err=PyMac_GetFullPathname(&PyMac_ApplicationFSSpec, PyMac_ApplicationPath, PATHNAMELEN) )
103 return err;
104 applocation_inited = 1;
105 return 0;
107 #endif /* !TARGET_API_MAC_OSX */
109 /* Check that there aren't any args remaining in the event */
111 static OSErr
112 get_missing_params(const AppleEvent *theAppleEvent)
114 DescType theType;
115 Size actualSize;
116 OSErr err;
118 err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
119 &theType, nil, 0, &actualSize);
120 if (err == errAEDescNotFound)
121 return noErr;
122 else
123 return errAEEventNotHandled;
126 static int got_one; /* Flag that we can stop getting events */
128 /* Handle the Print or Quit events (by failing) */
130 static pascal OSErr
131 handle_not(const AppleEvent *theAppleEvent, AppleEvent *reply, refcontype refCon)
133 #pragma unused (reply, refCon)
134 got_one = 1;
135 return errAEEventNotHandled;
138 /* Handle the Open Application event (by ignoring it) */
140 static pascal OSErr
141 handle_open_app(const AppleEvent *theAppleEvent, AppleEvent *reply, refcontype refCon)
143 #pragma unused (reply, refCon)
144 #if 0
145 /* Test by Jack: would removing this facilitate debugging? */
146 got_one = 1;
147 #endif
148 return get_missing_params(theAppleEvent);
151 /* Handle the Open Document event, by adding an argument */
153 static pascal OSErr
154 handle_open_doc(const AppleEvent *theAppleEvent, AppleEvent *reply, refcontype refCon)
156 #pragma unused (reply, refCon)
157 OSErr err;
158 AEDescList doclist;
159 AEKeyword keywd;
160 DescType rttype;
161 long i, ndocs, size;
162 FSSpec fss;
163 char path[PATHNAMELEN];
165 got_one = 1;
166 if ((err = AEGetParamDesc(theAppleEvent,
167 keyDirectObject, typeAEList, &doclist)))
168 return err;
169 if ((err = get_missing_params(theAppleEvent)))
170 return err;
171 if ((err = AECountItems(&doclist, &ndocs)))
172 return err;
173 for(i = 1; i <= ndocs; i++) {
174 err = AEGetNthPtr(&doclist, i, typeFSS,
175 &keywd, &rttype, &fss, sizeof(fss), &size);
176 if (err)
177 break;
178 PyMac_GetFullPathname(&fss, path, PATHNAMELEN);
179 arg_vector[arg_count++] = strdup(path);
181 return err;
184 /* Install standard core event handlers */
185 AEEventHandlerUPP open_doc_upp;
186 AEEventHandlerUPP open_app_upp;
187 AEEventHandlerUPP not_upp;
189 static void
190 set_ae_handlers(void)
192 open_doc_upp = NewAEEventHandlerUPP(&handle_open_doc);
193 open_app_upp = NewAEEventHandlerUPP(&handle_open_app);
194 not_upp = NewAEEventHandlerUPP(&handle_not);
196 AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
197 open_app_upp, 0L, false);
198 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
199 open_doc_upp, 0L, false);
200 AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
201 not_upp, 0L, false);
202 AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
203 not_upp, 0L, false);
206 /* Uninstall standard core event handlers */
208 static void
209 reset_ae_handlers(void)
211 AERemoveEventHandler(kCoreEventClass, kAEOpenApplication,
212 open_app_upp, false);
213 AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments,
214 open_doc_upp, false);
215 AERemoveEventHandler(kCoreEventClass, kAEPrintDocuments,
216 not_upp, false);
217 AERemoveEventHandler(kCoreEventClass, kAEQuitApplication,
218 not_upp, false);
221 /* Wait for events until a core event has been handled */
223 static void
224 event_loop(void)
226 EventRecord event;
227 int n;
228 int ok;
230 got_one = 0;
231 for (n = 0; n < 100 && !got_one; n++) {
232 #if !TARGET_API_MAC_CARBON
233 SystemTask();
234 #endif
235 ok = GetNextEvent(everyEvent, &event);
236 if (ok && event.what == kHighLevelEvent) {
237 AEProcessAppleEvent(&event);
242 /* Get the argv vector, return argc */
245 PyMac_GetArgv(char ***pargv, int noevents)
247 arg_count = 0;
248 #if TARGET_API_MAC_OSX
249 /* In an OSX bundle argv[0] is okay */
250 arg_count++;
251 #else
252 (void)PyMac_init_process_location();
253 arg_vector[arg_count++] = strdup(PyMac_ApplicationPath);
254 #endif /* TARGET_API_MAC_OSX */
256 if( !noevents ) {
257 set_ae_handlers();
258 event_loop();
259 reset_ae_handlers();
262 arg_vector[arg_count] = NULL;
264 *pargv = arg_vector;
265 return arg_count;