blob: 90cdb5a698df873237ea4585c8dd7c8c7df6d7bd [file] [log] [blame]
Guido van Rossumdbfb2821995-02-19 15:51:30 +00001/***********************************************************
2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
25/* Construct argc and argv for main() by using Apple Events */
26/* From Jack's implementation for STDWIN */
27
28#include <stdlib.h>
29
30#ifndef SystemSevenOrLater
31#define SystemSevenOrLater 1
32#endif
33
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>
Guido van Rossum6fc5aec1995-02-19 23:32:59 +000044#include <TextEdit.h>
45#include <Menus.h>
46#include <Dialogs.h>
47#include <Windows.h>
Guido van Rossumdbfb2821995-02-19 15:51:30 +000048
49#ifdef GENERATINGCFM /* Defined to 0 or 1 in Universal headers */
50#define HAVE_UNIVERSAL_HEADERS
51#endif
52
Jack Jansenf74f63a1995-06-27 13:18:14 +000053#ifdef SYMANTEC__CFM68K__
Guido van Rossumdbfb2821995-02-19 15:51:30 +000054#pragma lib_export on
55#endif
56
57#ifndef HAVE_UNIVERSAL_HEADERS
58#define NewAEEventHandlerProc(x) (x)
59#define AEEventHandlerUPP EventHandlerProcPtr
60#endif
61
62static int arg_count;
63static char *arg_vector[256];
64
Jack Jansen178652b1995-10-12 10:22:57 +000065/* Duplicate a string to the heap. We also export this since it isn't standard
66** and others use it
67*/
Guido van Rossumdbfb2821995-02-19 15:51:30 +000068
Jack Jansen178652b1995-10-12 10:22:57 +000069char *
Guido van Rossumdbfb2821995-02-19 15:51:30 +000070strdup(char *src)
71{
72 char *dst = malloc(strlen(src) + 1);
73 if (dst)
74 strcpy(dst, src);
75 return dst;
76}
77
78/* Return FSSpec of current application */
79
Jack Jansen41fa7ea1995-08-31 13:59:36 +000080OSErr
81PyMac_process_location(FSSpec *applicationSpec)
Guido van Rossumdbfb2821995-02-19 15:51:30 +000082{
83 ProcessSerialNumber currentPSN;
84 ProcessInfoRec info;
85
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);
92}
93
94/* Given an FSSpec, return the FSSpec of the parent folder */
95
96static OSErr
97get_folder_parent (FSSpec * fss, FSSpec * parent)
98{
99 CInfoPBRec rec;
100 short err;
101
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;
113}
114
115/* Given an FSSpec return a full, colon-separated pathname */
116
117static OSErr
118get_full_path (FSSpec *fss, char *buf)
119{
120 short err;
121 FSSpec fss_parent, fss_current;
122 char tmpbuf[256];
123 int plen;
124
Jack Jansenf74f63a1995-06-27 13:18:14 +0000125#if defined(__MWERKS__) && defined(__CFM68K__)
126 return -1; /* get_folder_parent doesn't work */
127#endif
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000128 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;
143 }
144 memcpy(tmpbuf, &fss_current.name[1], plen);
145 tmpbuf[plen] = ':';
146 strcpy(&tmpbuf[plen+1], buf);
147 strcpy(buf, tmpbuf);
148 }
149 return 0;
150}
151
152/* Return the full program name */
153
154static char *
155get_application_name()
156{
157 static char appname[256];
158 FSSpec appspec;
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000159
Jack Jansen41fa7ea1995-08-31 13:59:36 +0000160 if (PyMac_process_location(&appspec))
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000161 return NULL;
162 if (get_full_path(&appspec, appname))
163 return NULL;
164 return appname;
165}
166
167/* Check that there aren't any args remaining in the event */
168
169static OSErr
170get_missing_params(AppleEvent *theAppleEvent)
171{
172 DescType theType;
173 Size actualSize;
174 OSErr err;
175
176 err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
177 &theType, nil, 0, &actualSize);
178 if (err == errAEDescNotFound)
179 return noErr;
180 else
181 return errAEEventNotHandled;
182}
183
184static int got_one; /* Flag that we can stop getting events */
185
186/* Handle the Print or Quit events (by failing) */
187
188static pascal OSErr
189handle_not(AppleEvent *theAppleEvent, AppleEvent *reply, long refCon)
190{
191 #pragma unused (reply, refCon)
192 got_one = 1;
193 return errAEEventNotHandled;
194}
195
196/* Handle the Open Application event (by ignoring it) */
197
198static pascal OSErr
199handle_open_app(AppleEvent *theAppleEvent, AppleEvent *reply, long refCon)
200{
201 #pragma unused (reply, refCon)
Jack Jansendbe75ae1995-11-10 14:54:16 +0000202#if 0
203 /* Test by Jack: would removing this facilitate debugging? */
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000204 got_one = 1;
Jack Jansendbe75ae1995-11-10 14:54:16 +0000205#endif
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000206 return get_missing_params(theAppleEvent);
207}
208
209/* Handle the Open Document event, by adding an argument */
210
211static pascal OSErr
212handle_open_doc(AppleEvent *theAppleEvent, AppleEvent *reply, long refCon)
213{
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];
222
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);
238 }
239 return err;
240}
241
242/* Install standard core event handlers */
Jack Jansencc456fb1995-07-29 13:50:59 +0000243AEEventHandlerUPP open_doc_upp;
244AEEventHandlerUPP open_app_upp;
245AEEventHandlerUPP not_upp;
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000246
247static void
248set_ae_handlers()
249{
Jack Jansencc456fb1995-07-29 13:50:59 +0000250 open_doc_upp = NewAEEventHandlerProc(handle_open_doc);
251 open_app_upp = NewAEEventHandlerProc(handle_open_app);
252 not_upp = NewAEEventHandlerProc(handle_not);
253
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000254 AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000255 open_app_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000256 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000257 open_doc_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000258 AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000259 not_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000260 AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000261 not_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000262}
263
264/* Uninstall standard core event handlers */
265
266static void
267reset_ae_handlers()
268{
269 AERemoveEventHandler(kCoreEventClass, kAEOpenApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000270 open_app_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000271 AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000272 open_doc_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000273 AERemoveEventHandler(kCoreEventClass, kAEPrintDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000274 not_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000275 AERemoveEventHandler(kCoreEventClass, kAEQuitApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000276 not_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000277}
278
279/* Wait for events until a core event has been handled */
280
281static void
282event_loop()
283{
284 EventRecord event;
285 int n;
286 int ok;
287
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);
294 }
295 }
296}
297
298/* Initialize the Mac toolbox world */
299
300static void
301init_mac_world()
302{
Guido van Rossum6fc5aec1995-02-19 23:32:59 +0000303#ifdef THINK_C
304 printf("\n");
305#else
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000306 MaxApplZone();
307 InitGraf(&qd.thePort);
308 InitFonts();
309 InitWindows();
310 TEInit();
311 InitDialogs((long)0);
312 InitMenus();
313 InitCursor();
Guido van Rossum6fc5aec1995-02-19 23:32:59 +0000314#endif
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000315}
316/* Get the argv vector, return argc */
317
318int
319PyMac_GetArgv(pargv)
320 char ***pargv;
321{
322 init_mac_world();
323
324 arg_count = 0;
325 arg_vector[arg_count++] = strdup(get_application_name());
326
327 set_ae_handlers();
328 event_loop();
329 reset_ae_handlers();
330
331 arg_vector[arg_count] = NULL;
332
333 *pargv = arg_vector;
334 return arg_count;
335}