Fix three PyChecker-detected gotchas.
[python/dscho.git] / Mac / Python / macgetargv.c
blobbef197ca5ba9870fec6e48f353fe26276ca7bf7d
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 #include "Python.h"
45 #include "macglue.h"
47 static int arg_count;
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
54 ** and others use it
56 #ifndef HAVE_STRDUP
57 char *
58 strdup(const char *src)
60 char *dst = malloc(strlen(src) + 1);
61 if (dst)
62 strcpy(dst, src);
63 return dst;
65 #endif
67 /* Initialize FSSpec and full name of current application */
69 OSErr
70 PyMac_init_process_location()
72 ProcessSerialNumber currentPSN;
73 ProcessInfoRec info;
74 OSErr err;
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(&currentPSN, &info))
83 return err;
84 if ( err=PyMac_GetFullPath(&PyMac_ApplicationFSSpec, PyMac_ApplicationPath) )
85 return err;
86 applocation_inited = 1;
87 return 0;
90 /* Given an FSSpec, return the FSSpec of the parent folder */
92 static OSErr
93 get_folder_parent (FSSpec * fss, FSSpec * parent)
95 CInfoPBRec rec;
96 short err;
98 * parent = * fss;
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))
105 return err;
106 parent->parID = rec.dirInfo.ioDrParID;
107 /* parent->name[0] = 0; */
108 return 0;
111 /* Given an FSSpec return a full, colon-separated pathname */
113 OSErr
114 PyMac_GetFullPath (FSSpec *fss, char *buf)
116 short err;
117 FSSpec fss_parent, fss_current;
118 char tmpbuf[1024];
119 int plen;
121 fss_current = *fss;
122 plen = fss_current.name[0];
123 memcpy(buf, &fss_current.name[1], plen);
124 buf[plen] = 0;
125 /* Special case for disk names */
126 if ( fss_current.parID <= 1 ) {
127 buf[plen++] = ':';
128 buf[plen] = 0;
129 return 0;
131 while (fss_current.parID > 1) {
132 /* Get parent folder name */
133 if (err = get_folder_parent(&fss_current, &fss_parent))
134 return err;
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) */
140 *buf = 0;
141 return -1;
143 memcpy(tmpbuf, &fss_current.name[1], plen);
144 tmpbuf[plen] = ':';
145 strcpy(&tmpbuf[plen+1], buf);
146 strcpy(buf, tmpbuf);
148 return 0;
151 /* Check that there aren't any args remaining in the event */
153 static OSErr
154 get_missing_params(const AppleEvent *theAppleEvent)
156 DescType theType;
157 Size actualSize;
158 OSErr err;
160 err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
161 &theType, nil, 0, &actualSize);
162 if (err == errAEDescNotFound)
163 return noErr;
164 else
165 return errAEEventNotHandled;
168 static int got_one; /* Flag that we can stop getting events */
170 /* Handle the Print or Quit events (by failing) */
172 static pascal OSErr
173 handle_not(const AppleEvent *theAppleEvent, AppleEvent *reply, unsigned long refCon)
175 #pragma unused (reply, refCon)
176 got_one = 1;
177 return errAEEventNotHandled;
180 /* Handle the Open Application event (by ignoring it) */
182 static pascal OSErr
183 handle_open_app(const AppleEvent *theAppleEvent, AppleEvent *reply, unsigned long refCon)
185 #pragma unused (reply, refCon)
186 #if 0
187 /* Test by Jack: would removing this facilitate debugging? */
188 got_one = 1;
189 #endif
190 return get_missing_params(theAppleEvent);
193 /* Handle the Open Document event, by adding an argument */
195 static pascal OSErr
196 handle_open_doc(const AppleEvent *theAppleEvent, AppleEvent *reply, unsigned long refCon)
198 #pragma unused (reply, refCon)
199 OSErr err;
200 AEDescList doclist;
201 AEKeyword keywd;
202 DescType rttype;
203 long i, ndocs, size;
204 FSSpec fss;
205 char path[256];
207 got_one = 1;
208 if (err = AEGetParamDesc(theAppleEvent,
209 keyDirectObject, typeAEList, &doclist))
210 return err;
211 if (err = get_missing_params(theAppleEvent))
212 return err;
213 if (err = AECountItems(&doclist, &ndocs))
214 return err;
215 for(i = 1; i <= ndocs; i++) {
216 err = AEGetNthPtr(&doclist, i, typeFSS,
217 &keywd, &rttype, &fss, sizeof(fss), &size);
218 if (err)
219 break;
220 PyMac_GetFullPath(&fss, path);
221 arg_vector[arg_count++] = strdup(path);
223 return err;
226 /* Install standard core event handlers */
227 AEEventHandlerUPP open_doc_upp;
228 AEEventHandlerUPP open_app_upp;
229 AEEventHandlerUPP not_upp;
231 static void
232 set_ae_handlers()
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,
243 not_upp, 0L, false);
244 AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
245 not_upp, 0L, false);
248 /* Uninstall standard core event handlers */
250 static void
251 reset_ae_handlers()
253 AERemoveEventHandler(kCoreEventClass, kAEOpenApplication,
254 open_app_upp, false);
255 AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments,
256 open_doc_upp, false);
257 AERemoveEventHandler(kCoreEventClass, kAEPrintDocuments,
258 not_upp, false);
259 AERemoveEventHandler(kCoreEventClass, kAEQuitApplication,
260 not_upp, false);
263 /* Wait for events until a core event has been handled */
265 static void
266 event_loop()
268 EventRecord event;
269 int n;
270 int ok;
272 got_one = 0;
273 for (n = 0; n < 100 && !got_one; n++) {
274 #if !TARGET_API_MAC_CARBON
275 SystemTask();
276 #endif
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)
288 char ***pargv;
289 int noevents;
292 arg_count = 0;
293 (void)PyMac_init_process_location();
294 arg_vector[arg_count++] = strdup(PyMac_ApplicationPath);
296 if( !noevents ) {
297 set_ae_handlers();
298 event_loop();
299 reset_ae_handlers();
302 arg_vector[arg_count] = NULL;
304 *pargv = arg_vector;
305 return arg_count;