blob: b82395c9f439df54544bea7ed745cfe12ce5377b [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 Jansen8a387142001-02-11 01:08:04 +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 Jansen8a387142001-02-11 01:08:04 +000074/* A flag which remembers whether the user has acknowledged all the console
75** output (by typing something)
76*/
77#define STATE_UNKNOWN 0
78#define STATE_LASTREAD 1
79#define STATE_LASTWRITE 2
80int console_output_state = STATE_UNKNOWN;
81
Jack Jansendc86f9e2000-10-12 21:23:19 +000082PyMac_PrefRecord PyMac_options;
Jack Jansen0168f271995-10-27 13:32:30 +000083
Jack Jansend88296d2000-07-11 19:51:05 +000084static void Py_Main(int, char **); /* Forward */
85void PyMac_Exit(int); /* Forward */
Jack Jansen76ceece1996-08-19 11:18:24 +000086
Jack Jansen8f5725a1999-12-07 23:08:10 +000087static void init_appearance()
88{
89#ifdef USE_APPEARANCE
90 OSErr err;
91 SInt32 response;
92
93 err = Gestalt(gestaltAppearanceAttr,&response);
94 if ( err ) goto no_appearance;
95 if ( !(response&(1<<gestaltAppearanceExists)) ) goto no_appearance;
96 /* XXXX Should we check the version? Compat-mode? */
97 PyMac_AppearanceCompliant = 1;
98no_appearance:
99 return;
100#endif /* USE_APPEARANCE */
101}
Jack Jansen01fbc681996-02-28 15:42:47 +0000102/* Initialize the Mac toolbox world */
103
104static void
105init_mac_world()
106{
Jack Jansen74a1e632000-07-14 22:37:27 +0000107#if !TARGET_API_MAC_CARBON
Jack Jansenee6eeb12000-06-02 21:28:52 +0000108 /* These aren't needed for carbon */
Jack Jansen01fbc681996-02-28 15:42:47 +0000109 MaxApplZone();
110 InitGraf(&qd.thePort);
111 InitFonts();
112 InitWindows();
113 TEInit();
114 InitDialogs((long)0);
115 InitMenus();
Jack Jansenee6eeb12000-06-02 21:28:52 +0000116#endif
Jack Jansen01fbc681996-02-28 15:42:47 +0000117 InitCursor();
Jack Jansen8f5725a1999-12-07 23:08:10 +0000118 init_appearance();
Jack Jansen01fbc681996-02-28 15:42:47 +0000119}
120
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000121/*
122** PyMac_InteractiveOptions - Allow user to set options if option key is pressed
123*/
Jack Jansen01fbc681996-02-28 15:42:47 +0000124static void
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000125PyMac_InteractiveOptions(PyMac_PrefRecord *p, int *argcp, char ***argvp)
126{
127 KeyMap rmap;
128 unsigned char *map;
129 short item, type;
130 ControlHandle handle;
131 DialogPtr dialog;
132 Rect rect;
133 int old_argc = *argcp;
134 int i;
135
136 /*
137 ** If the preferences disallows interactive options we return,
138 ** similarly of <option> isn't pressed.
139 */
140 if (p->nointopt) return;
141
142 GetKeys(rmap);
143 map = (unsigned char *)rmap;
144 if ( ( map[0x3a>>3] & (1<<(0x3a&7)) ) == 0 ) /* option key is 3a */
145 return;
146
147 dialog = GetNewDialog(OPT_DIALOG, NULL, (WindowPtr)-1);
148 if ( dialog == NULL ) {
149 printf("Option dialog not found - cannot set options\n");
150 return;
151 }
152 SetDialogDefaultItem(dialog, OPT_OK);
153 SetDialogCancelItem(dialog, OPT_CANCEL);
154
155 /* Set default values */
156#define SET_OPT_ITEM(num, var) \
157 GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
Jack Jansen08c3be31997-04-08 15:27:00 +0000158 SetControlValue(handle, (short)p->var);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000159
160 SET_OPT_ITEM(OPT_INSPECT, inspect);
161 SET_OPT_ITEM(OPT_VERBOSE, verbose);
Jack Jansen36b983c1997-09-09 13:53:21 +0000162 SET_OPT_ITEM(OPT_OPTIMIZE, optimize);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000163 SET_OPT_ITEM(OPT_UNBUFFERED, unbuffered);
164 SET_OPT_ITEM(OPT_DEBUGGING, debugging);
Jack Jansen4a5eb962000-09-22 21:50:11 +0000165 GetDialogItem(dialog, OPT_KEEPALWAYS, &type, (Handle *)&handle, &rect);
166 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_ALWAYS));
167 GetDialogItem(dialog, OPT_KEEPOUTPUT, &type, (Handle *)&handle, &rect);
168 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_OUTPUT));
169 GetDialogItem(dialog, OPT_KEEPERROR, &type, (Handle *)&handle, &rect);
170 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_ERROR));
171 GetDialogItem(dialog, OPT_KEEPNEVER, &type, (Handle *)&handle, &rect);
172 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_NEVER));
173/* SET_OPT_ITEM(OPT_KEEPCONSOLE, keep_console); */
Jack Jansen0c6d0372000-05-05 23:11:14 +0000174 SET_OPT_ITEM(OPT_TABWARN, tabwarn);
Jack Jansen36b983c1997-09-09 13:53:21 +0000175 SET_OPT_ITEM(OPT_NOSITE, nosite);
Jack Jansen0c6d0372000-05-05 23:11:14 +0000176 SET_OPT_ITEM(OPT_NONAVSERV, nonavservice);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000177 /* The rest are not settable interactively */
178
179#undef SET_OPT_ITEM
180
181 while (1) {
182 handle = NULL;
183 ModalDialog(NULL, &item);
184 if ( item == OPT_OK )
185 break;
186 if ( item == OPT_CANCEL ) {
Jack Jansen08c3be31997-04-08 15:27:00 +0000187 DisposeDialog(dialog);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000188 exit(0);
189 }
Jack Jansen74a1e632000-07-14 22:37:27 +0000190#if !TARGET_API_MAC_CARBON
Jack Jansen36b983c1997-09-09 13:53:21 +0000191 if ( item == OPT_HELP ) {
192 HMSetBalloons(!HMGetBalloons());
193 }
Jack Jansenee6eeb12000-06-02 21:28:52 +0000194#endif
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000195 if ( item == OPT_CMDLINE ) {
196 int new_argc, newer_argc;
197 char **new_argv, **newer_argv;
198
199 new_argc = ccommand(&new_argv);
200 newer_argc = (new_argc-1) + old_argc;
201 newer_argv = malloc((newer_argc+1)*sizeof(char *));
202 if( !newer_argv )
203 Py_FatalError("Cannot malloc argv\n");
204 for(i=0; i<old_argc; i++)
205 newer_argv[i] = (*argvp)[i];
206 for(i=old_argc; i<=newer_argc; i++) /* Copy the NULL too */
207 newer_argv[i] = new_argv[i-old_argc+1];
208 *argvp = newer_argv;
209 *argcp = newer_argc;
210
211 /* XXXX Is it not safe to use free() here, apparently */
212 }
213#define OPT_ITEM(num, var) \
214 if ( item == (num) ) { \
215 p->var = !p->var; \
216 GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
Jack Jansen08c3be31997-04-08 15:27:00 +0000217 SetControlValue(handle, (short)p->var); \
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000218 }
219
220 OPT_ITEM(OPT_INSPECT, inspect);
221 OPT_ITEM(OPT_VERBOSE, verbose);
Jack Jansen36b983c1997-09-09 13:53:21 +0000222 OPT_ITEM(OPT_OPTIMIZE, optimize);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000223 OPT_ITEM(OPT_UNBUFFERED, unbuffered);
224 OPT_ITEM(OPT_DEBUGGING, debugging);
Jack Jansen4a5eb962000-09-22 21:50:11 +0000225 if ( item == OPT_KEEPALWAYS ) p->keep_console = POPT_KEEPCONSOLE_ALWAYS;
226 if ( item == OPT_KEEPOUTPUT ) p->keep_console = POPT_KEEPCONSOLE_OUTPUT;
227 if ( item == OPT_KEEPERROR ) p->keep_console = POPT_KEEPCONSOLE_ERROR;
228 if ( item == OPT_KEEPNEVER ) p->keep_console = POPT_KEEPCONSOLE_NEVER;
229 GetDialogItem(dialog, OPT_KEEPALWAYS, &type, (Handle *)&handle, &rect);
230 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_ALWAYS));
231 GetDialogItem(dialog, OPT_KEEPOUTPUT, &type, (Handle *)&handle, &rect);
232 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_OUTPUT));
233 GetDialogItem(dialog, OPT_KEEPERROR, &type, (Handle *)&handle, &rect);
234 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_ERROR));
235 GetDialogItem(dialog, OPT_KEEPNEVER, &type, (Handle *)&handle, &rect);
236 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_NEVER));
Jack Jansen0c6d0372000-05-05 23:11:14 +0000237 OPT_ITEM(OPT_TABWARN, tabwarn);
Jack Jansen36b983c1997-09-09 13:53:21 +0000238 OPT_ITEM(OPT_NOSITE, nosite);
Jack Jansen0c6d0372000-05-05 23:11:14 +0000239 OPT_ITEM(OPT_NONAVSERV, nonavservice);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000240
241#undef OPT_ITEM
242 }
Jack Jansen08c3be31997-04-08 15:27:00 +0000243 DisposeDialog(dialog);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000244}
245
246/*
247** Initialization code, shared by interpreter and applets
248*/
249static void
Jack Jansen52ac0371997-01-15 15:49:08 +0000250init_common(int *argcp, char ***argvp, int embedded)
Jack Jansen01fbc681996-02-28 15:42:47 +0000251{
Jack Jansen3f7d2b41996-09-06 22:21:07 +0000252 /* Remember resource fork refnum, for later */
253 PyMac_AppRefNum = CurResFile();
254
Jack Jansen01fbc681996-02-28 15:42:47 +0000255 /* Initialize toolboxes */
256 init_mac_world();
257
258#ifdef USE_MAC_SHARED_LIBRARY
259 /* Add the shared library to the stack of resource files */
Jack Jansen87c485c1998-07-31 09:38:01 +0000260 (void)PyMac_init_process_location();
Jack Jansen01fbc681996-02-28 15:42:47 +0000261 PyMac_AddLibResources();
262#endif
263
Jack Jansen2d1306b2000-04-07 09:10:49 +0000264#if defined(USE_GUSI1)
Jack Jansen01fbc681996-02-28 15:42:47 +0000265 /* Setup GUSI */
266 GUSIDefaultSetup();
Jack Jansenf6865f71996-09-04 15:24:59 +0000267 PyMac_SetGUSISpin();
Jack Jansen3f7d2b41996-09-06 22:21:07 +0000268 PyMac_SetGUSIOptions();
Jack Jansen01fbc681996-02-28 15:42:47 +0000269#endif
Jack Jansen2d1306b2000-04-07 09:10:49 +0000270#if defined(USE_GUSI)
271 atexit(PyMac_StopGUSISpin);
272#endif
Jack Jansen01fbc681996-02-28 15:42:47 +0000273
274#ifdef USE_SIOUX
275 /* Set various SIOUX flags. Some are changed later based on options */
Jack Jansencaa7c461997-06-12 10:49:13 +0000276/* SIOUXSettings.standalone = 0; /* XXXX Attempting to keep sioux from eating events */
Jack Jansen01fbc681996-02-28 15:42:47 +0000277 SIOUXSettings.asktosaveonclose = 0;
278 SIOUXSettings.showstatusline = 0;
279 SIOUXSettings.tabspaces = 4;
280#endif
281
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000282 /* Get options from preference file (or from applet resource fork) */
Jack Jansendc86f9e2000-10-12 21:23:19 +0000283 PyMac_options.keep_console = POPT_KEEPCONSOLE_OUTPUT; /* default-default */
284 PyMac_PreferenceOptions(&PyMac_options);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000285
Jack Jansen52ac0371997-01-15 15:49:08 +0000286 if ( embedded ) {
287 static char *emb_argv[] = {"embedded-python", 0};
288
289 *argcp = 1;
290 *argvp = emb_argv;
291 } else {
292 /* Create argc/argv. Do it before we go into the options event loop. */
Jack Jansendc86f9e2000-10-12 21:23:19 +0000293 *argcp = PyMac_GetArgv(argvp, PyMac_options.noargs);
Jack Jansenc00df0b2001-01-16 15:54:58 +0000294#ifndef NO_ARGV0_CHDIR
Jack Jansen660bb1d2000-07-18 09:40:39 +0000295 if (*argcp >= 1 && (*argvp)[0] && (*argvp)[0][0]) {
296 /* Workaround for MacOS X, which currently (DP4) doesn't set
297 ** the working folder correctly
298 */
299 char app_wd[256], *p;
300
301 strncpy(app_wd, (*argvp)[0], 256);
Jack Jansen660bb1d2000-07-18 09:40:39 +0000302 p = strrchr(app_wd, ':');
303 if ( p ) *p = 0;
Jack Jansen660bb1d2000-07-18 09:40:39 +0000304 chdir(app_wd);
305 }
306#endif
Jack Jansen52ac0371997-01-15 15:49:08 +0000307 /* Do interactive option setting, if allowed and <option> depressed */
Jack Jansendc86f9e2000-10-12 21:23:19 +0000308 PyMac_InteractiveOptions(&PyMac_options, argcp, argvp);
Jack Jansen52ac0371997-01-15 15:49:08 +0000309 }
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000310
311 /* Copy selected options to where the machine-independent stuff wants it */
Jack Jansendc86f9e2000-10-12 21:23:19 +0000312 Py_VerboseFlag = PyMac_options.verbose;
313/* Py_SuppressPrintingFlag = PyMac_options.suppress_print; */
314 Py_OptimizeFlag = PyMac_options.optimize;
315 Py_DebugFlag = PyMac_options.debugging;
316 Py_NoSiteFlag = PyMac_options.nosite;
317 Py_TabcheckFlag = PyMac_options.tabwarn;
318 if ( PyMac_options.noargs ) {
Jack Jansene3ae0df1997-06-03 15:28:29 +0000319 /* don't process events at all without the scripts permission */
320 PyMacSchedParams scp;
321
322 PyMac_GetSchedParams(&scp);
323 scp.process_events = 0;
324 /* Should we disable command-dot as well? */
325 PyMac_SetSchedParams(&scp);
326 }
Jack Jansen36b983c1997-09-09 13:53:21 +0000327 /* XXXX dispatch oldexc and nosite */
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000328
329 /* Set buffering */
Jack Jansendc86f9e2000-10-12 21:23:19 +0000330 if (PyMac_options.unbuffered) {
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000331#ifndef MPW
332 setbuf(stdout, (char *)NULL);
333 setbuf(stderr, (char *)NULL);
334#else
335 /* On MPW (3.2) unbuffered seems to hang */
336 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
337 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
338#endif
339 }
Jack Jansen8c693211997-01-07 16:19:42 +0000340#if __profile__ == 1
341 /* collectSummary or collectDetailed, timebase, #routines, max stack depth */
Jack Jansene7424871999-09-30 11:20:11 +0000342 ProfilerInit(collectSummary, bestTimeBase, 8000, 250);
Jack Jansen8c693211997-01-07 16:19:42 +0000343#endif
Jack Jansen7330b391997-08-08 14:56:41 +0000344
345 /* Tell the rest of python about our argc/argv */
346 orig_argc = *argcp; /* For Py_GetArgcArgv() */
347 orig_argv = *argvp;
348 Py_SetProgramName((*argvp)[0]);
Jack Jansen01fbc681996-02-28 15:42:47 +0000349}
350
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000351/*
352** Inspection mode after script/applet termination
353*/
354static int
355run_inspect()
356{
357 int sts = 0;
358
Jack Jansendc86f9e2000-10-12 21:23:19 +0000359 if (PyMac_options.inspect && isatty((int)fileno(stdin)))
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000360 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
361 return sts;
362}
Jack Jansen01fbc681996-02-28 15:42:47 +0000363
Jack Jansen0c6d0372000-05-05 23:11:14 +0000364/*
365** Import the macfsn module, which will override the Standard File
366** calls in the macfs builtin module by Navigation Services versions,
367** if available on this machine.
368*/
369static void
370PyMac_InstallNavServicesForSF()
371{
Jack Jansendc86f9e2000-10-12 21:23:19 +0000372 if ( !PyMac_options.nonavservice ) {
Jack Jansen0c6d0372000-05-05 23:11:14 +0000373 PyObject *m = PyImport_ImportModule("macfsn");
374
375 if ( m == NULL ) {
376 PySys_WriteStderr("'import macfsn' failed; ");
377 if (Py_VerboseFlag) {
378 PySys_WriteStderr("traceback:\n");
379 PyErr_Print();
380 }
381 else {
382 PySys_WriteStderr("use -v for traceback\n");
383 }
384 }
385 }
386}
387
Jack Jansen696c9581995-08-14 12:33:20 +0000388#ifdef USE_MAC_APPLET_SUPPORT
389/* Applet support */
390
391/* Run a compiled Python Python script from 'PYC ' resource __main__ */
392static int
393run_main_resource()
394{
395 Handle h;
396 long size;
397 PyObject *code;
398 PyObject *result;
399
400 h = GetNamedResource('PYC ', "\p__main__");
401 if (h == NULL) {
402 Alert(NOPYC_ALERT, NULL);
403 return 1;
404 }
405 size = GetResourceSizeOnDisk(h);
406 HLock(h);
407 code = PyMarshal_ReadObjectFromString(*h + 8, (int)(size - 8));
408 HUnlock(h);
409 ReleaseResource(h);
410 if (code == NULL) {
411 PyErr_Print();
412 return 1;
413 }
414 result = PyImport_ExecCodeModule("__main__", code);
415 Py_DECREF(code);
416 if (result == NULL) {
417 PyErr_Print();
418 return 1;
419 }
420 Py_DECREF(result);
421 return 0;
422}
423
424/* Initialization sequence for applets */
425void
426PyMac_InitApplet()
427{
Guido van Rossumb0f3c821994-08-23 13:34:25 +0000428 int argc;
429 char **argv;
Jack Jansen696c9581995-08-14 12:33:20 +0000430 int err;
431
Jack Jansen52ac0371997-01-15 15:49:08 +0000432 init_common(&argc, &argv, 0);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000433
Jack Jansen696c9581995-08-14 12:33:20 +0000434 Py_Initialize();
Jack Jansen0c6d0372000-05-05 23:11:14 +0000435 PyMac_InstallNavServicesForSF();
Jack Jansen696c9581995-08-14 12:33:20 +0000436 PySys_SetArgv(argc, argv);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000437
Jack Jansen696c9581995-08-14 12:33:20 +0000438 err = run_main_resource();
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000439
440 err = (run_inspect() || err);
441
Jack Jansen696c9581995-08-14 12:33:20 +0000442 fflush(stderr);
443 fflush(stdout);
Jack Jansen0168f271995-10-27 13:32:30 +0000444 PyMac_Exit(err);
Jack Jansen696c9581995-08-14 12:33:20 +0000445 /* XXX Should we bother to Py_Exit(sts)? */
446}
447
Jack Jansen52ac0371997-01-15 15:49:08 +0000448/*
449** Hook for embedding python.
450*/
451void
452PyMac_Initialize()
453{
454 int argc;
455 char **argv;
456
457 init_common(&argc, &argv, 1);
458 Py_Initialize();
Jack Jansen0c6d0372000-05-05 23:11:14 +0000459 PyMac_InstallNavServicesForSF();
Jack Jansen52ac0371997-01-15 15:49:08 +0000460 PySys_SetArgv(argc, argv);
461}
462
Jack Jansen696c9581995-08-14 12:33:20 +0000463#endif /* USE_MAC_APPLET_SUPPORT */
464
465/* For normal application */
466void
467PyMac_InitApplication()
468{
469 int argc;
470 char **argv;
471
Jack Jansen52ac0371997-01-15 15:49:08 +0000472 init_common(&argc, &argv, 0);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000473
Jack Jansen696c9581995-08-14 12:33:20 +0000474 if ( argc > 1 ) {
475 /* We're running a script. Attempt to change current directory */
476 char curwd[256], *endp;
477
478 strcpy(curwd, argv[1]);
479 endp = strrchr(curwd, ':');
480 if ( endp && endp > curwd ) {
481 *endp = '\0';
482
483 chdir(curwd);
Jack Jansen2d1306b2000-04-07 09:10:49 +0000484#ifdef USE_GUSI1
Jack Jansen378815c1996-03-06 16:21:34 +0000485 /* Change MacOS's idea of wd too */
486 PyMac_FixGUSIcd();
487#endif
Jack Jansen696c9581995-08-14 12:33:20 +0000488 }
Jack Jansencbed91b2001-08-03 13:31:36 +0000489 /* Check that the first argument is a text file */
490 if ( PyMac_getfiletype(argv[1]) != 'TEXT' ) {
491 Alert(NOTASCRIPT_ID, NULL);
492 exit(0);
493 }
Jack Jansen696c9581995-08-14 12:33:20 +0000494 }
495 Py_Main(argc, argv);
496}
497
Jack Jansen696c9581995-08-14 12:33:20 +0000498/* Main program */
499
Jack Jansen76ceece1996-08-19 11:18:24 +0000500static void
Jack Jansen696c9581995-08-14 12:33:20 +0000501Py_Main(argc, argv)
502 int argc;
503 char **argv;
504{
Jack Jansen696c9581995-08-14 12:33:20 +0000505 int sts;
506 char *command = NULL;
507 char *filename = NULL;
508 FILE *fp = stdin;
Jack Jansen696c9581995-08-14 12:33:20 +0000509
Jack Jansen696c9581995-08-14 12:33:20 +0000510 filename = argv[1];
511
512 if (Py_VerboseFlag ||
513 command == NULL && filename == NULL && isatty((int)fileno(fp)))
Jack Jansen65c3ee02000-09-08 10:20:37 +0000514 fprintf(stderr, "Python %s on %s\n%s\n",
515 Py_GetVersion(), Py_GetPlatform(), COPYRIGHT);
Jack Jansen696c9581995-08-14 12:33:20 +0000516
517 if (filename != NULL) {
518 if ((fp = fopen(filename, "r")) == NULL) {
519 fprintf(stderr, "%s: can't open file '%s'\n",
520 argv[0], filename);
Jack Jansen0168f271995-10-27 13:32:30 +0000521 PyMac_Exit(2);
Jack Jansen696c9581995-08-14 12:33:20 +0000522 }
523 }
524
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000525 /* We initialize the menubar here, hoping SIOUX is initialized by now */
Jack Jansen3469e991996-09-06 00:30:45 +0000526 PyMac_InitMenuBar();
527
Jack Jansen696c9581995-08-14 12:33:20 +0000528 Py_Initialize();
529
unknownd1054ef2001-07-04 22:37:19 +0000530 PyUnicode_SetDefaultEncoding(PyMac_getscript());
531
Jack Jansen0c6d0372000-05-05 23:11:14 +0000532 PyMac_InstallNavServicesForSF();
533
Jack Jansen696c9581995-08-14 12:33:20 +0000534 PySys_SetArgv(argc-1, argv+1);
535
536 if (filename == NULL && isatty((int)fileno(fp))) {
537 FILE *fp = fopen(STARTUP, "r");
538 if (fp != NULL) {
539 (void) PyRun_SimpleFile(fp, STARTUP);
540 PyErr_Clear();
541 fclose(fp);
542 }
543 }
544 sts = PyRun_AnyFile(
545 fp, filename == NULL ? "<stdin>" : filename) != 0;
546 if (filename != NULL)
547 fclose(fp);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000548
549 if ( filename != NULL || command != NULL )
550 sts = (run_inspect() || sts);
Jack Jansen696c9581995-08-14 12:33:20 +0000551
552 Py_Exit(sts);
553 /*NOTREACHED*/
554}
555
Jack Jansen0168f271995-10-27 13:32:30 +0000556/*
Jack Jansen8413b472000-10-19 22:02:16 +0000557** Reset the "unseen output" flag
558*/
559void
560PyMac_OutputSeen()
561{
Jack Jansen657ba272001-02-17 22:02:07 +0000562 if ( console_output_state == STATE_UNKNOWN )
563 PyMac_InitMenuBar();
Jack Jansen8a387142001-02-11 01:08:04 +0000564 console_output_state = STATE_LASTREAD;
Jack Jansen8413b472000-10-19 22:02:16 +0000565}
566
567/*
Jack Jansen8a387142001-02-11 01:08:04 +0000568** Set the "unseen output" flag
569*/
570void
571PyMac_OutputNotSeen()
572{
Jack Jansen657ba272001-02-17 22:02:07 +0000573 if ( console_output_state == STATE_UNKNOWN )
574 PyMac_InitMenuBar();
Jack Jansen8a387142001-02-11 01:08:04 +0000575 console_output_state = STATE_LASTWRITE;
576}
Jack Jansen15f1c082001-04-25 22:07:27 +0000577
578/*
579** Override abort() - The default one is not what we want.
580*/
581void
582abort()
583{
584 console_output_state = STATE_LASTWRITE;
585 PyMac_Exit(1);
586}
Jack Jansen8a387142001-02-11 01:08:04 +0000587
588/*
Jack Jansen0168f271995-10-27 13:32:30 +0000589** Terminate application
590*/
Jack Jansen76ceece1996-08-19 11:18:24 +0000591void
Jack Jansen0168f271995-10-27 13:32:30 +0000592PyMac_Exit(status)
593 int status;
594{
Jack Jansen4a5eb962000-09-22 21:50:11 +0000595 int keep = 0;
Jack Jansen8c693211997-01-07 16:19:42 +0000596
597#if __profile__ == 1
598 ProfilerDump("\pPython Profiler Results");
599 ProfilerTerm();
600#endif
Jack Jansen0168f271995-10-27 13:32:30 +0000601
Jack Jansen1e8557a1995-11-10 14:51:26 +0000602#ifdef USE_SIOUX
Jack Jansendc86f9e2000-10-12 21:23:19 +0000603 switch (PyMac_options.keep_console) {
Jack Jansen4a5eb962000-09-22 21:50:11 +0000604 case POPT_KEEPCONSOLE_NEVER:
605 keep = 0;
606 break;
607 case POPT_KEEPCONSOLE_OUTPUT:
Jack Jansen8a387142001-02-11 01:08:04 +0000608 if (console_output_state == STATE_LASTWRITE ||
609 console_output_state == STATE_UNKNOWN )
Jack Jansen4a5eb962000-09-22 21:50:11 +0000610 keep = 1;
611 else
612 keep = 0;
613 break;
614 case POPT_KEEPCONSOLE_ERROR:
615 keep = (status != 0);
616 break;
617 default:
618 keep = 1;
619 }
Jack Jansen1e8557a1995-11-10 14:51:26 +0000620 if (keep) {
621 SIOUXSettings.standalone = 1;
622 SIOUXSettings.autocloseonquit = 0;
Jack Jansen415571c1996-03-25 15:46:03 +0000623 SIOUXSetTitle("\p\307terminated\310");
Jack Jansen15f1c082001-04-25 22:07:27 +0000624 PyMac_RaiseConsoleWindow();
Jack Jansencaa7c461997-06-12 10:49:13 +0000625 PyMac_RestoreMenuBar();
Jack Jansene44545f1997-05-07 15:48:54 +0000626#ifdef USE_MSL
627 /*
628 ** Temporary workaround: autocloseonquit clearing does not
629 ** currently work for the MSL/GUSI combo.
630 */
631 while(getchar() > 0);
632#endif
Jack Jansen1e8557a1995-11-10 14:51:26 +0000633 }
Jack Jansen0168f271995-10-27 13:32:30 +0000634 else
635 SIOUXSettings.autocloseonquit = 1;
Jack Jansenf6865f71996-09-04 15:24:59 +0000636#endif /* USE_SIOUX */
Jack Jansen0168f271995-10-27 13:32:30 +0000637
638 exit(status);
639}
Jack Jansen696c9581995-08-14 12:33:20 +0000640
641/* Return the program name -- some code out there needs this. */
Jack Jansena39f1b01997-05-23 15:35:14 +0000642char *
643Py_GetProgramFullPath()
644{
Jack Jansen7330b391997-08-08 14:56:41 +0000645 return orig_argv[0];
Jack Jansena39f1b01997-05-23 15:35:14 +0000646}
647
Jack Jansen696c9581995-08-14 12:33:20 +0000648
649/* Make the *original* argc/argv available to other modules.
650 This is rare, but it is needed by the secureware extension. */
651
652void
Jack Jansen9ae898b2000-07-11 21:16:03 +0000653Py_GetArgcArgv(int *argc,char ***argv)
Jack Jansen696c9581995-08-14 12:33:20 +0000654{
655 *argc = orig_argc;
656 *argv = orig_argv;
Guido van Rossumb0f3c821994-08-23 13:34:25 +0000657}
Jack Jansen1d2f8631996-08-02 15:16:16 +0000658
659/* More cruft that shouldn't really be here, used in sysmodule.c */
660
661char *
662Py_GetPrefix()
663{
Jack Jansenac625691997-09-08 13:22:22 +0000664 return PyMac_GetPythonDir();
Jack Jansen1d2f8631996-08-02 15:16:16 +0000665}
666
667char *
668Py_GetExecPrefix()
669{
Jack Jansenac625691997-09-08 13:22:22 +0000670 return PyMac_GetPythonDir();
Jack Jansen1d2f8631996-08-02 15:16:16 +0000671}
Jack Jansen8a387142001-02-11 01:08:04 +0000672
673int
674PyMac_GetDelayConsoleFlag()
675{
676 return (int)PyMac_options.delayconsole;
677}