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