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