blob: 903f292b51c5af1d132a245fac9998bf70858e04 [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 Jansen5bdbabd2000-07-24 19:52:52 +000053#ifdef USE_MAC_SHARED_LIBRARY
54extern PyMac_AddLibResources(void);
55#endif
Jack Jansenc76fd391995-02-02 14:27:31 +000056
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 Jansend88296d2000-07-11 19:51:05 +000069static void Py_Main(int, char **); /* Forward */
70void PyMac_Exit(int); /* Forward */
Jack Jansen76ceece1996-08-19 11:18:24 +000071
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{
Jack Jansen74a1e632000-07-14 22:37:27 +000092#if !TARGET_API_MAC_CARBON
Jack Jansenee6eeb12000-06-02 21:28:52 +000093 /* These aren't needed for carbon */
Jack Jansen01fbc681996-02-28 15:42:47 +000094 MaxApplZone();
95 InitGraf(&qd.thePort);
96 InitFonts();
97 InitWindows();
98 TEInit();
99 InitDialogs((long)0);
100 InitMenus();
Jack Jansenee6eeb12000-06-02 21:28:52 +0000101#endif
Jack Jansen01fbc681996-02-28 15:42:47 +0000102 InitCursor();
Jack Jansen8f5725a1999-12-07 23:08:10 +0000103 init_appearance();
Jack Jansen01fbc681996-02-28 15:42:47 +0000104}
105
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000106/*
107** PyMac_InteractiveOptions - Allow user to set options if option key is pressed
108*/
Jack Jansen01fbc681996-02-28 15:42:47 +0000109static void
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000110PyMac_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 Jansen08c3be31997-04-08 15:27:00 +0000143 SetControlValue(handle, (short)p->var);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000144
145 SET_OPT_ITEM(OPT_INSPECT, inspect);
146 SET_OPT_ITEM(OPT_VERBOSE, verbose);
Jack Jansen36b983c1997-09-09 13:53:21 +0000147 SET_OPT_ITEM(OPT_OPTIMIZE, optimize);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000148 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 Jansen0c6d0372000-05-05 23:11:14 +0000152 SET_OPT_ITEM(OPT_TABWARN, tabwarn);
Jack Jansen36b983c1997-09-09 13:53:21 +0000153 SET_OPT_ITEM(OPT_NOSITE, nosite);
Jack Jansen0c6d0372000-05-05 23:11:14 +0000154 SET_OPT_ITEM(OPT_NONAVSERV, nonavservice);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000155 /* 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 Jansen08c3be31997-04-08 15:27:00 +0000165 DisposeDialog(dialog);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000166 exit(0);
167 }
Jack Jansen74a1e632000-07-14 22:37:27 +0000168#if !TARGET_API_MAC_CARBON
Jack Jansen36b983c1997-09-09 13:53:21 +0000169 if ( item == OPT_HELP ) {
170 HMSetBalloons(!HMGetBalloons());
171 }
Jack Jansenee6eeb12000-06-02 21:28:52 +0000172#endif
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000173 if ( item == OPT_CMDLINE ) {
174 int new_argc, newer_argc;
175 char **new_argv, **newer_argv;
176
177 new_argc = ccommand(&new_argv);
178 newer_argc = (new_argc-1) + old_argc;
179 newer_argv = malloc((newer_argc+1)*sizeof(char *));
180 if( !newer_argv )
181 Py_FatalError("Cannot malloc argv\n");
182 for(i=0; i<old_argc; i++)
183 newer_argv[i] = (*argvp)[i];
184 for(i=old_argc; i<=newer_argc; i++) /* Copy the NULL too */
185 newer_argv[i] = new_argv[i-old_argc+1];
186 *argvp = newer_argv;
187 *argcp = newer_argc;
188
189 /* XXXX Is it not safe to use free() here, apparently */
190 }
191#define OPT_ITEM(num, var) \
192 if ( item == (num) ) { \
193 p->var = !p->var; \
194 GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
Jack Jansen08c3be31997-04-08 15:27:00 +0000195 SetControlValue(handle, (short)p->var); \
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000196 }
197
198 OPT_ITEM(OPT_INSPECT, inspect);
199 OPT_ITEM(OPT_VERBOSE, verbose);
Jack Jansen36b983c1997-09-09 13:53:21 +0000200 OPT_ITEM(OPT_OPTIMIZE, optimize);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000201 OPT_ITEM(OPT_UNBUFFERED, unbuffered);
202 OPT_ITEM(OPT_DEBUGGING, debugging);
203 OPT_ITEM(OPT_KEEPNORMAL, keep_normal);
204 OPT_ITEM(OPT_KEEPERROR, keep_error);
Jack Jansen0c6d0372000-05-05 23:11:14 +0000205 OPT_ITEM(OPT_TABWARN, tabwarn);
Jack Jansen36b983c1997-09-09 13:53:21 +0000206 OPT_ITEM(OPT_NOSITE, nosite);
Jack Jansen0c6d0372000-05-05 23:11:14 +0000207 OPT_ITEM(OPT_NONAVSERV, nonavservice);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000208
209#undef OPT_ITEM
210 }
Jack Jansen08c3be31997-04-08 15:27:00 +0000211 DisposeDialog(dialog);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000212}
213
214/*
215** Initialization code, shared by interpreter and applets
216*/
217static void
Jack Jansen52ac0371997-01-15 15:49:08 +0000218init_common(int *argcp, char ***argvp, int embedded)
Jack Jansen01fbc681996-02-28 15:42:47 +0000219{
Jack Jansen3f7d2b41996-09-06 22:21:07 +0000220 /* Remember resource fork refnum, for later */
221 PyMac_AppRefNum = CurResFile();
222
Jack Jansen01fbc681996-02-28 15:42:47 +0000223 /* Initialize toolboxes */
224 init_mac_world();
225
226#ifdef USE_MAC_SHARED_LIBRARY
227 /* Add the shared library to the stack of resource files */
Jack Jansen87c485c1998-07-31 09:38:01 +0000228 (void)PyMac_init_process_location();
Jack Jansen01fbc681996-02-28 15:42:47 +0000229 PyMac_AddLibResources();
230#endif
231
Jack Jansen2d1306b2000-04-07 09:10:49 +0000232#if defined(USE_GUSI1)
Jack Jansen01fbc681996-02-28 15:42:47 +0000233 /* Setup GUSI */
234 GUSIDefaultSetup();
Jack Jansenf6865f71996-09-04 15:24:59 +0000235 PyMac_SetGUSISpin();
Jack Jansen3f7d2b41996-09-06 22:21:07 +0000236 PyMac_SetGUSIOptions();
Jack Jansen01fbc681996-02-28 15:42:47 +0000237#endif
Jack Jansen2d1306b2000-04-07 09:10:49 +0000238#if defined(USE_GUSI)
239 atexit(PyMac_StopGUSISpin);
240#endif
Jack Jansen01fbc681996-02-28 15:42:47 +0000241
242#ifdef USE_SIOUX
243 /* Set various SIOUX flags. Some are changed later based on options */
Jack Jansencaa7c461997-06-12 10:49:13 +0000244/* SIOUXSettings.standalone = 0; /* XXXX Attempting to keep sioux from eating events */
Jack Jansen01fbc681996-02-28 15:42:47 +0000245 SIOUXSettings.asktosaveonclose = 0;
246 SIOUXSettings.showstatusline = 0;
247 SIOUXSettings.tabspaces = 4;
248#endif
249
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000250 /* Get options from preference file (or from applet resource fork) */
251 options.keep_error = 1; /* default-default */
252 PyMac_PreferenceOptions(&options);
253
Jack Jansen52ac0371997-01-15 15:49:08 +0000254 if ( embedded ) {
255 static char *emb_argv[] = {"embedded-python", 0};
256
257 *argcp = 1;
258 *argvp = emb_argv;
259 } else {
260 /* Create argc/argv. Do it before we go into the options event loop. */
261 *argcp = PyMac_GetArgv(argvp, options.noargs);
Jack Jansen660bb1d2000-07-18 09:40:39 +0000262#ifdef USE_ARGV0_CHDIR
263 printf("argc=%d, argv[0]=%x=%s\n", *argcp, (*argvp)[0], (*argvp)[0]);
264 if (*argcp >= 1 && (*argvp)[0] && (*argvp)[0][0]) {
265 /* Workaround for MacOS X, which currently (DP4) doesn't set
266 ** the working folder correctly
267 */
268 char app_wd[256], *p;
269
270 strncpy(app_wd, (*argvp)[0], 256);
271 printf("Modifying dir, argv[0]=%s\n", (*argvp)[0]);
272 p = strrchr(app_wd, ':');
273 if ( p ) *p = 0;
274 printf("app_wd=%s\n", app_wd);
275 chdir(app_wd);
276 }
277#endif
Jack Jansen52ac0371997-01-15 15:49:08 +0000278 /* Do interactive option setting, if allowed and <option> depressed */
279 PyMac_InteractiveOptions(&options, argcp, argvp);
280 }
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000281
282 /* Copy selected options to where the machine-independent stuff wants it */
283 Py_VerboseFlag = options.verbose;
Jack Jansen7330b391997-08-08 14:56:41 +0000284/* Py_SuppressPrintingFlag = options.suppress_print; */
Jack Jansen36b983c1997-09-09 13:53:21 +0000285 Py_OptimizeFlag = options.optimize;
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000286 Py_DebugFlag = options.debugging;
Jack Jansena7a89eb1997-10-07 21:48:57 +0000287 Py_NoSiteFlag = options.nosite;
Jack Jansen0c6d0372000-05-05 23:11:14 +0000288 Py_TabcheckFlag = options.tabwarn;
Jack Jansene3ae0df1997-06-03 15:28:29 +0000289 if ( options.noargs ) {
290 /* don't process events at all without the scripts permission */
291 PyMacSchedParams scp;
292
293 PyMac_GetSchedParams(&scp);
294 scp.process_events = 0;
295 /* Should we disable command-dot as well? */
296 PyMac_SetSchedParams(&scp);
297 }
Jack Jansen36b983c1997-09-09 13:53:21 +0000298 /* XXXX dispatch oldexc and nosite */
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000299
300 /* Set buffering */
301 if (options.unbuffered) {
302#ifndef MPW
303 setbuf(stdout, (char *)NULL);
304 setbuf(stderr, (char *)NULL);
305#else
306 /* On MPW (3.2) unbuffered seems to hang */
307 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
308 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
309#endif
310 }
Jack Jansen8c693211997-01-07 16:19:42 +0000311#if __profile__ == 1
312 /* collectSummary or collectDetailed, timebase, #routines, max stack depth */
Jack Jansene7424871999-09-30 11:20:11 +0000313 ProfilerInit(collectSummary, bestTimeBase, 8000, 250);
Jack Jansen8c693211997-01-07 16:19:42 +0000314#endif
Jack Jansen7330b391997-08-08 14:56:41 +0000315
316 /* Tell the rest of python about our argc/argv */
317 orig_argc = *argcp; /* For Py_GetArgcArgv() */
318 orig_argv = *argvp;
319 Py_SetProgramName((*argvp)[0]);
Jack Jansen01fbc681996-02-28 15:42:47 +0000320}
321
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000322/*
323** Inspection mode after script/applet termination
324*/
325static int
326run_inspect()
327{
328 int sts = 0;
329
330 if (options.inspect && isatty((int)fileno(stdin)))
331 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
332 return sts;
333}
Jack Jansen01fbc681996-02-28 15:42:47 +0000334
Jack Jansen0c6d0372000-05-05 23:11:14 +0000335/*
336** Import the macfsn module, which will override the Standard File
337** calls in the macfs builtin module by Navigation Services versions,
338** if available on this machine.
339*/
340static void
341PyMac_InstallNavServicesForSF()
342{
343 if ( !options.nonavservice ) {
344 PyObject *m = PyImport_ImportModule("macfsn");
345
346 if ( m == NULL ) {
347 PySys_WriteStderr("'import macfsn' failed; ");
348 if (Py_VerboseFlag) {
349 PySys_WriteStderr("traceback:\n");
350 PyErr_Print();
351 }
352 else {
353 PySys_WriteStderr("use -v for traceback\n");
354 }
355 }
356 }
357}
358
Jack Jansen696c9581995-08-14 12:33:20 +0000359#ifdef USE_MAC_APPLET_SUPPORT
360/* Applet support */
361
362/* Run a compiled Python Python script from 'PYC ' resource __main__ */
363static int
364run_main_resource()
365{
366 Handle h;
367 long size;
368 PyObject *code;
369 PyObject *result;
370
371 h = GetNamedResource('PYC ', "\p__main__");
372 if (h == NULL) {
373 Alert(NOPYC_ALERT, NULL);
374 return 1;
375 }
376 size = GetResourceSizeOnDisk(h);
377 HLock(h);
378 code = PyMarshal_ReadObjectFromString(*h + 8, (int)(size - 8));
379 HUnlock(h);
380 ReleaseResource(h);
381 if (code == NULL) {
382 PyErr_Print();
383 return 1;
384 }
385 result = PyImport_ExecCodeModule("__main__", code);
386 Py_DECREF(code);
387 if (result == NULL) {
388 PyErr_Print();
389 return 1;
390 }
391 Py_DECREF(result);
392 return 0;
393}
394
395/* Initialization sequence for applets */
396void
397PyMac_InitApplet()
398{
Guido van Rossumb0f3c821994-08-23 13:34:25 +0000399 int argc;
400 char **argv;
Jack Jansen696c9581995-08-14 12:33:20 +0000401 int err;
402
Jack Jansen52ac0371997-01-15 15:49:08 +0000403 init_common(&argc, &argv, 0);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000404
Jack Jansen696c9581995-08-14 12:33:20 +0000405 Py_Initialize();
Jack Jansen0c6d0372000-05-05 23:11:14 +0000406 PyMac_InstallNavServicesForSF();
Jack Jansen696c9581995-08-14 12:33:20 +0000407 PySys_SetArgv(argc, argv);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000408
Jack Jansen696c9581995-08-14 12:33:20 +0000409 err = run_main_resource();
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000410
411 err = (run_inspect() || err);
412
Jack Jansen696c9581995-08-14 12:33:20 +0000413 fflush(stderr);
414 fflush(stdout);
Jack Jansen0168f271995-10-27 13:32:30 +0000415 PyMac_Exit(err);
Jack Jansen696c9581995-08-14 12:33:20 +0000416 /* XXX Should we bother to Py_Exit(sts)? */
417}
418
Jack Jansen52ac0371997-01-15 15:49:08 +0000419/*
420** Hook for embedding python.
421*/
422void
423PyMac_Initialize()
424{
425 int argc;
426 char **argv;
427
428 init_common(&argc, &argv, 1);
429 Py_Initialize();
Jack Jansen0c6d0372000-05-05 23:11:14 +0000430 PyMac_InstallNavServicesForSF();
Jack Jansen52ac0371997-01-15 15:49:08 +0000431 PySys_SetArgv(argc, argv);
432}
433
Jack Jansen696c9581995-08-14 12:33:20 +0000434#endif /* USE_MAC_APPLET_SUPPORT */
435
436/* For normal application */
437void
438PyMac_InitApplication()
439{
440 int argc;
441 char **argv;
442
Jack Jansen52ac0371997-01-15 15:49:08 +0000443 init_common(&argc, &argv, 0);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000444
Jack Jansen696c9581995-08-14 12:33:20 +0000445 if ( argc > 1 ) {
446 /* We're running a script. Attempt to change current directory */
447 char curwd[256], *endp;
448
449 strcpy(curwd, argv[1]);
450 endp = strrchr(curwd, ':');
451 if ( endp && endp > curwd ) {
452 *endp = '\0';
453
454 chdir(curwd);
Jack Jansen2d1306b2000-04-07 09:10:49 +0000455#ifdef USE_GUSI1
Jack Jansen378815c1996-03-06 16:21:34 +0000456 /* Change MacOS's idea of wd too */
457 PyMac_FixGUSIcd();
458#endif
Jack Jansen696c9581995-08-14 12:33:20 +0000459 }
460 }
461 Py_Main(argc, argv);
462}
463
Jack Jansen696c9581995-08-14 12:33:20 +0000464/* Main program */
465
Jack Jansen76ceece1996-08-19 11:18:24 +0000466static void
Jack Jansen696c9581995-08-14 12:33:20 +0000467Py_Main(argc, argv)
468 int argc;
469 char **argv;
470{
Jack Jansen696c9581995-08-14 12:33:20 +0000471 int sts;
472 char *command = NULL;
473 char *filename = NULL;
474 FILE *fp = stdin;
Jack Jansen696c9581995-08-14 12:33:20 +0000475
Jack Jansen696c9581995-08-14 12:33:20 +0000476 filename = argv[1];
477
478 if (Py_VerboseFlag ||
479 command == NULL && filename == NULL && isatty((int)fileno(fp)))
480 fprintf(stderr, "Python %s\n%s\n",
Jack Jansena547dca1996-07-10 15:48:25 +0000481 Py_GetVersion(), Py_GetCopyright());
Jack Jansen696c9581995-08-14 12:33:20 +0000482
483 if (filename != NULL) {
484 if ((fp = fopen(filename, "r")) == NULL) {
485 fprintf(stderr, "%s: can't open file '%s'\n",
486 argv[0], filename);
Jack Jansen0168f271995-10-27 13:32:30 +0000487 PyMac_Exit(2);
Jack Jansen696c9581995-08-14 12:33:20 +0000488 }
489 }
490
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000491 /* We initialize the menubar here, hoping SIOUX is initialized by now */
Jack Jansen3469e991996-09-06 00:30:45 +0000492 PyMac_InitMenuBar();
493
Jack Jansen696c9581995-08-14 12:33:20 +0000494 Py_Initialize();
495
Jack Jansen0c6d0372000-05-05 23:11:14 +0000496 PyMac_InstallNavServicesForSF();
497
Jack Jansen696c9581995-08-14 12:33:20 +0000498 PySys_SetArgv(argc-1, argv+1);
499
500 if (filename == NULL && isatty((int)fileno(fp))) {
501 FILE *fp = fopen(STARTUP, "r");
502 if (fp != NULL) {
503 (void) PyRun_SimpleFile(fp, STARTUP);
504 PyErr_Clear();
505 fclose(fp);
506 }
507 }
508 sts = PyRun_AnyFile(
509 fp, filename == NULL ? "<stdin>" : filename) != 0;
510 if (filename != NULL)
511 fclose(fp);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000512
513 if ( filename != NULL || command != NULL )
514 sts = (run_inspect() || sts);
Jack Jansen696c9581995-08-14 12:33:20 +0000515
516 Py_Exit(sts);
517 /*NOTREACHED*/
518}
519
Jack Jansen0168f271995-10-27 13:32:30 +0000520/*
521** Terminate application
522*/
Jack Jansen76ceece1996-08-19 11:18:24 +0000523void
Jack Jansen0168f271995-10-27 13:32:30 +0000524PyMac_Exit(status)
525 int status;
526{
527 int keep;
Jack Jansen8c693211997-01-07 16:19:42 +0000528
529#if __profile__ == 1
530 ProfilerDump("\pPython Profiler Results");
531 ProfilerTerm();
532#endif
Jack Jansen0168f271995-10-27 13:32:30 +0000533 if ( status )
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000534 keep = options.keep_error;
Jack Jansen0168f271995-10-27 13:32:30 +0000535 else
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000536 keep = options.keep_normal;
Jack Jansen0168f271995-10-27 13:32:30 +0000537
Jack Jansen1e8557a1995-11-10 14:51:26 +0000538#ifdef USE_SIOUX
539 if (keep) {
540 SIOUXSettings.standalone = 1;
541 SIOUXSettings.autocloseonquit = 0;
Jack Jansen415571c1996-03-25 15:46:03 +0000542 SIOUXSetTitle("\p\307terminated\310");
Jack Jansencaa7c461997-06-12 10:49:13 +0000543 PyMac_RestoreMenuBar();
Jack Jansene44545f1997-05-07 15:48:54 +0000544#ifdef USE_MSL
545 /*
546 ** Temporary workaround: autocloseonquit clearing does not
547 ** currently work for the MSL/GUSI combo.
548 */
549 while(getchar() > 0);
550#endif
Jack Jansen1e8557a1995-11-10 14:51:26 +0000551 }
Jack Jansen0168f271995-10-27 13:32:30 +0000552 else
553 SIOUXSettings.autocloseonquit = 1;
Jack Jansenf6865f71996-09-04 15:24:59 +0000554#endif /* USE_SIOUX */
Jack Jansen0168f271995-10-27 13:32:30 +0000555
556 exit(status);
557}
Jack Jansen696c9581995-08-14 12:33:20 +0000558
559/* Return the program name -- some code out there needs this. */
Jack Jansena39f1b01997-05-23 15:35:14 +0000560char *
561Py_GetProgramFullPath()
562{
Jack Jansen7330b391997-08-08 14:56:41 +0000563 return orig_argv[0];
Jack Jansena39f1b01997-05-23 15:35:14 +0000564}
565
Jack Jansen696c9581995-08-14 12:33:20 +0000566
567/* Make the *original* argc/argv available to other modules.
568 This is rare, but it is needed by the secureware extension. */
569
570void
Jack Jansen9ae898b2000-07-11 21:16:03 +0000571Py_GetArgcArgv(int *argc,char ***argv)
Jack Jansen696c9581995-08-14 12:33:20 +0000572{
573 *argc = orig_argc;
574 *argv = orig_argv;
Guido van Rossumb0f3c821994-08-23 13:34:25 +0000575}
Jack Jansen1d2f8631996-08-02 15:16:16 +0000576
577/* More cruft that shouldn't really be here, used in sysmodule.c */
578
579char *
580Py_GetPrefix()
581{
Jack Jansenac625691997-09-08 13:22:22 +0000582 return PyMac_GetPythonDir();
Jack Jansen1d2f8631996-08-02 15:16:16 +0000583}
584
585char *
586Py_GetExecPrefix()
587{
Jack Jansenac625691997-09-08 13:22:22 +0000588 return PyMac_GetPythonDir();
Jack Jansen1d2f8631996-08-02 15:16:16 +0000589}