Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 9954699 | 1995-01-08 14:33:34 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +0000 | [diff] [blame] | 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 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 25 | /* Python interpreter main program */ |
Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +0000 | [diff] [blame] | 26 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 27 | #include "Python.h" |
| 28 | #include "pythonresources.h" |
| 29 | #include "import.h" |
| 30 | #include "marshal.h" |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 31 | #include "macglue.h" |
Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +0000 | [diff] [blame] | 32 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 33 | #include <Memory.h> |
| 34 | #include <Resources.h> |
Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +0000 | [diff] [blame] | 35 | #include <stdio.h> |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 36 | #include <Events.h> |
| 37 | #include <Windows.h> |
| 38 | #include <Desk.h> |
Jack Jansen | 2429c72 | 1996-03-07 15:17:11 +0000 | [diff] [blame] | 39 | #include <Fonts.h> |
Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +0000 | [diff] [blame] | 40 | |
Jack Jansen | c76fd39 | 1995-02-02 14:27:31 +0000 | [diff] [blame] | 41 | #ifdef __MWERKS__ |
| 42 | #include <SIOUX.h> |
Jack Jansen | 1e8557a | 1995-11-10 14:51:26 +0000 | [diff] [blame] | 43 | #define USE_SIOUX |
Jack Jansen | c76fd39 | 1995-02-02 14:27:31 +0000 | [diff] [blame] | 44 | #endif |
| 45 | |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 46 | #ifdef THINK_C |
| 47 | #include <console.h> |
| 48 | #endif |
| 49 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 50 | #define STARTUP "PythonStartup" |
Jack Jansen | bac428d | 1994-12-14 13:47:30 +0000 | [diff] [blame] | 51 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 52 | extern int Py_DebugFlag; /* For parser.c, declared in pythonrun.c */ |
| 53 | extern int Py_VerboseFlag; /* For import.c, declared in pythonrun.c */ |
| 54 | extern int Py_SuppressPrintingFlag; /* For ceval.c, declared in pythonrun.c */ |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 55 | short PyMac_AppRefNum; /* RefNum of application resource fork */ |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 56 | |
| 57 | |
| 58 | /* Subroutines that live in their own file */ |
Jack Jansen | a547dca | 1996-07-10 15:48:25 +0000 | [diff] [blame] | 59 | extern char *Py_GetVersion Py_PROTO((void)); |
| 60 | extern char *Py_GetCopyright Py_PROTO((void)); |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 61 | |
| 62 | |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame^] | 63 | /* #define OBSOLETE_ARGCARGV 1 /* I think this is not needed anymore... */ |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 64 | |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame^] | 65 | #ifdef OBSOLETE_ARGCARGV |
Jack Jansen | 1d2f863 | 1996-08-02 15:16:16 +0000 | [diff] [blame] | 66 | /* For Py_GetArgcArgv(); set by main() */ |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 67 | static char **orig_argv; |
| 68 | static int orig_argc; |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame^] | 69 | #endif |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 70 | |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame^] | 71 | PyMac_PrefRecord options; |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 72 | |
Jack Jansen | 76ceece | 1996-08-19 11:18:24 +0000 | [diff] [blame] | 73 | static void Py_Main Py_PROTO((int, char **)); /* Forward */ |
| 74 | void PyMac_Exit Py_PROTO((int)); /* Forward */ |
| 75 | |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 76 | /* Initialize the Mac toolbox world */ |
| 77 | |
| 78 | static void |
| 79 | init_mac_world() |
| 80 | { |
| 81 | #ifdef THINK_C |
| 82 | printf("\n"); |
| 83 | #else |
| 84 | MaxApplZone(); |
| 85 | InitGraf(&qd.thePort); |
| 86 | InitFonts(); |
| 87 | InitWindows(); |
| 88 | TEInit(); |
| 89 | InitDialogs((long)0); |
| 90 | InitMenus(); |
| 91 | InitCursor(); |
| 92 | #endif |
| 93 | } |
| 94 | |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame^] | 95 | /* |
| 96 | ** PyMac_InteractiveOptions - Allow user to set options if option key is pressed |
| 97 | */ |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 98 | static void |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame^] | 99 | PyMac_InteractiveOptions(PyMac_PrefRecord *p, int *argcp, char ***argvp) |
| 100 | { |
| 101 | KeyMap rmap; |
| 102 | unsigned char *map; |
| 103 | short item, type; |
| 104 | ControlHandle handle; |
| 105 | DialogPtr dialog; |
| 106 | Rect rect; |
| 107 | int old_argc = *argcp; |
| 108 | int i; |
| 109 | |
| 110 | /* |
| 111 | ** If the preferences disallows interactive options we return, |
| 112 | ** similarly of <option> isn't pressed. |
| 113 | */ |
| 114 | if (p->nointopt) return; |
| 115 | |
| 116 | GetKeys(rmap); |
| 117 | map = (unsigned char *)rmap; |
| 118 | if ( ( map[0x3a>>3] & (1<<(0x3a&7)) ) == 0 ) /* option key is 3a */ |
| 119 | return; |
| 120 | |
| 121 | dialog = GetNewDialog(OPT_DIALOG, NULL, (WindowPtr)-1); |
| 122 | if ( dialog == NULL ) { |
| 123 | printf("Option dialog not found - cannot set options\n"); |
| 124 | return; |
| 125 | } |
| 126 | SetDialogDefaultItem(dialog, OPT_OK); |
| 127 | SetDialogCancelItem(dialog, OPT_CANCEL); |
| 128 | |
| 129 | /* Set default values */ |
| 130 | #define SET_OPT_ITEM(num, var) \ |
| 131 | GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \ |
| 132 | SetCtlValue(handle, (short)p->var); |
| 133 | |
| 134 | SET_OPT_ITEM(OPT_INSPECT, inspect); |
| 135 | SET_OPT_ITEM(OPT_VERBOSE, verbose); |
| 136 | SET_OPT_ITEM(OPT_SUPPRESS, suppress_print); |
| 137 | SET_OPT_ITEM(OPT_UNBUFFERED, unbuffered); |
| 138 | SET_OPT_ITEM(OPT_DEBUGGING, debugging); |
| 139 | SET_OPT_ITEM(OPT_KEEPNORMAL, keep_normal); |
| 140 | SET_OPT_ITEM(OPT_KEEPERROR, keep_error); |
| 141 | /* The rest are not settable interactively */ |
| 142 | |
| 143 | #undef SET_OPT_ITEM |
| 144 | |
| 145 | while (1) { |
| 146 | handle = NULL; |
| 147 | ModalDialog(NULL, &item); |
| 148 | if ( item == OPT_OK ) |
| 149 | break; |
| 150 | if ( item == OPT_CANCEL ) { |
| 151 | DisposDialog(dialog); |
| 152 | exit(0); |
| 153 | } |
| 154 | if ( item == OPT_CMDLINE ) { |
| 155 | int new_argc, newer_argc; |
| 156 | char **new_argv, **newer_argv; |
| 157 | |
| 158 | new_argc = ccommand(&new_argv); |
| 159 | newer_argc = (new_argc-1) + old_argc; |
| 160 | newer_argv = malloc((newer_argc+1)*sizeof(char *)); |
| 161 | if( !newer_argv ) |
| 162 | Py_FatalError("Cannot malloc argv\n"); |
| 163 | for(i=0; i<old_argc; i++) |
| 164 | newer_argv[i] = (*argvp)[i]; |
| 165 | for(i=old_argc; i<=newer_argc; i++) /* Copy the NULL too */ |
| 166 | newer_argv[i] = new_argv[i-old_argc+1]; |
| 167 | *argvp = newer_argv; |
| 168 | *argcp = newer_argc; |
| 169 | |
| 170 | /* XXXX Is it not safe to use free() here, apparently */ |
| 171 | } |
| 172 | #define OPT_ITEM(num, var) \ |
| 173 | if ( item == (num) ) { \ |
| 174 | p->var = !p->var; \ |
| 175 | GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \ |
| 176 | SetCtlValue(handle, (short)p->var); \ |
| 177 | } |
| 178 | |
| 179 | OPT_ITEM(OPT_INSPECT, inspect); |
| 180 | OPT_ITEM(OPT_VERBOSE, verbose); |
| 181 | OPT_ITEM(OPT_SUPPRESS, suppress_print); |
| 182 | OPT_ITEM(OPT_UNBUFFERED, unbuffered); |
| 183 | OPT_ITEM(OPT_DEBUGGING, debugging); |
| 184 | OPT_ITEM(OPT_KEEPNORMAL, keep_normal); |
| 185 | OPT_ITEM(OPT_KEEPERROR, keep_error); |
| 186 | |
| 187 | #undef OPT_ITEM |
| 188 | } |
| 189 | DisposDialog(dialog); |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | ** Initialization code, shared by interpreter and applets |
| 194 | */ |
| 195 | static void |
| 196 | init_common(int *argcp, char ***argvp) |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 197 | { |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 198 | /* Remember resource fork refnum, for later */ |
| 199 | PyMac_AppRefNum = CurResFile(); |
| 200 | |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 201 | /* Initialize toolboxes */ |
| 202 | init_mac_world(); |
| 203 | |
| 204 | #ifdef USE_MAC_SHARED_LIBRARY |
| 205 | /* Add the shared library to the stack of resource files */ |
| 206 | PyMac_AddLibResources(); |
| 207 | #endif |
| 208 | |
| 209 | #if defined(USE_GUSI) |
| 210 | /* Setup GUSI */ |
| 211 | GUSIDefaultSetup(); |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 212 | PyMac_SetGUSISpin(); |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 213 | PyMac_SetGUSIOptions(); |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 214 | #endif |
| 215 | |
| 216 | #ifdef USE_SIOUX |
| 217 | /* Set various SIOUX flags. Some are changed later based on options */ |
| 218 | SIOUXSettings.asktosaveonclose = 0; |
| 219 | SIOUXSettings.showstatusline = 0; |
| 220 | SIOUXSettings.tabspaces = 4; |
| 221 | #endif |
| 222 | |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame^] | 223 | /* Get options from preference file (or from applet resource fork) */ |
| 224 | options.keep_error = 1; /* default-default */ |
| 225 | PyMac_PreferenceOptions(&options); |
| 226 | |
| 227 | /* Create argc/argv. Do it before we go into the options event loop. */ |
| 228 | *argcp = PyMac_GetArgv(argvp, options.noargs); |
| 229 | |
| 230 | /* Do interactive option setting, if allowed and <option> depressed */ |
| 231 | PyMac_InteractiveOptions(&options, argcp, argvp); |
| 232 | |
| 233 | /* Copy selected options to where the machine-independent stuff wants it */ |
| 234 | Py_VerboseFlag = options.verbose; |
| 235 | Py_SuppressPrintingFlag = options.suppress_print; |
| 236 | Py_DebugFlag = options.debugging; |
| 237 | if ( options.noargs ) |
| 238 | PyMac_DoYieldEnabled = 0; |
| 239 | |
| 240 | /* Set buffering */ |
| 241 | if (options.unbuffered) { |
| 242 | #ifndef MPW |
| 243 | setbuf(stdout, (char *)NULL); |
| 244 | setbuf(stderr, (char *)NULL); |
| 245 | #else |
| 246 | /* On MPW (3.2) unbuffered seems to hang */ |
| 247 | setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ); |
| 248 | setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ); |
| 249 | #endif |
| 250 | } |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame^] | 253 | /* |
| 254 | ** Inspection mode after script/applet termination |
| 255 | */ |
| 256 | static int |
| 257 | run_inspect() |
| 258 | { |
| 259 | int sts = 0; |
| 260 | |
| 261 | if (options.inspect && isatty((int)fileno(stdin))) |
| 262 | sts = PyRun_AnyFile(stdin, "<stdin>") != 0; |
| 263 | return sts; |
| 264 | } |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 265 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 266 | #ifdef USE_MAC_APPLET_SUPPORT |
| 267 | /* Applet support */ |
| 268 | |
| 269 | /* Run a compiled Python Python script from 'PYC ' resource __main__ */ |
| 270 | static int |
| 271 | run_main_resource() |
| 272 | { |
| 273 | Handle h; |
| 274 | long size; |
| 275 | PyObject *code; |
| 276 | PyObject *result; |
| 277 | |
| 278 | h = GetNamedResource('PYC ', "\p__main__"); |
| 279 | if (h == NULL) { |
| 280 | Alert(NOPYC_ALERT, NULL); |
| 281 | return 1; |
| 282 | } |
| 283 | size = GetResourceSizeOnDisk(h); |
| 284 | HLock(h); |
| 285 | code = PyMarshal_ReadObjectFromString(*h + 8, (int)(size - 8)); |
| 286 | HUnlock(h); |
| 287 | ReleaseResource(h); |
| 288 | if (code == NULL) { |
| 289 | PyErr_Print(); |
| 290 | return 1; |
| 291 | } |
| 292 | result = PyImport_ExecCodeModule("__main__", code); |
| 293 | Py_DECREF(code); |
| 294 | if (result == NULL) { |
| 295 | PyErr_Print(); |
| 296 | return 1; |
| 297 | } |
| 298 | Py_DECREF(result); |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | /* Initialization sequence for applets */ |
| 303 | void |
| 304 | PyMac_InitApplet() |
| 305 | { |
Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +0000 | [diff] [blame] | 306 | int argc; |
| 307 | char **argv; |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 308 | int err; |
| 309 | |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame^] | 310 | init_common(&argc, &argv); |
| 311 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 312 | Py_Initialize(); |
| 313 | PySys_SetArgv(argc, argv); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame^] | 314 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 315 | err = run_main_resource(); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame^] | 316 | |
| 317 | err = (run_inspect() || err); |
| 318 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 319 | fflush(stderr); |
| 320 | fflush(stdout); |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 321 | PyMac_Exit(err); |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 322 | /* XXX Should we bother to Py_Exit(sts)? */ |
| 323 | } |
| 324 | |
| 325 | #endif /* USE_MAC_APPLET_SUPPORT */ |
| 326 | |
| 327 | /* For normal application */ |
| 328 | void |
| 329 | PyMac_InitApplication() |
| 330 | { |
| 331 | int argc; |
| 332 | char **argv; |
| 333 | |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame^] | 334 | init_common(&argc, &argv); |
| 335 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 336 | if ( argc > 1 ) { |
| 337 | /* We're running a script. Attempt to change current directory */ |
| 338 | char curwd[256], *endp; |
| 339 | |
| 340 | strcpy(curwd, argv[1]); |
| 341 | endp = strrchr(curwd, ':'); |
| 342 | if ( endp && endp > curwd ) { |
| 343 | *endp = '\0'; |
| 344 | |
| 345 | chdir(curwd); |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 346 | #ifdef USE_GUSI |
| 347 | /* Change MacOS's idea of wd too */ |
| 348 | PyMac_FixGUSIcd(); |
| 349 | #endif |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | Py_Main(argc, argv); |
| 353 | } |
| 354 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 355 | /* Main program */ |
| 356 | |
Jack Jansen | 76ceece | 1996-08-19 11:18:24 +0000 | [diff] [blame] | 357 | static void |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 358 | Py_Main(argc, argv) |
| 359 | int argc; |
| 360 | char **argv; |
| 361 | { |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 362 | int sts; |
| 363 | char *command = NULL; |
| 364 | char *filename = NULL; |
| 365 | FILE *fp = stdin; |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 366 | |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame^] | 367 | #ifdef OBSOLETE_ARGCARGV |
Jack Jansen | 1d2f863 | 1996-08-02 15:16:16 +0000 | [diff] [blame] | 368 | orig_argc = argc; /* For Py_GetArgcArgv() */ |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 369 | orig_argv = argv; |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 370 | #endif |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 371 | filename = argv[1]; |
| 372 | |
| 373 | if (Py_VerboseFlag || |
| 374 | command == NULL && filename == NULL && isatty((int)fileno(fp))) |
| 375 | fprintf(stderr, "Python %s\n%s\n", |
Jack Jansen | a547dca | 1996-07-10 15:48:25 +0000 | [diff] [blame] | 376 | Py_GetVersion(), Py_GetCopyright()); |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 377 | |
| 378 | if (filename != NULL) { |
| 379 | if ((fp = fopen(filename, "r")) == NULL) { |
| 380 | fprintf(stderr, "%s: can't open file '%s'\n", |
| 381 | argv[0], filename); |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 382 | PyMac_Exit(2); |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame^] | 386 | /* We initialize the menubar here, hoping SIOUX is initialized by now */ |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 387 | PyMac_InitMenuBar(); |
| 388 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 389 | Py_Initialize(); |
| 390 | |
| 391 | PySys_SetArgv(argc-1, argv+1); |
| 392 | |
| 393 | if (filename == NULL && isatty((int)fileno(fp))) { |
| 394 | FILE *fp = fopen(STARTUP, "r"); |
| 395 | if (fp != NULL) { |
| 396 | (void) PyRun_SimpleFile(fp, STARTUP); |
| 397 | PyErr_Clear(); |
| 398 | fclose(fp); |
| 399 | } |
| 400 | } |
| 401 | sts = PyRun_AnyFile( |
| 402 | fp, filename == NULL ? "<stdin>" : filename) != 0; |
| 403 | if (filename != NULL) |
| 404 | fclose(fp); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame^] | 405 | |
| 406 | if ( filename != NULL || command != NULL ) |
| 407 | sts = (run_inspect() || sts); |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 408 | |
| 409 | Py_Exit(sts); |
| 410 | /*NOTREACHED*/ |
| 411 | } |
| 412 | |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 413 | /* |
| 414 | ** Terminate application |
| 415 | */ |
Jack Jansen | 76ceece | 1996-08-19 11:18:24 +0000 | [diff] [blame] | 416 | void |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 417 | PyMac_Exit(status) |
| 418 | int status; |
| 419 | { |
| 420 | int keep; |
| 421 | |
| 422 | if ( status ) |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame^] | 423 | keep = options.keep_error; |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 424 | else |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame^] | 425 | keep = options.keep_normal; |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 426 | |
Jack Jansen | 1e8557a | 1995-11-10 14:51:26 +0000 | [diff] [blame] | 427 | #ifdef USE_SIOUX |
| 428 | if (keep) { |
| 429 | SIOUXSettings.standalone = 1; |
| 430 | SIOUXSettings.autocloseonquit = 0; |
Jack Jansen | 415571c | 1996-03-25 15:46:03 +0000 | [diff] [blame] | 431 | SIOUXSetTitle("\p\307terminated\310"); |
Jack Jansen | 1e8557a | 1995-11-10 14:51:26 +0000 | [diff] [blame] | 432 | } |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 433 | else |
| 434 | SIOUXSettings.autocloseonquit = 1; |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 435 | #ifdef USE_GUSI |
| 436 | /* |
| 437 | ** Workaround for Sioux/GUSI combo: we should not call |
| 438 | ** SiouxHandleOneEvent after the window is closed |
| 439 | */ |
| 440 | PyMac_ConsoleIsDead = 1; |
| 441 | #endif /* USE_GUSI */ |
| 442 | #endif /* USE_SIOUX */ |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 443 | #ifdef THINK_C |
| 444 | console_options.pause_atexit = keep; |
| 445 | #endif |
| 446 | |
| 447 | exit(status); |
| 448 | } |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 449 | |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame^] | 450 | #ifdef OBSOLETE_ARGCARGV |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 451 | /* Return the program name -- some code out there needs this. */ |
| 452 | |
| 453 | char * |
Jack Jansen | 1d2f863 | 1996-08-02 15:16:16 +0000 | [diff] [blame] | 454 | Py_GetProgramName() |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 455 | { |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame^] | 456 | return orig_argv[0]; |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | |
| 460 | /* Make the *original* argc/argv available to other modules. |
| 461 | This is rare, but it is needed by the secureware extension. */ |
| 462 | |
| 463 | void |
Jack Jansen | 1d2f863 | 1996-08-02 15:16:16 +0000 | [diff] [blame] | 464 | Py_GetArgcArgv(argc,argv) |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 465 | int *argc; |
| 466 | char ***argv; |
| 467 | { |
| 468 | *argc = orig_argc; |
| 469 | *argv = orig_argv; |
Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +0000 | [diff] [blame] | 470 | } |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame^] | 471 | #endif /* OBSOLETE_ARGCARGV */ |
Jack Jansen | 1d2f863 | 1996-08-02 15:16:16 +0000 | [diff] [blame] | 472 | |
| 473 | /* More cruft that shouldn't really be here, used in sysmodule.c */ |
| 474 | |
| 475 | char * |
| 476 | Py_GetPrefix() |
| 477 | { |
| 478 | return ""; |
| 479 | } |
| 480 | |
| 481 | char * |
| 482 | Py_GetExecPrefix() |
| 483 | { |
| 484 | return ""; |
| 485 | } |