blob: e3e9615b861c868d3b24da9c131a2ad605075ae6 [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 Jansen9ae898b2000-07-11 21:16:03 +000047extern int ccommand(char ***);
Jack Jansen8c693211997-01-07 16:19:42 +000048#if __profile__ == 1
49#include <profiler.h>
50#endif
Jack Jansenc76fd391995-02-02 14:27:31 +000051#endif
Jack Jansenee6eeb12000-06-02 21:28:52 +000052#include <unistd.h>
Jack Jansenc76fd391995-02-02 14:27:31 +000053
Jack Jansen696c9581995-08-14 12:33:20 +000054#define STARTUP "PythonStartup"
Jack Jansenbac428d1994-12-14 13:47:30 +000055
Jack Jansen696c9581995-08-14 12:33:20 +000056extern int Py_DebugFlag; /* For parser.c, declared in pythonrun.c */
57extern int Py_VerboseFlag; /* For import.c, declared in pythonrun.c */
Jack Jansen3f7d2b41996-09-06 22:21:07 +000058short PyMac_AppRefNum; /* RefNum of application resource fork */
Jack Jansen696c9581995-08-14 12:33:20 +000059
Jack Jansen1d2f8631996-08-02 15:16:16 +000060/* For Py_GetArgcArgv(); set by main() */
Jack Jansen696c9581995-08-14 12:33:20 +000061static char **orig_argv;
62static int orig_argc;
63
Jack Jansen7d5f9e81996-09-07 17:09:31 +000064PyMac_PrefRecord options;
Jack Jansen0168f271995-10-27 13:32:30 +000065
Jack Jansend88296d2000-07-11 19:51:05 +000066static void Py_Main(int, char **); /* Forward */
67void PyMac_Exit(int); /* Forward */
Jack Jansen76ceece1996-08-19 11:18:24 +000068
Jack Jansen8f5725a1999-12-07 23:08:10 +000069static 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;
80no_appearance:
81 return;
82#endif /* USE_APPEARANCE */
83}
Jack Jansen01fbc681996-02-28 15:42:47 +000084/* Initialize the Mac toolbox world */
85
86static void
87init_mac_world()
88{
Jack Jansenee6eeb12000-06-02 21:28:52 +000089#ifndef TARGET_API_MAC_CARBON
90 /* These aren't needed for carbon */
Jack Jansen01fbc681996-02-28 15:42:47 +000091 MaxApplZone();
92 InitGraf(&qd.thePort);
93 InitFonts();
94 InitWindows();
95 TEInit();
96 InitDialogs((long)0);
97 InitMenus();
Jack Jansenee6eeb12000-06-02 21:28:52 +000098#endif
Jack Jansen01fbc681996-02-28 15:42:47 +000099 InitCursor();
Jack Jansen8f5725a1999-12-07 23:08:10 +0000100 init_appearance();
Jack Jansen01fbc681996-02-28 15:42:47 +0000101}
102
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000103/*
104** PyMac_InteractiveOptions - Allow user to set options if option key is pressed
105*/
Jack Jansen01fbc681996-02-28 15:42:47 +0000106static void
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000107PyMac_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 Jansen08c3be31997-04-08 15:27:00 +0000140 SetControlValue(handle, (short)p->var);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000141
142 SET_OPT_ITEM(OPT_INSPECT, inspect);
143 SET_OPT_ITEM(OPT_VERBOSE, verbose);
Jack Jansen36b983c1997-09-09 13:53:21 +0000144 SET_OPT_ITEM(OPT_OPTIMIZE, optimize);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000145 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 Jansen0c6d0372000-05-05 23:11:14 +0000149 SET_OPT_ITEM(OPT_TABWARN, tabwarn);
Jack Jansen36b983c1997-09-09 13:53:21 +0000150 SET_OPT_ITEM(OPT_NOSITE, nosite);
Jack Jansen0c6d0372000-05-05 23:11:14 +0000151 SET_OPT_ITEM(OPT_NONAVSERV, nonavservice);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000152 /* 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 Jansen08c3be31997-04-08 15:27:00 +0000162 DisposeDialog(dialog);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000163 exit(0);
164 }
Jack Jansenee6eeb12000-06-02 21:28:52 +0000165#ifndef TARGET_API_MAC_CARBON
Jack Jansen36b983c1997-09-09 13:53:21 +0000166 if ( item == OPT_HELP ) {
167 HMSetBalloons(!HMGetBalloons());
168 }
Jack Jansenee6eeb12000-06-02 21:28:52 +0000169#endif
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000170 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 Jansen08c3be31997-04-08 15:27:00 +0000192 SetControlValue(handle, (short)p->var); \
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000193 }
194
195 OPT_ITEM(OPT_INSPECT, inspect);
196 OPT_ITEM(OPT_VERBOSE, verbose);
Jack Jansen36b983c1997-09-09 13:53:21 +0000197 OPT_ITEM(OPT_OPTIMIZE, optimize);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000198 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 Jansen0c6d0372000-05-05 23:11:14 +0000202 OPT_ITEM(OPT_TABWARN, tabwarn);
Jack Jansen36b983c1997-09-09 13:53:21 +0000203 OPT_ITEM(OPT_NOSITE, nosite);
Jack Jansen0c6d0372000-05-05 23:11:14 +0000204 OPT_ITEM(OPT_NONAVSERV, nonavservice);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000205
206#undef OPT_ITEM
207 }
Jack Jansen08c3be31997-04-08 15:27:00 +0000208 DisposeDialog(dialog);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000209}
210
211/*
212** Initialization code, shared by interpreter and applets
213*/
214static void
Jack Jansen52ac0371997-01-15 15:49:08 +0000215init_common(int *argcp, char ***argvp, int embedded)
Jack Jansen01fbc681996-02-28 15:42:47 +0000216{
Jack Jansen3f7d2b41996-09-06 22:21:07 +0000217 /* Remember resource fork refnum, for later */
218 PyMac_AppRefNum = CurResFile();
219
Jack Jansen01fbc681996-02-28 15:42:47 +0000220 /* 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 Jansen87c485c1998-07-31 09:38:01 +0000225 (void)PyMac_init_process_location();
Jack Jansen01fbc681996-02-28 15:42:47 +0000226 PyMac_AddLibResources();
227#endif
228
Jack Jansen2d1306b2000-04-07 09:10:49 +0000229#if defined(USE_GUSI1)
Jack Jansen01fbc681996-02-28 15:42:47 +0000230 /* Setup GUSI */
231 GUSIDefaultSetup();
Jack Jansenf6865f71996-09-04 15:24:59 +0000232 PyMac_SetGUSISpin();
Jack Jansen3f7d2b41996-09-06 22:21:07 +0000233 PyMac_SetGUSIOptions();
Jack Jansen01fbc681996-02-28 15:42:47 +0000234#endif
Jack Jansen2d1306b2000-04-07 09:10:49 +0000235#if defined(USE_GUSI)
236 atexit(PyMac_StopGUSISpin);
237#endif
Jack Jansen01fbc681996-02-28 15:42:47 +0000238
239#ifdef USE_SIOUX
240 /* Set various SIOUX flags. Some are changed later based on options */
Jack Jansencaa7c461997-06-12 10:49:13 +0000241/* SIOUXSettings.standalone = 0; /* XXXX Attempting to keep sioux from eating events */
Jack Jansen01fbc681996-02-28 15:42:47 +0000242 SIOUXSettings.asktosaveonclose = 0;
243 SIOUXSettings.showstatusline = 0;
244 SIOUXSettings.tabspaces = 4;
245#endif
246
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000247 /* Get options from preference file (or from applet resource fork) */
248 options.keep_error = 1; /* default-default */
249 PyMac_PreferenceOptions(&options);
250
Jack Jansen52ac0371997-01-15 15:49:08 +0000251 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);
259
260 /* Do interactive option setting, if allowed and <option> depressed */
261 PyMac_InteractiveOptions(&options, argcp, argvp);
262 }
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000263
264 /* Copy selected options to where the machine-independent stuff wants it */
265 Py_VerboseFlag = options.verbose;
Jack Jansen7330b391997-08-08 14:56:41 +0000266/* Py_SuppressPrintingFlag = options.suppress_print; */
Jack Jansen36b983c1997-09-09 13:53:21 +0000267 Py_OptimizeFlag = options.optimize;
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000268 Py_DebugFlag = options.debugging;
Jack Jansena7a89eb1997-10-07 21:48:57 +0000269 Py_NoSiteFlag = options.nosite;
Jack Jansen0c6d0372000-05-05 23:11:14 +0000270 Py_TabcheckFlag = options.tabwarn;
Jack Jansene3ae0df1997-06-03 15:28:29 +0000271 if ( options.noargs ) {
272 /* don't process events at all without the scripts permission */
273 PyMacSchedParams scp;
274
275 PyMac_GetSchedParams(&scp);
276 scp.process_events = 0;
277 /* Should we disable command-dot as well? */
278 PyMac_SetSchedParams(&scp);
279 }
Jack Jansen36b983c1997-09-09 13:53:21 +0000280 /* XXXX dispatch oldexc and nosite */
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000281
282 /* Set buffering */
283 if (options.unbuffered) {
284#ifndef MPW
285 setbuf(stdout, (char *)NULL);
286 setbuf(stderr, (char *)NULL);
287#else
288 /* On MPW (3.2) unbuffered seems to hang */
289 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
290 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
291#endif
292 }
Jack Jansen8c693211997-01-07 16:19:42 +0000293#if __profile__ == 1
294 /* collectSummary or collectDetailed, timebase, #routines, max stack depth */
Jack Jansene7424871999-09-30 11:20:11 +0000295 ProfilerInit(collectSummary, bestTimeBase, 8000, 250);
Jack Jansen8c693211997-01-07 16:19:42 +0000296#endif
Jack Jansen7330b391997-08-08 14:56:41 +0000297
298 /* Tell the rest of python about our argc/argv */
299 orig_argc = *argcp; /* For Py_GetArgcArgv() */
300 orig_argv = *argvp;
301 Py_SetProgramName((*argvp)[0]);
Jack Jansen01fbc681996-02-28 15:42:47 +0000302}
303
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000304/*
305** Inspection mode after script/applet termination
306*/
307static int
308run_inspect()
309{
310 int sts = 0;
311
312 if (options.inspect && isatty((int)fileno(stdin)))
313 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
314 return sts;
315}
Jack Jansen01fbc681996-02-28 15:42:47 +0000316
Jack Jansen0c6d0372000-05-05 23:11:14 +0000317/*
318** Import the macfsn module, which will override the Standard File
319** calls in the macfs builtin module by Navigation Services versions,
320** if available on this machine.
321*/
322static void
323PyMac_InstallNavServicesForSF()
324{
325 if ( !options.nonavservice ) {
326 PyObject *m = PyImport_ImportModule("macfsn");
327
328 if ( m == NULL ) {
329 PySys_WriteStderr("'import macfsn' failed; ");
330 if (Py_VerboseFlag) {
331 PySys_WriteStderr("traceback:\n");
332 PyErr_Print();
333 }
334 else {
335 PySys_WriteStderr("use -v for traceback\n");
336 }
337 }
338 }
339}
340
Jack Jansen696c9581995-08-14 12:33:20 +0000341#ifdef USE_MAC_APPLET_SUPPORT
342/* Applet support */
343
344/* Run a compiled Python Python script from 'PYC ' resource __main__ */
345static int
346run_main_resource()
347{
348 Handle h;
349 long size;
350 PyObject *code;
351 PyObject *result;
352
353 h = GetNamedResource('PYC ', "\p__main__");
354 if (h == NULL) {
355 Alert(NOPYC_ALERT, NULL);
356 return 1;
357 }
358 size = GetResourceSizeOnDisk(h);
359 HLock(h);
360 code = PyMarshal_ReadObjectFromString(*h + 8, (int)(size - 8));
361 HUnlock(h);
362 ReleaseResource(h);
363 if (code == NULL) {
364 PyErr_Print();
365 return 1;
366 }
367 result = PyImport_ExecCodeModule("__main__", code);
368 Py_DECREF(code);
369 if (result == NULL) {
370 PyErr_Print();
371 return 1;
372 }
373 Py_DECREF(result);
374 return 0;
375}
376
377/* Initialization sequence for applets */
378void
379PyMac_InitApplet()
380{
Guido van Rossumb0f3c821994-08-23 13:34:25 +0000381 int argc;
382 char **argv;
Jack Jansen696c9581995-08-14 12:33:20 +0000383 int err;
384
Jack Jansen52ac0371997-01-15 15:49:08 +0000385 init_common(&argc, &argv, 0);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000386
Jack Jansen696c9581995-08-14 12:33:20 +0000387 Py_Initialize();
Jack Jansen0c6d0372000-05-05 23:11:14 +0000388 PyMac_InstallNavServicesForSF();
Jack Jansen696c9581995-08-14 12:33:20 +0000389 PySys_SetArgv(argc, argv);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000390
Jack Jansen696c9581995-08-14 12:33:20 +0000391 err = run_main_resource();
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000392
393 err = (run_inspect() || err);
394
Jack Jansen696c9581995-08-14 12:33:20 +0000395 fflush(stderr);
396 fflush(stdout);
Jack Jansen0168f271995-10-27 13:32:30 +0000397 PyMac_Exit(err);
Jack Jansen696c9581995-08-14 12:33:20 +0000398 /* XXX Should we bother to Py_Exit(sts)? */
399}
400
Jack Jansen52ac0371997-01-15 15:49:08 +0000401/*
402** Hook for embedding python.
403*/
404void
405PyMac_Initialize()
406{
407 int argc;
408 char **argv;
409
410 init_common(&argc, &argv, 1);
411 Py_Initialize();
Jack Jansen0c6d0372000-05-05 23:11:14 +0000412 PyMac_InstallNavServicesForSF();
Jack Jansen52ac0371997-01-15 15:49:08 +0000413 PySys_SetArgv(argc, argv);
414}
415
Jack Jansen696c9581995-08-14 12:33:20 +0000416#endif /* USE_MAC_APPLET_SUPPORT */
417
418/* For normal application */
419void
420PyMac_InitApplication()
421{
422 int argc;
423 char **argv;
424
Jack Jansen52ac0371997-01-15 15:49:08 +0000425 init_common(&argc, &argv, 0);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000426
Jack Jansen696c9581995-08-14 12:33:20 +0000427 if ( argc > 1 ) {
428 /* We're running a script. Attempt to change current directory */
429 char curwd[256], *endp;
430
431 strcpy(curwd, argv[1]);
432 endp = strrchr(curwd, ':');
433 if ( endp && endp > curwd ) {
434 *endp = '\0';
435
436 chdir(curwd);
Jack Jansen2d1306b2000-04-07 09:10:49 +0000437#ifdef USE_GUSI1
Jack Jansen378815c1996-03-06 16:21:34 +0000438 /* Change MacOS's idea of wd too */
439 PyMac_FixGUSIcd();
440#endif
Jack Jansen696c9581995-08-14 12:33:20 +0000441 }
442 }
443 Py_Main(argc, argv);
444}
445
Jack Jansen696c9581995-08-14 12:33:20 +0000446/* Main program */
447
Jack Jansen76ceece1996-08-19 11:18:24 +0000448static void
Jack Jansen696c9581995-08-14 12:33:20 +0000449Py_Main(argc, argv)
450 int argc;
451 char **argv;
452{
Jack Jansen696c9581995-08-14 12:33:20 +0000453 int sts;
454 char *command = NULL;
455 char *filename = NULL;
456 FILE *fp = stdin;
Jack Jansen696c9581995-08-14 12:33:20 +0000457
Jack Jansen696c9581995-08-14 12:33:20 +0000458 filename = argv[1];
459
460 if (Py_VerboseFlag ||
461 command == NULL && filename == NULL && isatty((int)fileno(fp)))
462 fprintf(stderr, "Python %s\n%s\n",
Jack Jansena547dca1996-07-10 15:48:25 +0000463 Py_GetVersion(), Py_GetCopyright());
Jack Jansen696c9581995-08-14 12:33:20 +0000464
465 if (filename != NULL) {
466 if ((fp = fopen(filename, "r")) == NULL) {
467 fprintf(stderr, "%s: can't open file '%s'\n",
468 argv[0], filename);
Jack Jansen0168f271995-10-27 13:32:30 +0000469 PyMac_Exit(2);
Jack Jansen696c9581995-08-14 12:33:20 +0000470 }
471 }
472
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000473 /* We initialize the menubar here, hoping SIOUX is initialized by now */
Jack Jansen3469e991996-09-06 00:30:45 +0000474 PyMac_InitMenuBar();
475
Jack Jansen696c9581995-08-14 12:33:20 +0000476 Py_Initialize();
477
Jack Jansen0c6d0372000-05-05 23:11:14 +0000478 PyMac_InstallNavServicesForSF();
479
Jack Jansen696c9581995-08-14 12:33:20 +0000480 PySys_SetArgv(argc-1, argv+1);
481
482 if (filename == NULL && isatty((int)fileno(fp))) {
483 FILE *fp = fopen(STARTUP, "r");
484 if (fp != NULL) {
485 (void) PyRun_SimpleFile(fp, STARTUP);
486 PyErr_Clear();
487 fclose(fp);
488 }
489 }
490 sts = PyRun_AnyFile(
491 fp, filename == NULL ? "<stdin>" : filename) != 0;
492 if (filename != NULL)
493 fclose(fp);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000494
495 if ( filename != NULL || command != NULL )
496 sts = (run_inspect() || sts);
Jack Jansen696c9581995-08-14 12:33:20 +0000497
498 Py_Exit(sts);
499 /*NOTREACHED*/
500}
501
Jack Jansen0168f271995-10-27 13:32:30 +0000502/*
503** Terminate application
504*/
Jack Jansen76ceece1996-08-19 11:18:24 +0000505void
Jack Jansen0168f271995-10-27 13:32:30 +0000506PyMac_Exit(status)
507 int status;
508{
509 int keep;
Jack Jansen8c693211997-01-07 16:19:42 +0000510
511#if __profile__ == 1
512 ProfilerDump("\pPython Profiler Results");
513 ProfilerTerm();
514#endif
Jack Jansen0168f271995-10-27 13:32:30 +0000515 if ( status )
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000516 keep = options.keep_error;
Jack Jansen0168f271995-10-27 13:32:30 +0000517 else
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000518 keep = options.keep_normal;
Jack Jansen0168f271995-10-27 13:32:30 +0000519
Jack Jansen1e8557a1995-11-10 14:51:26 +0000520#ifdef USE_SIOUX
521 if (keep) {
522 SIOUXSettings.standalone = 1;
523 SIOUXSettings.autocloseonquit = 0;
Jack Jansen415571c1996-03-25 15:46:03 +0000524 SIOUXSetTitle("\p\307terminated\310");
Jack Jansencaa7c461997-06-12 10:49:13 +0000525 PyMac_RestoreMenuBar();
Jack Jansene44545f1997-05-07 15:48:54 +0000526#ifdef USE_MSL
527 /*
528 ** Temporary workaround: autocloseonquit clearing does not
529 ** currently work for the MSL/GUSI combo.
530 */
531 while(getchar() > 0);
532#endif
Jack Jansen1e8557a1995-11-10 14:51:26 +0000533 }
Jack Jansen0168f271995-10-27 13:32:30 +0000534 else
535 SIOUXSettings.autocloseonquit = 1;
Jack Jansenf6865f71996-09-04 15:24:59 +0000536#endif /* USE_SIOUX */
Jack Jansen0168f271995-10-27 13:32:30 +0000537
538 exit(status);
539}
Jack Jansen696c9581995-08-14 12:33:20 +0000540
541/* Return the program name -- some code out there needs this. */
Jack Jansena39f1b01997-05-23 15:35:14 +0000542char *
543Py_GetProgramFullPath()
544{
Jack Jansen7330b391997-08-08 14:56:41 +0000545 return orig_argv[0];
Jack Jansena39f1b01997-05-23 15:35:14 +0000546}
547
Jack Jansen696c9581995-08-14 12:33:20 +0000548
549/* Make the *original* argc/argv available to other modules.
550 This is rare, but it is needed by the secureware extension. */
551
552void
Jack Jansen9ae898b2000-07-11 21:16:03 +0000553Py_GetArgcArgv(int *argc,char ***argv)
Jack Jansen696c9581995-08-14 12:33:20 +0000554{
555 *argc = orig_argc;
556 *argv = orig_argv;
Guido van Rossumb0f3c821994-08-23 13:34:25 +0000557}
Jack Jansen1d2f8631996-08-02 15:16:16 +0000558
559/* More cruft that shouldn't really be here, used in sysmodule.c */
560
561char *
562Py_GetPrefix()
563{
Jack Jansenac625691997-09-08 13:22:22 +0000564 return PyMac_GetPythonDir();
Jack Jansen1d2f8631996-08-02 15:16:16 +0000565}
566
567char *
568Py_GetExecPrefix()
569{
Jack Jansenac625691997-09-08 13:22:22 +0000570 return PyMac_GetPythonDir();
Jack Jansen1d2f8631996-08-02 15:16:16 +0000571}