blob: b0eedc799ab7e3e6f5e40f9ba5d44c76c835ec48 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
2Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Python interpreter main program */
26
Guido van Rossum3f5da241990-12-20 15:06:42 +000027#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028
Guido van Rossuma5900aa1991-04-16 08:42:06 +000029#include "patchlevel.h"
30
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031#include "grammar.h"
32#include "node.h"
33#include "parsetok.h"
34#include "graminit.h"
35#include "errcode.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000036#include "sysmodule.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000037#include "compile.h"
38#include "ceval.h"
39#include "pythonrun.h"
40#include "import.h"
41
42extern char *getpythonpath();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043
44extern grammar gram; /* From graminit.c */
45
Guido van Rossum3f5da241990-12-20 15:06:42 +000046#ifdef DEBUG
47int debugging; /* Needed by parser.c */
48#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000049
50main(argc, argv)
51 int argc;
52 char **argv;
53{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054 char *filename = NULL;
55 FILE *fp = stdin;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056
Guido van Rossum3f5da241990-12-20 15:06:42 +000057 initargs(&argc, &argv);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058
59 if (argc > 1 && strcmp(argv[1], "-") != 0)
60 filename = argv[1];
Guido van Rossum3f5da241990-12-20 15:06:42 +000061
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062 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 Rossum3f5da241990-12-20 15:06:42 +000070 initall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071
Guido van Rossumda0c6bd1990-11-18 17:28:24 +000072 setpythonpath(getpythonpath());
Guido van Rossum3f5da241990-12-20 15:06:42 +000073 setpythonargv(argc-1, argv+1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074
Guido van Rossum3f5da241990-12-20 15:06:42 +000075 goaway(run(fp, filename == NULL ? "<stdin>" : filename));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076 /*NOTREACHED*/
77}
78
Guido van Rossum3f5da241990-12-20 15:06:42 +000079/* Initialize all */
80
81void
82initall()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083{
Guido van Rossum3f5da241990-12-20 15:06:42 +000084 static int inited;
85
86 if (inited)
87 return;
Guido van Rossum6c4f4a91990-12-20 23:11:33 +000088 inited = 1;
Guido van Rossum3f5da241990-12-20 15:06:42 +000089
90 initimport();
91
Guido van Rossum6c4f4a91990-12-20 23:11:33 +000092 /* 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 Rossum3f5da241990-12-20 15:06:42 +000097 initbuiltin(); /* Also initializes builtin exceptions */
98 initsys();
Guido van Rossum3f5da241990-12-20 15:06:42 +000099
100 initcalls(); /* Configuration-dependent initializations */
101
102 initintr(); /* For intrcheck() */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103}
104
105/* Parse input from a file and execute it */
106
Guido van Rossum3f5da241990-12-20 15:06:42 +0000107int
108run(fp, filename)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109 FILE *fp;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000110 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
120int
121run_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
151int
152run_tty_1(fp, filename)
153 FILE *fp;
154 char *filename;
155{
156 object *m, *d, *v, *w;
157 node *n;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000158 char *ps1, *ps2;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000159 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
202int
203run_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
222void
223print_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
239object *
240run_string(str, start, globals, locals)
241 char *str;
242 int start;
243 /*dict*/object *globals, *locals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244{
245 node *n;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000246 int err;
247 err = parse_string(str, start, &n);
248 return run_err_node(err, n, "<string>", globals, locals);
249}
250
251object *
252run_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
264object *
265run_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
278object *
279run_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
296object *
297eval_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
316int
317parse_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
329int
330parse_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
344void
345fatal(msg)
346 char *msg;
347{
348 fprintf(stderr, "Fatal error: %s\n", msg);
349 abort();
350}
351
352/* Clean up and exit */
353
354void
355goaway(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 Rossum6c4f4a91990-12-20 23:11:33 +0000371#ifdef THINK_C_3_0
Guido van Rossum3f5da241990-12-20 15:06:42 +0000372 if (sts == 0)
373 Click_On(0);
374#endif
375
376#ifdef TRACE_REFS
377 if (askyesno("Print left references?")) {
Guido van Rossum6c4f4a91990-12-20 23:11:33 +0000378#ifdef THINK_C_3_0
Guido van Rossum3f5da241990-12-20 15:06:42 +0000379 Click_On(1);
380#endif
381 printrefs(stderr);
382 }
383#endif /* TRACE_REFS */
384
385 exit(sts);
386 /*NOTREACHED*/
387}
388
389static
390finaloutput()
391{
392#ifdef TRACE_REFS
393 if (!askyesno("Print left references?"))
394 return;
Guido van Rossum6c4f4a91990-12-20 23:11:33 +0000395#ifdef THINK_C_3_0
Guido van Rossum3f5da241990-12-20 15:06:42 +0000396 Click_On(1);
Guido van Rossum6c4f4a91990-12-20 23:11:33 +0000397#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000398 printrefs(stderr);
399#endif /* TRACE_REFS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000400}
401
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000402/* Ask a yes/no question */
403
Guido van Rossum3f5da241990-12-20 15:06:42 +0000404static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405askyesno(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 Rossum6c4f4a91990-12-20 23:11:33 +0000416#ifdef THINK_C_3_0
Guido van Rossumda0c6bd1990-11-18 17:28:24 +0000417
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000418/* Check for file descriptor connected to interactive device.
419 Pretend that stdin is always interactive, other files never. */
420
421int
422isatty(fd)
423 int fd;
424{
425 return fd == fileno(stdin);
426}
427
Guido van Rossume6c67a71991-04-04 10:47:59 +0000428#ifdef macintosh
429/* same */
430int
431isatty(fd)
432 int fd;
433{
434 return fd == fileno(stdin);
435}
436#endif
437
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000438#endif
439
Guido van Rossum3f5da241990-12-20 15:06:42 +0000440/* XXX WISH LIST
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442 - possible new types:
443 - iterator (for range, keys, ...)
444 - improve interpreter error handling, e.g., true tracebacks
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000445 - save precompiled modules on file?
446 - fork threads, locking
447 - allow syntax extensions
448*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000449
450/* "Floccinaucinihilipilification" */