Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 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. |
| 14 | |
| 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. |
| 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 Rossum | 6fc5aec | 1995-02-19 23:32:59 +0000 | [diff] [blame] | 44 | #include <TextEdit.h> |
| 45 | #include <Menus.h> |
| 46 | #include <Dialogs.h> |
| 47 | #include <Windows.h> |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 48 | |
| 49 | #ifdef GENERATINGCFM /* Defined to 0 or 1 in Universal headers */ |
| 50 | #define HAVE_UNIVERSAL_HEADERS |
| 51 | #endif |
| 52 | |
Jack Jansen | f74f63a | 1995-06-27 13:18:14 +0000 | [diff] [blame] | 53 | #ifdef SYMANTEC__CFM68K__ |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 54 | #pragma lib_export on |
| 55 | #endif |
| 56 | |
| 57 | #ifndef HAVE_UNIVERSAL_HEADERS |
| 58 | #define NewAEEventHandlerProc(x) (x) |
| 59 | #define AEEventHandlerUPP EventHandlerProcPtr |
| 60 | #endif |
| 61 | |
| 62 | static int arg_count; |
| 63 | static char *arg_vector[256]; |
| 64 | |
Jack Jansen | 178652b | 1995-10-12 10:22:57 +0000 | [diff] [blame] | 65 | /* Duplicate a string to the heap. We also export this since it isn't standard |
| 66 | ** and others use it |
| 67 | */ |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 68 | |
Jack Jansen | 178652b | 1995-10-12 10:22:57 +0000 | [diff] [blame] | 69 | char * |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 70 | strdup(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 Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 80 | OSErr |
| 81 | PyMac_process_location(FSSpec *applicationSpec) |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 82 | { |
| 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(¤tPSN, &info); |
| 92 | } |
| 93 | |
| 94 | /* Given an FSSpec, return the FSSpec of the parent folder */ |
| 95 | |
| 96 | static OSErr |
| 97 | get_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 | |
| 117 | static OSErr |
| 118 | get_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 Jansen | f74f63a | 1995-06-27 13:18:14 +0000 | [diff] [blame] | 125 | #if defined(__MWERKS__) && defined(__CFM68K__) |
| 126 | return -1; /* get_folder_parent doesn't work */ |
| 127 | #endif |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 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; |
| 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 | |
| 154 | static char * |
| 155 | get_application_name() |
| 156 | { |
| 157 | static char appname[256]; |
| 158 | FSSpec appspec; |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 159 | |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 160 | if (PyMac_process_location(&appspec)) |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 161 | 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 | |
| 169 | static OSErr |
| 170 | get_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 | |
| 184 | static int got_one; /* Flag that we can stop getting events */ |
| 185 | |
| 186 | /* Handle the Print or Quit events (by failing) */ |
| 187 | |
| 188 | static pascal OSErr |
| 189 | handle_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 | |
| 198 | static pascal OSErr |
| 199 | handle_open_app(AppleEvent *theAppleEvent, AppleEvent *reply, long refCon) |
| 200 | { |
| 201 | #pragma unused (reply, refCon) |
Jack Jansen | dbe75ae | 1995-11-10 14:54:16 +0000 | [diff] [blame] | 202 | #if 0 |
| 203 | /* Test by Jack: would removing this facilitate debugging? */ |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 204 | got_one = 1; |
Jack Jansen | dbe75ae | 1995-11-10 14:54:16 +0000 | [diff] [blame] | 205 | #endif |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 206 | return get_missing_params(theAppleEvent); |
| 207 | } |
| 208 | |
| 209 | /* Handle the Open Document event, by adding an argument */ |
| 210 | |
| 211 | static pascal OSErr |
| 212 | handle_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 Jansen | cc456fb | 1995-07-29 13:50:59 +0000 | [diff] [blame] | 243 | AEEventHandlerUPP open_doc_upp; |
| 244 | AEEventHandlerUPP open_app_upp; |
| 245 | AEEventHandlerUPP not_upp; |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 246 | |
| 247 | static void |
| 248 | set_ae_handlers() |
| 249 | { |
Jack Jansen | cc456fb | 1995-07-29 13:50:59 +0000 | [diff] [blame] | 250 | open_doc_upp = NewAEEventHandlerProc(handle_open_doc); |
| 251 | open_app_upp = NewAEEventHandlerProc(handle_open_app); |
| 252 | not_upp = NewAEEventHandlerProc(handle_not); |
| 253 | |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 254 | AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, |
Jack Jansen | cc456fb | 1995-07-29 13:50:59 +0000 | [diff] [blame] | 255 | open_app_upp, 0L, false); |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 256 | AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, |
Jack Jansen | cc456fb | 1995-07-29 13:50:59 +0000 | [diff] [blame] | 257 | open_doc_upp, 0L, false); |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 258 | AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, |
Jack Jansen | cc456fb | 1995-07-29 13:50:59 +0000 | [diff] [blame] | 259 | not_upp, 0L, false); |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 260 | AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, |
Jack Jansen | cc456fb | 1995-07-29 13:50:59 +0000 | [diff] [blame] | 261 | not_upp, 0L, false); |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /* Uninstall standard core event handlers */ |
| 265 | |
| 266 | static void |
| 267 | reset_ae_handlers() |
| 268 | { |
| 269 | AERemoveEventHandler(kCoreEventClass, kAEOpenApplication, |
Jack Jansen | cc456fb | 1995-07-29 13:50:59 +0000 | [diff] [blame] | 270 | open_app_upp, false); |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 271 | AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments, |
Jack Jansen | cc456fb | 1995-07-29 13:50:59 +0000 | [diff] [blame] | 272 | open_doc_upp, false); |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 273 | AERemoveEventHandler(kCoreEventClass, kAEPrintDocuments, |
Jack Jansen | cc456fb | 1995-07-29 13:50:59 +0000 | [diff] [blame] | 274 | not_upp, false); |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 275 | AERemoveEventHandler(kCoreEventClass, kAEQuitApplication, |
Jack Jansen | cc456fb | 1995-07-29 13:50:59 +0000 | [diff] [blame] | 276 | not_upp, false); |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /* Wait for events until a core event has been handled */ |
| 280 | |
| 281 | static void |
| 282 | event_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 | |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 298 | /* Get the argv vector, return argc */ |
| 299 | |
| 300 | int |
| 301 | PyMac_GetArgv(pargv) |
| 302 | char ***pargv; |
| 303 | { |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 304 | |
| 305 | arg_count = 0; |
| 306 | arg_vector[arg_count++] = strdup(get_application_name()); |
| 307 | |
| 308 | set_ae_handlers(); |
| 309 | event_loop(); |
| 310 | reset_ae_handlers(); |
| 311 | |
| 312 | arg_vector[arg_count] = NULL; |
| 313 | |
| 314 | *pargv = arg_vector; |
| 315 | return arg_count; |
| 316 | } |