This commit was manufactured by cvs2svn to create tag 'r221c2'.
[python/dscho.git] / Modules / main.c
blobb36714c14d91ef8a4aa5799c58b2fcbc803c9480
1 /* Python interpreter main program */
3 #include "Python.h"
4 #include "osdefs.h"
5 #include "compile.h" /* For CO_FUTURE_DIVISION */
7 #ifdef MS_WINDOWS
8 #include <fcntl.h>
9 #endif
11 #if defined(PYOS_OS2) || defined(MS_WINDOWS)
12 #define PYTHONHOMEHELP "<prefix>\\lib"
13 #else
14 #define PYTHONHOMEHELP "<prefix>/pythonX.X"
15 #endif
17 #include "pygetopt.h"
19 #define COPYRIGHT \
20 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
21 "for more information."
23 /* For Py_GetArgcArgv(); set by main() */
24 static char **orig_argv;
25 static int orig_argc;
27 /* command line options */
28 #define BASE_OPTS "c:dEhiOQ:StuUvVW:xX"
30 #ifndef RISCOS
31 #define PROGRAM_OPTS BASE_OPTS
32 #else /*RISCOS*/
33 /* extra option saying that we are running under a special task window
34 frontend; especially my_readline will behave different */
35 #define PROGRAM_OPTS BASE_OPTS "w"
36 /* corresponding flag */
37 extern int Py_RISCOSWimpFlag;
38 #endif /*RISCOS*/
40 /* Short usage message (with %s for argv0) */
41 static char *usage_line =
42 "usage: %s [option] ... [-c cmd | file | -] [arg] ...\n";
44 /* Long usage message, split into parts < 512 bytes */
45 static char *usage_1 = "\
46 Options and arguments (and corresponding environment variables):\n\
47 -c cmd : program passed in as string (terminates option list)\n\
48 -d : debug output from parser (also PYTHONDEBUG=x)\n\
49 -E : ignore environment variables (such as PYTHONPATH)\n\
50 -h : print this help message and exit\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\
54 static char *usage_2 = "\
55 -O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
56 -OO : remove doc-strings in addition to the -O optimizations\n\
57 -Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\
58 -S : don't imply 'import site' on initialization\n\
59 -t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
60 -u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
62 static char *usage_3 = "\
63 -v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
64 -V : print the Python version number and exit\n\
65 -W arg : warning control (arg is action:message:category:module:lineno)\n\
66 -x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
67 file : program read from script file\n\
68 - : program read from stdin (default; interactive mode if a tty)\n\
70 static char *usage_4 = "\
71 arg ...: arguments passed to program in sys.argv[1:]\n\
72 Other environment variables:\n\
73 PYTHONSTARTUP: file executed on interactive startup (no default)\n\
74 PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
75 default module search path. The result is sys.path.\n\
76 PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
77 The default module search path uses %s.\n\
78 PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
82 static void
83 usage(int exitcode, char* program)
85 FILE *f = exitcode ? stderr : stdout;
87 fprintf(f, usage_line, program);
88 if (exitcode)
89 fprintf(f, "Try `python -h' for more information.\n");
90 else {
91 fprintf(f, usage_1);
92 fprintf(f, usage_2);
93 fprintf(f, usage_3);
94 fprintf(f, usage_4, DELIM, DELIM, PYTHONHOMEHELP);
96 exit(exitcode);
97 /*NOTREACHED*/
101 /* Main program */
103 DL_EXPORT(int)
104 Py_Main(int argc, char **argv)
106 int c;
107 int sts;
108 char *command = NULL;
109 char *filename = NULL;
110 FILE *fp = stdin;
111 char *p;
112 int inspect = 0;
113 int unbuffered = 0;
114 int skipfirstline = 0;
115 int stdin_is_interactive = 0;
116 int help = 0;
117 int version = 0;
118 int saw_inspect_flag = 0;
119 int saw_unbuffered_flag = 0;
120 PyCompilerFlags cf;
122 cf.cf_flags = 0;
124 orig_argc = argc; /* For Py_GetArgcArgv() */
125 orig_argv = argv;
127 #ifdef RISCOS
128 Py_RISCOSWimpFlag = 0;
129 #endif
131 PySys_ResetWarnOptions();
133 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
134 if (c == 'c') {
135 /* -c is the last option; following arguments
136 that look like options are left for the
137 the command to interpret. */
138 command = malloc(strlen(_PyOS_optarg) + 2);
139 if (command == NULL)
140 Py_FatalError(
141 "not enough memory to copy -c argument");
142 strcpy(command, _PyOS_optarg);
143 strcat(command, "\n");
144 break;
147 switch (c) {
149 case 'd':
150 Py_DebugFlag++;
151 break;
153 case 'Q':
154 if (strcmp(_PyOS_optarg, "old") == 0) {
155 Py_DivisionWarningFlag = 0;
156 break;
158 if (strcmp(_PyOS_optarg, "warn") == 0) {
159 Py_DivisionWarningFlag = 1;
160 break;
162 if (strcmp(_PyOS_optarg, "warnall") == 0) {
163 Py_DivisionWarningFlag = 2;
164 break;
166 if (strcmp(_PyOS_optarg, "new") == 0) {
167 /* This only affects __main__ */
168 cf.cf_flags |= CO_FUTURE_DIVISION;
169 /* And this tells the eval loop to treat
170 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
171 _Py_QnewFlag = 1;
172 break;
174 fprintf(stderr,
175 "-Q option should be `-Qold', "
176 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
177 usage(2, argv[0]);
178 /* NOTREACHED */
180 case 'i':
181 inspect++;
182 saw_inspect_flag = 1;
183 Py_InteractiveFlag++;
184 break;
186 case 'O':
187 Py_OptimizeFlag++;
188 break;
190 case 'S':
191 Py_NoSiteFlag++;
192 break;
194 case 'E':
195 Py_IgnoreEnvironmentFlag++;
196 break;
198 case 't':
199 Py_TabcheckFlag++;
200 break;
202 case 'u':
203 unbuffered++;
204 saw_unbuffered_flag = 1;
205 break;
207 case 'v':
208 Py_VerboseFlag++;
209 break;
211 #ifdef RISCOS
212 case 'w':
213 Py_RISCOSWimpFlag = 1;
214 break;
215 #endif
217 case 'x':
218 skipfirstline = 1;
219 break;
221 case 'U':
222 Py_UnicodeFlag++;
223 break;
224 case 'h':
225 help++;
226 break;
227 case 'V':
228 version++;
229 break;
231 case 'W':
232 PySys_AddWarnOption(_PyOS_optarg);
233 break;
235 /* This space reserved for other options */
237 default:
238 usage(2, argv[0]);
239 /*NOTREACHED*/
244 if (help)
245 usage(0, argv[0]);
247 if (version) {
248 fprintf(stderr, "Python %s\n", PY_VERSION);
249 exit(0);
252 if (!saw_inspect_flag &&
253 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
254 inspect = 1;
255 if (!saw_unbuffered_flag &&
256 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
257 unbuffered = 1;
259 if (command == NULL && _PyOS_optind < argc &&
260 strcmp(argv[_PyOS_optind], "-") != 0)
262 filename = argv[_PyOS_optind];
263 if (filename != NULL) {
264 if ((fp = fopen(filename, "r")) == NULL) {
265 fprintf(stderr, "%s: can't open file '%s'\n",
266 argv[0], filename);
267 exit(2);
269 else if (skipfirstline) {
270 int ch;
271 /* Push back first newline so line numbers
272 remain the same */
273 while ((ch = getc(fp)) != EOF) {
274 if (ch == '\n') {
275 (void)ungetc(ch, fp);
276 break;
283 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
285 if (unbuffered) {
286 #ifdef MS_WINDOWS
287 _setmode(fileno(stdin), O_BINARY);
288 _setmode(fileno(stdout), O_BINARY);
289 #endif
290 #ifndef MPW
291 #ifdef HAVE_SETVBUF
292 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
293 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
294 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
295 #else /* !HAVE_SETVBUF */
296 setbuf(stdin, (char *)NULL);
297 setbuf(stdout, (char *)NULL);
298 setbuf(stderr, (char *)NULL);
299 #endif /* !HAVE_SETVBUF */
300 #else /* MPW */
301 /* On MPW (3.2) unbuffered seems to hang */
302 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
303 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
304 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
305 #endif /* MPW */
307 else if (Py_InteractiveFlag) {
308 #ifdef MS_WINDOWS
309 /* Doesn't have to have line-buffered -- use unbuffered */
310 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
311 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
312 #else /* !MS_WINDOWS */
313 #ifdef HAVE_SETVBUF
314 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
315 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
316 #endif /* HAVE_SETVBUF */
317 #endif /* !MS_WINDOWS */
318 /* Leave stderr alone - it should be unbuffered anyway. */
321 Py_SetProgramName(argv[0]);
322 Py_Initialize();
324 if (Py_VerboseFlag ||
325 (command == NULL && filename == NULL && stdin_is_interactive))
326 fprintf(stderr, "Python %s on %s\n%s\n",
327 Py_GetVersion(), Py_GetPlatform(), COPYRIGHT);
329 if (command != NULL) {
330 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
331 _PyOS_optind--;
332 argv[_PyOS_optind] = "-c";
335 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
337 if ((inspect || (command == NULL && filename == NULL)) &&
338 isatty(fileno(stdin))) {
339 PyObject *v;
340 v = PyImport_ImportModule("readline");
341 if (v == NULL)
342 PyErr_Clear();
343 else
344 Py_DECREF(v);
347 if (command) {
348 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
349 free(command);
351 else {
352 if (filename == NULL && stdin_is_interactive) {
353 char *startup = Py_GETENV("PYTHONSTARTUP");
354 if (startup != NULL && startup[0] != '\0') {
355 FILE *fp = fopen(startup, "r");
356 if (fp != NULL) {
357 (void) PyRun_SimpleFile(fp, startup);
358 PyErr_Clear();
359 fclose(fp);
363 /* XXX */
364 sts = PyRun_AnyFileExFlags(
366 filename == NULL ? "<stdin>" : filename,
367 filename != NULL, &cf) != 0;
370 if (inspect && stdin_is_interactive &&
371 (filename != NULL || command != NULL))
372 /* XXX */
373 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
375 Py_Finalize();
376 #ifdef RISCOS
377 if(Py_RISCOSWimpFlag)
378 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
379 #endif
381 #ifdef __INSURE__
382 /* Insure++ is a memory analysis tool that aids in discovering
383 * memory leaks and other memory problems. On Python exit, the
384 * interned string dictionary is flagged as being in use at exit
385 * (which it is). Under normal circumstances, this is fine because
386 * the memory will be automatically reclaimed by the system. Under
387 * memory debugging, it's a huge source of useless noise, so we
388 * trade off slower shutdown for less distraction in the memory
389 * reports. -baw
391 _Py_ReleaseInternedStrings();
392 #endif /* __INSURE__ */
394 return sts;
398 /* Make the *original* argc/argv available to other modules.
399 This is rare, but it is needed by the secureware extension. */
401 void
402 Py_GetArgcArgv(int *argc, char ***argv)
404 *argc = orig_argc;
405 *argv = orig_argv;