1 /* Python interpreter main program */
14 #if defined(PYOS_OS2) || defined(MS_WINDOWS)
15 #define PYTHONHOMEHELP "<prefix>\\lib"
17 #define PYTHONHOMEHELP "<prefix>/python2.0"
23 "Type \"copyright\", \"credits\" or \"license\" for more information."
25 /* For Py_GetArgcArgv(); set by main() */
26 static char **orig_argv
;
29 /* For my_readline when running under RISCOS */
31 extern int Py_RISCOSWimpFlag
;
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\
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
);
88 Py_Main(int argc
, char **argv
)
93 char *filename
= NULL
;
98 int skipfirstline
= 0;
99 int stdin_is_interactive
= 0;
103 orig_argc
= argc
; /* For Py_GetArgcArgv() */
107 Py_RISCOSWimpFlag
= 0;
110 if ((p
= getenv("PYTHONINSPECT")) && *p
!= '\0')
112 if ((p
= getenv("PYTHONUNBUFFERED")) && *p
!= '\0')
115 PySys_ResetWarnOptions();
118 while ((c
= getopt(argc
, argv
, "c:diOStuUvwxXhV")) != EOF
) {
120 while ((c
= _PyOS_GetOpt(argc
, argv
, "c:diOStuUvxXhVW:")) != EOF
) {
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);
129 "not enough memory to copy -c argument");
130 strcpy(command
, _PyOS_optarg
);
131 strcat(command
, "\n");
143 Py_InteractiveFlag
++;
168 Py_RISCOSWimpFlag
= 1;
187 PySys_AddWarnOption(_PyOS_optarg
);
190 /* This space reserved for other options */
203 fprintf(stderr
, "Python %s\n", PY_VERSION
);
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",
217 else if (skipfirstline
) {
219 /* Push back first newline so line numbers
221 while ((ch
= getc(fp
)) != EOF
) {
223 (void)ungetc(ch
, fp
);
231 stdin_is_interactive
= Py_FdIsInteractive(stdin
, (char *)0);
235 _setmode(fileno(stdin
), O_BINARY
);
236 _setmode(fileno(stdout
), O_BINARY
);
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 */
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
);
255 else if (Py_InteractiveFlag
) {
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 */
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]);
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' */
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
))) {
289 v
= PyImport_ImportModule("readline");
297 sts
= PyRun_SimpleString(command
) != 0;
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");
306 (void) PyRun_SimpleFile(fp
, startup
);
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;
324 if(Py_RISCOSWimpFlag
)
325 fprintf(stderr
, "\x0cq\x0c"); /* make frontend quit */
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
338 _Py_ReleaseInternedStrings();
339 #endif /* __INSURE__ */
345 /* Make the *original* argc/argv available to other modules.
346 This is rare, but it is needed by the secureware extension. */
349 Py_GetArgcArgv(int *argc
, char ***argv
)