blob: ad2616da04092ee5d02e9c2df607cd3ac090709a [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
Marc-André Lemburgda4dbc32001-06-12 16:13:51 +000017#define PYTHONHOMEHELP "<prefix>/pythonX.X"
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 \
Guido van Rossum36002d72001-07-18 16:59:46 +000023 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
24 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000025
Guido van Rossumac56b031996-07-21 02:33:38 +000026/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000027static char **orig_argv;
28static int orig_argc;
29
Guido van Rossumbceccf52001-04-10 22:07:43 +000030/* command line options */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000031#define BASE_OPTS "c:diOSEtuUvxXhVW:"
Guido van Rossumbceccf52001-04-10 22:07:43 +000032
33#ifndef RISCOS
34#define PROGRAM_OPTS BASE_OPTS
35#else /*RISCOS*/
36/* extra option saying that we are running under a special task window
37 frontend; especially my_readline will behave different */
38#define PROGRAM_OPTS BASE_OPTS "w"
39/* corresponding flag */
Guido van Rossum3ed4c152001-03-02 06:18:03 +000040extern int Py_RISCOSWimpFlag;
Guido van Rossumbceccf52001-04-10 22:07:43 +000041#endif /*RISCOS*/
Guido van Rossum3ed4c152001-03-02 06:18:03 +000042
Guido van Rossum667d7041995-08-04 04:20:48 +000043/* Short usage message (with %s for argv0) */
44static char *usage_line =
Guido van Rossum6b86a421999-01-28 15:07:47 +000045"usage: %s [option] ... [-c cmd | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000046
47/* Long usage message, split into parts < 512 bytes */
Guido van Rossuma075ce11997-12-05 21:56:45 +000048static char *usage_top = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000049Options and arguments (and corresponding environment variables):\n\
50-d : debug output from parser (also PYTHONDEBUG=x)\n\
Guido van Rossum775af911997-02-14 19:50:32 +000051-i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000052 and force prompts, even if stdin does not appear to be a terminal\n\
Guido van Rossume7adf3e1998-10-07 14:50:06 +000053-O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000054-OO : remove doc-strings in addition to the -O optimizations\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000055-S : don't imply 'import site' on initialization\n\
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000056-E : ignore environment variables (such as PYTHONPATH)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000057-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000058";
Guido van Rossum7922bd71997-08-29 22:34:47 +000059static char *usage_mid = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000060-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
Guido van Rossumc15a9a12000-05-01 17:54:33 +000061-U : Unicode literals: treats '...' literals like u'...'\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000062-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000063-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000064-h : print this help message and exit\n\
65-V : print the Python version number and exit\n\
Guido van Rossum47f5fdc2000-12-15 22:00:54 +000066-W arg : warning control (arg is action:message:category:module:lineno)\n\
Guido van Rossum775af911997-02-14 19:50:32 +000067-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000068file : program read from script file\n\
69- : program read from stdin (default; interactive mode if a tty)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000070";
71static char *usage_bot = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000072arg ...: arguments passed to program in sys.argv[1:]\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000073Other environment variables:\n\
74PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000075PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000076 default module search path. The result is sys.path.\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000077PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
78 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +000079PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000080";
81
82
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000083static void
84usage(int exitcode, char* program)
85{
86 fprintf(stderr, usage_line, program);
87 fprintf(stderr, usage_top);
88 fprintf(stderr, usage_mid);
89 fprintf(stderr, usage_bot, DELIM, DELIM, PYTHONHOMEHELP);
90 exit(exitcode);
91 /*NOTREACHED*/
92}
93
94
Guido van Rossum667d7041995-08-04 04:20:48 +000095/* Main program */
96
Guido van Rossum9c1201f1998-12-07 14:28:47 +000097DL_EXPORT(int)
Fredrik Lundh620f3772000-07-09 20:42:34 +000098Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +000099{
100 int c;
101 int sts;
102 char *command = NULL;
103 char *filename = NULL;
104 FILE *fp = stdin;
105 char *p;
106 int inspect = 0;
107 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000108 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000109 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000110 int help = 0;
111 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000112 int saw_inspect_flag = 0;
113 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000114 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000115
Guido van Rossumac56b031996-07-21 02:33:38 +0000116 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000117 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000118
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000119#ifdef RISCOS
120 Py_RISCOSWimpFlag = 0;
121#endif
122
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000123 PySys_ResetWarnOptions();
124
Guido van Rossumbceccf52001-04-10 22:07:43 +0000125 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000126 if (c == 'c') {
127 /* -c is the last option; following arguments
128 that look like options are left for the
129 the command to interpret. */
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000130 command = malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000131 if (command == NULL)
132 Py_FatalError(
133 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000134 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000135 strcat(command, "\n");
136 break;
137 }
138
139 switch (c) {
140
141 case 'd':
142 Py_DebugFlag++;
143 break;
144
145 case 'i':
146 inspect++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000147 saw_inspect_flag = 1;
Guido van Rossum775af911997-02-14 19:50:32 +0000148 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000149 break;
150
Guido van Rossum7614da61997-03-03 19:14:45 +0000151 case 'O':
152 Py_OptimizeFlag++;
153 break;
154
Guido van Rossum7922bd71997-08-29 22:34:47 +0000155 case 'S':
156 Py_NoSiteFlag++;
157 break;
158
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000159 case 'E':
160 Py_IgnoreEnvironmentFlag++;
161 break;
162
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000163 case 't':
164 Py_TabcheckFlag++;
165 break;
166
Guido van Rossum667d7041995-08-04 04:20:48 +0000167 case 'u':
168 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000169 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000170 break;
171
172 case 'v':
173 Py_VerboseFlag++;
174 break;
175
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000176#ifdef RISCOS
177 case 'w':
178 Py_RISCOSWimpFlag = 1;
179 break;
180#endif
181
Guido van Rossuma075ce11997-12-05 21:56:45 +0000182 case 'x':
183 skipfirstline = 1;
184 break;
185
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000186 case 'U':
187 Py_UnicodeFlag++;
188 break;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000189 case 'h':
190 help++;
191 break;
192 case 'V':
193 version++;
194 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000195
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000196 case 'W':
197 PySys_AddWarnOption(_PyOS_optarg);
198 break;
199
Guido van Rossum667d7041995-08-04 04:20:48 +0000200 /* This space reserved for other options */
201
202 default:
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000203 usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000204 /*NOTREACHED*/
205
206 }
207 }
208
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000209 if (help)
210 usage(0, argv[0]);
211
212 if (version) {
213 fprintf(stderr, "Python %s\n", PY_VERSION);
214 exit(0);
215 }
216
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000217 if (!saw_inspect_flag &&
218 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
219 inspect = 1;
220 if (!saw_unbuffered_flag &&
221 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
222 unbuffered = 1;
223
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000224 if (command == NULL && _PyOS_optind < argc &&
225 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000226 {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000227 filename = argv[_PyOS_optind];
Guido van Rossum775af911997-02-14 19:50:32 +0000228 if (filename != NULL) {
229 if ((fp = fopen(filename, "r")) == NULL) {
230 fprintf(stderr, "%s: can't open file '%s'\n",
231 argv[0], filename);
232 exit(2);
233 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000234 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000235 int ch;
236 /* Push back first newline so line numbers
237 remain the same */
238 while ((ch = getc(fp)) != EOF) {
239 if (ch == '\n') {
240 (void)ungetc(ch, fp);
241 break;
242 }
243 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000244 }
Guido van Rossum775af911997-02-14 19:50:32 +0000245 }
246 }
247
248 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
249
Guido van Rossum667d7041995-08-04 04:20:48 +0000250 if (unbuffered) {
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000251#ifdef MS_WINDOWS
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000252 _setmode(fileno(stdin), O_BINARY);
253 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000254#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000255#ifndef MPW
Guido van Rossum22ffac11998-03-06 15:30:39 +0000256#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000257 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
258 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
259 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000260#else /* !HAVE_SETVBUF */
261 setbuf(stdin, (char *)NULL);
262 setbuf(stdout, (char *)NULL);
263 setbuf(stderr, (char *)NULL);
264#endif /* !HAVE_SETVBUF */
265#else /* MPW */
Guido van Rossum667d7041995-08-04 04:20:48 +0000266 /* On MPW (3.2) unbuffered seems to hang */
Guido van Rossum775af911997-02-14 19:50:32 +0000267 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000268 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
269 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000270#endif /* MPW */
Guido van Rossum667d7041995-08-04 04:20:48 +0000271 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000272 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000273#ifdef MS_WINDOWS
274 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000275 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000276 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000277#else /* !MS_WINDOWS */
278#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000279 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
280 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000281#endif /* HAVE_SETVBUF */
282#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000283 /* Leave stderr alone - it should be unbuffered anyway. */
284 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000285
Guido van Rossumed52aac1997-07-19 19:20:32 +0000286 Py_SetProgramName(argv[0]);
287 Py_Initialize();
288
Guido van Rossum667d7041995-08-04 04:20:48 +0000289 if (Py_VerboseFlag ||
Guido van Rossum129e91a1997-02-14 21:00:50 +0000290 (command == NULL && filename == NULL && stdin_is_interactive))
Guido van Rossumfe4dfc71997-05-19 18:33:01 +0000291 fprintf(stderr, "Python %s on %s\n%s\n",
Guido van Rossuma22865e2000-09-05 04:41:18 +0000292 Py_GetVersion(), Py_GetPlatform(), COPYRIGHT);
Guido van Rossum667d7041995-08-04 04:20:48 +0000293
Guido van Rossum667d7041995-08-04 04:20:48 +0000294
295 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000296 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
297 _PyOS_optind--;
298 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000299 }
300
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000301 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000302
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000303 if ((inspect || (command == NULL && filename == NULL)) &&
304 isatty(fileno(stdin))) {
305 PyObject *v;
306 v = PyImport_ImportModule("readline");
307 if (v == NULL)
308 PyErr_Clear();
309 else
310 Py_DECREF(v);
311 }
312
Tim Peters5ba58662001-07-16 02:29:45 +0000313 cf.cf_flags = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000314
Guido van Rossum667d7041995-08-04 04:20:48 +0000315 if (command) {
316 sts = PyRun_SimpleString(command) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000317 free(command);
Guido van Rossum667d7041995-08-04 04:20:48 +0000318 }
319 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000320 if (filename == NULL && stdin_is_interactive) {
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000321 char *startup = Py_GETENV("PYTHONSTARTUP");
Guido van Rossum667d7041995-08-04 04:20:48 +0000322 if (startup != NULL && startup[0] != '\0') {
323 FILE *fp = fopen(startup, "r");
324 if (fp != NULL) {
325 (void) PyRun_SimpleFile(fp, startup);
326 PyErr_Clear();
327 fclose(fp);
328 }
329 }
330 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000331 /* XXX */
332 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000333 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000334 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000335 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000336 }
337
Guido van Rossum775af911997-02-14 19:50:32 +0000338 if (inspect && stdin_is_interactive &&
Guido van Rossum667d7041995-08-04 04:20:48 +0000339 (filename != NULL || command != NULL))
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000340 /* XXX */
341 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000342
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000343 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000344#ifdef RISCOS
345 if(Py_RISCOSWimpFlag)
346 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
347#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000348
349#ifdef __INSURE__
350 /* Insure++ is a memory analysis tool that aids in discovering
351 * memory leaks and other memory problems. On Python exit, the
352 * interned string dictionary is flagged as being in use at exit
353 * (which it is). Under normal circumstances, this is fine because
354 * the memory will be automatically reclaimed by the system. Under
355 * memory debugging, it's a huge source of useless noise, so we
356 * trade off slower shutdown for less distraction in the memory
357 * reports. -baw
358 */
359 _Py_ReleaseInternedStrings();
360#endif /* __INSURE__ */
361
Guido van Rossum05f7c501997-08-02 03:00:42 +0000362 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000363}
364
365
Guido van Rossum667d7041995-08-04 04:20:48 +0000366/* Make the *original* argc/argv available to other modules.
367 This is rare, but it is needed by the secureware extension. */
368
369void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000370Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000371{
372 *argc = orig_argc;
373 *argv = orig_argv;
374}