blob: 4763a595f5260b9124cec20f321a885f378ee6e9 [file] [log] [blame]
Guido van Rossum667d7041995-08-04 04:20:48 +00001/***********************************************************
2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
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
25/* Python interpreter main program */
26
27#include "Python.h"
28
29
30/* Interface to getopt(): */
31extern int optind;
32extern char *optarg;
33extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
34
35
36extern int Py_DebugFlag; /* For parser.c, declared in pythonrun.c */
37extern int Py_VerboseFlag; /* For import.c, declared in pythonrun.c */
38extern int Py_SuppressPrintingFlag; /* For ceval.c, declared in pythonrun.c */
39
40
41/* Subroutines that live in their own file */
42extern char *getversion();
43extern char *getcopyright();
44
45
46/* For getprogramname(); set by main() */
47static char *argv0;
48
49/* For getargcargv(); set by main() */
50static char **orig_argv;
51static int orig_argc;
52
53
54/* Short usage message (with %s for argv0) */
55static char *usage_line =
56"usage: %s [-d] [-i] [-s] [-u ] [-v] [-c cmd | file | -] [arg] ...\n";
57
58/* Long usage message, split into parts < 512 bytes */
59static char *usage_top = "\n\
60Options and arguments (and corresponding environment variables):\n\
61-d : debug output from parser (also PYTHONDEBUG=x)\n\
62-i : inspect interactively after running script (also PYTHONINSPECT=x)\n\
63-s : suppress printing of top level expressions (also PYTHONSUPPRESS=x)\n\
64-u : unbuffered stdout and stderr (also PYTHONUNBUFFERED=x)\n\
65-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
66-c cmd : program passed in as string (terminates option list)\n\
67";
68static char *usage_bot = "\
69file : program read from script file\n\
70- : program read from stdin (default; interactive mode if a tty)\n\
71arg ...: arguments passed to program in sys.argv[1:]\n\
72\n\
73Other environment variables:\n\
74PYTHONSTARTUP: file executed on interactive startup (no default)\n\
75PYTHONPATH : colon-separated list of directories prefixed to the\n\
76 default module search path. The result is sys.path.\n\
77";
78
79
80/* Main program */
81
82int
83main(argc, argv)
84 int argc;
85 char **argv;
86{
87 int c;
88 int sts;
89 char *command = NULL;
90 char *filename = NULL;
91 FILE *fp = stdin;
92 char *p;
93 int inspect = 0;
94 int unbuffered = 0;
95
96 orig_argc = argc; /* For getargcargv() */
97 orig_argv = argv;
98 argv0 = argv[0]; /* For getprogramname() */
99
100 if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
101 Py_DebugFlag = 1;
102 if ((p = getenv("PYTHONSUPPRESS")) && *p != '\0')
103 Py_SuppressPrintingFlag = 1;
104 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
105 Py_VerboseFlag = 1;
106 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
107 inspect = 1;
108 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
109 unbuffered = 1;
110
111 while ((c = getopt(argc, argv, "c:disuv")) != EOF) {
112 if (c == 'c') {
113 /* -c is the last option; following arguments
114 that look like options are left for the
115 the command to interpret. */
116 command = malloc(strlen(optarg) + 2);
117 if (command == NULL)
118 Py_FatalError(
119 "not enough memory to copy -c argument");
120 strcpy(command, optarg);
121 strcat(command, "\n");
122 break;
123 }
124
125 switch (c) {
126
127 case 'd':
128 Py_DebugFlag++;
129 break;
130
131 case 'i':
132 inspect++;
133 break;
134
135 case 's':
136 Py_SuppressPrintingFlag++;
137 break;
138
139 case 'u':
140 unbuffered++;
141 break;
142
143 case 'v':
144 Py_VerboseFlag++;
145 break;
146
147 /* This space reserved for other options */
148
149 default:
150 fprintf(stderr, usage_line, argv[0]);
151 fprintf(stderr, usage_top);
152 fprintf(stderr, usage_bot);
153 exit(2);
154 /*NOTREACHED*/
155
156 }
157 }
158
159 if (unbuffered) {
160#ifndef MPW
161 setbuf(stdout, (char *)NULL);
162 setbuf(stderr, (char *)NULL);
163#else
164 /* On MPW (3.2) unbuffered seems to hang */
165 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
166 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
167#endif
168 }
169
170 if (command == NULL && optind < argc &&
171 strcmp(argv[optind], "-") != 0)
172 filename = argv[optind];
173
174 if (Py_VerboseFlag ||
175 command == NULL && filename == NULL && isatty((int)fileno(fp)))
176 fprintf(stderr, "Python %s\n%s\n",
177 getversion(), getcopyright());
178
179 if (filename != NULL) {
180 if ((fp = fopen(filename, "r")) == NULL) {
181 fprintf(stderr, "%s: can't open file '%s'\n",
182 argv[0], filename);
183 exit(2);
184 }
185 }
186
187 Py_Initialize();
188
189 if (command != NULL) {
190 /* Backup optind and force sys.argv[0] = '-c' */
191 optind--;
192 argv[optind] = "-c";
193 }
194
195 PySys_SetArgv(argc-optind, argv+optind);
196
197 if (command) {
198 sts = PyRun_SimpleString(command) != 0;
199 }
200 else {
201 if (filename == NULL && isatty((int)fileno(fp))) {
202 char *startup = getenv("PYTHONSTARTUP");
203 if (startup != NULL && startup[0] != '\0') {
204 FILE *fp = fopen(startup, "r");
205 if (fp != NULL) {
206 (void) PyRun_SimpleFile(fp, startup);
207 PyErr_Clear();
208 fclose(fp);
209 }
210 }
211 }
212 sts = PyRun_AnyFile(
213 fp, filename == NULL ? "<stdin>" : filename) != 0;
214 if (filename != NULL)
215 fclose(fp);
216 }
217
218 if (inspect && isatty((int)fileno(stdin)) &&
219 (filename != NULL || command != NULL))
220 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
221
222 Py_Exit(sts);
223 /*NOTREACHED*/
224}
225
226
227/* Return the program name -- some code out there needs this. */
228
229char *
230getprogramname()
231{
232 return argv0;
233}
234
235
236/* Make the *original* argc/argv available to other modules.
237 This is rare, but it is needed by the secureware extension. */
238
239void
240getargcargv(argc,argv)
241 int *argc;
242 char ***argv;
243{
244 *argc = orig_argc;
245 *argv = orig_argv;
246}