blob: 34e99b9de6b37e99ec3800e48a34787a5736d989 [file] [log] [blame]
Guido van Rossum667d7041995-08-04 04:20:48 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossum667d7041995-08-04 04:20:48 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum667d7041995-08-04 04:20:48 +00009******************************************************************/
10
11/* Python interpreter main program */
12
13#include "Python.h"
Guido van Rossuma075ce11997-12-05 21:56:45 +000014#include "osdefs.h"
Guido van Rossum667d7041995-08-04 04:20:48 +000015
Guido van Rossum66a70131996-12-09 18:46:58 +000016#ifdef HAVE_UNISTD_H
17#include <unistd.h>
18#endif
Guido van Rossum667d7041995-08-04 04:20:48 +000019
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000020#ifdef MS_WINDOWS
21#include <fcntl.h>
22#endif
23
Guido van Rossuma075ce11997-12-05 21:56:45 +000024#if defined(PYOS_OS2) || defined(MS_WINDOWS)
25#define PYTHONHOMEHELP "<prefix>\\lib"
26#else
Fredrik Lundh620f3772000-07-09 20:42:34 +000027#define PYTHONHOMEHELP "<prefix>/python2.0"
Guido van Rossuma075ce11997-12-05 21:56:45 +000028#endif
29
Guido van Rossum667d7041995-08-04 04:20:48 +000030/* Interface to getopt(): */
31extern int optind;
32extern char *optarg;
33extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
34
35
Guido van Rossumac56b031996-07-21 02:33:38 +000036/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000037static char **orig_argv;
38static int orig_argc;
39
Guido van Rossum667d7041995-08-04 04:20:48 +000040/* Short usage message (with %s for argv0) */
41static char *usage_line =
Guido van Rossum6b86a421999-01-28 15:07:47 +000042"usage: %s [option] ... [-c cmd | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000043
44/* Long usage message, split into parts < 512 bytes */
Guido van Rossuma075ce11997-12-05 21:56:45 +000045static char *usage_top = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000046Options and arguments (and corresponding environment variables):\n\
47-d : debug output from parser (also PYTHONDEBUG=x)\n\
Guido van Rossum775af911997-02-14 19:50:32 +000048-i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000049 and force prompts, even if stdin does not appear to be a terminal\n\
Guido van Rossume7adf3e1998-10-07 14:50:06 +000050-O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000051-OO : remove doc-strings in addition to the -O optimizations\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000052-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000053-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000054";
Guido van Rossum7922bd71997-08-29 22:34:47 +000055static char *usage_mid = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000056-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
Guido van Rossumc15a9a12000-05-01 17:54:33 +000057-U : Unicode literals: treats '...' literals like u'...'\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000058-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000059-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Guido van Rossum775af911997-02-14 19:50:32 +000060-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000061file : program read from script file\n\
62- : program read from stdin (default; interactive mode if a tty)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000063";
64static char *usage_bot = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000065arg ...: arguments passed to program in sys.argv[1:]\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000066Other environment variables:\n\
67PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000068PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000069 default module search path. The result is sys.path.\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000070PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
71 The default module search path uses %s.\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000072";
73
74
75/* Main program */
76
Guido van Rossum9c1201f1998-12-07 14:28:47 +000077DL_EXPORT(int)
Fredrik Lundh620f3772000-07-09 20:42:34 +000078Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +000079{
80 int c;
81 int sts;
82 char *command = NULL;
83 char *filename = NULL;
84 FILE *fp = stdin;
85 char *p;
86 int inspect = 0;
87 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +000088 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +000089 int stdin_is_interactive = 0;
Guido van Rossum667d7041995-08-04 04:20:48 +000090
Guido van Rossumac56b031996-07-21 02:33:38 +000091 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +000092 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +000093
Guido van Rossum667d7041995-08-04 04:20:48 +000094 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
95 inspect = 1;
96 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
97 unbuffered = 1;
98
Guido van Rossumc15a9a12000-05-01 17:54:33 +000099 while ((c = getopt(argc, argv, "c:diOStuUvxX")) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000100 if (c == 'c') {
101 /* -c is the last option; following arguments
102 that look like options are left for the
103 the command to interpret. */
104 command = malloc(strlen(optarg) + 2);
105 if (command == NULL)
106 Py_FatalError(
107 "not enough memory to copy -c argument");
108 strcpy(command, optarg);
109 strcat(command, "\n");
110 break;
111 }
112
113 switch (c) {
114
115 case 'd':
116 Py_DebugFlag++;
117 break;
118
119 case 'i':
120 inspect++;
Guido van Rossum775af911997-02-14 19:50:32 +0000121 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000122 break;
123
Guido van Rossum7614da61997-03-03 19:14:45 +0000124 case 'O':
125 Py_OptimizeFlag++;
126 break;
127
Guido van Rossum7922bd71997-08-29 22:34:47 +0000128 case 'S':
129 Py_NoSiteFlag++;
130 break;
131
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000132 case 't':
133 Py_TabcheckFlag++;
134 break;
135
Guido van Rossum667d7041995-08-04 04:20:48 +0000136 case 'u':
137 unbuffered++;
138 break;
139
140 case 'v':
141 Py_VerboseFlag++;
142 break;
143
Guido van Rossuma075ce11997-12-05 21:56:45 +0000144 case 'x':
145 skipfirstline = 1;
146 break;
147
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000148 case 'U':
149 Py_UnicodeFlag++;
150 break;
151
Guido van Rossum667d7041995-08-04 04:20:48 +0000152 /* This space reserved for other options */
153
154 default:
155 fprintf(stderr, usage_line, argv[0]);
156 fprintf(stderr, usage_top);
Guido van Rossum7922bd71997-08-29 22:34:47 +0000157 fprintf(stderr, usage_mid);
Guido van Rossuma075ce11997-12-05 21:56:45 +0000158 fprintf(stderr, usage_bot,
159 DELIM, DELIM, PYTHONHOMEHELP);
Guido van Rossum667d7041995-08-04 04:20:48 +0000160 exit(2);
161 /*NOTREACHED*/
162
163 }
164 }
165
Guido van Rossum775af911997-02-14 19:50:32 +0000166 if (command == NULL && optind < argc &&
167 strcmp(argv[optind], "-") != 0)
168 {
169 filename = argv[optind];
170 if (filename != NULL) {
171 if ((fp = fopen(filename, "r")) == NULL) {
172 fprintf(stderr, "%s: can't open file '%s'\n",
173 argv[0], filename);
174 exit(2);
175 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000176 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000177 int ch;
178 /* Push back first newline so line numbers
179 remain the same */
180 while ((ch = getc(fp)) != EOF) {
181 if (ch == '\n') {
182 (void)ungetc(ch, fp);
183 break;
184 }
185 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000186 }
Guido van Rossum775af911997-02-14 19:50:32 +0000187 }
188 }
189
190 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
191
Guido van Rossum667d7041995-08-04 04:20:48 +0000192 if (unbuffered) {
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000193#ifdef MS_WINDOWS
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000194 _setmode(fileno(stdin), O_BINARY);
195 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000196#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000197#ifndef MPW
Guido van Rossum22ffac11998-03-06 15:30:39 +0000198#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000199 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
200 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
201 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000202#else /* !HAVE_SETVBUF */
203 setbuf(stdin, (char *)NULL);
204 setbuf(stdout, (char *)NULL);
205 setbuf(stderr, (char *)NULL);
206#endif /* !HAVE_SETVBUF */
207#else /* MPW */
Guido van Rossum667d7041995-08-04 04:20:48 +0000208 /* On MPW (3.2) unbuffered seems to hang */
Guido van Rossum775af911997-02-14 19:50:32 +0000209 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000210 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
211 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000212#endif /* MPW */
Guido van Rossum667d7041995-08-04 04:20:48 +0000213 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000214 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000215#ifdef MS_WINDOWS
216 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000217 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000218 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000219#else /* !MS_WINDOWS */
220#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000221 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
222 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000223#endif /* HAVE_SETVBUF */
224#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000225 /* Leave stderr alone - it should be unbuffered anyway. */
226 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000227
Guido van Rossumed52aac1997-07-19 19:20:32 +0000228 Py_SetProgramName(argv[0]);
229 Py_Initialize();
230
Guido van Rossum667d7041995-08-04 04:20:48 +0000231 if (Py_VerboseFlag ||
Guido van Rossum129e91a1997-02-14 21:00:50 +0000232 (command == NULL && filename == NULL && stdin_is_interactive))
Guido van Rossumfe4dfc71997-05-19 18:33:01 +0000233 fprintf(stderr, "Python %s on %s\n%s\n",
234 Py_GetVersion(), Py_GetPlatform(), Py_GetCopyright());
Guido van Rossum667d7041995-08-04 04:20:48 +0000235
Guido van Rossum667d7041995-08-04 04:20:48 +0000236
237 if (command != NULL) {
238 /* Backup optind and force sys.argv[0] = '-c' */
239 optind--;
240 argv[optind] = "-c";
241 }
242
243 PySys_SetArgv(argc-optind, argv+optind);
244
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000245 if ((inspect || (command == NULL && filename == NULL)) &&
246 isatty(fileno(stdin))) {
247 PyObject *v;
248 v = PyImport_ImportModule("readline");
249 if (v == NULL)
250 PyErr_Clear();
251 else
252 Py_DECREF(v);
253 }
254
Guido van Rossum667d7041995-08-04 04:20:48 +0000255 if (command) {
256 sts = PyRun_SimpleString(command) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000257 free(command);
Guido van Rossum667d7041995-08-04 04:20:48 +0000258 }
259 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000260 if (filename == NULL && stdin_is_interactive) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000261 char *startup = getenv("PYTHONSTARTUP");
262 if (startup != NULL && startup[0] != '\0') {
263 FILE *fp = fopen(startup, "r");
264 if (fp != NULL) {
265 (void) PyRun_SimpleFile(fp, startup);
266 PyErr_Clear();
267 fclose(fp);
268 }
269 }
270 }
271 sts = PyRun_AnyFile(
Guido van Rossum775af911997-02-14 19:50:32 +0000272 fp,
273 filename == NULL ? "<stdin>" : filename) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000274 if (filename != NULL)
275 fclose(fp);
276 }
277
Guido van Rossum775af911997-02-14 19:50:32 +0000278 if (inspect && stdin_is_interactive &&
Guido van Rossum667d7041995-08-04 04:20:48 +0000279 (filename != NULL || command != NULL))
280 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
281
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000282 Py_Finalize();
Guido van Rossum05f7c501997-08-02 03:00:42 +0000283 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000284}
285
286
Guido van Rossum667d7041995-08-04 04:20:48 +0000287/* Make the *original* argc/argv available to other modules.
288 This is rare, but it is needed by the secureware extension. */
289
290void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000291Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000292{
293 *argc = orig_argc;
294 *argv = orig_argv;
295}