blob: 5c631d01bd9c1c85840d3091354e2f356e8dca96 [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();
Guido van Rossumfe4dfc71997-05-19 18:33:01 +000052extern char *Py_GetPlatform();
Guido van Rossum582646a1996-05-28 22:30:17 +000053extern char *Py_GetCopyright();
Guido van Rossum667d7041995-08-04 04:20:48 +000054
55
Guido van Rossumac56b031996-07-21 02:33:38 +000056/* For Py_GetProgramName(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000057static char *argv0;
58
Guido van Rossumac56b031996-07-21 02:33:38 +000059/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000060static char **orig_argv;
61static int orig_argc;
62
Guido van Rossum667d7041995-08-04 04:20:48 +000063/* Short usage message (with %s for argv0) */
64static char *usage_line =
Guido van Rossum7614da61997-03-03 19:14:45 +000065"usage: %s [-d] [-i] [-O] [-s] [-u] [-v] [-c cmd | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000066
67/* Long usage message, split into parts < 512 bytes */
68static char *usage_top = "\n\
69Options and arguments (and corresponding environment variables):\n\
70-d : debug output from parser (also PYTHONDEBUG=x)\n\
Guido van Rossum775af911997-02-14 19:50:32 +000071-i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
72 and force prompts, even if stdin does not appear to be a terminal.\n\
Guido van Rossum7614da61997-03-03 19:14:45 +000073-O : optimize generated bytecode (a tad).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000074-s : suppress printing of top level expressions (also PYTHONSUPPRESS=x)\n\
75-u : unbuffered stdout and stderr (also PYTHONUNBUFFERED=x)\n\
76-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000077";
78static char *usage_bot = "\
Guido van Rossum775af911997-02-14 19:50:32 +000079-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000080file : program read from script file\n\
81- : program read from stdin (default; interactive mode if a tty)\n\
82arg ...: arguments passed to program in sys.argv[1:]\n\
83\n\
84Other environment variables:\n\
85PYTHONSTARTUP: file executed on interactive startup (no default)\n\
86PYTHONPATH : colon-separated list of directories prefixed to the\n\
87 default module search path. The result is sys.path.\n\
Guido van Rossum03ef6471997-04-30 19:48:59 +000088PYTHONHOME : alternate <prefix> directory (or <prefix>:<exec_prefix>).\n\
89 The default module search path uses <prefix>/lib/python1.5.\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000090";
91
92
93/* Main program */
94
95int
96main(argc, argv)
97 int argc;
98 char **argv;
99{
100 int c;
101 int sts;
102 char *command = NULL;
103 char *filename = NULL;
104 FILE *fp = stdin;
105 char *p;
106 int inspect = 0;
107 int unbuffered = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000108 int stdin_is_interactive = 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000109
Guido van Rossumac56b031996-07-21 02:33:38 +0000110 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000111 orig_argv = argv;
Guido van Rossumac56b031996-07-21 02:33:38 +0000112 argv0 = argv[0]; /* For Py_GetProgramName() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000113
114 if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
115 Py_DebugFlag = 1;
116 if ((p = getenv("PYTHONSUPPRESS")) && *p != '\0')
117 Py_SuppressPrintingFlag = 1;
118 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
119 Py_VerboseFlag = 1;
120 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
121 inspect = 1;
122 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
123 unbuffered = 1;
124
Guido van Rossum7614da61997-03-03 19:14:45 +0000125 while ((c = getopt(argc, argv, "c:diOsuv")) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000126 if (c == 'c') {
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(optarg) + 2);
131 if (command == NULL)
132 Py_FatalError(
133 "not enough memory to copy -c argument");
134 strcpy(command, optarg);
135 strcat(command, "\n");
136 break;
137 }
138
139 switch (c) {
140
141 case 'd':
142 Py_DebugFlag++;
143 break;
144
145 case 'i':
146 inspect++;
Guido van Rossum775af911997-02-14 19:50:32 +0000147 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000148 break;
149
Guido van Rossum7614da61997-03-03 19:14:45 +0000150 case 'O':
151 Py_OptimizeFlag++;
152 break;
153
Guido van Rossum667d7041995-08-04 04:20:48 +0000154 case 's':
155 Py_SuppressPrintingFlag++;
156 break;
157
158 case 'u':
159 unbuffered++;
160 break;
161
162 case 'v':
163 Py_VerboseFlag++;
164 break;
165
166 /* This space reserved for other options */
167
168 default:
169 fprintf(stderr, usage_line, argv[0]);
170 fprintf(stderr, usage_top);
171 fprintf(stderr, usage_bot);
172 exit(2);
173 /*NOTREACHED*/
174
175 }
176 }
177
Guido van Rossum775af911997-02-14 19:50:32 +0000178 if (command == NULL && optind < argc &&
179 strcmp(argv[optind], "-") != 0)
180 {
181 filename = argv[optind];
182 if (filename != NULL) {
183 if ((fp = fopen(filename, "r")) == NULL) {
184 fprintf(stderr, "%s: can't open file '%s'\n",
185 argv[0], filename);
186 exit(2);
187 }
188 }
189 }
190
191 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
192
Guido van Rossum667d7041995-08-04 04:20:48 +0000193 if (unbuffered) {
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000194#ifdef MS_WINDOWS
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000195 _setmode(fileno(stdin), O_BINARY);
196 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000197#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000198#ifndef MPW
Guido van Rossum775af911997-02-14 19:50:32 +0000199 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
200 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
201 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000202#else
203 /* On MPW (3.2) unbuffered seems to hang */
Guido van Rossum775af911997-02-14 19:50:32 +0000204 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000205 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
206 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
207#endif
208 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000209 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000210#ifdef MS_WINDOWS
211 /* Doesn't have to have line-buffered -- use unbuffered */
212 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
213 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
214#else
215 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
216 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
217#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000218 /* Leave stderr alone - it should be unbuffered anyway. */
219 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000220
221 if (Py_VerboseFlag ||
Guido van Rossum129e91a1997-02-14 21:00:50 +0000222 (command == NULL && filename == NULL && stdin_is_interactive))
Guido van Rossumfe4dfc71997-05-19 18:33:01 +0000223 fprintf(stderr, "Python %s on %s\n%s\n",
224 Py_GetVersion(), Py_GetPlatform(), Py_GetCopyright());
Guido van Rossum667d7041995-08-04 04:20:48 +0000225
Guido van Rossum667d7041995-08-04 04:20:48 +0000226 Py_Initialize();
227
228 if (command != NULL) {
229 /* Backup optind and force sys.argv[0] = '-c' */
230 optind--;
231 argv[optind] = "-c";
232 }
233
234 PySys_SetArgv(argc-optind, argv+optind);
235
236 if (command) {
237 sts = PyRun_SimpleString(command) != 0;
238 }
239 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000240 if (filename == NULL && stdin_is_interactive) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000241 char *startup = getenv("PYTHONSTARTUP");
242 if (startup != NULL && startup[0] != '\0') {
243 FILE *fp = fopen(startup, "r");
244 if (fp != NULL) {
245 (void) PyRun_SimpleFile(fp, startup);
246 PyErr_Clear();
247 fclose(fp);
248 }
249 }
250 }
251 sts = PyRun_AnyFile(
Guido van Rossum775af911997-02-14 19:50:32 +0000252 fp,
253 filename == NULL ? "<stdin>" : filename) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000254 if (filename != NULL)
255 fclose(fp);
256 }
257
Guido van Rossum775af911997-02-14 19:50:32 +0000258 if (inspect && stdin_is_interactive &&
Guido van Rossum667d7041995-08-04 04:20:48 +0000259 (filename != NULL || command != NULL))
260 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
261
262 Py_Exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +0000263 return 0; /* Make gcc -Wall happy */
Guido van Rossum667d7041995-08-04 04:20:48 +0000264}
265
266
Guido van Rossum8026feb1996-06-20 16:49:26 +0000267/* Return the program name -- some code out there needs this
268 (currently _tkinter.c and importdl.c). */
Guido van Rossum667d7041995-08-04 04:20:48 +0000269
270char *
Guido van Rossumac56b031996-07-21 02:33:38 +0000271Py_GetProgramName()
Guido van Rossum667d7041995-08-04 04:20:48 +0000272{
273 return argv0;
274}
275
276
277/* Make the *original* argc/argv available to other modules.
278 This is rare, but it is needed by the secureware extension. */
279
280void
Guido van Rossumac56b031996-07-21 02:33:38 +0000281Py_GetArgcArgv(argc, argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000282 int *argc;
283 char ***argv;
284{
285 *argc = orig_argc;
286 *argv = orig_argv;
287}