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