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" |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 35 | #include "osdefs.h" |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 66a7013 | 1996-12-09 18:46:58 +0000 | [diff] [blame] | 37 | #ifdef HAVE_UNISTD_H |
| 38 | #include <unistd.h> |
| 39 | #endif |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 40 | |
Guido van Rossum | 3e7ae7a | 1997-01-17 22:05:38 +0000 | [diff] [blame] | 41 | #ifdef MS_WINDOWS |
| 42 | #include <fcntl.h> |
| 43 | #endif |
| 44 | |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 45 | #if defined(PYOS_OS2) || defined(MS_WINDOWS) |
| 46 | #define PYTHONHOMEHELP "<prefix>\\lib" |
| 47 | #else |
| 48 | #define PYTHONHOMEHELP "<prefix>/python1.5" |
| 49 | #endif |
| 50 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 51 | /* Interface to getopt(): */ |
| 52 | extern int optind; |
| 53 | extern char *optarg; |
| 54 | extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */ |
| 55 | |
| 56 | |
Guido van Rossum | ac56b03 | 1996-07-21 02:33:38 +0000 | [diff] [blame] | 57 | /* For Py_GetArgcArgv(); set by main() */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 58 | static char **orig_argv; |
| 59 | static int orig_argc; |
| 60 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 61 | /* Short usage message (with %s for argv0) */ |
| 62 | static char *usage_line = |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 63 | "usage: %s [-d] [-i] [-O] [-S] [-u] [-v] [-x] [-X] [-c cmd | file | -] [arg] ...\n"; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 64 | |
| 65 | /* Long usage message, split into parts < 512 bytes */ |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 66 | static char *usage_top = "\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 67 | Options and arguments (and corresponding environment variables):\n\ |
| 68 | -d : debug output from parser (also PYTHONDEBUG=x)\n\ |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 69 | -i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\ |
| 70 | 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] | 71 | -O : optimize generated bytecode (a tad).\n\ |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 72 | -S : don't imply 'import site' on initialization\n\ |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 73 | -u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 74 | "; |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 75 | static char *usage_mid = "\ |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 76 | -v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\ |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 77 | -x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\ |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 78 | -X : disable class based built-in exceptions\n\ |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 79 | -c cmd : program passed in as string (terminates option list)\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 80 | file : program read from script file\n\ |
| 81 | - : program read from stdin (default; interactive mode if a tty)\n\ |
| 82 | arg ...: arguments passed to program in sys.argv[1:]\n\ |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 83 | "; |
| 84 | static char *usage_bot = "\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 85 | Other environment variables:\n\ |
| 86 | PYTHONSTARTUP: file executed on interactive startup (no default)\n\ |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 87 | PYTHONPATH : '%c'-separated list of directories prefixed to the\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 88 | default module search path. The result is sys.path.\n\ |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 89 | PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\ |
| 90 | The default module search path uses %s.\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 91 | "; |
| 92 | |
| 93 | |
| 94 | /* Main program */ |
| 95 | |
| 96 | int |
Guido van Rossum | ed52aac | 1997-07-19 19:20:32 +0000 | [diff] [blame] | 97 | Py_Main(argc, argv) |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 98 | int argc; |
| 99 | char **argv; |
| 100 | { |
| 101 | int c; |
| 102 | int sts; |
| 103 | char *command = NULL; |
| 104 | char *filename = NULL; |
| 105 | FILE *fp = stdin; |
| 106 | char *p; |
| 107 | int inspect = 0; |
| 108 | int unbuffered = 0; |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 109 | int skipfirstline = 0; |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 110 | int stdin_is_interactive = 0; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 111 | |
Guido van Rossum | ac56b03 | 1996-07-21 02:33:38 +0000 | [diff] [blame] | 112 | orig_argc = argc; /* For Py_GetArgcArgv() */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 113 | orig_argv = argv; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 115 | if ((p = getenv("PYTHONINSPECT")) && *p != '\0') |
| 116 | inspect = 1; |
| 117 | if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0') |
| 118 | unbuffered = 1; |
| 119 | |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 120 | while ((c = getopt(argc, argv, "c:diOSuvxX")) != EOF) { |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 121 | if (c == 'c') { |
| 122 | /* -c is the last option; following arguments |
| 123 | that look like options are left for the |
| 124 | the command to interpret. */ |
| 125 | command = malloc(strlen(optarg) + 2); |
| 126 | if (command == NULL) |
| 127 | Py_FatalError( |
| 128 | "not enough memory to copy -c argument"); |
| 129 | strcpy(command, optarg); |
| 130 | strcat(command, "\n"); |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | switch (c) { |
| 135 | |
| 136 | case 'd': |
| 137 | Py_DebugFlag++; |
| 138 | break; |
| 139 | |
| 140 | case 'i': |
| 141 | inspect++; |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 142 | Py_InteractiveFlag++; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 143 | break; |
| 144 | |
Guido van Rossum | 7614da6 | 1997-03-03 19:14:45 +0000 | [diff] [blame] | 145 | case 'O': |
| 146 | Py_OptimizeFlag++; |
| 147 | break; |
| 148 | |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 149 | case 'S': |
| 150 | Py_NoSiteFlag++; |
| 151 | break; |
| 152 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 153 | case 'u': |
| 154 | unbuffered++; |
| 155 | break; |
| 156 | |
| 157 | case 'v': |
| 158 | Py_VerboseFlag++; |
| 159 | break; |
| 160 | |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 161 | case 'x': |
| 162 | skipfirstline = 1; |
| 163 | break; |
| 164 | |
Barry Warsaw | f488af3 | 1997-08-29 21:57:49 +0000 | [diff] [blame] | 165 | case 'X': |
Barry Warsaw | 83b6709 | 1997-08-29 22:20:16 +0000 | [diff] [blame] | 166 | Py_UseClassExceptionsFlag = 0; |
Barry Warsaw | f488af3 | 1997-08-29 21:57:49 +0000 | [diff] [blame] | 167 | break; |
| 168 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 169 | /* This space reserved for other options */ |
| 170 | |
| 171 | default: |
| 172 | fprintf(stderr, usage_line, argv[0]); |
| 173 | fprintf(stderr, usage_top); |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 174 | fprintf(stderr, usage_mid); |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 175 | fprintf(stderr, usage_bot, |
| 176 | DELIM, DELIM, PYTHONHOMEHELP); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 177 | exit(2); |
| 178 | /*NOTREACHED*/ |
| 179 | |
| 180 | } |
| 181 | } |
| 182 | |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 183 | if (command == NULL && optind < argc && |
| 184 | strcmp(argv[optind], "-") != 0) |
| 185 | { |
| 186 | filename = argv[optind]; |
| 187 | if (filename != NULL) { |
| 188 | if ((fp = fopen(filename, "r")) == NULL) { |
| 189 | fprintf(stderr, "%s: can't open file '%s'\n", |
| 190 | argv[0], filename); |
| 191 | exit(2); |
| 192 | } |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 193 | else if (skipfirstline) { |
| 194 | char line[256]; |
| 195 | fgets(line, sizeof line, fp); |
| 196 | } |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0); |
| 201 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 202 | if (unbuffered) { |
Guido van Rossum | f22d7e2 | 1997-01-11 19:28:55 +0000 | [diff] [blame] | 203 | #ifdef MS_WINDOWS |
Guido van Rossum | 3e7ae7a | 1997-01-17 22:05:38 +0000 | [diff] [blame] | 204 | _setmode(fileno(stdin), O_BINARY); |
| 205 | _setmode(fileno(stdout), O_BINARY); |
Guido van Rossum | f22d7e2 | 1997-01-11 19:28:55 +0000 | [diff] [blame] | 206 | #endif |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 207 | #ifndef MPW |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 208 | setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ); |
| 209 | setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); |
| 210 | setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 211 | #else |
| 212 | /* On MPW (3.2) unbuffered seems to hang */ |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 213 | setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 214 | setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ); |
| 215 | setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ); |
| 216 | #endif |
| 217 | } |
Guido van Rossum | 2a21219 | 1997-04-11 21:57:53 +0000 | [diff] [blame] | 218 | else if (Py_InteractiveFlag) { |
Guido van Rossum | b31c7dc | 1997-04-11 22:19:12 +0000 | [diff] [blame] | 219 | #ifdef MS_WINDOWS |
| 220 | /* Doesn't have to have line-buffered -- use unbuffered */ |
| 221 | setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ); |
| 222 | setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); |
| 223 | #else |
| 224 | setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ); |
| 225 | setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ); |
| 226 | #endif |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 227 | /* Leave stderr alone - it should be unbuffered anyway. */ |
| 228 | } |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 229 | |
Guido van Rossum | ed52aac | 1997-07-19 19:20:32 +0000 | [diff] [blame] | 230 | Py_SetProgramName(argv[0]); |
| 231 | Py_Initialize(); |
| 232 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 233 | if (Py_VerboseFlag || |
Guido van Rossum | 129e91a | 1997-02-14 21:00:50 +0000 | [diff] [blame] | 234 | (command == NULL && filename == NULL && stdin_is_interactive)) |
Guido van Rossum | fe4dfc7 | 1997-05-19 18:33:01 +0000 | [diff] [blame] | 235 | fprintf(stderr, "Python %s on %s\n%s\n", |
| 236 | Py_GetVersion(), Py_GetPlatform(), Py_GetCopyright()); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 237 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 238 | |
| 239 | if (command != NULL) { |
| 240 | /* Backup optind and force sys.argv[0] = '-c' */ |
| 241 | optind--; |
| 242 | argv[optind] = "-c"; |
| 243 | } |
| 244 | |
| 245 | PySys_SetArgv(argc-optind, argv+optind); |
| 246 | |
Guido van Rossum | 3d26cc9 | 1997-09-16 16:11:28 +0000 | [diff] [blame] | 247 | if ((inspect || (command == NULL && filename == NULL)) && |
| 248 | isatty(fileno(stdin))) { |
| 249 | PyObject *v; |
| 250 | v = PyImport_ImportModule("readline"); |
| 251 | if (v == NULL) |
| 252 | PyErr_Clear(); |
| 253 | else |
| 254 | Py_DECREF(v); |
| 255 | } |
| 256 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 257 | if (command) { |
| 258 | sts = PyRun_SimpleString(command) != 0; |
Guido van Rossum | 05f7c50 | 1997-08-02 03:00:42 +0000 | [diff] [blame] | 259 | free(command); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 260 | } |
| 261 | else { |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 262 | if (filename == NULL && stdin_is_interactive) { |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 263 | char *startup = getenv("PYTHONSTARTUP"); |
| 264 | if (startup != NULL && startup[0] != '\0') { |
| 265 | FILE *fp = fopen(startup, "r"); |
| 266 | if (fp != NULL) { |
| 267 | (void) PyRun_SimpleFile(fp, startup); |
| 268 | PyErr_Clear(); |
| 269 | fclose(fp); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | sts = PyRun_AnyFile( |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 274 | fp, |
| 275 | filename == NULL ? "<stdin>" : filename) != 0; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 276 | if (filename != NULL) |
| 277 | fclose(fp); |
| 278 | } |
| 279 | |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 280 | if (inspect && stdin_is_interactive && |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 281 | (filename != NULL || command != NULL)) |
| 282 | sts = PyRun_AnyFile(stdin, "<stdin>") != 0; |
| 283 | |
Guido van Rossum | 5d1770e | 1997-08-05 02:23:48 +0000 | [diff] [blame] | 284 | Py_Finalize(); |
Guido van Rossum | 05f7c50 | 1997-08-02 03:00:42 +0000 | [diff] [blame] | 285 | return sts; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 289 | /* Make the *original* argc/argv available to other modules. |
| 290 | This is rare, but it is needed by the secureware extension. */ |
| 291 | |
| 292 | void |
Guido van Rossum | ac56b03 | 1996-07-21 02:33:38 +0000 | [diff] [blame] | 293 | Py_GetArgcArgv(argc, argv) |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 294 | int *argc; |
| 295 | char ***argv; |
| 296 | { |
| 297 | *argc = orig_argc; |
| 298 | *argv = orig_argv; |
| 299 | } |