blob: c8b583a455d796af2a390c528a15846e7364937c [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) {
165#ifndef MPW
166 setbuf(stdout, (char *)NULL);
167 setbuf(stderr, (char *)NULL);
168#else
169 /* On MPW (3.2) unbuffered seems to hang */
170 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
171 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
172#endif
173 }
174
175 if (command == NULL && optind < argc &&
176 strcmp(argv[optind], "-") != 0)
177 filename = argv[optind];
178
179 if (Py_VerboseFlag ||
Guido van Rossuma376cc51996-12-05 23:43:35 +0000180 (command == NULL && filename == NULL && isatty((int)fileno(fp))))
Guido van Rossum667d7041995-08-04 04:20:48 +0000181 fprintf(stderr, "Python %s\n%s\n",
Guido van Rossum582646a1996-05-28 22:30:17 +0000182 Py_GetVersion(), Py_GetCopyright());
Guido van Rossum667d7041995-08-04 04:20:48 +0000183
184 if (filename != NULL) {
185 if ((fp = fopen(filename, "r")) == NULL) {
186 fprintf(stderr, "%s: can't open file '%s'\n",
187 argv[0], filename);
188 exit(2);
189 }
190 }
191
192 Py_Initialize();
193
194 if (command != NULL) {
195 /* Backup optind and force sys.argv[0] = '-c' */
196 optind--;
197 argv[optind] = "-c";
198 }
199
200 PySys_SetArgv(argc-optind, argv+optind);
201
202 if (command) {
203 sts = PyRun_SimpleString(command) != 0;
204 }
205 else {
206 if (filename == NULL && isatty((int)fileno(fp))) {
207 char *startup = getenv("PYTHONSTARTUP");
208 if (startup != NULL && startup[0] != '\0') {
209 FILE *fp = fopen(startup, "r");
210 if (fp != NULL) {
211 (void) PyRun_SimpleFile(fp, startup);
212 PyErr_Clear();
213 fclose(fp);
214 }
215 }
216 }
217 sts = PyRun_AnyFile(
218 fp, filename == NULL ? "<stdin>" : filename) != 0;
219 if (filename != NULL)
220 fclose(fp);
221 }
222
223 if (inspect && isatty((int)fileno(stdin)) &&
224 (filename != NULL || command != NULL))
225 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
226
227 Py_Exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +0000228 return 0; /* Make gcc -Wall happy */
Guido van Rossum667d7041995-08-04 04:20:48 +0000229}
230
231
Guido van Rossum8026feb1996-06-20 16:49:26 +0000232/* Return the program name -- some code out there needs this
233 (currently _tkinter.c and importdl.c). */
Guido van Rossum667d7041995-08-04 04:20:48 +0000234
235char *
Guido van Rossumac56b031996-07-21 02:33:38 +0000236Py_GetProgramName()
Guido van Rossum667d7041995-08-04 04:20:48 +0000237{
238 return argv0;
239}
240
241
242/* Make the *original* argc/argv available to other modules.
243 This is rare, but it is needed by the secureware extension. */
244
245void
Guido van Rossumac56b031996-07-21 02:33:38 +0000246Py_GetArgcArgv(argc, argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000247 int *argc;
248 char ***argv;
249{
250 *argc = orig_argc;
251 *argv = orig_argv;
252}