blob: 801804bd4e000d818fe389fe4623b7e54c706bd6 [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();
Guido van Rossumfe4dfc71997-05-19 18:33:01 +000052extern char *Py_GetPlatform();
Guido van Rossum582646a1996-05-28 22:30:17 +000053extern char *Py_GetCopyright();
Guido van Rossum667d7041995-08-04 04:20:48 +000054
55
Guido van Rossumac56b031996-07-21 02:33:38 +000056/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000057static char **orig_argv;
58static int orig_argc;
59
Guido van Rossum667d7041995-08-04 04:20:48 +000060/* Short usage message (with %s for argv0) */
61static char *usage_line =
Guido van Rossum7614da61997-03-03 19:14:45 +000062"usage: %s [-d] [-i] [-O] [-s] [-u] [-v] [-c cmd | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000063
64/* Long usage message, split into parts < 512 bytes */
65static char *usage_top = "\n\
66Options and arguments (and corresponding environment variables):\n\
67-d : debug output from parser (also PYTHONDEBUG=x)\n\
Guido van Rossum775af911997-02-14 19:50:32 +000068-i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
69 and force prompts, even if stdin does not appear to be a terminal.\n\
Guido van Rossum7614da61997-03-03 19:14:45 +000070-O : optimize generated bytecode (a tad).\n\
Guido van Rossumed52aac1997-07-19 19:20:32 +000071-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000072-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000073";
74static char *usage_bot = "\
Guido van Rossum775af911997-02-14 19:50:32 +000075-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000076file : program read from script file\n\
77- : program read from stdin (default; interactive mode if a tty)\n\
78arg ...: arguments passed to program in sys.argv[1:]\n\
79\n\
80Other environment variables:\n\
81PYTHONSTARTUP: file executed on interactive startup (no default)\n\
82PYTHONPATH : colon-separated list of directories prefixed to the\n\
83 default module search path. The result is sys.path.\n\
Guido van Rossum03ef6471997-04-30 19:48:59 +000084PYTHONHOME : alternate <prefix> directory (or <prefix>:<exec_prefix>).\n\
85 The default module search path uses <prefix>/lib/python1.5.\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000086";
87
88
89/* Main program */
90
91int
Guido van Rossumed52aac1997-07-19 19:20:32 +000092Py_Main(argc, argv)
Guido van Rossum667d7041995-08-04 04:20:48 +000093 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;
Guido van Rossum775af911997-02-14 19:50:32 +0000104 int stdin_is_interactive = 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000105
Guido van Rossumac56b031996-07-21 02:33:38 +0000106 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000107 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000108
Guido van Rossum667d7041995-08-04 04:20:48 +0000109 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
110 inspect = 1;
111 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
112 unbuffered = 1;
113
Guido van Rossumed52aac1997-07-19 19:20:32 +0000114 while ((c = getopt(argc, argv, "c:diOuv")) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000115 if (c == 'c') {
116 /* -c is the last option; following arguments
117 that look like options are left for the
118 the command to interpret. */
119 command = malloc(strlen(optarg) + 2);
120 if (command == NULL)
121 Py_FatalError(
122 "not enough memory to copy -c argument");
123 strcpy(command, optarg);
124 strcat(command, "\n");
125 break;
126 }
127
128 switch (c) {
129
130 case 'd':
131 Py_DebugFlag++;
132 break;
133
134 case 'i':
135 inspect++;
Guido van Rossum775af911997-02-14 19:50:32 +0000136 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000137 break;
138
Guido van Rossum7614da61997-03-03 19:14:45 +0000139 case 'O':
140 Py_OptimizeFlag++;
141 break;
142
Guido van Rossum667d7041995-08-04 04:20:48 +0000143 case 'u':
144 unbuffered++;
145 break;
146
147 case 'v':
148 Py_VerboseFlag++;
149 break;
150
151 /* This space reserved for other options */
152
153 default:
154 fprintf(stderr, usage_line, argv[0]);
155 fprintf(stderr, usage_top);
156 fprintf(stderr, usage_bot);
157 exit(2);
158 /*NOTREACHED*/
159
160 }
161 }
162
Guido van Rossum775af911997-02-14 19:50:32 +0000163 if (command == NULL && optind < argc &&
164 strcmp(argv[optind], "-") != 0)
165 {
166 filename = argv[optind];
167 if (filename != NULL) {
168 if ((fp = fopen(filename, "r")) == NULL) {
169 fprintf(stderr, "%s: can't open file '%s'\n",
170 argv[0], filename);
171 exit(2);
172 }
173 }
174 }
175
176 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
177
Guido van Rossum667d7041995-08-04 04:20:48 +0000178 if (unbuffered) {
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000179#ifdef MS_WINDOWS
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000180 _setmode(fileno(stdin), O_BINARY);
181 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000182#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000183#ifndef MPW
Guido van Rossum775af911997-02-14 19:50:32 +0000184 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
185 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
186 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000187#else
188 /* On MPW (3.2) unbuffered seems to hang */
Guido van Rossum775af911997-02-14 19:50:32 +0000189 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000190 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
191 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
192#endif
193 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000194 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000195#ifdef MS_WINDOWS
196 /* Doesn't have to have line-buffered -- use unbuffered */
197 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
198 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
199#else
200 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
201 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
202#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000203 /* Leave stderr alone - it should be unbuffered anyway. */
204 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000205
Guido van Rossumed52aac1997-07-19 19:20:32 +0000206 Py_SetProgramName(argv[0]);
207 Py_Initialize();
208
Guido van Rossum667d7041995-08-04 04:20:48 +0000209 if (Py_VerboseFlag ||
Guido van Rossum129e91a1997-02-14 21:00:50 +0000210 (command == NULL && filename == NULL && stdin_is_interactive))
Guido van Rossumfe4dfc71997-05-19 18:33:01 +0000211 fprintf(stderr, "Python %s on %s\n%s\n",
212 Py_GetVersion(), Py_GetPlatform(), Py_GetCopyright());
Guido van Rossum667d7041995-08-04 04:20:48 +0000213
Guido van Rossum667d7041995-08-04 04:20:48 +0000214
215 if (command != NULL) {
216 /* Backup optind and force sys.argv[0] = '-c' */
217 optind--;
218 argv[optind] = "-c";
219 }
220
221 PySys_SetArgv(argc-optind, argv+optind);
222
223 if (command) {
224 sts = PyRun_SimpleString(command) != 0;
225 }
226 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000227 if (filename == NULL && stdin_is_interactive) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000228 char *startup = getenv("PYTHONSTARTUP");
229 if (startup != NULL && startup[0] != '\0') {
230 FILE *fp = fopen(startup, "r");
231 if (fp != NULL) {
232 (void) PyRun_SimpleFile(fp, startup);
233 PyErr_Clear();
234 fclose(fp);
235 }
236 }
237 }
238 sts = PyRun_AnyFile(
Guido van Rossum775af911997-02-14 19:50:32 +0000239 fp,
240 filename == NULL ? "<stdin>" : filename) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000241 if (filename != NULL)
242 fclose(fp);
243 }
244
Guido van Rossum775af911997-02-14 19:50:32 +0000245 if (inspect && stdin_is_interactive &&
Guido van Rossum667d7041995-08-04 04:20:48 +0000246 (filename != NULL || command != NULL))
247 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
248
249 Py_Exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +0000250 return 0; /* Make gcc -Wall happy */
Guido van Rossum667d7041995-08-04 04:20:48 +0000251}
252
253
Guido van Rossum667d7041995-08-04 04:20:48 +0000254/* Make the *original* argc/argv available to other modules.
255 This is rare, but it is needed by the secureware extension. */
256
257void
Guido van Rossumac56b031996-07-21 02:33:38 +0000258Py_GetArgcArgv(argc, argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000259 int *argc;
260 char ***argv;
261{
262 *argc = orig_argc;
263 *argv = orig_argv;
264}