blob: 760e5dab2e4f6ef4c7f1a7ac91a7d2b146de3b42 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum9bfef441993-03-29 10:43:31 +00002Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
3Amsterdam, The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +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
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 Rossum1984f1e1992-08-04 12:41:02 +000029extern int debugging; /* Needed by parser.c */
30extern int verbose; /* Needed by import.c */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031
Guido van Rossum689e7011991-06-07 13:59:53 +000032/* Interface to getopt(): */
33extern int optind;
34extern char *optarg;
35extern int getopt PROTO((int, char **, char *));
36
Guido van Rossumf56e3db1993-04-01 20:59:32 +000037extern char *getenv();
38
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039main(argc, argv)
40 int argc;
41 char **argv;
42{
Guido van Rossum689e7011991-06-07 13:59:53 +000043 int c;
44 int sts;
45 char *command = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046 char *filename = NULL;
47 FILE *fp = stdin;
Guido van Rossumf56e3db1993-04-01 20:59:32 +000048 char *p;
49 int inspect = 0;
50
51 if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
52 debugging = 1;
53 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
54 verbose = 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055
Guido van Rossum689e7011991-06-07 13:59:53 +000056 initargs(&argc, &argv); /* Defined in config*.c */
57
Guido van Rossumf56e3db1993-04-01 20:59:32 +000058 while ((c = getopt(argc, argv, "c:div")) != EOF) {
Guido van Rossum46b16381992-01-02 16:16:18 +000059 if (c == 'c') {
60 /* -c is the last option; following arguments
61 that look like options are left for the
62 the command to interpret. */
63 command = malloc(strlen(optarg) + 2);
64 /* Ignore malloc errors this early... */
65 strcpy(command, optarg);
66 strcat(command, "\n");
67 break;
68 }
69
Guido van Rossum689e7011991-06-07 13:59:53 +000070 switch (c) {
71
Guido van Rossum8401e561992-01-19 16:48:36 +000072 case 'd':
73 debugging++;
74 break;
75
Guido van Rossumf56e3db1993-04-01 20:59:32 +000076 case 'i':
77 inspect++;
78 break;
79
Guido van Rossume3d70451992-03-27 17:21:30 +000080 case 'v':
81 verbose++;
82 break;
83
Guido van Rossum46b16381992-01-02 16:16:18 +000084 /* This space reserved for other options */
85
Guido van Rossum689e7011991-06-07 13:59:53 +000086 default:
87 fprintf(stderr,
88 "usage: %s [-c cmd | file | -] [arg] ...\n",
89 argv[0]);
90 exit(2);
91 /*NOTREACHED*/
92
Guido van Rossum689e7011991-06-07 13:59:53 +000093 }
94 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095
Guido van Rossum689e7011991-06-07 13:59:53 +000096 if (command == NULL && optind < argc && strcmp(argv[optind], "-") != 0)
97 filename = argv[optind];
Guido van Rossum3f5da241990-12-20 15:06:42 +000098
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099 if (filename != NULL) {
100 if ((fp = fopen(filename, "r")) == NULL) {
Guido van Rossum689e7011991-06-07 13:59:53 +0000101 fprintf(stderr, "%s: can't open file '%s'\n",
102 argv[0], filename);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103 exit(2);
104 }
105 }
106
Guido van Rossum138e6bf1992-06-03 17:08:15 +0000107 initall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108
Guido van Rossum689e7011991-06-07 13:59:53 +0000109 if (command != NULL) {
110 /* Backup optind and force sys.argv[0] = '-c' */
111 optind--;
112 argv[optind] = "-c";
113 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000114
Guido van Rossum689e7011991-06-07 13:59:53 +0000115 setpythonargv(argc-optind, argv+optind);
116
117 if (command) {
118 sts = run_command(command) != 0;
119 }
120 else {
Guido van Rossumda8cd861992-09-03 20:26:57 +0000121 if (filename == NULL && isatty((int)fileno(fp))) {
122 char *startup = getenv("PYTHONSTARTUP");
123 if (startup != NULL && startup[0] != '\0') {
124 FILE *fp = fopen(startup, "r");
125 if (fp != NULL) {
126 (void) run_script(fp, startup);
127 err_clear();
128 }
129 }
130 }
Guido van Rossum689e7011991-06-07 13:59:53 +0000131 sts = run(fp, filename == NULL ? "<stdin>" : filename) != 0;
132 }
133
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000134 if (inspect && isatty((int)fileno(stdin)) &&
135 (filename != NULL || command != NULL))
136 sts = run(stdin, "<stdin>") != 0;
137
Guido van Rossum689e7011991-06-07 13:59:53 +0000138 goaway(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139 /*NOTREACHED*/
140}