Files for 2.1b1 distribution.
[python/dscho.git] / Modules / main.c
blob03dff1888f6b790c3777b7c2db834a3dc6201da6
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>/python2.0"
18 #endif
20 #include "pygetopt.h"
22 #define COPYRIGHT \
23 "Type \"copyright\", \"credits\" or \"license\" for more information."
25 /* For Py_GetArgcArgv(); set by main() */
26 static char **orig_argv;
27 static int orig_argc;
29 /* For my_readline when running under RISCOS */
30 #ifdef RISCOS
31 extern int Py_RISCOSWimpFlag;
32 #endif
34 /* Short usage message (with %s for argv0) */
35 static char *usage_line =
36 "usage: %s [option] ... [-c cmd | file | -] [arg] ...\n";
38 /* Long usage message, split into parts < 512 bytes */
39 static char *usage_top = "\
40 Options and arguments (and corresponding environment variables):\n\
41 -d : debug output from parser (also PYTHONDEBUG=x)\n\
42 -i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
43 and force prompts, even if stdin does not appear to be a terminal\n\
44 -O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
45 -OO : remove doc-strings in addition to the -O optimizations\n\
46 -S : don't imply 'import site' on initialization\n\
47 -t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
49 static char *usage_mid = "\
50 -u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
51 -U : Unicode literals: treats '...' literals like u'...'\n\
52 -v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
53 -x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
54 -h : print this help message and exit\n\
55 -V : print the Python version number and exit\n\
56 -W arg : warning control (arg is action:message:category:module:lineno)\n\
57 -c cmd : program passed in as string (terminates option list)\n\
58 file : program read from script file\n\
59 - : program read from stdin (default; interactive mode if a tty)\n\
61 static char *usage_bot = "\
62 arg ...: arguments passed to program in sys.argv[1:]\n\
63 Other environment variables:\n\
64 PYTHONSTARTUP: file executed on interactive startup (no default)\n\
65 PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
66 default module search path. The result is sys.path.\n\
67 PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
68 The default module search path uses %s.\n\
69 PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
73 static void
74 usage(int exitcode, char* program)
76 fprintf(stderr, usage_line, program);
77 fprintf(stderr, usage_top);
78 fprintf(stderr, usage_mid);
79 fprintf(stderr, usage_bot, DELIM, DELIM, PYTHONHOMEHELP);
80 exit(exitcode);
81 /*NOTREACHED*/
85 /* Main program */
87 DL_EXPORT(int)
88 Py_Main(int argc, char **argv)
90 int c;
91 int sts;
92 char *command = NULL;
93 char *filename = NULL;
94 FILE *fp = stdin;
95 char *p;
96 int inspect = 0;
97 int unbuffered = 0;
98 int skipfirstline = 0;
99 int stdin_is_interactive = 0;
100 int help = 0;
101 int version = 0;
103 orig_argc = argc; /* For Py_GetArgcArgv() */
104 orig_argv = argv;
106 #ifdef RISCOS
107 Py_RISCOSWimpFlag = 0;
108 #endif
110 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
111 inspect = 1;
112 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
113 unbuffered = 1;
115 PySys_ResetWarnOptions();
117 #ifdef RISCOS
118 while ((c = getopt(argc, argv, "c:diOStuUvwxXhV")) != EOF) {
119 #else
120 while ((c = _PyOS_GetOpt(argc, argv, "c:diOStuUvxXhVW:")) != EOF) {
121 #endif
122 if (c == 'c') {
123 /* -c is the last option; following arguments
124 that look like options are left for the
125 the command to interpret. */
126 command = malloc(strlen(_PyOS_optarg) + 2);
127 if (command == NULL)
128 Py_FatalError(
129 "not enough memory to copy -c argument");
130 strcpy(command, _PyOS_optarg);
131 strcat(command, "\n");
132 break;
135 switch (c) {
137 case 'd':
138 Py_DebugFlag++;
139 break;
141 case 'i':
142 inspect++;
143 Py_InteractiveFlag++;
144 break;
146 case 'O':
147 Py_OptimizeFlag++;
148 break;
150 case 'S':
151 Py_NoSiteFlag++;
152 break;
154 case 't':
155 Py_TabcheckFlag++;
156 break;
158 case 'u':
159 unbuffered++;
160 break;
162 case 'v':
163 Py_VerboseFlag++;
164 break;
166 #ifdef RISCOS
167 case 'w':
168 Py_RISCOSWimpFlag = 1;
169 break;
170 #endif
172 case 'x':
173 skipfirstline = 1;
174 break;
176 case 'U':
177 Py_UnicodeFlag++;
178 break;
179 case 'h':
180 help++;
181 break;
182 case 'V':
183 version++;
184 break;
186 case 'W':
187 PySys_AddWarnOption(_PyOS_optarg);
188 break;
190 /* This space reserved for other options */
192 default:
193 usage(2, argv[0]);
194 /*NOTREACHED*/
199 if (help)
200 usage(0, argv[0]);
202 if (version) {
203 fprintf(stderr, "Python %s\n", PY_VERSION);
204 exit(0);
207 if (command == NULL && _PyOS_optind < argc &&
208 strcmp(argv[_PyOS_optind], "-") != 0)
210 filename = argv[_PyOS_optind];
211 if (filename != NULL) {
212 if ((fp = fopen(filename, "r")) == NULL) {
213 fprintf(stderr, "%s: can't open file '%s'\n",
214 argv[0], filename);
215 exit(2);
217 else if (skipfirstline) {
218 int ch;
219 /* Push back first newline so line numbers
220 remain the same */
221 while ((ch = getc(fp)) != EOF) {
222 if (ch == '\n') {
223 (void)ungetc(ch, fp);
224 break;
231 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
233 if (unbuffered) {
234 #ifdef MS_WINDOWS
235 _setmode(fileno(stdin), O_BINARY);
236 _setmode(fileno(stdout), O_BINARY);
237 #endif
238 #ifndef MPW
239 #ifdef HAVE_SETVBUF
240 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
241 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
242 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
243 #else /* !HAVE_SETVBUF */
244 setbuf(stdin, (char *)NULL);
245 setbuf(stdout, (char *)NULL);
246 setbuf(stderr, (char *)NULL);
247 #endif /* !HAVE_SETVBUF */
248 #else /* MPW */
249 /* On MPW (3.2) unbuffered seems to hang */
250 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
251 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
252 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
253 #endif /* MPW */
255 else if (Py_InteractiveFlag) {
256 #ifdef MS_WINDOWS
257 /* Doesn't have to have line-buffered -- use unbuffered */
258 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
259 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
260 #else /* !MS_WINDOWS */
261 #ifdef HAVE_SETVBUF
262 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
263 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
264 #endif /* HAVE_SETVBUF */
265 #endif /* !MS_WINDOWS */
266 /* Leave stderr alone - it should be unbuffered anyway. */
269 Py_SetProgramName(argv[0]);
270 Py_Initialize();
272 if (Py_VerboseFlag ||
273 (command == NULL && filename == NULL && stdin_is_interactive))
274 fprintf(stderr, "Python %s on %s\n%s\n",
275 Py_GetVersion(), Py_GetPlatform(), COPYRIGHT);
278 if (command != NULL) {
279 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
280 _PyOS_optind--;
281 argv[_PyOS_optind] = "-c";
284 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
286 if ((inspect || (command == NULL && filename == NULL)) &&
287 isatty(fileno(stdin))) {
288 PyObject *v;
289 v = PyImport_ImportModule("readline");
290 if (v == NULL)
291 PyErr_Clear();
292 else
293 Py_DECREF(v);
296 if (command) {
297 sts = PyRun_SimpleString(command) != 0;
298 free(command);
300 else {
301 if (filename == NULL && stdin_is_interactive) {
302 char *startup = getenv("PYTHONSTARTUP");
303 if (startup != NULL && startup[0] != '\0') {
304 FILE *fp = fopen(startup, "r");
305 if (fp != NULL) {
306 (void) PyRun_SimpleFile(fp, startup);
307 PyErr_Clear();
308 fclose(fp);
312 sts = PyRun_AnyFileEx(
314 filename == NULL ? "<stdin>" : filename,
315 filename != NULL) != 0;
318 if (inspect && stdin_is_interactive &&
319 (filename != NULL || command != NULL))
320 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
322 Py_Finalize();
323 #ifdef RISCOS
324 if(Py_RISCOSWimpFlag)
325 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
326 #endif
328 #ifdef __INSURE__
329 /* Insure++ is a memory analysis tool that aids in discovering
330 * memory leaks and other memory problems. On Python exit, the
331 * interned string dictionary is flagged as being in use at exit
332 * (which it is). Under normal circumstances, this is fine because
333 * the memory will be automatically reclaimed by the system. Under
334 * memory debugging, it's a huge source of useless noise, so we
335 * trade off slower shutdown for less distraction in the memory
336 * reports. -baw
338 _Py_ReleaseInternedStrings();
339 #endif /* __INSURE__ */
341 return sts;
345 /* Make the *original* argc/argv available to other modules.
346 This is rare, but it is needed by the secureware extension. */
348 void
349 Py_GetArgcArgv(int *argc, char ***argv)
351 *argc = orig_argc;
352 *argv = orig_argv;