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