blob: 5d01abdabb43c3d0cb6f68c0f91f8048cb772511 [file] [log] [blame]
Guido van Rossum667d7041995-08-04 04:20:48 +00001/***********************************************************
2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum667d7041995-08-04 04:20:48 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum667d7041995-08-04 04:20:48 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum667d7041995-08-04 04:20:48 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum667d7041995-08-04 04:20:48 +000029
30******************************************************************/
31
32/* Python interpreter main program */
33
34#include "Python.h"
35
Guido van Rossum66a70131996-12-09 18:46:58 +000036#ifdef HAVE_UNISTD_H
37#include <unistd.h>
38#endif
Guido van Rossum667d7041995-08-04 04:20:48 +000039
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000040#ifdef MS_WINDOWS
41#include <fcntl.h>
42#endif
43
Guido van Rossum667d7041995-08-04 04:20:48 +000044/* Interface to getopt(): */
45extern int optind;
46extern char *optarg;
47extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
48
49
Guido van Rossum667d7041995-08-04 04:20:48 +000050/* Subroutines that live in their own file */
Guido van Rossum582646a1996-05-28 22:30:17 +000051extern char *Py_GetVersion();
52extern char *Py_GetCopyright();
Guido van Rossum667d7041995-08-04 04:20:48 +000053
54
Guido van Rossumac56b031996-07-21 02:33:38 +000055/* For Py_GetProgramName(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000056static char *argv0;
57
Guido van Rossumac56b031996-07-21 02:33:38 +000058/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000059static char **orig_argv;
60static int orig_argc;
61
Guido van Rossum667d7041995-08-04 04:20:48 +000062/* Short usage message (with %s for argv0) */
63static char *usage_line =
Guido van Rossum7614da61997-03-03 19:14:45 +000064"usage: %s [-d] [-i] [-O] [-s] [-u] [-v] [-c cmd | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000065
66/* Long usage message, split into parts < 512 bytes */
67static char *usage_top = "\n\
68Options and arguments (and corresponding environment variables):\n\
69-d : debug output from parser (also PYTHONDEBUG=x)\n\
Guido van Rossum775af911997-02-14 19:50:32 +000070-i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
71 and force prompts, even if stdin does not appear to be a terminal.\n\
Guido van Rossum7614da61997-03-03 19:14:45 +000072-O : optimize generated bytecode (a tad).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000073-s : suppress printing of top level expressions (also PYTHONSUPPRESS=x)\n\
74-u : unbuffered stdout and stderr (also PYTHONUNBUFFERED=x)\n\
75-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000076";
77static char *usage_bot = "\
Guido van Rossum775af911997-02-14 19:50:32 +000078-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000079file : program read from script file\n\
80- : program read from stdin (default; interactive mode if a tty)\n\
81arg ...: arguments passed to program in sys.argv[1:]\n\
82\n\
83Other environment variables:\n\
84PYTHONSTARTUP: file executed on interactive startup (no default)\n\
85PYTHONPATH : colon-separated list of directories prefixed to the\n\
86 default module search path. The result is sys.path.\n\
Guido van Rossum03ef6471997-04-30 19:48:59 +000087PYTHONHOME : alternate <prefix> directory (or <prefix>:<exec_prefix>).\n\
88 The default module search path uses <prefix>/lib/python1.5.\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000089";
90
91
92/* Main program */
93
94int
95main(argc, argv)
96 int argc;
97 char **argv;
98{
99 int c;
100 int sts;
101 char *command = NULL;
102 char *filename = NULL;
103 FILE *fp = stdin;
104 char *p;
105 int inspect = 0;
106 int unbuffered = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000107 int stdin_is_interactive = 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000108
Guido van Rossumac56b031996-07-21 02:33:38 +0000109 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000110 orig_argv = argv;
Guido van Rossumac56b031996-07-21 02:33:38 +0000111 argv0 = argv[0]; /* For Py_GetProgramName() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000112
113 if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
114 Py_DebugFlag = 1;
115 if ((p = getenv("PYTHONSUPPRESS")) && *p != '\0')
116 Py_SuppressPrintingFlag = 1;
117 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
118 Py_VerboseFlag = 1;
119 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
120 inspect = 1;
121 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
122 unbuffered = 1;
123
Guido van Rossum7614da61997-03-03 19:14:45 +0000124 while ((c = getopt(argc, argv, "c:diOsuv")) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000125 if (c == 'c') {
126 /* -c is the last option; following arguments
127 that look like options are left for the
128 the command to interpret. */
129 command = malloc(strlen(optarg) + 2);
130 if (command == NULL)
131 Py_FatalError(
132 "not enough memory to copy -c argument");
133 strcpy(command, optarg);
134 strcat(command, "\n");
135 break;
136 }
137
138 switch (c) {
139
140 case 'd':
141 Py_DebugFlag++;
142 break;
143
144 case 'i':
145 inspect++;
Guido van Rossum775af911997-02-14 19:50:32 +0000146 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000147 break;
148
Guido van Rossum7614da61997-03-03 19:14:45 +0000149 case 'O':
150 Py_OptimizeFlag++;
151 break;
152
Guido van Rossum667d7041995-08-04 04:20:48 +0000153 case 's':
154 Py_SuppressPrintingFlag++;
155 break;
156
157 case 'u':
158 unbuffered++;
159 break;
160
161 case 'v':
162 Py_VerboseFlag++;
163 break;
164
165 /* This space reserved for other options */
166
167 default:
168 fprintf(stderr, usage_line, argv[0]);
169 fprintf(stderr, usage_top);
170 fprintf(stderr, usage_bot);
171 exit(2);
172 /*NOTREACHED*/
173
174 }
175 }
176
Guido van Rossum775af911997-02-14 19:50:32 +0000177 if (command == NULL && optind < argc &&
178 strcmp(argv[optind], "-") != 0)
179 {
180 filename = argv[optind];
181 if (filename != NULL) {
182 if ((fp = fopen(filename, "r")) == NULL) {
183 fprintf(stderr, "%s: can't open file '%s'\n",
184 argv[0], filename);
185 exit(2);
186 }
187 }
188 }
189
190 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
191
Guido van Rossum667d7041995-08-04 04:20:48 +0000192 if (unbuffered) {
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000193#ifdef MS_WINDOWS
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000194 _setmode(fileno(stdin), O_BINARY);
195 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000196#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000197#ifndef MPW
Guido van Rossum775af911997-02-14 19:50:32 +0000198 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
199 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
200 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000201#else
202 /* On MPW (3.2) unbuffered seems to hang */
Guido van Rossum775af911997-02-14 19:50:32 +0000203 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000204 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
205 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
206#endif
207 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000208 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000209#ifdef MS_WINDOWS
210 /* Doesn't have to have line-buffered -- use unbuffered */
211 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
212 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
213#else
214 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
215 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
216#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000217 /* Leave stderr alone - it should be unbuffered anyway. */
218 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000219
220 if (Py_VerboseFlag ||
Guido van Rossum129e91a1997-02-14 21:00:50 +0000221 (command == NULL && filename == NULL && stdin_is_interactive))
Guido van Rossum667d7041995-08-04 04:20:48 +0000222 fprintf(stderr, "Python %s\n%s\n",
Guido van Rossum582646a1996-05-28 22:30:17 +0000223 Py_GetVersion(), Py_GetCopyright());
Guido van Rossum667d7041995-08-04 04:20:48 +0000224
Guido van Rossum667d7041995-08-04 04:20:48 +0000225 Py_Initialize();
226
227 if (command != NULL) {
228 /* Backup optind and force sys.argv[0] = '-c' */
229 optind--;
230 argv[optind] = "-c";
231 }
232
233 PySys_SetArgv(argc-optind, argv+optind);
234
235 if (command) {
236 sts = PyRun_SimpleString(command) != 0;
237 }
238 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000239 if (filename == NULL && stdin_is_interactive) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000240 char *startup = getenv("PYTHONSTARTUP");
241 if (startup != NULL && startup[0] != '\0') {
242 FILE *fp = fopen(startup, "r");
243 if (fp != NULL) {
244 (void) PyRun_SimpleFile(fp, startup);
245 PyErr_Clear();
246 fclose(fp);
247 }
248 }
249 }
250 sts = PyRun_AnyFile(
Guido van Rossum775af911997-02-14 19:50:32 +0000251 fp,
252 filename == NULL ? "<stdin>" : filename) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000253 if (filename != NULL)
254 fclose(fp);
255 }
256
Guido van Rossum775af911997-02-14 19:50:32 +0000257 if (inspect && stdin_is_interactive &&
Guido van Rossum667d7041995-08-04 04:20:48 +0000258 (filename != NULL || command != NULL))
259 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
260
261 Py_Exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +0000262 return 0; /* Make gcc -Wall happy */
Guido van Rossum667d7041995-08-04 04:20:48 +0000263}
264
265
Guido van Rossum8026feb1996-06-20 16:49:26 +0000266/* Return the program name -- some code out there needs this
267 (currently _tkinter.c and importdl.c). */
Guido van Rossum667d7041995-08-04 04:20:48 +0000268
269char *
Guido van Rossumac56b031996-07-21 02:33:38 +0000270Py_GetProgramName()
Guido van Rossum667d7041995-08-04 04:20:48 +0000271{
272 return argv0;
273}
274
275
276/* Make the *original* argc/argv available to other modules.
277 This is rare, but it is needed by the secureware extension. */
278
279void
Guido van Rossumac56b031996-07-21 02:33:38 +0000280Py_GetArgcArgv(argc, argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000281 int *argc;
282 char ***argv;
283{
284 *argc = orig_argc;
285 *argv = orig_argv;
286}