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 | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 33 | #include <Memory.h> |
| 34 | #include <Resources.h> |
Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +0000 | [diff] [blame] | 35 | #include <stdio.h> |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 36 | #include <Events.h> |
| 37 | #include <Windows.h> |
Jack Jansen | 2429c72 | 1996-03-07 15:17:11 +0000 | [diff] [blame] | 38 | #include <Fonts.h> |
Jack Jansen | 36b983c | 1997-09-09 13:53:21 +0000 | [diff] [blame] | 39 | #include <Balloons.h> |
Jack Jansen | 8f5725a | 1999-12-07 23:08:10 +0000 | [diff] [blame] | 40 | #ifdef USE_APPEARANCE |
| 41 | #include <Gestalt.h> |
| 42 | #include <Appearance.h> |
| 43 | #endif /* USE_APPEARANCE */ |
Jack Jansen | c76fd39 | 1995-02-02 14:27:31 +0000 | [diff] [blame] | 44 | #ifdef __MWERKS__ |
| 45 | #include <SIOUX.h> |
Jack Jansen | 1e8557a | 1995-11-10 14:51:26 +0000 | [diff] [blame] | 46 | #define USE_SIOUX |
Jack Jansen | 8c69321 | 1997-01-07 16:19:42 +0000 | [diff] [blame] | 47 | #if __profile__ == 1 |
| 48 | #include <profiler.h> |
| 49 | #endif |
Jack Jansen | c76fd39 | 1995-02-02 14:27:31 +0000 | [diff] [blame] | 50 | #endif |
Jack Jansen | ee6eeb1 | 2000-06-02 21:28:52 +0000 | [diff] [blame] | 51 | #include <unistd.h> |
Jack Jansen | c76fd39 | 1995-02-02 14:27:31 +0000 | [diff] [blame] | 52 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 53 | #define STARTUP "PythonStartup" |
Jack Jansen | bac428d | 1994-12-14 13:47:30 +0000 | [diff] [blame] | 54 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 55 | extern int Py_DebugFlag; /* For parser.c, declared in pythonrun.c */ |
| 56 | extern int Py_VerboseFlag; /* For import.c, declared in pythonrun.c */ |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 57 | short PyMac_AppRefNum; /* RefNum of application resource fork */ |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 58 | |
Jack Jansen | 1d2f863 | 1996-08-02 15:16:16 +0000 | [diff] [blame] | 59 | /* For Py_GetArgcArgv(); set by main() */ |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 60 | static char **orig_argv; |
| 61 | static int orig_argc; |
| 62 | |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 63 | PyMac_PrefRecord options; |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 64 | |
Jack Jansen | d88296d | 2000-07-11 19:51:05 +0000 | [diff] [blame^] | 65 | static void Py_Main(int, char **); /* Forward */ |
| 66 | void PyMac_Exit(int); /* Forward */ |
Jack Jansen | 76ceece | 1996-08-19 11:18:24 +0000 | [diff] [blame] | 67 | |
Jack Jansen | 8f5725a | 1999-12-07 23:08:10 +0000 | [diff] [blame] | 68 | static void init_appearance() |
| 69 | { |
| 70 | #ifdef USE_APPEARANCE |
| 71 | OSErr err; |
| 72 | SInt32 response; |
| 73 | |
| 74 | err = Gestalt(gestaltAppearanceAttr,&response); |
| 75 | if ( err ) goto no_appearance; |
| 76 | if ( !(response&(1<<gestaltAppearanceExists)) ) goto no_appearance; |
| 77 | /* XXXX Should we check the version? Compat-mode? */ |
| 78 | PyMac_AppearanceCompliant = 1; |
| 79 | no_appearance: |
| 80 | return; |
| 81 | #endif /* USE_APPEARANCE */ |
| 82 | } |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 83 | /* Initialize the Mac toolbox world */ |
| 84 | |
| 85 | static void |
| 86 | init_mac_world() |
| 87 | { |
Jack Jansen | ee6eeb1 | 2000-06-02 21:28:52 +0000 | [diff] [blame] | 88 | #ifndef TARGET_API_MAC_CARBON |
| 89 | /* These aren't needed for carbon */ |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 90 | MaxApplZone(); |
| 91 | InitGraf(&qd.thePort); |
| 92 | InitFonts(); |
| 93 | InitWindows(); |
| 94 | TEInit(); |
| 95 | InitDialogs((long)0); |
| 96 | InitMenus(); |
Jack Jansen | ee6eeb1 | 2000-06-02 21:28:52 +0000 | [diff] [blame] | 97 | #endif |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 98 | InitCursor(); |
Jack Jansen | 8f5725a | 1999-12-07 23:08:10 +0000 | [diff] [blame] | 99 | init_appearance(); |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 102 | /* |
| 103 | ** PyMac_InteractiveOptions - Allow user to set options if option key is pressed |
| 104 | */ |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 105 | static void |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 106 | PyMac_InteractiveOptions(PyMac_PrefRecord *p, int *argcp, char ***argvp) |
| 107 | { |
| 108 | KeyMap rmap; |
| 109 | unsigned char *map; |
| 110 | short item, type; |
| 111 | ControlHandle handle; |
| 112 | DialogPtr dialog; |
| 113 | Rect rect; |
| 114 | int old_argc = *argcp; |
| 115 | int i; |
| 116 | |
| 117 | /* |
| 118 | ** If the preferences disallows interactive options we return, |
| 119 | ** similarly of <option> isn't pressed. |
| 120 | */ |
| 121 | if (p->nointopt) return; |
| 122 | |
| 123 | GetKeys(rmap); |
| 124 | map = (unsigned char *)rmap; |
| 125 | if ( ( map[0x3a>>3] & (1<<(0x3a&7)) ) == 0 ) /* option key is 3a */ |
| 126 | return; |
| 127 | |
| 128 | dialog = GetNewDialog(OPT_DIALOG, NULL, (WindowPtr)-1); |
| 129 | if ( dialog == NULL ) { |
| 130 | printf("Option dialog not found - cannot set options\n"); |
| 131 | return; |
| 132 | } |
| 133 | SetDialogDefaultItem(dialog, OPT_OK); |
| 134 | SetDialogCancelItem(dialog, OPT_CANCEL); |
| 135 | |
| 136 | /* Set default values */ |
| 137 | #define SET_OPT_ITEM(num, var) \ |
| 138 | GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \ |
Jack Jansen | 08c3be3 | 1997-04-08 15:27:00 +0000 | [diff] [blame] | 139 | SetControlValue(handle, (short)p->var); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 140 | |
| 141 | SET_OPT_ITEM(OPT_INSPECT, inspect); |
| 142 | SET_OPT_ITEM(OPT_VERBOSE, verbose); |
Jack Jansen | 36b983c | 1997-09-09 13:53:21 +0000 | [diff] [blame] | 143 | SET_OPT_ITEM(OPT_OPTIMIZE, optimize); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 144 | SET_OPT_ITEM(OPT_UNBUFFERED, unbuffered); |
| 145 | SET_OPT_ITEM(OPT_DEBUGGING, debugging); |
| 146 | SET_OPT_ITEM(OPT_KEEPNORMAL, keep_normal); |
| 147 | SET_OPT_ITEM(OPT_KEEPERROR, keep_error); |
Jack Jansen | 0c6d037 | 2000-05-05 23:11:14 +0000 | [diff] [blame] | 148 | SET_OPT_ITEM(OPT_TABWARN, tabwarn); |
Jack Jansen | 36b983c | 1997-09-09 13:53:21 +0000 | [diff] [blame] | 149 | SET_OPT_ITEM(OPT_NOSITE, nosite); |
Jack Jansen | 0c6d037 | 2000-05-05 23:11:14 +0000 | [diff] [blame] | 150 | SET_OPT_ITEM(OPT_NONAVSERV, nonavservice); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 151 | /* The rest are not settable interactively */ |
| 152 | |
| 153 | #undef SET_OPT_ITEM |
| 154 | |
| 155 | while (1) { |
| 156 | handle = NULL; |
| 157 | ModalDialog(NULL, &item); |
| 158 | if ( item == OPT_OK ) |
| 159 | break; |
| 160 | if ( item == OPT_CANCEL ) { |
Jack Jansen | 08c3be3 | 1997-04-08 15:27:00 +0000 | [diff] [blame] | 161 | DisposeDialog(dialog); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 162 | exit(0); |
| 163 | } |
Jack Jansen | ee6eeb1 | 2000-06-02 21:28:52 +0000 | [diff] [blame] | 164 | #ifndef TARGET_API_MAC_CARBON |
Jack Jansen | 36b983c | 1997-09-09 13:53:21 +0000 | [diff] [blame] | 165 | if ( item == OPT_HELP ) { |
| 166 | HMSetBalloons(!HMGetBalloons()); |
| 167 | } |
Jack Jansen | ee6eeb1 | 2000-06-02 21:28:52 +0000 | [diff] [blame] | 168 | #endif |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 169 | if ( item == OPT_CMDLINE ) { |
| 170 | int new_argc, newer_argc; |
| 171 | char **new_argv, **newer_argv; |
| 172 | |
| 173 | new_argc = ccommand(&new_argv); |
| 174 | newer_argc = (new_argc-1) + old_argc; |
| 175 | newer_argv = malloc((newer_argc+1)*sizeof(char *)); |
| 176 | if( !newer_argv ) |
| 177 | Py_FatalError("Cannot malloc argv\n"); |
| 178 | for(i=0; i<old_argc; i++) |
| 179 | newer_argv[i] = (*argvp)[i]; |
| 180 | for(i=old_argc; i<=newer_argc; i++) /* Copy the NULL too */ |
| 181 | newer_argv[i] = new_argv[i-old_argc+1]; |
| 182 | *argvp = newer_argv; |
| 183 | *argcp = newer_argc; |
| 184 | |
| 185 | /* XXXX Is it not safe to use free() here, apparently */ |
| 186 | } |
| 187 | #define OPT_ITEM(num, var) \ |
| 188 | if ( item == (num) ) { \ |
| 189 | p->var = !p->var; \ |
| 190 | GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \ |
Jack Jansen | 08c3be3 | 1997-04-08 15:27:00 +0000 | [diff] [blame] | 191 | SetControlValue(handle, (short)p->var); \ |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | OPT_ITEM(OPT_INSPECT, inspect); |
| 195 | OPT_ITEM(OPT_VERBOSE, verbose); |
Jack Jansen | 36b983c | 1997-09-09 13:53:21 +0000 | [diff] [blame] | 196 | OPT_ITEM(OPT_OPTIMIZE, optimize); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 197 | OPT_ITEM(OPT_UNBUFFERED, unbuffered); |
| 198 | OPT_ITEM(OPT_DEBUGGING, debugging); |
| 199 | OPT_ITEM(OPT_KEEPNORMAL, keep_normal); |
| 200 | OPT_ITEM(OPT_KEEPERROR, keep_error); |
Jack Jansen | 0c6d037 | 2000-05-05 23:11:14 +0000 | [diff] [blame] | 201 | OPT_ITEM(OPT_TABWARN, tabwarn); |
Jack Jansen | 36b983c | 1997-09-09 13:53:21 +0000 | [diff] [blame] | 202 | OPT_ITEM(OPT_NOSITE, nosite); |
Jack Jansen | 0c6d037 | 2000-05-05 23:11:14 +0000 | [diff] [blame] | 203 | OPT_ITEM(OPT_NONAVSERV, nonavservice); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 204 | |
| 205 | #undef OPT_ITEM |
| 206 | } |
Jack Jansen | 08c3be3 | 1997-04-08 15:27:00 +0000 | [diff] [blame] | 207 | DisposeDialog(dialog); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /* |
| 211 | ** Initialization code, shared by interpreter and applets |
| 212 | */ |
| 213 | static void |
Jack Jansen | 52ac037 | 1997-01-15 15:49:08 +0000 | [diff] [blame] | 214 | init_common(int *argcp, char ***argvp, int embedded) |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 215 | { |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 216 | /* Remember resource fork refnum, for later */ |
| 217 | PyMac_AppRefNum = CurResFile(); |
| 218 | |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 219 | /* Initialize toolboxes */ |
| 220 | init_mac_world(); |
| 221 | |
| 222 | #ifdef USE_MAC_SHARED_LIBRARY |
| 223 | /* Add the shared library to the stack of resource files */ |
Jack Jansen | 87c485c | 1998-07-31 09:38:01 +0000 | [diff] [blame] | 224 | (void)PyMac_init_process_location(); |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 225 | PyMac_AddLibResources(); |
| 226 | #endif |
| 227 | |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 228 | #if defined(USE_GUSI1) |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 229 | /* Setup GUSI */ |
| 230 | GUSIDefaultSetup(); |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 231 | PyMac_SetGUSISpin(); |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 232 | PyMac_SetGUSIOptions(); |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 233 | #endif |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 234 | #if defined(USE_GUSI) |
| 235 | atexit(PyMac_StopGUSISpin); |
| 236 | #endif |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 237 | |
| 238 | #ifdef USE_SIOUX |
| 239 | /* Set various SIOUX flags. Some are changed later based on options */ |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 240 | /* SIOUXSettings.standalone = 0; /* XXXX Attempting to keep sioux from eating events */ |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 241 | SIOUXSettings.asktosaveonclose = 0; |
| 242 | SIOUXSettings.showstatusline = 0; |
| 243 | SIOUXSettings.tabspaces = 4; |
| 244 | #endif |
| 245 | |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 246 | /* Get options from preference file (or from applet resource fork) */ |
| 247 | options.keep_error = 1; /* default-default */ |
| 248 | PyMac_PreferenceOptions(&options); |
| 249 | |
Jack Jansen | 52ac037 | 1997-01-15 15:49:08 +0000 | [diff] [blame] | 250 | if ( embedded ) { |
| 251 | static char *emb_argv[] = {"embedded-python", 0}; |
| 252 | |
| 253 | *argcp = 1; |
| 254 | *argvp = emb_argv; |
| 255 | } else { |
| 256 | /* Create argc/argv. Do it before we go into the options event loop. */ |
| 257 | *argcp = PyMac_GetArgv(argvp, options.noargs); |
| 258 | |
| 259 | /* Do interactive option setting, if allowed and <option> depressed */ |
| 260 | PyMac_InteractiveOptions(&options, argcp, argvp); |
| 261 | } |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 262 | |
| 263 | /* Copy selected options to where the machine-independent stuff wants it */ |
| 264 | Py_VerboseFlag = options.verbose; |
Jack Jansen | 7330b39 | 1997-08-08 14:56:41 +0000 | [diff] [blame] | 265 | /* Py_SuppressPrintingFlag = options.suppress_print; */ |
Jack Jansen | 36b983c | 1997-09-09 13:53:21 +0000 | [diff] [blame] | 266 | Py_OptimizeFlag = options.optimize; |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 267 | Py_DebugFlag = options.debugging; |
Jack Jansen | a7a89eb | 1997-10-07 21:48:57 +0000 | [diff] [blame] | 268 | Py_NoSiteFlag = options.nosite; |
Jack Jansen | 0c6d037 | 2000-05-05 23:11:14 +0000 | [diff] [blame] | 269 | Py_TabcheckFlag = options.tabwarn; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 270 | if ( options.noargs ) { |
| 271 | /* don't process events at all without the scripts permission */ |
| 272 | PyMacSchedParams scp; |
| 273 | |
| 274 | PyMac_GetSchedParams(&scp); |
| 275 | scp.process_events = 0; |
| 276 | /* Should we disable command-dot as well? */ |
| 277 | PyMac_SetSchedParams(&scp); |
| 278 | } |
Jack Jansen | 36b983c | 1997-09-09 13:53:21 +0000 | [diff] [blame] | 279 | /* XXXX dispatch oldexc and nosite */ |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 280 | |
| 281 | /* Set buffering */ |
| 282 | if (options.unbuffered) { |
| 283 | #ifndef MPW |
| 284 | setbuf(stdout, (char *)NULL); |
| 285 | setbuf(stderr, (char *)NULL); |
| 286 | #else |
| 287 | /* On MPW (3.2) unbuffered seems to hang */ |
| 288 | setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ); |
| 289 | setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ); |
| 290 | #endif |
| 291 | } |
Jack Jansen | 8c69321 | 1997-01-07 16:19:42 +0000 | [diff] [blame] | 292 | #if __profile__ == 1 |
| 293 | /* collectSummary or collectDetailed, timebase, #routines, max stack depth */ |
Jack Jansen | e742487 | 1999-09-30 11:20:11 +0000 | [diff] [blame] | 294 | ProfilerInit(collectSummary, bestTimeBase, 8000, 250); |
Jack Jansen | 8c69321 | 1997-01-07 16:19:42 +0000 | [diff] [blame] | 295 | #endif |
Jack Jansen | 7330b39 | 1997-08-08 14:56:41 +0000 | [diff] [blame] | 296 | |
| 297 | /* Tell the rest of python about our argc/argv */ |
| 298 | orig_argc = *argcp; /* For Py_GetArgcArgv() */ |
| 299 | orig_argv = *argvp; |
| 300 | Py_SetProgramName((*argvp)[0]); |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 303 | /* |
| 304 | ** Inspection mode after script/applet termination |
| 305 | */ |
| 306 | static int |
| 307 | run_inspect() |
| 308 | { |
| 309 | int sts = 0; |
| 310 | |
| 311 | if (options.inspect && isatty((int)fileno(stdin))) |
| 312 | sts = PyRun_AnyFile(stdin, "<stdin>") != 0; |
| 313 | return sts; |
| 314 | } |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 315 | |
Jack Jansen | 0c6d037 | 2000-05-05 23:11:14 +0000 | [diff] [blame] | 316 | /* |
| 317 | ** Import the macfsn module, which will override the Standard File |
| 318 | ** calls in the macfs builtin module by Navigation Services versions, |
| 319 | ** if available on this machine. |
| 320 | */ |
| 321 | static void |
| 322 | PyMac_InstallNavServicesForSF() |
| 323 | { |
| 324 | if ( !options.nonavservice ) { |
| 325 | PyObject *m = PyImport_ImportModule("macfsn"); |
| 326 | |
| 327 | if ( m == NULL ) { |
| 328 | PySys_WriteStderr("'import macfsn' failed; "); |
| 329 | if (Py_VerboseFlag) { |
| 330 | PySys_WriteStderr("traceback:\n"); |
| 331 | PyErr_Print(); |
| 332 | } |
| 333 | else { |
| 334 | PySys_WriteStderr("use -v for traceback\n"); |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 340 | #ifdef USE_MAC_APPLET_SUPPORT |
| 341 | /* Applet support */ |
| 342 | |
| 343 | /* Run a compiled Python Python script from 'PYC ' resource __main__ */ |
| 344 | static int |
| 345 | run_main_resource() |
| 346 | { |
| 347 | Handle h; |
| 348 | long size; |
| 349 | PyObject *code; |
| 350 | PyObject *result; |
| 351 | |
| 352 | h = GetNamedResource('PYC ', "\p__main__"); |
| 353 | if (h == NULL) { |
| 354 | Alert(NOPYC_ALERT, NULL); |
| 355 | return 1; |
| 356 | } |
| 357 | size = GetResourceSizeOnDisk(h); |
| 358 | HLock(h); |
| 359 | code = PyMarshal_ReadObjectFromString(*h + 8, (int)(size - 8)); |
| 360 | HUnlock(h); |
| 361 | ReleaseResource(h); |
| 362 | if (code == NULL) { |
| 363 | PyErr_Print(); |
| 364 | return 1; |
| 365 | } |
| 366 | result = PyImport_ExecCodeModule("__main__", code); |
| 367 | Py_DECREF(code); |
| 368 | if (result == NULL) { |
| 369 | PyErr_Print(); |
| 370 | return 1; |
| 371 | } |
| 372 | Py_DECREF(result); |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | /* Initialization sequence for applets */ |
| 377 | void |
| 378 | PyMac_InitApplet() |
| 379 | { |
Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +0000 | [diff] [blame] | 380 | int argc; |
| 381 | char **argv; |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 382 | int err; |
| 383 | |
Jack Jansen | 52ac037 | 1997-01-15 15:49:08 +0000 | [diff] [blame] | 384 | init_common(&argc, &argv, 0); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 385 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 386 | Py_Initialize(); |
Jack Jansen | 0c6d037 | 2000-05-05 23:11:14 +0000 | [diff] [blame] | 387 | PyMac_InstallNavServicesForSF(); |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 388 | PySys_SetArgv(argc, argv); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 389 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 390 | err = run_main_resource(); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 391 | |
| 392 | err = (run_inspect() || err); |
| 393 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 394 | fflush(stderr); |
| 395 | fflush(stdout); |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 396 | PyMac_Exit(err); |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 397 | /* XXX Should we bother to Py_Exit(sts)? */ |
| 398 | } |
| 399 | |
Jack Jansen | 52ac037 | 1997-01-15 15:49:08 +0000 | [diff] [blame] | 400 | /* |
| 401 | ** Hook for embedding python. |
| 402 | */ |
| 403 | void |
| 404 | PyMac_Initialize() |
| 405 | { |
| 406 | int argc; |
| 407 | char **argv; |
| 408 | |
| 409 | init_common(&argc, &argv, 1); |
| 410 | Py_Initialize(); |
Jack Jansen | 0c6d037 | 2000-05-05 23:11:14 +0000 | [diff] [blame] | 411 | PyMac_InstallNavServicesForSF(); |
Jack Jansen | 52ac037 | 1997-01-15 15:49:08 +0000 | [diff] [blame] | 412 | PySys_SetArgv(argc, argv); |
| 413 | } |
| 414 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 415 | #endif /* USE_MAC_APPLET_SUPPORT */ |
| 416 | |
| 417 | /* For normal application */ |
| 418 | void |
| 419 | PyMac_InitApplication() |
| 420 | { |
| 421 | int argc; |
| 422 | char **argv; |
| 423 | |
Jack Jansen | 52ac037 | 1997-01-15 15:49:08 +0000 | [diff] [blame] | 424 | init_common(&argc, &argv, 0); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 425 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 426 | if ( argc > 1 ) { |
| 427 | /* We're running a script. Attempt to change current directory */ |
| 428 | char curwd[256], *endp; |
| 429 | |
| 430 | strcpy(curwd, argv[1]); |
| 431 | endp = strrchr(curwd, ':'); |
| 432 | if ( endp && endp > curwd ) { |
| 433 | *endp = '\0'; |
| 434 | |
| 435 | chdir(curwd); |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 436 | #ifdef USE_GUSI1 |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 437 | /* Change MacOS's idea of wd too */ |
| 438 | PyMac_FixGUSIcd(); |
| 439 | #endif |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | Py_Main(argc, argv); |
| 443 | } |
| 444 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 445 | /* Main program */ |
| 446 | |
Jack Jansen | 76ceece | 1996-08-19 11:18:24 +0000 | [diff] [blame] | 447 | static void |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 448 | Py_Main(argc, argv) |
| 449 | int argc; |
| 450 | char **argv; |
| 451 | { |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 452 | int sts; |
| 453 | char *command = NULL; |
| 454 | char *filename = NULL; |
| 455 | FILE *fp = stdin; |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 456 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 457 | filename = argv[1]; |
| 458 | |
| 459 | if (Py_VerboseFlag || |
| 460 | command == NULL && filename == NULL && isatty((int)fileno(fp))) |
| 461 | fprintf(stderr, "Python %s\n%s\n", |
Jack Jansen | a547dca | 1996-07-10 15:48:25 +0000 | [diff] [blame] | 462 | Py_GetVersion(), Py_GetCopyright()); |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 463 | |
| 464 | if (filename != NULL) { |
| 465 | if ((fp = fopen(filename, "r")) == NULL) { |
| 466 | fprintf(stderr, "%s: can't open file '%s'\n", |
| 467 | argv[0], filename); |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 468 | PyMac_Exit(2); |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 472 | /* We initialize the menubar here, hoping SIOUX is initialized by now */ |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 473 | PyMac_InitMenuBar(); |
| 474 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 475 | Py_Initialize(); |
| 476 | |
Jack Jansen | 0c6d037 | 2000-05-05 23:11:14 +0000 | [diff] [blame] | 477 | PyMac_InstallNavServicesForSF(); |
| 478 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 479 | PySys_SetArgv(argc-1, argv+1); |
| 480 | |
| 481 | if (filename == NULL && isatty((int)fileno(fp))) { |
| 482 | FILE *fp = fopen(STARTUP, "r"); |
| 483 | if (fp != NULL) { |
| 484 | (void) PyRun_SimpleFile(fp, STARTUP); |
| 485 | PyErr_Clear(); |
| 486 | fclose(fp); |
| 487 | } |
| 488 | } |
| 489 | sts = PyRun_AnyFile( |
| 490 | fp, filename == NULL ? "<stdin>" : filename) != 0; |
| 491 | if (filename != NULL) |
| 492 | fclose(fp); |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 493 | |
| 494 | if ( filename != NULL || command != NULL ) |
| 495 | sts = (run_inspect() || sts); |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 496 | |
| 497 | Py_Exit(sts); |
| 498 | /*NOTREACHED*/ |
| 499 | } |
| 500 | |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 501 | /* |
| 502 | ** Terminate application |
| 503 | */ |
Jack Jansen | 76ceece | 1996-08-19 11:18:24 +0000 | [diff] [blame] | 504 | void |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 505 | PyMac_Exit(status) |
| 506 | int status; |
| 507 | { |
| 508 | int keep; |
Jack Jansen | 8c69321 | 1997-01-07 16:19:42 +0000 | [diff] [blame] | 509 | |
| 510 | #if __profile__ == 1 |
| 511 | ProfilerDump("\pPython Profiler Results"); |
| 512 | ProfilerTerm(); |
| 513 | #endif |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 514 | if ( status ) |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 515 | keep = options.keep_error; |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 516 | else |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 517 | keep = options.keep_normal; |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 518 | |
Jack Jansen | 1e8557a | 1995-11-10 14:51:26 +0000 | [diff] [blame] | 519 | #ifdef USE_SIOUX |
| 520 | if (keep) { |
| 521 | SIOUXSettings.standalone = 1; |
| 522 | SIOUXSettings.autocloseonquit = 0; |
Jack Jansen | 415571c | 1996-03-25 15:46:03 +0000 | [diff] [blame] | 523 | SIOUXSetTitle("\p\307terminated\310"); |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 524 | PyMac_RestoreMenuBar(); |
Jack Jansen | e44545f | 1997-05-07 15:48:54 +0000 | [diff] [blame] | 525 | #ifdef USE_MSL |
| 526 | /* |
| 527 | ** Temporary workaround: autocloseonquit clearing does not |
| 528 | ** currently work for the MSL/GUSI combo. |
| 529 | */ |
| 530 | while(getchar() > 0); |
| 531 | #endif |
Jack Jansen | 1e8557a | 1995-11-10 14:51:26 +0000 | [diff] [blame] | 532 | } |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 533 | else |
| 534 | SIOUXSettings.autocloseonquit = 1; |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 535 | #endif /* USE_SIOUX */ |
Jack Jansen | 0168f27 | 1995-10-27 13:32:30 +0000 | [diff] [blame] | 536 | |
| 537 | exit(status); |
| 538 | } |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 539 | |
| 540 | /* Return the program name -- some code out there needs this. */ |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 541 | char * |
| 542 | Py_GetProgramFullPath() |
| 543 | { |
Jack Jansen | 7330b39 | 1997-08-08 14:56:41 +0000 | [diff] [blame] | 544 | return orig_argv[0]; |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 547 | |
| 548 | /* Make the *original* argc/argv available to other modules. |
| 549 | This is rare, but it is needed by the secureware extension. */ |
| 550 | |
| 551 | void |
Jack Jansen | 1d2f863 | 1996-08-02 15:16:16 +0000 | [diff] [blame] | 552 | Py_GetArgcArgv(argc,argv) |
Jack Jansen | 696c958 | 1995-08-14 12:33:20 +0000 | [diff] [blame] | 553 | int *argc; |
| 554 | char ***argv; |
| 555 | { |
| 556 | *argc = orig_argc; |
| 557 | *argv = orig_argv; |
Guido van Rossum | b0f3c82 | 1994-08-23 13:34:25 +0000 | [diff] [blame] | 558 | } |
Jack Jansen | 1d2f863 | 1996-08-02 15:16:16 +0000 | [diff] [blame] | 559 | |
| 560 | /* More cruft that shouldn't really be here, used in sysmodule.c */ |
| 561 | |
| 562 | char * |
| 563 | Py_GetPrefix() |
| 564 | { |
Jack Jansen | ac62569 | 1997-09-08 13:22:22 +0000 | [diff] [blame] | 565 | return PyMac_GetPythonDir(); |
Jack Jansen | 1d2f863 | 1996-08-02 15:16:16 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | char * |
| 569 | Py_GetExecPrefix() |
| 570 | { |
Jack Jansen | ac62569 | 1997-09-08 13:22:22 +0000 | [diff] [blame] | 571 | return PyMac_GetPythonDir(); |
Jack Jansen | 1d2f863 | 1996-08-02 15:16:16 +0000 | [diff] [blame] | 572 | } |