blob: 15d0cc5ccaf014820d7c559815d1fe3dcaa1a219 [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
40/* Interface to getopt(): */
41extern int optind;
42extern char *optarg;
43extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
44
45
Guido van Rossum667d7041995-08-04 04:20:48 +000046/* Subroutines that live in their own file */
Guido van Rossum582646a1996-05-28 22:30:17 +000047extern char *Py_GetVersion();
48extern char *Py_GetCopyright();
Guido van Rossum667d7041995-08-04 04:20:48 +000049
50
Guido van Rossumac56b031996-07-21 02:33:38 +000051/* For Py_GetProgramName(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000052static char *argv0;
53
Guido van Rossumac56b031996-07-21 02:33:38 +000054/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000055static char **orig_argv;
56static int orig_argc;
57
58
59/* Short usage message (with %s for argv0) */
60static char *usage_line =
61"usage: %s [-d] [-i] [-s] [-u ] [-v] [-c cmd | file | -] [arg] ...\n";
62
63/* Long usage message, split into parts < 512 bytes */
64static char *usage_top = "\n\
65Options and arguments (and corresponding environment variables):\n\
66-d : debug output from parser (also PYTHONDEBUG=x)\n\
67-i : inspect interactively after running script (also PYTHONINSPECT=x)\n\
68-s : suppress printing of top level expressions (also PYTHONSUPPRESS=x)\n\
69-u : unbuffered stdout and stderr (also PYTHONUNBUFFERED=x)\n\
70-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
71-c cmd : program passed in as string (terminates option list)\n\
72";
73static char *usage_bot = "\
74file : program read from script file\n\
75- : program read from stdin (default; interactive mode if a tty)\n\
76arg ...: arguments passed to program in sys.argv[1:]\n\
77\n\
78Other environment variables:\n\
79PYTHONSTARTUP: file executed on interactive startup (no default)\n\
80PYTHONPATH : colon-separated list of directories prefixed to the\n\
81 default module search path. The result is sys.path.\n\
82";
83
84
85/* Main program */
86
87int
88main(argc, argv)
89 int argc;
90 char **argv;
91{
92 int c;
93 int sts;
94 char *command = NULL;
95 char *filename = NULL;
96 FILE *fp = stdin;
97 char *p;
98 int inspect = 0;
99 int unbuffered = 0;
100
Guido van Rossumac56b031996-07-21 02:33:38 +0000101 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000102 orig_argv = argv;
Guido van Rossumac56b031996-07-21 02:33:38 +0000103 argv0 = argv[0]; /* For Py_GetProgramName() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000104
105 if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
106 Py_DebugFlag = 1;
107 if ((p = getenv("PYTHONSUPPRESS")) && *p != '\0')
108 Py_SuppressPrintingFlag = 1;
109 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
110 Py_VerboseFlag = 1;
111 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
112 inspect = 1;
113 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
114 unbuffered = 1;
115
116 while ((c = getopt(argc, argv, "c:disuv")) != EOF) {
117 if (c == 'c') {
118 /* -c is the last option; following arguments
119 that look like options are left for the
120 the command to interpret. */
121 command = malloc(strlen(optarg) + 2);
122 if (command == NULL)
123 Py_FatalError(
124 "not enough memory to copy -c argument");
125 strcpy(command, optarg);
126 strcat(command, "\n");
127 break;
128 }
129
130 switch (c) {
131
132 case 'd':
133 Py_DebugFlag++;
134 break;
135
136 case 'i':
137 inspect++;
138 break;
139
140 case 's':
141 Py_SuppressPrintingFlag++;
142 break;
143
144 case 'u':
145 unbuffered++;
146 break;
147
148 case 'v':
149 Py_VerboseFlag++;
150 break;
151
152 /* This space reserved for other options */
153
154 default:
155 fprintf(stderr, usage_line, argv[0]);
156 fprintf(stderr, usage_top);
157 fprintf(stderr, usage_bot);
158 exit(2);
159 /*NOTREACHED*/
160
161 }
162 }
163
164 if (unbuffered) {
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000165#ifdef MS_WINDOWS
166 _setmode(stdin, O_BINARY);
167 _setmode(stdout, O_BINARY);
168#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000169#ifndef MPW
170 setbuf(stdout, (char *)NULL);
171 setbuf(stderr, (char *)NULL);
172#else
173 /* On MPW (3.2) unbuffered seems to hang */
174 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
175 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
176#endif
177 }
178
179 if (command == NULL && optind < argc &&
180 strcmp(argv[optind], "-") != 0)
181 filename = argv[optind];
182
183 if (Py_VerboseFlag ||
Guido van Rossuma376cc51996-12-05 23:43:35 +0000184 (command == NULL && filename == NULL && isatty((int)fileno(fp))))
Guido van Rossum667d7041995-08-04 04:20:48 +0000185 fprintf(stderr, "Python %s\n%s\n",
Guido van Rossum582646a1996-05-28 22:30:17 +0000186 Py_GetVersion(), Py_GetCopyright());
Guido van Rossum667d7041995-08-04 04:20:48 +0000187
188 if (filename != NULL) {
189 if ((fp = fopen(filename, "r")) == NULL) {
190 fprintf(stderr, "%s: can't open file '%s'\n",
191 argv[0], filename);
192 exit(2);
193 }
194 }
195
196 Py_Initialize();
197
198 if (command != NULL) {
199 /* Backup optind and force sys.argv[0] = '-c' */
200 optind--;
201 argv[optind] = "-c";
202 }
203
204 PySys_SetArgv(argc-optind, argv+optind);
205
206 if (command) {
207 sts = PyRun_SimpleString(command) != 0;
208 }
209 else {
210 if (filename == NULL && isatty((int)fileno(fp))) {
211 char *startup = getenv("PYTHONSTARTUP");
212 if (startup != NULL && startup[0] != '\0') {
213 FILE *fp = fopen(startup, "r");
214 if (fp != NULL) {
215 (void) PyRun_SimpleFile(fp, startup);
216 PyErr_Clear();
217 fclose(fp);
218 }
219 }
220 }
221 sts = PyRun_AnyFile(
222 fp, filename == NULL ? "<stdin>" : filename) != 0;
223 if (filename != NULL)
224 fclose(fp);
225 }
226
227 if (inspect && isatty((int)fileno(stdin)) &&
228 (filename != NULL || command != NULL))
229 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
230
231 Py_Exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +0000232 return 0; /* Make gcc -Wall happy */
Guido van Rossum667d7041995-08-04 04:20:48 +0000233}
234
235
Guido van Rossum8026feb1996-06-20 16:49:26 +0000236/* Return the program name -- some code out there needs this
237 (currently _tkinter.c and importdl.c). */
Guido van Rossum667d7041995-08-04 04:20:48 +0000238
239char *
Guido van Rossumac56b031996-07-21 02:33:38 +0000240Py_GetProgramName()
Guido van Rossum667d7041995-08-04 04:20:48 +0000241{
242 return argv0;
243}
244
245
246/* Make the *original* argc/argv available to other modules.
247 This is rare, but it is needed by the secureware extension. */
248
249void
Guido van Rossumac56b031996-07-21 02:33:38 +0000250Py_GetArgcArgv(argc, argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000251 int *argc;
252 char ***argv;
253{
254 *argc = orig_argc;
255 *argv = orig_argv;
256}