blob: 36b2be963003b381f3e21d1cd9045f202293b17b [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"
Guido van Rossuma075ce11997-12-05 21:56:45 +000035#include "osdefs.h"
Guido van Rossum667d7041995-08-04 04:20:48 +000036
Guido van Rossum66a70131996-12-09 18:46:58 +000037#ifdef HAVE_UNISTD_H
38#include <unistd.h>
39#endif
Guido van Rossum667d7041995-08-04 04:20:48 +000040
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000041#ifdef MS_WINDOWS
42#include <fcntl.h>
43#endif
44
Guido van Rossuma075ce11997-12-05 21:56:45 +000045#if defined(PYOS_OS2) || defined(MS_WINDOWS)
46#define PYTHONHOMEHELP "<prefix>\\lib"
47#else
48#define PYTHONHOMEHELP "<prefix>/python1.5"
49#endif
50
Guido van Rossum667d7041995-08-04 04:20:48 +000051/* Interface to getopt(): */
52extern int optind;
53extern char *optarg;
54extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
55
56
Guido van Rossumac56b031996-07-21 02:33:38 +000057/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000058static char **orig_argv;
59static int orig_argc;
60
Guido van Rossum667d7041995-08-04 04:20:48 +000061/* Short usage message (with %s for argv0) */
62static char *usage_line =
Guido van Rossuma075ce11997-12-05 21:56:45 +000063"usage: %s [-d] [-i] [-O] [-S] [-u] [-v] [-x] [-X] [-c cmd | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000064
65/* Long usage message, split into parts < 512 bytes */
Guido van Rossuma075ce11997-12-05 21:56:45 +000066static char *usage_top = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000067Options and arguments (and corresponding environment variables):\n\
68-d : debug output from parser (also PYTHONDEBUG=x)\n\
Guido van Rossum775af911997-02-14 19:50:32 +000069-i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000070 and force prompts, even if stdin does not appear to be a terminal\n\
Guido van Rossume7adf3e1998-10-07 14:50:06 +000071-O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000072-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000073-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000074";
Guido van Rossum7922bd71997-08-29 22:34:47 +000075static char *usage_mid = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000076-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000077-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000078-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000079-X : disable class based built-in exceptions\n\
Guido van Rossum775af911997-02-14 19:50:32 +000080-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000081file : program read from script file\n\
82- : program read from stdin (default; interactive mode if a tty)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000083";
84static char *usage_bot = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000085arg ...: arguments passed to program in sys.argv[1:]\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000086Other environment variables:\n\
87PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000088PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000089 default module search path. The result is sys.path.\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000090PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
91 The default module search path uses %s.\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000092";
93
94
95/* Main program */
96
Guido van Rossum9c1201f1998-12-07 14:28:47 +000097DL_EXPORT(int)
Guido van Rossumed52aac1997-07-19 19:20:32 +000098Py_Main(argc, argv)
Guido van Rossum667d7041995-08-04 04:20:48 +000099 int argc;
100 char **argv;
101{
102 int c;
103 int sts;
104 char *command = NULL;
105 char *filename = NULL;
106 FILE *fp = stdin;
107 char *p;
108 int inspect = 0;
109 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000110 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000111 int stdin_is_interactive = 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000112
Guido van Rossumac56b031996-07-21 02:33:38 +0000113 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000114 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000115
Guido van Rossum667d7041995-08-04 04:20:48 +0000116 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
117 inspect = 1;
118 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
119 unbuffered = 1;
120
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000121 while ((c = getopt(argc, argv, "c:diOStuvxX")) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000122 if (c == 'c') {
123 /* -c is the last option; following arguments
124 that look like options are left for the
125 the command to interpret. */
126 command = malloc(strlen(optarg) + 2);
127 if (command == NULL)
128 Py_FatalError(
129 "not enough memory to copy -c argument");
130 strcpy(command, optarg);
131 strcat(command, "\n");
132 break;
133 }
134
135 switch (c) {
136
137 case 'd':
138 Py_DebugFlag++;
139 break;
140
141 case 'i':
142 inspect++;
Guido van Rossum775af911997-02-14 19:50:32 +0000143 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000144 break;
145
Guido van Rossum7614da61997-03-03 19:14:45 +0000146 case 'O':
147 Py_OptimizeFlag++;
148 break;
149
Guido van Rossum7922bd71997-08-29 22:34:47 +0000150 case 'S':
151 Py_NoSiteFlag++;
152 break;
153
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000154 case 't':
155 Py_TabcheckFlag++;
156 break;
157
Guido van Rossum667d7041995-08-04 04:20:48 +0000158 case 'u':
159 unbuffered++;
160 break;
161
162 case 'v':
163 Py_VerboseFlag++;
164 break;
165
Guido van Rossuma075ce11997-12-05 21:56:45 +0000166 case 'x':
167 skipfirstline = 1;
168 break;
169
Barry Warsawf488af31997-08-29 21:57:49 +0000170 case 'X':
Barry Warsaw83b67091997-08-29 22:20:16 +0000171 Py_UseClassExceptionsFlag = 0;
Barry Warsawf488af31997-08-29 21:57:49 +0000172 break;
173
Guido van Rossum667d7041995-08-04 04:20:48 +0000174 /* This space reserved for other options */
175
176 default:
177 fprintf(stderr, usage_line, argv[0]);
178 fprintf(stderr, usage_top);
Guido van Rossum7922bd71997-08-29 22:34:47 +0000179 fprintf(stderr, usage_mid);
Guido van Rossuma075ce11997-12-05 21:56:45 +0000180 fprintf(stderr, usage_bot,
181 DELIM, DELIM, PYTHONHOMEHELP);
Guido van Rossum667d7041995-08-04 04:20:48 +0000182 exit(2);
183 /*NOTREACHED*/
184
185 }
186 }
187
Guido van Rossum775af911997-02-14 19:50:32 +0000188 if (command == NULL && optind < argc &&
189 strcmp(argv[optind], "-") != 0)
190 {
191 filename = argv[optind];
192 if (filename != NULL) {
193 if ((fp = fopen(filename, "r")) == NULL) {
194 fprintf(stderr, "%s: can't open file '%s'\n",
195 argv[0], filename);
196 exit(2);
197 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000198 else if (skipfirstline) {
199 char line[256];
200 fgets(line, sizeof line, fp);
201 }
Guido van Rossum775af911997-02-14 19:50:32 +0000202 }
203 }
204
205 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
206
Guido van Rossum667d7041995-08-04 04:20:48 +0000207 if (unbuffered) {
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000208#ifdef MS_WINDOWS
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000209 _setmode(fileno(stdin), O_BINARY);
210 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000211#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000212#ifndef MPW
Guido van Rossum22ffac11998-03-06 15:30:39 +0000213#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000214 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
215 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
216 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000217#else /* !HAVE_SETVBUF */
218 setbuf(stdin, (char *)NULL);
219 setbuf(stdout, (char *)NULL);
220 setbuf(stderr, (char *)NULL);
221#endif /* !HAVE_SETVBUF */
222#else /* MPW */
Guido van Rossum667d7041995-08-04 04:20:48 +0000223 /* On MPW (3.2) unbuffered seems to hang */
Guido van Rossum775af911997-02-14 19:50:32 +0000224 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000225 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
226 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000227#endif /* MPW */
Guido van Rossum667d7041995-08-04 04:20:48 +0000228 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000229 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000230#ifdef MS_WINDOWS
231 /* Doesn't have to have line-buffered -- use unbuffered */
232 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
233 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000234#else /* !MS_WINDOWS */
235#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000236 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
237 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000238#endif /* HAVE_SETVBUF */
239#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000240 /* Leave stderr alone - it should be unbuffered anyway. */
241 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000242
Guido van Rossumed52aac1997-07-19 19:20:32 +0000243 Py_SetProgramName(argv[0]);
244 Py_Initialize();
245
Guido van Rossum667d7041995-08-04 04:20:48 +0000246 if (Py_VerboseFlag ||
Guido van Rossum129e91a1997-02-14 21:00:50 +0000247 (command == NULL && filename == NULL && stdin_is_interactive))
Guido van Rossumfe4dfc71997-05-19 18:33:01 +0000248 fprintf(stderr, "Python %s on %s\n%s\n",
249 Py_GetVersion(), Py_GetPlatform(), Py_GetCopyright());
Guido van Rossum667d7041995-08-04 04:20:48 +0000250
Guido van Rossum667d7041995-08-04 04:20:48 +0000251
252 if (command != NULL) {
253 /* Backup optind and force sys.argv[0] = '-c' */
254 optind--;
255 argv[optind] = "-c";
256 }
257
258 PySys_SetArgv(argc-optind, argv+optind);
259
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000260 if ((inspect || (command == NULL && filename == NULL)) &&
261 isatty(fileno(stdin))) {
262 PyObject *v;
263 v = PyImport_ImportModule("readline");
264 if (v == NULL)
265 PyErr_Clear();
266 else
267 Py_DECREF(v);
268 }
269
Guido van Rossum667d7041995-08-04 04:20:48 +0000270 if (command) {
271 sts = PyRun_SimpleString(command) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000272 free(command);
Guido van Rossum667d7041995-08-04 04:20:48 +0000273 }
274 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000275 if (filename == NULL && stdin_is_interactive) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000276 char *startup = getenv("PYTHONSTARTUP");
277 if (startup != NULL && startup[0] != '\0') {
278 FILE *fp = fopen(startup, "r");
279 if (fp != NULL) {
280 (void) PyRun_SimpleFile(fp, startup);
281 PyErr_Clear();
282 fclose(fp);
283 }
284 }
285 }
286 sts = PyRun_AnyFile(
Guido van Rossum775af911997-02-14 19:50:32 +0000287 fp,
288 filename == NULL ? "<stdin>" : filename) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000289 if (filename != NULL)
290 fclose(fp);
291 }
292
Guido van Rossum775af911997-02-14 19:50:32 +0000293 if (inspect && stdin_is_interactive &&
Guido van Rossum667d7041995-08-04 04:20:48 +0000294 (filename != NULL || command != NULL))
295 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
296
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000297 Py_Finalize();
Guido van Rossum05f7c501997-08-02 03:00:42 +0000298 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000299}
300
301
Guido van Rossum667d7041995-08-04 04:20:48 +0000302/* Make the *original* argc/argv available to other modules.
303 This is rare, but it is needed by the secureware extension. */
304
305void
Guido van Rossumac56b031996-07-21 02:33:38 +0000306Py_GetArgcArgv(argc, argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000307 int *argc;
308 char ***argv;
309{
310 *argc = orig_argc;
311 *argv = orig_argv;
312}