blob: 789f76456e931491dbb2af0eb756d50d777438ec [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
62
63/* Short usage message (with %s for argv0) */
64static char *usage_line =
65"usage: %s [-d] [-i] [-s] [-u ] [-v] [-c cmd | file | -] [arg] ...\n";
66
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\
71-i : inspect interactively after running script (also PYTHONINSPECT=x)\n\
72-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\
75-c cmd : program passed in as string (terminates option list)\n\
76";
77static char *usage_bot = "\
78file : 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;
104
Guido van Rossumac56b031996-07-21 02:33:38 +0000105 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000106 orig_argv = argv;
Guido van Rossumac56b031996-07-21 02:33:38 +0000107 argv0 = argv[0]; /* For Py_GetProgramName() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000108
109 if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
110 Py_DebugFlag = 1;
111 if ((p = getenv("PYTHONSUPPRESS")) && *p != '\0')
112 Py_SuppressPrintingFlag = 1;
113 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
114 Py_VerboseFlag = 1;
115 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
116 inspect = 1;
117 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
118 unbuffered = 1;
119
120 while ((c = getopt(argc, argv, "c:disuv")) != EOF) {
121 if (c == 'c') {
122 /* -c is the last option; following arguments
123 that look like options are left for the
124 the command to interpret. */
125 command = malloc(strlen(optarg) + 2);
126 if (command == NULL)
127 Py_FatalError(
128 "not enough memory to copy -c argument");
129 strcpy(command, optarg);
130 strcat(command, "\n");
131 break;
132 }
133
134 switch (c) {
135
136 case 'd':
137 Py_DebugFlag++;
138 break;
139
140 case 'i':
141 inspect++;
142 break;
143
144 case 's':
145 Py_SuppressPrintingFlag++;
146 break;
147
148 case 'u':
149 unbuffered++;
150 break;
151
152 case 'v':
153 Py_VerboseFlag++;
154 break;
155
156 /* This space reserved for other options */
157
158 default:
159 fprintf(stderr, usage_line, argv[0]);
160 fprintf(stderr, usage_top);
161 fprintf(stderr, usage_bot);
162 exit(2);
163 /*NOTREACHED*/
164
165 }
166 }
167
168 if (unbuffered) {
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000169#ifdef MS_WINDOWS
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000170 _setmode(fileno(stdin), O_BINARY);
171 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000172#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000173#ifndef MPW
174 setbuf(stdout, (char *)NULL);
175 setbuf(stderr, (char *)NULL);
176#else
177 /* On MPW (3.2) unbuffered seems to hang */
178 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
179 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
180#endif
181 }
182
183 if (command == NULL && optind < argc &&
184 strcmp(argv[optind], "-") != 0)
185 filename = argv[optind];
186
187 if (Py_VerboseFlag ||
Guido van Rossuma376cc51996-12-05 23:43:35 +0000188 (command == NULL && filename == NULL && isatty((int)fileno(fp))))
Guido van Rossum667d7041995-08-04 04:20:48 +0000189 fprintf(stderr, "Python %s\n%s\n",
Guido van Rossum582646a1996-05-28 22:30:17 +0000190 Py_GetVersion(), Py_GetCopyright());
Guido van Rossum667d7041995-08-04 04:20:48 +0000191
192 if (filename != NULL) {
193 if ((fp = fopen(filename, "r")) == NULL) {
194 fprintf(stderr, "%s: can't open file '%s'\n",
195 argv[0], filename);
196 exit(2);
197 }
198 }
199
200 Py_Initialize();
201
202 if (command != NULL) {
203 /* Backup optind and force sys.argv[0] = '-c' */
204 optind--;
205 argv[optind] = "-c";
206 }
207
208 PySys_SetArgv(argc-optind, argv+optind);
209
210 if (command) {
211 sts = PyRun_SimpleString(command) != 0;
212 }
213 else {
214 if (filename == NULL && isatty((int)fileno(fp))) {
215 char *startup = getenv("PYTHONSTARTUP");
216 if (startup != NULL && startup[0] != '\0') {
217 FILE *fp = fopen(startup, "r");
218 if (fp != NULL) {
219 (void) PyRun_SimpleFile(fp, startup);
220 PyErr_Clear();
221 fclose(fp);
222 }
223 }
224 }
225 sts = PyRun_AnyFile(
226 fp, filename == NULL ? "<stdin>" : filename) != 0;
227 if (filename != NULL)
228 fclose(fp);
229 }
230
231 if (inspect && isatty((int)fileno(stdin)) &&
232 (filename != NULL || command != NULL))
233 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
234
235 Py_Exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +0000236 return 0; /* Make gcc -Wall happy */
Guido van Rossum667d7041995-08-04 04:20:48 +0000237}
238
239
Guido van Rossum8026feb1996-06-20 16:49:26 +0000240/* Return the program name -- some code out there needs this
241 (currently _tkinter.c and importdl.c). */
Guido van Rossum667d7041995-08-04 04:20:48 +0000242
243char *
Guido van Rossumac56b031996-07-21 02:33:38 +0000244Py_GetProgramName()
Guido van Rossum667d7041995-08-04 04:20:48 +0000245{
246 return argv0;
247}
248
249
250/* Make the *original* argc/argv available to other modules.
251 This is rare, but it is needed by the secureware extension. */
252
253void
Guido van Rossumac56b031996-07-21 02:33:38 +0000254Py_GetArgcArgv(argc, argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000255 int *argc;
256 char ***argv;
257{
258 *argc = orig_argc;
259 *argv = orig_argv;
260}