blob: 64abaf3eb27eab6898f421a15797a0e305d775f6 [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 \
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 Rossumbceccf52001-04-10 22:07:43 +000029/* command line options */
30#define BASE_OPTS "c:diOStuUvxXhVW:"
31
32#ifndef RISCOS
33#define PROGRAM_OPTS BASE_OPTS
34#else /*RISCOS*/
35/* extra option saying that we are running under a special task window
36 frontend; especially my_readline will behave different */
37#define PROGRAM_OPTS BASE_OPTS "w"
38/* corresponding flag */
Guido van Rossum3ed4c152001-03-02 06:18:03 +000039extern int Py_RISCOSWimpFlag;
Guido van Rossumbceccf52001-04-10 22:07:43 +000040#endif /*RISCOS*/
Guido van Rossum3ed4c152001-03-02 06:18:03 +000041
Guido van Rossum667d7041995-08-04 04:20:48 +000042/* Short usage message (with %s for argv0) */
43static char *usage_line =
Guido van Rossum6b86a421999-01-28 15:07:47 +000044"usage: %s [option] ... [-c cmd | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000045
46/* Long usage message, split into parts < 512 bytes */
Guido van Rossuma075ce11997-12-05 21:56:45 +000047static char *usage_top = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000048Options and arguments (and corresponding environment variables):\n\
49-d : debug output from parser (also PYTHONDEBUG=x)\n\
Guido van Rossum775af911997-02-14 19:50:32 +000050-i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000051 and force prompts, even if stdin does not appear to be a terminal\n\
Guido van Rossume7adf3e1998-10-07 14:50:06 +000052-O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000053-OO : remove doc-strings in addition to the -O optimizations\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000054-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000055-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000056";
Guido van Rossum7922bd71997-08-29 22:34:47 +000057static char *usage_mid = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000058-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
Guido van Rossumc15a9a12000-05-01 17:54:33 +000059-U : Unicode literals: treats '...' literals like u'...'\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000060-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000061-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000062-h : print this help message and exit\n\
63-V : print the Python version number and exit\n\
Guido van Rossum47f5fdc2000-12-15 22:00:54 +000064-W arg : warning control (arg is action:message:category:module:lineno)\n\
Guido van Rossum775af911997-02-14 19:50:32 +000065-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000066file : program read from script file\n\
67- : program read from stdin (default; interactive mode if a tty)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000068";
69static char *usage_bot = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000070arg ...: arguments passed to program in sys.argv[1:]\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000071Other environment variables:\n\
72PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000073PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000074 default module search path. The result is sys.path.\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000075PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
76 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +000077PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000078";
79
80
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000081static void
82usage(int exitcode, char* program)
83{
84 fprintf(stderr, usage_line, program);
85 fprintf(stderr, usage_top);
86 fprintf(stderr, usage_mid);
87 fprintf(stderr, usage_bot, DELIM, DELIM, PYTHONHOMEHELP);
88 exit(exitcode);
89 /*NOTREACHED*/
90}
91
92
Guido van Rossum667d7041995-08-04 04:20:48 +000093/* Main program */
94
Guido van Rossum9c1201f1998-12-07 14:28:47 +000095DL_EXPORT(int)
Fredrik Lundh620f3772000-07-09 20:42:34 +000096Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +000097{
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 Rossuma075ce11997-12-05 21:56:45 +0000106 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000107 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000108 int help = 0;
109 int version = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000110 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000111
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 Rossum3ed4c152001-03-02 06:18:03 +0000115#ifdef RISCOS
116 Py_RISCOSWimpFlag = 0;
117#endif
118
Guido van Rossum667d7041995-08-04 04:20:48 +0000119 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
120 inspect = 1;
121 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
122 unbuffered = 1;
123
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000124 PySys_ResetWarnOptions();
125
Guido van Rossumbceccf52001-04-10 22:07:43 +0000126 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000127 if (c == 'c') {
128 /* -c is the last option; following arguments
129 that look like options are left for the
130 the command to interpret. */
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000131 command = malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000132 if (command == NULL)
133 Py_FatalError(
134 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000135 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000136 strcat(command, "\n");
137 break;
138 }
139
140 switch (c) {
141
142 case 'd':
143 Py_DebugFlag++;
144 break;
145
146 case 'i':
147 inspect++;
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
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000159 case 't':
160 Py_TabcheckFlag++;
161 break;
162
Guido van Rossum667d7041995-08-04 04:20:48 +0000163 case 'u':
164 unbuffered++;
165 break;
166
167 case 'v':
168 Py_VerboseFlag++;
169 break;
170
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000171#ifdef RISCOS
172 case 'w':
173 Py_RISCOSWimpFlag = 1;
174 break;
175#endif
176
Guido van Rossuma075ce11997-12-05 21:56:45 +0000177 case 'x':
178 skipfirstline = 1;
179 break;
180
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000181 case 'U':
182 Py_UnicodeFlag++;
183 break;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000184 case 'h':
185 help++;
186 break;
187 case 'V':
188 version++;
189 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000190
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000191 case 'W':
192 PySys_AddWarnOption(_PyOS_optarg);
193 break;
194
Guido van Rossum667d7041995-08-04 04:20:48 +0000195 /* This space reserved for other options */
196
197 default:
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000198 usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000199 /*NOTREACHED*/
200
201 }
202 }
203
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000204 if (help)
205 usage(0, argv[0]);
206
207 if (version) {
208 fprintf(stderr, "Python %s\n", PY_VERSION);
209 exit(0);
210 }
211
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000212 if (command == NULL && _PyOS_optind < argc &&
213 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000214 {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000215 filename = argv[_PyOS_optind];
Guido van Rossum775af911997-02-14 19:50:32 +0000216 if (filename != NULL) {
217 if ((fp = fopen(filename, "r")) == NULL) {
218 fprintf(stderr, "%s: can't open file '%s'\n",
219 argv[0], filename);
220 exit(2);
221 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000222 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000223 int ch;
224 /* Push back first newline so line numbers
225 remain the same */
226 while ((ch = getc(fp)) != EOF) {
227 if (ch == '\n') {
228 (void)ungetc(ch, fp);
229 break;
230 }
231 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000232 }
Guido van Rossum775af911997-02-14 19:50:32 +0000233 }
234 }
235
236 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
237
Guido van Rossum667d7041995-08-04 04:20:48 +0000238 if (unbuffered) {
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000239#ifdef MS_WINDOWS
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000240 _setmode(fileno(stdin), O_BINARY);
241 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000242#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000243#ifndef MPW
Guido van Rossum22ffac11998-03-06 15:30:39 +0000244#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000245 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
246 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
247 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000248#else /* !HAVE_SETVBUF */
249 setbuf(stdin, (char *)NULL);
250 setbuf(stdout, (char *)NULL);
251 setbuf(stderr, (char *)NULL);
252#endif /* !HAVE_SETVBUF */
253#else /* MPW */
Guido van Rossum667d7041995-08-04 04:20:48 +0000254 /* On MPW (3.2) unbuffered seems to hang */
Guido van Rossum775af911997-02-14 19:50:32 +0000255 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000256 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
257 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000258#endif /* MPW */
Guido van Rossum667d7041995-08-04 04:20:48 +0000259 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000260 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000261#ifdef MS_WINDOWS
262 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000263 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000264 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000265#else /* !MS_WINDOWS */
266#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000267 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
268 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000269#endif /* HAVE_SETVBUF */
270#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000271 /* Leave stderr alone - it should be unbuffered anyway. */
272 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000273
Guido van Rossumed52aac1997-07-19 19:20:32 +0000274 Py_SetProgramName(argv[0]);
275 Py_Initialize();
276
Guido van Rossum667d7041995-08-04 04:20:48 +0000277 if (Py_VerboseFlag ||
Guido van Rossum129e91a1997-02-14 21:00:50 +0000278 (command == NULL && filename == NULL && stdin_is_interactive))
Guido van Rossumfe4dfc71997-05-19 18:33:01 +0000279 fprintf(stderr, "Python %s on %s\n%s\n",
Guido van Rossuma22865e2000-09-05 04:41:18 +0000280 Py_GetVersion(), Py_GetPlatform(), COPYRIGHT);
Guido van Rossum667d7041995-08-04 04:20:48 +0000281
Guido van Rossum667d7041995-08-04 04:20:48 +0000282
283 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000284 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
285 _PyOS_optind--;
286 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000287 }
288
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000289 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000290
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000291 if ((inspect || (command == NULL && filename == NULL)) &&
292 isatty(fileno(stdin))) {
293 PyObject *v;
294 v = PyImport_ImportModule("readline");
295 if (v == NULL)
296 PyErr_Clear();
297 else
298 Py_DECREF(v);
299 }
300
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000301 cf.cf_nested_scopes = 0;
302
Guido van Rossum667d7041995-08-04 04:20:48 +0000303 if (command) {
304 sts = PyRun_SimpleString(command) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000305 free(command);
Guido van Rossum667d7041995-08-04 04:20:48 +0000306 }
307 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000308 if (filename == NULL && stdin_is_interactive) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000309 char *startup = getenv("PYTHONSTARTUP");
310 if (startup != NULL && startup[0] != '\0') {
311 FILE *fp = fopen(startup, "r");
312 if (fp != NULL) {
313 (void) PyRun_SimpleFile(fp, startup);
314 PyErr_Clear();
315 fclose(fp);
316 }
317 }
318 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000319 /* XXX */
320 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000321 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000322 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000323 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000324 }
325
Guido van Rossum775af911997-02-14 19:50:32 +0000326 if (inspect && stdin_is_interactive &&
Guido van Rossum667d7041995-08-04 04:20:48 +0000327 (filename != NULL || command != NULL))
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000328 /* XXX */
329 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000330
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000331 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000332#ifdef RISCOS
333 if(Py_RISCOSWimpFlag)
334 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
335#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000336
337#ifdef __INSURE__
338 /* Insure++ is a memory analysis tool that aids in discovering
339 * memory leaks and other memory problems. On Python exit, the
340 * interned string dictionary is flagged as being in use at exit
341 * (which it is). Under normal circumstances, this is fine because
342 * the memory will be automatically reclaimed by the system. Under
343 * memory debugging, it's a huge source of useless noise, so we
344 * trade off slower shutdown for less distraction in the memory
345 * reports. -baw
346 */
347 _Py_ReleaseInternedStrings();
348#endif /* __INSURE__ */
349
Guido van Rossum05f7c501997-08-02 03:00:42 +0000350 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000351}
352
353
Guido van Rossum667d7041995-08-04 04:20:48 +0000354/* Make the *original* argc/argv available to other modules.
355 This is rare, but it is needed by the secureware extension. */
356
357void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000358Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000359{
360 *argc = orig_argc;
361 *argv = orig_argv;
362}