Added ref to Misc/NEWS file; added hint to run regen script on Linux.
[python/dscho.git] / Mac / Python / macgetargv.c
blob90cdb5a698df873237ea4585c8dd7c8c7df6d7bd
1 /***********************************************************
2 Copyright 1991-1995 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 <Desk.h>
43 #include <Fonts.h>
44 #include <TextEdit.h>
45 #include <Menus.h>
46 #include <Dialogs.h>
47 #include <Windows.h>
49 #ifdef GENERATINGCFM /* Defined to 0 or 1 in Universal headers */
50 #define HAVE_UNIVERSAL_HEADERS
51 #endif
53 #ifdef SYMANTEC__CFM68K__
54 #pragma lib_export on
55 #endif
57 #ifndef HAVE_UNIVERSAL_HEADERS
58 #define NewAEEventHandlerProc(x) (x)
59 #define AEEventHandlerUPP EventHandlerProcPtr
60 #endif
62 static int arg_count;
63 static char *arg_vector[256];
65 /* Duplicate a string to the heap. We also export this since it isn't standard
66 ** and others use it
69 char *
70 strdup(char *src)
72 char *dst = malloc(strlen(src) + 1);
73 if (dst)
74 strcpy(dst, src);
75 return dst;
78 /* Return FSSpec of current application */
80 OSErr
81 PyMac_process_location(FSSpec *applicationSpec)
83 ProcessSerialNumber currentPSN;
84 ProcessInfoRec info;
86 currentPSN.highLongOfPSN = 0;
87 currentPSN.lowLongOfPSN = kCurrentProcess;
88 info.processInfoLength = sizeof(ProcessInfoRec);
89 info.processName = NULL;
90 info.processAppSpec = applicationSpec;
91 return GetProcessInformation(&currentPSN, &info);
94 /* Given an FSSpec, return the FSSpec of the parent folder */
96 static OSErr
97 get_folder_parent (FSSpec * fss, FSSpec * parent)
99 CInfoPBRec rec;
100 short err;
102 * parent = * fss;
103 rec.hFileInfo.ioNamePtr = parent->name;
104 rec.hFileInfo.ioVRefNum = parent->vRefNum;
105 rec.hFileInfo.ioDirID = parent->parID;
106 rec.hFileInfo.ioFDirIndex = -1;
107 rec.hFileInfo.ioFVersNum = 0;
108 if (err = PBGetCatInfoSync (& rec))
109 return err;
110 parent->parID = rec.dirInfo.ioDrParID;
111 /* parent->name[0] = 0; */
112 return 0;
115 /* Given an FSSpec return a full, colon-separated pathname */
117 static OSErr
118 get_full_path (FSSpec *fss, char *buf)
120 short err;
121 FSSpec fss_parent, fss_current;
122 char tmpbuf[256];
123 int plen;
125 #if defined(__MWERKS__) && defined(__CFM68K__)
126 return -1; /* get_folder_parent doesn't work */
127 #endif
128 fss_current = *fss;
129 plen = fss_current.name[0];
130 memcpy(buf, &fss_current.name[1], plen);
131 buf[plen] = 0;
132 while (fss_current.parID > 1) {
133 /* Get parent folder name */
134 if (err = get_folder_parent(&fss_current, &fss_parent))
135 return err;
136 fss_current = fss_parent;
137 /* Prepend path component just found to buf */
138 plen = fss_current.name[0];
139 if (strlen(buf) + plen + 1 > 256) {
140 /* Oops... Not enough space (shouldn't happen) */
141 *buf = 0;
142 return -1;
144 memcpy(tmpbuf, &fss_current.name[1], plen);
145 tmpbuf[plen] = ':';
146 strcpy(&tmpbuf[plen+1], buf);
147 strcpy(buf, tmpbuf);
149 return 0;
152 /* Return the full program name */
154 static char *
155 get_application_name()
157 static char appname[256];
158 FSSpec appspec;
160 if (PyMac_process_location(&appspec))
161 return NULL;
162 if (get_full_path(&appspec, appname))
163 return NULL;
164 return appname;
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 get_full_path(&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 /* Initialize the Mac toolbox world */
300 static void
301 init_mac_world()
303 #ifdef THINK_C
304 printf("\n");
305 #else
306 MaxApplZone();
307 InitGraf(&qd.thePort);
308 InitFonts();
309 InitWindows();
310 TEInit();
311 InitDialogs((long)0);
312 InitMenus();
313 InitCursor();
314 #endif
316 /* Get the argv vector, return argc */
319 PyMac_GetArgv(pargv)
320 char ***pargv;
322 init_mac_world();
324 arg_count = 0;
325 arg_vector[arg_count++] = strdup(get_application_name());
327 set_ae_handlers();
328 event_loop();
329 reset_ae_handlers();
331 arg_vector[arg_count] = NULL;
333 *pargv = arg_vector;
334 return arg_count;