blob: 0cab825157568574c34615a1448f198a0da56046 [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
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum667d7041995-08-04 04:20:48 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum667d7041995-08-04 04:20:48 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum667d7041995-08-04 04:20:48 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum667d7041995-08-04 04:20:48 +000029
30******************************************************************/
31
32/* Python interpreter main program */
33
34#include "Python.h"
35
Guido van Rossum66a70131996-12-09 18:46:58 +000036#ifdef HAVE_UNISTD_H
37#include <unistd.h>
38#endif
Guido van Rossum667d7041995-08-04 04:20:48 +000039
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000040#ifdef MS_WINDOWS
41#include <fcntl.h>
42#endif
43
Guido van Rossum667d7041995-08-04 04:20:48 +000044/* Interface to getopt(): */
45extern int optind;
46extern char *optarg;
47extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
48
49
Guido van Rossum667d7041995-08-04 04:20:48 +000050/* Subroutines that live in their own file */
Guido van Rossum582646a1996-05-28 22:30:17 +000051extern char *Py_GetVersion();
52extern char *Py_GetCopyright();
Guido van Rossum667d7041995-08-04 04:20:48 +000053
54
Guido van Rossumac56b031996-07-21 02:33:38 +000055/* For Py_GetProgramName(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000056static char *argv0;
57
Guido van Rossumac56b031996-07-21 02:33:38 +000058/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000059static char **orig_argv;
60static int orig_argc;
61
Guido van Rossum667d7041995-08-04 04:20:48 +000062/* Short usage message (with %s for argv0) */
63static char *usage_line =
Guido van Rossum7614da61997-03-03 19:14:45 +000064"usage: %s [-d] [-i] [-O] [-s] [-u] [-v] [-c cmd | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000065
66/* Long usage message, split into parts < 512 bytes */
67static char *usage_top = "\n\
68Options and arguments (and corresponding environment variables):\n\
69-d : debug output from parser (also PYTHONDEBUG=x)\n\
Guido van Rossum775af911997-02-14 19:50:32 +000070-i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
71 and force prompts, even if stdin does not appear to be a terminal.\n\
Guido van Rossum7614da61997-03-03 19:14:45 +000072-O : optimize generated bytecode (a tad).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000073-s : suppress printing of top level expressions (also PYTHONSUPPRESS=x)\n\
74-u : unbuffered stdout and stderr (also PYTHONUNBUFFERED=x)\n\
75-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000076";
77static char *usage_bot = "\
Guido van Rossum775af911997-02-14 19:50:32 +000078-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000079file : program read from script file\n\
80- : program read from stdin (default; interactive mode if a tty)\n\
81arg ...: arguments passed to program in sys.argv[1:]\n\
82\n\
83Other environment variables:\n\
84PYTHONSTARTUP: file executed on interactive startup (no default)\n\
85PYTHONPATH : colon-separated list of directories prefixed to the\n\
86 default module search path. The result is sys.path.\n\
87";
88
89
90/* Main program */
91
92int
93main(argc, argv)
94 int argc;
95 char **argv;
96{
97 int c;
98 int sts;
99 char *command = NULL;
100 char *filename = NULL;
101 FILE *fp = stdin;
102 char *p;
103 int inspect = 0;
104 int unbuffered = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000105 int stdin_is_interactive = 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000106
Guido van Rossumac56b031996-07-21 02:33:38 +0000107 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000108 orig_argv = argv;
Guido van Rossumac56b031996-07-21 02:33:38 +0000109 argv0 = argv[0]; /* For Py_GetProgramName() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000110
111 if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
112 Py_DebugFlag = 1;
113 if ((p = getenv("PYTHONSUPPRESS")) && *p != '\0')
114 Py_SuppressPrintingFlag = 1;
115 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
116 Py_VerboseFlag = 1;
117 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
118 inspect = 1;
119 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
120 unbuffered = 1;
121
Guido van Rossum7614da61997-03-03 19:14:45 +0000122 while ((c = getopt(argc, argv, "c:diOsuv")) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000123 if (c == 'c') {
124 /* -c is the last option; following arguments
125 that look like options are left for the
126 the command to interpret. */
127 command = malloc(strlen(optarg) + 2);
128 if (command == NULL)
129 Py_FatalError(
130 "not enough memory to copy -c argument");
131 strcpy(command, optarg);
132 strcat(command, "\n");
133 break;
134 }
135
136 switch (c) {
137
138 case 'd':
139 Py_DebugFlag++;
140 break;
141
142 case 'i':
143 inspect++;
Guido van Rossum775af911997-02-14 19:50:32 +0000144 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000145 break;
146
Guido van Rossum7614da61997-03-03 19:14:45 +0000147 case 'O':
148 Py_OptimizeFlag++;
149 break;
150
Guido van Rossum667d7041995-08-04 04:20:48 +0000151 case 's':
152 Py_SuppressPrintingFlag++;
153 break;
154
155 case 'u':
156 unbuffered++;
157 break;
158
159 case 'v':
160 Py_VerboseFlag++;
161 break;
162
163 /* This space reserved for other options */
164
165 default:
166 fprintf(stderr, usage_line, argv[0]);
167 fprintf(stderr, usage_top);
168 fprintf(stderr, usage_bot);
169 exit(2);
170 /*NOTREACHED*/
171
172 }
173 }
174
Guido van Rossum775af911997-02-14 19:50:32 +0000175 if (command == NULL && optind < argc &&
176 strcmp(argv[optind], "-") != 0)
177 {
178 filename = argv[optind];
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
188 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
189
Guido van Rossum667d7041995-08-04 04:20:48 +0000190 if (unbuffered) {
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000191#ifdef MS_WINDOWS
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000192 _setmode(fileno(stdin), O_BINARY);
193 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000194#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000195#ifndef MPW
Guido van Rossum775af911997-02-14 19:50:32 +0000196 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
197 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
198 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000199#else
200 /* On MPW (3.2) unbuffered seems to hang */
Guido van Rossum775af911997-02-14 19:50:32 +0000201 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000202 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
203 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
204#endif
205 }
Guido van Rossum775af911997-02-14 19:50:32 +0000206 else if (stdin_is_interactive) {
207 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
208 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
209 /* Leave stderr alone - it should be unbuffered anyway. */
210 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000211
212 if (Py_VerboseFlag ||
Guido van Rossum129e91a1997-02-14 21:00:50 +0000213 (command == NULL && filename == NULL && stdin_is_interactive))
Guido van Rossum667d7041995-08-04 04:20:48 +0000214 fprintf(stderr, "Python %s\n%s\n",
Guido van Rossum582646a1996-05-28 22:30:17 +0000215 Py_GetVersion(), Py_GetCopyright());
Guido van Rossum667d7041995-08-04 04:20:48 +0000216
Guido van Rossum667d7041995-08-04 04:20:48 +0000217 Py_Initialize();
218
219 if (command != NULL) {
220 /* Backup optind and force sys.argv[0] = '-c' */
221 optind--;
222 argv[optind] = "-c";
223 }
224
225 PySys_SetArgv(argc-optind, argv+optind);
226
227 if (command) {
228 sts = PyRun_SimpleString(command) != 0;
229 }
230 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000231 if (filename == NULL && stdin_is_interactive) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000232 char *startup = getenv("PYTHONSTARTUP");
233 if (startup != NULL && startup[0] != '\0') {
234 FILE *fp = fopen(startup, "r");
235 if (fp != NULL) {
236 (void) PyRun_SimpleFile(fp, startup);
237 PyErr_Clear();
238 fclose(fp);
239 }
240 }
241 }
242 sts = PyRun_AnyFile(
Guido van Rossum775af911997-02-14 19:50:32 +0000243 fp,
244 filename == NULL ? "<stdin>" : filename) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000245 if (filename != NULL)
246 fclose(fp);
247 }
248
Guido van Rossum775af911997-02-14 19:50:32 +0000249 if (inspect && stdin_is_interactive &&
Guido van Rossum667d7041995-08-04 04:20:48 +0000250 (filename != NULL || command != NULL))
251 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
252
253 Py_Exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +0000254 return 0; /* Make gcc -Wall happy */
Guido van Rossum667d7041995-08-04 04:20:48 +0000255}
256
257
Guido van Rossum8026feb1996-06-20 16:49:26 +0000258/* Return the program name -- some code out there needs this
259 (currently _tkinter.c and importdl.c). */
Guido van Rossum667d7041995-08-04 04:20:48 +0000260
261char *
Guido van Rossumac56b031996-07-21 02:33:38 +0000262Py_GetProgramName()
Guido van Rossum667d7041995-08-04 04:20:48 +0000263{
264 return argv0;
265}
266
267
268/* Make the *original* argc/argv available to other modules.
269 This is rare, but it is needed by the secureware extension. */
270
271void
Guido van Rossumac56b031996-07-21 02:33:38 +0000272Py_GetArgcArgv(argc, argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000273 int *argc;
274 char ***argv;
275{
276 *argc = orig_argc;
277 *argv = orig_argv;
278}