blob: 00b2e4c0794d08525c25156e8a94a868f7519734 [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 Rossumac56b031996-07-21 02:33:38 +000050/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000051static char **orig_argv;
52static int orig_argc;
53
Guido van Rossum667d7041995-08-04 04:20:48 +000054/* Short usage message (with %s for argv0) */
55static char *usage_line =
Guido van Rossum7614da61997-03-03 19:14:45 +000056"usage: %s [-d] [-i] [-O] [-s] [-u] [-v] [-c cmd | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000057
58/* Long usage message, split into parts < 512 bytes */
59static char *usage_top = "\n\
60Options and arguments (and corresponding environment variables):\n\
61-d : debug output from parser (also PYTHONDEBUG=x)\n\
Guido van Rossum775af911997-02-14 19:50:32 +000062-i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
63 and force prompts, even if stdin does not appear to be a terminal.\n\
Guido van Rossum7614da61997-03-03 19:14:45 +000064-O : optimize generated bytecode (a tad).\n\
Guido van Rossumed52aac1997-07-19 19:20:32 +000065-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000066-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000067";
68static char *usage_bot = "\
Guido van Rossum775af911997-02-14 19:50:32 +000069-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000070file : program read from script file\n\
71- : program read from stdin (default; interactive mode if a tty)\n\
72arg ...: arguments passed to program in sys.argv[1:]\n\
73\n\
74Other environment variables:\n\
75PYTHONSTARTUP: file executed on interactive startup (no default)\n\
76PYTHONPATH : colon-separated list of directories prefixed to the\n\
77 default module search path. The result is sys.path.\n\
Guido van Rossum03ef6471997-04-30 19:48:59 +000078PYTHONHOME : alternate <prefix> directory (or <prefix>:<exec_prefix>).\n\
79 The default module search path uses <prefix>/lib/python1.5.\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000080";
81
82
83/* Main program */
84
85int
Guido van Rossumed52aac1997-07-19 19:20:32 +000086Py_Main(argc, argv)
Guido van Rossum667d7041995-08-04 04:20:48 +000087 int argc;
88 char **argv;
89{
90 int c;
91 int sts;
92 char *command = NULL;
93 char *filename = NULL;
94 FILE *fp = stdin;
95 char *p;
96 int inspect = 0;
97 int unbuffered = 0;
Guido van Rossum775af911997-02-14 19:50:32 +000098 int stdin_is_interactive = 0;
Guido van Rossum667d7041995-08-04 04:20:48 +000099
Guido van Rossumac56b031996-07-21 02:33:38 +0000100 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000101 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000102
Guido van Rossum667d7041995-08-04 04:20:48 +0000103 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
104 inspect = 1;
105 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
106 unbuffered = 1;
107
Guido van Rossumed52aac1997-07-19 19:20:32 +0000108 while ((c = getopt(argc, argv, "c:diOuv")) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000109 if (c == 'c') {
110 /* -c is the last option; following arguments
111 that look like options are left for the
112 the command to interpret. */
113 command = malloc(strlen(optarg) + 2);
114 if (command == NULL)
115 Py_FatalError(
116 "not enough memory to copy -c argument");
117 strcpy(command, optarg);
118 strcat(command, "\n");
119 break;
120 }
121
122 switch (c) {
123
124 case 'd':
125 Py_DebugFlag++;
126 break;
127
128 case 'i':
129 inspect++;
Guido van Rossum775af911997-02-14 19:50:32 +0000130 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000131 break;
132
Guido van Rossum7614da61997-03-03 19:14:45 +0000133 case 'O':
134 Py_OptimizeFlag++;
135 break;
136
Guido van Rossum667d7041995-08-04 04:20:48 +0000137 case 'u':
138 unbuffered++;
139 break;
140
141 case 'v':
142 Py_VerboseFlag++;
143 break;
144
145 /* This space reserved for other options */
146
147 default:
148 fprintf(stderr, usage_line, argv[0]);
149 fprintf(stderr, usage_top);
150 fprintf(stderr, usage_bot);
151 exit(2);
152 /*NOTREACHED*/
153
154 }
155 }
156
Guido van Rossum775af911997-02-14 19:50:32 +0000157 if (command == NULL && optind < argc &&
158 strcmp(argv[optind], "-") != 0)
159 {
160 filename = argv[optind];
161 if (filename != NULL) {
162 if ((fp = fopen(filename, "r")) == NULL) {
163 fprintf(stderr, "%s: can't open file '%s'\n",
164 argv[0], filename);
165 exit(2);
166 }
167 }
168 }
169
170 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
171
Guido van Rossum667d7041995-08-04 04:20:48 +0000172 if (unbuffered) {
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000173#ifdef MS_WINDOWS
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000174 _setmode(fileno(stdin), O_BINARY);
175 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000176#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000177#ifndef MPW
Guido van Rossum775af911997-02-14 19:50:32 +0000178 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
179 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
180 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000181#else
182 /* On MPW (3.2) unbuffered seems to hang */
Guido van Rossum775af911997-02-14 19:50:32 +0000183 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000184 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
185 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
186#endif
187 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000188 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000189#ifdef MS_WINDOWS
190 /* Doesn't have to have line-buffered -- use unbuffered */
191 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
192 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
193#else
194 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
195 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
196#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000197 /* Leave stderr alone - it should be unbuffered anyway. */
198 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000199
Guido van Rossumed52aac1997-07-19 19:20:32 +0000200 Py_SetProgramName(argv[0]);
201 Py_Initialize();
202
Guido van Rossum667d7041995-08-04 04:20:48 +0000203 if (Py_VerboseFlag ||
Guido van Rossum129e91a1997-02-14 21:00:50 +0000204 (command == NULL && filename == NULL && stdin_is_interactive))
Guido van Rossumfe4dfc71997-05-19 18:33:01 +0000205 fprintf(stderr, "Python %s on %s\n%s\n",
206 Py_GetVersion(), Py_GetPlatform(), Py_GetCopyright());
Guido van Rossum667d7041995-08-04 04:20:48 +0000207
Guido van Rossum667d7041995-08-04 04:20:48 +0000208
209 if (command != NULL) {
210 /* Backup optind and force sys.argv[0] = '-c' */
211 optind--;
212 argv[optind] = "-c";
213 }
214
215 PySys_SetArgv(argc-optind, argv+optind);
216
217 if (command) {
218 sts = PyRun_SimpleString(command) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000219 free(command);
Guido van Rossum667d7041995-08-04 04:20:48 +0000220 }
221 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000222 if (filename == NULL && stdin_is_interactive) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000223 char *startup = getenv("PYTHONSTARTUP");
224 if (startup != NULL && startup[0] != '\0') {
225 FILE *fp = fopen(startup, "r");
226 if (fp != NULL) {
227 (void) PyRun_SimpleFile(fp, startup);
228 PyErr_Clear();
229 fclose(fp);
230 }
231 }
232 }
233 sts = PyRun_AnyFile(
Guido van Rossum775af911997-02-14 19:50:32 +0000234 fp,
235 filename == NULL ? "<stdin>" : filename) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000236 if (filename != NULL)
237 fclose(fp);
238 }
239
Guido van Rossum775af911997-02-14 19:50:32 +0000240 if (inspect && stdin_is_interactive &&
Guido van Rossum667d7041995-08-04 04:20:48 +0000241 (filename != NULL || command != NULL))
242 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
243
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000244 Py_Finalize();
Guido van Rossum05f7c501997-08-02 03:00:42 +0000245 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000246}
247
248
Guido van Rossum667d7041995-08-04 04:20:48 +0000249/* Make the *original* argc/argv available to other modules.
250 This is rare, but it is needed by the secureware extension. */
251
252void
Guido van Rossumac56b031996-07-21 02:33:38 +0000253Py_GetArgcArgv(argc, argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000254 int *argc;
255 char ***argv;
256{
257 *argc = orig_argc;
258 *argv = orig_argv;
259}