Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* Python interpreter main program */ |
| 33 | |
| 34 | #include "Python.h" |
| 35 | |
Guido van Rossum | 66a7013 | 1996-12-09 18:46:58 +0000 | [diff] [blame] | 36 | #ifdef HAVE_UNISTD_H |
| 37 | #include <unistd.h> |
| 38 | #endif |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | 3e7ae7a | 1997-01-17 22:05:38 +0000 | [diff] [blame] | 40 | #ifdef MS_WINDOWS |
| 41 | #include <fcntl.h> |
| 42 | #endif |
| 43 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 44 | /* Interface to getopt(): */ |
| 45 | extern int optind; |
| 46 | extern char *optarg; |
| 47 | extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */ |
| 48 | |
| 49 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 50 | /* Subroutines that live in their own file */ |
Guido van Rossum | 582646a | 1996-05-28 22:30:17 +0000 | [diff] [blame] | 51 | extern char *Py_GetVersion(); |
| 52 | extern char *Py_GetCopyright(); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 53 | |
| 54 | |
Guido van Rossum | ac56b03 | 1996-07-21 02:33:38 +0000 | [diff] [blame] | 55 | /* For Py_GetProgramName(); set by main() */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 56 | static char *argv0; |
| 57 | |
Guido van Rossum | ac56b03 | 1996-07-21 02:33:38 +0000 | [diff] [blame] | 58 | /* For Py_GetArgcArgv(); set by main() */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 59 | static char **orig_argv; |
| 60 | static int orig_argc; |
| 61 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 62 | /* Short usage message (with %s for argv0) */ |
| 63 | static char *usage_line = |
Guido van Rossum | 7614da6 | 1997-03-03 19:14:45 +0000 | [diff] [blame] | 64 | "usage: %s [-d] [-i] [-O] [-s] [-u] [-v] [-c cmd | file | -] [arg] ...\n"; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 65 | |
| 66 | /* Long usage message, split into parts < 512 bytes */ |
| 67 | static char *usage_top = "\n\ |
| 68 | Options and arguments (and corresponding environment variables):\n\ |
| 69 | -d : debug output from parser (also PYTHONDEBUG=x)\n\ |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 70 | -i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\ |
| 71 | and force prompts, even if stdin does not appear to be a terminal.\n\ |
Guido van Rossum | 7614da6 | 1997-03-03 19:14:45 +0000 | [diff] [blame] | 72 | -O : optimize generated bytecode (a tad).\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 73 | -s : suppress printing of top level expressions (also PYTHONSUPPRESS=x)\n\ |
| 74 | -u : unbuffered stdout and stderr (also PYTHONUNBUFFERED=x)\n\ |
| 75 | -v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 76 | "; |
| 77 | static char *usage_bot = "\ |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 78 | -c cmd : program passed in as string (terminates option list)\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 79 | file : program read from script file\n\ |
| 80 | - : program read from stdin (default; interactive mode if a tty)\n\ |
| 81 | arg ...: arguments passed to program in sys.argv[1:]\n\ |
| 82 | \n\ |
| 83 | Other environment variables:\n\ |
| 84 | PYTHONSTARTUP: file executed on interactive startup (no default)\n\ |
| 85 | PYTHONPATH : colon-separated list of directories prefixed to the\n\ |
| 86 | default module search path. The result is sys.path.\n\ |
Guido van Rossum | 03ef647 | 1997-04-30 19:48:59 +0000 | [diff] [blame^] | 87 | PYTHONHOME : alternate <prefix> directory (or <prefix>:<exec_prefix>).\n\ |
| 88 | The default module search path uses <prefix>/lib/python1.5.\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 89 | "; |
| 90 | |
| 91 | |
| 92 | /* Main program */ |
| 93 | |
| 94 | int |
| 95 | main(argc, argv) |
| 96 | int argc; |
| 97 | char **argv; |
| 98 | { |
| 99 | int c; |
| 100 | int sts; |
| 101 | char *command = NULL; |
| 102 | char *filename = NULL; |
| 103 | FILE *fp = stdin; |
| 104 | char *p; |
| 105 | int inspect = 0; |
| 106 | int unbuffered = 0; |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 107 | int stdin_is_interactive = 0; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 108 | |
Guido van Rossum | ac56b03 | 1996-07-21 02:33:38 +0000 | [diff] [blame] | 109 | orig_argc = argc; /* For Py_GetArgcArgv() */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 110 | orig_argv = argv; |
Guido van Rossum | ac56b03 | 1996-07-21 02:33:38 +0000 | [diff] [blame] | 111 | argv0 = argv[0]; /* For Py_GetProgramName() */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 112 | |
| 113 | if ((p = getenv("PYTHONDEBUG")) && *p != '\0') |
| 114 | Py_DebugFlag = 1; |
| 115 | if ((p = getenv("PYTHONSUPPRESS")) && *p != '\0') |
| 116 | Py_SuppressPrintingFlag = 1; |
| 117 | if ((p = getenv("PYTHONVERBOSE")) && *p != '\0') |
| 118 | Py_VerboseFlag = 1; |
| 119 | if ((p = getenv("PYTHONINSPECT")) && *p != '\0') |
| 120 | inspect = 1; |
| 121 | if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0') |
| 122 | unbuffered = 1; |
| 123 | |
Guido van Rossum | 7614da6 | 1997-03-03 19:14:45 +0000 | [diff] [blame] | 124 | while ((c = getopt(argc, argv, "c:diOsuv")) != EOF) { |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 125 | if (c == 'c') { |
| 126 | /* -c is the last option; following arguments |
| 127 | that look like options are left for the |
| 128 | the command to interpret. */ |
| 129 | command = malloc(strlen(optarg) + 2); |
| 130 | if (command == NULL) |
| 131 | Py_FatalError( |
| 132 | "not enough memory to copy -c argument"); |
| 133 | strcpy(command, optarg); |
| 134 | strcat(command, "\n"); |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | switch (c) { |
| 139 | |
| 140 | case 'd': |
| 141 | Py_DebugFlag++; |
| 142 | break; |
| 143 | |
| 144 | case 'i': |
| 145 | inspect++; |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 146 | Py_InteractiveFlag++; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 147 | break; |
| 148 | |
Guido van Rossum | 7614da6 | 1997-03-03 19:14:45 +0000 | [diff] [blame] | 149 | case 'O': |
| 150 | Py_OptimizeFlag++; |
| 151 | break; |
| 152 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 153 | case 's': |
| 154 | Py_SuppressPrintingFlag++; |
| 155 | break; |
| 156 | |
| 157 | case 'u': |
| 158 | unbuffered++; |
| 159 | break; |
| 160 | |
| 161 | case 'v': |
| 162 | Py_VerboseFlag++; |
| 163 | break; |
| 164 | |
| 165 | /* This space reserved for other options */ |
| 166 | |
| 167 | default: |
| 168 | fprintf(stderr, usage_line, argv[0]); |
| 169 | fprintf(stderr, usage_top); |
| 170 | fprintf(stderr, usage_bot); |
| 171 | exit(2); |
| 172 | /*NOTREACHED*/ |
| 173 | |
| 174 | } |
| 175 | } |
| 176 | |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 177 | if (command == NULL && optind < argc && |
| 178 | strcmp(argv[optind], "-") != 0) |
| 179 | { |
| 180 | filename = argv[optind]; |
| 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 | |
| 190 | stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0); |
| 191 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 192 | if (unbuffered) { |
Guido van Rossum | f22d7e2 | 1997-01-11 19:28:55 +0000 | [diff] [blame] | 193 | #ifdef MS_WINDOWS |
Guido van Rossum | 3e7ae7a | 1997-01-17 22:05:38 +0000 | [diff] [blame] | 194 | _setmode(fileno(stdin), O_BINARY); |
| 195 | _setmode(fileno(stdout), O_BINARY); |
Guido van Rossum | f22d7e2 | 1997-01-11 19:28:55 +0000 | [diff] [blame] | 196 | #endif |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 197 | #ifndef MPW |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 198 | setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ); |
| 199 | setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); |
| 200 | setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 201 | #else |
| 202 | /* On MPW (3.2) unbuffered seems to hang */ |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 203 | setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 204 | setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ); |
| 205 | setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ); |
| 206 | #endif |
| 207 | } |
Guido van Rossum | 2a21219 | 1997-04-11 21:57:53 +0000 | [diff] [blame] | 208 | else if (Py_InteractiveFlag) { |
Guido van Rossum | b31c7dc | 1997-04-11 22:19:12 +0000 | [diff] [blame] | 209 | #ifdef MS_WINDOWS |
| 210 | /* Doesn't have to have line-buffered -- use unbuffered */ |
| 211 | setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ); |
| 212 | setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); |
| 213 | #else |
| 214 | setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ); |
| 215 | setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ); |
| 216 | #endif |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 217 | /* Leave stderr alone - it should be unbuffered anyway. */ |
| 218 | } |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 219 | |
| 220 | if (Py_VerboseFlag || |
Guido van Rossum | 129e91a | 1997-02-14 21:00:50 +0000 | [diff] [blame] | 221 | (command == NULL && filename == NULL && stdin_is_interactive)) |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 222 | fprintf(stderr, "Python %s\n%s\n", |
Guido van Rossum | 582646a | 1996-05-28 22:30:17 +0000 | [diff] [blame] | 223 | Py_GetVersion(), Py_GetCopyright()); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 224 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 225 | Py_Initialize(); |
| 226 | |
| 227 | if (command != NULL) { |
| 228 | /* Backup optind and force sys.argv[0] = '-c' */ |
| 229 | optind--; |
| 230 | argv[optind] = "-c"; |
| 231 | } |
| 232 | |
| 233 | PySys_SetArgv(argc-optind, argv+optind); |
| 234 | |
| 235 | if (command) { |
| 236 | sts = PyRun_SimpleString(command) != 0; |
| 237 | } |
| 238 | else { |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 239 | if (filename == NULL && stdin_is_interactive) { |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 240 | char *startup = getenv("PYTHONSTARTUP"); |
| 241 | if (startup != NULL && startup[0] != '\0') { |
| 242 | FILE *fp = fopen(startup, "r"); |
| 243 | if (fp != NULL) { |
| 244 | (void) PyRun_SimpleFile(fp, startup); |
| 245 | PyErr_Clear(); |
| 246 | fclose(fp); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | sts = PyRun_AnyFile( |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 251 | fp, |
| 252 | filename == NULL ? "<stdin>" : filename) != 0; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 253 | if (filename != NULL) |
| 254 | fclose(fp); |
| 255 | } |
| 256 | |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 257 | if (inspect && stdin_is_interactive && |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 258 | (filename != NULL || command != NULL)) |
| 259 | sts = PyRun_AnyFile(stdin, "<stdin>") != 0; |
| 260 | |
| 261 | Py_Exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 262 | return 0; /* Make gcc -Wall happy */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | |
Guido van Rossum | 8026feb | 1996-06-20 16:49:26 +0000 | [diff] [blame] | 266 | /* Return the program name -- some code out there needs this |
| 267 | (currently _tkinter.c and importdl.c). */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 268 | |
| 269 | char * |
Guido van Rossum | ac56b03 | 1996-07-21 02:33:38 +0000 | [diff] [blame] | 270 | Py_GetProgramName() |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 271 | { |
| 272 | return argv0; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | /* Make the *original* argc/argv available to other modules. |
| 277 | This is rare, but it is needed by the secureware extension. */ |
| 278 | |
| 279 | void |
Guido van Rossum | ac56b03 | 1996-07-21 02:33:38 +0000 | [diff] [blame] | 280 | Py_GetArgcArgv(argc, argv) |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 281 | int *argc; |
| 282 | char ***argv; |
| 283 | { |
| 284 | *argc = orig_argc; |
| 285 | *argv = orig_argv; |
| 286 | } |