blob: bef2574e0aeb2d271c787d7b32bba52426f50bc8 [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 Rossum667d7041995-08-04 04:20:48 +000029/* Short usage message (with %s for argv0) */
30static char *usage_line =
Guido van Rossum6b86a421999-01-28 15:07:47 +000031"usage: %s [option] ... [-c cmd | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000032
33/* Long usage message, split into parts < 512 bytes */
Guido van Rossuma075ce11997-12-05 21:56:45 +000034static char *usage_top = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000035Options and arguments (and corresponding environment variables):\n\
36-d : debug output from parser (also PYTHONDEBUG=x)\n\
Guido van Rossum775af911997-02-14 19:50:32 +000037-i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000038 and force prompts, even if stdin does not appear to be a terminal\n\
Guido van Rossume7adf3e1998-10-07 14:50:06 +000039-O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000040-OO : remove doc-strings in addition to the -O optimizations\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000041-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000042-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000043";
Guido van Rossum7922bd71997-08-29 22:34:47 +000044static char *usage_mid = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000045-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
Guido van Rossumc15a9a12000-05-01 17:54:33 +000046-U : Unicode literals: treats '...' literals like u'...'\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000047-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000048-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000049-h : print this help message and exit\n\
50-V : print the Python version number and exit\n\
Guido van Rossum47f5fdc2000-12-15 22:00:54 +000051-W arg : warning control (arg is action:message:category:module:lineno)\n\
Guido van Rossum775af911997-02-14 19:50:32 +000052-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000053file : program read from script file\n\
54- : program read from stdin (default; interactive mode if a tty)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000055";
56static char *usage_bot = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000057arg ...: arguments passed to program in sys.argv[1:]\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000058Other environment variables:\n\
59PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000060PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000061 default module search path. The result is sys.path.\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000062PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
63 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +000064PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000065";
66
67
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000068static void
69usage(int exitcode, char* program)
70{
71 fprintf(stderr, usage_line, program);
72 fprintf(stderr, usage_top);
73 fprintf(stderr, usage_mid);
74 fprintf(stderr, usage_bot, DELIM, DELIM, PYTHONHOMEHELP);
75 exit(exitcode);
76 /*NOTREACHED*/
77}
78
79
Guido van Rossum667d7041995-08-04 04:20:48 +000080/* Main program */
81
Guido van Rossum9c1201f1998-12-07 14:28:47 +000082DL_EXPORT(int)
Fredrik Lundh620f3772000-07-09 20:42:34 +000083Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +000084{
85 int c;
86 int sts;
87 char *command = NULL;
88 char *filename = NULL;
89 FILE *fp = stdin;
90 char *p;
91 int inspect = 0;
92 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +000093 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +000094 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000095 int help = 0;
96 int version = 0;
Guido van Rossum667d7041995-08-04 04:20:48 +000097
Guido van Rossumac56b031996-07-21 02:33:38 +000098 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +000099 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000100
Guido van Rossum667d7041995-08-04 04:20:48 +0000101 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
102 inspect = 1;
103 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
104 unbuffered = 1;
105
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000106 PySys_ResetWarnOptions();
107
108 while ((c = _PyOS_GetOpt(argc, argv, "c:diOStuUvxXhVW:")) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000109 if (c == 'c') {
110 /* -c is the last option; following arguments
111 that look like options are left for the
112 the command to interpret. */
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000113 command = malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000114 if (command == NULL)
115 Py_FatalError(
116 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000117 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000118 strcat(command, "\n");
119 break;
120 }
121
122 switch (c) {
123
124 case 'd':
125 Py_DebugFlag++;
126 break;
127
128 case 'i':
129 inspect++;
Guido van Rossum775af911997-02-14 19:50:32 +0000130 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000131 break;
132
Guido van Rossum7614da61997-03-03 19:14:45 +0000133 case 'O':
134 Py_OptimizeFlag++;
135 break;
136
Guido van Rossum7922bd71997-08-29 22:34:47 +0000137 case 'S':
138 Py_NoSiteFlag++;
139 break;
140
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000141 case 't':
142 Py_TabcheckFlag++;
143 break;
144
Guido van Rossum667d7041995-08-04 04:20:48 +0000145 case 'u':
146 unbuffered++;
147 break;
148
149 case 'v':
150 Py_VerboseFlag++;
151 break;
152
Guido van Rossuma075ce11997-12-05 21:56:45 +0000153 case 'x':
154 skipfirstline = 1;
155 break;
156
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000157 case 'U':
158 Py_UnicodeFlag++;
159 break;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000160 case 'h':
161 help++;
162 break;
163 case 'V':
164 version++;
165 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000166
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000167 case 'W':
168 PySys_AddWarnOption(_PyOS_optarg);
169 break;
170
Guido van Rossum667d7041995-08-04 04:20:48 +0000171 /* This space reserved for other options */
172
173 default:
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000174 usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000175 /*NOTREACHED*/
176
177 }
178 }
179
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000180 if (help)
181 usage(0, argv[0]);
182
183 if (version) {
184 fprintf(stderr, "Python %s\n", PY_VERSION);
185 exit(0);
186 }
187
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000188 if (command == NULL && _PyOS_optind < argc &&
189 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000190 {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000191 filename = argv[_PyOS_optind];
Guido van Rossum775af911997-02-14 19:50:32 +0000192 if (filename != NULL) {
193 if ((fp = fopen(filename, "r")) == NULL) {
194 fprintf(stderr, "%s: can't open file '%s'\n",
195 argv[0], filename);
196 exit(2);
197 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000198 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000199 int ch;
200 /* Push back first newline so line numbers
201 remain the same */
202 while ((ch = getc(fp)) != EOF) {
203 if (ch == '\n') {
204 (void)ungetc(ch, fp);
205 break;
206 }
207 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000208 }
Guido van Rossum775af911997-02-14 19:50:32 +0000209 }
210 }
211
212 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
213
Guido van Rossum667d7041995-08-04 04:20:48 +0000214 if (unbuffered) {
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000215#ifdef MS_WINDOWS
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000216 _setmode(fileno(stdin), O_BINARY);
217 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000218#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000219#ifndef MPW
Guido van Rossum22ffac11998-03-06 15:30:39 +0000220#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000221 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
222 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
223 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000224#else /* !HAVE_SETVBUF */
225 setbuf(stdin, (char *)NULL);
226 setbuf(stdout, (char *)NULL);
227 setbuf(stderr, (char *)NULL);
228#endif /* !HAVE_SETVBUF */
229#else /* MPW */
Guido van Rossum667d7041995-08-04 04:20:48 +0000230 /* On MPW (3.2) unbuffered seems to hang */
Guido van Rossum775af911997-02-14 19:50:32 +0000231 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000232 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
233 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000234#endif /* MPW */
Guido van Rossum667d7041995-08-04 04:20:48 +0000235 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000236 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000237#ifdef MS_WINDOWS
238 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000239 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000240 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000241#else /* !MS_WINDOWS */
242#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000243 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
244 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000245#endif /* HAVE_SETVBUF */
246#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000247 /* Leave stderr alone - it should be unbuffered anyway. */
248 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000249
Guido van Rossumed52aac1997-07-19 19:20:32 +0000250 Py_SetProgramName(argv[0]);
251 Py_Initialize();
252
Guido van Rossum667d7041995-08-04 04:20:48 +0000253 if (Py_VerboseFlag ||
Guido van Rossum129e91a1997-02-14 21:00:50 +0000254 (command == NULL && filename == NULL && stdin_is_interactive))
Guido van Rossumfe4dfc71997-05-19 18:33:01 +0000255 fprintf(stderr, "Python %s on %s\n%s\n",
Guido van Rossuma22865e2000-09-05 04:41:18 +0000256 Py_GetVersion(), Py_GetPlatform(), COPYRIGHT);
Guido van Rossum667d7041995-08-04 04:20:48 +0000257
Guido van Rossum667d7041995-08-04 04:20:48 +0000258
259 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000260 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
261 _PyOS_optind--;
262 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000263 }
264
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000265 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000266
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000267 if ((inspect || (command == NULL && filename == NULL)) &&
268 isatty(fileno(stdin))) {
269 PyObject *v;
270 v = PyImport_ImportModule("readline");
271 if (v == NULL)
272 PyErr_Clear();
273 else
274 Py_DECREF(v);
275 }
276
Guido van Rossum667d7041995-08-04 04:20:48 +0000277 if (command) {
278 sts = PyRun_SimpleString(command) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000279 free(command);
Guido van Rossum667d7041995-08-04 04:20:48 +0000280 }
281 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000282 if (filename == NULL && stdin_is_interactive) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000283 char *startup = getenv("PYTHONSTARTUP");
284 if (startup != NULL && startup[0] != '\0') {
285 FILE *fp = fopen(startup, "r");
286 if (fp != NULL) {
287 (void) PyRun_SimpleFile(fp, startup);
288 PyErr_Clear();
289 fclose(fp);
290 }
291 }
292 }
Guido van Rossum0df002c2000-08-27 19:21:52 +0000293 sts = PyRun_AnyFileEx(
Guido van Rossum775af911997-02-14 19:50:32 +0000294 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000295 filename == NULL ? "<stdin>" : filename,
296 filename != NULL) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000297 }
298
Guido van Rossum775af911997-02-14 19:50:32 +0000299 if (inspect && stdin_is_interactive &&
Guido van Rossum667d7041995-08-04 04:20:48 +0000300 (filename != NULL || command != NULL))
301 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
302
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000303 Py_Finalize();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000304
305#ifdef __INSURE__
306 /* Insure++ is a memory analysis tool that aids in discovering
307 * memory leaks and other memory problems. On Python exit, the
308 * interned string dictionary is flagged as being in use at exit
309 * (which it is). Under normal circumstances, this is fine because
310 * the memory will be automatically reclaimed by the system. Under
311 * memory debugging, it's a huge source of useless noise, so we
312 * trade off slower shutdown for less distraction in the memory
313 * reports. -baw
314 */
315 _Py_ReleaseInternedStrings();
316#endif /* __INSURE__ */
317
Guido van Rossum05f7c501997-08-02 03:00:42 +0000318 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000319}
320
321
Guido van Rossum667d7041995-08-04 04:20:48 +0000322/* Make the *original* argc/argv available to other modules.
323 This is rare, but it is needed by the secureware extension. */
324
325void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000326Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000327{
328 *argc = orig_argc;
329 *argv = orig_argv;
330}