1 /* Python interpreter main program */
14 #if defined(PYOS_OS2) || defined(MS_WINDOWS)
15 #define PYTHONHOMEHELP "<prefix>\\lib"
17 #define PYTHONHOMEHELP "<prefix>/pythonX.X"
23 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
24 "for more information."
26 /* For Py_GetArgcArgv(); set by main() */
27 static char **orig_argv
;
30 /* command line options */
31 #define BASE_OPTS "c:diOSEtuUvxXhVW:"
34 #define PROGRAM_OPTS BASE_OPTS
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
;
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\
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
);
98 Py_Main(int argc
, char **argv
)
102 char *command
= NULL
;
103 char *filename
= NULL
;
108 int skipfirstline
= 0;
109 int stdin_is_interactive
= 0;
112 int saw_inspect_flag
= 0;
113 int saw_unbuffered_flag
= 0;
116 orig_argc
= argc
; /* For Py_GetArgcArgv() */
120 Py_RISCOSWimpFlag
= 0;
123 PySys_ResetWarnOptions();
125 while ((c
= _PyOS_GetOpt(argc
, argv
, PROGRAM_OPTS
)) != EOF
) {
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);
133 "not enough memory to copy -c argument");
134 strcpy(command
, _PyOS_optarg
);
135 strcat(command
, "\n");
147 saw_inspect_flag
= 1;
148 Py_InteractiveFlag
++;
160 Py_IgnoreEnvironmentFlag
++;
169 saw_unbuffered_flag
= 1;
178 Py_RISCOSWimpFlag
= 1;
197 PySys_AddWarnOption(_PyOS_optarg
);
200 /* This space reserved for other options */
213 fprintf(stderr
, "Python %s\n", PY_VERSION
);
217 if (!saw_inspect_flag
&&
218 (p
= Py_GETENV("PYTHONINSPECT")) && *p
!= '\0')
220 if (!saw_unbuffered_flag
&&
221 (p
= Py_GETENV("PYTHONUNBUFFERED")) && *p
!= '\0')
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",
234 else if (skipfirstline
) {
236 /* Push back first newline so line numbers
238 while ((ch
= getc(fp
)) != EOF
) {
240 (void)ungetc(ch
, fp
);
248 stdin_is_interactive
= Py_FdIsInteractive(stdin
, (char *)0);
252 _setmode(fileno(stdin
), O_BINARY
);
253 _setmode(fileno(stdout
), O_BINARY
);
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 */
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
);
272 else if (Py_InteractiveFlag
) {
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 */
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]);
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' */
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
))) {
306 v
= PyImport_ImportModule("readline");
316 sts
= PyRun_SimpleString(command
) != 0;
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");
325 (void) PyRun_SimpleFile(fp
, startup
);
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
))
341 sts
= PyRun_AnyFileFlags(stdin
, "<stdin>", &cf
) != 0;
345 if(Py_RISCOSWimpFlag
)
346 fprintf(stderr
, "\x0cq\x0c"); /* make frontend quit */
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
359 _Py_ReleaseInternedStrings();
360 #endif /* __INSURE__ */
366 /* Make the *original* argc/argv available to other modules.
367 This is rare, but it is needed by the secureware extension. */
370 Py_GetArgcArgv(int *argc
, char ***argv
)