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" |
Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +0000 | [diff] [blame] | 31 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 32 | #include <Memory.h> |
| 33 | #include <Resources.h> |
Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +0000 | [diff] [blame] | 34 | #include <stdio.h> |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 35 | #include <Events.h> |
| 36 | #include <Windows.h> |
| 37 | #include <Desk.h> |
Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +0000 | [diff] [blame] | 38 | |
Jack Jansen | c76fd39 | 1995-02-02 14:27:31 +0000 | [diff] [blame] | 39 | #ifdef __MWERKS__ |
| 40 | #include <SIOUX.h> |
| 41 | #endif |
| 42 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 43 | #define STARTUP "PythonStartup" |
Jack Jansen | bac428d | 1994-12-14 13:47:30 +0000 | [diff] [blame] | 44 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 45 | extern int Py_DebugFlag; /* For parser.c, declared in pythonrun.c */ |
| 46 | extern int Py_VerboseFlag; /* For import.c, declared in pythonrun.c */ |
| 47 | extern int Py_SuppressPrintingFlag; /* For ceval.c, declared in pythonrun.c */ |
| 48 | |
| 49 | |
| 50 | /* Subroutines that live in their own file */ |
Jack Jansen | 85c9aea | 1995-10-23 13:57:03 +0000 | [diff] [blame^] | 51 | extern char *getversion Py_PROTO((void)); |
| 52 | extern char *getcopyright Py_PROTO((void)); |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | /* For getprogramname(); set by main() */ |
| 56 | static char *argv0; |
| 57 | |
| 58 | /* For getargcargv(); set by main() */ |
| 59 | static char **orig_argv; |
| 60 | static int orig_argc; |
| 61 | |
| 62 | #ifdef USE_MAC_APPLET_SUPPORT |
| 63 | /* Applet support */ |
| 64 | |
| 65 | /* Run a compiled Python Python script from 'PYC ' resource __main__ */ |
| 66 | static int |
| 67 | run_main_resource() |
| 68 | { |
| 69 | Handle h; |
| 70 | long size; |
| 71 | PyObject *code; |
| 72 | PyObject *result; |
| 73 | |
| 74 | h = GetNamedResource('PYC ', "\p__main__"); |
| 75 | if (h == NULL) { |
| 76 | Alert(NOPYC_ALERT, NULL); |
| 77 | return 1; |
| 78 | } |
| 79 | size = GetResourceSizeOnDisk(h); |
| 80 | HLock(h); |
| 81 | code = PyMarshal_ReadObjectFromString(*h + 8, (int)(size - 8)); |
| 82 | HUnlock(h); |
| 83 | ReleaseResource(h); |
| 84 | if (code == NULL) { |
| 85 | PyErr_Print(); |
| 86 | return 1; |
| 87 | } |
| 88 | result = PyImport_ExecCodeModule("__main__", code); |
| 89 | Py_DECREF(code); |
| 90 | if (result == NULL) { |
| 91 | PyErr_Print(); |
| 92 | return 1; |
| 93 | } |
| 94 | Py_DECREF(result); |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | /* Initialization sequence for applets */ |
| 99 | void |
| 100 | PyMac_InitApplet() |
| 101 | { |
Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +0000 | [diff] [blame] | 102 | int argc; |
| 103 | char **argv; |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 104 | int err; |
| 105 | |
Jack Jansen | f950f8d | 1995-02-13 11:35:34 +0000 | [diff] [blame] | 106 | #ifdef USE_MAC_SHARED_LIBRARY |
| 107 | PyMac_AddLibResources(); |
| 108 | #endif |
| 109 | #ifdef __MWERKS__ |
| 110 | SIOUXSettings.asktosaveonclose = 0; |
| 111 | SIOUXSettings.showstatusline = 0; |
| 112 | SIOUXSettings.tabspaces = 4; |
| 113 | #endif |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 114 | argc = PyMac_GetArgv(&argv); |
| 115 | Py_Initialize(); |
| 116 | PySys_SetArgv(argc, argv); |
| 117 | err = run_main_resource(); |
| 118 | fflush(stderr); |
| 119 | fflush(stdout); |
| 120 | #ifdef __MWERKS__ |
| 121 | if (!err) |
| 122 | SIOUXSettings.autocloseonquit = 1; |
Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +0000 | [diff] [blame] | 123 | else |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 124 | printf("\n[Terminated]\n"); |
| 125 | #endif |
| 126 | /* XXX Should we bother to Py_Exit(sts)? */ |
| 127 | } |
| 128 | |
| 129 | #endif /* USE_MAC_APPLET_SUPPORT */ |
| 130 | |
| 131 | /* For normal application */ |
| 132 | void |
| 133 | PyMac_InitApplication() |
| 134 | { |
| 135 | int argc; |
| 136 | char **argv; |
| 137 | |
| 138 | #ifdef USE_MAC_SHARED_LIBRARY |
| 139 | PyMac_AddLibResources(); |
| 140 | #endif |
| 141 | #ifdef __MWERKS__ |
| 142 | SIOUXSettings.asktosaveonclose = 0; |
| 143 | SIOUXSettings.showstatusline = 0; |
| 144 | SIOUXSettings.tabspaces = 4; |
| 145 | #endif |
| 146 | argc = PyMac_GetArgv(&argv); |
| 147 | if ( argc > 1 ) { |
| 148 | /* We're running a script. Attempt to change current directory */ |
| 149 | char curwd[256], *endp; |
| 150 | |
| 151 | strcpy(curwd, argv[1]); |
| 152 | endp = strrchr(curwd, ':'); |
| 153 | if ( endp && endp > curwd ) { |
| 154 | *endp = '\0'; |
| 155 | |
| 156 | chdir(curwd); |
| 157 | } |
| 158 | } |
| 159 | Py_Main(argc, argv); |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | ** PyMac_InteractiveOptions - Allow user to set options if option key is pressed |
| 164 | */ |
| 165 | void |
| 166 | PyMac_InteractiveOptions(int *inspect, int *verbose, int *suppress_print, |
| 167 | int *unbuffered, int *debugging) |
| 168 | { |
| 169 | KeyMap rmap; |
| 170 | unsigned char *map; |
| 171 | short item, type; |
| 172 | ControlHandle handle; |
| 173 | DialogPtr dialog; |
| 174 | Rect rect; |
| 175 | |
| 176 | GetKeys(rmap); |
| 177 | map = (unsigned char *)rmap; |
| 178 | if ( ( map[0x3a>>3] & (1<<(0x3a&7)) ) == 0 ) /* option key is 3a */ |
| 179 | return; |
| 180 | |
| 181 | dialog = GetNewDialog(OPT_DIALOG, NULL, (WindowPtr)-1); |
| 182 | if ( dialog == NULL ) { |
| 183 | printf("Option dialog not found - cannot set options\n"); |
| 184 | return; |
| 185 | } |
| 186 | while (1) { |
| 187 | handle = NULL; |
| 188 | ModalDialog(NULL, &item); |
| 189 | if ( item == OPT_OK ) |
| 190 | break; |
| 191 | if ( item == OPT_CANCEL ) { |
| 192 | DisposDialog(dialog); |
| 193 | exit(0); |
| 194 | } |
| 195 | #define OPT_ITEM(num, var) \ |
| 196 | if ( item == (num) ) { \ |
| 197 | *(var) = !*(var); \ |
| 198 | GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \ |
| 199 | SetCtlValue(handle, (short)*(var)); \ |
| 200 | } |
| 201 | |
| 202 | OPT_ITEM(OPT_INSPECT, inspect); |
| 203 | OPT_ITEM(OPT_VERBOSE, verbose); |
| 204 | OPT_ITEM(OPT_SUPPRESS, suppress_print); |
| 205 | OPT_ITEM(OPT_UNBUFFERED, unbuffered); |
| 206 | OPT_ITEM(OPT_DEBUGGING, debugging); |
| 207 | |
| 208 | #undef OPT_ITEM |
| 209 | } |
| 210 | DisposDialog(dialog); |
| 211 | } |
| 212 | /* Main program */ |
| 213 | |
| 214 | int |
| 215 | Py_Main(argc, argv) |
| 216 | int argc; |
| 217 | char **argv; |
| 218 | { |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 219 | int sts; |
| 220 | char *command = NULL; |
| 221 | char *filename = NULL; |
| 222 | FILE *fp = stdin; |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 223 | int inspect = 0; |
| 224 | int unbuffered = 0; |
| 225 | |
| 226 | orig_argc = argc; /* For getargcargv() */ |
| 227 | orig_argv = argv; |
| 228 | argv0 = argv[0]; /* For getprogramname() */ |
| 229 | |
| 230 | PyMac_InteractiveOptions(&inspect, &Py_VerboseFlag, &Py_SuppressPrintingFlag, |
| 231 | &unbuffered, &Py_DebugFlag); |
| 232 | |
| 233 | |
| 234 | if (unbuffered) { |
| 235 | #ifndef MPW |
| 236 | setbuf(stdout, (char *)NULL); |
| 237 | setbuf(stderr, (char *)NULL); |
| 238 | #else |
| 239 | /* On MPW (3.2) unbuffered seems to hang */ |
| 240 | setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ); |
| 241 | setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ); |
| 242 | #endif |
| 243 | } |
| 244 | |
| 245 | filename = argv[1]; |
| 246 | |
| 247 | if (Py_VerboseFlag || |
| 248 | command == NULL && filename == NULL && isatty((int)fileno(fp))) |
| 249 | fprintf(stderr, "Python %s\n%s\n", |
| 250 | getversion(), getcopyright()); |
| 251 | |
| 252 | if (filename != NULL) { |
| 253 | if ((fp = fopen(filename, "r")) == NULL) { |
| 254 | fprintf(stderr, "%s: can't open file '%s'\n", |
| 255 | argv[0], filename); |
| 256 | exit(2); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | Py_Initialize(); |
| 261 | |
| 262 | PySys_SetArgv(argc-1, argv+1); |
| 263 | |
| 264 | if (filename == NULL && isatty((int)fileno(fp))) { |
| 265 | FILE *fp = fopen(STARTUP, "r"); |
| 266 | if (fp != NULL) { |
| 267 | (void) PyRun_SimpleFile(fp, STARTUP); |
| 268 | PyErr_Clear(); |
| 269 | fclose(fp); |
| 270 | } |
| 271 | } |
| 272 | sts = PyRun_AnyFile( |
| 273 | fp, filename == NULL ? "<stdin>" : filename) != 0; |
| 274 | if (filename != NULL) |
| 275 | fclose(fp); |
| 276 | |
| 277 | if (inspect && isatty((int)fileno(stdin)) && |
| 278 | (filename != NULL || command != NULL)) |
| 279 | sts = PyRun_AnyFile(stdin, "<stdin>") != 0; |
| 280 | |
| 281 | Py_Exit(sts); |
| 282 | /*NOTREACHED*/ |
| 283 | } |
| 284 | |
| 285 | |
| 286 | /* Return the program name -- some code out there needs this. */ |
| 287 | |
| 288 | char * |
| 289 | getprogramname() |
| 290 | { |
| 291 | return argv0; |
| 292 | } |
| 293 | |
| 294 | |
| 295 | /* Make the *original* argc/argv available to other modules. |
| 296 | This is rare, but it is needed by the secureware extension. */ |
| 297 | |
| 298 | void |
| 299 | getargcargv(argc,argv) |
| 300 | int *argc; |
| 301 | char ***argv; |
| 302 | { |
| 303 | *argc = orig_argc; |
| 304 | *argv = orig_argv; |
Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +0000 | [diff] [blame] | 305 | } |