Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
| 3 | 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 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* Python interpreter main program */ |
| 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | #include "allobjects.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | a5900aa | 1991-04-16 08:42:06 +0000 | [diff] [blame] | 29 | #include "patchlevel.h" |
| 30 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 31 | #include "grammar.h" |
| 32 | #include "node.h" |
| 33 | #include "parsetok.h" |
| 34 | #include "graminit.h" |
| 35 | #include "errcode.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 36 | #include "sysmodule.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 37 | #include "compile.h" |
| 38 | #include "ceval.h" |
| 39 | #include "pythonrun.h" |
| 40 | #include "import.h" |
| 41 | |
| 42 | extern char *getpythonpath(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 43 | |
| 44 | extern grammar gram; /* From graminit.c */ |
| 45 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 46 | #ifdef DEBUG |
| 47 | int debugging; /* Needed by parser.c */ |
| 48 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 49 | |
| 50 | main(argc, argv) |
| 51 | int argc; |
| 52 | char **argv; |
| 53 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 54 | char *filename = NULL; |
| 55 | FILE *fp = stdin; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 57 | initargs(&argc, &argv); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 58 | |
| 59 | if (argc > 1 && strcmp(argv[1], "-") != 0) |
| 60 | filename = argv[1]; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 62 | if (filename != NULL) { |
| 63 | if ((fp = fopen(filename, "r")) == NULL) { |
| 64 | fprintf(stderr, "python: can't open file '%s'\n", |
| 65 | filename); |
| 66 | exit(2); |
| 67 | } |
| 68 | } |
| 69 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 70 | initall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | da0c6bd | 1990-11-18 17:28:24 +0000 | [diff] [blame] | 72 | setpythonpath(getpythonpath()); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 73 | setpythonargv(argc-1, argv+1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 75 | goaway(run(fp, filename == NULL ? "<stdin>" : filename)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 76 | /*NOTREACHED*/ |
| 77 | } |
| 78 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 79 | /* Initialize all */ |
| 80 | |
| 81 | void |
| 82 | initall() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 83 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 84 | static int inited; |
| 85 | |
| 86 | if (inited) |
| 87 | return; |
Guido van Rossum | 6c4f4a9 | 1990-12-20 23:11:33 +0000 | [diff] [blame] | 88 | inited = 1; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 89 | |
| 90 | initimport(); |
| 91 | |
Guido van Rossum | 6c4f4a9 | 1990-12-20 23:11:33 +0000 | [diff] [blame] | 92 | /* Modules 'builtin' and 'sys' are initialized here, |
| 93 | they are needed by random bits of the interpreter. |
| 94 | All other modules are optional and should be initialized |
| 95 | by the initcalls() of a specific configuration. */ |
| 96 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 97 | initbuiltin(); /* Also initializes builtin exceptions */ |
| 98 | initsys(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 99 | |
| 100 | initcalls(); /* Configuration-dependent initializations */ |
| 101 | |
| 102 | initintr(); /* For intrcheck() */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* Parse input from a file and execute it */ |
| 106 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 107 | int |
| 108 | run(fp, filename) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 109 | FILE *fp; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 110 | char *filename; |
| 111 | { |
| 112 | if (filename == NULL) |
| 113 | filename = "???"; |
| 114 | if (isatty(fileno(fp))) |
| 115 | return run_tty_loop(fp, filename); |
| 116 | else |
| 117 | return run_script(fp, filename); |
| 118 | } |
| 119 | |
| 120 | int |
| 121 | run_tty_loop(fp, filename) |
| 122 | FILE *fp; |
| 123 | char *filename; |
| 124 | { |
| 125 | object *v; |
| 126 | int ret; |
| 127 | v = sysget("ps1"); |
| 128 | if (v == NULL) { |
| 129 | sysset("ps1", v = newstringobject(">>> ")); |
| 130 | XDECREF(v); |
| 131 | } |
| 132 | v = sysget("ps2"); |
| 133 | if (v == NULL) { |
| 134 | sysset("ps2", v = newstringobject("... ")); |
| 135 | XDECREF(v); |
| 136 | } |
| 137 | for (;;) { |
| 138 | ret = run_tty_1(fp, filename); |
| 139 | #ifdef REF_DEBUG |
| 140 | fprintf(stderr, "[%ld refs]\n", ref_total); |
| 141 | #endif |
| 142 | if (ret == E_EOF) |
| 143 | return 0; |
| 144 | /* |
| 145 | if (ret == E_NOMEM) |
| 146 | return -1; |
| 147 | */ |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | int |
| 152 | run_tty_1(fp, filename) |
| 153 | FILE *fp; |
| 154 | char *filename; |
| 155 | { |
| 156 | object *m, *d, *v, *w; |
| 157 | node *n; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 158 | char *ps1, *ps2; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 159 | int err; |
| 160 | v = sysget("ps1"); |
| 161 | w = sysget("ps2"); |
| 162 | if (v != NULL && is_stringobject(v)) { |
| 163 | INCREF(v); |
| 164 | ps1 = getstringvalue(v); |
| 165 | } |
| 166 | else { |
| 167 | v = NULL; |
| 168 | ps1 = ""; |
| 169 | } |
| 170 | if (w != NULL && is_stringobject(w)) { |
| 171 | INCREF(w); |
| 172 | ps2 = getstringvalue(w); |
| 173 | } |
| 174 | else { |
| 175 | w = NULL; |
| 176 | ps2 = ""; |
| 177 | } |
| 178 | err = parsefile(fp, filename, &gram, single_input, ps1, ps2, &n); |
| 179 | XDECREF(v); |
| 180 | XDECREF(w); |
| 181 | if (err == E_EOF) |
| 182 | return E_EOF; |
| 183 | if (err != E_DONE) { |
| 184 | err_input(err); |
| 185 | print_error(); |
| 186 | return err; |
| 187 | } |
| 188 | m = add_module("__main__"); |
| 189 | if (m == NULL) |
| 190 | return -1; |
| 191 | d = getmoduledict(m); |
| 192 | v = run_node(n, filename, d, d); |
| 193 | flushline(); |
| 194 | if (v == NULL) { |
| 195 | print_error(); |
| 196 | return -1; |
| 197 | } |
| 198 | DECREF(v); |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | int |
| 203 | run_script(fp, filename) |
| 204 | FILE *fp; |
| 205 | char *filename; |
| 206 | { |
| 207 | object *m, *d, *v; |
| 208 | m = add_module("__main__"); |
| 209 | if (m == NULL) |
| 210 | return -1; |
| 211 | d = getmoduledict(m); |
| 212 | v = run_file(fp, filename, file_input, d, d); |
| 213 | flushline(); |
| 214 | if (v == NULL) { |
| 215 | print_error(); |
| 216 | return -1; |
| 217 | } |
| 218 | DECREF(v); |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | void |
| 223 | print_error() |
| 224 | { |
| 225 | object *exception, *v; |
| 226 | err_get(&exception, &v); |
| 227 | fprintf(stderr, "Unhandled exception: "); |
| 228 | printobject(exception, stderr, PRINT_RAW); |
| 229 | if (v != NULL && v != None) { |
| 230 | fprintf(stderr, ": "); |
| 231 | printobject(v, stderr, PRINT_RAW); |
| 232 | } |
| 233 | fprintf(stderr, "\n"); |
| 234 | XDECREF(exception); |
| 235 | XDECREF(v); |
| 236 | printtraceback(stderr); |
| 237 | } |
| 238 | |
| 239 | object * |
| 240 | run_string(str, start, globals, locals) |
| 241 | char *str; |
| 242 | int start; |
| 243 | /*dict*/object *globals, *locals; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 244 | { |
| 245 | node *n; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 246 | int err; |
| 247 | err = parse_string(str, start, &n); |
| 248 | return run_err_node(err, n, "<string>", globals, locals); |
| 249 | } |
| 250 | |
| 251 | object * |
| 252 | run_file(fp, filename, start, globals, locals) |
| 253 | FILE *fp; |
| 254 | char *filename; |
| 255 | int start; |
| 256 | /*dict*/object *globals, *locals; |
| 257 | { |
| 258 | node *n; |
| 259 | int err; |
| 260 | err = parse_file(fp, filename, start, &n); |
| 261 | return run_err_node(err, n, filename, globals, locals); |
| 262 | } |
| 263 | |
| 264 | object * |
| 265 | run_err_node(err, n, filename, globals, locals) |
| 266 | int err; |
| 267 | node *n; |
| 268 | char *filename; |
| 269 | /*dict*/object *globals, *locals; |
| 270 | { |
| 271 | if (err != E_DONE) { |
| 272 | err_input(err); |
| 273 | return NULL; |
| 274 | } |
| 275 | return run_node(n, filename, globals, locals); |
| 276 | } |
| 277 | |
| 278 | object * |
| 279 | run_node(n, filename, globals, locals) |
| 280 | node *n; |
| 281 | char *filename; |
| 282 | /*dict*/object *globals, *locals; |
| 283 | { |
| 284 | if (globals == NULL) { |
| 285 | globals = getglobals(); |
| 286 | if (locals == NULL) |
| 287 | locals = getlocals(); |
| 288 | } |
| 289 | else { |
| 290 | if (locals == NULL) |
| 291 | locals = globals; |
| 292 | } |
| 293 | return eval_node(n, filename, globals, locals); |
| 294 | } |
| 295 | |
| 296 | object * |
| 297 | eval_node(n, filename, globals, locals) |
| 298 | node *n; |
| 299 | char *filename; |
| 300 | object *globals; |
| 301 | object *locals; |
| 302 | { |
| 303 | codeobject *co; |
| 304 | object *v; |
| 305 | co = compile(n, filename); |
| 306 | freetree(n); |
| 307 | if (co == NULL) |
| 308 | return NULL; |
| 309 | v = eval_code(co, globals, locals, (object *)NULL); |
| 310 | DECREF(co); |
| 311 | return v; |
| 312 | } |
| 313 | |
| 314 | /* Simplified interface to parsefile */ |
| 315 | |
| 316 | int |
| 317 | parse_file(fp, filename, start, n_ret) |
| 318 | FILE *fp; |
| 319 | char *filename; |
| 320 | int start; |
| 321 | node **n_ret; |
| 322 | { |
| 323 | return parsefile(fp, filename, &gram, start, |
| 324 | (char *)0, (char *)0, n_ret); |
| 325 | } |
| 326 | |
| 327 | /* Simplified interface to parsestring */ |
| 328 | |
| 329 | int |
| 330 | parse_string(str, start, n_ret) |
| 331 | char *str; |
| 332 | int start; |
| 333 | node **n_ret; |
| 334 | { |
| 335 | int err = parsestring(str, &gram, start, n_ret); |
| 336 | /* Don't confuse early end of string with early end of input */ |
| 337 | if (err == E_EOF) |
| 338 | err = E_SYNTAX; |
| 339 | return err; |
| 340 | } |
| 341 | |
| 342 | /* Print fatal error message and abort */ |
| 343 | |
| 344 | void |
| 345 | fatal(msg) |
| 346 | char *msg; |
| 347 | { |
| 348 | fprintf(stderr, "Fatal error: %s\n", msg); |
| 349 | abort(); |
| 350 | } |
| 351 | |
| 352 | /* Clean up and exit */ |
| 353 | |
| 354 | void |
| 355 | goaway(sts) |
| 356 | int sts; |
| 357 | { |
| 358 | flushline(); |
| 359 | |
| 360 | /* XXX Call doneimport() before donecalls(), since donecalls() |
| 361 | calls wdone(), and doneimport() may close windows */ |
| 362 | doneimport(); |
| 363 | donecalls(); |
| 364 | |
| 365 | err_clear(); |
| 366 | |
| 367 | #ifdef REF_DEBUG |
| 368 | fprintf(stderr, "[%ld refs]\n", ref_total); |
| 369 | #endif |
| 370 | |
Guido van Rossum | 6c4f4a9 | 1990-12-20 23:11:33 +0000 | [diff] [blame] | 371 | #ifdef THINK_C_3_0 |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 372 | if (sts == 0) |
| 373 | Click_On(0); |
| 374 | #endif |
| 375 | |
| 376 | #ifdef TRACE_REFS |
| 377 | if (askyesno("Print left references?")) { |
Guido van Rossum | 6c4f4a9 | 1990-12-20 23:11:33 +0000 | [diff] [blame] | 378 | #ifdef THINK_C_3_0 |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 379 | Click_On(1); |
| 380 | #endif |
| 381 | printrefs(stderr); |
| 382 | } |
| 383 | #endif /* TRACE_REFS */ |
| 384 | |
| 385 | exit(sts); |
| 386 | /*NOTREACHED*/ |
| 387 | } |
| 388 | |
| 389 | static |
| 390 | finaloutput() |
| 391 | { |
| 392 | #ifdef TRACE_REFS |
| 393 | if (!askyesno("Print left references?")) |
| 394 | return; |
Guido van Rossum | 6c4f4a9 | 1990-12-20 23:11:33 +0000 | [diff] [blame] | 395 | #ifdef THINK_C_3_0 |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 396 | Click_On(1); |
Guido van Rossum | 6c4f4a9 | 1990-12-20 23:11:33 +0000 | [diff] [blame] | 397 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 398 | printrefs(stderr); |
| 399 | #endif /* TRACE_REFS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 402 | /* Ask a yes/no question */ |
| 403 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 404 | static int |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 405 | askyesno(prompt) |
| 406 | char *prompt; |
| 407 | { |
| 408 | char buf[256]; |
| 409 | |
| 410 | printf("%s [ny] ", prompt); |
| 411 | if (fgets(buf, sizeof buf, stdin) == NULL) |
| 412 | return 0; |
| 413 | return buf[0] == 'y' || buf[0] == 'Y'; |
| 414 | } |
| 415 | |
Guido van Rossum | 6c4f4a9 | 1990-12-20 23:11:33 +0000 | [diff] [blame] | 416 | #ifdef THINK_C_3_0 |
Guido van Rossum | da0c6bd | 1990-11-18 17:28:24 +0000 | [diff] [blame] | 417 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 418 | /* Check for file descriptor connected to interactive device. |
| 419 | Pretend that stdin is always interactive, other files never. */ |
| 420 | |
| 421 | int |
| 422 | isatty(fd) |
| 423 | int fd; |
| 424 | { |
| 425 | return fd == fileno(stdin); |
| 426 | } |
| 427 | |
Guido van Rossum | e6c67a7 | 1991-04-04 10:47:59 +0000 | [diff] [blame] | 428 | #ifdef macintosh |
| 429 | /* same */ |
| 430 | int |
| 431 | isatty(fd) |
| 432 | int fd; |
| 433 | { |
| 434 | return fd == fileno(stdin); |
| 435 | } |
| 436 | #endif |
| 437 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 438 | #endif |
| 439 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 440 | /* XXX WISH LIST |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 441 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 442 | - possible new types: |
| 443 | - iterator (for range, keys, ...) |
| 444 | - improve interpreter error handling, e.g., true tracebacks |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 445 | - save precompiled modules on file? |
| 446 | - fork threads, locking |
| 447 | - allow syntax extensions |
| 448 | */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 449 | |
| 450 | /* "Floccinaucinihilipilification" */ |