Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 1 | /* Python interpreter main program */ |
| 2 | |
| 3 | #include "Python.h" |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 4 | #include "osdefs.h" |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 5 | #include "compile.h" /* For CO_FUTURE_DIVISION */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 66a7013 | 1996-12-09 18:46:58 +0000 | [diff] [blame] | 7 | #ifdef HAVE_UNISTD_H |
| 8 | #include <unistd.h> |
| 9 | #endif |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 3e7ae7a | 1997-01-17 22:05:38 +0000 | [diff] [blame] | 11 | #ifdef MS_WINDOWS |
| 12 | #include <fcntl.h> |
| 13 | #endif |
| 14 | |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 15 | #if defined(PYOS_OS2) || defined(MS_WINDOWS) |
| 16 | #define PYTHONHOMEHELP "<prefix>\\lib" |
| 17 | #else |
Marc-André Lemburg | da4dbc3 | 2001-06-12 16:13:51 +0000 | [diff] [blame] | 18 | #define PYTHONHOMEHELP "<prefix>/pythonX.X" |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 19 | #endif |
| 20 | |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 21 | #include "pygetopt.h" |
| 22 | |
Guido van Rossum | a22865e | 2000-09-05 04:41:18 +0000 | [diff] [blame] | 23 | #define COPYRIGHT \ |
Guido van Rossum | 36002d7 | 2001-07-18 16:59:46 +0000 | [diff] [blame] | 24 | "Type \"help\", \"copyright\", \"credits\" or \"license\" " \ |
| 25 | "for more information." |
Guido van Rossum | a22865e | 2000-09-05 04:41:18 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | ac56b03 | 1996-07-21 02:33:38 +0000 | [diff] [blame] | 27 | /* For Py_GetArgcArgv(); set by main() */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 28 | static char **orig_argv; |
| 29 | static int orig_argc; |
| 30 | |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 31 | /* command line options */ |
Guido van Rossum | 61c345f | 2001-09-04 03:26:15 +0000 | [diff] [blame] | 32 | #define BASE_OPTS "c:dEhiOQ:StuUvVW:xX" |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 33 | |
| 34 | #ifndef RISCOS |
| 35 | #define PROGRAM_OPTS BASE_OPTS |
| 36 | #else /*RISCOS*/ |
| 37 | /* extra option saying that we are running under a special task window |
| 38 | frontend; especially my_readline will behave different */ |
| 39 | #define PROGRAM_OPTS BASE_OPTS "w" |
| 40 | /* corresponding flag */ |
Guido van Rossum | 3ed4c15 | 2001-03-02 06:18:03 +0000 | [diff] [blame] | 41 | extern int Py_RISCOSWimpFlag; |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 42 | #endif /*RISCOS*/ |
Guido van Rossum | 3ed4c15 | 2001-03-02 06:18:03 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 44 | /* Short usage message (with %s for argv0) */ |
| 45 | static char *usage_line = |
Guido van Rossum | 6b86a42 | 1999-01-28 15:07:47 +0000 | [diff] [blame] | 46 | "usage: %s [option] ... [-c cmd | file | -] [arg] ...\n"; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 47 | |
| 48 | /* Long usage message, split into parts < 512 bytes */ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 49 | static char *usage_1 = "\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 50 | Options and arguments (and corresponding environment variables):\n\ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 51 | -c cmd : program passed in as string (terminates option list)\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 52 | -d : debug output from parser (also PYTHONDEBUG=x)\n\ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 53 | -E : ignore environment variables (such as PYTHONPATH)\n\ |
| 54 | -h : print this help message and exit\n\ |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 55 | -i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\ |
Guido van Rossum | bba92ca | 1998-04-10 19:39:15 +0000 | [diff] [blame] | 56 | and force prompts, even if stdin does not appear to be a terminal\n\ |
Guido van Rossum | 61c345f | 2001-09-04 03:26:15 +0000 | [diff] [blame] | 57 | "; |
| 58 | static char *usage_2 = "\ |
Guido van Rossum | e7adf3e | 1998-10-07 14:50:06 +0000 | [diff] [blame] | 59 | -O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\ |
Guido van Rossum | 6b86a42 | 1999-01-28 15:07:47 +0000 | [diff] [blame] | 60 | -OO : remove doc-strings in addition to the -O optimizations\n\ |
Guido van Rossum | 1832de4 | 2001-09-04 03:51:09 +0000 | [diff] [blame] | 61 | -Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\ |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 62 | -S : don't imply 'import site' on initialization\n\ |
Guido van Rossum | bba92ca | 1998-04-10 19:39:15 +0000 | [diff] [blame] | 63 | -t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\ |
Guido van Rossum | bba92ca | 1998-04-10 19:39:15 +0000 | [diff] [blame] | 64 | -u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 65 | "; |
| 66 | static char *usage_3 = "\ |
Guido van Rossum | c15a9a1 | 2000-05-01 17:54:33 +0000 | [diff] [blame] | 67 | -U : Unicode literals: treats '...' literals like u'...'\n\ |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 68 | -v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\ |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 69 | -V : print the Python version number and exit\n\ |
Guido van Rossum | 47f5fdc | 2000-12-15 22:00:54 +0000 | [diff] [blame] | 70 | -W arg : warning control (arg is action:message:category:module:lineno)\n\ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 71 | -x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 72 | file : program read from script file\n\ |
| 73 | - : program read from stdin (default; interactive mode if a tty)\n\ |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 74 | "; |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 75 | static char *usage_4 = "\ |
Guido van Rossum | bba92ca | 1998-04-10 19:39:15 +0000 | [diff] [blame] | 76 | arg ...: arguments passed to program in sys.argv[1:]\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 77 | Other environment variables:\n\ |
| 78 | PYTHONSTARTUP: file executed on interactive startup (no default)\n\ |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 79 | PYTHONPATH : '%c'-separated list of directories prefixed to the\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 80 | default module search path. The result is sys.path.\n\ |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 81 | PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\ |
| 82 | The default module search path uses %s.\n\ |
Tim Peters | 793de09 | 2001-02-22 00:39:47 +0000 | [diff] [blame] | 83 | PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 84 | "; |
| 85 | |
| 86 | |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 87 | static void |
| 88 | usage(int exitcode, char* program) |
| 89 | { |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 90 | FILE *f = exitcode ? stderr : stdout; |
| 91 | |
| 92 | fprintf(f, usage_line, program); |
| 93 | if (exitcode) |
| 94 | fprintf(f, "Try `python -h' for more information.\n"); |
| 95 | else { |
| 96 | fprintf(f, usage_1); |
| 97 | fprintf(f, usage_2); |
| 98 | fprintf(f, usage_3); |
| 99 | fprintf(f, usage_4, DELIM, DELIM, PYTHONHOMEHELP); |
| 100 | } |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 101 | exit(exitcode); |
| 102 | /*NOTREACHED*/ |
| 103 | } |
| 104 | |
| 105 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 106 | /* Main program */ |
| 107 | |
Guido van Rossum | 9c1201f | 1998-12-07 14:28:47 +0000 | [diff] [blame] | 108 | DL_EXPORT(int) |
Fredrik Lundh | 620f377 | 2000-07-09 20:42:34 +0000 | [diff] [blame] | 109 | Py_Main(int argc, char **argv) |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 110 | { |
| 111 | int c; |
| 112 | int sts; |
| 113 | char *command = NULL; |
| 114 | char *filename = NULL; |
| 115 | FILE *fp = stdin; |
| 116 | char *p; |
| 117 | int inspect = 0; |
| 118 | int unbuffered = 0; |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 119 | int skipfirstline = 0; |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 120 | int stdin_is_interactive = 0; |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 121 | int help = 0; |
| 122 | int version = 0; |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 123 | int saw_inspect_flag = 0; |
| 124 | int saw_unbuffered_flag = 0; |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 125 | PyCompilerFlags cf; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 126 | |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 127 | cf.cf_flags = 0; |
| 128 | |
Guido van Rossum | ac56b03 | 1996-07-21 02:33:38 +0000 | [diff] [blame] | 129 | orig_argc = argc; /* For Py_GetArgcArgv() */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 130 | orig_argv = argv; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 131 | |
Guido van Rossum | 3ed4c15 | 2001-03-02 06:18:03 +0000 | [diff] [blame] | 132 | #ifdef RISCOS |
| 133 | Py_RISCOSWimpFlag = 0; |
| 134 | #endif |
| 135 | |
Guido van Rossum | 47f5fdc | 2000-12-15 22:00:54 +0000 | [diff] [blame] | 136 | PySys_ResetWarnOptions(); |
| 137 | |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 138 | while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) { |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 139 | if (c == 'c') { |
| 140 | /* -c is the last option; following arguments |
| 141 | that look like options are left for the |
| 142 | the command to interpret. */ |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 143 | command = malloc(strlen(_PyOS_optarg) + 2); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 144 | if (command == NULL) |
| 145 | Py_FatalError( |
| 146 | "not enough memory to copy -c argument"); |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 147 | strcpy(command, _PyOS_optarg); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 148 | strcat(command, "\n"); |
| 149 | break; |
| 150 | } |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 151 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 152 | switch (c) { |
| 153 | |
| 154 | case 'd': |
| 155 | Py_DebugFlag++; |
| 156 | break; |
| 157 | |
Guido van Rossum | 61c345f | 2001-09-04 03:26:15 +0000 | [diff] [blame] | 158 | case 'Q': |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 159 | if (strcmp(_PyOS_optarg, "old") == 0) { |
| 160 | Py_DivisionWarningFlag = 0; |
| 161 | break; |
| 162 | } |
| 163 | if (strcmp(_PyOS_optarg, "warn") == 0) { |
Guido van Rossum | 1832de4 | 2001-09-04 03:51:09 +0000 | [diff] [blame] | 164 | Py_DivisionWarningFlag = 1; |
| 165 | break; |
| 166 | } |
| 167 | if (strcmp(_PyOS_optarg, "warnall") == 0) { |
| 168 | Py_DivisionWarningFlag = 2; |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 169 | break; |
| 170 | } |
| 171 | if (strcmp(_PyOS_optarg, "new") == 0) { |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 172 | /* This only affects __main__ */ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 173 | cf.cf_flags |= CO_FUTURE_DIVISION; |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 174 | /* And this tells the eval loop to treat |
| 175 | BINARY_DIVIDE as BINARY_TRUE_DIVIDE */ |
| 176 | _Py_QnewFlag = 1; |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 177 | break; |
| 178 | } |
| 179 | fprintf(stderr, |
Guido van Rossum | 1832de4 | 2001-09-04 03:51:09 +0000 | [diff] [blame] | 180 | "-Q option should be `-Qold', " |
| 181 | "`-Qwarn', `-Qwarnall', or `-Qnew' only\n"); |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 182 | usage(2, argv[0]); |
| 183 | /* NOTREACHED */ |
| 184 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 185 | case 'i': |
| 186 | inspect++; |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 187 | saw_inspect_flag = 1; |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 188 | Py_InteractiveFlag++; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 189 | break; |
| 190 | |
Guido van Rossum | 7614da6 | 1997-03-03 19:14:45 +0000 | [diff] [blame] | 191 | case 'O': |
| 192 | Py_OptimizeFlag++; |
| 193 | break; |
| 194 | |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 195 | case 'S': |
| 196 | Py_NoSiteFlag++; |
| 197 | break; |
| 198 | |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 199 | case 'E': |
| 200 | Py_IgnoreEnvironmentFlag++; |
| 201 | break; |
| 202 | |
Guido van Rossum | bba92ca | 1998-04-10 19:39:15 +0000 | [diff] [blame] | 203 | case 't': |
| 204 | Py_TabcheckFlag++; |
| 205 | break; |
| 206 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 207 | case 'u': |
| 208 | unbuffered++; |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 209 | saw_unbuffered_flag = 1; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 210 | break; |
| 211 | |
| 212 | case 'v': |
| 213 | Py_VerboseFlag++; |
| 214 | break; |
| 215 | |
Guido van Rossum | 3ed4c15 | 2001-03-02 06:18:03 +0000 | [diff] [blame] | 216 | #ifdef RISCOS |
| 217 | case 'w': |
| 218 | Py_RISCOSWimpFlag = 1; |
| 219 | break; |
| 220 | #endif |
| 221 | |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 222 | case 'x': |
| 223 | skipfirstline = 1; |
| 224 | break; |
| 225 | |
Guido van Rossum | c15a9a1 | 2000-05-01 17:54:33 +0000 | [diff] [blame] | 226 | case 'U': |
| 227 | Py_UnicodeFlag++; |
| 228 | break; |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 229 | case 'h': |
| 230 | help++; |
| 231 | break; |
| 232 | case 'V': |
| 233 | version++; |
| 234 | break; |
Guido van Rossum | c15a9a1 | 2000-05-01 17:54:33 +0000 | [diff] [blame] | 235 | |
Guido van Rossum | 47f5fdc | 2000-12-15 22:00:54 +0000 | [diff] [blame] | 236 | case 'W': |
| 237 | PySys_AddWarnOption(_PyOS_optarg); |
| 238 | break; |
| 239 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 240 | /* This space reserved for other options */ |
| 241 | |
| 242 | default: |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 243 | usage(2, argv[0]); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 244 | /*NOTREACHED*/ |
| 245 | |
| 246 | } |
| 247 | } |
| 248 | |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 249 | if (help) |
| 250 | usage(0, argv[0]); |
| 251 | |
| 252 | if (version) { |
| 253 | fprintf(stderr, "Python %s\n", PY_VERSION); |
| 254 | exit(0); |
| 255 | } |
| 256 | |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 257 | if (!saw_inspect_flag && |
| 258 | (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0') |
| 259 | inspect = 1; |
| 260 | if (!saw_unbuffered_flag && |
| 261 | (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0') |
| 262 | unbuffered = 1; |
| 263 | |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 264 | if (command == NULL && _PyOS_optind < argc && |
| 265 | strcmp(argv[_PyOS_optind], "-") != 0) |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 266 | { |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 267 | filename = argv[_PyOS_optind]; |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 268 | if (filename != NULL) { |
| 269 | if ((fp = fopen(filename, "r")) == NULL) { |
| 270 | fprintf(stderr, "%s: can't open file '%s'\n", |
| 271 | argv[0], filename); |
| 272 | exit(2); |
| 273 | } |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 274 | else if (skipfirstline) { |
Guido van Rossum | dc8b569 | 1999-04-19 17:54:19 +0000 | [diff] [blame] | 275 | int ch; |
| 276 | /* Push back first newline so line numbers |
| 277 | remain the same */ |
| 278 | while ((ch = getc(fp)) != EOF) { |
| 279 | if (ch == '\n') { |
| 280 | (void)ungetc(ch, fp); |
| 281 | break; |
| 282 | } |
| 283 | } |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 284 | } |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0); |
| 289 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 290 | if (unbuffered) { |
Guido van Rossum | f22d7e2 | 1997-01-11 19:28:55 +0000 | [diff] [blame] | 291 | #ifdef MS_WINDOWS |
Guido van Rossum | 3e7ae7a | 1997-01-17 22:05:38 +0000 | [diff] [blame] | 292 | _setmode(fileno(stdin), O_BINARY); |
| 293 | _setmode(fileno(stdout), O_BINARY); |
Guido van Rossum | f22d7e2 | 1997-01-11 19:28:55 +0000 | [diff] [blame] | 294 | #endif |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 295 | #ifndef MPW |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 296 | #ifdef HAVE_SETVBUF |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 297 | setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ); |
| 298 | setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); |
| 299 | setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 300 | #else /* !HAVE_SETVBUF */ |
| 301 | setbuf(stdin, (char *)NULL); |
| 302 | setbuf(stdout, (char *)NULL); |
| 303 | setbuf(stderr, (char *)NULL); |
| 304 | #endif /* !HAVE_SETVBUF */ |
| 305 | #else /* MPW */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 306 | /* On MPW (3.2) unbuffered seems to hang */ |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 307 | setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 308 | setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ); |
| 309 | setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 310 | #endif /* MPW */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 311 | } |
Guido van Rossum | 2a21219 | 1997-04-11 21:57:53 +0000 | [diff] [blame] | 312 | else if (Py_InteractiveFlag) { |
Guido van Rossum | b31c7dc | 1997-04-11 22:19:12 +0000 | [diff] [blame] | 313 | #ifdef MS_WINDOWS |
| 314 | /* Doesn't have to have line-buffered -- use unbuffered */ |
Guido van Rossum | 01b7ced | 1999-02-09 18:36:51 +0000 | [diff] [blame] | 315 | /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */ |
Guido van Rossum | b31c7dc | 1997-04-11 22:19:12 +0000 | [diff] [blame] | 316 | setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 317 | #else /* !MS_WINDOWS */ |
| 318 | #ifdef HAVE_SETVBUF |
Guido van Rossum | b31c7dc | 1997-04-11 22:19:12 +0000 | [diff] [blame] | 319 | setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ); |
| 320 | setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 321 | #endif /* HAVE_SETVBUF */ |
| 322 | #endif /* !MS_WINDOWS */ |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 323 | /* Leave stderr alone - it should be unbuffered anyway. */ |
| 324 | } |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 325 | |
Guido van Rossum | ed52aac | 1997-07-19 19:20:32 +0000 | [diff] [blame] | 326 | Py_SetProgramName(argv[0]); |
| 327 | Py_Initialize(); |
| 328 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 329 | if (Py_VerboseFlag || |
Guido van Rossum | 129e91a | 1997-02-14 21:00:50 +0000 | [diff] [blame] | 330 | (command == NULL && filename == NULL && stdin_is_interactive)) |
Guido van Rossum | fe4dfc7 | 1997-05-19 18:33:01 +0000 | [diff] [blame] | 331 | fprintf(stderr, "Python %s on %s\n%s\n", |
Guido van Rossum | a22865e | 2000-09-05 04:41:18 +0000 | [diff] [blame] | 332 | Py_GetVersion(), Py_GetPlatform(), COPYRIGHT); |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 333 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 334 | if (command != NULL) { |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 335 | /* Backup _PyOS_optind and force sys.argv[0] = '-c' */ |
| 336 | _PyOS_optind--; |
| 337 | argv[_PyOS_optind] = "-c"; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 340 | PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 341 | |
Guido van Rossum | 3d26cc9 | 1997-09-16 16:11:28 +0000 | [diff] [blame] | 342 | if ((inspect || (command == NULL && filename == NULL)) && |
| 343 | isatty(fileno(stdin))) { |
| 344 | PyObject *v; |
| 345 | v = PyImport_ImportModule("readline"); |
| 346 | if (v == NULL) |
| 347 | PyErr_Clear(); |
| 348 | else |
| 349 | Py_DECREF(v); |
| 350 | } |
| 351 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 352 | if (command) { |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 353 | sts = PyRun_SimpleStringFlags(command, &cf) != 0; |
Guido van Rossum | 05f7c50 | 1997-08-02 03:00:42 +0000 | [diff] [blame] | 354 | free(command); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 355 | } |
| 356 | else { |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 357 | if (filename == NULL && stdin_is_interactive) { |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 358 | char *startup = Py_GETENV("PYTHONSTARTUP"); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 359 | if (startup != NULL && startup[0] != '\0') { |
| 360 | FILE *fp = fopen(startup, "r"); |
| 361 | if (fp != NULL) { |
| 362 | (void) PyRun_SimpleFile(fp, startup); |
| 363 | PyErr_Clear(); |
| 364 | fclose(fp); |
| 365 | } |
| 366 | } |
| 367 | } |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 368 | /* XXX */ |
| 369 | sts = PyRun_AnyFileExFlags( |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 370 | fp, |
Guido van Rossum | 0df002c | 2000-08-27 19:21:52 +0000 | [diff] [blame] | 371 | filename == NULL ? "<stdin>" : filename, |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 372 | filename != NULL, &cf) != 0; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 375 | if (inspect && stdin_is_interactive && |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 376 | (filename != NULL || command != NULL)) |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 377 | /* XXX */ |
| 378 | sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 379 | |
Guido van Rossum | 5d1770e | 1997-08-05 02:23:48 +0000 | [diff] [blame] | 380 | Py_Finalize(); |
Guido van Rossum | 3ed4c15 | 2001-03-02 06:18:03 +0000 | [diff] [blame] | 381 | #ifdef RISCOS |
| 382 | if(Py_RISCOSWimpFlag) |
| 383 | fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */ |
| 384 | #endif |
Barry Warsaw | 3e13b1e | 2001-02-23 16:46:39 +0000 | [diff] [blame] | 385 | |
| 386 | #ifdef __INSURE__ |
| 387 | /* Insure++ is a memory analysis tool that aids in discovering |
| 388 | * memory leaks and other memory problems. On Python exit, the |
| 389 | * interned string dictionary is flagged as being in use at exit |
| 390 | * (which it is). Under normal circumstances, this is fine because |
| 391 | * the memory will be automatically reclaimed by the system. Under |
| 392 | * memory debugging, it's a huge source of useless noise, so we |
| 393 | * trade off slower shutdown for less distraction in the memory |
| 394 | * reports. -baw |
| 395 | */ |
| 396 | _Py_ReleaseInternedStrings(); |
| 397 | #endif /* __INSURE__ */ |
| 398 | |
Guido van Rossum | 05f7c50 | 1997-08-02 03:00:42 +0000 | [diff] [blame] | 399 | return sts; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 403 | /* Make the *original* argc/argv available to other modules. |
| 404 | This is rare, but it is needed by the secureware extension. */ |
| 405 | |
| 406 | void |
Fredrik Lundh | 620f377 | 2000-07-09 20:42:34 +0000 | [diff] [blame] | 407 | Py_GetArgcArgv(int *argc, char ***argv) |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 408 | { |
| 409 | *argc = orig_argc; |
| 410 | *argv = orig_argv; |
| 411 | } |