blob: 03dff1888f6b790c3777b7c2db834a3dc6201da6 [file] [log] [blame]
Guido van Rossum667d7041995-08-04 04:20:48 +00001/* Python interpreter main program */
2
3#include "Python.h"
Guido van Rossuma075ce11997-12-05 21:56:45 +00004#include "osdefs.h"
Guido van Rossum667d7041995-08-04 04:20:48 +00005
Guido van Rossum66a70131996-12-09 18:46:58 +00006#ifdef HAVE_UNISTD_H
7#include <unistd.h>
8#endif
Guido van Rossum667d7041995-08-04 04:20:48 +00009
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000010#ifdef MS_WINDOWS
11#include <fcntl.h>
12#endif
13
Guido van Rossuma075ce11997-12-05 21:56:45 +000014#if defined(PYOS_OS2) || defined(MS_WINDOWS)
15#define PYTHONHOMEHELP "<prefix>\\lib"
16#else
Fredrik Lundh620f3772000-07-09 20:42:34 +000017#define PYTHONHOMEHELP "<prefix>/python2.0"
Guido van Rossuma075ce11997-12-05 21:56:45 +000018#endif
19
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000020#include "pygetopt.h"
21
Guido van Rossuma22865e2000-09-05 04:41:18 +000022#define COPYRIGHT \
23 "Type \"copyright\", \"credits\" or \"license\" for more information."
24
Guido van Rossumac56b031996-07-21 02:33:38 +000025/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000026static char **orig_argv;
27static int orig_argc;
28
Guido van Rossum3ed4c152001-03-02 06:18:03 +000029/* For my_readline when running under RISCOS */
30#ifdef RISCOS
31extern int Py_RISCOSWimpFlag;
32#endif
33
Guido van Rossum667d7041995-08-04 04:20:48 +000034/* Short usage message (with %s for argv0) */
35static char *usage_line =
Guido van Rossum6b86a421999-01-28 15:07:47 +000036"usage: %s [option] ... [-c cmd | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000037
38/* Long usage message, split into parts < 512 bytes */
Guido van Rossuma075ce11997-12-05 21:56:45 +000039static char *usage_top = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000040Options and arguments (and corresponding environment variables):\n\
41-d : debug output from parser (also PYTHONDEBUG=x)\n\
Guido van Rossum775af911997-02-14 19:50:32 +000042-i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000043 and force prompts, even if stdin does not appear to be a terminal\n\
Guido van Rossume7adf3e1998-10-07 14:50:06 +000044-O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000045-OO : remove doc-strings in addition to the -O optimizations\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000046-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000047-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000048";
Guido van Rossum7922bd71997-08-29 22:34:47 +000049static char *usage_mid = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000050-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
Guido van Rossumc15a9a12000-05-01 17:54:33 +000051-U : Unicode literals: treats '...' literals like u'...'\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000052-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000053-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000054-h : print this help message and exit\n\
55-V : print the Python version number and exit\n\
Guido van Rossum47f5fdc2000-12-15 22:00:54 +000056-W arg : warning control (arg is action:message:category:module:lineno)\n\
Guido van Rossum775af911997-02-14 19:50:32 +000057-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000058file : program read from script file\n\
59- : program read from stdin (default; interactive mode if a tty)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000060";
61static char *usage_bot = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000062arg ...: arguments passed to program in sys.argv[1:]\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000063Other environment variables:\n\
64PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000065PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000066 default module search path. The result is sys.path.\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000067PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
68 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +000069PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000070";
71
72
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000073static void
74usage(int exitcode, char* program)
75{
76 fprintf(stderr, usage_line, program);
77 fprintf(stderr, usage_top);
78 fprintf(stderr, usage_mid);
79 fprintf(stderr, usage_bot, DELIM, DELIM, PYTHONHOMEHELP);
80 exit(exitcode);
81 /*NOTREACHED*/
82}
83
84
Guido van Rossum667d7041995-08-04 04:20:48 +000085/* Main program */
86
Guido van Rossum9c1201f1998-12-07 14:28:47 +000087DL_EXPORT(int)
Fredrik Lundh620f3772000-07-09 20:42:34 +000088Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +000089{
90 int c;
91 int sts;
92 char *command = NULL;
93 char *filename = NULL;
94 FILE *fp = stdin;
95 char *p;
96 int inspect = 0;
97 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +000098 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +000099 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000100 int help = 0;
101 int version = 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000102
Guido van Rossumac56b031996-07-21 02:33:38 +0000103 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000104 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000105
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000106#ifdef RISCOS
107 Py_RISCOSWimpFlag = 0;
108#endif
109
Guido van Rossum667d7041995-08-04 04:20:48 +0000110 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
111 inspect = 1;
112 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
113 unbuffered = 1;
114
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000115 PySys_ResetWarnOptions();
116
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000117#ifdef RISCOS
118 while ((c = getopt(argc, argv, "c:diOStuUvwxXhV")) != EOF) {
119#else
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000120 while ((c = _PyOS_GetOpt(argc, argv, "c:diOStuUvxXhVW:")) != EOF) {
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000121#endif
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. */
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000126 command = malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000127 if (command == NULL)
128 Py_FatalError(
129 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000130 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000131 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 Rossum3ed4c152001-03-02 06:18:03 +0000166#ifdef RISCOS
167 case 'w':
168 Py_RISCOSWimpFlag = 1;
169 break;
170#endif
171
Guido van Rossuma075ce11997-12-05 21:56:45 +0000172 case 'x':
173 skipfirstline = 1;
174 break;
175
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000176 case 'U':
177 Py_UnicodeFlag++;
178 break;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000179 case 'h':
180 help++;
181 break;
182 case 'V':
183 version++;
184 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000185
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000186 case 'W':
187 PySys_AddWarnOption(_PyOS_optarg);
188 break;
189
Guido van Rossum667d7041995-08-04 04:20:48 +0000190 /* This space reserved for other options */
191
192 default:
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000193 usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000194 /*NOTREACHED*/
195
196 }
197 }
198
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000199 if (help)
200 usage(0, argv[0]);
201
202 if (version) {
203 fprintf(stderr, "Python %s\n", PY_VERSION);
204 exit(0);
205 }
206
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000207 if (command == NULL && _PyOS_optind < argc &&
208 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000209 {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000210 filename = argv[_PyOS_optind];
Guido van Rossum775af911997-02-14 19:50:32 +0000211 if (filename != NULL) {
212 if ((fp = fopen(filename, "r")) == NULL) {
213 fprintf(stderr, "%s: can't open file '%s'\n",
214 argv[0], filename);
215 exit(2);
216 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000217 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000218 int ch;
219 /* Push back first newline so line numbers
220 remain the same */
221 while ((ch = getc(fp)) != EOF) {
222 if (ch == '\n') {
223 (void)ungetc(ch, fp);
224 break;
225 }
226 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000227 }
Guido van Rossum775af911997-02-14 19:50:32 +0000228 }
229 }
230
231 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
232
Guido van Rossum667d7041995-08-04 04:20:48 +0000233 if (unbuffered) {
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000234#ifdef MS_WINDOWS
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000235 _setmode(fileno(stdin), O_BINARY);
236 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000237#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000238#ifndef MPW
Guido van Rossum22ffac11998-03-06 15:30:39 +0000239#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000240 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
241 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
242 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000243#else /* !HAVE_SETVBUF */
244 setbuf(stdin, (char *)NULL);
245 setbuf(stdout, (char *)NULL);
246 setbuf(stderr, (char *)NULL);
247#endif /* !HAVE_SETVBUF */
248#else /* MPW */
Guido van Rossum667d7041995-08-04 04:20:48 +0000249 /* On MPW (3.2) unbuffered seems to hang */
Guido van Rossum775af911997-02-14 19:50:32 +0000250 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000251 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
252 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000253#endif /* MPW */
Guido van Rossum667d7041995-08-04 04:20:48 +0000254 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000255 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000256#ifdef MS_WINDOWS
257 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000258 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000259 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000260#else /* !MS_WINDOWS */
261#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000262 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
263 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000264#endif /* HAVE_SETVBUF */
265#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000266 /* Leave stderr alone - it should be unbuffered anyway. */
267 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000268
Guido van Rossumed52aac1997-07-19 19:20:32 +0000269 Py_SetProgramName(argv[0]);
270 Py_Initialize();
271
Guido van Rossum667d7041995-08-04 04:20:48 +0000272 if (Py_VerboseFlag ||
Guido van Rossum129e91a1997-02-14 21:00:50 +0000273 (command == NULL && filename == NULL && stdin_is_interactive))
Guido van Rossumfe4dfc71997-05-19 18:33:01 +0000274 fprintf(stderr, "Python %s on %s\n%s\n",
Guido van Rossuma22865e2000-09-05 04:41:18 +0000275 Py_GetVersion(), Py_GetPlatform(), COPYRIGHT);
Guido van Rossum667d7041995-08-04 04:20:48 +0000276
Guido van Rossum667d7041995-08-04 04:20:48 +0000277
278 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000279 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
280 _PyOS_optind--;
281 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000282 }
283
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000284 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000285
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000286 if ((inspect || (command == NULL && filename == NULL)) &&
287 isatty(fileno(stdin))) {
288 PyObject *v;
289 v = PyImport_ImportModule("readline");
290 if (v == NULL)
291 PyErr_Clear();
292 else
293 Py_DECREF(v);
294 }
295
Guido van Rossum667d7041995-08-04 04:20:48 +0000296 if (command) {
297 sts = PyRun_SimpleString(command) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000298 free(command);
Guido van Rossum667d7041995-08-04 04:20:48 +0000299 }
300 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000301 if (filename == NULL && stdin_is_interactive) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000302 char *startup = getenv("PYTHONSTARTUP");
303 if (startup != NULL && startup[0] != '\0') {
304 FILE *fp = fopen(startup, "r");
305 if (fp != NULL) {
306 (void) PyRun_SimpleFile(fp, startup);
307 PyErr_Clear();
308 fclose(fp);
309 }
310 }
311 }
Guido van Rossum0df002c2000-08-27 19:21:52 +0000312 sts = PyRun_AnyFileEx(
Guido van Rossum775af911997-02-14 19:50:32 +0000313 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000314 filename == NULL ? "<stdin>" : filename,
315 filename != NULL) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000316 }
317
Guido van Rossum775af911997-02-14 19:50:32 +0000318 if (inspect && stdin_is_interactive &&
Guido van Rossum667d7041995-08-04 04:20:48 +0000319 (filename != NULL || command != NULL))
320 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
321
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000322 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000323#ifdef RISCOS
324 if(Py_RISCOSWimpFlag)
325 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
326#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000327
328#ifdef __INSURE__
329 /* Insure++ is a memory analysis tool that aids in discovering
330 * memory leaks and other memory problems. On Python exit, the
331 * interned string dictionary is flagged as being in use at exit
332 * (which it is). Under normal circumstances, this is fine because
333 * the memory will be automatically reclaimed by the system. Under
334 * memory debugging, it's a huge source of useless noise, so we
335 * trade off slower shutdown for less distraction in the memory
336 * reports. -baw
337 */
338 _Py_ReleaseInternedStrings();
339#endif /* __INSURE__ */
340
Guido van Rossum05f7c501997-08-02 03:00:42 +0000341 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000342}
343
344
Guido van Rossum667d7041995-08-04 04:20:48 +0000345/* Make the *original* argc/argv available to other modules.
346 This is rare, but it is needed by the secureware extension. */
347
348void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000349Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000350{
351 *argc = orig_argc;
352 *argv = orig_argv;
353}