blob: 0b35a9415e8dc906edbfdcb3660dcd53e74e350b [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 Rossum44c36bb1997-10-08 22:49:17 +000040#ifdef HAVE_LOCALE_H
41#include <locale.h>
42#endif
43
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000044#ifdef MS_WINDOWS
45#include <fcntl.h>
46#endif
47
Guido van Rossum667d7041995-08-04 04:20:48 +000048/* Interface to getopt(): */
49extern int optind;
50extern char *optarg;
51extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
52
53
Guido van Rossumac56b031996-07-21 02:33:38 +000054/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000055static char **orig_argv;
56static int orig_argc;
57
Guido van Rossum667d7041995-08-04 04:20:48 +000058/* Short usage message (with %s for argv0) */
59static char *usage_line =
Guido van Rossum7922bd71997-08-29 22:34:47 +000060"usage: %s [-d] [-i] [-O] [-S] [-u] [-v] [-X] [-c cmd | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000061
62/* Long usage message, split into parts < 512 bytes */
63static char *usage_top = "\n\
64Options and arguments (and corresponding environment variables):\n\
65-d : debug output from parser (also PYTHONDEBUG=x)\n\
Guido van Rossum775af911997-02-14 19:50:32 +000066-i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
67 and force prompts, even if stdin does not appear to be a terminal.\n\
Guido van Rossum7614da61997-03-03 19:14:45 +000068-O : optimize generated bytecode (a tad).\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000069-S : don't imply 'import site' on initialization\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000070";
Guido van Rossum7922bd71997-08-29 22:34:47 +000071static char *usage_mid = "\
72-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
73-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
74-X : disable class based built-in exceptions\n\
Guido van Rossum775af911997-02-14 19:50:32 +000075-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000076file : program read from script file\n\
77- : program read from stdin (default; interactive mode if a tty)\n\
78arg ...: arguments passed to program in sys.argv[1:]\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000079";
80static char *usage_bot = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000081\n\
82Other environment variables:\n\
83PYTHONSTARTUP: file executed on interactive startup (no default)\n\
84PYTHONPATH : colon-separated list of directories prefixed to the\n\
85 default module search path. The result is sys.path.\n\
Guido van Rossum03ef6471997-04-30 19:48:59 +000086PYTHONHOME : alternate <prefix> directory (or <prefix>:<exec_prefix>).\n\
87 The default module search path uses <prefix>/lib/python1.5.\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000088";
89
90
91/* Main program */
92
93int
Guido van Rossumed52aac1997-07-19 19:20:32 +000094Py_Main(argc, argv)
Guido van Rossum667d7041995-08-04 04:20:48 +000095 int argc;
96 char **argv;
97{
98 int c;
99 int sts;
100 char *command = NULL;
101 char *filename = NULL;
102 FILE *fp = stdin;
103 char *p;
104 int inspect = 0;
105 int unbuffered = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000106 int stdin_is_interactive = 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000107
Guido van Rossum44c36bb1997-10-08 22:49:17 +0000108#ifdef HAVE_SETLOCALE
109 setlocale(LC_ALL, "");
110#endif
111
Guido van Rossumac56b031996-07-21 02:33:38 +0000112 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000113 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000114
Guido van Rossum667d7041995-08-04 04:20:48 +0000115 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
116 inspect = 1;
117 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
118 unbuffered = 1;
119
Guido van Rossum7922bd71997-08-29 22:34:47 +0000120 while ((c = getopt(argc, argv, "c:diOSuvX")) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000121 if (c == 'c') {
122 /* -c is the last option; following arguments
123 that look like options are left for the
124 the command to interpret. */
125 command = malloc(strlen(optarg) + 2);
126 if (command == NULL)
127 Py_FatalError(
128 "not enough memory to copy -c argument");
129 strcpy(command, optarg);
130 strcat(command, "\n");
131 break;
132 }
133
134 switch (c) {
135
136 case 'd':
137 Py_DebugFlag++;
138 break;
139
140 case 'i':
141 inspect++;
Guido van Rossum775af911997-02-14 19:50:32 +0000142 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000143 break;
144
Guido van Rossum7614da61997-03-03 19:14:45 +0000145 case 'O':
146 Py_OptimizeFlag++;
147 break;
148
Guido van Rossum7922bd71997-08-29 22:34:47 +0000149 case 'S':
150 Py_NoSiteFlag++;
151 break;
152
Guido van Rossum667d7041995-08-04 04:20:48 +0000153 case 'u':
154 unbuffered++;
155 break;
156
157 case 'v':
158 Py_VerboseFlag++;
159 break;
160
Barry Warsawf488af31997-08-29 21:57:49 +0000161 case 'X':
Barry Warsaw83b67091997-08-29 22:20:16 +0000162 Py_UseClassExceptionsFlag = 0;
Barry Warsawf488af31997-08-29 21:57:49 +0000163 break;
164
Guido van Rossum667d7041995-08-04 04:20:48 +0000165 /* This space reserved for other options */
166
167 default:
168 fprintf(stderr, usage_line, argv[0]);
169 fprintf(stderr, usage_top);
Guido van Rossum7922bd71997-08-29 22:34:47 +0000170 fprintf(stderr, usage_mid);
Guido van Rossum667d7041995-08-04 04:20:48 +0000171 fprintf(stderr, usage_bot);
172 exit(2);
173 /*NOTREACHED*/
174
175 }
176 }
177
Guido van Rossum775af911997-02-14 19:50:32 +0000178 if (command == NULL && optind < argc &&
179 strcmp(argv[optind], "-") != 0)
180 {
181 filename = argv[optind];
182 if (filename != NULL) {
183 if ((fp = fopen(filename, "r")) == NULL) {
184 fprintf(stderr, "%s: can't open file '%s'\n",
185 argv[0], filename);
186 exit(2);
187 }
188 }
189 }
190
191 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
192
Guido van Rossum667d7041995-08-04 04:20:48 +0000193 if (unbuffered) {
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000194#ifdef MS_WINDOWS
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000195 _setmode(fileno(stdin), O_BINARY);
196 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000197#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000198#ifndef MPW
Guido van Rossum775af911997-02-14 19:50:32 +0000199 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
200 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
201 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000202#else
203 /* On MPW (3.2) unbuffered seems to hang */
Guido van Rossum775af911997-02-14 19:50:32 +0000204 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000205 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
206 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
207#endif
208 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000209 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000210#ifdef MS_WINDOWS
211 /* Doesn't have to have line-buffered -- use unbuffered */
212 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
213 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
214#else
215 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
216 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
217#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000218 /* Leave stderr alone - it should be unbuffered anyway. */
219 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000220
Guido van Rossumed52aac1997-07-19 19:20:32 +0000221 Py_SetProgramName(argv[0]);
222 Py_Initialize();
223
Guido van Rossum667d7041995-08-04 04:20:48 +0000224 if (Py_VerboseFlag ||
Guido van Rossum129e91a1997-02-14 21:00:50 +0000225 (command == NULL && filename == NULL && stdin_is_interactive))
Guido van Rossumfe4dfc71997-05-19 18:33:01 +0000226 fprintf(stderr, "Python %s on %s\n%s\n",
227 Py_GetVersion(), Py_GetPlatform(), Py_GetCopyright());
Guido van Rossum667d7041995-08-04 04:20:48 +0000228
Guido van Rossum667d7041995-08-04 04:20:48 +0000229
230 if (command != NULL) {
231 /* Backup optind and force sys.argv[0] = '-c' */
232 optind--;
233 argv[optind] = "-c";
234 }
235
236 PySys_SetArgv(argc-optind, argv+optind);
237
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000238 if ((inspect || (command == NULL && filename == NULL)) &&
239 isatty(fileno(stdin))) {
240 PyObject *v;
241 v = PyImport_ImportModule("readline");
242 if (v == NULL)
243 PyErr_Clear();
244 else
245 Py_DECREF(v);
246 }
247
Guido van Rossum667d7041995-08-04 04:20:48 +0000248 if (command) {
249 sts = PyRun_SimpleString(command) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000250 free(command);
Guido van Rossum667d7041995-08-04 04:20:48 +0000251 }
252 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000253 if (filename == NULL && stdin_is_interactive) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000254 char *startup = getenv("PYTHONSTARTUP");
255 if (startup != NULL && startup[0] != '\0') {
256 FILE *fp = fopen(startup, "r");
257 if (fp != NULL) {
258 (void) PyRun_SimpleFile(fp, startup);
259 PyErr_Clear();
260 fclose(fp);
261 }
262 }
263 }
264 sts = PyRun_AnyFile(
Guido van Rossum775af911997-02-14 19:50:32 +0000265 fp,
266 filename == NULL ? "<stdin>" : filename) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000267 if (filename != NULL)
268 fclose(fp);
269 }
270
Guido van Rossum775af911997-02-14 19:50:32 +0000271 if (inspect && stdin_is_interactive &&
Guido van Rossum667d7041995-08-04 04:20:48 +0000272 (filename != NULL || command != NULL))
273 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
274
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000275 Py_Finalize();
Guido van Rossum05f7c501997-08-02 03:00:42 +0000276 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000277}
278
279
Guido van Rossum667d7041995-08-04 04:20:48 +0000280/* Make the *original* argc/argv available to other modules.
281 This is rare, but it is needed by the secureware extension. */
282
283void
Guido van Rossumac56b031996-07-21 02:33:38 +0000284Py_GetArgcArgv(argc, argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000285 int *argc;
286 char ***argv;
287{
288 *argc = orig_argc;
289 *argv = orig_argv;
290}