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