blob: b882ff9e00bb05b1d7e4a3f8a854d9f35ee2f1ea [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
Barry Warsaw83b67091997-08-29 22:20:16 +0000112 Py_UseClassExceptionsFlag = 1;
Guido van Rossum7922bd71997-08-29 22:34:47 +0000113 while ((c = getopt(argc, argv, "c:diOSuvX")) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000114 if (c == 'c') {
115 /* -c is the last option; following arguments
116 that look like options are left for the
117 the command to interpret. */
118 command = malloc(strlen(optarg) + 2);
119 if (command == NULL)
120 Py_FatalError(
121 "not enough memory to copy -c argument");
122 strcpy(command, optarg);
123 strcat(command, "\n");
124 break;
125 }
126
127 switch (c) {
128
129 case 'd':
130 Py_DebugFlag++;
131 break;
132
133 case 'i':
134 inspect++;
Guido van Rossum775af911997-02-14 19:50:32 +0000135 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000136 break;
137
Guido van Rossum7614da61997-03-03 19:14:45 +0000138 case 'O':
139 Py_OptimizeFlag++;
140 break;
141
Guido van Rossum7922bd71997-08-29 22:34:47 +0000142 case 'S':
143 Py_NoSiteFlag++;
144 break;
145
Guido van Rossum667d7041995-08-04 04:20:48 +0000146 case 'u':
147 unbuffered++;
148 break;
149
150 case 'v':
151 Py_VerboseFlag++;
152 break;
153
Barry Warsawf488af31997-08-29 21:57:49 +0000154 case 'X':
Barry Warsaw83b67091997-08-29 22:20:16 +0000155 Py_UseClassExceptionsFlag = 0;
Barry Warsawf488af31997-08-29 21:57:49 +0000156 break;
157
Guido van Rossum667d7041995-08-04 04:20:48 +0000158 /* This space reserved for other options */
159
160 default:
161 fprintf(stderr, usage_line, argv[0]);
162 fprintf(stderr, usage_top);
Guido van Rossum7922bd71997-08-29 22:34:47 +0000163 fprintf(stderr, usage_mid);
Guido van Rossum667d7041995-08-04 04:20:48 +0000164 fprintf(stderr, usage_bot);
165 exit(2);
166 /*NOTREACHED*/
167
168 }
169 }
170
Guido van Rossum775af911997-02-14 19:50:32 +0000171 if (command == NULL && optind < argc &&
172 strcmp(argv[optind], "-") != 0)
173 {
174 filename = argv[optind];
175 if (filename != NULL) {
176 if ((fp = fopen(filename, "r")) == NULL) {
177 fprintf(stderr, "%s: can't open file '%s'\n",
178 argv[0], filename);
179 exit(2);
180 }
181 }
182 }
183
184 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
185
Guido van Rossum667d7041995-08-04 04:20:48 +0000186 if (unbuffered) {
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000187#ifdef MS_WINDOWS
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000188 _setmode(fileno(stdin), O_BINARY);
189 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000190#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000191#ifndef MPW
Guido van Rossum775af911997-02-14 19:50:32 +0000192 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
193 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
194 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000195#else
196 /* On MPW (3.2) unbuffered seems to hang */
Guido van Rossum775af911997-02-14 19:50:32 +0000197 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000198 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
199 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
200#endif
201 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000202 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000203#ifdef MS_WINDOWS
204 /* Doesn't have to have line-buffered -- use unbuffered */
205 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
206 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
207#else
208 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
209 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
210#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000211 /* Leave stderr alone - it should be unbuffered anyway. */
212 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000213
Guido van Rossumed52aac1997-07-19 19:20:32 +0000214 Py_SetProgramName(argv[0]);
215 Py_Initialize();
216
Guido van Rossum667d7041995-08-04 04:20:48 +0000217 if (Py_VerboseFlag ||
Guido van Rossum129e91a1997-02-14 21:00:50 +0000218 (command == NULL && filename == NULL && stdin_is_interactive))
Guido van Rossumfe4dfc71997-05-19 18:33:01 +0000219 fprintf(stderr, "Python %s on %s\n%s\n",
220 Py_GetVersion(), Py_GetPlatform(), Py_GetCopyright());
Guido van Rossum667d7041995-08-04 04:20:48 +0000221
Guido van Rossum667d7041995-08-04 04:20:48 +0000222
223 if (command != NULL) {
224 /* Backup optind and force sys.argv[0] = '-c' */
225 optind--;
226 argv[optind] = "-c";
227 }
228
229 PySys_SetArgv(argc-optind, argv+optind);
230
231 if (command) {
232 sts = PyRun_SimpleString(command) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000233 free(command);
Guido van Rossum667d7041995-08-04 04:20:48 +0000234 }
235 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000236 if (filename == NULL && stdin_is_interactive) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000237 char *startup = getenv("PYTHONSTARTUP");
238 if (startup != NULL && startup[0] != '\0') {
239 FILE *fp = fopen(startup, "r");
240 if (fp != NULL) {
241 (void) PyRun_SimpleFile(fp, startup);
242 PyErr_Clear();
243 fclose(fp);
244 }
245 }
Guido van Rossum9b5dbed1997-08-05 21:34:14 +0000246 if (isatty(fileno(stdin))) {
247 PyObject *v;
248 v = PyImport_ImportModule("readline");
249 if (v == NULL)
250 PyErr_Clear();
251 else
252 Py_DECREF(v);
253 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000254 }
255 sts = PyRun_AnyFile(
Guido van Rossum775af911997-02-14 19:50:32 +0000256 fp,
257 filename == NULL ? "<stdin>" : filename) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000258 if (filename != NULL)
259 fclose(fp);
260 }
261
Guido van Rossum775af911997-02-14 19:50:32 +0000262 if (inspect && stdin_is_interactive &&
Guido van Rossum667d7041995-08-04 04:20:48 +0000263 (filename != NULL || command != NULL))
264 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
265
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000266 Py_Finalize();
Guido van Rossum05f7c501997-08-02 03:00:42 +0000267 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000268}
269
270
Guido van Rossum667d7041995-08-04 04:20:48 +0000271/* Make the *original* argc/argv available to other modules.
272 This is rare, but it is needed by the secureware extension. */
273
274void
Guido van Rossumac56b031996-07-21 02:33:38 +0000275Py_GetArgcArgv(argc, argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000276 int *argc;
277 char ***argv;
278{
279 *argc = orig_argc;
280 *argv = orig_argv;
281}