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 | |
| 62 | |
| 63 | /* Short usage message (with %s for argv0) */ |
| 64 | static char *usage_line = |
| 65 | "usage: %s [-d] [-i] [-s] [-u ] [-v] [-c cmd | file | -] [arg] ...\n"; |
| 66 | |
| 67 | /* Long usage message, split into parts < 512 bytes */ |
| 68 | static char *usage_top = "\n\ |
| 69 | Options and arguments (and corresponding environment variables):\n\ |
| 70 | -d : debug output from parser (also PYTHONDEBUG=x)\n\ |
| 71 | -i : inspect interactively after running script (also PYTHONINSPECT=x)\n\ |
| 72 | -s : suppress printing of top level expressions (also PYTHONSUPPRESS=x)\n\ |
| 73 | -u : unbuffered stdout and stderr (also PYTHONUNBUFFERED=x)\n\ |
| 74 | -v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\ |
| 75 | -c cmd : program passed in as string (terminates option list)\n\ |
| 76 | "; |
| 77 | static char *usage_bot = "\ |
| 78 | file : program read from script file\n\ |
| 79 | - : program read from stdin (default; interactive mode if a tty)\n\ |
| 80 | arg ...: arguments passed to program in sys.argv[1:]\n\ |
| 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\ |
| 86 | "; |
| 87 | |
| 88 | |
| 89 | /* Main program */ |
| 90 | |
| 91 | int |
| 92 | main(argc, argv) |
| 93 | 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; |
| 104 | |
Guido van Rossum | ac56b03 | 1996-07-21 02:33:38 +0000 | [diff] [blame] | 105 | orig_argc = argc; /* For Py_GetArgcArgv() */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 106 | orig_argv = argv; |
Guido van Rossum | ac56b03 | 1996-07-21 02:33:38 +0000 | [diff] [blame] | 107 | argv0 = argv[0]; /* For Py_GetProgramName() */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 108 | |
| 109 | if ((p = getenv("PYTHONDEBUG")) && *p != '\0') |
| 110 | Py_DebugFlag = 1; |
| 111 | if ((p = getenv("PYTHONSUPPRESS")) && *p != '\0') |
| 112 | Py_SuppressPrintingFlag = 1; |
| 113 | if ((p = getenv("PYTHONVERBOSE")) && *p != '\0') |
| 114 | Py_VerboseFlag = 1; |
| 115 | if ((p = getenv("PYTHONINSPECT")) && *p != '\0') |
| 116 | inspect = 1; |
| 117 | if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0') |
| 118 | unbuffered = 1; |
| 119 | |
| 120 | while ((c = getopt(argc, argv, "c:disuv")) != EOF) { |
| 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++; |
| 142 | break; |
| 143 | |
| 144 | case 's': |
| 145 | Py_SuppressPrintingFlag++; |
| 146 | break; |
| 147 | |
| 148 | case 'u': |
| 149 | unbuffered++; |
| 150 | break; |
| 151 | |
| 152 | case 'v': |
| 153 | Py_VerboseFlag++; |
| 154 | break; |
| 155 | |
| 156 | /* This space reserved for other options */ |
| 157 | |
| 158 | default: |
| 159 | fprintf(stderr, usage_line, argv[0]); |
| 160 | fprintf(stderr, usage_top); |
| 161 | fprintf(stderr, usage_bot); |
| 162 | exit(2); |
| 163 | /*NOTREACHED*/ |
| 164 | |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (unbuffered) { |
Guido van Rossum | f22d7e2 | 1997-01-11 19:28:55 +0000 | [diff] [blame] | 169 | #ifdef MS_WINDOWS |
Guido van Rossum | 3e7ae7a | 1997-01-17 22:05:38 +0000 | [diff] [blame] | 170 | _setmode(fileno(stdin), O_BINARY); |
| 171 | _setmode(fileno(stdout), O_BINARY); |
Guido van Rossum | f22d7e2 | 1997-01-11 19:28:55 +0000 | [diff] [blame] | 172 | #endif |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 173 | #ifndef MPW |
| 174 | setbuf(stdout, (char *)NULL); |
| 175 | setbuf(stderr, (char *)NULL); |
| 176 | #else |
| 177 | /* On MPW (3.2) unbuffered seems to hang */ |
| 178 | setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ); |
| 179 | setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ); |
| 180 | #endif |
| 181 | } |
| 182 | |
| 183 | if (command == NULL && optind < argc && |
| 184 | strcmp(argv[optind], "-") != 0) |
| 185 | filename = argv[optind]; |
| 186 | |
| 187 | if (Py_VerboseFlag || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 188 | (command == NULL && filename == NULL && isatty((int)fileno(fp)))) |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 189 | fprintf(stderr, "Python %s\n%s\n", |
Guido van Rossum | 582646a | 1996-05-28 22:30:17 +0000 | [diff] [blame] | 190 | Py_GetVersion(), Py_GetCopyright()); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 191 | |
| 192 | if (filename != NULL) { |
| 193 | if ((fp = fopen(filename, "r")) == NULL) { |
| 194 | fprintf(stderr, "%s: can't open file '%s'\n", |
| 195 | argv[0], filename); |
| 196 | exit(2); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | Py_Initialize(); |
| 201 | |
| 202 | if (command != NULL) { |
| 203 | /* Backup optind and force sys.argv[0] = '-c' */ |
| 204 | optind--; |
| 205 | argv[optind] = "-c"; |
| 206 | } |
| 207 | |
| 208 | PySys_SetArgv(argc-optind, argv+optind); |
| 209 | |
| 210 | if (command) { |
| 211 | sts = PyRun_SimpleString(command) != 0; |
| 212 | } |
| 213 | else { |
| 214 | if (filename == NULL && isatty((int)fileno(fp))) { |
| 215 | char *startup = getenv("PYTHONSTARTUP"); |
| 216 | if (startup != NULL && startup[0] != '\0') { |
| 217 | FILE *fp = fopen(startup, "r"); |
| 218 | if (fp != NULL) { |
| 219 | (void) PyRun_SimpleFile(fp, startup); |
| 220 | PyErr_Clear(); |
| 221 | fclose(fp); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | sts = PyRun_AnyFile( |
| 226 | fp, filename == NULL ? "<stdin>" : filename) != 0; |
| 227 | if (filename != NULL) |
| 228 | fclose(fp); |
| 229 | } |
| 230 | |
| 231 | if (inspect && isatty((int)fileno(stdin)) && |
| 232 | (filename != NULL || command != NULL)) |
| 233 | sts = PyRun_AnyFile(stdin, "<stdin>") != 0; |
| 234 | |
| 235 | Py_Exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 236 | return 0; /* Make gcc -Wall happy */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | |
Guido van Rossum | 8026feb | 1996-06-20 16:49:26 +0000 | [diff] [blame] | 240 | /* Return the program name -- some code out there needs this |
| 241 | (currently _tkinter.c and importdl.c). */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 242 | |
| 243 | char * |
Guido van Rossum | ac56b03 | 1996-07-21 02:33:38 +0000 | [diff] [blame] | 244 | Py_GetProgramName() |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 245 | { |
| 246 | return argv0; |
| 247 | } |
| 248 | |
| 249 | |
| 250 | /* Make the *original* argc/argv available to other modules. |
| 251 | This is rare, but it is needed by the secureware extension. */ |
| 252 | |
| 253 | void |
Guido van Rossum | ac56b03 | 1996-07-21 02:33:38 +0000 | [diff] [blame] | 254 | Py_GetArgcArgv(argc, argv) |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 255 | int *argc; |
| 256 | char ***argv; |
| 257 | { |
| 258 | *argc = orig_argc; |
| 259 | *argv = orig_argv; |
| 260 | } |