blob: 72b756be2f99044707703919e076337cbefeebe1 [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 */
31#define BASE_OPTS "c:diOStuUvxXhVW:"
32
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\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000056-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000057";
Guido van Rossum7922bd71997-08-29 22:34:47 +000058static char *usage_mid = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000059-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
Guido van Rossumc15a9a12000-05-01 17:54:33 +000060-U : Unicode literals: treats '...' literals like u'...'\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000061-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000062-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000063-h : print this help message and exit\n\
64-V : print the Python version number and exit\n\
Guido van Rossum47f5fdc2000-12-15 22:00:54 +000065-W arg : warning control (arg is action:message:category:module:lineno)\n\
Guido van Rossum775af911997-02-14 19:50:32 +000066-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000067file : program read from script file\n\
68- : program read from stdin (default; interactive mode if a tty)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000069";
70static char *usage_bot = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000071arg ...: arguments passed to program in sys.argv[1:]\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000072Other environment variables:\n\
73PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000074PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000075 default module search path. The result is sys.path.\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000076PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
77 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +000078PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000079";
80
81
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000082static void
83usage(int exitcode, char* program)
84{
85 fprintf(stderr, usage_line, program);
86 fprintf(stderr, usage_top);
87 fprintf(stderr, usage_mid);
88 fprintf(stderr, usage_bot, DELIM, DELIM, PYTHONHOMEHELP);
89 exit(exitcode);
90 /*NOTREACHED*/
91}
92
93
Guido van Rossum667d7041995-08-04 04:20:48 +000094/* Main program */
95
Guido van Rossum9c1201f1998-12-07 14:28:47 +000096DL_EXPORT(int)
Fredrik Lundh620f3772000-07-09 20:42:34 +000097Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +000098{
99 int c;
100 int sts;
101 char *command = NULL;
102 char *filename = NULL;
103 FILE *fp = stdin;
104 char *p;
105 int inspect = 0;
106 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000107 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000108 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000109 int help = 0;
110 int version = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000111 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000112
Guido van Rossumac56b031996-07-21 02:33:38 +0000113 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000114 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000115
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000116#ifdef RISCOS
117 Py_RISCOSWimpFlag = 0;
118#endif
119
Guido van Rossum667d7041995-08-04 04:20:48 +0000120 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
121 inspect = 1;
122 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
123 unbuffered = 1;
124
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000125 PySys_ResetWarnOptions();
126
Guido van Rossumbceccf52001-04-10 22:07:43 +0000127 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000128 if (c == 'c') {
129 /* -c is the last option; following arguments
130 that look like options are left for the
131 the command to interpret. */
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000132 command = malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000133 if (command == NULL)
134 Py_FatalError(
135 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000136 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000137 strcat(command, "\n");
138 break;
139 }
140
141 switch (c) {
142
143 case 'd':
144 Py_DebugFlag++;
145 break;
146
147 case 'i':
148 inspect++;
Guido van Rossum775af911997-02-14 19:50:32 +0000149 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000150 break;
151
Guido van Rossum7614da61997-03-03 19:14:45 +0000152 case 'O':
153 Py_OptimizeFlag++;
154 break;
155
Guido van Rossum7922bd71997-08-29 22:34:47 +0000156 case 'S':
157 Py_NoSiteFlag++;
158 break;
159
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000160 case 't':
161 Py_TabcheckFlag++;
162 break;
163
Guido van Rossum667d7041995-08-04 04:20:48 +0000164 case 'u':
165 unbuffered++;
166 break;
167
168 case 'v':
169 Py_VerboseFlag++;
170 break;
171
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000172#ifdef RISCOS
173 case 'w':
174 Py_RISCOSWimpFlag = 1;
175 break;
176#endif
177
Guido van Rossuma075ce11997-12-05 21:56:45 +0000178 case 'x':
179 skipfirstline = 1;
180 break;
181
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000182 case 'U':
183 Py_UnicodeFlag++;
184 break;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000185 case 'h':
186 help++;
187 break;
188 case 'V':
189 version++;
190 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000191
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000192 case 'W':
193 PySys_AddWarnOption(_PyOS_optarg);
194 break;
195
Guido van Rossum667d7041995-08-04 04:20:48 +0000196 /* This space reserved for other options */
197
198 default:
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000199 usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000200 /*NOTREACHED*/
201
202 }
203 }
204
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000205 if (help)
206 usage(0, argv[0]);
207
208 if (version) {
209 fprintf(stderr, "Python %s\n", PY_VERSION);
210 exit(0);
211 }
212
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000213 if (command == NULL && _PyOS_optind < argc &&
214 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000215 {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000216 filename = argv[_PyOS_optind];
Guido van Rossum775af911997-02-14 19:50:32 +0000217 if (filename != NULL) {
218 if ((fp = fopen(filename, "r")) == NULL) {
219 fprintf(stderr, "%s: can't open file '%s'\n",
220 argv[0], filename);
221 exit(2);
222 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000223 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000224 int ch;
225 /* Push back first newline so line numbers
226 remain the same */
227 while ((ch = getc(fp)) != EOF) {
228 if (ch == '\n') {
229 (void)ungetc(ch, fp);
230 break;
231 }
232 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000233 }
Guido van Rossum775af911997-02-14 19:50:32 +0000234 }
235 }
236
237 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
238
Guido van Rossum667d7041995-08-04 04:20:48 +0000239 if (unbuffered) {
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000240#ifdef MS_WINDOWS
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000241 _setmode(fileno(stdin), O_BINARY);
242 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000243#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000244#ifndef MPW
Guido van Rossum22ffac11998-03-06 15:30:39 +0000245#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000246 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
247 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
248 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000249#else /* !HAVE_SETVBUF */
250 setbuf(stdin, (char *)NULL);
251 setbuf(stdout, (char *)NULL);
252 setbuf(stderr, (char *)NULL);
253#endif /* !HAVE_SETVBUF */
254#else /* MPW */
Guido van Rossum667d7041995-08-04 04:20:48 +0000255 /* On MPW (3.2) unbuffered seems to hang */
Guido van Rossum775af911997-02-14 19:50:32 +0000256 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000257 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
258 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000259#endif /* MPW */
Guido van Rossum667d7041995-08-04 04:20:48 +0000260 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000261 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000262#ifdef MS_WINDOWS
263 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000264 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000265 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000266#else /* !MS_WINDOWS */
267#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000268 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
269 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000270#endif /* HAVE_SETVBUF */
271#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000272 /* Leave stderr alone - it should be unbuffered anyway. */
273 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000274
Guido van Rossumed52aac1997-07-19 19:20:32 +0000275 Py_SetProgramName(argv[0]);
276 Py_Initialize();
277
Guido van Rossum667d7041995-08-04 04:20:48 +0000278 if (Py_VerboseFlag ||
Guido van Rossum129e91a1997-02-14 21:00:50 +0000279 (command == NULL && filename == NULL && stdin_is_interactive))
Guido van Rossumfe4dfc71997-05-19 18:33:01 +0000280 fprintf(stderr, "Python %s on %s\n%s\n",
Guido van Rossuma22865e2000-09-05 04:41:18 +0000281 Py_GetVersion(), Py_GetPlatform(), COPYRIGHT);
Guido van Rossum667d7041995-08-04 04:20:48 +0000282
Guido van Rossum667d7041995-08-04 04:20:48 +0000283
284 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000285 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
286 _PyOS_optind--;
287 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000288 }
289
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000290 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000291
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000292 if ((inspect || (command == NULL && filename == NULL)) &&
293 isatty(fileno(stdin))) {
294 PyObject *v;
295 v = PyImport_ImportModule("readline");
296 if (v == NULL)
297 PyErr_Clear();
298 else
299 Py_DECREF(v);
300 }
301
Tim Peters5ba58662001-07-16 02:29:45 +0000302 cf.cf_flags = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000303
Guido van Rossum667d7041995-08-04 04:20:48 +0000304 if (command) {
305 sts = PyRun_SimpleString(command) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000306 free(command);
Guido van Rossum667d7041995-08-04 04:20:48 +0000307 }
308 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000309 if (filename == NULL && stdin_is_interactive) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000310 char *startup = getenv("PYTHONSTARTUP");
311 if (startup != NULL && startup[0] != '\0') {
312 FILE *fp = fopen(startup, "r");
313 if (fp != NULL) {
314 (void) PyRun_SimpleFile(fp, startup);
315 PyErr_Clear();
316 fclose(fp);
317 }
318 }
319 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000320 /* XXX */
321 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000322 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000323 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000324 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000325 }
326
Guido van Rossum775af911997-02-14 19:50:32 +0000327 if (inspect && stdin_is_interactive &&
Guido van Rossum667d7041995-08-04 04:20:48 +0000328 (filename != NULL || command != NULL))
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000329 /* XXX */
330 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000331
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000332 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000333#ifdef RISCOS
334 if(Py_RISCOSWimpFlag)
335 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
336#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000337
338#ifdef __INSURE__
339 /* Insure++ is a memory analysis tool that aids in discovering
340 * memory leaks and other memory problems. On Python exit, the
341 * interned string dictionary is flagged as being in use at exit
342 * (which it is). Under normal circumstances, this is fine because
343 * the memory will be automatically reclaimed by the system. Under
344 * memory debugging, it's a huge source of useless noise, so we
345 * trade off slower shutdown for less distraction in the memory
346 * reports. -baw
347 */
348 _Py_ReleaseInternedStrings();
349#endif /* __INSURE__ */
350
Guido van Rossum05f7c501997-08-02 03:00:42 +0000351 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000352}
353
354
Guido van Rossum667d7041995-08-04 04:20:48 +0000355/* Make the *original* argc/argv available to other modules.
356 This is rare, but it is needed by the secureware extension. */
357
358void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000359Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000360{
361 *argc = orig_argc;
362 *argv = orig_argv;
363}