blob: c0214fc1e584f11e31a4b261980afcc269b950db [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum1984f1e1992-08-04 12:41:02 +00004
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
25/* Python interpreter top-level routines, including init/exit */
26
27#include "allobjects.h"
28
29#include "grammar.h"
30#include "node.h"
31#include "parsetok.h"
32#include "graminit.h"
33#include "errcode.h"
34#include "sysmodule.h"
Guido van Rossum6135a871995-01-09 17:53:26 +000035#include "bltinmodule.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000036#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000037#include "eval.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000038#include "ceval.h"
39#include "pythonrun.h"
40#include "import.h"
Guido van Rossumfdef2711994-09-14 13:31:04 +000041#include "marshal.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000042
Guido van Rossuma110aa61994-08-29 12:50:44 +000043#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000044#include <signal.h>
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000045#endif
46
Guido van Rossum42a51241995-01-30 12:52:46 +000047#ifdef THINK_C
48#include <console.h>
49#endif
50
Jack Jansen08e767b1995-02-02 14:30:20 +000051#ifdef __MWERKS__
52#include <SIOUX.h>
53#endif
54
Guido van Rossum1984f1e1992-08-04 12:41:02 +000055extern char *getpythonpath();
56
57extern grammar gram; /* From graminit.c */
58
Guido van Rossumb73cc041993-11-01 16:28:59 +000059/* Forward */
Guido van Rossum6135a871995-01-09 17:53:26 +000060static void initmain PROTO((void));
Guido van Rossuma110aa61994-08-29 12:50:44 +000061static object *run_err_node PROTO((node *n, char *filename,
Guido van Rossumb73cc041993-11-01 16:28:59 +000062 object *globals, object *locals));
63static object *run_node PROTO((node *n, char *filename,
64 object *globals, object *locals));
Guido van Rossuma110aa61994-08-29 12:50:44 +000065static void err_input PROTO((perrdetail *));
Guido van Rossumb376a4a1993-11-23 17:53:17 +000066static void initsigs PROTO((void));
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000067
Guido van Rossum1984f1e1992-08-04 12:41:02 +000068int debugging; /* Needed by parser.c */
69int verbose; /* Needed by import.c */
Guido van Rossuma110aa61994-08-29 12:50:44 +000070int suppress_print; /* Needed by ceval.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000071
72/* Initialize all */
73
74void
75initall()
76{
77 static int inited;
78
79 if (inited)
80 return;
81 inited = 1;
82
83 initimport();
84
Guido van Rossuma110aa61994-08-29 12:50:44 +000085 /* Modules '__builtin__' and 'sys' are initialized here,
Guido van Rossum1984f1e1992-08-04 12:41:02 +000086 they are needed by random bits of the interpreter.
87 All other modules are optional and are initialized
88 when they are first imported. */
89
90 initbuiltin(); /* Also initializes builtin exceptions */
91 initsys();
Guido van Rossum1984f1e1992-08-04 12:41:02 +000092
93 setpythonpath(getpythonpath());
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000094
95 initsigs(); /* Signal handling stuff, including initintr() */
Guido van Rossum6135a871995-01-09 17:53:26 +000096
97 initmain();
98}
99
100/* Create __main__ module */
101
102static void
103initmain()
104{
105 object *m, *d;
106 m = add_module("__main__");
107 if (m == NULL)
108 fatal("can't create __main__ module");
109 d = getmoduledict(m);
110 if (dictlookup(d, "__builtins__") == NULL) {
Guido van Rossum94390ec1995-01-12 11:37:57 +0000111 if (dictinsert(d, "__builtins__", getbuiltins()))
Guido van Rossum6135a871995-01-09 17:53:26 +0000112 fatal("can't add __builtins__ to __main__");
113 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000114}
115
116/* Parse input from a file and execute it */
117
118int
119run(fp, filename)
120 FILE *fp;
121 char *filename;
122{
123 if (filename == NULL)
124 filename = "???";
125 if (isatty((int)fileno(fp)))
126 return run_tty_loop(fp, filename);
127 else
128 return run_script(fp, filename);
129}
130
131int
132run_tty_loop(fp, filename)
133 FILE *fp;
134 char *filename;
135{
136 object *v;
137 int ret;
138 v = sysget("ps1");
139 if (v == NULL) {
140 sysset("ps1", v = newstringobject(">>> "));
141 XDECREF(v);
142 }
143 v = sysget("ps2");
144 if (v == NULL) {
145 sysset("ps2", v = newstringobject("... "));
146 XDECREF(v);
147 }
148 for (;;) {
149 ret = run_tty_1(fp, filename);
150#ifdef REF_DEBUG
151 fprintf(stderr, "[%ld refs]\n", ref_total);
152#endif
153 if (ret == E_EOF)
154 return 0;
155 /*
156 if (ret == E_NOMEM)
157 return -1;
158 */
159 }
160}
161
162int
163run_tty_1(fp, filename)
164 FILE *fp;
165 char *filename;
166{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000167 object *m, *d, *v, *w;
168 node *n;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000169 perrdetail err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000170 char *ps1, *ps2;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000171 v = sysget("ps1");
172 w = sysget("ps2");
173 if (v != NULL && is_stringobject(v)) {
174 INCREF(v);
175 ps1 = getstringvalue(v);
176 }
177 else {
178 v = NULL;
179 ps1 = "";
180 }
181 if (w != NULL && is_stringobject(w)) {
182 INCREF(w);
183 ps2 = getstringvalue(w);
184 }
185 else {
186 w = NULL;
187 ps2 = "";
188 }
Guido van Rossumff4949e1992-08-05 19:58:53 +0000189 BGN_SAVE
Guido van Rossuma110aa61994-08-29 12:50:44 +0000190 n = parsefile(fp, filename, &gram, single_input, ps1, ps2, &err);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000191 END_SAVE
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000192 XDECREF(v);
193 XDECREF(w);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000194 if (n == NULL) {
195 if (err.error == E_EOF) {
196 if (err.text)
197 free(err.text);
198 return E_EOF;
199 }
200 err_input(&err);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000201 print_error();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000202 return err.error;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000203 }
204 m = add_module("__main__");
205 if (m == NULL)
206 return -1;
207 d = getmoduledict(m);
208 v = run_node(n, filename, d, d);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000209 if (v == NULL) {
210 print_error();
211 return -1;
212 }
213 DECREF(v);
Guido van Rossum6d023c91995-01-04 19:12:13 +0000214 flushline();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000215 return 0;
216}
217
218int
219run_script(fp, filename)
220 FILE *fp;
221 char *filename;
222{
223 object *m, *d, *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000224 char *ext;
225
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000226 m = add_module("__main__");
227 if (m == NULL)
228 return -1;
229 d = getmoduledict(m);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000230 ext = filename + strlen(filename) - 4;
231 if ( strcmp(ext, ".pyc") == 0 ) {
232 /* Try to run a pyc file. First, re-open in binary */
Jack Jansene00637b1994-12-14 12:58:37 +0000233 /* Don't close, done in main: fclose(fp); */
Guido van Rossumfdef2711994-09-14 13:31:04 +0000234 if( (fp = fopen(filename, "rb")) == NULL ) {
235 fprintf(stderr, "python: Can't reopen .pyc file\n");
236 return -1;
237 }
238 v = run_pyc_file(fp, filename, d, d);
239 } else {
240 v = run_file(fp, filename, file_input, d, d);
241 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000242 if (v == NULL) {
243 print_error();
244 return -1;
245 }
246 DECREF(v);
Guido van Rossum6d023c91995-01-04 19:12:13 +0000247 flushline();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000248 return 0;
249}
250
251int
252run_command(command)
253 char *command;
254{
255 object *m, *d, *v;
256 m = add_module("__main__");
257 if (m == NULL)
258 return -1;
259 d = getmoduledict(m);
260 v = run_string(command, file_input, d, d);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000261 if (v == NULL) {
262 print_error();
263 return -1;
264 }
265 DECREF(v);
Guido van Rossum6d023c91995-01-04 19:12:13 +0000266 flushline();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000267 return 0;
268}
269
270void
271print_error()
272{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000273 object *exception, *v, *tb, *f;
274 err_fetch(&exception, &v, &tb);
Guido van Rossum6d023c91995-01-04 19:12:13 +0000275 flushline();
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000276 if (exception == NULL)
277 fatal("print_error called but no exception");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000278 if (exception == SystemExit) {
279 if (v == NULL || v == None)
280 goaway(0);
281 if (is_intobject(v))
282 goaway((int)getintvalue(v));
283 else {
Guido van Rossum3165fe61992-09-25 21:59:05 +0000284 /* OK to use real stderr here */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000285 printobject(v, stderr, PRINT_RAW);
286 fprintf(stderr, "\n");
287 goaway(1);
288 }
289 }
290 sysset("last_type", exception);
291 sysset("last_value", v);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000292 sysset("last_traceback", tb);
Guido van Rossum3165fe61992-09-25 21:59:05 +0000293 f = sysget("stderr");
294 if (f == NULL)
295 fprintf(stderr, "lost sys.stderr\n");
296 else {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000297 tb_print(tb, f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000298 if (exception == SyntaxError) {
299 object *message;
300 char *filename, *text;
301 int lineno, offset;
302 if (!getargs(v, "(O(ziiz))", &message,
303 &filename, &lineno, &offset, &text))
304 err_clear();
305 else {
306 char buf[10];
307 writestring(" File \"", f);
308 if (filename == NULL)
309 writestring("<string>", f);
310 else
311 writestring(filename, f);
312 writestring("\", line ", f);
313 sprintf(buf, "%d", lineno);
314 writestring(buf, f);
315 writestring("\n", f);
316 if (text != NULL) {
Guido van Rossum798199d1994-09-19 08:08:50 +0000317 char *nl;
318 if (offset > 0 &&
319 offset == strlen(text))
320 offset--;
321 for (;;) {
322 nl = strchr(text, '\n');
323 if (nl == NULL ||
324 nl-text >= offset)
325 break;
326 offset -= (nl+1-text);
327 text = nl+1;
328 }
Guido van Rossuma110aa61994-08-29 12:50:44 +0000329 while (*text == ' ' || *text == '\t') {
330 text++;
331 offset--;
332 }
333 writestring(" ", f);
334 writestring(text, f);
335 if (*text == '\0' ||
336 text[strlen(text)-1] != '\n')
337 writestring("\n", f);
338 writestring(" ", f);
339 offset--;
340 while (offset > 0) {
341 writestring(" ", f);
342 offset--;
343 }
344 writestring("^\n", f);
345 }
346 INCREF(message);
347 DECREF(v);
348 v = message;
349 }
350 }
Guido van Rossum3165fe61992-09-25 21:59:05 +0000351 if (writeobject(exception, f, PRINT_RAW) != 0)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000352 err_clear();
Guido van Rossum3165fe61992-09-25 21:59:05 +0000353 if (v != NULL && v != None) {
354 writestring(": ", f);
355 if (writeobject(v, f, PRINT_RAW) != 0)
356 err_clear();
357 }
358 writestring("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000359 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000360 XDECREF(exception);
361 XDECREF(v);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000362 XDECREF(tb);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000363}
364
365object *
366run_string(str, start, globals, locals)
367 char *str;
368 int start;
Guido van Rossum5b722181993-03-30 17:46:03 +0000369 object *globals, *locals;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000370{
Guido van Rossuma110aa61994-08-29 12:50:44 +0000371 return run_err_node(parse_string(str, start),
372 "<string>", globals, locals);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000373}
374
375object *
376run_file(fp, filename, start, globals, locals)
377 FILE *fp;
378 char *filename;
379 int start;
Guido van Rossum5b722181993-03-30 17:46:03 +0000380 object *globals, *locals;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000381{
Guido van Rossuma110aa61994-08-29 12:50:44 +0000382 return run_err_node(parse_file(fp, filename, start),
383 filename, globals, locals);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000384}
385
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000386static object *
Guido van Rossuma110aa61994-08-29 12:50:44 +0000387run_err_node(n, filename, globals, locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000388 node *n;
389 char *filename;
Guido van Rossum5b722181993-03-30 17:46:03 +0000390 object *globals, *locals;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000391{
Guido van Rossuma110aa61994-08-29 12:50:44 +0000392 if (n == NULL)
393 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000394 return run_node(n, filename, globals, locals);
395}
396
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000397static object *
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000398run_node(n, filename, globals, locals)
399 node *n;
400 char *filename;
Guido van Rossum5b722181993-03-30 17:46:03 +0000401 object *globals, *locals;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000402{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000403 codeobject *co;
404 object *v;
405 co = compile(n, filename);
406 freetree(n);
407 if (co == NULL)
408 return NULL;
Guido van Rossum81daa321993-05-20 14:24:46 +0000409 v = eval_code(co, globals, locals, (object *)NULL, (object *)NULL);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000410 DECREF(co);
411 return v;
412}
413
Guido van Rossum8ae87c01995-01-26 00:40:38 +0000414static object *
Guido van Rossumfdef2711994-09-14 13:31:04 +0000415run_pyc_file(fp, filename, globals, locals)
416 FILE *fp;
417 char *filename;
418 object *globals, *locals;
419{
420 codeobject *co;
421 object *v;
422 long magic;
423 long get_pyc_magic();
424
425 magic = rd_long(fp);
426 if (magic != get_pyc_magic()) {
427 err_setstr(RuntimeError,
428 "Bad magic number in .pyc file");
429 return NULL;
430 }
431 (void) rd_long(fp);
432 v = rd_object(fp);
433 fclose(fp);
434 if (v == NULL || !is_codeobject(v)) {
435 XDECREF(v);
436 err_setstr(RuntimeError,
437 "Bad code object in .pyc file");
438 return NULL;
439 }
440 co = (codeobject *)v;
441 v = eval_code(co, globals, locals, (object *)NULL, (object *)NULL);
442 DECREF(co);
443 return v;
444}
445
446object *
Guido van Rossum5b722181993-03-30 17:46:03 +0000447compile_string(str, filename, start)
448 char *str;
449 char *filename;
450 int start;
451{
452 node *n;
Guido van Rossum5b722181993-03-30 17:46:03 +0000453 codeobject *co;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000454 n = parse_string(str, start);
455 if (n == NULL)
Guido van Rossum5b722181993-03-30 17:46:03 +0000456 return NULL;
Guido van Rossum5b722181993-03-30 17:46:03 +0000457 co = compile(n, filename);
458 freetree(n);
459 return (object *)co;
460}
461
Guido van Rossuma110aa61994-08-29 12:50:44 +0000462/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000463
Guido van Rossuma110aa61994-08-29 12:50:44 +0000464node *
465parse_file(fp, filename, start)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000466 FILE *fp;
467 char *filename;
468 int start;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000469{
Guido van Rossuma110aa61994-08-29 12:50:44 +0000470 node *n;
471 perrdetail err;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000472 BGN_SAVE
Guido van Rossuma110aa61994-08-29 12:50:44 +0000473 n = parsefile(fp, filename, &gram, start,
474 (char *)0, (char *)0, &err);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000475 END_SAVE
Guido van Rossuma110aa61994-08-29 12:50:44 +0000476 if (n == NULL)
477 err_input(&err);
478 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000479}
480
Guido van Rossuma110aa61994-08-29 12:50:44 +0000481/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000482
Guido van Rossuma110aa61994-08-29 12:50:44 +0000483node *
484parse_string(str, start)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000485 char *str;
486 int start;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000487{
Guido van Rossuma110aa61994-08-29 12:50:44 +0000488 node *n;
489 perrdetail err;
490 n = parsestring(str, &gram, start, &err);
491 if (n == NULL)
492 err_input(&err);
493 return n;
494}
495
496/* Set the error appropriate to the given input error code (see errcode.h) */
497
498static void
499err_input(err)
500 perrdetail *err;
501{
502 object *v, *w;
503 char *msg = NULL;
504 v = mkvalue("(ziiz)", err->filename,
505 err->lineno, err->offset, err->text);
506 if (err->text != NULL) {
507 free(err->text);
508 err->text = NULL;
509 }
510 switch (err->error) {
511 case E_SYNTAX:
512 msg = "invalid syntax";
513 break;
514 case E_TOKEN:
515 msg = "invalid token";
516
517 break;
518 case E_INTR:
519 err_set(KeyboardInterrupt);
520 return;
521 case E_NOMEM:
522 err_nomem();
523 return;
524 case E_EOF:
525 msg = "unexpected EOF while parsing";
526 break;
527 default:
528 fprintf(stderr, "error=%d\n", err->error);
529 msg = "unknown parsing error";
530 break;
531 }
532 w = mkvalue("(sO)", msg, v);
533 XDECREF(v);
534 err_setval(SyntaxError, w);
535 XDECREF(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000536}
537
538/* Print fatal error message and abort */
539
540void
541fatal(msg)
542 char *msg;
543{
Guido van Rossum83dd6c31994-09-29 09:38:33 +0000544 fprintf(stderr, "Fatal Python error: %s\n", msg);
Guido van Rossum8ae87c01995-01-26 00:40:38 +0000545#ifdef macintosh
546 for (;;);
547#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000548 abort();
549}
550
551/* Clean up and exit */
552
Guido van Rossuma110aa61994-08-29 12:50:44 +0000553#ifdef WITH_THREAD
554#include "thread.h"
555int threads_started = 0; /* Set by threadmodule.c and maybe others */
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000556#endif
557
Guido van Rossum1662dd51994-09-07 14:38:28 +0000558#define NEXITFUNCS 32
559static void (*exitfuncs[NEXITFUNCS])();
560static int nexitfuncs = 0;
561
562int Py_AtExit(func)
563 void (*func) PROTO((void));
564{
565 if (nexitfuncs >= NEXITFUNCS)
566 return -1;
567 exitfuncs[nexitfuncs++] = func;
568 return 0;
569}
570
Guido van Rossuma110aa61994-08-29 12:50:44 +0000571void
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000572cleanup()
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000573{
Guido van Rossum59bff391992-09-03 20:28:00 +0000574 object *exitfunc = sysget("exitfunc");
575
576 if (exitfunc) {
577 object *arg;
578 object *res;
579 sysset("exitfunc", (object *)NULL);
580 arg = newtupleobject(0);
581 if (arg == NULL)
582 res = NULL;
583 else {
584 res = call_object(exitfunc, arg);
585 DECREF(arg);
586 }
587 if (res == NULL) {
588 fprintf(stderr, "Error in sys.exitfunc:\n");
589 print_error();
590 }
591 }
592
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000593 flushline();
Guido van Rossum1662dd51994-09-07 14:38:28 +0000594
595 while (nexitfuncs > 0)
596 (*exitfuncs[--nexitfuncs])();
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000597}
598
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000599#ifdef COUNT_ALLOCS
600extern void dump_counts PROTO((void));
601#endif
602
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000603void
604goaway(sts)
605 int sts;
606{
607 cleanup();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000608
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000609#ifdef COUNT_ALLOCS
610 dump_counts();
611#endif
612
Guido van Rossuma110aa61994-08-29 12:50:44 +0000613#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000614
615 /* Other threads may still be active, so skip most of the
616 cleanup actions usually done (these are mostly for
617 debugging anyway). */
618
Guido van Rossumdf72a651992-08-12 15:27:32 +0000619 (void) save_thread();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000620#ifndef NO_EXIT_PROG
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000621 if (threads_started)
622 _exit_prog(sts);
623 else
624 exit_prog(sts);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000625#else /* !NO_EXIT_PROG */
626 if (threads_started)
627 _exit(sts);
628 else
629 exit(sts);
630#endif /* !NO_EXIT_PROG */
Guido van Rossumff4949e1992-08-05 19:58:53 +0000631
Guido van Rossuma110aa61994-08-29 12:50:44 +0000632#else /* WITH_THREAD */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000633
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000634 doneimport();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000635
636 err_clear();
637
638#ifdef REF_DEBUG
639 fprintf(stderr, "[%ld refs]\n", ref_total);
640#endif
641
642#ifdef TRACE_REFS
643 if (askyesno("Print left references?")) {
644 printrefs(stderr);
645 }
646#endif /* TRACE_REFS */
647
Jack Jansen08e767b1995-02-02 14:30:20 +0000648 /* XXXX Jack thinks it would be nicer to pause if any output has
649 ** been generated since the last interaction with the user...
650 */
Guido van Rossum42a51241995-01-30 12:52:46 +0000651#ifdef THINK_C
652 if (sts == 0)
653 console_options.pause_atexit = 0;
654#endif
Jack Jansen08e767b1995-02-02 14:30:20 +0000655#ifdef __MWERKS__
656 if (sts == 0)
657 SIOUXSettings.autocloseonquit = 1;
658#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000659 exit(sts);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000660#endif /* WITH_THREAD */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000661 /*NOTREACHED*/
662}
663
Guido van Rossuma110aa61994-08-29 12:50:44 +0000664#ifdef HAVE_SIGNAL_H
665static RETSIGTYPE
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000666sighandler(sig)
667 int sig;
668{
669 signal(sig, SIG_DFL); /* Don't catch recursive signals */
670 cleanup(); /* Do essential clean-up */
Guido van Rossuma110aa61994-08-29 12:50:44 +0000671#ifdef HAVE_GETPID
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000672 kill(getpid(), sig); /* Pretend the signal killed us */
Guido van Rossuma110aa61994-08-29 12:50:44 +0000673#else
674 exit(1);
675#endif
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000676 /*NOTREACHED*/
677}
678#endif
679
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000680static void
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000681initsigs()
682{
Guido van Rossuma110aa61994-08-29 12:50:44 +0000683 RETSIGTYPE (*t)();
684#ifdef HAVE_SIGNAL_H
685#ifdef SIGPIPE
686 signal(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000687#endif
Guido van Rossuma110aa61994-08-29 12:50:44 +0000688#ifdef SIGHUP
689 t = signal(SIGHUP, SIG_IGN);
690 if (t == SIG_DFL)
691 signal(SIGHUP, sighandler);
692 else
693 signal(SIGHUP, t);
694#endif
695#ifdef SIGTERM
696 t = signal(SIGTERM, SIG_IGN);
697 if (t == SIG_DFL)
698 signal(SIGTERM, sighandler);
699 else
700 signal(SIGTERM, t);
701#endif
702#endif /* HAVE_SIGNAL_H */
703 initintr(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000704}
705
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000706#ifdef TRACE_REFS
707/* Ask a yes/no question */
708
Guido van Rossum59bff391992-09-03 20:28:00 +0000709int
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000710askyesno(prompt)
711 char *prompt;
712{
713 char buf[256];
714
715 printf("%s [ny] ", prompt);
716 if (fgets(buf, sizeof buf, stdin) == NULL)
717 return 0;
718 return buf[0] == 'y' || buf[0] == 'Y';
719}
720#endif
721
Guido van Rossuma110aa61994-08-29 12:50:44 +0000722#ifdef MPW
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000723
724/* Check for file descriptor connected to interactive device.
725 Pretend that stdin is always interactive, other files never. */
726
727int
728isatty(fd)
729 int fd;
730{
731 return fd == fileno(stdin);
732}
733
734#endif