blob: c46fd2f62e0e8e748cbc579d0599ba5ba80302a9 [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 Jansen65c3ee02000-09-08 10:20:37 +000059#define COPYRIGHT \
60 "Type \"copyright\", \"credits\" or \"license\" for more information."
61
62
Jack Jansen696c9581995-08-14 12:33:20 +000063extern int Py_DebugFlag; /* For parser.c, declared in pythonrun.c */
64extern int Py_VerboseFlag; /* For import.c, declared in pythonrun.c */
Jack Jansen3f7d2b41996-09-06 22:21:07 +000065short PyMac_AppRefNum; /* RefNum of application resource fork */
Jack Jansen696c9581995-08-14 12:33:20 +000066
Jack Jansen1d2f8631996-08-02 15:16:16 +000067/* For Py_GetArgcArgv(); set by main() */
Jack Jansen696c9581995-08-14 12:33:20 +000068static char **orig_argv;
69static int orig_argc;
70
Jack Jansen7d5f9e81996-09-07 17:09:31 +000071PyMac_PrefRecord options;
Jack Jansen0168f271995-10-27 13:32:30 +000072
Jack Jansend88296d2000-07-11 19:51:05 +000073static void Py_Main(int, char **); /* Forward */
74void PyMac_Exit(int); /* Forward */
Jack Jansen76ceece1996-08-19 11:18:24 +000075
Jack Jansen8f5725a1999-12-07 23:08:10 +000076static void init_appearance()
77{
78#ifdef USE_APPEARANCE
79 OSErr err;
80 SInt32 response;
81
82 err = Gestalt(gestaltAppearanceAttr,&response);
83 if ( err ) goto no_appearance;
84 if ( !(response&(1<<gestaltAppearanceExists)) ) goto no_appearance;
85 /* XXXX Should we check the version? Compat-mode? */
86 PyMac_AppearanceCompliant = 1;
87no_appearance:
88 return;
89#endif /* USE_APPEARANCE */
90}
Jack Jansen01fbc681996-02-28 15:42:47 +000091/* Initialize the Mac toolbox world */
92
93static void
94init_mac_world()
95{
Jack Jansen74a1e632000-07-14 22:37:27 +000096#if !TARGET_API_MAC_CARBON
Jack Jansenee6eeb12000-06-02 21:28:52 +000097 /* These aren't needed for carbon */
Jack Jansen01fbc681996-02-28 15:42:47 +000098 MaxApplZone();
99 InitGraf(&qd.thePort);
100 InitFonts();
101 InitWindows();
102 TEInit();
103 InitDialogs((long)0);
104 InitMenus();
Jack Jansenee6eeb12000-06-02 21:28:52 +0000105#endif
Jack Jansen01fbc681996-02-28 15:42:47 +0000106 InitCursor();
Jack Jansen8f5725a1999-12-07 23:08:10 +0000107 init_appearance();
Jack Jansen01fbc681996-02-28 15:42:47 +0000108}
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 Jansen74a1e632000-07-14 22:37:27 +0000172#if !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);
Jack Jansen660bb1d2000-07-18 09:40:39 +0000266#ifdef USE_ARGV0_CHDIR
Jack Jansen660bb1d2000-07-18 09:40:39 +0000267 if (*argcp >= 1 && (*argvp)[0] && (*argvp)[0][0]) {
268 /* Workaround for MacOS X, which currently (DP4) doesn't set
269 ** the working folder correctly
270 */
271 char app_wd[256], *p;
272
273 strncpy(app_wd, (*argvp)[0], 256);
Jack Jansen660bb1d2000-07-18 09:40:39 +0000274 p = strrchr(app_wd, ':');
275 if ( p ) *p = 0;
Jack Jansen660bb1d2000-07-18 09:40:39 +0000276 chdir(app_wd);
277 }
278#endif
Jack Jansen52ac0371997-01-15 15:49:08 +0000279 /* Do interactive option setting, if allowed and <option> depressed */
280 PyMac_InteractiveOptions(&options, argcp, argvp);
281 }
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000282
283 /* Copy selected options to where the machine-independent stuff wants it */
Jack Jansen65c3ee02000-09-08 10:20:37 +0000284 Py_VerboseFlag = options.verbose *2;
Jack Jansen7330b391997-08-08 14:56:41 +0000285/* Py_SuppressPrintingFlag = options.suppress_print; */
Jack Jansen36b983c1997-09-09 13:53:21 +0000286 Py_OptimizeFlag = options.optimize;
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000287 Py_DebugFlag = options.debugging;
Jack Jansena7a89eb1997-10-07 21:48:57 +0000288 Py_NoSiteFlag = options.nosite;
Jack Jansen0c6d0372000-05-05 23:11:14 +0000289 Py_TabcheckFlag = options.tabwarn;
Jack Jansene3ae0df1997-06-03 15:28:29 +0000290 if ( options.noargs ) {
291 /* don't process events at all without the scripts permission */
292 PyMacSchedParams scp;
293
294 PyMac_GetSchedParams(&scp);
295 scp.process_events = 0;
296 /* Should we disable command-dot as well? */
297 PyMac_SetSchedParams(&scp);
298 }
Jack Jansen36b983c1997-09-09 13:53:21 +0000299 /* XXXX dispatch oldexc and nosite */
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000300
301 /* Set buffering */
302 if (options.unbuffered) {
303#ifndef MPW
304 setbuf(stdout, (char *)NULL);
305 setbuf(stderr, (char *)NULL);
306#else
307 /* On MPW (3.2) unbuffered seems to hang */
308 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
309 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
310#endif
311 }
Jack Jansen8c693211997-01-07 16:19:42 +0000312#if __profile__ == 1
313 /* collectSummary or collectDetailed, timebase, #routines, max stack depth */
Jack Jansene7424871999-09-30 11:20:11 +0000314 ProfilerInit(collectSummary, bestTimeBase, 8000, 250);
Jack Jansen8c693211997-01-07 16:19:42 +0000315#endif
Jack Jansen7330b391997-08-08 14:56:41 +0000316
317 /* Tell the rest of python about our argc/argv */
318 orig_argc = *argcp; /* For Py_GetArgcArgv() */
319 orig_argv = *argvp;
320 Py_SetProgramName((*argvp)[0]);
Jack Jansen01fbc681996-02-28 15:42:47 +0000321}
322
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000323/*
324** Inspection mode after script/applet termination
325*/
326static int
327run_inspect()
328{
329 int sts = 0;
330
331 if (options.inspect && isatty((int)fileno(stdin)))
332 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
333 return sts;
334}
Jack Jansen01fbc681996-02-28 15:42:47 +0000335
Jack Jansen0c6d0372000-05-05 23:11:14 +0000336/*
337** Import the macfsn module, which will override the Standard File
338** calls in the macfs builtin module by Navigation Services versions,
339** if available on this machine.
340*/
341static void
342PyMac_InstallNavServicesForSF()
343{
344 if ( !options.nonavservice ) {
345 PyObject *m = PyImport_ImportModule("macfsn");
346
347 if ( m == NULL ) {
348 PySys_WriteStderr("'import macfsn' failed; ");
349 if (Py_VerboseFlag) {
350 PySys_WriteStderr("traceback:\n");
351 PyErr_Print();
352 }
353 else {
354 PySys_WriteStderr("use -v for traceback\n");
355 }
356 }
357 }
358}
359
Jack Jansen696c9581995-08-14 12:33:20 +0000360#ifdef USE_MAC_APPLET_SUPPORT
361/* Applet support */
362
363/* Run a compiled Python Python script from 'PYC ' resource __main__ */
364static int
365run_main_resource()
366{
367 Handle h;
368 long size;
369 PyObject *code;
370 PyObject *result;
371
372 h = GetNamedResource('PYC ', "\p__main__");
373 if (h == NULL) {
374 Alert(NOPYC_ALERT, NULL);
375 return 1;
376 }
377 size = GetResourceSizeOnDisk(h);
378 HLock(h);
379 code = PyMarshal_ReadObjectFromString(*h + 8, (int)(size - 8));
380 HUnlock(h);
381 ReleaseResource(h);
382 if (code == NULL) {
383 PyErr_Print();
384 return 1;
385 }
386 result = PyImport_ExecCodeModule("__main__", code);
387 Py_DECREF(code);
388 if (result == NULL) {
389 PyErr_Print();
390 return 1;
391 }
392 Py_DECREF(result);
393 return 0;
394}
395
396/* Initialization sequence for applets */
397void
398PyMac_InitApplet()
399{
Guido van Rossumb0f3c821994-08-23 13:34:25 +0000400 int argc;
401 char **argv;
Jack Jansen696c9581995-08-14 12:33:20 +0000402 int err;
403
Jack Jansen52ac0371997-01-15 15:49:08 +0000404 init_common(&argc, &argv, 0);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000405
Jack Jansen696c9581995-08-14 12:33:20 +0000406 Py_Initialize();
Jack Jansen0c6d0372000-05-05 23:11:14 +0000407 PyMac_InstallNavServicesForSF();
Jack Jansen696c9581995-08-14 12:33:20 +0000408 PySys_SetArgv(argc, argv);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000409
Jack Jansen696c9581995-08-14 12:33:20 +0000410 err = run_main_resource();
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000411
412 err = (run_inspect() || err);
413
Jack Jansen696c9581995-08-14 12:33:20 +0000414 fflush(stderr);
415 fflush(stdout);
Jack Jansen0168f271995-10-27 13:32:30 +0000416 PyMac_Exit(err);
Jack Jansen696c9581995-08-14 12:33:20 +0000417 /* XXX Should we bother to Py_Exit(sts)? */
418}
419
Jack Jansen52ac0371997-01-15 15:49:08 +0000420/*
421** Hook for embedding python.
422*/
423void
424PyMac_Initialize()
425{
426 int argc;
427 char **argv;
428
429 init_common(&argc, &argv, 1);
430 Py_Initialize();
Jack Jansen0c6d0372000-05-05 23:11:14 +0000431 PyMac_InstallNavServicesForSF();
Jack Jansen52ac0371997-01-15 15:49:08 +0000432 PySys_SetArgv(argc, argv);
433}
434
Jack Jansen696c9581995-08-14 12:33:20 +0000435#endif /* USE_MAC_APPLET_SUPPORT */
436
437/* For normal application */
438void
439PyMac_InitApplication()
440{
441 int argc;
442 char **argv;
443
Jack Jansen52ac0371997-01-15 15:49:08 +0000444 init_common(&argc, &argv, 0);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000445
Jack Jansen696c9581995-08-14 12:33:20 +0000446 if ( argc > 1 ) {
447 /* We're running a script. Attempt to change current directory */
448 char curwd[256], *endp;
449
450 strcpy(curwd, argv[1]);
451 endp = strrchr(curwd, ':');
452 if ( endp && endp > curwd ) {
453 *endp = '\0';
454
455 chdir(curwd);
Jack Jansen2d1306b2000-04-07 09:10:49 +0000456#ifdef USE_GUSI1
Jack Jansen378815c1996-03-06 16:21:34 +0000457 /* Change MacOS's idea of wd too */
458 PyMac_FixGUSIcd();
459#endif
Jack Jansen696c9581995-08-14 12:33:20 +0000460 }
461 }
462 Py_Main(argc, argv);
463}
464
Jack Jansen696c9581995-08-14 12:33:20 +0000465/* Main program */
466
Jack Jansen76ceece1996-08-19 11:18:24 +0000467static void
Jack Jansen696c9581995-08-14 12:33:20 +0000468Py_Main(argc, argv)
469 int argc;
470 char **argv;
471{
Jack Jansen696c9581995-08-14 12:33:20 +0000472 int sts;
473 char *command = NULL;
474 char *filename = NULL;
475 FILE *fp = stdin;
Jack Jansen696c9581995-08-14 12:33:20 +0000476
Jack Jansen696c9581995-08-14 12:33:20 +0000477 filename = argv[1];
478
479 if (Py_VerboseFlag ||
480 command == NULL && filename == NULL && isatty((int)fileno(fp)))
Jack Jansen65c3ee02000-09-08 10:20:37 +0000481 fprintf(stderr, "Python %s on %s\n%s\n",
482 Py_GetVersion(), Py_GetPlatform(), COPYRIGHT);
Jack Jansen696c9581995-08-14 12:33:20 +0000483
484 if (filename != NULL) {
485 if ((fp = fopen(filename, "r")) == NULL) {
486 fprintf(stderr, "%s: can't open file '%s'\n",
487 argv[0], filename);
Jack Jansen0168f271995-10-27 13:32:30 +0000488 PyMac_Exit(2);
Jack Jansen696c9581995-08-14 12:33:20 +0000489 }
490 }
491
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000492 /* We initialize the menubar here, hoping SIOUX is initialized by now */
Jack Jansen3469e991996-09-06 00:30:45 +0000493 PyMac_InitMenuBar();
494
Jack Jansen696c9581995-08-14 12:33:20 +0000495 Py_Initialize();
496
Jack Jansen0c6d0372000-05-05 23:11:14 +0000497 PyMac_InstallNavServicesForSF();
498
Jack Jansen696c9581995-08-14 12:33:20 +0000499 PySys_SetArgv(argc-1, argv+1);
500
501 if (filename == NULL && isatty((int)fileno(fp))) {
502 FILE *fp = fopen(STARTUP, "r");
503 if (fp != NULL) {
504 (void) PyRun_SimpleFile(fp, STARTUP);
505 PyErr_Clear();
506 fclose(fp);
507 }
508 }
509 sts = PyRun_AnyFile(
510 fp, filename == NULL ? "<stdin>" : filename) != 0;
511 if (filename != NULL)
512 fclose(fp);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000513
514 if ( filename != NULL || command != NULL )
515 sts = (run_inspect() || sts);
Jack Jansen696c9581995-08-14 12:33:20 +0000516
517 Py_Exit(sts);
518 /*NOTREACHED*/
519}
520
Jack Jansen0168f271995-10-27 13:32:30 +0000521/*
522** Terminate application
523*/
Jack Jansen76ceece1996-08-19 11:18:24 +0000524void
Jack Jansen0168f271995-10-27 13:32:30 +0000525PyMac_Exit(status)
526 int status;
527{
528 int keep;
Jack Jansen8c693211997-01-07 16:19:42 +0000529
530#if __profile__ == 1
531 ProfilerDump("\pPython Profiler Results");
532 ProfilerTerm();
533#endif
Jack Jansen0168f271995-10-27 13:32:30 +0000534 if ( status )
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000535 keep = options.keep_error;
Jack Jansen0168f271995-10-27 13:32:30 +0000536 else
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000537 keep = options.keep_normal;
Jack Jansen0168f271995-10-27 13:32:30 +0000538
Jack Jansen1e8557a1995-11-10 14:51:26 +0000539#ifdef USE_SIOUX
540 if (keep) {
541 SIOUXSettings.standalone = 1;
542 SIOUXSettings.autocloseonquit = 0;
Jack Jansen415571c1996-03-25 15:46:03 +0000543 SIOUXSetTitle("\p\307terminated\310");
Jack Jansencaa7c461997-06-12 10:49:13 +0000544 PyMac_RestoreMenuBar();
Jack Jansene44545f1997-05-07 15:48:54 +0000545#ifdef USE_MSL
546 /*
547 ** Temporary workaround: autocloseonquit clearing does not
548 ** currently work for the MSL/GUSI combo.
549 */
550 while(getchar() > 0);
551#endif
Jack Jansen1e8557a1995-11-10 14:51:26 +0000552 }
Jack Jansen0168f271995-10-27 13:32:30 +0000553 else
554 SIOUXSettings.autocloseonquit = 1;
Jack Jansenf6865f71996-09-04 15:24:59 +0000555#endif /* USE_SIOUX */
Jack Jansen0168f271995-10-27 13:32:30 +0000556
557 exit(status);
558}
Jack Jansen696c9581995-08-14 12:33:20 +0000559
560/* Return the program name -- some code out there needs this. */
Jack Jansena39f1b01997-05-23 15:35:14 +0000561char *
562Py_GetProgramFullPath()
563{
Jack Jansen7330b391997-08-08 14:56:41 +0000564 return orig_argv[0];
Jack Jansena39f1b01997-05-23 15:35:14 +0000565}
566
Jack Jansen696c9581995-08-14 12:33:20 +0000567
568/* Make the *original* argc/argv available to other modules.
569 This is rare, but it is needed by the secureware extension. */
570
571void
Jack Jansen9ae898b2000-07-11 21:16:03 +0000572Py_GetArgcArgv(int *argc,char ***argv)
Jack Jansen696c9581995-08-14 12:33:20 +0000573{
574 *argc = orig_argc;
575 *argv = orig_argv;
Guido van Rossumb0f3c821994-08-23 13:34:25 +0000576}
Jack Jansen1d2f8631996-08-02 15:16:16 +0000577
578/* More cruft that shouldn't really be here, used in sysmodule.c */
579
580char *
581Py_GetPrefix()
582{
Jack Jansenac625691997-09-08 13:22:22 +0000583 return PyMac_GetPythonDir();
Jack Jansen1d2f8631996-08-02 15:16:16 +0000584}
585
586char *
587Py_GetExecPrefix()
588{
Jack Jansenac625691997-09-08 13:22:22 +0000589 return PyMac_GetPythonDir();
Jack Jansen1d2f8631996-08-02 15:16:16 +0000590}