1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Python interpreter main program */
45 #if defined(PYOS_OS2) || defined(MS_WINDOWS)
46 #define PYTHONHOMEHELP "<prefix>\\lib"
48 #define PYTHONHOMEHELP "<prefix>/python1.5"
51 /* Interface to getopt(): */
54 extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
57 /* For Py_GetArgcArgv(); set by main() */
58 static char **orig_argv
;
61 /* Short usage message (with %s for argv0) */
62 static char *usage_line
=
63 "usage: %s [-d] [-i] [-O] [-S] [-u] [-v] [-x] [-X] [-c cmd | file | -] [arg] ...\n";
65 /* Long usage message, split into parts < 512 bytes */
66 static char *usage_top
= "\
67 Options and arguments (and corresponding environment variables):\n\
68 -d : debug output from parser (also PYTHONDEBUG=x)\n\
69 -i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
70 and force prompts, even if stdin does not appear to be a terminal\n\
71 -O : optimize generated bytecode (a tad)\n\
72 -S : don't imply 'import site' on initialization\n\
73 -t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
75 static char *usage_mid
= "\
76 -u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
77 -v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
78 -x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
79 -X : disable class based built-in exceptions\n\
80 -c cmd : program passed in as string (terminates option list)\n\
81 file : program read from script file\n\
82 - : program read from stdin (default; interactive mode if a tty)\n\
84 static char *usage_bot
= "\
85 arg ...: arguments passed to program in sys.argv[1:]\n\
86 Other environment variables:\n\
87 PYTHONSTARTUP: file executed on interactive startup (no default)\n\
88 PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
89 default module search path. The result is sys.path.\n\
90 PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
91 The default module search path uses %s.\n\
104 char *command
= NULL
;
105 char *filename
= NULL
;
110 int skipfirstline
= 0;
111 int stdin_is_interactive
= 0;
113 orig_argc
= argc
; /* For Py_GetArgcArgv() */
116 if ((p
= getenv("PYTHONINSPECT")) && *p
!= '\0')
118 if ((p
= getenv("PYTHONUNBUFFERED")) && *p
!= '\0')
121 while ((c
= getopt(argc
, argv
, "c:diOStuvxX")) != 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(optarg
) + 2);
129 "not enough memory to copy -c argument");
130 strcpy(command
, optarg
);
131 strcat(command
, "\n");
143 Py_InteractiveFlag
++;
171 Py_UseClassExceptionsFlag
= 0;
174 /* This space reserved for other options */
177 fprintf(stderr
, usage_line
, argv
[0]);
178 fprintf(stderr
, usage_top
);
179 fprintf(stderr
, usage_mid
);
180 fprintf(stderr
, usage_bot
,
181 DELIM
, DELIM
, PYTHONHOMEHELP
);
188 if (command
== NULL
&& optind
< argc
&&
189 strcmp(argv
[optind
], "-") != 0)
191 filename
= argv
[optind
];
192 if (filename
!= NULL
) {
193 if ((fp
= fopen(filename
, "r")) == NULL
) {
194 fprintf(stderr
, "%s: can't open file '%s'\n",
198 else if (skipfirstline
) {
200 fgets(line
, sizeof line
, fp
);
205 stdin_is_interactive
= Py_FdIsInteractive(stdin
, (char *)0);
209 _setmode(fileno(stdin
), O_BINARY
);
210 _setmode(fileno(stdout
), O_BINARY
);
214 setvbuf(stdin
, (char *)NULL
, _IONBF
, BUFSIZ
);
215 setvbuf(stdout
, (char *)NULL
, _IONBF
, BUFSIZ
);
216 setvbuf(stderr
, (char *)NULL
, _IONBF
, BUFSIZ
);
217 #else /* !HAVE_SETVBUF */
218 setbuf(stdin
, (char *)NULL
);
219 setbuf(stdout
, (char *)NULL
);
220 setbuf(stderr
, (char *)NULL
);
221 #endif /* !HAVE_SETVBUF */
223 /* On MPW (3.2) unbuffered seems to hang */
224 setvbuf(stdin
, (char *)NULL
, _IOLBF
, BUFSIZ
);
225 setvbuf(stdout
, (char *)NULL
, _IOLBF
, BUFSIZ
);
226 setvbuf(stderr
, (char *)NULL
, _IOLBF
, BUFSIZ
);
229 else if (Py_InteractiveFlag
) {
231 /* Doesn't have to have line-buffered -- use unbuffered */
232 setvbuf(stdin
, (char *)NULL
, _IONBF
, BUFSIZ
);
233 setvbuf(stdout
, (char *)NULL
, _IONBF
, BUFSIZ
);
234 #else /* !MS_WINDOWS */
236 setvbuf(stdin
, (char *)NULL
, _IOLBF
, BUFSIZ
);
237 setvbuf(stdout
, (char *)NULL
, _IOLBF
, BUFSIZ
);
238 #endif /* HAVE_SETVBUF */
239 #endif /* !MS_WINDOWS */
240 /* Leave stderr alone - it should be unbuffered anyway. */
243 Py_SetProgramName(argv
[0]);
246 if (Py_VerboseFlag
||
247 (command
== NULL
&& filename
== NULL
&& stdin_is_interactive
))
248 fprintf(stderr
, "Python %s on %s\n%s\n",
249 Py_GetVersion(), Py_GetPlatform(), Py_GetCopyright());
252 if (command
!= NULL
) {
253 /* Backup optind and force sys.argv[0] = '-c' */
258 PySys_SetArgv(argc
-optind
, argv
+optind
);
260 if ((inspect
|| (command
== NULL
&& filename
== NULL
)) &&
261 isatty(fileno(stdin
))) {
263 v
= PyImport_ImportModule("readline");
271 sts
= PyRun_SimpleString(command
) != 0;
275 if (filename
== NULL
&& stdin_is_interactive
) {
276 char *startup
= getenv("PYTHONSTARTUP");
277 if (startup
!= NULL
&& startup
[0] != '\0') {
278 FILE *fp
= fopen(startup
, "r");
280 (void) PyRun_SimpleFile(fp
, startup
);
288 filename
== NULL
? "<stdin>" : filename
) != 0;
289 if (filename
!= NULL
)
293 if (inspect
&& stdin_is_interactive
&&
294 (filename
!= NULL
|| command
!= NULL
))
295 sts
= PyRun_AnyFile(stdin
, "<stdin>") != 0;
302 /* Make the *original* argc/argv available to other modules.
303 This is rare, but it is needed by the secureware extension. */
306 Py_GetArgcArgv(argc
, argv
)