blob: df79ab5c4d87a1b38fb01cd0df5a266a28b1edb4 [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 Rossumac56b031996-07-21 02:33:38 +000050/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000051static char **orig_argv;
52static int orig_argc;
53
Guido van Rossum667d7041995-08-04 04:20:48 +000054/* Short usage message (with %s for argv0) */
55static char *usage_line =
Guido van Rossum7922bd71997-08-29 22:34:47 +000056"usage: %s [-d] [-i] [-O] [-S] [-u] [-v] [-X] [-c cmd | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000057
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\
Guido van Rossum775af911997-02-14 19:50:32 +000062-i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
63 and force prompts, even if stdin does not appear to be a terminal.\n\
Guido van Rossum7614da61997-03-03 19:14:45 +000064-O : optimize generated bytecode (a tad).\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000065-S : don't imply 'import site' on initialization\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000066";
Guido van Rossum7922bd71997-08-29 22:34:47 +000067static char *usage_mid = "\
68-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
69-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
70-X : disable class based built-in exceptions\n\
Guido van Rossum775af911997-02-14 19:50:32 +000071-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000072file : program read from script file\n\
73- : program read from stdin (default; interactive mode if a tty)\n\
74arg ...: arguments passed to program in sys.argv[1:]\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000075";
76static char *usage_bot = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000077\n\
78Other environment variables:\n\
79PYTHONSTARTUP: file executed on interactive startup (no default)\n\
80PYTHONPATH : colon-separated list of directories prefixed to the\n\
81 default module search path. The result is sys.path.\n\
Guido van Rossum03ef6471997-04-30 19:48:59 +000082PYTHONHOME : alternate <prefix> directory (or <prefix>:<exec_prefix>).\n\
83 The default module search path uses <prefix>/lib/python1.5.\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000084";
85
86
87/* Main program */
88
89int
Guido van Rossumed52aac1997-07-19 19:20:32 +000090Py_Main(argc, argv)
Guido van Rossum667d7041995-08-04 04:20:48 +000091 int argc;
92 char **argv;
93{
94 int c;
95 int sts;
96 char *command = NULL;
97 char *filename = NULL;
98 FILE *fp = stdin;
99 char *p;
100 int inspect = 0;
101 int unbuffered = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000102 int stdin_is_interactive = 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000103
Guido van Rossumac56b031996-07-21 02:33:38 +0000104 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000105 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000106
Guido van Rossum667d7041995-08-04 04:20:48 +0000107 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
108 inspect = 1;
109 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
110 unbuffered = 1;
111
Guido van Rossum7922bd71997-08-29 22:34:47 +0000112 while ((c = getopt(argc, argv, "c:diOSuvX")) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000113 if (c == 'c') {
114 /* -c is the last option; following arguments
115 that look like options are left for the
116 the command to interpret. */
117 command = malloc(strlen(optarg) + 2);
118 if (command == NULL)
119 Py_FatalError(
120 "not enough memory to copy -c argument");
121 strcpy(command, optarg);
122 strcat(command, "\n");
123 break;
124 }
125
126 switch (c) {
127
128 case 'd':
129 Py_DebugFlag++;
130 break;
131
132 case 'i':
133 inspect++;
Guido van Rossum775af911997-02-14 19:50:32 +0000134 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000135 break;
136
Guido van Rossum7614da61997-03-03 19:14:45 +0000137 case 'O':
138 Py_OptimizeFlag++;
139 break;
140
Guido van Rossum7922bd71997-08-29 22:34:47 +0000141 case 'S':
142 Py_NoSiteFlag++;
143 break;
144
Guido van Rossum667d7041995-08-04 04:20:48 +0000145 case 'u':
146 unbuffered++;
147 break;
148
149 case 'v':
150 Py_VerboseFlag++;
151 break;
152
Barry Warsawf488af31997-08-29 21:57:49 +0000153 case 'X':
Barry Warsaw83b67091997-08-29 22:20:16 +0000154 Py_UseClassExceptionsFlag = 0;
Barry Warsawf488af31997-08-29 21:57:49 +0000155 break;
156
Guido van Rossum667d7041995-08-04 04:20:48 +0000157 /* This space reserved for other options */
158
159 default:
160 fprintf(stderr, usage_line, argv[0]);
161 fprintf(stderr, usage_top);
Guido van Rossum7922bd71997-08-29 22:34:47 +0000162 fprintf(stderr, usage_mid);
Guido van Rossum667d7041995-08-04 04:20:48 +0000163 fprintf(stderr, usage_bot);
164 exit(2);
165 /*NOTREACHED*/
166
167 }
168 }
169
Guido van Rossum775af911997-02-14 19:50:32 +0000170 if (command == NULL && optind < argc &&
171 strcmp(argv[optind], "-") != 0)
172 {
173 filename = argv[optind];
174 if (filename != NULL) {
175 if ((fp = fopen(filename, "r")) == NULL) {
176 fprintf(stderr, "%s: can't open file '%s'\n",
177 argv[0], filename);
178 exit(2);
179 }
180 }
181 }
182
183 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
184
Guido van Rossum667d7041995-08-04 04:20:48 +0000185 if (unbuffered) {
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000186#ifdef MS_WINDOWS
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000187 _setmode(fileno(stdin), O_BINARY);
188 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000189#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000190#ifndef MPW
Guido van Rossum775af911997-02-14 19:50:32 +0000191 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
192 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
193 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000194#else
195 /* On MPW (3.2) unbuffered seems to hang */
Guido van Rossum775af911997-02-14 19:50:32 +0000196 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000197 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
198 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
199#endif
200 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000201 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000202#ifdef MS_WINDOWS
203 /* Doesn't have to have line-buffered -- use unbuffered */
204 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
205 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
206#else
207 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
208 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
209#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000210 /* Leave stderr alone - it should be unbuffered anyway. */
211 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000212
Guido van Rossumed52aac1997-07-19 19:20:32 +0000213 Py_SetProgramName(argv[0]);
214 Py_Initialize();
215
Guido van Rossum667d7041995-08-04 04:20:48 +0000216 if (Py_VerboseFlag ||
Guido van Rossum129e91a1997-02-14 21:00:50 +0000217 (command == NULL && filename == NULL && stdin_is_interactive))
Guido van Rossumfe4dfc71997-05-19 18:33:01 +0000218 fprintf(stderr, "Python %s on %s\n%s\n",
219 Py_GetVersion(), Py_GetPlatform(), Py_GetCopyright());
Guido van Rossum667d7041995-08-04 04:20:48 +0000220
Guido van Rossum667d7041995-08-04 04:20:48 +0000221
222 if (command != NULL) {
223 /* Backup optind and force sys.argv[0] = '-c' */
224 optind--;
225 argv[optind] = "-c";
226 }
227
228 PySys_SetArgv(argc-optind, argv+optind);
229
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000230 if ((inspect || (command == NULL && filename == NULL)) &&
231 isatty(fileno(stdin))) {
232 PyObject *v;
233 v = PyImport_ImportModule("readline");
234 if (v == NULL)
235 PyErr_Clear();
236 else
237 Py_DECREF(v);
238 }
239
Guido van Rossum667d7041995-08-04 04:20:48 +0000240 if (command) {
241 sts = PyRun_SimpleString(command) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000242 free(command);
Guido van Rossum667d7041995-08-04 04:20:48 +0000243 }
244 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000245 if (filename == NULL && stdin_is_interactive) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000246 char *startup = getenv("PYTHONSTARTUP");
247 if (startup != NULL && startup[0] != '\0') {
248 FILE *fp = fopen(startup, "r");
249 if (fp != NULL) {
250 (void) PyRun_SimpleFile(fp, startup);
251 PyErr_Clear();
252 fclose(fp);
253 }
254 }
255 }
256 sts = PyRun_AnyFile(
Guido van Rossum775af911997-02-14 19:50:32 +0000257 fp,
258 filename == NULL ? "<stdin>" : filename) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000259 if (filename != NULL)
260 fclose(fp);
261 }
262
Guido van Rossum775af911997-02-14 19:50:32 +0000263 if (inspect && stdin_is_interactive &&
Guido van Rossum667d7041995-08-04 04:20:48 +0000264 (filename != NULL || command != NULL))
265 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
266
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000267 Py_Finalize();
Guido van Rossum05f7c501997-08-02 03:00:42 +0000268 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000269}
270
271
Guido van Rossum667d7041995-08-04 04:20:48 +0000272/* Make the *original* argc/argv available to other modules.
273 This is rare, but it is needed by the secureware extension. */
274
275void
Guido van Rossumac56b031996-07-21 02:33:38 +0000276Py_GetArgcArgv(argc, argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000277 int *argc;
278 char ***argv;
279{
280 *argc = orig_argc;
281 *argv = orig_argv;
282}