blob: 19c3e1dc786ca76c14594a83ce2ad53218318367 [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 Rossum775af911997-02-14 19:50:32 +000064"usage: %s [-d] [-i] [-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 Rossum667d7041995-08-04 04:20:48 +000072-s : suppress printing of top level expressions (also PYTHONSUPPRESS=x)\n\
73-u : unbuffered stdout and stderr (also PYTHONUNBUFFERED=x)\n\
74-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000075";
76static char *usage_bot = "\
Guido van Rossum775af911997-02-14 19:50:32 +000077-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000078file : program read from script file\n\
79- : program read from stdin (default; interactive mode if a tty)\n\
80arg ...: arguments passed to program in sys.argv[1:]\n\
81\n\
82Other environment variables:\n\
83PYTHONSTARTUP: file executed on interactive startup (no default)\n\
84PYTHONPATH : colon-separated list of directories prefixed to the\n\
85 default module search path. The result is sys.path.\n\
86";
87
88
89/* Main program */
90
91int
92main(argc, argv)
93 int argc;
94 char **argv;
95{
96 int c;
97 int sts;
98 char *command = NULL;
99 char *filename = NULL;
100 FILE *fp = stdin;
101 char *p;
102 int inspect = 0;
103 int unbuffered = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000104 int stdin_is_interactive = 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000105
Guido van Rossumac56b031996-07-21 02:33:38 +0000106 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000107 orig_argv = argv;
Guido van Rossumac56b031996-07-21 02:33:38 +0000108 argv0 = argv[0]; /* For Py_GetProgramName() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000109
110 if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
111 Py_DebugFlag = 1;
112 if ((p = getenv("PYTHONSUPPRESS")) && *p != '\0')
113 Py_SuppressPrintingFlag = 1;
114 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
115 Py_VerboseFlag = 1;
116 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
117 inspect = 1;
118 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
119 unbuffered = 1;
120
121 while ((c = getopt(argc, argv, "c:disuv")) != EOF) {
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(optarg) + 2);
127 if (command == NULL)
128 Py_FatalError(
129 "not enough memory to copy -c argument");
130 strcpy(command, optarg);
131 strcat(command, "\n");
132 break;
133 }
134
135 switch (c) {
136
137 case 'd':
138 Py_DebugFlag++;
139 break;
140
141 case 'i':
142 inspect++;
Guido van Rossum775af911997-02-14 19:50:32 +0000143 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000144 break;
145
146 case 's':
147 Py_SuppressPrintingFlag++;
148 break;
149
150 case 'u':
151 unbuffered++;
152 break;
153
154 case 'v':
155 Py_VerboseFlag++;
156 break;
157
158 /* This space reserved for other options */
159
160 default:
161 fprintf(stderr, usage_line, argv[0]);
162 fprintf(stderr, usage_top);
163 fprintf(stderr, usage_bot);
164 exit(2);
165 /*NOTREACHED*/
166
167 }
168 }
169
Guido van Rossum775af911997-02-14 19:50:32 +0000170 if (command == NULL && optind < argc &&
171 strcmp(argv[optind], "-") != 0)
172 {
173 filename = argv[optind];
174 if (filename != NULL) {
175 if ((fp = fopen(filename, "r")) == NULL) {
176 fprintf(stderr, "%s: can't open file '%s'\n",
177 argv[0], filename);
178 exit(2);
179 }
180 }
181 }
182
183 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
184
Guido van Rossum667d7041995-08-04 04:20:48 +0000185 if (unbuffered) {
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000186#ifdef MS_WINDOWS
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000187 _setmode(fileno(stdin), O_BINARY);
188 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000189#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000190#ifndef MPW
Guido van Rossum775af911997-02-14 19:50:32 +0000191 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
192 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
193 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000194#else
195 /* On MPW (3.2) unbuffered seems to hang */
Guido van Rossum775af911997-02-14 19:50:32 +0000196 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000197 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
198 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
199#endif
200 }
Guido van Rossum775af911997-02-14 19:50:32 +0000201 else if (stdin_is_interactive) {
202 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
203 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
204 /* Leave stderr alone - it should be unbuffered anyway. */
205 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000206
207 if (Py_VerboseFlag ||
Guido van Rossum775af911997-02-14 19:50:32 +0000208 command == NULL && filename == NULL && stdin_is_interactive)
Guido van Rossum667d7041995-08-04 04:20:48 +0000209 fprintf(stderr, "Python %s\n%s\n",
Guido van Rossum582646a1996-05-28 22:30:17 +0000210 Py_GetVersion(), Py_GetCopyright());
Guido van Rossum667d7041995-08-04 04:20:48 +0000211
Guido van Rossum667d7041995-08-04 04:20:48 +0000212 Py_Initialize();
213
214 if (command != NULL) {
215 /* Backup optind and force sys.argv[0] = '-c' */
216 optind--;
217 argv[optind] = "-c";
218 }
219
220 PySys_SetArgv(argc-optind, argv+optind);
221
222 if (command) {
223 sts = PyRun_SimpleString(command) != 0;
224 }
225 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000226 if (filename == NULL && stdin_is_interactive) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000227 char *startup = getenv("PYTHONSTARTUP");
228 if (startup != NULL && startup[0] != '\0') {
229 FILE *fp = fopen(startup, "r");
230 if (fp != NULL) {
231 (void) PyRun_SimpleFile(fp, startup);
232 PyErr_Clear();
233 fclose(fp);
234 }
235 }
236 }
237 sts = PyRun_AnyFile(
Guido van Rossum775af911997-02-14 19:50:32 +0000238 fp,
239 filename == NULL ? "<stdin>" : filename) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000240 if (filename != NULL)
241 fclose(fp);
242 }
243
Guido van Rossum775af911997-02-14 19:50:32 +0000244 if (inspect && stdin_is_interactive &&
Guido van Rossum667d7041995-08-04 04:20:48 +0000245 (filename != NULL || command != NULL))
246 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
247
248 Py_Exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +0000249 return 0; /* Make gcc -Wall happy */
Guido van Rossum667d7041995-08-04 04:20:48 +0000250}
251
252
Guido van Rossum8026feb1996-06-20 16:49:26 +0000253/* Return the program name -- some code out there needs this
254 (currently _tkinter.c and importdl.c). */
Guido van Rossum667d7041995-08-04 04:20:48 +0000255
256char *
Guido van Rossumac56b031996-07-21 02:33:38 +0000257Py_GetProgramName()
Guido van Rossum667d7041995-08-04 04:20:48 +0000258{
259 return argv0;
260}
261
262
263/* Make the *original* argc/argv available to other modules.
264 This is rare, but it is needed by the secureware extension. */
265
266void
Guido van Rossumac56b031996-07-21 02:33:38 +0000267Py_GetArgcArgv(argc, argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000268 int *argc;
269 char ***argv;
270{
271 *argc = orig_argc;
272 *argv = orig_argv;
273}