blob: 90d009379f187cb7c740b8bbf5e08fb22b7023e6 [file] [log] [blame]
Guido van Rossumb0f3c821994-08-23 13:34:25 +00001/***********************************************************
Jack Jansen42218ce1997-01-31 16:15:11 +00002Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
Guido van Rossum99546991995-01-08 14:33:34 +00003The Netherlands.
Guido van Rossumb0f3c821994-08-23 13:34:25 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Jack Jansen696c9581995-08-14 12:33:20 +000025/* Python interpreter main program */
Guido van Rossumb0f3c821994-08-23 13:34:25 +000026
Jack Jansen696c9581995-08-14 12:33:20 +000027#include "Python.h"
28#include "pythonresources.h"
29#include "import.h"
30#include "marshal.h"
Jack Jansenf6865f71996-09-04 15:24:59 +000031#include "macglue.h"
Guido van Rossumb0f3c821994-08-23 13:34:25 +000032
Jack Jansen696c9581995-08-14 12:33:20 +000033#include <Memory.h>
34#include <Resources.h>
Guido van Rossumb0f3c821994-08-23 13:34:25 +000035#include <stdio.h>
Jack Jansen696c9581995-08-14 12:33:20 +000036#include <Events.h>
37#include <Windows.h>
Jack Jansen2429c721996-03-07 15:17:11 +000038#include <Fonts.h>
Jack Jansen36b983c1997-09-09 13:53:21 +000039#include <Balloons.h>
Jack Jansen8f5725a1999-12-07 23:08:10 +000040#ifdef USE_APPEARANCE
41#include <Gestalt.h>
42#include <Appearance.h>
43#endif /* USE_APPEARANCE */
Jack Jansenc76fd391995-02-02 14:27:31 +000044#ifdef __MWERKS__
45#include <SIOUX.h>
Jack Jansen1e8557a1995-11-10 14:51:26 +000046#define USE_SIOUX
Jack Jansen8c693211997-01-07 16:19:42 +000047#if __profile__ == 1
48#include <profiler.h>
49#endif
Jack Jansenc76fd391995-02-02 14:27:31 +000050#endif
Jack Jansenee6eeb12000-06-02 21:28:52 +000051#include <unistd.h>
Jack Jansenc76fd391995-02-02 14:27:31 +000052
Jack Jansen0168f271995-10-27 13:32:30 +000053#ifdef THINK_C
54#include <console.h>
55#endif
56
Jack Jansen696c9581995-08-14 12:33:20 +000057#define STARTUP "PythonStartup"
Jack Jansenbac428d1994-12-14 13:47:30 +000058
Jack Jansen696c9581995-08-14 12:33:20 +000059extern int Py_DebugFlag; /* For parser.c, declared in pythonrun.c */
60extern int Py_VerboseFlag; /* For import.c, declared in pythonrun.c */
Jack Jansen3f7d2b41996-09-06 22:21:07 +000061short PyMac_AppRefNum; /* RefNum of application resource fork */
Jack Jansen696c9581995-08-14 12:33:20 +000062
Jack Jansen1d2f8631996-08-02 15:16:16 +000063/* For Py_GetArgcArgv(); set by main() */
Jack Jansen696c9581995-08-14 12:33:20 +000064static char **orig_argv;
65static int orig_argc;
66
Jack Jansen7d5f9e81996-09-07 17:09:31 +000067PyMac_PrefRecord options;
Jack Jansen0168f271995-10-27 13:32:30 +000068
Jack Jansen76ceece1996-08-19 11:18:24 +000069static void Py_Main Py_PROTO((int, char **)); /* Forward */
70void PyMac_Exit Py_PROTO((int)); /* Forward */
71
Jack Jansen8f5725a1999-12-07 23:08:10 +000072static void init_appearance()
73{
74#ifdef USE_APPEARANCE
75 OSErr err;
76 SInt32 response;
77
78 err = Gestalt(gestaltAppearanceAttr,&response);
79 if ( err ) goto no_appearance;
80 if ( !(response&(1<<gestaltAppearanceExists)) ) goto no_appearance;
81 /* XXXX Should we check the version? Compat-mode? */
82 PyMac_AppearanceCompliant = 1;
83no_appearance:
84 return;
85#endif /* USE_APPEARANCE */
86}
Jack Jansen01fbc681996-02-28 15:42:47 +000087/* Initialize the Mac toolbox world */
88
89static void
90init_mac_world()
91{
92#ifdef THINK_C
93 printf("\n");
94#else
Jack Jansenee6eeb12000-06-02 21:28:52 +000095#ifndef TARGET_API_MAC_CARBON
96 /* These aren't needed for carbon */
Jack Jansen01fbc681996-02-28 15:42:47 +000097 MaxApplZone();
98 InitGraf(&qd.thePort);
99 InitFonts();
100 InitWindows();
101 TEInit();
102 InitDialogs((long)0);
103 InitMenus();
Jack Jansenee6eeb12000-06-02 21:28:52 +0000104#endif
Jack Jansen01fbc681996-02-28 15:42:47 +0000105 InitCursor();
Jack Jansen8f5725a1999-12-07 23:08:10 +0000106 init_appearance();
Jack Jansen01fbc681996-02-28 15:42:47 +0000107#endif
108}
109
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000110/*
111** PyMac_InteractiveOptions - Allow user to set options if option key is pressed
112*/
Jack Jansen01fbc681996-02-28 15:42:47 +0000113static void
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000114PyMac_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;
122 int old_argc = *argcp;
123 int i;
124
125 /*
126 ** If the preferences disallows interactive options we return,
127 ** similarly of <option> isn't pressed.
128 */
129 if (p->nointopt) return;
130
131 GetKeys(rmap);
132 map = (unsigned char *)rmap;
133 if ( ( map[0x3a>>3] & (1<<(0x3a&7)) ) == 0 ) /* option key is 3a */
134 return;
135
136 dialog = GetNewDialog(OPT_DIALOG, NULL, (WindowPtr)-1);
137 if ( dialog == NULL ) {
138 printf("Option dialog not found - cannot set options\n");
139 return;
140 }
141 SetDialogDefaultItem(dialog, OPT_OK);
142 SetDialogCancelItem(dialog, OPT_CANCEL);
143
144 /* Set default values */
145#define SET_OPT_ITEM(num, var) \
146 GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
Jack Jansen08c3be31997-04-08 15:27:00 +0000147 SetControlValue(handle, (short)p->var);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000148
149 SET_OPT_ITEM(OPT_INSPECT, inspect);
150 SET_OPT_ITEM(OPT_VERBOSE, verbose);
Jack Jansen36b983c1997-09-09 13:53:21 +0000151 SET_OPT_ITEM(OPT_OPTIMIZE, optimize);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000152 SET_OPT_ITEM(OPT_UNBUFFERED, unbuffered);
153 SET_OPT_ITEM(OPT_DEBUGGING, debugging);
154 SET_OPT_ITEM(OPT_KEEPNORMAL, keep_normal);
155 SET_OPT_ITEM(OPT_KEEPERROR, keep_error);
Jack Jansen0c6d0372000-05-05 23:11:14 +0000156 SET_OPT_ITEM(OPT_TABWARN, tabwarn);
Jack Jansen36b983c1997-09-09 13:53:21 +0000157 SET_OPT_ITEM(OPT_NOSITE, nosite);
Jack Jansen0c6d0372000-05-05 23:11:14 +0000158 SET_OPT_ITEM(OPT_NONAVSERV, nonavservice);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000159 /* The rest are not settable interactively */
160
161#undef SET_OPT_ITEM
162
163 while (1) {
164 handle = NULL;
165 ModalDialog(NULL, &item);
166 if ( item == OPT_OK )
167 break;
168 if ( item == OPT_CANCEL ) {
Jack Jansen08c3be31997-04-08 15:27:00 +0000169 DisposeDialog(dialog);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000170 exit(0);
171 }
Jack Jansenee6eeb12000-06-02 21:28:52 +0000172#ifndef TARGET_API_MAC_CARBON
Jack Jansen36b983c1997-09-09 13:53:21 +0000173 if ( item == OPT_HELP ) {
174 HMSetBalloons(!HMGetBalloons());
175 }
Jack Jansenee6eeb12000-06-02 21:28:52 +0000176#endif
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000177 if ( item == OPT_CMDLINE ) {
178 int new_argc, newer_argc;
179 char **new_argv, **newer_argv;
180
181 new_argc = ccommand(&new_argv);
182 newer_argc = (new_argc-1) + old_argc;
183 newer_argv = malloc((newer_argc+1)*sizeof(char *));
184 if( !newer_argv )
185 Py_FatalError("Cannot malloc argv\n");
186 for(i=0; i<old_argc; i++)
187 newer_argv[i] = (*argvp)[i];
188 for(i=old_argc; i<=newer_argc; i++) /* Copy the NULL too */
189 newer_argv[i] = new_argv[i-old_argc+1];
190 *argvp = newer_argv;
191 *argcp = newer_argc;
192
193 /* XXXX Is it not safe to use free() here, apparently */
194 }
195#define OPT_ITEM(num, var) \
196 if ( item == (num) ) { \
197 p->var = !p->var; \
198 GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
Jack Jansen08c3be31997-04-08 15:27:00 +0000199 SetControlValue(handle, (short)p->var); \
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000200 }
201
202 OPT_ITEM(OPT_INSPECT, inspect);
203 OPT_ITEM(OPT_VERBOSE, verbose);
Jack Jansen36b983c1997-09-09 13:53:21 +0000204 OPT_ITEM(OPT_OPTIMIZE, optimize);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000205 OPT_ITEM(OPT_UNBUFFERED, unbuffered);
206 OPT_ITEM(OPT_DEBUGGING, debugging);
207 OPT_ITEM(OPT_KEEPNORMAL, keep_normal);
208 OPT_ITEM(OPT_KEEPERROR, keep_error);
Jack Jansen0c6d0372000-05-05 23:11:14 +0000209 OPT_ITEM(OPT_TABWARN, tabwarn);
Jack Jansen36b983c1997-09-09 13:53:21 +0000210 OPT_ITEM(OPT_NOSITE, nosite);
Jack Jansen0c6d0372000-05-05 23:11:14 +0000211 OPT_ITEM(OPT_NONAVSERV, nonavservice);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000212
213#undef OPT_ITEM
214 }
Jack Jansen08c3be31997-04-08 15:27:00 +0000215 DisposeDialog(dialog);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000216}
217
218/*
219** Initialization code, shared by interpreter and applets
220*/
221static void
Jack Jansen52ac0371997-01-15 15:49:08 +0000222init_common(int *argcp, char ***argvp, int embedded)
Jack Jansen01fbc681996-02-28 15:42:47 +0000223{
Jack Jansen3f7d2b41996-09-06 22:21:07 +0000224 /* Remember resource fork refnum, for later */
225 PyMac_AppRefNum = CurResFile();
226
Jack Jansen01fbc681996-02-28 15:42:47 +0000227 /* Initialize toolboxes */
228 init_mac_world();
229
230#ifdef USE_MAC_SHARED_LIBRARY
231 /* Add the shared library to the stack of resource files */
Jack Jansen87c485c1998-07-31 09:38:01 +0000232 (void)PyMac_init_process_location();
Jack Jansen01fbc681996-02-28 15:42:47 +0000233 PyMac_AddLibResources();
234#endif
235
Jack Jansen2d1306b2000-04-07 09:10:49 +0000236#if defined(USE_GUSI1)
Jack Jansen01fbc681996-02-28 15:42:47 +0000237 /* Setup GUSI */
238 GUSIDefaultSetup();
Jack Jansenf6865f71996-09-04 15:24:59 +0000239 PyMac_SetGUSISpin();
Jack Jansen3f7d2b41996-09-06 22:21:07 +0000240 PyMac_SetGUSIOptions();
Jack Jansen01fbc681996-02-28 15:42:47 +0000241#endif
Jack Jansen2d1306b2000-04-07 09:10:49 +0000242#if defined(USE_GUSI)
243 atexit(PyMac_StopGUSISpin);
244#endif
Jack Jansen01fbc681996-02-28 15:42:47 +0000245
246#ifdef USE_SIOUX
247 /* Set various SIOUX flags. Some are changed later based on options */
Jack Jansencaa7c461997-06-12 10:49:13 +0000248/* SIOUXSettings.standalone = 0; /* XXXX Attempting to keep sioux from eating events */
Jack Jansen01fbc681996-02-28 15:42:47 +0000249 SIOUXSettings.asktosaveonclose = 0;
250 SIOUXSettings.showstatusline = 0;
251 SIOUXSettings.tabspaces = 4;
252#endif
253
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000254 /* Get options from preference file (or from applet resource fork) */
255 options.keep_error = 1; /* default-default */
256 PyMac_PreferenceOptions(&options);
257
Jack Jansen52ac0371997-01-15 15:49:08 +0000258 if ( embedded ) {
259 static char *emb_argv[] = {"embedded-python", 0};
260
261 *argcp = 1;
262 *argvp = emb_argv;
263 } else {
264 /* Create argc/argv. Do it before we go into the options event loop. */
265 *argcp = PyMac_GetArgv(argvp, options.noargs);
266
267 /* Do interactive option setting, if allowed and <option> depressed */
268 PyMac_InteractiveOptions(&options, argcp, argvp);
269 }
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000270
271 /* Copy selected options to where the machine-independent stuff wants it */
272 Py_VerboseFlag = options.verbose;
Jack Jansen7330b391997-08-08 14:56:41 +0000273/* Py_SuppressPrintingFlag = options.suppress_print; */
Jack Jansen36b983c1997-09-09 13:53:21 +0000274 Py_OptimizeFlag = options.optimize;
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000275 Py_DebugFlag = options.debugging;
Jack Jansena7a89eb1997-10-07 21:48:57 +0000276 Py_NoSiteFlag = options.nosite;
Jack Jansen0c6d0372000-05-05 23:11:14 +0000277 Py_TabcheckFlag = options.tabwarn;
Jack Jansene3ae0df1997-06-03 15:28:29 +0000278 if ( options.noargs ) {
279 /* don't process events at all without the scripts permission */
280 PyMacSchedParams scp;
281
282 PyMac_GetSchedParams(&scp);
283 scp.process_events = 0;
284 /* Should we disable command-dot as well? */
285 PyMac_SetSchedParams(&scp);
286 }
Jack Jansen36b983c1997-09-09 13:53:21 +0000287 /* XXXX dispatch oldexc and nosite */
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000288
289 /* Set buffering */
290 if (options.unbuffered) {
291#ifndef MPW
292 setbuf(stdout, (char *)NULL);
293 setbuf(stderr, (char *)NULL);
294#else
295 /* On MPW (3.2) unbuffered seems to hang */
296 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
297 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
298#endif
299 }
Jack Jansen8c693211997-01-07 16:19:42 +0000300#if __profile__ == 1
301 /* collectSummary or collectDetailed, timebase, #routines, max stack depth */
Jack Jansene7424871999-09-30 11:20:11 +0000302 ProfilerInit(collectSummary, bestTimeBase, 8000, 250);
Jack Jansen8c693211997-01-07 16:19:42 +0000303#endif
Jack Jansen7330b391997-08-08 14:56:41 +0000304
305 /* Tell the rest of python about our argc/argv */
306 orig_argc = *argcp; /* For Py_GetArgcArgv() */
307 orig_argv = *argvp;
308 Py_SetProgramName((*argvp)[0]);
Jack Jansen01fbc681996-02-28 15:42:47 +0000309}
310
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000311/*
312** Inspection mode after script/applet termination
313*/
314static int
315run_inspect()
316{
317 int sts = 0;
318
319 if (options.inspect && isatty((int)fileno(stdin)))
320 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
321 return sts;
322}
Jack Jansen01fbc681996-02-28 15:42:47 +0000323
Jack Jansen0c6d0372000-05-05 23:11:14 +0000324/*
325** Import the macfsn module, which will override the Standard File
326** calls in the macfs builtin module by Navigation Services versions,
327** if available on this machine.
328*/
329static void
330PyMac_InstallNavServicesForSF()
331{
332 if ( !options.nonavservice ) {
333 PyObject *m = PyImport_ImportModule("macfsn");
334
335 if ( m == NULL ) {
336 PySys_WriteStderr("'import macfsn' failed; ");
337 if (Py_VerboseFlag) {
338 PySys_WriteStderr("traceback:\n");
339 PyErr_Print();
340 }
341 else {
342 PySys_WriteStderr("use -v for traceback\n");
343 }
344 }
345 }
346}
347
Jack Jansen696c9581995-08-14 12:33:20 +0000348#ifdef USE_MAC_APPLET_SUPPORT
349/* Applet support */
350
351/* Run a compiled Python Python script from 'PYC ' resource __main__ */
352static int
353run_main_resource()
354{
355 Handle h;
356 long size;
357 PyObject *code;
358 PyObject *result;
359
360 h = GetNamedResource('PYC ', "\p__main__");
361 if (h == NULL) {
362 Alert(NOPYC_ALERT, NULL);
363 return 1;
364 }
365 size = GetResourceSizeOnDisk(h);
366 HLock(h);
367 code = PyMarshal_ReadObjectFromString(*h + 8, (int)(size - 8));
368 HUnlock(h);
369 ReleaseResource(h);
370 if (code == NULL) {
371 PyErr_Print();
372 return 1;
373 }
374 result = PyImport_ExecCodeModule("__main__", code);
375 Py_DECREF(code);
376 if (result == NULL) {
377 PyErr_Print();
378 return 1;
379 }
380 Py_DECREF(result);
381 return 0;
382}
383
384/* Initialization sequence for applets */
385void
386PyMac_InitApplet()
387{
Guido van Rossumb0f3c821994-08-23 13:34:25 +0000388 int argc;
389 char **argv;
Jack Jansen696c9581995-08-14 12:33:20 +0000390 int err;
391
Jack Jansen52ac0371997-01-15 15:49:08 +0000392 init_common(&argc, &argv, 0);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000393
Jack Jansen696c9581995-08-14 12:33:20 +0000394 Py_Initialize();
Jack Jansen0c6d0372000-05-05 23:11:14 +0000395 PyMac_InstallNavServicesForSF();
Jack Jansen696c9581995-08-14 12:33:20 +0000396 PySys_SetArgv(argc, argv);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000397
Jack Jansen696c9581995-08-14 12:33:20 +0000398 err = run_main_resource();
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000399
400 err = (run_inspect() || err);
401
Jack Jansen696c9581995-08-14 12:33:20 +0000402 fflush(stderr);
403 fflush(stdout);
Jack Jansen0168f271995-10-27 13:32:30 +0000404 PyMac_Exit(err);
Jack Jansen696c9581995-08-14 12:33:20 +0000405 /* XXX Should we bother to Py_Exit(sts)? */
406}
407
Jack Jansen52ac0371997-01-15 15:49:08 +0000408/*
409** Hook for embedding python.
410*/
411void
412PyMac_Initialize()
413{
414 int argc;
415 char **argv;
416
417 init_common(&argc, &argv, 1);
418 Py_Initialize();
Jack Jansen0c6d0372000-05-05 23:11:14 +0000419 PyMac_InstallNavServicesForSF();
Jack Jansen52ac0371997-01-15 15:49:08 +0000420 PySys_SetArgv(argc, argv);
421}
422
Jack Jansen696c9581995-08-14 12:33:20 +0000423#endif /* USE_MAC_APPLET_SUPPORT */
424
425/* For normal application */
426void
427PyMac_InitApplication()
428{
429 int argc;
430 char **argv;
431
Jack Jansen52ac0371997-01-15 15:49:08 +0000432 init_common(&argc, &argv, 0);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000433
Jack Jansen696c9581995-08-14 12:33:20 +0000434 if ( argc > 1 ) {
435 /* We're running a script. Attempt to change current directory */
436 char curwd[256], *endp;
437
438 strcpy(curwd, argv[1]);
439 endp = strrchr(curwd, ':');
440 if ( endp && endp > curwd ) {
441 *endp = '\0';
442
443 chdir(curwd);
Jack Jansen2d1306b2000-04-07 09:10:49 +0000444#ifdef USE_GUSI1
Jack Jansen378815c1996-03-06 16:21:34 +0000445 /* Change MacOS's idea of wd too */
446 PyMac_FixGUSIcd();
447#endif
Jack Jansen696c9581995-08-14 12:33:20 +0000448 }
449 }
450 Py_Main(argc, argv);
451}
452
Jack Jansen696c9581995-08-14 12:33:20 +0000453/* Main program */
454
Jack Jansen76ceece1996-08-19 11:18:24 +0000455static void
Jack Jansen696c9581995-08-14 12:33:20 +0000456Py_Main(argc, argv)
457 int argc;
458 char **argv;
459{
Jack Jansen696c9581995-08-14 12:33:20 +0000460 int sts;
461 char *command = NULL;
462 char *filename = NULL;
463 FILE *fp = stdin;
Jack Jansen696c9581995-08-14 12:33:20 +0000464
Jack Jansen696c9581995-08-14 12:33:20 +0000465 filename = argv[1];
466
467 if (Py_VerboseFlag ||
468 command == NULL && filename == NULL && isatty((int)fileno(fp)))
469 fprintf(stderr, "Python %s\n%s\n",
Jack Jansena547dca1996-07-10 15:48:25 +0000470 Py_GetVersion(), Py_GetCopyright());
Jack Jansen696c9581995-08-14 12:33:20 +0000471
472 if (filename != NULL) {
473 if ((fp = fopen(filename, "r")) == NULL) {
474 fprintf(stderr, "%s: can't open file '%s'\n",
475 argv[0], filename);
Jack Jansen0168f271995-10-27 13:32:30 +0000476 PyMac_Exit(2);
Jack Jansen696c9581995-08-14 12:33:20 +0000477 }
478 }
479
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000480 /* We initialize the menubar here, hoping SIOUX is initialized by now */
Jack Jansen3469e991996-09-06 00:30:45 +0000481 PyMac_InitMenuBar();
482
Jack Jansen696c9581995-08-14 12:33:20 +0000483 Py_Initialize();
484
Jack Jansen0c6d0372000-05-05 23:11:14 +0000485 PyMac_InstallNavServicesForSF();
486
Jack Jansen696c9581995-08-14 12:33:20 +0000487 PySys_SetArgv(argc-1, argv+1);
488
489 if (filename == NULL && isatty((int)fileno(fp))) {
490 FILE *fp = fopen(STARTUP, "r");
491 if (fp != NULL) {
492 (void) PyRun_SimpleFile(fp, STARTUP);
493 PyErr_Clear();
494 fclose(fp);
495 }
496 }
497 sts = PyRun_AnyFile(
498 fp, filename == NULL ? "<stdin>" : filename) != 0;
499 if (filename != NULL)
500 fclose(fp);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000501
502 if ( filename != NULL || command != NULL )
503 sts = (run_inspect() || sts);
Jack Jansen696c9581995-08-14 12:33:20 +0000504
505 Py_Exit(sts);
506 /*NOTREACHED*/
507}
508
Jack Jansen0168f271995-10-27 13:32:30 +0000509/*
510** Terminate application
511*/
Jack Jansen76ceece1996-08-19 11:18:24 +0000512void
Jack Jansen0168f271995-10-27 13:32:30 +0000513PyMac_Exit(status)
514 int status;
515{
516 int keep;
Jack Jansen8c693211997-01-07 16:19:42 +0000517
518#if __profile__ == 1
519 ProfilerDump("\pPython Profiler Results");
520 ProfilerTerm();
521#endif
Jack Jansen0168f271995-10-27 13:32:30 +0000522 if ( status )
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000523 keep = options.keep_error;
Jack Jansen0168f271995-10-27 13:32:30 +0000524 else
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000525 keep = options.keep_normal;
Jack Jansen0168f271995-10-27 13:32:30 +0000526
Jack Jansen1e8557a1995-11-10 14:51:26 +0000527#ifdef USE_SIOUX
528 if (keep) {
529 SIOUXSettings.standalone = 1;
530 SIOUXSettings.autocloseonquit = 0;
Jack Jansen415571c1996-03-25 15:46:03 +0000531 SIOUXSetTitle("\p\307terminated\310");
Jack Jansencaa7c461997-06-12 10:49:13 +0000532 PyMac_RestoreMenuBar();
Jack Jansene44545f1997-05-07 15:48:54 +0000533#ifdef USE_MSL
534 /*
535 ** Temporary workaround: autocloseonquit clearing does not
536 ** currently work for the MSL/GUSI combo.
537 */
538 while(getchar() > 0);
539#endif
Jack Jansen1e8557a1995-11-10 14:51:26 +0000540 }
Jack Jansen0168f271995-10-27 13:32:30 +0000541 else
542 SIOUXSettings.autocloseonquit = 1;
Jack Jansenf6865f71996-09-04 15:24:59 +0000543#endif /* USE_SIOUX */
Jack Jansen0168f271995-10-27 13:32:30 +0000544#ifdef THINK_C
545 console_options.pause_atexit = keep;
546#endif
547
548 exit(status);
549}
Jack Jansen696c9581995-08-14 12:33:20 +0000550
551/* Return the program name -- some code out there needs this. */
Jack Jansena39f1b01997-05-23 15:35:14 +0000552char *
553Py_GetProgramFullPath()
554{
Jack Jansen7330b391997-08-08 14:56:41 +0000555 return orig_argv[0];
Jack Jansena39f1b01997-05-23 15:35:14 +0000556}
557
Jack Jansen696c9581995-08-14 12:33:20 +0000558
559/* Make the *original* argc/argv available to other modules.
560 This is rare, but it is needed by the secureware extension. */
561
562void
Jack Jansen1d2f8631996-08-02 15:16:16 +0000563Py_GetArgcArgv(argc,argv)
Jack Jansen696c9581995-08-14 12:33:20 +0000564 int *argc;
565 char ***argv;
566{
567 *argc = orig_argc;
568 *argv = orig_argv;
Guido van Rossumb0f3c821994-08-23 13:34:25 +0000569}
Jack Jansen1d2f8631996-08-02 15:16:16 +0000570
571/* More cruft that shouldn't really be here, used in sysmodule.c */
572
573char *
574Py_GetPrefix()
575{
Jack Jansenac625691997-09-08 13:22:22 +0000576 return PyMac_GetPythonDir();
Jack Jansen1d2f8631996-08-02 15:16:16 +0000577}
578
579char *
580Py_GetExecPrefix()
581{
Jack Jansenac625691997-09-08 13:22:22 +0000582 return PyMac_GetPythonDir();
Jack Jansen1d2f8631996-08-02 15:16:16 +0000583}