Merged release21-maint changes.
[python/dscho.git] / Modules / main.c
blobad2616da04092ee5d02e9c2df607cd3ac090709a
1 /* Python interpreter main program */
3 #include "Python.h"
4 #include "osdefs.h"
6 #ifdef HAVE_UNISTD_H
7 #include <unistd.h>
8 #endif
10 #ifdef MS_WINDOWS
11 #include <fcntl.h>
12 #endif
14 #if defined(PYOS_OS2) || defined(MS_WINDOWS)
15 #define PYTHONHOMEHELP "<prefix>\\lib"
16 #else
17 #define PYTHONHOMEHELP "<prefix>/pythonX.X"
18 #endif
20 #include "pygetopt.h"
22 #define COPYRIGHT \
23 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
24 "for more information."
26 /* For Py_GetArgcArgv(); set by main() */
27 static char **orig_argv;
28 static int orig_argc;
30 /* command line options */
31 #define BASE_OPTS "c:diOSEtuUvxXhVW:"
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 */
40 extern int Py_RISCOSWimpFlag;
41 #endif /*RISCOS*/
43 /* Short usage message (with %s for argv0) */
44 static char *usage_line =
45 "usage: %s [option] ... [-c cmd | file | -] [arg] ...\n";
47 /* Long usage message, split into parts < 512 bytes */
48 static char *usage_top = "\
49 Options and arguments (and corresponding environment variables):\n\
50 -d : debug output from parser (also PYTHONDEBUG=x)\n\
51 -i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
52 and force prompts, even if stdin does not appear to be a terminal\n\
53 -O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
54 -OO : remove doc-strings in addition to the -O optimizations\n\
55 -S : don't imply 'import site' on initialization\n\
56 -E : ignore environment variables (such as PYTHONPATH)\n\
57 -t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
59 static char *usage_mid = "\
60 -u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
61 -U : Unicode literals: treats '...' literals like u'...'\n\
62 -v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
63 -x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
64 -h : print this help message and exit\n\
65 -V : print the Python version number and exit\n\
66 -W arg : warning control (arg is action:message:category:module:lineno)\n\
67 -c cmd : program passed in as string (terminates option list)\n\
68 file : program read from script file\n\
69 - : program read from stdin (default; interactive mode if a tty)\n\
71 static char *usage_bot = "\
72 arg ...: arguments passed to program in sys.argv[1:]\n\
73 Other environment variables:\n\
74 PYTHONSTARTUP: file executed on interactive startup (no default)\n\
75 PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
76 default module search path. The result is sys.path.\n\
77 PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
78 The default module search path uses %s.\n\
79 PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
83 static void
84 usage(int exitcode, char* program)
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*/
95 /* Main program */
97 DL_EXPORT(int)
98 Py_Main(int argc, char **argv)
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;
108 int skipfirstline = 0;
109 int stdin_is_interactive = 0;
110 int help = 0;
111 int version = 0;
112 int saw_inspect_flag = 0;
113 int saw_unbuffered_flag = 0;
114 PyCompilerFlags cf;
116 orig_argc = argc; /* For Py_GetArgcArgv() */
117 orig_argv = argv;
119 #ifdef RISCOS
120 Py_RISCOSWimpFlag = 0;
121 #endif
123 PySys_ResetWarnOptions();
125 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
126 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. */
130 command = malloc(strlen(_PyOS_optarg) + 2);
131 if (command == NULL)
132 Py_FatalError(
133 "not enough memory to copy -c argument");
134 strcpy(command, _PyOS_optarg);
135 strcat(command, "\n");
136 break;
139 switch (c) {
141 case 'd':
142 Py_DebugFlag++;
143 break;
145 case 'i':
146 inspect++;
147 saw_inspect_flag = 1;
148 Py_InteractiveFlag++;
149 break;
151 case 'O':
152 Py_OptimizeFlag++;
153 break;
155 case 'S':
156 Py_NoSiteFlag++;
157 break;
159 case 'E':
160 Py_IgnoreEnvironmentFlag++;
161 break;
163 case 't':
164 Py_TabcheckFlag++;
165 break;
167 case 'u':
168 unbuffered++;
169 saw_unbuffered_flag = 1;
170 break;
172 case 'v':
173 Py_VerboseFlag++;
174 break;
176 #ifdef RISCOS
177 case 'w':
178 Py_RISCOSWimpFlag = 1;
179 break;
180 #endif
182 case 'x':
183 skipfirstline = 1;
184 break;
186 case 'U':
187 Py_UnicodeFlag++;
188 break;
189 case 'h':
190 help++;
191 break;
192 case 'V':
193 version++;
194 break;
196 case 'W':
197 PySys_AddWarnOption(_PyOS_optarg);
198 break;
200 /* This space reserved for other options */
202 default:
203 usage(2, argv[0]);
204 /*NOTREACHED*/
209 if (help)
210 usage(0, argv[0]);
212 if (version) {
213 fprintf(stderr, "Python %s\n", PY_VERSION);
214 exit(0);
217 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;
224 if (command == NULL && _PyOS_optind < argc &&
225 strcmp(argv[_PyOS_optind], "-") != 0)
227 filename = argv[_PyOS_optind];
228 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);
234 else if (skipfirstline) {
235 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;
248 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
250 if (unbuffered) {
251 #ifdef MS_WINDOWS
252 _setmode(fileno(stdin), O_BINARY);
253 _setmode(fileno(stdout), O_BINARY);
254 #endif
255 #ifndef MPW
256 #ifdef HAVE_SETVBUF
257 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
258 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
259 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
260 #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 */
266 /* On MPW (3.2) unbuffered seems to hang */
267 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
268 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
269 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
270 #endif /* MPW */
272 else if (Py_InteractiveFlag) {
273 #ifdef MS_WINDOWS
274 /* Doesn't have to have line-buffered -- use unbuffered */
275 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
276 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
277 #else /* !MS_WINDOWS */
278 #ifdef HAVE_SETVBUF
279 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
280 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
281 #endif /* HAVE_SETVBUF */
282 #endif /* !MS_WINDOWS */
283 /* Leave stderr alone - it should be unbuffered anyway. */
286 Py_SetProgramName(argv[0]);
287 Py_Initialize();
289 if (Py_VerboseFlag ||
290 (command == NULL && filename == NULL && stdin_is_interactive))
291 fprintf(stderr, "Python %s on %s\n%s\n",
292 Py_GetVersion(), Py_GetPlatform(), COPYRIGHT);
295 if (command != NULL) {
296 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
297 _PyOS_optind--;
298 argv[_PyOS_optind] = "-c";
301 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
303 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);
313 cf.cf_flags = 0;
315 if (command) {
316 sts = PyRun_SimpleString(command) != 0;
317 free(command);
319 else {
320 if (filename == NULL && stdin_is_interactive) {
321 char *startup = Py_GETENV("PYTHONSTARTUP");
322 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);
331 /* XXX */
332 sts = PyRun_AnyFileExFlags(
334 filename == NULL ? "<stdin>" : filename,
335 filename != NULL, &cf) != 0;
338 if (inspect && stdin_is_interactive &&
339 (filename != NULL || command != NULL))
340 /* XXX */
341 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
343 Py_Finalize();
344 #ifdef RISCOS
345 if(Py_RISCOSWimpFlag)
346 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
347 #endif
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
359 _Py_ReleaseInternedStrings();
360 #endif /* __INSURE__ */
362 return sts;
366 /* Make the *original* argc/argv available to other modules.
367 This is rare, but it is needed by the secureware extension. */
369 void
370 Py_GetArgcArgv(int *argc, char ***argv)
372 *argc = orig_argc;
373 *argv = orig_argv;