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