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 | |
| 53 | #ifdef __CFM68K__ |
| 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 | |
| 65 | /* Duplicate a string to the heap */ |
| 66 | |
| 67 | static char * |
| 68 | strdup(char *src) |
| 69 | { |
| 70 | char *dst = malloc(strlen(src) + 1); |
| 71 | if (dst) |
| 72 | strcpy(dst, src); |
| 73 | return dst; |
| 74 | } |
| 75 | |
| 76 | /* Return FSSpec of current application */ |
| 77 | |
| 78 | static OSErr |
| 79 | current_process_location(FSSpec *applicationSpec) |
| 80 | { |
| 81 | ProcessSerialNumber currentPSN; |
| 82 | ProcessInfoRec info; |
| 83 | |
| 84 | currentPSN.highLongOfPSN = 0; |
| 85 | currentPSN.lowLongOfPSN = kCurrentProcess; |
| 86 | info.processInfoLength = sizeof(ProcessInfoRec); |
| 87 | info.processName = NULL; |
| 88 | info.processAppSpec = applicationSpec; |
| 89 | return GetProcessInformation(¤tPSN, &info); |
| 90 | } |
| 91 | |
| 92 | /* Given an FSSpec, return the FSSpec of the parent folder */ |
| 93 | |
| 94 | static OSErr |
| 95 | get_folder_parent (FSSpec * fss, FSSpec * parent) |
| 96 | { |
| 97 | CInfoPBRec rec; |
| 98 | short err; |
| 99 | |
| 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; |
| 111 | } |
| 112 | |
| 113 | /* Given an FSSpec return a full, colon-separated pathname */ |
| 114 | |
| 115 | static OSErr |
| 116 | get_full_path (FSSpec *fss, char *buf) |
| 117 | { |
| 118 | short err; |
| 119 | FSSpec fss_parent, fss_current; |
| 120 | char tmpbuf[256]; |
| 121 | int plen; |
| 122 | |
| 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; |
| 138 | } |
| 139 | memcpy(tmpbuf, &fss_current.name[1], plen); |
| 140 | tmpbuf[plen] = ':'; |
| 141 | strcpy(&tmpbuf[plen+1], buf); |
| 142 | strcpy(buf, tmpbuf); |
| 143 | } |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | /* Return the full program name */ |
| 148 | |
| 149 | static char * |
| 150 | get_application_name() |
| 151 | { |
| 152 | static char appname[256]; |
| 153 | FSSpec appspec; |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 154 | |
| 155 | if (current_process_location(&appspec)) |
| 156 | return NULL; |
| 157 | if (get_full_path(&appspec, appname)) |
| 158 | return NULL; |
| 159 | return appname; |
| 160 | } |
| 161 | |
| 162 | /* Check that there aren't any args remaining in the event */ |
| 163 | |
| 164 | static OSErr |
| 165 | get_missing_params(AppleEvent *theAppleEvent) |
| 166 | { |
| 167 | DescType theType; |
| 168 | Size actualSize; |
| 169 | OSErr err; |
| 170 | |
| 171 | err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard, |
| 172 | &theType, nil, 0, &actualSize); |
| 173 | if (err == errAEDescNotFound) |
| 174 | return noErr; |
| 175 | else |
| 176 | return errAEEventNotHandled; |
| 177 | } |
| 178 | |
| 179 | static int got_one; /* Flag that we can stop getting events */ |
| 180 | |
| 181 | /* Handle the Print or Quit events (by failing) */ |
| 182 | |
| 183 | static pascal OSErr |
| 184 | handle_not(AppleEvent *theAppleEvent, AppleEvent *reply, long refCon) |
| 185 | { |
| 186 | #pragma unused (reply, refCon) |
| 187 | got_one = 1; |
| 188 | return errAEEventNotHandled; |
| 189 | } |
| 190 | |
| 191 | /* Handle the Open Application event (by ignoring it) */ |
| 192 | |
| 193 | static pascal OSErr |
| 194 | handle_open_app(AppleEvent *theAppleEvent, AppleEvent *reply, long refCon) |
| 195 | { |
| 196 | #pragma unused (reply, refCon) |
| 197 | got_one = 1; |
| 198 | return get_missing_params(theAppleEvent); |
| 199 | } |
| 200 | |
| 201 | /* Handle the Open Document event, by adding an argument */ |
| 202 | |
| 203 | static pascal OSErr |
| 204 | handle_open_doc(AppleEvent *theAppleEvent, AppleEvent *reply, long refCon) |
| 205 | { |
| 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]; |
| 214 | |
| 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); |
| 230 | } |
| 231 | return err; |
| 232 | } |
| 233 | |
| 234 | /* Install standard core event handlers */ |
| 235 | |
| 236 | static void |
| 237 | set_ae_handlers() |
| 238 | { |
| 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); |
| 247 | } |
| 248 | |
| 249 | /* Uninstall standard core event handlers */ |
| 250 | |
| 251 | static void |
| 252 | reset_ae_handlers() |
| 253 | { |
| 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); |
| 262 | } |
| 263 | |
| 264 | /* Wait for events until a core event has been handled */ |
| 265 | |
| 266 | static void |
| 267 | event_loop() |
| 268 | { |
| 269 | EventRecord event; |
| 270 | int n; |
| 271 | int ok; |
| 272 | |
| 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); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /* Initialize the Mac toolbox world */ |
| 284 | |
| 285 | static void |
| 286 | init_mac_world() |
| 287 | { |
Guido van Rossum | 6fc5aec | 1995-02-19 23:32:59 +0000 | [diff] [blame] | 288 | #ifdef THINK_C |
| 289 | printf("\n"); |
| 290 | #else |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 291 | MaxApplZone(); |
| 292 | InitGraf(&qd.thePort); |
| 293 | InitFonts(); |
| 294 | InitWindows(); |
| 295 | TEInit(); |
| 296 | InitDialogs((long)0); |
| 297 | InitMenus(); |
| 298 | InitCursor(); |
Guido van Rossum | 6fc5aec | 1995-02-19 23:32:59 +0000 | [diff] [blame] | 299 | #endif |
Guido van Rossum | dbfb282 | 1995-02-19 15:51:30 +0000 | [diff] [blame] | 300 | } |
| 301 | /* Get the argv vector, return argc */ |
| 302 | |
| 303 | int |
| 304 | PyMac_GetArgv(pargv) |
| 305 | char ***pargv; |
| 306 | { |
| 307 | init_mac_world(); |
| 308 | |
| 309 | arg_count = 0; |
| 310 | arg_vector[arg_count++] = strdup(get_application_name()); |
| 311 | |
| 312 | set_ae_handlers(); |
| 313 | event_loop(); |
| 314 | reset_ae_handlers(); |
| 315 | |
| 316 | arg_vector[arg_count] = NULL; |
| 317 | |
| 318 | *pargv = arg_vector; |
| 319 | return arg_count; |
| 320 | } |