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