blob: 8b78f36178be4f5f0ebe6248198cf72911d2d1c6 [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
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
25/* Python interpreter main program */
26
27#include "Python.h"
28
29
30/* Interface to getopt(): */
31extern int optind;
32extern char *optarg;
33extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
34
35
Guido van Rossum667d7041995-08-04 04:20:48 +000036/* Subroutines that live in their own file */
Guido van Rossum582646a1996-05-28 22:30:17 +000037extern char *Py_GetVersion();
38extern char *Py_GetCopyright();
Guido van Rossum667d7041995-08-04 04:20:48 +000039
40
Guido van Rossumac56b031996-07-21 02:33:38 +000041/* For Py_GetProgramName(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000042static char *argv0;
43
Guido van Rossumac56b031996-07-21 02:33:38 +000044/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000045static char **orig_argv;
46static int orig_argc;
47
48
49/* Short usage message (with %s for argv0) */
50static char *usage_line =
51"usage: %s [-d] [-i] [-s] [-u ] [-v] [-c cmd | file | -] [arg] ...\n";
52
53/* Long usage message, split into parts < 512 bytes */
54static char *usage_top = "\n\
55Options and arguments (and corresponding environment variables):\n\
56-d : debug output from parser (also PYTHONDEBUG=x)\n\
57-i : inspect interactively after running script (also PYTHONINSPECT=x)\n\
58-s : suppress printing of top level expressions (also PYTHONSUPPRESS=x)\n\
59-u : unbuffered stdout and stderr (also PYTHONUNBUFFERED=x)\n\
60-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
61-c cmd : program passed in as string (terminates option list)\n\
62";
63static char *usage_bot = "\
64file : program read from script file\n\
65- : program read from stdin (default; interactive mode if a tty)\n\
66arg ...: arguments passed to program in sys.argv[1:]\n\
67\n\
68Other environment variables:\n\
69PYTHONSTARTUP: file executed on interactive startup (no default)\n\
70PYTHONPATH : colon-separated list of directories prefixed to the\n\
71 default module search path. The result is sys.path.\n\
72";
73
74
75/* Main program */
76
77int
78main(argc, argv)
79 int argc;
80 char **argv;
81{
82 int c;
83 int sts;
84 char *command = NULL;
85 char *filename = NULL;
86 FILE *fp = stdin;
87 char *p;
88 int inspect = 0;
89 int unbuffered = 0;
90
Guido van Rossumac56b031996-07-21 02:33:38 +000091 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +000092 orig_argv = argv;
Guido van Rossumac56b031996-07-21 02:33:38 +000093 argv0 = argv[0]; /* For Py_GetProgramName() */
Guido van Rossum667d7041995-08-04 04:20:48 +000094
95 if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
96 Py_DebugFlag = 1;
97 if ((p = getenv("PYTHONSUPPRESS")) && *p != '\0')
98 Py_SuppressPrintingFlag = 1;
99 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
100 Py_VerboseFlag = 1;
101 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
102 inspect = 1;
103 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
104 unbuffered = 1;
105
106 while ((c = getopt(argc, argv, "c:disuv")) != EOF) {
107 if (c == 'c') {
108 /* -c is the last option; following arguments
109 that look like options are left for the
110 the command to interpret. */
111 command = malloc(strlen(optarg) + 2);
112 if (command == NULL)
113 Py_FatalError(
114 "not enough memory to copy -c argument");
115 strcpy(command, optarg);
116 strcat(command, "\n");
117 break;
118 }
119
120 switch (c) {
121
122 case 'd':
123 Py_DebugFlag++;
124 break;
125
126 case 'i':
127 inspect++;
128 break;
129
130 case 's':
131 Py_SuppressPrintingFlag++;
132 break;
133
134 case 'u':
135 unbuffered++;
136 break;
137
138 case 'v':
139 Py_VerboseFlag++;
140 break;
141
142 /* This space reserved for other options */
143
144 default:
145 fprintf(stderr, usage_line, argv[0]);
146 fprintf(stderr, usage_top);
147 fprintf(stderr, usage_bot);
148 exit(2);
149 /*NOTREACHED*/
150
151 }
152 }
153
154 if (unbuffered) {
155#ifndef MPW
156 setbuf(stdout, (char *)NULL);
157 setbuf(stderr, (char *)NULL);
158#else
159 /* On MPW (3.2) unbuffered seems to hang */
160 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
161 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
162#endif
163 }
164
165 if (command == NULL && optind < argc &&
166 strcmp(argv[optind], "-") != 0)
167 filename = argv[optind];
168
169 if (Py_VerboseFlag ||
170 command == NULL && filename == NULL && isatty((int)fileno(fp)))
171 fprintf(stderr, "Python %s\n%s\n",
Guido van Rossum582646a1996-05-28 22:30:17 +0000172 Py_GetVersion(), Py_GetCopyright());
Guido van Rossum667d7041995-08-04 04:20:48 +0000173
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 Py_Initialize();
183
184 if (command != NULL) {
185 /* Backup optind and force sys.argv[0] = '-c' */
186 optind--;
187 argv[optind] = "-c";
188 }
189
190 PySys_SetArgv(argc-optind, argv+optind);
191
192 if (command) {
193 sts = PyRun_SimpleString(command) != 0;
194 }
195 else {
196 if (filename == NULL && isatty((int)fileno(fp))) {
197 char *startup = getenv("PYTHONSTARTUP");
198 if (startup != NULL && startup[0] != '\0') {
199 FILE *fp = fopen(startup, "r");
200 if (fp != NULL) {
201 (void) PyRun_SimpleFile(fp, startup);
202 PyErr_Clear();
203 fclose(fp);
204 }
205 }
206 }
207 sts = PyRun_AnyFile(
208 fp, filename == NULL ? "<stdin>" : filename) != 0;
209 if (filename != NULL)
210 fclose(fp);
211 }
212
213 if (inspect && isatty((int)fileno(stdin)) &&
214 (filename != NULL || command != NULL))
215 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
216
217 Py_Exit(sts);
218 /*NOTREACHED*/
219}
220
221
Guido van Rossum8026feb1996-06-20 16:49:26 +0000222/* Return the program name -- some code out there needs this
223 (currently _tkinter.c and importdl.c). */
Guido van Rossum667d7041995-08-04 04:20:48 +0000224
225char *
Guido van Rossumac56b031996-07-21 02:33:38 +0000226Py_GetProgramName()
Guido van Rossum667d7041995-08-04 04:20:48 +0000227{
228 return argv0;
229}
230
231
232/* Make the *original* argc/argv available to other modules.
233 This is rare, but it is needed by the secureware extension. */
234
235void
Guido van Rossumac56b031996-07-21 02:33:38 +0000236Py_GetArgcArgv(argc, argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000237 int *argc;
238 char ***argv;
239{
240 *argc = orig_argc;
241 *argv = orig_argv;
242}