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