Don't reference removed files in Makefile
[python/dscho.git] / Mac / Python / macgetargv.c
blob66c6d59f218c025330443d815d0f204c46712213
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 __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 */
67 static char *
68 strdup(char *src)
70 char *dst = malloc(strlen(src) + 1);
71 if (dst)
72 strcpy(dst, src);
73 return dst;
76 /* Return FSSpec of current application */
78 static OSErr
79 current_process_location(FSSpec *applicationSpec)
81 ProcessSerialNumber currentPSN;
82 ProcessInfoRec info;
84 currentPSN.highLongOfPSN = 0;
85 currentPSN.lowLongOfPSN = kCurrentProcess;
86 info.processInfoLength = sizeof(ProcessInfoRec);
87 info.processName = NULL;
88 info.processAppSpec = applicationSpec;
89 return GetProcessInformation(&currentPSN, &info);
92 /* Given an FSSpec, return the FSSpec of the parent folder */
94 static OSErr
95 get_folder_parent (FSSpec * fss, FSSpec * parent)
97 CInfoPBRec rec;
98 short err;
100 * parent = * fss;
101 rec.hFileInfo.ioNamePtr = parent->name;
102 rec.hFileInfo.ioVRefNum = parent->vRefNum;
103 rec.hFileInfo.ioDirID = parent->parID;
104 rec.hFileInfo.ioFDirIndex = -1;
105 rec.hFileInfo.ioFVersNum = 0;
106 if (err = PBGetCatInfoSync (& rec))
107 return err;
108 parent->parID = rec.dirInfo.ioDrParID;
109 /* parent->name[0] = 0; */
110 return 0;
113 /* Given an FSSpec return a full, colon-separated pathname */
115 static OSErr
116 get_full_path (FSSpec *fss, char *buf)
118 short err;
119 FSSpec fss_parent, fss_current;
120 char tmpbuf[256];
121 int plen;
123 fss_current = *fss;
124 plen = fss_current.name[0];
125 memcpy(buf, &fss_current.name[1], plen);
126 buf[plen] = 0;
127 while (fss_current.parID > 1) {
128 /* Get parent folder name */
129 if (err = get_folder_parent(&fss_current, &fss_parent))
130 return err;
131 fss_current = fss_parent;
132 /* Prepend path component just found to buf */
133 plen = fss_current.name[0];
134 if (strlen(buf) + plen + 1 > 256) {
135 /* Oops... Not enough space (shouldn't happen) */
136 *buf = 0;
137 return -1;
139 memcpy(tmpbuf, &fss_current.name[1], plen);
140 tmpbuf[plen] = ':';
141 strcpy(&tmpbuf[plen+1], buf);
142 strcpy(buf, tmpbuf);
144 return 0;
147 /* Return the full program name */
149 static char *
150 get_application_name()
152 static char appname[256];
153 FSSpec appspec;
155 if (current_process_location(&appspec))
156 return NULL;
157 if (get_full_path(&appspec, appname))
158 return NULL;
159 return appname;
162 /* Check that there aren't any args remaining in the event */
164 static OSErr
165 get_missing_params(AppleEvent *theAppleEvent)
167 DescType theType;
168 Size actualSize;
169 OSErr err;
171 err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
172 &theType, nil, 0, &actualSize);
173 if (err == errAEDescNotFound)
174 return noErr;
175 else
176 return errAEEventNotHandled;
179 static int got_one; /* Flag that we can stop getting events */
181 /* Handle the Print or Quit events (by failing) */
183 static pascal OSErr
184 handle_not(AppleEvent *theAppleEvent, AppleEvent *reply, long refCon)
186 #pragma unused (reply, refCon)
187 got_one = 1;
188 return errAEEventNotHandled;
191 /* Handle the Open Application event (by ignoring it) */
193 static pascal OSErr
194 handle_open_app(AppleEvent *theAppleEvent, AppleEvent *reply, long refCon)
196 #pragma unused (reply, refCon)
197 got_one = 1;
198 return get_missing_params(theAppleEvent);
201 /* Handle the Open Document event, by adding an argument */
203 static pascal OSErr
204 handle_open_doc(AppleEvent *theAppleEvent, AppleEvent *reply, long refCon)
206 #pragma unused (reply, refCon)
207 OSErr err;
208 AEDescList doclist;
209 AEKeyword keywd;
210 DescType rttype;
211 long i, ndocs, size;
212 FSSpec fss;
213 char path[256];
215 got_one = 1;
216 if (err = AEGetParamDesc(theAppleEvent,
217 keyDirectObject, typeAEList, &doclist))
218 return err;
219 if (err = get_missing_params(theAppleEvent))
220 return err;
221 if (err = AECountItems(&doclist, &ndocs))
222 return err;
223 for(i = 1; i <= ndocs; i++) {
224 err = AEGetNthPtr(&doclist, i, typeFSS,
225 &keywd, &rttype, &fss, sizeof(fss), &size);
226 if (err)
227 break;
228 get_full_path(&fss, path);
229 arg_vector[arg_count++] = strdup(path);
231 return err;
234 /* Install standard core event handlers */
236 static void
237 set_ae_handlers()
239 AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
240 NewAEEventHandlerProc(handle_open_app), 0L, false);
241 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
242 NewAEEventHandlerProc(handle_open_doc), 0L, false);
243 AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
244 NewAEEventHandlerProc(handle_not), 0L, false);
245 AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
246 NewAEEventHandlerProc(handle_not), 0L, false);
249 /* Uninstall standard core event handlers */
251 static void
252 reset_ae_handlers()
254 AERemoveEventHandler(kCoreEventClass, kAEOpenApplication,
255 NewAEEventHandlerProc(handle_open_app), false);
256 AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments,
257 NewAEEventHandlerProc(handle_open_doc), false);
258 AERemoveEventHandler(kCoreEventClass, kAEPrintDocuments,
259 NewAEEventHandlerProc(handle_not), false);
260 AERemoveEventHandler(kCoreEventClass, kAEQuitApplication,
261 NewAEEventHandlerProc(handle_not), false);
264 /* Wait for events until a core event has been handled */
266 static void
267 event_loop()
269 EventRecord event;
270 int n;
271 int ok;
273 got_one = 0;
274 for (n = 0; n < 100 && !got_one; n++) {
275 SystemTask();
276 ok = GetNextEvent(everyEvent, &event);
277 if (ok && event.what == kHighLevelEvent) {
278 AEProcessAppleEvent(&event);
283 /* Initialize the Mac toolbox world */
285 static void
286 init_mac_world()
288 #ifdef THINK_C
289 printf("\n");
290 #else
291 MaxApplZone();
292 InitGraf(&qd.thePort);
293 InitFonts();
294 InitWindows();
295 TEInit();
296 InitDialogs((long)0);
297 InitMenus();
298 InitCursor();
299 #endif
301 /* Get the argv vector, return argc */
304 PyMac_GetArgv(pargv)
305 char ***pargv;
307 init_mac_world();
309 arg_count = 0;
310 arg_vector[arg_count++] = strdup(get_application_name());
312 set_ae_handlers();
313 event_loop();
314 reset_ae_handlers();
316 arg_vector[arg_count] = NULL;
318 *pargv = arg_vector;
319 return arg_count;