blob: 3531f4c2bac68e008c8fc1320094f0bc133c270d [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 Jansen4a5eb962000-09-22 21:50:11 +000056#ifdef USE_GUSI
57#include "GUSISIOUX.h"
58#endif
Jack Jansenc76fd391995-02-02 14:27:31 +000059
Jack Jansen696c9581995-08-14 12:33:20 +000060#define STARTUP "PythonStartup"
Jack Jansenbac428d1994-12-14 13:47:30 +000061
Jack Jansen65c3ee02000-09-08 10:20:37 +000062#define COPYRIGHT \
63 "Type \"copyright\", \"credits\" or \"license\" for more information."
64
65
Jack Jansen696c9581995-08-14 12:33:20 +000066extern int Py_DebugFlag; /* For parser.c, declared in pythonrun.c */
67extern int Py_VerboseFlag; /* For import.c, declared in pythonrun.c */
Jack Jansen3f7d2b41996-09-06 22:21:07 +000068short PyMac_AppRefNum; /* RefNum of application resource fork */
Jack Jansen696c9581995-08-14 12:33:20 +000069
Jack Jansen1d2f8631996-08-02 15:16:16 +000070/* For Py_GetArgcArgv(); set by main() */
Jack Jansen696c9581995-08-14 12:33:20 +000071static char **orig_argv;
72static int orig_argc;
73
Jack Jansen7d5f9e81996-09-07 17:09:31 +000074PyMac_PrefRecord options;
Jack Jansen0168f271995-10-27 13:32:30 +000075
Jack Jansend88296d2000-07-11 19:51:05 +000076static void Py_Main(int, char **); /* Forward */
77void PyMac_Exit(int); /* Forward */
Jack Jansen76ceece1996-08-19 11:18:24 +000078
Jack Jansen8f5725a1999-12-07 23:08:10 +000079static void init_appearance()
80{
81#ifdef USE_APPEARANCE
82 OSErr err;
83 SInt32 response;
84
85 err = Gestalt(gestaltAppearanceAttr,&response);
86 if ( err ) goto no_appearance;
87 if ( !(response&(1<<gestaltAppearanceExists)) ) goto no_appearance;
88 /* XXXX Should we check the version? Compat-mode? */
89 PyMac_AppearanceCompliant = 1;
90no_appearance:
91 return;
92#endif /* USE_APPEARANCE */
93}
Jack Jansen01fbc681996-02-28 15:42:47 +000094/* Initialize the Mac toolbox world */
95
96static void
97init_mac_world()
98{
Jack Jansen74a1e632000-07-14 22:37:27 +000099#if !TARGET_API_MAC_CARBON
Jack Jansenee6eeb12000-06-02 21:28:52 +0000100 /* These aren't needed for carbon */
Jack Jansen01fbc681996-02-28 15:42:47 +0000101 MaxApplZone();
102 InitGraf(&qd.thePort);
103 InitFonts();
104 InitWindows();
105 TEInit();
106 InitDialogs((long)0);
107 InitMenus();
Jack Jansenee6eeb12000-06-02 21:28:52 +0000108#endif
Jack Jansen01fbc681996-02-28 15:42:47 +0000109 InitCursor();
Jack Jansen8f5725a1999-12-07 23:08:10 +0000110 init_appearance();
Jack Jansen01fbc681996-02-28 15:42:47 +0000111}
112
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000113/*
114** PyMac_InteractiveOptions - Allow user to set options if option key is pressed
115*/
Jack Jansen01fbc681996-02-28 15:42:47 +0000116static void
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000117PyMac_InteractiveOptions(PyMac_PrefRecord *p, int *argcp, char ***argvp)
118{
119 KeyMap rmap;
120 unsigned char *map;
121 short item, type;
122 ControlHandle handle;
123 DialogPtr dialog;
124 Rect rect;
125 int old_argc = *argcp;
126 int i;
127
128 /*
129 ** If the preferences disallows interactive options we return,
130 ** similarly of <option> isn't pressed.
131 */
132 if (p->nointopt) return;
133
134 GetKeys(rmap);
135 map = (unsigned char *)rmap;
136 if ( ( map[0x3a>>3] & (1<<(0x3a&7)) ) == 0 ) /* option key is 3a */
137 return;
138
139 dialog = GetNewDialog(OPT_DIALOG, NULL, (WindowPtr)-1);
140 if ( dialog == NULL ) {
141 printf("Option dialog not found - cannot set options\n");
142 return;
143 }
144 SetDialogDefaultItem(dialog, OPT_OK);
145 SetDialogCancelItem(dialog, OPT_CANCEL);
146
147 /* Set default values */
148#define SET_OPT_ITEM(num, var) \
149 GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
Jack Jansen08c3be31997-04-08 15:27:00 +0000150 SetControlValue(handle, (short)p->var);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000151
152 SET_OPT_ITEM(OPT_INSPECT, inspect);
153 SET_OPT_ITEM(OPT_VERBOSE, verbose);
Jack Jansen36b983c1997-09-09 13:53:21 +0000154 SET_OPT_ITEM(OPT_OPTIMIZE, optimize);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000155 SET_OPT_ITEM(OPT_UNBUFFERED, unbuffered);
156 SET_OPT_ITEM(OPT_DEBUGGING, debugging);
Jack Jansen4a5eb962000-09-22 21:50:11 +0000157 GetDialogItem(dialog, OPT_KEEPALWAYS, &type, (Handle *)&handle, &rect);
158 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_ALWAYS));
159 GetDialogItem(dialog, OPT_KEEPOUTPUT, &type, (Handle *)&handle, &rect);
160 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_OUTPUT));
161 GetDialogItem(dialog, OPT_KEEPERROR, &type, (Handle *)&handle, &rect);
162 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_ERROR));
163 GetDialogItem(dialog, OPT_KEEPNEVER, &type, (Handle *)&handle, &rect);
164 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_NEVER));
165/* SET_OPT_ITEM(OPT_KEEPCONSOLE, keep_console); */
Jack Jansen0c6d0372000-05-05 23:11:14 +0000166 SET_OPT_ITEM(OPT_TABWARN, tabwarn);
Jack Jansen36b983c1997-09-09 13:53:21 +0000167 SET_OPT_ITEM(OPT_NOSITE, nosite);
Jack Jansen0c6d0372000-05-05 23:11:14 +0000168 SET_OPT_ITEM(OPT_NONAVSERV, nonavservice);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000169 /* The rest are not settable interactively */
170
171#undef SET_OPT_ITEM
172
173 while (1) {
174 handle = NULL;
175 ModalDialog(NULL, &item);
176 if ( item == OPT_OK )
177 break;
178 if ( item == OPT_CANCEL ) {
Jack Jansen08c3be31997-04-08 15:27:00 +0000179 DisposeDialog(dialog);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000180 exit(0);
181 }
Jack Jansen74a1e632000-07-14 22:37:27 +0000182#if !TARGET_API_MAC_CARBON
Jack Jansen36b983c1997-09-09 13:53:21 +0000183 if ( item == OPT_HELP ) {
184 HMSetBalloons(!HMGetBalloons());
185 }
Jack Jansenee6eeb12000-06-02 21:28:52 +0000186#endif
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000187 if ( item == OPT_CMDLINE ) {
188 int new_argc, newer_argc;
189 char **new_argv, **newer_argv;
190
191 new_argc = ccommand(&new_argv);
192 newer_argc = (new_argc-1) + old_argc;
193 newer_argv = malloc((newer_argc+1)*sizeof(char *));
194 if( !newer_argv )
195 Py_FatalError("Cannot malloc argv\n");
196 for(i=0; i<old_argc; i++)
197 newer_argv[i] = (*argvp)[i];
198 for(i=old_argc; i<=newer_argc; i++) /* Copy the NULL too */
199 newer_argv[i] = new_argv[i-old_argc+1];
200 *argvp = newer_argv;
201 *argcp = newer_argc;
202
203 /* XXXX Is it not safe to use free() here, apparently */
204 }
205#define OPT_ITEM(num, var) \
206 if ( item == (num) ) { \
207 p->var = !p->var; \
208 GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
Jack Jansen08c3be31997-04-08 15:27:00 +0000209 SetControlValue(handle, (short)p->var); \
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000210 }
211
212 OPT_ITEM(OPT_INSPECT, inspect);
213 OPT_ITEM(OPT_VERBOSE, verbose);
Jack Jansen36b983c1997-09-09 13:53:21 +0000214 OPT_ITEM(OPT_OPTIMIZE, optimize);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000215 OPT_ITEM(OPT_UNBUFFERED, unbuffered);
216 OPT_ITEM(OPT_DEBUGGING, debugging);
Jack Jansen4a5eb962000-09-22 21:50:11 +0000217 if ( item == OPT_KEEPALWAYS ) p->keep_console = POPT_KEEPCONSOLE_ALWAYS;
218 if ( item == OPT_KEEPOUTPUT ) p->keep_console = POPT_KEEPCONSOLE_OUTPUT;
219 if ( item == OPT_KEEPERROR ) p->keep_console = POPT_KEEPCONSOLE_ERROR;
220 if ( item == OPT_KEEPNEVER ) p->keep_console = POPT_KEEPCONSOLE_NEVER;
221 GetDialogItem(dialog, OPT_KEEPALWAYS, &type, (Handle *)&handle, &rect);
222 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_ALWAYS));
223 GetDialogItem(dialog, OPT_KEEPOUTPUT, &type, (Handle *)&handle, &rect);
224 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_OUTPUT));
225 GetDialogItem(dialog, OPT_KEEPERROR, &type, (Handle *)&handle, &rect);
226 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_ERROR));
227 GetDialogItem(dialog, OPT_KEEPNEVER, &type, (Handle *)&handle, &rect);
228 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_NEVER));
Jack Jansen0c6d0372000-05-05 23:11:14 +0000229 OPT_ITEM(OPT_TABWARN, tabwarn);
Jack Jansen36b983c1997-09-09 13:53:21 +0000230 OPT_ITEM(OPT_NOSITE, nosite);
Jack Jansen0c6d0372000-05-05 23:11:14 +0000231 OPT_ITEM(OPT_NONAVSERV, nonavservice);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000232
233#undef OPT_ITEM
234 }
Jack Jansen08c3be31997-04-08 15:27:00 +0000235 DisposeDialog(dialog);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000236}
237
238/*
239** Initialization code, shared by interpreter and applets
240*/
241static void
Jack Jansen52ac0371997-01-15 15:49:08 +0000242init_common(int *argcp, char ***argvp, int embedded)
Jack Jansen01fbc681996-02-28 15:42:47 +0000243{
Jack Jansen3f7d2b41996-09-06 22:21:07 +0000244 /* Remember resource fork refnum, for later */
245 PyMac_AppRefNum = CurResFile();
246
Jack Jansen01fbc681996-02-28 15:42:47 +0000247 /* Initialize toolboxes */
248 init_mac_world();
249
250#ifdef USE_MAC_SHARED_LIBRARY
251 /* Add the shared library to the stack of resource files */
Jack Jansen87c485c1998-07-31 09:38:01 +0000252 (void)PyMac_init_process_location();
Jack Jansen01fbc681996-02-28 15:42:47 +0000253 PyMac_AddLibResources();
254#endif
255
Jack Jansen2d1306b2000-04-07 09:10:49 +0000256#if defined(USE_GUSI1)
Jack Jansen01fbc681996-02-28 15:42:47 +0000257 /* Setup GUSI */
258 GUSIDefaultSetup();
Jack Jansenf6865f71996-09-04 15:24:59 +0000259 PyMac_SetGUSISpin();
Jack Jansen3f7d2b41996-09-06 22:21:07 +0000260 PyMac_SetGUSIOptions();
Jack Jansen01fbc681996-02-28 15:42:47 +0000261#endif
Jack Jansen2d1306b2000-04-07 09:10:49 +0000262#if defined(USE_GUSI)
263 atexit(PyMac_StopGUSISpin);
264#endif
Jack Jansen01fbc681996-02-28 15:42:47 +0000265
266#ifdef USE_SIOUX
267 /* Set various SIOUX flags. Some are changed later based on options */
Jack Jansencaa7c461997-06-12 10:49:13 +0000268/* SIOUXSettings.standalone = 0; /* XXXX Attempting to keep sioux from eating events */
Jack Jansen01fbc681996-02-28 15:42:47 +0000269 SIOUXSettings.asktosaveonclose = 0;
270 SIOUXSettings.showstatusline = 0;
271 SIOUXSettings.tabspaces = 4;
272#endif
273
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000274 /* Get options from preference file (or from applet resource fork) */
Jack Jansen4a5eb962000-09-22 21:50:11 +0000275 options.keep_console = POPT_KEEPCONSOLE_OUTPUT; /* default-default */
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000276 PyMac_PreferenceOptions(&options);
277
Jack Jansen52ac0371997-01-15 15:49:08 +0000278 if ( embedded ) {
279 static char *emb_argv[] = {"embedded-python", 0};
280
281 *argcp = 1;
282 *argvp = emb_argv;
283 } else {
284 /* Create argc/argv. Do it before we go into the options event loop. */
285 *argcp = PyMac_GetArgv(argvp, options.noargs);
Jack Jansen660bb1d2000-07-18 09:40:39 +0000286#ifdef USE_ARGV0_CHDIR
Jack Jansen660bb1d2000-07-18 09:40:39 +0000287 if (*argcp >= 1 && (*argvp)[0] && (*argvp)[0][0]) {
288 /* Workaround for MacOS X, which currently (DP4) doesn't set
289 ** the working folder correctly
290 */
291 char app_wd[256], *p;
292
293 strncpy(app_wd, (*argvp)[0], 256);
Jack Jansen660bb1d2000-07-18 09:40:39 +0000294 p = strrchr(app_wd, ':');
295 if ( p ) *p = 0;
Jack Jansen660bb1d2000-07-18 09:40:39 +0000296 chdir(app_wd);
297 }
298#endif
Jack Jansen52ac0371997-01-15 15:49:08 +0000299 /* Do interactive option setting, if allowed and <option> depressed */
300 PyMac_InteractiveOptions(&options, argcp, argvp);
301 }
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000302
303 /* Copy selected options to where the machine-independent stuff wants it */
Jack Jansen4a5eb962000-09-22 21:50:11 +0000304 Py_VerboseFlag = options.verbose;
Jack Jansen7330b391997-08-08 14:56:41 +0000305/* Py_SuppressPrintingFlag = options.suppress_print; */
Jack Jansen36b983c1997-09-09 13:53:21 +0000306 Py_OptimizeFlag = options.optimize;
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000307 Py_DebugFlag = options.debugging;
Jack Jansena7a89eb1997-10-07 21:48:57 +0000308 Py_NoSiteFlag = options.nosite;
Jack Jansen0c6d0372000-05-05 23:11:14 +0000309 Py_TabcheckFlag = options.tabwarn;
Jack Jansene3ae0df1997-06-03 15:28:29 +0000310 if ( options.noargs ) {
311 /* don't process events at all without the scripts permission */
312 PyMacSchedParams scp;
313
314 PyMac_GetSchedParams(&scp);
315 scp.process_events = 0;
316 /* Should we disable command-dot as well? */
317 PyMac_SetSchedParams(&scp);
318 }
Jack Jansen36b983c1997-09-09 13:53:21 +0000319 /* XXXX dispatch oldexc and nosite */
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000320
321 /* Set buffering */
322 if (options.unbuffered) {
323#ifndef MPW
324 setbuf(stdout, (char *)NULL);
325 setbuf(stderr, (char *)NULL);
326#else
327 /* On MPW (3.2) unbuffered seems to hang */
328 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
329 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
330#endif
331 }
Jack Jansen8c693211997-01-07 16:19:42 +0000332#if __profile__ == 1
333 /* collectSummary or collectDetailed, timebase, #routines, max stack depth */
Jack Jansene7424871999-09-30 11:20:11 +0000334 ProfilerInit(collectSummary, bestTimeBase, 8000, 250);
Jack Jansen8c693211997-01-07 16:19:42 +0000335#endif
Jack Jansen7330b391997-08-08 14:56:41 +0000336
337 /* Tell the rest of python about our argc/argv */
338 orig_argc = *argcp; /* For Py_GetArgcArgv() */
339 orig_argv = *argvp;
340 Py_SetProgramName((*argvp)[0]);
Jack Jansen01fbc681996-02-28 15:42:47 +0000341}
342
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000343/*
344** Inspection mode after script/applet termination
345*/
346static int
347run_inspect()
348{
349 int sts = 0;
350
351 if (options.inspect && isatty((int)fileno(stdin)))
352 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
353 return sts;
354}
Jack Jansen01fbc681996-02-28 15:42:47 +0000355
Jack Jansen0c6d0372000-05-05 23:11:14 +0000356/*
357** Import the macfsn module, which will override the Standard File
358** calls in the macfs builtin module by Navigation Services versions,
359** if available on this machine.
360*/
361static void
362PyMac_InstallNavServicesForSF()
363{
364 if ( !options.nonavservice ) {
365 PyObject *m = PyImport_ImportModule("macfsn");
366
367 if ( m == NULL ) {
368 PySys_WriteStderr("'import macfsn' failed; ");
369 if (Py_VerboseFlag) {
370 PySys_WriteStderr("traceback:\n");
371 PyErr_Print();
372 }
373 else {
374 PySys_WriteStderr("use -v for traceback\n");
375 }
376 }
377 }
378}
379
Jack Jansen696c9581995-08-14 12:33:20 +0000380#ifdef USE_MAC_APPLET_SUPPORT
381/* Applet support */
382
383/* Run a compiled Python Python script from 'PYC ' resource __main__ */
384static int
385run_main_resource()
386{
387 Handle h;
388 long size;
389 PyObject *code;
390 PyObject *result;
391
392 h = GetNamedResource('PYC ', "\p__main__");
393 if (h == NULL) {
394 Alert(NOPYC_ALERT, NULL);
395 return 1;
396 }
397 size = GetResourceSizeOnDisk(h);
398 HLock(h);
399 code = PyMarshal_ReadObjectFromString(*h + 8, (int)(size - 8));
400 HUnlock(h);
401 ReleaseResource(h);
402 if (code == NULL) {
403 PyErr_Print();
404 return 1;
405 }
406 result = PyImport_ExecCodeModule("__main__", code);
407 Py_DECREF(code);
408 if (result == NULL) {
409 PyErr_Print();
410 return 1;
411 }
412 Py_DECREF(result);
413 return 0;
414}
415
416/* Initialization sequence for applets */
417void
418PyMac_InitApplet()
419{
Guido van Rossumb0f3c821994-08-23 13:34:25 +0000420 int argc;
421 char **argv;
Jack Jansen696c9581995-08-14 12:33:20 +0000422 int err;
423
Jack Jansen52ac0371997-01-15 15:49:08 +0000424 init_common(&argc, &argv, 0);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000425
Jack Jansen696c9581995-08-14 12:33:20 +0000426 Py_Initialize();
Jack Jansen0c6d0372000-05-05 23:11:14 +0000427 PyMac_InstallNavServicesForSF();
Jack Jansen696c9581995-08-14 12:33:20 +0000428 PySys_SetArgv(argc, argv);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000429
Jack Jansen696c9581995-08-14 12:33:20 +0000430 err = run_main_resource();
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000431
432 err = (run_inspect() || err);
433
Jack Jansen696c9581995-08-14 12:33:20 +0000434 fflush(stderr);
435 fflush(stdout);
Jack Jansen0168f271995-10-27 13:32:30 +0000436 PyMac_Exit(err);
Jack Jansen696c9581995-08-14 12:33:20 +0000437 /* XXX Should we bother to Py_Exit(sts)? */
438}
439
Jack Jansen52ac0371997-01-15 15:49:08 +0000440/*
441** Hook for embedding python.
442*/
443void
444PyMac_Initialize()
445{
446 int argc;
447 char **argv;
448
449 init_common(&argc, &argv, 1);
450 Py_Initialize();
Jack Jansen0c6d0372000-05-05 23:11:14 +0000451 PyMac_InstallNavServicesForSF();
Jack Jansen52ac0371997-01-15 15:49:08 +0000452 PySys_SetArgv(argc, argv);
453}
454
Jack Jansen696c9581995-08-14 12:33:20 +0000455#endif /* USE_MAC_APPLET_SUPPORT */
456
457/* For normal application */
458void
459PyMac_InitApplication()
460{
461 int argc;
462 char **argv;
463
Jack Jansen52ac0371997-01-15 15:49:08 +0000464 init_common(&argc, &argv, 0);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000465
Jack Jansen696c9581995-08-14 12:33:20 +0000466 if ( argc > 1 ) {
467 /* We're running a script. Attempt to change current directory */
468 char curwd[256], *endp;
469
470 strcpy(curwd, argv[1]);
471 endp = strrchr(curwd, ':');
472 if ( endp && endp > curwd ) {
473 *endp = '\0';
474
475 chdir(curwd);
Jack Jansen2d1306b2000-04-07 09:10:49 +0000476#ifdef USE_GUSI1
Jack Jansen378815c1996-03-06 16:21:34 +0000477 /* Change MacOS's idea of wd too */
478 PyMac_FixGUSIcd();
479#endif
Jack Jansen696c9581995-08-14 12:33:20 +0000480 }
481 }
482 Py_Main(argc, argv);
483}
484
Jack Jansen696c9581995-08-14 12:33:20 +0000485/* Main program */
486
Jack Jansen76ceece1996-08-19 11:18:24 +0000487static void
Jack Jansen696c9581995-08-14 12:33:20 +0000488Py_Main(argc, argv)
489 int argc;
490 char **argv;
491{
Jack Jansen696c9581995-08-14 12:33:20 +0000492 int sts;
493 char *command = NULL;
494 char *filename = NULL;
495 FILE *fp = stdin;
Jack Jansen696c9581995-08-14 12:33:20 +0000496
Jack Jansen696c9581995-08-14 12:33:20 +0000497 filename = argv[1];
498
499 if (Py_VerboseFlag ||
500 command == NULL && filename == NULL && isatty((int)fileno(fp)))
Jack Jansen65c3ee02000-09-08 10:20:37 +0000501 fprintf(stderr, "Python %s on %s\n%s\n",
502 Py_GetVersion(), Py_GetPlatform(), COPYRIGHT);
Jack Jansen696c9581995-08-14 12:33:20 +0000503
504 if (filename != NULL) {
505 if ((fp = fopen(filename, "r")) == NULL) {
506 fprintf(stderr, "%s: can't open file '%s'\n",
507 argv[0], filename);
Jack Jansen0168f271995-10-27 13:32:30 +0000508 PyMac_Exit(2);
Jack Jansen696c9581995-08-14 12:33:20 +0000509 }
510 }
511
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000512 /* We initialize the menubar here, hoping SIOUX is initialized by now */
Jack Jansen3469e991996-09-06 00:30:45 +0000513 PyMac_InitMenuBar();
514
Jack Jansen696c9581995-08-14 12:33:20 +0000515 Py_Initialize();
516
Jack Jansen0c6d0372000-05-05 23:11:14 +0000517 PyMac_InstallNavServicesForSF();
518
Jack Jansen696c9581995-08-14 12:33:20 +0000519 PySys_SetArgv(argc-1, argv+1);
520
521 if (filename == NULL && isatty((int)fileno(fp))) {
522 FILE *fp = fopen(STARTUP, "r");
523 if (fp != NULL) {
524 (void) PyRun_SimpleFile(fp, STARTUP);
525 PyErr_Clear();
526 fclose(fp);
527 }
528 }
529 sts = PyRun_AnyFile(
530 fp, filename == NULL ? "<stdin>" : filename) != 0;
531 if (filename != NULL)
532 fclose(fp);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000533
534 if ( filename != NULL || command != NULL )
535 sts = (run_inspect() || sts);
Jack Jansen696c9581995-08-14 12:33:20 +0000536
537 Py_Exit(sts);
538 /*NOTREACHED*/
539}
540
Jack Jansen0168f271995-10-27 13:32:30 +0000541/*
542** Terminate application
543*/
Jack Jansen76ceece1996-08-19 11:18:24 +0000544void
Jack Jansen0168f271995-10-27 13:32:30 +0000545PyMac_Exit(status)
546 int status;
547{
Jack Jansen4a5eb962000-09-22 21:50:11 +0000548 int keep = 0;
Jack Jansen8c693211997-01-07 16:19:42 +0000549
550#if __profile__ == 1
551 ProfilerDump("\pPython Profiler Results");
552 ProfilerTerm();
553#endif
Jack Jansen0168f271995-10-27 13:32:30 +0000554
Jack Jansen1e8557a1995-11-10 14:51:26 +0000555#ifdef USE_SIOUX
Jack Jansen4a5eb962000-09-22 21:50:11 +0000556 switch (options.keep_console) {
557 case POPT_KEEPCONSOLE_NEVER:
558 keep = 0;
559 break;
560 case POPT_KEEPCONSOLE_OUTPUT:
561 if (gusisioux_state == GUSISIOUX_STATE_LASTWRITE ||
562 gusisioux_state == GUSISIOUX_STATE_UNKNOWN )
563 keep = 1;
564 else
565 keep = 0;
566 break;
567 case POPT_KEEPCONSOLE_ERROR:
568 keep = (status != 0);
569 break;
570 default:
571 keep = 1;
572 }
Jack Jansen1e8557a1995-11-10 14:51:26 +0000573 if (keep) {
574 SIOUXSettings.standalone = 1;
575 SIOUXSettings.autocloseonquit = 0;
Jack Jansen415571c1996-03-25 15:46:03 +0000576 SIOUXSetTitle("\p\307terminated\310");
Jack Jansencaa7c461997-06-12 10:49:13 +0000577 PyMac_RestoreMenuBar();
Jack Jansene44545f1997-05-07 15:48:54 +0000578#ifdef USE_MSL
579 /*
580 ** Temporary workaround: autocloseonquit clearing does not
581 ** currently work for the MSL/GUSI combo.
582 */
583 while(getchar() > 0);
584#endif
Jack Jansen1e8557a1995-11-10 14:51:26 +0000585 }
Jack Jansen0168f271995-10-27 13:32:30 +0000586 else
587 SIOUXSettings.autocloseonquit = 1;
Jack Jansenf6865f71996-09-04 15:24:59 +0000588#endif /* USE_SIOUX */
Jack Jansen0168f271995-10-27 13:32:30 +0000589
590 exit(status);
591}
Jack Jansen696c9581995-08-14 12:33:20 +0000592
593/* Return the program name -- some code out there needs this. */
Jack Jansena39f1b01997-05-23 15:35:14 +0000594char *
595Py_GetProgramFullPath()
596{
Jack Jansen7330b391997-08-08 14:56:41 +0000597 return orig_argv[0];
Jack Jansena39f1b01997-05-23 15:35:14 +0000598}
599
Jack Jansen696c9581995-08-14 12:33:20 +0000600
601/* Make the *original* argc/argv available to other modules.
602 This is rare, but it is needed by the secureware extension. */
603
604void
Jack Jansen9ae898b2000-07-11 21:16:03 +0000605Py_GetArgcArgv(int *argc,char ***argv)
Jack Jansen696c9581995-08-14 12:33:20 +0000606{
607 *argc = orig_argc;
608 *argv = orig_argv;
Guido van Rossumb0f3c821994-08-23 13:34:25 +0000609}
Jack Jansen1d2f8631996-08-02 15:16:16 +0000610
611/* More cruft that shouldn't really be here, used in sysmodule.c */
612
613char *
614Py_GetPrefix()
615{
Jack Jansenac625691997-09-08 13:22:22 +0000616 return PyMac_GetPythonDir();
Jack Jansen1d2f8631996-08-02 15:16:16 +0000617}
618
619char *
620Py_GetExecPrefix()
621{
Jack Jansenac625691997-09-08 13:22:22 +0000622 return PyMac_GetPythonDir();
Jack Jansen1d2f8631996-08-02 15:16:16 +0000623}