Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* Python interpreter main program */ |
| 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | #include "allobjects.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 29 | extern int debugging; /* Needed in parser.c, declared in pythonrun.c */ |
| 30 | extern int verbose; /* Needed in import.c, declared in pythonrun.c */ |
| 31 | extern int suppress_print; /* Needed in ceval.c, declared in pythonrun.c */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 689e701 | 1991-06-07 13:59:53 +0000 | [diff] [blame] | 33 | /* Interface to getopt(): */ |
| 34 | extern int optind; |
| 35 | extern char *optarg; |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 36 | extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */ |
Guido van Rossum | 689e701 | 1991-06-07 13:59:53 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 38 | extern char *getenv(); |
| 39 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 40 | extern char *getversion(); |
| 41 | extern char *getcopyright(); |
| 42 | |
| 43 | int |
| 44 | realmain(argc, argv) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 45 | int argc; |
| 46 | char **argv; |
| 47 | { |
Guido van Rossum | 689e701 | 1991-06-07 13:59:53 +0000 | [diff] [blame] | 48 | int c; |
| 49 | int sts; |
| 50 | char *command = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 51 | char *filename = NULL; |
| 52 | FILE *fp = stdin; |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 53 | char *p; |
| 54 | int inspect = 0; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 55 | int unbuffered = 0; |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 56 | |
| 57 | if ((p = getenv("PYTHONDEBUG")) && *p != '\0') |
| 58 | debugging = 1; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 59 | if ((p = getenv("PYTHONSUPPRESS")) && *p != '\0') |
| 60 | suppress_print = 1; |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 61 | if ((p = getenv("PYTHONVERBOSE")) && *p != '\0') |
| 62 | verbose = 1; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 63 | if ((p = getenv("PYTHONINSPECT")) && *p != '\0') |
| 64 | inspect = 1; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 65 | if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0') |
| 66 | unbuffered = 1; |
Guido van Rossum | 689e701 | 1991-06-07 13:59:53 +0000 | [diff] [blame] | 67 | |
Jack Jansen | b7d0df4 | 1995-07-29 13:53:59 +0000 | [diff] [blame] | 68 | #ifdef macintosh |
| 69 | PyMac_InteractiveOptions(&inspect, &verbose, &suppress_print, &unbuffered, &debugging); |
| 70 | #endif |
| 71 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 72 | while ((c = getopt(argc, argv, "c:disuv")) != EOF) { |
Guido van Rossum | 46b1638 | 1992-01-02 16:16:18 +0000 | [diff] [blame] | 73 | if (c == 'c') { |
| 74 | /* -c is the last option; following arguments |
| 75 | that look like options are left for the |
| 76 | the command to interpret. */ |
| 77 | command = malloc(strlen(optarg) + 2); |
Guido van Rossum | a66e25d | 1994-09-29 09:38:18 +0000 | [diff] [blame] | 78 | if (command == NULL) |
| 79 | fatal("not enough memory to copy -c argument"); |
Guido van Rossum | 46b1638 | 1992-01-02 16:16:18 +0000 | [diff] [blame] | 80 | strcpy(command, optarg); |
| 81 | strcat(command, "\n"); |
| 82 | break; |
| 83 | } |
| 84 | |
Guido van Rossum | 689e701 | 1991-06-07 13:59:53 +0000 | [diff] [blame] | 85 | switch (c) { |
| 86 | |
Guido van Rossum | 8401e56 | 1992-01-19 16:48:36 +0000 | [diff] [blame] | 87 | case 'd': |
| 88 | debugging++; |
| 89 | break; |
| 90 | |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 91 | case 'i': |
| 92 | inspect++; |
| 93 | break; |
| 94 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 95 | case 's': |
| 96 | suppress_print++; |
| 97 | break; |
| 98 | |
| 99 | case 'u': |
| 100 | unbuffered++; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 101 | break; |
| 102 | |
Guido van Rossum | e3d7045 | 1992-03-27 17:21:30 +0000 | [diff] [blame] | 103 | case 'v': |
| 104 | verbose++; |
| 105 | break; |
| 106 | |
Guido van Rossum | 46b1638 | 1992-01-02 16:16:18 +0000 | [diff] [blame] | 107 | /* This space reserved for other options */ |
| 108 | |
Guido van Rossum | 689e701 | 1991-06-07 13:59:53 +0000 | [diff] [blame] | 109 | default: |
| 110 | fprintf(stderr, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 111 | "usage: %s [-d] [-i] [-s] [-u ] [-v] [-c cmd | file | -] [arg] ...\n", |
Guido van Rossum | 689e701 | 1991-06-07 13:59:53 +0000 | [diff] [blame] | 112 | argv[0]); |
Jack Jansen | eceb3e3 | 1995-06-27 13:15:15 +0000 | [diff] [blame] | 113 | #if !(defined(__CFM68K__) && defined(__MWERKS__)) |
| 114 | /* Mwerks cfm68k linker doesn't like these... */ |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 115 | fprintf(stderr, "\ |
| 116 | \n\ |
| 117 | Options and arguments (and corresponding environment variables):\n\ |
| 118 | -d : debug output from parser (also PYTHONDEBUG=x)\n\ |
| 119 | -i : inspect interactively after running script (also PYTHONINSPECT=x)\n\ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 120 | -s : suppress the printing of top level expressions (also PYTHONSUPPRESS=x)\n\ |
| 121 | -u : unbuffered stdout and stderr (also PYTHONUNBUFFERED=x)\n\ |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 122 | -v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\ |
| 123 | -c cmd : program passed in as string (terminates option list)\n\ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 124 | "); |
| 125 | /* ANSI does not allow strings > 512 chars |
| 126 | and MPW doesn't like it either -- so split it! */ |
| 127 | fprintf(stderr, "\ |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 128 | file : program read from script file\n\ |
| 129 | - : program read from stdin (default; interactive mode if a tty)\n\ |
| 130 | arg ...: arguments passed to program in sys.argv[1:]\n\ |
| 131 | \n\ |
| 132 | Other environment variables:\n\ |
| 133 | PYTHONSTARTUP: file executed on interactive startup (no default)\n\ |
| 134 | PYTHONPATH : colon-separated list of directories prefixed to the\n\ |
| 135 | default module search path. The result is sys.path.\n\ |
| 136 | "); |
Jack Jansen | eceb3e3 | 1995-06-27 13:15:15 +0000 | [diff] [blame] | 137 | #endif /* !cfm68k || !mwerks */ |
Guido van Rossum | 689e701 | 1991-06-07 13:59:53 +0000 | [diff] [blame] | 138 | exit(2); |
| 139 | /*NOTREACHED*/ |
| 140 | |
Guido van Rossum | 689e701 | 1991-06-07 13:59:53 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 143 | |
| 144 | if (unbuffered) { |
| 145 | #ifndef MPW |
| 146 | setbuf(stdout, (char *)NULL); |
| 147 | setbuf(stderr, (char *)NULL); |
| 148 | #else |
| 149 | /* On MPW (3.2) unbuffered seems to hang */ |
| 150 | setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ); |
| 151 | setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ); |
| 152 | #endif |
| 153 | } |
| 154 | |
Guido van Rossum | 689e701 | 1991-06-07 13:59:53 +0000 | [diff] [blame] | 155 | if (command == NULL && optind < argc && strcmp(argv[optind], "-") != 0) |
| 156 | filename = argv[optind]; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 157 | |
| 158 | if (verbose || |
| 159 | command == NULL && filename == NULL && isatty((int)fileno(fp))) |
| 160 | fprintf(stderr, "Python %s\n%s\n", |
| 161 | getversion(), getcopyright()); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 162 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 163 | if (filename != NULL) { |
| 164 | if ((fp = fopen(filename, "r")) == NULL) { |
Guido van Rossum | 689e701 | 1991-06-07 13:59:53 +0000 | [diff] [blame] | 165 | fprintf(stderr, "%s: can't open file '%s'\n", |
| 166 | argv[0], filename); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 167 | exit(2); |
| 168 | } |
| 169 | } |
| 170 | |
Guido van Rossum | 138e6bf | 1992-06-03 17:08:15 +0000 | [diff] [blame] | 171 | initall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 172 | |
Guido van Rossum | 689e701 | 1991-06-07 13:59:53 +0000 | [diff] [blame] | 173 | if (command != NULL) { |
| 174 | /* Backup optind and force sys.argv[0] = '-c' */ |
| 175 | optind--; |
| 176 | argv[optind] = "-c"; |
| 177 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 178 | |
Guido van Rossum | 689e701 | 1991-06-07 13:59:53 +0000 | [diff] [blame] | 179 | setpythonargv(argc-optind, argv+optind); |
| 180 | |
| 181 | if (command) { |
| 182 | sts = run_command(command) != 0; |
| 183 | } |
| 184 | else { |
Guido van Rossum | da8cd86 | 1992-09-03 20:26:57 +0000 | [diff] [blame] | 185 | if (filename == NULL && isatty((int)fileno(fp))) { |
| 186 | char *startup = getenv("PYTHONSTARTUP"); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 187 | #ifdef macintosh |
| 188 | if (startup == NULL) |
| 189 | startup = "PythonStartup"; |
| 190 | #endif |
Guido van Rossum | da8cd86 | 1992-09-03 20:26:57 +0000 | [diff] [blame] | 191 | if (startup != NULL && startup[0] != '\0') { |
| 192 | FILE *fp = fopen(startup, "r"); |
| 193 | if (fp != NULL) { |
| 194 | (void) run_script(fp, startup); |
| 195 | err_clear(); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 196 | fclose(fp); |
Guido van Rossum | da8cd86 | 1992-09-03 20:26:57 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | } |
Guido van Rossum | 689e701 | 1991-06-07 13:59:53 +0000 | [diff] [blame] | 200 | sts = run(fp, filename == NULL ? "<stdin>" : filename) != 0; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 201 | if (filename != NULL) |
| 202 | fclose(fp); |
Guido van Rossum | 689e701 | 1991-06-07 13:59:53 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 205 | if (inspect && isatty((int)fileno(stdin)) && |
| 206 | (filename != NULL || command != NULL)) |
| 207 | sts = run(stdin, "<stdin>") != 0; |
| 208 | |
Guido van Rossum | 689e701 | 1991-06-07 13:59:53 +0000 | [diff] [blame] | 209 | goaway(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 210 | /*NOTREACHED*/ |
| 211 | } |