blob: 000a3d789f8ce458a88f8853a595b11a6c7c206a [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The 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 Rossum1d5735e1994-08-30 08:27:36 +000029extern int debugging; /* Needed in parser.c, declared in pythonrun.c */
30extern int verbose; /* Needed in import.c, declared in pythonrun.c */
31extern int suppress_print; /* Needed in ceval.c, declared in pythonrun.c */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032
Guido van Rossum689e7011991-06-07 13:59:53 +000033/* Interface to getopt(): */
34extern int optind;
35extern char *optarg;
Guido van Rossumb376a4a1993-11-23 17:53:17 +000036extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
Guido van Rossum689e7011991-06-07 13:59:53 +000037
Guido van Rossumf56e3db1993-04-01 20:59:32 +000038extern char *getenv();
39
Guido van Rossum1d5735e1994-08-30 08:27:36 +000040extern char *getversion();
41extern char *getcopyright();
42
43int
44realmain(argc, argv)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045 int argc;
46 char **argv;
47{
Guido van Rossum689e7011991-06-07 13:59:53 +000048 int c;
49 int sts;
50 char *command = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051 char *filename = NULL;
52 FILE *fp = stdin;
Guido van Rossumf56e3db1993-04-01 20:59:32 +000053 char *p;
54 int inspect = 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +000055 int unbuffered = 0;
Guido van Rossumf56e3db1993-04-01 20:59:32 +000056
57 if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
58 debugging = 1;
Guido van Rossum1d5735e1994-08-30 08:27:36 +000059 if ((p = getenv("PYTHONSUPPRESS")) && *p != '\0')
60 suppress_print = 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +000061 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
62 verbose = 1;
Guido van Rossum9e90a671993-06-24 11:10:19 +000063 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
64 inspect = 1;
Guido van Rossum1d5735e1994-08-30 08:27:36 +000065 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
66 unbuffered = 1;
Guido van Rossum689e7011991-06-07 13:59:53 +000067
Jack Jansenb7d0df41995-07-29 13:53:59 +000068#ifdef macintosh
69 PyMac_InteractiveOptions(&inspect, &verbose, &suppress_print, &unbuffered, &debugging);
70#endif
71
Guido van Rossum1d5735e1994-08-30 08:27:36 +000072 while ((c = getopt(argc, argv, "c:disuv")) != EOF) {
Guido van Rossum46b16381992-01-02 16:16:18 +000073 if (c == 'c') {
74 /* -c is the last option; following arguments
75 that look like options are left for the
76 the command to interpret. */
77 command = malloc(strlen(optarg) + 2);
Guido van Rossuma66e25d1994-09-29 09:38:18 +000078 if (command == NULL)
79 fatal("not enough memory to copy -c argument");
Guido van Rossum46b16381992-01-02 16:16:18 +000080 strcpy(command, optarg);
81 strcat(command, "\n");
82 break;
83 }
84
Guido van Rossum689e7011991-06-07 13:59:53 +000085 switch (c) {
86
Guido van Rossum8401e561992-01-19 16:48:36 +000087 case 'd':
88 debugging++;
89 break;
90
Guido van Rossumf56e3db1993-04-01 20:59:32 +000091 case 'i':
92 inspect++;
93 break;
94
Guido van Rossum1d5735e1994-08-30 08:27:36 +000095 case 's':
96 suppress_print++;
97 break;
98
99 case 'u':
100 unbuffered++;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000101 break;
102
Guido van Rossume3d70451992-03-27 17:21:30 +0000103 case 'v':
104 verbose++;
105 break;
106
Guido van Rossum46b16381992-01-02 16:16:18 +0000107 /* This space reserved for other options */
108
Guido van Rossum689e7011991-06-07 13:59:53 +0000109 default:
110 fprintf(stderr,
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000111"usage: %s [-d] [-i] [-s] [-u ] [-v] [-c cmd | file | -] [arg] ...\n",
Guido van Rossum689e7011991-06-07 13:59:53 +0000112 argv[0]);
Jack Janseneceb3e31995-06-27 13:15:15 +0000113#if !(defined(__CFM68K__) && defined(__MWERKS__))
114 /* Mwerks cfm68k linker doesn't like these... */
Guido van Rossum9e90a671993-06-24 11:10:19 +0000115 fprintf(stderr, "\
116\n\
117Options and arguments (and corresponding environment variables):\n\
118-d : debug output from parser (also PYTHONDEBUG=x)\n\
119-i : inspect interactively after running script (also PYTHONINSPECT=x)\n\
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000120-s : suppress the printing of top level expressions (also PYTHONSUPPRESS=x)\n\
121-u : unbuffered stdout and stderr (also PYTHONUNBUFFERED=x)\n\
Guido van Rossum9e90a671993-06-24 11:10:19 +0000122-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
123-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000124");
125 /* ANSI does not allow strings > 512 chars
126 and MPW doesn't like it either -- so split it! */
127 fprintf(stderr, "\
Guido van Rossum9e90a671993-06-24 11:10:19 +0000128file : program read from script file\n\
129- : program read from stdin (default; interactive mode if a tty)\n\
130arg ...: arguments passed to program in sys.argv[1:]\n\
131\n\
132Other environment variables:\n\
133PYTHONSTARTUP: file executed on interactive startup (no default)\n\
134PYTHONPATH : colon-separated list of directories prefixed to the\n\
135 default module search path. The result is sys.path.\n\
136");
Jack Janseneceb3e31995-06-27 13:15:15 +0000137#endif /* !cfm68k || !mwerks */
Guido van Rossum689e7011991-06-07 13:59:53 +0000138 exit(2);
139 /*NOTREACHED*/
140
Guido van Rossum689e7011991-06-07 13:59:53 +0000141 }
142 }
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000143
144 if (unbuffered) {
145#ifndef MPW
146 setbuf(stdout, (char *)NULL);
147 setbuf(stderr, (char *)NULL);
148#else
149 /* On MPW (3.2) unbuffered seems to hang */
150 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
151 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
152#endif
153 }
154
Guido van Rossum689e7011991-06-07 13:59:53 +0000155 if (command == NULL && optind < argc && strcmp(argv[optind], "-") != 0)
156 filename = argv[optind];
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000157
158 if (verbose ||
159 command == NULL && filename == NULL && isatty((int)fileno(fp)))
160 fprintf(stderr, "Python %s\n%s\n",
161 getversion(), getcopyright());
Guido van Rossum3f5da241990-12-20 15:06:42 +0000162
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000163 if (filename != NULL) {
164 if ((fp = fopen(filename, "r")) == NULL) {
Guido van Rossum689e7011991-06-07 13:59:53 +0000165 fprintf(stderr, "%s: can't open file '%s'\n",
166 argv[0], filename);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000167 exit(2);
168 }
169 }
170
Guido van Rossum138e6bf1992-06-03 17:08:15 +0000171 initall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172
Guido van Rossum689e7011991-06-07 13:59:53 +0000173 if (command != NULL) {
174 /* Backup optind and force sys.argv[0] = '-c' */
175 optind--;
176 argv[optind] = "-c";
177 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000178
Guido van Rossum689e7011991-06-07 13:59:53 +0000179 setpythonargv(argc-optind, argv+optind);
180
181 if (command) {
182 sts = run_command(command) != 0;
183 }
184 else {
Guido van Rossumda8cd861992-09-03 20:26:57 +0000185 if (filename == NULL && isatty((int)fileno(fp))) {
186 char *startup = getenv("PYTHONSTARTUP");
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000187#ifdef macintosh
188 if (startup == NULL)
189 startup = "PythonStartup";
190#endif
Guido van Rossumda8cd861992-09-03 20:26:57 +0000191 if (startup != NULL && startup[0] != '\0') {
192 FILE *fp = fopen(startup, "r");
193 if (fp != NULL) {
194 (void) run_script(fp, startup);
195 err_clear();
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000196 fclose(fp);
Guido van Rossumda8cd861992-09-03 20:26:57 +0000197 }
198 }
199 }
Guido van Rossum689e7011991-06-07 13:59:53 +0000200 sts = run(fp, filename == NULL ? "<stdin>" : filename) != 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000201 if (filename != NULL)
202 fclose(fp);
Guido van Rossum689e7011991-06-07 13:59:53 +0000203 }
204
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000205 if (inspect && isatty((int)fileno(stdin)) &&
206 (filename != NULL || command != NULL))
207 sts = run(stdin, "<stdin>") != 0;
208
Guido van Rossum689e7011991-06-07 13:59:53 +0000209 goaway(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210 /*NOTREACHED*/
211}