Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* Python interpreter main program */ |
| 2 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | #include <ctype.h> |
| 5 | #include "string.h" |
| 6 | |
Guido van Rossum | da0c6bd | 1990-11-18 17:28:24 +0000 | [diff] [blame] | 7 | extern char *getpythonpath(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8 | |
| 9 | #include "PROTO.h" |
| 10 | #include "grammar.h" |
| 11 | #include "node.h" |
| 12 | #include "parsetok.h" |
| 13 | #include "graminit.h" |
| 14 | #include "errcode.h" |
| 15 | #include "object.h" |
| 16 | #include "stringobject.h" |
| 17 | #include "sysmodule.h" |
| 18 | |
| 19 | extern grammar gram; /* From graminit.c */ |
| 20 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 21 | int debugging; |
| 22 | |
| 23 | main(argc, argv) |
| 24 | int argc; |
| 25 | char **argv; |
| 26 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 27 | char *filename = NULL; |
| 28 | FILE *fp = stdin; |
| 29 | int ret; |
Guido van Rossum | da0c6bd | 1990-11-18 17:28:24 +0000 | [diff] [blame] | 30 | |
| 31 | initargs(&argc, &argv); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | |
| 33 | initintr(); /* For intrcheck() */ |
| 34 | |
| 35 | if (argc > 1 && strcmp(argv[1], "-") != 0) |
| 36 | filename = argv[1]; |
| 37 | |
| 38 | if (filename != NULL) { |
| 39 | if ((fp = fopen(filename, "r")) == NULL) { |
| 40 | fprintf(stderr, "python: can't open file '%s'\n", |
| 41 | filename); |
| 42 | exit(2); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /* XXX what is the ideal initialization order? */ |
| 47 | |
| 48 | initsys(argc-1, argv+1); |
| 49 | inittime(); |
| 50 | initmath(); |
| 51 | |
Guido van Rossum | da0c6bd | 1990-11-18 17:28:24 +0000 | [diff] [blame] | 52 | setpythonpath(getpythonpath()); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 53 | |
| 54 | initrun(); |
Guido van Rossum | da0c6bd | 1990-11-18 17:28:24 +0000 | [diff] [blame] | 55 | initcalls(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 56 | |
| 57 | if (!isatty(fileno(fp))) { |
| 58 | ret = runfile(fp, file_input, (char *)NULL, (char *)NULL); |
| 59 | } |
| 60 | else { |
| 61 | sysset("ps1", newstringobject(">>> ")); |
| 62 | sysset("ps2", newstringobject("... ")); |
| 63 | for (;;) { |
| 64 | object *v = sysget("ps1"), *w = sysget("ps2"); |
| 65 | char *ps1 = NULL, *ps2 = NULL; |
| 66 | if (v != NULL && is_stringobject(v)) { |
| 67 | INCREF(v); |
| 68 | ps1 = getstringvalue(v); |
| 69 | } |
| 70 | else |
| 71 | v = NULL; |
| 72 | if (w != NULL && is_stringobject(w)) { |
| 73 | INCREF(w); |
| 74 | ps2 = getstringvalue(w); |
| 75 | } |
| 76 | else |
| 77 | w = NULL; |
| 78 | ret = runfile(fp, single_input, ps1, ps2); |
| 79 | if (v != NULL) |
| 80 | DECREF(v); |
| 81 | if (w != NULL) |
| 82 | DECREF(w); |
| 83 | if (ret == E_EOF || ret == E_NOMEM) |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | goaway(ret == E_DONE || ret == E_EOF ? 0 : 1); |
| 88 | /*NOTREACHED*/ |
| 89 | } |
| 90 | |
| 91 | goaway(sts) |
| 92 | int sts; |
| 93 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 94 | #ifdef THINK_C |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 95 | if (sts == 0) |
| 96 | Click_On(0); |
| 97 | #endif |
Guido van Rossum | da0c6bd | 1990-11-18 17:28:24 +0000 | [diff] [blame] | 98 | closerun(); |
| 99 | donecalls(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 100 | exit(sts); |
| 101 | /*NOTREACHED*/ |
| 102 | } |
| 103 | |
| 104 | /* Parse input from a file and execute it */ |
| 105 | |
| 106 | static int |
| 107 | runfile(fp, start, ps1, ps2) |
| 108 | FILE *fp; |
| 109 | int start; |
| 110 | char *ps1, *ps2; |
| 111 | { |
| 112 | node *n; |
| 113 | int ret; |
| 114 | ret = parsefile(fp, &gram, start, ps1, ps2, &n); |
| 115 | if (ret != E_DONE) |
| 116 | return ret; |
| 117 | return execute(n) == 0 ? E_DONE : E_ERROR; |
| 118 | } |
| 119 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 120 | /* Ask a yes/no question */ |
| 121 | |
| 122 | int |
| 123 | askyesno(prompt) |
| 124 | char *prompt; |
| 125 | { |
| 126 | char buf[256]; |
| 127 | |
| 128 | printf("%s [ny] ", prompt); |
| 129 | if (fgets(buf, sizeof buf, stdin) == NULL) |
| 130 | return 0; |
| 131 | return buf[0] == 'y' || buf[0] == 'Y'; |
| 132 | } |
| 133 | |
Guido van Rossum | da0c6bd | 1990-11-18 17:28:24 +0000 | [diff] [blame] | 134 | #ifdef THINK_C |
| 135 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 136 | /* Check for file descriptor connected to interactive device. |
| 137 | Pretend that stdin is always interactive, other files never. */ |
| 138 | |
| 139 | int |
| 140 | isatty(fd) |
| 141 | int fd; |
| 142 | { |
| 143 | return fd == fileno(stdin); |
| 144 | } |
| 145 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 146 | #endif |
| 147 | |
| 148 | /* WISH LIST |
| 149 | |
| 150 | - improved per-module error handling; different use of errno |
| 151 | - possible new types: |
| 152 | - iterator (for range, keys, ...) |
| 153 | - improve interpreter error handling, e.g., true tracebacks |
| 154 | - release parse trees when no longer needed (make them objects?) |
| 155 | - faster parser (for long modules) |
| 156 | - save precompiled modules on file? |
| 157 | - fork threads, locking |
| 158 | - allow syntax extensions |
| 159 | */ |