Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Jack Jansen | 42218ce | 1997-01-31 16:15:11 +0000 | [diff] [blame] | 2 | Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam, |
Guido van Rossum | 9954699 | 1995-01-08 14:33:34 +0000 | [diff] [blame] | 3 | The Netherlands. |
Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +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 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 25 | /* Python interpreter main program */ |
Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +0000 | [diff] [blame] | 26 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 27 | #include "Python.h" |
| 28 | #include "pythonresources.h" |
| 29 | #include "import.h" |
| 30 | #include "marshal.h" |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 31 | #include "macglue.h" |
Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +0000 | [diff] [blame] | 32 | |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 33 | #ifdef WITHOUT_FRAMEWORKS |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 34 | #include <Memory.h> |
| 35 | #include <Resources.h> |
Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +0000 | [diff] [blame] | 36 | #include <stdio.h> |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 37 | #include <Events.h> |
| 38 | #include <Windows.h> |
Jack Jansen | 2429c72 | 1996-03-07 15:17:11 +0000 | [diff] [blame] | 39 | #include <Fonts.h> |
Jack Jansen | 36b983c | 1997-09-09 13:53:21 +0000 | [diff] [blame] | 40 | #include <Balloons.h> |
Jack Jansen | 5ded1bf | 2001-10-30 22:48:36 +0000 | [diff] [blame] | 41 | #if TARGET_API_MAC_CARBON |
| 42 | #include <CFBundle.h> |
| 43 | #include <CFURL.h> |
| 44 | #include <CFString.h> |
| 45 | #include <CFBase.h> |
| 46 | #include <CFArray.h> |
| 47 | #endif /* TARGET_API_MAC_CARBON */ |
Jack Jansen | 8f5725a | 1999-12-07 23:08:10 +0000 | [diff] [blame] | 48 | #include <Gestalt.h> |
| 49 | #include <Appearance.h> |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 50 | #else |
| 51 | #include <Carbon/Carbon.h> |
| 52 | #endif /* WITHOUT_FRAMEWORKS */ |
| 53 | |
Jack Jansen | c76fd39 | 1995-02-02 14:27:31 +0000 | [diff] [blame] | 54 | #ifdef __MWERKS__ |
| 55 | #include <SIOUX.h> |
Jack Jansen | 1e8557a | 1995-11-10 14:51:26 +0000 | [diff] [blame] | 56 | #define USE_SIOUX |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 57 | extern int ccommand(char ***); |
Jack Jansen | 8c69321 | 1997-01-07 16:19:42 +0000 | [diff] [blame] | 58 | #if __profile__ == 1 |
| 59 | #include <profiler.h> |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 60 | #endif /* __profile__ */ |
| 61 | #endif /* __MWERKS__ */ |
| 62 | |
Jack Jansen | ee6eeb1 | 2000-06-02 21:28:52 +0000 | [diff] [blame] | 63 | #include <unistd.h> |
Jack Jansen | 5bdbabd | 2000-07-24 19:52:52 +0000 | [diff] [blame] | 64 | #ifdef USE_MAC_SHARED_LIBRARY |
| 65 | extern PyMac_AddLibResources(void); |
| 66 | #endif |
Jack Jansen | c76fd39 | 1995-02-02 14:27:31 +0000 | [diff] [blame] | 67 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 68 | #define STARTUP "PythonStartup" |
Jack Jansen | bac428d | 1994-12-14 13:47:30 +0000 | [diff] [blame] | 69 | |
Jack Jansen | 65c3ee0 | 2000-09-08 10:20:37 +0000 | [diff] [blame] | 70 | #define COPYRIGHT \ |
| 71 | "Type \"copyright\", \"credits\" or \"license\" for more information." |
| 72 | |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 73 | short PyMac_AppRefNum; /* RefNum of application resource fork */ |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 74 | |
Jack Jansen | 1d2f863 | 1996-08-02 15:16:16 +0000 | [diff] [blame] | 75 | /* For Py_GetArgcArgv(); set by main() */ |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 76 | static char **orig_argv; |
| 77 | static int orig_argc; |
| 78 | |
Jack Jansen | 8a38714 | 2001-02-11 01:08:04 +0000 | [diff] [blame] | 79 | /* A flag which remembers whether the user has acknowledged all the console |
| 80 | ** output (by typing something) |
| 81 | */ |
| 82 | #define STATE_UNKNOWN 0 |
| 83 | #define STATE_LASTREAD 1 |
| 84 | #define STATE_LASTWRITE 2 |
| 85 | int console_output_state = STATE_UNKNOWN; |
| 86 | |
Jack Jansen | dc86f9e | 2000-10-12 21:23:19 +0000 | [diff] [blame] | 87 | PyMac_PrefRecord PyMac_options; |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 88 | |
Jack Jansen | 998a40a | 2001-09-11 13:08:10 +0000 | [diff] [blame] | 89 | static void Py_Main(int, char **, char *); /* Forward */ |
Jack Jansen | d88296d | 2000-07-11 19:51:05 +0000 | [diff] [blame] | 90 | void PyMac_Exit(int); /* Forward */ |
Jack Jansen | 76ceece | 1996-08-19 11:18:24 +0000 | [diff] [blame] | 91 | |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 92 | /* Initialize the Mac toolbox world */ |
| 93 | |
| 94 | static void |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 95 | init_mac_world(void) |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 96 | { |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame] | 97 | #if !TARGET_API_MAC_CARBON |
Jack Jansen | ee6eeb1 | 2000-06-02 21:28:52 +0000 | [diff] [blame] | 98 | /* These aren't needed for carbon */ |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 99 | MaxApplZone(); |
| 100 | InitGraf(&qd.thePort); |
| 101 | InitFonts(); |
| 102 | InitWindows(); |
| 103 | TEInit(); |
| 104 | InitDialogs((long)0); |
| 105 | InitMenus(); |
Jack Jansen | ee6eeb1 | 2000-06-02 21:28:52 +0000 | [diff] [blame] | 106 | #endif |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 107 | InitCursor(); |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 110 | /* |
| 111 | ** PyMac_InteractiveOptions - Allow user to set options if option key is pressed |
| 112 | */ |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 113 | static void |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 114 | PyMac_InteractiveOptions(PyMac_PrefRecord *p, int *argcp, char ***argvp) |
| 115 | { |
| 116 | KeyMap rmap; |
| 117 | unsigned char *map; |
| 118 | short item, type; |
| 119 | ControlHandle handle; |
| 120 | DialogPtr dialog; |
| 121 | Rect rect; |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 122 | |
| 123 | /* |
| 124 | ** If the preferences disallows interactive options we return, |
| 125 | ** similarly of <option> isn't pressed. |
| 126 | */ |
| 127 | if (p->nointopt) return; |
| 128 | |
| 129 | GetKeys(rmap); |
| 130 | map = (unsigned char *)rmap; |
| 131 | if ( ( map[0x3a>>3] & (1<<(0x3a&7)) ) == 0 ) /* option key is 3a */ |
| 132 | return; |
| 133 | |
| 134 | dialog = GetNewDialog(OPT_DIALOG, NULL, (WindowPtr)-1); |
| 135 | if ( dialog == NULL ) { |
| 136 | printf("Option dialog not found - cannot set options\n"); |
| 137 | return; |
| 138 | } |
| 139 | SetDialogDefaultItem(dialog, OPT_OK); |
| 140 | SetDialogCancelItem(dialog, OPT_CANCEL); |
| 141 | |
| 142 | /* Set default values */ |
| 143 | #define SET_OPT_ITEM(num, var) \ |
| 144 | GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \ |
Jack Jansen | 08c3be3 | 1997-04-08 15:27:00 +0000 | [diff] [blame] | 145 | SetControlValue(handle, (short)p->var); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 146 | |
| 147 | SET_OPT_ITEM(OPT_INSPECT, inspect); |
| 148 | SET_OPT_ITEM(OPT_VERBOSE, verbose); |
Jack Jansen | ff5d8aa | 2001-09-01 22:37:54 +0000 | [diff] [blame] | 149 | /* OPT_VERBOSEVERBOSE is default off */ |
Jack Jansen | 36b983c | 1997-09-09 13:53:21 +0000 | [diff] [blame] | 150 | SET_OPT_ITEM(OPT_OPTIMIZE, optimize); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 151 | SET_OPT_ITEM(OPT_UNBUFFERED, unbuffered); |
| 152 | SET_OPT_ITEM(OPT_DEBUGGING, debugging); |
Jack Jansen | 4a5eb96 | 2000-09-22 21:50:11 +0000 | [diff] [blame] | 153 | GetDialogItem(dialog, OPT_KEEPALWAYS, &type, (Handle *)&handle, &rect); |
| 154 | SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_ALWAYS)); |
| 155 | GetDialogItem(dialog, OPT_KEEPOUTPUT, &type, (Handle *)&handle, &rect); |
| 156 | SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_OUTPUT)); |
| 157 | GetDialogItem(dialog, OPT_KEEPERROR, &type, (Handle *)&handle, &rect); |
| 158 | SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_ERROR)); |
| 159 | GetDialogItem(dialog, OPT_KEEPNEVER, &type, (Handle *)&handle, &rect); |
| 160 | SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_NEVER)); |
| 161 | /* SET_OPT_ITEM(OPT_KEEPCONSOLE, keep_console); */ |
Jack Jansen | 0c6d037 | 2000-05-05 23:11:14 +0000 | [diff] [blame] | 162 | SET_OPT_ITEM(OPT_TABWARN, tabwarn); |
Jack Jansen | 36b983c | 1997-09-09 13:53:21 +0000 | [diff] [blame] | 163 | SET_OPT_ITEM(OPT_NOSITE, nosite); |
Jack Jansen | ff5d8aa | 2001-09-01 22:37:54 +0000 | [diff] [blame] | 164 | SET_OPT_ITEM(OPT_DIVISIONWARN, divisionwarn); |
| 165 | SET_OPT_ITEM(OPT_UNIXNEWLINES, unixnewlines); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 166 | /* The rest are not settable interactively */ |
| 167 | |
| 168 | #undef SET_OPT_ITEM |
| 169 | |
| 170 | while (1) { |
| 171 | handle = NULL; |
| 172 | ModalDialog(NULL, &item); |
| 173 | if ( item == OPT_OK ) |
| 174 | break; |
| 175 | if ( item == OPT_CANCEL ) { |
Jack Jansen | 08c3be3 | 1997-04-08 15:27:00 +0000 | [diff] [blame] | 176 | DisposeDialog(dialog); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 177 | exit(0); |
| 178 | } |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame] | 179 | #if !TARGET_API_MAC_CARBON |
Jack Jansen | 36b983c | 1997-09-09 13:53:21 +0000 | [diff] [blame] | 180 | if ( item == OPT_HELP ) { |
| 181 | HMSetBalloons(!HMGetBalloons()); |
| 182 | } |
Jack Jansen | ee6eeb1 | 2000-06-02 21:28:52 +0000 | [diff] [blame] | 183 | #endif |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 184 | #if !TARGET_API_MAC_OSX |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 185 | if ( item == OPT_CMDLINE ) { |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 186 | int old_argc = *argcp; |
| 187 | int i; |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 188 | int new_argc, newer_argc; |
| 189 | char **new_argv, **newer_argv; |
| 190 | |
| 191 | new_argc = ccommand(&new_argv); |
| 192 | newer_argc = (new_argc-1) + old_argc; |
| 193 | newer_argv = malloc((newer_argc+1)*sizeof(char *)); |
| 194 | if( !newer_argv ) |
| 195 | Py_FatalError("Cannot malloc argv\n"); |
| 196 | for(i=0; i<old_argc; i++) |
| 197 | newer_argv[i] = (*argvp)[i]; |
| 198 | for(i=old_argc; i<=newer_argc; i++) /* Copy the NULL too */ |
| 199 | newer_argv[i] = new_argv[i-old_argc+1]; |
| 200 | *argvp = newer_argv; |
| 201 | *argcp = newer_argc; |
| 202 | |
| 203 | /* XXXX Is it not safe to use free() here, apparently */ |
| 204 | } |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 205 | #endif /* !TARGET_API_MAC_OSX */ |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 206 | #define OPT_ITEM(num, var) \ |
| 207 | if ( item == (num) ) { \ |
| 208 | p->var = !p->var; \ |
| 209 | GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \ |
Jack Jansen | 08c3be3 | 1997-04-08 15:27:00 +0000 | [diff] [blame] | 210 | SetControlValue(handle, (short)p->var); \ |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | OPT_ITEM(OPT_INSPECT, inspect); |
| 214 | OPT_ITEM(OPT_VERBOSE, verbose); |
Jack Jansen | ff5d8aa | 2001-09-01 22:37:54 +0000 | [diff] [blame] | 215 | if ( item == OPT_VERBOSEVERBOSE ) { |
| 216 | if ( p->verbose == 2 ) |
| 217 | p->verbose = 1; |
| 218 | else |
| 219 | p->verbose = 2; |
| 220 | GetDialogItem(dialog, OPT_VERBOSE, &type, (Handle *)&handle, &rect); |
| 221 | SetControlValue(handle, 1); |
| 222 | } |
| 223 | GetDialogItem(dialog, OPT_VERBOSEVERBOSE, &type, (Handle *)&handle, &rect); |
| 224 | SetControlValue(handle, p->verbose == 2); |
Jack Jansen | 36b983c | 1997-09-09 13:53:21 +0000 | [diff] [blame] | 225 | OPT_ITEM(OPT_OPTIMIZE, optimize); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 226 | OPT_ITEM(OPT_UNBUFFERED, unbuffered); |
| 227 | OPT_ITEM(OPT_DEBUGGING, debugging); |
Jack Jansen | 4a5eb96 | 2000-09-22 21:50:11 +0000 | [diff] [blame] | 228 | if ( item == OPT_KEEPALWAYS ) p->keep_console = POPT_KEEPCONSOLE_ALWAYS; |
| 229 | if ( item == OPT_KEEPOUTPUT ) p->keep_console = POPT_KEEPCONSOLE_OUTPUT; |
| 230 | if ( item == OPT_KEEPERROR ) p->keep_console = POPT_KEEPCONSOLE_ERROR; |
| 231 | if ( item == OPT_KEEPNEVER ) p->keep_console = POPT_KEEPCONSOLE_NEVER; |
| 232 | GetDialogItem(dialog, OPT_KEEPALWAYS, &type, (Handle *)&handle, &rect); |
| 233 | SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_ALWAYS)); |
| 234 | GetDialogItem(dialog, OPT_KEEPOUTPUT, &type, (Handle *)&handle, &rect); |
| 235 | SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_OUTPUT)); |
| 236 | GetDialogItem(dialog, OPT_KEEPERROR, &type, (Handle *)&handle, &rect); |
| 237 | SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_ERROR)); |
| 238 | GetDialogItem(dialog, OPT_KEEPNEVER, &type, (Handle *)&handle, &rect); |
| 239 | SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_NEVER)); |
Jack Jansen | 0c6d037 | 2000-05-05 23:11:14 +0000 | [diff] [blame] | 240 | OPT_ITEM(OPT_TABWARN, tabwarn); |
Jack Jansen | 36b983c | 1997-09-09 13:53:21 +0000 | [diff] [blame] | 241 | OPT_ITEM(OPT_NOSITE, nosite); |
Jack Jansen | ff5d8aa | 2001-09-01 22:37:54 +0000 | [diff] [blame] | 242 | OPT_ITEM(OPT_DIVISIONWARN, divisionwarn); |
| 243 | OPT_ITEM(OPT_UNIXNEWLINES, unixnewlines); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 244 | |
| 245 | #undef OPT_ITEM |
| 246 | } |
Jack Jansen | 08c3be3 | 1997-04-08 15:27:00 +0000 | [diff] [blame] | 247 | DisposeDialog(dialog); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /* |
| 251 | ** Initialization code, shared by interpreter and applets |
| 252 | */ |
| 253 | static void |
Jack Jansen | 52ac037 | 1997-01-15 15:49:08 +0000 | [diff] [blame] | 254 | init_common(int *argcp, char ***argvp, int embedded) |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 255 | { |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 256 | /* Remember resource fork refnum, for later */ |
| 257 | PyMac_AppRefNum = CurResFile(); |
| 258 | |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 259 | /* Initialize toolboxes */ |
| 260 | init_mac_world(); |
| 261 | |
| 262 | #ifdef USE_MAC_SHARED_LIBRARY |
| 263 | /* Add the shared library to the stack of resource files */ |
Jack Jansen | 87c485c | 1998-07-31 09:38:01 +0000 | [diff] [blame] | 264 | (void)PyMac_init_process_location(); |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 265 | PyMac_AddLibResources(); |
| 266 | #endif |
| 267 | |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 268 | #if defined(USE_GUSI) |
| 269 | atexit(PyMac_StopGUSISpin); |
| 270 | #endif |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 271 | |
| 272 | #ifdef USE_SIOUX |
| 273 | /* Set various SIOUX flags. Some are changed later based on options */ |
| 274 | SIOUXSettings.asktosaveonclose = 0; |
| 275 | SIOUXSettings.showstatusline = 0; |
| 276 | SIOUXSettings.tabspaces = 4; |
| 277 | #endif |
| 278 | |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 279 | /* Get options from preference file (or from applet resource fork) */ |
Jack Jansen | dc86f9e | 2000-10-12 21:23:19 +0000 | [diff] [blame] | 280 | PyMac_options.keep_console = POPT_KEEPCONSOLE_OUTPUT; /* default-default */ |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 281 | PyMac_options.unixnewlines = 1; |
| 282 | #if !TARGET_API_MAC_OSX |
Jack Jansen | dc86f9e | 2000-10-12 21:23:19 +0000 | [diff] [blame] | 283 | PyMac_PreferenceOptions(&PyMac_options); |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 284 | #endif |
| 285 | |
Jack Jansen | 52ac037 | 1997-01-15 15:49:08 +0000 | [diff] [blame] | 286 | if ( embedded ) { |
| 287 | static char *emb_argv[] = {"embedded-python", 0}; |
| 288 | |
| 289 | *argcp = 1; |
| 290 | *argvp = emb_argv; |
| 291 | } else { |
Jack Jansen | 4b51785 | 2002-01-02 22:53:38 +0000 | [diff] [blame] | 292 | /* Create argc/argv. Do it before we go into the options event loop. |
| 293 | ** In MachoPython we skip this step if we already have plausible |
| 294 | ** command line arguments. |
| 295 | */ |
| 296 | #if TARGET_API_MAC_OSX |
| 297 | if (*argcp == 2 && strncmp((*argvp)[1], "-psn_", 5) == 0) |
| 298 | #endif |
| 299 | *argcp = PyMac_GetArgv(argvp, PyMac_options.noargs); |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 300 | #if !TARGET_API_MAC_OSX |
Jack Jansen | c00df0b | 2001-01-16 15:54:58 +0000 | [diff] [blame] | 301 | #ifndef NO_ARGV0_CHDIR |
Jack Jansen | 660bb1d | 2000-07-18 09:40:39 +0000 | [diff] [blame] | 302 | if (*argcp >= 1 && (*argvp)[0] && (*argvp)[0][0]) { |
| 303 | /* Workaround for MacOS X, which currently (DP4) doesn't set |
| 304 | ** the working folder correctly |
| 305 | */ |
| 306 | char app_wd[256], *p; |
| 307 | |
| 308 | strncpy(app_wd, (*argvp)[0], 256); |
Jack Jansen | 660bb1d | 2000-07-18 09:40:39 +0000 | [diff] [blame] | 309 | p = strrchr(app_wd, ':'); |
| 310 | if ( p ) *p = 0; |
Jack Jansen | 660bb1d | 2000-07-18 09:40:39 +0000 | [diff] [blame] | 311 | chdir(app_wd); |
| 312 | } |
| 313 | #endif |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 314 | #endif |
Jack Jansen | 52ac037 | 1997-01-15 15:49:08 +0000 | [diff] [blame] | 315 | /* Do interactive option setting, if allowed and <option> depressed */ |
Jack Jansen | dc86f9e | 2000-10-12 21:23:19 +0000 | [diff] [blame] | 316 | PyMac_InteractiveOptions(&PyMac_options, argcp, argvp); |
Jack Jansen | 52ac037 | 1997-01-15 15:49:08 +0000 | [diff] [blame] | 317 | } |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 318 | |
| 319 | /* Copy selected options to where the machine-independent stuff wants it */ |
Jack Jansen | dc86f9e | 2000-10-12 21:23:19 +0000 | [diff] [blame] | 320 | Py_VerboseFlag = PyMac_options.verbose; |
| 321 | /* Py_SuppressPrintingFlag = PyMac_options.suppress_print; */ |
| 322 | Py_OptimizeFlag = PyMac_options.optimize; |
| 323 | Py_DebugFlag = PyMac_options.debugging; |
| 324 | Py_NoSiteFlag = PyMac_options.nosite; |
| 325 | Py_TabcheckFlag = PyMac_options.tabwarn; |
Jack Jansen | ff5d8aa | 2001-09-01 22:37:54 +0000 | [diff] [blame] | 326 | Py_DivisionWarningFlag = PyMac_options.divisionwarn; |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 327 | #if !TARGET_API_MAC_OSX |
Jack Jansen | dc86f9e | 2000-10-12 21:23:19 +0000 | [diff] [blame] | 328 | if ( PyMac_options.noargs ) { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 329 | /* don't process events at all without the scripts permission */ |
| 330 | PyMacSchedParams scp; |
| 331 | |
| 332 | PyMac_GetSchedParams(&scp); |
| 333 | scp.process_events = 0; |
| 334 | /* Should we disable command-dot as well? */ |
| 335 | PyMac_SetSchedParams(&scp); |
| 336 | } |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 337 | #endif /* !TARGET_API_MAC_OSX */ |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 338 | |
| 339 | /* Set buffering */ |
Jack Jansen | dc86f9e | 2000-10-12 21:23:19 +0000 | [diff] [blame] | 340 | if (PyMac_options.unbuffered) { |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 341 | #ifndef MPW |
| 342 | setbuf(stdout, (char *)NULL); |
| 343 | setbuf(stderr, (char *)NULL); |
| 344 | #else |
| 345 | /* On MPW (3.2) unbuffered seems to hang */ |
| 346 | setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ); |
| 347 | setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ); |
| 348 | #endif |
| 349 | } |
Jack Jansen | 8c69321 | 1997-01-07 16:19:42 +0000 | [diff] [blame] | 350 | #if __profile__ == 1 |
| 351 | /* collectSummary or collectDetailed, timebase, #routines, max stack depth */ |
Jack Jansen | e742487 | 1999-09-30 11:20:11 +0000 | [diff] [blame] | 352 | ProfilerInit(collectSummary, bestTimeBase, 8000, 250); |
Jack Jansen | 8c69321 | 1997-01-07 16:19:42 +0000 | [diff] [blame] | 353 | #endif |
Jack Jansen | 7330b39 | 1997-08-08 14:56:41 +0000 | [diff] [blame] | 354 | |
| 355 | /* Tell the rest of python about our argc/argv */ |
| 356 | orig_argc = *argcp; /* For Py_GetArgcArgv() */ |
| 357 | orig_argv = *argvp; |
| 358 | Py_SetProgramName((*argvp)[0]); |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 361 | /* |
| 362 | ** Inspection mode after script/applet termination |
| 363 | */ |
| 364 | static int |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 365 | run_inspect(void) |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 366 | { |
| 367 | int sts = 0; |
| 368 | |
Jack Jansen | dc86f9e | 2000-10-12 21:23:19 +0000 | [diff] [blame] | 369 | if (PyMac_options.inspect && isatty((int)fileno(stdin))) |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 370 | sts = PyRun_AnyFile(stdin, "<stdin>") != 0; |
| 371 | return sts; |
| 372 | } |
Jack Jansen | 6438e1d | 2001-09-11 11:29:31 +0000 | [diff] [blame] | 373 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 374 | #ifdef USE_MAC_APPLET_SUPPORT |
| 375 | /* Applet support */ |
| 376 | |
| 377 | /* Run a compiled Python Python script from 'PYC ' resource __main__ */ |
| 378 | static int |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 379 | run_main_resource(void) |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 380 | { |
| 381 | Handle h; |
| 382 | long size; |
| 383 | PyObject *code; |
| 384 | PyObject *result; |
| 385 | |
| 386 | h = GetNamedResource('PYC ', "\p__main__"); |
| 387 | if (h == NULL) { |
| 388 | Alert(NOPYC_ALERT, NULL); |
| 389 | return 1; |
| 390 | } |
| 391 | size = GetResourceSizeOnDisk(h); |
| 392 | HLock(h); |
| 393 | code = PyMarshal_ReadObjectFromString(*h + 8, (int)(size - 8)); |
| 394 | HUnlock(h); |
| 395 | ReleaseResource(h); |
| 396 | if (code == NULL) { |
| 397 | PyErr_Print(); |
| 398 | return 1; |
| 399 | } |
| 400 | result = PyImport_ExecCodeModule("__main__", code); |
| 401 | Py_DECREF(code); |
| 402 | if (result == NULL) { |
| 403 | PyErr_Print(); |
| 404 | return 1; |
| 405 | } |
| 406 | Py_DECREF(result); |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | /* Initialization sequence for applets */ |
| 411 | void |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 412 | PyMac_InitApplet(void) |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 413 | { |
Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +0000 | [diff] [blame] | 414 | int argc; |
| 415 | char **argv; |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 416 | int err; |
| 417 | |
Jack Jansen | 52ac037 | 1997-01-15 15:49:08 +0000 | [diff] [blame] | 418 | init_common(&argc, &argv, 0); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 419 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 420 | Py_Initialize(); |
| 421 | PySys_SetArgv(argc, argv); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 422 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 423 | err = run_main_resource(); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 424 | |
| 425 | err = (run_inspect() || err); |
| 426 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 427 | fflush(stderr); |
| 428 | fflush(stdout); |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 429 | PyMac_Exit(err); |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 430 | /* XXX Should we bother to Py_Exit(sts)? */ |
| 431 | } |
| 432 | |
Jack Jansen | 52ac037 | 1997-01-15 15:49:08 +0000 | [diff] [blame] | 433 | /* |
| 434 | ** Hook for embedding python. |
| 435 | */ |
| 436 | void |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 437 | PyMac_Initialize(void) |
Jack Jansen | 52ac037 | 1997-01-15 15:49:08 +0000 | [diff] [blame] | 438 | { |
| 439 | int argc; |
| 440 | char **argv; |
| 441 | |
| 442 | init_common(&argc, &argv, 1); |
| 443 | Py_Initialize(); |
| 444 | PySys_SetArgv(argc, argv); |
| 445 | } |
| 446 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 447 | #endif /* USE_MAC_APPLET_SUPPORT */ |
| 448 | |
Jack Jansen | 5ded1bf | 2001-10-30 22:48:36 +0000 | [diff] [blame] | 449 | #if TARGET_API_MAC_OSX /* Really: TARGET_API_MAC_CARBON */ |
Jack Jansen | 998a40a | 2001-09-11 13:08:10 +0000 | [diff] [blame] | 450 | |
| 451 | static int |
Jack Jansen | 3cef721 | 2002-03-29 14:43:50 +0000 | [diff] [blame] | 452 | locateResourcePy(CFStringRef resourceType, char *resourceName, char *resourceURLCStr, int length) |
| 453 | { |
Jack Jansen | 998a40a | 2001-09-11 13:08:10 +0000 | [diff] [blame] | 454 | CFBundleRef mainBundle = NULL; |
| 455 | CFURLRef URL, absoluteURL; |
| 456 | CFStringRef filenameString, filepathString, rsrcString; |
| 457 | CFIndex size, i; |
Jack Jansen | 5ded1bf | 2001-10-30 22:48:36 +0000 | [diff] [blame] | 458 | CFArrayRef arrayRef = NULL; |
| 459 | int success = 0; |
| 460 | |
| 461 | #if TARGET_API_MAC_OSX |
| 462 | CFURLPathStyle thePathStyle = kCFURLPOSIXPathStyle; |
| 463 | #else |
| 464 | CFURLPathStyle thePathStyle = kCFURLHFSPathStyle; |
| 465 | #endif |
Jack Jansen | 998a40a | 2001-09-11 13:08:10 +0000 | [diff] [blame] | 466 | |
| 467 | /* Get a reference to our main bundle */ |
| 468 | mainBundle = CFBundleGetMainBundle(); |
| 469 | |
Jack Jansen | 5ded1bf | 2001-10-30 22:48:36 +0000 | [diff] [blame] | 470 | /* If we are running inside a bundle, look through it. Otherwise, do nothing. */ |
| 471 | if (mainBundle) { |
| 472 | /* Create a CFString with the resource name in it */ |
| 473 | rsrcString = CFStringCreateWithCString(0, resourceName, kCFStringEncodingMacRoman); |
Jack Jansen | 998a40a | 2001-09-11 13:08:10 +0000 | [diff] [blame] | 474 | |
Jack Jansen | 5ded1bf | 2001-10-30 22:48:36 +0000 | [diff] [blame] | 475 | /* Look for py files in the main bundle by type */ |
| 476 | arrayRef = CFBundleCopyResourceURLsOfType( mainBundle, |
Jack Jansen | 3cef721 | 2002-03-29 14:43:50 +0000 | [diff] [blame] | 477 | resourceType, |
Jack Jansen | 5ded1bf | 2001-10-30 22:48:36 +0000 | [diff] [blame] | 478 | NULL ); |
Jack Jansen | 998a40a | 2001-09-11 13:08:10 +0000 | [diff] [blame] | 479 | |
Jack Jansen | 5ded1bf | 2001-10-30 22:48:36 +0000 | [diff] [blame] | 480 | /* See if there are any filename matches */ |
| 481 | size = CFArrayGetCount(arrayRef); |
| 482 | for (i = 0; i < size; i++) { |
| 483 | URL = CFArrayGetValueAtIndex(arrayRef, i); |
| 484 | filenameString = CFURLCopyLastPathComponent(URL); |
| 485 | if (CFStringCompare(filenameString, rsrcString, 0) == kCFCompareEqualTo) { |
| 486 | /* We found a match, get the file's full path */ |
| 487 | absoluteURL = CFURLCopyAbsoluteURL(URL); |
| 488 | filepathString = CFURLCopyFileSystemPath(absoluteURL, thePathStyle); |
| 489 | CFRelease(absoluteURL); |
Jack Jansen | 998a40a | 2001-09-11 13:08:10 +0000 | [diff] [blame] | 490 | |
Jack Jansen | 5ded1bf | 2001-10-30 22:48:36 +0000 | [diff] [blame] | 491 | /* Copy the full path into the caller's character buffer */ |
| 492 | success = CFStringGetCString(filepathString, resourceURLCStr, length, |
| 493 | kCFStringEncodingMacRoman); |
Jack Jansen | 998a40a | 2001-09-11 13:08:10 +0000 | [diff] [blame] | 494 | |
Jack Jansen | 5ded1bf | 2001-10-30 22:48:36 +0000 | [diff] [blame] | 495 | CFRelease(filepathString); |
| 496 | } |
| 497 | CFRelease(filenameString); |
| 498 | } |
| 499 | CFRelease(arrayRef); |
| 500 | CFRelease(rsrcString); |
| 501 | } |
Jack Jansen | 998a40a | 2001-09-11 13:08:10 +0000 | [diff] [blame] | 502 | return success; |
| 503 | } |
| 504 | |
Jack Jansen | 5ded1bf | 2001-10-30 22:48:36 +0000 | [diff] [blame] | 505 | #endif /* TARGET_API_MAC_CARBON */ |
| 506 | |
| 507 | #if TARGET_API_MAC_OSX |
| 508 | |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 509 | int |
| 510 | main(int argc, char **argv) |
| 511 | { |
Jack Jansen | 998a40a | 2001-09-11 13:08:10 +0000 | [diff] [blame] | 512 | static char scriptpath[1024]; |
| 513 | char *script = NULL; |
| 514 | |
| 515 | /* First we see whether we have __rawmain__.py and run that if it |
| 516 | ** is there |
| 517 | */ |
Jack Jansen | 3cef721 | 2002-03-29 14:43:50 +0000 | [diff] [blame] | 518 | if (locateResourcePy(CFSTR("py"), "__rawmain__.py", scriptpath, 1024)) { |
Jack Jansen | 998a40a | 2001-09-11 13:08:10 +0000 | [diff] [blame] | 519 | /* If we have a raw main we don't do AppleEvent processing. |
| 520 | ** Notice that this also means we keep the -psn.... argv[1] |
| 521 | ** value intact. Not sure whether that is important to someone, |
| 522 | ** but you never know... |
| 523 | */ |
| 524 | script = scriptpath; |
Jack Jansen | 3cef721 | 2002-03-29 14:43:50 +0000 | [diff] [blame] | 525 | } else if (locateResourcePy(CFSTR("pyc"), "__rawmain__.pyc", scriptpath, 1024)) { |
| 526 | script = scriptpath; |
Jack Jansen | 998a40a | 2001-09-11 13:08:10 +0000 | [diff] [blame] | 527 | } else { |
| 528 | /* Otherwise we look for __main__.py. Whether that is |
| 529 | ** found or not we also process AppleEvent arguments. |
| 530 | */ |
Jack Jansen | 3cef721 | 2002-03-29 14:43:50 +0000 | [diff] [blame] | 531 | if (locateResourcePy(CFSTR("py"), "__main__.py", scriptpath, 1024)) |
| 532 | script = scriptpath; |
| 533 | else if (locateResourcePy(CFSTR("pyc"), "__main__.pyc", scriptpath, 1024)) |
Jack Jansen | 998a40a | 2001-09-11 13:08:10 +0000 | [diff] [blame] | 534 | script = scriptpath; |
| 535 | |
Jack Jansen | 998a40a | 2001-09-11 13:08:10 +0000 | [diff] [blame] | 536 | init_common(&argc, &argv, 0); |
| 537 | |
Jack Jansen | 998a40a | 2001-09-11 13:08:10 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | Py_Main(argc, argv, script); |
| 541 | return 0; |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | #else |
| 545 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 546 | /* For normal application */ |
| 547 | void |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 548 | PyMac_InitApplication(void) |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 549 | { |
| 550 | int argc; |
| 551 | char **argv; |
Jack Jansen | a7f4ded | 2002-05-22 15:02:08 +0000 | [diff] [blame^] | 552 | OSType filetype; |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 553 | |
Jack Jansen | 5ded1bf | 2001-10-30 22:48:36 +0000 | [diff] [blame] | 554 | static char scriptpath[1024]; |
| 555 | char *script = NULL; |
| 556 | |
Jack Jansen | 52ac037 | 1997-01-15 15:49:08 +0000 | [diff] [blame] | 557 | init_common(&argc, &argv, 0); |
Jack Jansen | 5ded1bf | 2001-10-30 22:48:36 +0000 | [diff] [blame] | 558 | |
| 559 | #if TARGET_API_MAC_OSX /* Really: TARGET_API_MAC_CARBON */ |
| 560 | /* If we are running inside of a bundle, and a __main__.py is available, use it */ |
| 561 | if (locateResourcePy("__main__.py", scriptpath, 1024)) |
| 562 | script = scriptpath; |
| 563 | #endif |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 564 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 565 | if ( argc > 1 ) { |
| 566 | /* We're running a script. Attempt to change current directory */ |
| 567 | char curwd[256], *endp; |
| 568 | |
| 569 | strcpy(curwd, argv[1]); |
| 570 | endp = strrchr(curwd, ':'); |
| 571 | if ( endp && endp > curwd ) { |
| 572 | *endp = '\0'; |
| 573 | |
| 574 | chdir(curwd); |
| 575 | } |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 576 | /* Check that the first argument is a text file */ |
Jack Jansen | a7f4ded | 2002-05-22 15:02:08 +0000 | [diff] [blame^] | 577 | filetype = PyMac_getfiletype(argv[1]); |
| 578 | if ( filetype != 'TEXT' && filetype != 0 ) { |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 579 | Alert(NOTASCRIPT_ID, NULL); |
| 580 | exit(0); |
| 581 | } |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 582 | } |
Jack Jansen | 5ded1bf | 2001-10-30 22:48:36 +0000 | [diff] [blame] | 583 | Py_Main(argc, argv, script); |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 584 | } |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 585 | #endif /* TARGET_API_MAC_OSX */ |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 586 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 587 | /* Main program */ |
| 588 | |
Jack Jansen | 76ceece | 1996-08-19 11:18:24 +0000 | [diff] [blame] | 589 | static void |
Jack Jansen | 998a40a | 2001-09-11 13:08:10 +0000 | [diff] [blame] | 590 | Py_Main(int argc, char **argv, char *filename) |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 591 | { |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 592 | int sts; |
| 593 | char *command = NULL; |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 594 | FILE *fp = stdin; |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 595 | |
Jack Jansen | 998a40a | 2001-09-11 13:08:10 +0000 | [diff] [blame] | 596 | if ( filename ) { |
| 597 | /* Someone else has found our "script" already */ |
| 598 | argv[0] = filename; |
| 599 | } else { |
| 600 | filename = argv[1]; |
| 601 | argv++; |
| 602 | argc--; |
| 603 | } |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 604 | |
| 605 | if (Py_VerboseFlag || |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 606 | (command == NULL && filename == NULL && isatty((int)fileno(fp)))) |
Jack Jansen | 0035fb2 | 2002-03-29 14:27:06 +0000 | [diff] [blame] | 607 | fprintf(stderr, "%s %s on %s\n%s\n", |
| 608 | #if !TARGET_API_MAC_OSX |
| 609 | "Python", |
| 610 | #else |
| 611 | "Pythonw", |
| 612 | #endif |
Jack Jansen | 65c3ee0 | 2000-09-08 10:20:37 +0000 | [diff] [blame] | 613 | Py_GetVersion(), Py_GetPlatform(), COPYRIGHT); |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 614 | |
| 615 | if (filename != NULL) { |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 616 | if ((fp = fopen(filename, "r" PY_STDIOTEXTMODE)) == NULL) { |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 617 | fprintf(stderr, "%s: can't open file '%s'\n", |
| 618 | argv[0], filename); |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 619 | PyMac_Exit(2); |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 623 | #if !TARGET_API_MAC_OSX |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 624 | /* We initialize the menubar here, hoping SIOUX is initialized by now */ |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 625 | PyMac_InitMenuBar(); |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 626 | #endif |
| 627 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 628 | Py_Initialize(); |
| 629 | |
unknown | d1054ef | 2001-07-04 22:37:19 +0000 | [diff] [blame] | 630 | PyUnicode_SetDefaultEncoding(PyMac_getscript()); |
| 631 | |
Jack Jansen | 998a40a | 2001-09-11 13:08:10 +0000 | [diff] [blame] | 632 | PySys_SetArgv(argc, argv); |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 633 | |
| 634 | if (filename == NULL && isatty((int)fileno(fp))) { |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 635 | FILE *fp = fopen(STARTUP, "r" PY_STDIOTEXTMODE); |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 636 | if (fp != NULL) { |
| 637 | (void) PyRun_SimpleFile(fp, STARTUP); |
| 638 | PyErr_Clear(); |
| 639 | fclose(fp); |
| 640 | } |
| 641 | } |
| 642 | sts = PyRun_AnyFile( |
| 643 | fp, filename == NULL ? "<stdin>" : filename) != 0; |
| 644 | if (filename != NULL) |
| 645 | fclose(fp); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 646 | |
| 647 | if ( filename != NULL || command != NULL ) |
| 648 | sts = (run_inspect() || sts); |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 649 | |
| 650 | Py_Exit(sts); |
| 651 | /*NOTREACHED*/ |
| 652 | } |
| 653 | |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 654 | #if !TARGET_API_MAC_OSX |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 655 | /* |
Jack Jansen | 8413b47 | 2000-10-19 22:02:16 +0000 | [diff] [blame] | 656 | ** Reset the "unseen output" flag |
| 657 | */ |
| 658 | void |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 659 | PyMac_OutputSeen(void) |
Jack Jansen | 8413b47 | 2000-10-19 22:02:16 +0000 | [diff] [blame] | 660 | { |
Jack Jansen | 657ba27 | 2001-02-17 22:02:07 +0000 | [diff] [blame] | 661 | if ( console_output_state == STATE_UNKNOWN ) |
| 662 | PyMac_InitMenuBar(); |
Jack Jansen | 8a38714 | 2001-02-11 01:08:04 +0000 | [diff] [blame] | 663 | console_output_state = STATE_LASTREAD; |
Jack Jansen | 8413b47 | 2000-10-19 22:02:16 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /* |
Jack Jansen | 8a38714 | 2001-02-11 01:08:04 +0000 | [diff] [blame] | 667 | ** Set the "unseen output" flag |
| 668 | */ |
| 669 | void |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 670 | PyMac_OutputNotSeen(void) |
Jack Jansen | 8a38714 | 2001-02-11 01:08:04 +0000 | [diff] [blame] | 671 | { |
Jack Jansen | 657ba27 | 2001-02-17 22:02:07 +0000 | [diff] [blame] | 672 | if ( console_output_state == STATE_UNKNOWN ) |
| 673 | PyMac_InitMenuBar(); |
Jack Jansen | 8a38714 | 2001-02-11 01:08:04 +0000 | [diff] [blame] | 674 | console_output_state = STATE_LASTWRITE; |
| 675 | } |
Jack Jansen | 15f1c08 | 2001-04-25 22:07:27 +0000 | [diff] [blame] | 676 | |
| 677 | /* |
| 678 | ** Override abort() - The default one is not what we want. |
| 679 | */ |
| 680 | void |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 681 | abort(void) |
Jack Jansen | 15f1c08 | 2001-04-25 22:07:27 +0000 | [diff] [blame] | 682 | { |
| 683 | console_output_state = STATE_LASTWRITE; |
| 684 | PyMac_Exit(1); |
| 685 | } |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 686 | #endif /* !TARGET_API_MAC_OSX */ |
Jack Jansen | 8a38714 | 2001-02-11 01:08:04 +0000 | [diff] [blame] | 687 | |
| 688 | /* |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 689 | ** Terminate application |
| 690 | */ |
Jack Jansen | 76ceece | 1996-08-19 11:18:24 +0000 | [diff] [blame] | 691 | void |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 692 | PyMac_Exit(int status) |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 693 | { |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 694 | #ifdef USE_SIOUX |
Jack Jansen | 4a5eb96 | 2000-09-22 21:50:11 +0000 | [diff] [blame] | 695 | int keep = 0; |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 696 | #endif |
Jack Jansen | 8c69321 | 1997-01-07 16:19:42 +0000 | [diff] [blame] | 697 | |
| 698 | #if __profile__ == 1 |
| 699 | ProfilerDump("\pPython Profiler Results"); |
| 700 | ProfilerTerm(); |
| 701 | #endif |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 702 | |
Jack Jansen | 1e8557a | 1995-11-10 14:51:26 +0000 | [diff] [blame] | 703 | #ifdef USE_SIOUX |
Jack Jansen | dc86f9e | 2000-10-12 21:23:19 +0000 | [diff] [blame] | 704 | switch (PyMac_options.keep_console) { |
Jack Jansen | 4a5eb96 | 2000-09-22 21:50:11 +0000 | [diff] [blame] | 705 | case POPT_KEEPCONSOLE_NEVER: |
| 706 | keep = 0; |
| 707 | break; |
| 708 | case POPT_KEEPCONSOLE_OUTPUT: |
Jack Jansen | 8a38714 | 2001-02-11 01:08:04 +0000 | [diff] [blame] | 709 | if (console_output_state == STATE_LASTWRITE || |
| 710 | console_output_state == STATE_UNKNOWN ) |
Jack Jansen | 4a5eb96 | 2000-09-22 21:50:11 +0000 | [diff] [blame] | 711 | keep = 1; |
| 712 | else |
| 713 | keep = 0; |
| 714 | break; |
| 715 | case POPT_KEEPCONSOLE_ERROR: |
| 716 | keep = (status != 0); |
| 717 | break; |
| 718 | default: |
| 719 | keep = 1; |
| 720 | } |
Jack Jansen | 1e8557a | 1995-11-10 14:51:26 +0000 | [diff] [blame] | 721 | if (keep) { |
| 722 | SIOUXSettings.standalone = 1; |
| 723 | SIOUXSettings.autocloseonquit = 0; |
Jack Jansen | 415571c | 1996-03-25 15:46:03 +0000 | [diff] [blame] | 724 | SIOUXSetTitle("\p\307terminated\310"); |
Jack Jansen | 15f1c08 | 2001-04-25 22:07:27 +0000 | [diff] [blame] | 725 | PyMac_RaiseConsoleWindow(); |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 726 | PyMac_RestoreMenuBar(); |
Jack Jansen | e44545f | 1997-05-07 15:48:54 +0000 | [diff] [blame] | 727 | #ifdef USE_MSL |
| 728 | /* |
| 729 | ** Temporary workaround: autocloseonquit clearing does not |
| 730 | ** currently work for the MSL/GUSI combo. |
| 731 | */ |
| 732 | while(getchar() > 0); |
| 733 | #endif |
Jack Jansen | 1e8557a | 1995-11-10 14:51:26 +0000 | [diff] [blame] | 734 | } |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 735 | else |
| 736 | SIOUXSettings.autocloseonquit = 1; |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 737 | #endif /* USE_SIOUX */ |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 738 | |
| 739 | exit(status); |
| 740 | } |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 741 | |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 742 | #if !TARGET_API_MAC_OSX |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 743 | /* Make the *original* argc/argv available to other modules. |
| 744 | This is rare, but it is needed by the secureware extension. */ |
| 745 | |
| 746 | void |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 747 | Py_GetArgcArgv(int *argc,char ***argv) |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 748 | { |
| 749 | *argc = orig_argc; |
| 750 | *argv = orig_argv; |
Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +0000 | [diff] [blame] | 751 | } |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 752 | #endif |
Jack Jansen | 1d2f863 | 1996-08-02 15:16:16 +0000 | [diff] [blame] | 753 | |
| 754 | /* More cruft that shouldn't really be here, used in sysmodule.c */ |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 755 | #if !TARGET_API_MAC_OSX |
| 756 | /* Return the program name -- some code out there needs this. */ |
| 757 | char * |
| 758 | Py_GetProgramFullPath(void) |
| 759 | { |
| 760 | return orig_argv[0]; |
| 761 | } |
Jack Jansen | 1d2f863 | 1996-08-02 15:16:16 +0000 | [diff] [blame] | 762 | |
| 763 | char * |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 764 | Py_GetPrefix(void) |
Jack Jansen | 1d2f863 | 1996-08-02 15:16:16 +0000 | [diff] [blame] | 765 | { |
Jack Jansen | ac62569 | 1997-09-08 13:22:22 +0000 | [diff] [blame] | 766 | return PyMac_GetPythonDir(); |
Jack Jansen | 1d2f863 | 1996-08-02 15:16:16 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | char * |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 770 | Py_GetExecPrefix(void) |
Jack Jansen | 1d2f863 | 1996-08-02 15:16:16 +0000 | [diff] [blame] | 771 | { |
Jack Jansen | ac62569 | 1997-09-08 13:22:22 +0000 | [diff] [blame] | 772 | return PyMac_GetPythonDir(); |
Jack Jansen | 1d2f863 | 1996-08-02 15:16:16 +0000 | [diff] [blame] | 773 | } |
Jack Jansen | 8a38714 | 2001-02-11 01:08:04 +0000 | [diff] [blame] | 774 | |
| 775 | int |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 776 | PyMac_GetDelayConsoleFlag(void) |
Jack Jansen | 8a38714 | 2001-02-11 01:08:04 +0000 | [diff] [blame] | 777 | { |
| 778 | return (int)PyMac_options.delayconsole; |
Jack Jansen | ff5d8aa | 2001-09-01 22:37:54 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | #ifndef WITHOUT_UNIX_NEWLINES |
| 782 | /* |
| 783 | ** Experimental feature (for 2.2a2): optionally allow unix newlines |
| 784 | ** as well as Mac newlines on input. We replace a lowlevel |
| 785 | ** MSL routine to accomplish this. |
| 786 | */ |
| 787 | void |
| 788 | __convert_to_newlines(unsigned char * buf, size_t * n_ptr) |
| 789 | { |
| 790 | unsigned char *p; |
| 791 | size_t n = *n_ptr; |
| 792 | |
| 793 | for(p=buf; n > 0; p++, n--) |
| 794 | if ( *p == '\r' ) *p = '\n'; |
| 795 | else if ( *p == '\n' && !PyMac_options.unixnewlines ) |
| 796 | *p = '\r'; |
| 797 | } |
| 798 | #endif /* WITHOUT_UNIX_NEWLINES */ |
Jack Jansen | dff7770 | 2001-09-05 22:07:52 +0000 | [diff] [blame] | 799 | #endif /* !TARGET_API_MAC_OSX */ |
Jack Jansen | ff5d8aa | 2001-09-01 22:37:54 +0000 | [diff] [blame] | 800 | |