blob: b86b52847fc6eabc2c6f6e1abc2a52760113af58 [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 Jansendff77702001-09-05 22:07:52 +000033#ifdef WITHOUT_FRAMEWORKS
Jack Jansen696c9581995-08-14 12:33:20 +000034#include <Memory.h>
35#include <Resources.h>
Guido van Rossumb0f3c821994-08-23 13:34:25 +000036#include <stdio.h>
Jack Jansen696c9581995-08-14 12:33:20 +000037#include <Events.h>
38#include <Windows.h>
Jack Jansen2429c721996-03-07 15:17:11 +000039#include <Fonts.h>
Jack Jansen36b983c1997-09-09 13:53:21 +000040#include <Balloons.h>
Jack Jansen5ded1bf2001-10-30 22:48:36 +000041#if TARGET_API_MAC_CARBON
42#include <CFBundle.h>
43#include <CFURL.h>
44#include <CFString.h>
45#include <CFBase.h>
46#include <CFArray.h>
47#endif /* TARGET_API_MAC_CARBON */
Jack Jansen8f5725a1999-12-07 23:08:10 +000048#ifdef USE_APPEARANCE
49#include <Gestalt.h>
50#include <Appearance.h>
51#endif /* USE_APPEARANCE */
Jack Jansendff77702001-09-05 22:07:52 +000052#else
53#include <Carbon/Carbon.h>
54#endif /* WITHOUT_FRAMEWORKS */
55
Jack Jansenc76fd391995-02-02 14:27:31 +000056#ifdef __MWERKS__
57#include <SIOUX.h>
Jack Jansen1e8557a1995-11-10 14:51:26 +000058#define USE_SIOUX
Jack Jansen9ae898b2000-07-11 21:16:03 +000059extern int ccommand(char ***);
Jack Jansen8c693211997-01-07 16:19:42 +000060#if __profile__ == 1
61#include <profiler.h>
Jack Jansendff77702001-09-05 22:07:52 +000062#endif /* __profile__ */
63#endif /* __MWERKS__ */
64
Jack Jansenee6eeb12000-06-02 21:28:52 +000065#include <unistd.h>
Jack Jansen5bdbabd2000-07-24 19:52:52 +000066#ifdef USE_MAC_SHARED_LIBRARY
67extern PyMac_AddLibResources(void);
68#endif
Jack Jansenc76fd391995-02-02 14:27:31 +000069
Jack Jansen696c9581995-08-14 12:33:20 +000070#define STARTUP "PythonStartup"
Jack Jansenbac428d1994-12-14 13:47:30 +000071
Jack Jansen65c3ee02000-09-08 10:20:37 +000072#define COPYRIGHT \
73 "Type \"copyright\", \"credits\" or \"license\" for more information."
74
Jack Jansen3f7d2b41996-09-06 22:21:07 +000075short PyMac_AppRefNum; /* RefNum of application resource fork */
Jack Jansen696c9581995-08-14 12:33:20 +000076
Jack Jansen1d2f8631996-08-02 15:16:16 +000077/* For Py_GetArgcArgv(); set by main() */
Jack Jansen696c9581995-08-14 12:33:20 +000078static char **orig_argv;
79static int orig_argc;
80
Jack Jansen8a387142001-02-11 01:08:04 +000081/* A flag which remembers whether the user has acknowledged all the console
82** output (by typing something)
83*/
84#define STATE_UNKNOWN 0
85#define STATE_LASTREAD 1
86#define STATE_LASTWRITE 2
87int console_output_state = STATE_UNKNOWN;
88
Jack Jansendc86f9e2000-10-12 21:23:19 +000089PyMac_PrefRecord PyMac_options;
Jack Jansen0168f271995-10-27 13:32:30 +000090
Jack Jansen998a40a2001-09-11 13:08:10 +000091static void Py_Main(int, char **, char *); /* Forward */
Jack Jansend88296d2000-07-11 19:51:05 +000092void PyMac_Exit(int); /* Forward */
Jack Jansen76ceece1996-08-19 11:18:24 +000093
Jack Jansendff77702001-09-05 22:07:52 +000094static void init_appearance(void)
Jack Jansen8f5725a1999-12-07 23:08:10 +000095{
96#ifdef USE_APPEARANCE
97 OSErr err;
98 SInt32 response;
99
100 err = Gestalt(gestaltAppearanceAttr,&response);
101 if ( err ) goto no_appearance;
102 if ( !(response&(1<<gestaltAppearanceExists)) ) goto no_appearance;
103 /* XXXX Should we check the version? Compat-mode? */
104 PyMac_AppearanceCompliant = 1;
105no_appearance:
106 return;
107#endif /* USE_APPEARANCE */
108}
Jack Jansen01fbc681996-02-28 15:42:47 +0000109/* Initialize the Mac toolbox world */
110
111static void
Jack Jansendff77702001-09-05 22:07:52 +0000112init_mac_world(void)
Jack Jansen01fbc681996-02-28 15:42:47 +0000113{
Jack Jansen74a1e632000-07-14 22:37:27 +0000114#if !TARGET_API_MAC_CARBON
Jack Jansenee6eeb12000-06-02 21:28:52 +0000115 /* These aren't needed for carbon */
Jack Jansen01fbc681996-02-28 15:42:47 +0000116 MaxApplZone();
117 InitGraf(&qd.thePort);
118 InitFonts();
119 InitWindows();
120 TEInit();
121 InitDialogs((long)0);
122 InitMenus();
Jack Jansenee6eeb12000-06-02 21:28:52 +0000123#endif
Jack Jansen01fbc681996-02-28 15:42:47 +0000124 InitCursor();
Jack Jansen8f5725a1999-12-07 23:08:10 +0000125 init_appearance();
Jack Jansen01fbc681996-02-28 15:42:47 +0000126}
127
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000128/*
129** PyMac_InteractiveOptions - Allow user to set options if option key is pressed
130*/
Jack Jansen01fbc681996-02-28 15:42:47 +0000131static void
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000132PyMac_InteractiveOptions(PyMac_PrefRecord *p, int *argcp, char ***argvp)
133{
134 KeyMap rmap;
135 unsigned char *map;
136 short item, type;
137 ControlHandle handle;
138 DialogPtr dialog;
139 Rect rect;
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000140
141 /*
142 ** If the preferences disallows interactive options we return,
143 ** similarly of <option> isn't pressed.
144 */
145 if (p->nointopt) return;
146
147 GetKeys(rmap);
148 map = (unsigned char *)rmap;
149 if ( ( map[0x3a>>3] & (1<<(0x3a&7)) ) == 0 ) /* option key is 3a */
150 return;
151
152 dialog = GetNewDialog(OPT_DIALOG, NULL, (WindowPtr)-1);
153 if ( dialog == NULL ) {
154 printf("Option dialog not found - cannot set options\n");
155 return;
156 }
157 SetDialogDefaultItem(dialog, OPT_OK);
158 SetDialogCancelItem(dialog, OPT_CANCEL);
159
160 /* Set default values */
161#define SET_OPT_ITEM(num, var) \
162 GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
Jack Jansen08c3be31997-04-08 15:27:00 +0000163 SetControlValue(handle, (short)p->var);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000164
165 SET_OPT_ITEM(OPT_INSPECT, inspect);
166 SET_OPT_ITEM(OPT_VERBOSE, verbose);
Jack Jansenff5d8aa2001-09-01 22:37:54 +0000167 /* OPT_VERBOSEVERBOSE is default off */
Jack Jansen36b983c1997-09-09 13:53:21 +0000168 SET_OPT_ITEM(OPT_OPTIMIZE, optimize);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000169 SET_OPT_ITEM(OPT_UNBUFFERED, unbuffered);
170 SET_OPT_ITEM(OPT_DEBUGGING, debugging);
Jack Jansen4a5eb962000-09-22 21:50:11 +0000171 GetDialogItem(dialog, OPT_KEEPALWAYS, &type, (Handle *)&handle, &rect);
172 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_ALWAYS));
173 GetDialogItem(dialog, OPT_KEEPOUTPUT, &type, (Handle *)&handle, &rect);
174 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_OUTPUT));
175 GetDialogItem(dialog, OPT_KEEPERROR, &type, (Handle *)&handle, &rect);
176 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_ERROR));
177 GetDialogItem(dialog, OPT_KEEPNEVER, &type, (Handle *)&handle, &rect);
178 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_NEVER));
179/* SET_OPT_ITEM(OPT_KEEPCONSOLE, keep_console); */
Jack Jansen0c6d0372000-05-05 23:11:14 +0000180 SET_OPT_ITEM(OPT_TABWARN, tabwarn);
Jack Jansen36b983c1997-09-09 13:53:21 +0000181 SET_OPT_ITEM(OPT_NOSITE, nosite);
Jack Jansenff5d8aa2001-09-01 22:37:54 +0000182 SET_OPT_ITEM(OPT_DIVISIONWARN, divisionwarn);
183 SET_OPT_ITEM(OPT_UNIXNEWLINES, unixnewlines);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000184 /* The rest are not settable interactively */
185
186#undef SET_OPT_ITEM
187
188 while (1) {
189 handle = NULL;
190 ModalDialog(NULL, &item);
191 if ( item == OPT_OK )
192 break;
193 if ( item == OPT_CANCEL ) {
Jack Jansen08c3be31997-04-08 15:27:00 +0000194 DisposeDialog(dialog);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000195 exit(0);
196 }
Jack Jansen74a1e632000-07-14 22:37:27 +0000197#if !TARGET_API_MAC_CARBON
Jack Jansen36b983c1997-09-09 13:53:21 +0000198 if ( item == OPT_HELP ) {
199 HMSetBalloons(!HMGetBalloons());
200 }
Jack Jansenee6eeb12000-06-02 21:28:52 +0000201#endif
Jack Jansendff77702001-09-05 22:07:52 +0000202#if !TARGET_API_MAC_OSX
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000203 if ( item == OPT_CMDLINE ) {
Jack Jansendff77702001-09-05 22:07:52 +0000204 int old_argc = *argcp;
205 int i;
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000206 int new_argc, newer_argc;
207 char **new_argv, **newer_argv;
208
209 new_argc = ccommand(&new_argv);
210 newer_argc = (new_argc-1) + old_argc;
211 newer_argv = malloc((newer_argc+1)*sizeof(char *));
212 if( !newer_argv )
213 Py_FatalError("Cannot malloc argv\n");
214 for(i=0; i<old_argc; i++)
215 newer_argv[i] = (*argvp)[i];
216 for(i=old_argc; i<=newer_argc; i++) /* Copy the NULL too */
217 newer_argv[i] = new_argv[i-old_argc+1];
218 *argvp = newer_argv;
219 *argcp = newer_argc;
220
221 /* XXXX Is it not safe to use free() here, apparently */
222 }
Jack Jansendff77702001-09-05 22:07:52 +0000223#endif /* !TARGET_API_MAC_OSX */
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000224#define OPT_ITEM(num, var) \
225 if ( item == (num) ) { \
226 p->var = !p->var; \
227 GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
Jack Jansen08c3be31997-04-08 15:27:00 +0000228 SetControlValue(handle, (short)p->var); \
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000229 }
230
231 OPT_ITEM(OPT_INSPECT, inspect);
232 OPT_ITEM(OPT_VERBOSE, verbose);
Jack Jansenff5d8aa2001-09-01 22:37:54 +0000233 if ( item == OPT_VERBOSEVERBOSE ) {
234 if ( p->verbose == 2 )
235 p->verbose = 1;
236 else
237 p->verbose = 2;
238 GetDialogItem(dialog, OPT_VERBOSE, &type, (Handle *)&handle, &rect);
239 SetControlValue(handle, 1);
240 }
241 GetDialogItem(dialog, OPT_VERBOSEVERBOSE, &type, (Handle *)&handle, &rect);
242 SetControlValue(handle, p->verbose == 2);
Jack Jansen36b983c1997-09-09 13:53:21 +0000243 OPT_ITEM(OPT_OPTIMIZE, optimize);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000244 OPT_ITEM(OPT_UNBUFFERED, unbuffered);
245 OPT_ITEM(OPT_DEBUGGING, debugging);
Jack Jansen4a5eb962000-09-22 21:50:11 +0000246 if ( item == OPT_KEEPALWAYS ) p->keep_console = POPT_KEEPCONSOLE_ALWAYS;
247 if ( item == OPT_KEEPOUTPUT ) p->keep_console = POPT_KEEPCONSOLE_OUTPUT;
248 if ( item == OPT_KEEPERROR ) p->keep_console = POPT_KEEPCONSOLE_ERROR;
249 if ( item == OPT_KEEPNEVER ) p->keep_console = POPT_KEEPCONSOLE_NEVER;
250 GetDialogItem(dialog, OPT_KEEPALWAYS, &type, (Handle *)&handle, &rect);
251 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_ALWAYS));
252 GetDialogItem(dialog, OPT_KEEPOUTPUT, &type, (Handle *)&handle, &rect);
253 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_OUTPUT));
254 GetDialogItem(dialog, OPT_KEEPERROR, &type, (Handle *)&handle, &rect);
255 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_ERROR));
256 GetDialogItem(dialog, OPT_KEEPNEVER, &type, (Handle *)&handle, &rect);
257 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_NEVER));
Jack Jansen0c6d0372000-05-05 23:11:14 +0000258 OPT_ITEM(OPT_TABWARN, tabwarn);
Jack Jansen36b983c1997-09-09 13:53:21 +0000259 OPT_ITEM(OPT_NOSITE, nosite);
Jack Jansenff5d8aa2001-09-01 22:37:54 +0000260 OPT_ITEM(OPT_DIVISIONWARN, divisionwarn);
261 OPT_ITEM(OPT_UNIXNEWLINES, unixnewlines);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000262
263#undef OPT_ITEM
264 }
Jack Jansen08c3be31997-04-08 15:27:00 +0000265 DisposeDialog(dialog);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000266}
267
268/*
269** Initialization code, shared by interpreter and applets
270*/
271static void
Jack Jansen52ac0371997-01-15 15:49:08 +0000272init_common(int *argcp, char ***argvp, int embedded)
Jack Jansen01fbc681996-02-28 15:42:47 +0000273{
Jack Jansen3f7d2b41996-09-06 22:21:07 +0000274 /* Remember resource fork refnum, for later */
275 PyMac_AppRefNum = CurResFile();
276
Jack Jansen01fbc681996-02-28 15:42:47 +0000277 /* Initialize toolboxes */
278 init_mac_world();
279
280#ifdef USE_MAC_SHARED_LIBRARY
281 /* Add the shared library to the stack of resource files */
Jack Jansen87c485c1998-07-31 09:38:01 +0000282 (void)PyMac_init_process_location();
Jack Jansen01fbc681996-02-28 15:42:47 +0000283 PyMac_AddLibResources();
284#endif
285
Jack Jansen2d1306b2000-04-07 09:10:49 +0000286#if defined(USE_GUSI1)
Jack Jansen01fbc681996-02-28 15:42:47 +0000287 /* Setup GUSI */
288 GUSIDefaultSetup();
Jack Jansenf6865f71996-09-04 15:24:59 +0000289 PyMac_SetGUSISpin();
Jack Jansen3f7d2b41996-09-06 22:21:07 +0000290 PyMac_SetGUSIOptions();
Jack Jansen01fbc681996-02-28 15:42:47 +0000291#endif
Jack Jansen2d1306b2000-04-07 09:10:49 +0000292#if defined(USE_GUSI)
293 atexit(PyMac_StopGUSISpin);
294#endif
Jack Jansen01fbc681996-02-28 15:42:47 +0000295
296#ifdef USE_SIOUX
297 /* Set various SIOUX flags. Some are changed later based on options */
Jack Jansendff77702001-09-05 22:07:52 +0000298#if 0
299 SIOUXSettings.standalone = 0; /* XXXX Attempting to keep sioux from eating events */
300#endif
Jack Jansen01fbc681996-02-28 15:42:47 +0000301 SIOUXSettings.asktosaveonclose = 0;
302 SIOUXSettings.showstatusline = 0;
303 SIOUXSettings.tabspaces = 4;
304#endif
305
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000306 /* Get options from preference file (or from applet resource fork) */
Jack Jansendc86f9e2000-10-12 21:23:19 +0000307 PyMac_options.keep_console = POPT_KEEPCONSOLE_OUTPUT; /* default-default */
Jack Jansendff77702001-09-05 22:07:52 +0000308 PyMac_options.unixnewlines = 1;
309#if !TARGET_API_MAC_OSX
Jack Jansendc86f9e2000-10-12 21:23:19 +0000310 PyMac_PreferenceOptions(&PyMac_options);
Jack Jansendff77702001-09-05 22:07:52 +0000311#endif
312
Jack Jansen52ac0371997-01-15 15:49:08 +0000313 if ( embedded ) {
314 static char *emb_argv[] = {"embedded-python", 0};
315
316 *argcp = 1;
317 *argvp = emb_argv;
318 } else {
319 /* Create argc/argv. Do it before we go into the options event loop. */
Jack Jansendc86f9e2000-10-12 21:23:19 +0000320 *argcp = PyMac_GetArgv(argvp, PyMac_options.noargs);
Jack Jansendff77702001-09-05 22:07:52 +0000321#if !TARGET_API_MAC_OSX
Jack Jansenc00df0b2001-01-16 15:54:58 +0000322#ifndef NO_ARGV0_CHDIR
Jack Jansen660bb1d2000-07-18 09:40:39 +0000323 if (*argcp >= 1 && (*argvp)[0] && (*argvp)[0][0]) {
324 /* Workaround for MacOS X, which currently (DP4) doesn't set
325 ** the working folder correctly
326 */
327 char app_wd[256], *p;
328
329 strncpy(app_wd, (*argvp)[0], 256);
Jack Jansen660bb1d2000-07-18 09:40:39 +0000330 p = strrchr(app_wd, ':');
331 if ( p ) *p = 0;
Jack Jansen660bb1d2000-07-18 09:40:39 +0000332 chdir(app_wd);
333 }
334#endif
Jack Jansendff77702001-09-05 22:07:52 +0000335#endif
Jack Jansen52ac0371997-01-15 15:49:08 +0000336 /* Do interactive option setting, if allowed and <option> depressed */
Jack Jansendc86f9e2000-10-12 21:23:19 +0000337 PyMac_InteractiveOptions(&PyMac_options, argcp, argvp);
Jack Jansen52ac0371997-01-15 15:49:08 +0000338 }
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000339
340 /* Copy selected options to where the machine-independent stuff wants it */
Jack Jansendc86f9e2000-10-12 21:23:19 +0000341 Py_VerboseFlag = PyMac_options.verbose;
342/* Py_SuppressPrintingFlag = PyMac_options.suppress_print; */
343 Py_OptimizeFlag = PyMac_options.optimize;
344 Py_DebugFlag = PyMac_options.debugging;
345 Py_NoSiteFlag = PyMac_options.nosite;
346 Py_TabcheckFlag = PyMac_options.tabwarn;
Jack Jansenff5d8aa2001-09-01 22:37:54 +0000347 Py_DivisionWarningFlag = PyMac_options.divisionwarn;
Jack Jansendff77702001-09-05 22:07:52 +0000348#if !TARGET_API_MAC_OSX
Jack Jansendc86f9e2000-10-12 21:23:19 +0000349 if ( PyMac_options.noargs ) {
Jack Jansene3ae0df1997-06-03 15:28:29 +0000350 /* don't process events at all without the scripts permission */
351 PyMacSchedParams scp;
352
353 PyMac_GetSchedParams(&scp);
354 scp.process_events = 0;
355 /* Should we disable command-dot as well? */
356 PyMac_SetSchedParams(&scp);
357 }
Jack Jansendff77702001-09-05 22:07:52 +0000358#endif /* !TARGET_API_MAC_OSX */
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000359
360 /* Set buffering */
Jack Jansendc86f9e2000-10-12 21:23:19 +0000361 if (PyMac_options.unbuffered) {
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000362#ifndef MPW
363 setbuf(stdout, (char *)NULL);
364 setbuf(stderr, (char *)NULL);
365#else
366 /* On MPW (3.2) unbuffered seems to hang */
367 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
368 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
369#endif
370 }
Jack Jansen8c693211997-01-07 16:19:42 +0000371#if __profile__ == 1
372 /* collectSummary or collectDetailed, timebase, #routines, max stack depth */
Jack Jansene7424871999-09-30 11:20:11 +0000373 ProfilerInit(collectSummary, bestTimeBase, 8000, 250);
Jack Jansen8c693211997-01-07 16:19:42 +0000374#endif
Jack Jansen7330b391997-08-08 14:56:41 +0000375
376 /* Tell the rest of python about our argc/argv */
377 orig_argc = *argcp; /* For Py_GetArgcArgv() */
378 orig_argv = *argvp;
379 Py_SetProgramName((*argvp)[0]);
Jack Jansen01fbc681996-02-28 15:42:47 +0000380}
381
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000382/*
383** Inspection mode after script/applet termination
384*/
385static int
Jack Jansendff77702001-09-05 22:07:52 +0000386run_inspect(void)
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000387{
388 int sts = 0;
389
Jack Jansendc86f9e2000-10-12 21:23:19 +0000390 if (PyMac_options.inspect && isatty((int)fileno(stdin)))
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000391 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
392 return sts;
393}
Jack Jansen6438e1d2001-09-11 11:29:31 +0000394
Jack Jansen696c9581995-08-14 12:33:20 +0000395#ifdef USE_MAC_APPLET_SUPPORT
396/* Applet support */
397
398/* Run a compiled Python Python script from 'PYC ' resource __main__ */
399static int
Jack Jansendff77702001-09-05 22:07:52 +0000400run_main_resource(void)
Jack Jansen696c9581995-08-14 12:33:20 +0000401{
402 Handle h;
403 long size;
404 PyObject *code;
405 PyObject *result;
406
407 h = GetNamedResource('PYC ', "\p__main__");
408 if (h == NULL) {
409 Alert(NOPYC_ALERT, NULL);
410 return 1;
411 }
412 size = GetResourceSizeOnDisk(h);
413 HLock(h);
414 code = PyMarshal_ReadObjectFromString(*h + 8, (int)(size - 8));
415 HUnlock(h);
416 ReleaseResource(h);
417 if (code == NULL) {
418 PyErr_Print();
419 return 1;
420 }
421 result = PyImport_ExecCodeModule("__main__", code);
422 Py_DECREF(code);
423 if (result == NULL) {
424 PyErr_Print();
425 return 1;
426 }
427 Py_DECREF(result);
428 return 0;
429}
430
431/* Initialization sequence for applets */
432void
Jack Jansendff77702001-09-05 22:07:52 +0000433PyMac_InitApplet(void)
Jack Jansen696c9581995-08-14 12:33:20 +0000434{
Guido van Rossumb0f3c821994-08-23 13:34:25 +0000435 int argc;
436 char **argv;
Jack Jansen696c9581995-08-14 12:33:20 +0000437 int err;
438
Jack Jansen52ac0371997-01-15 15:49:08 +0000439 init_common(&argc, &argv, 0);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000440
Jack Jansen696c9581995-08-14 12:33:20 +0000441 Py_Initialize();
442 PySys_SetArgv(argc, argv);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000443
Jack Jansen696c9581995-08-14 12:33:20 +0000444 err = run_main_resource();
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000445
446 err = (run_inspect() || err);
447
Jack Jansen696c9581995-08-14 12:33:20 +0000448 fflush(stderr);
449 fflush(stdout);
Jack Jansen0168f271995-10-27 13:32:30 +0000450 PyMac_Exit(err);
Jack Jansen696c9581995-08-14 12:33:20 +0000451 /* XXX Should we bother to Py_Exit(sts)? */
452}
453
Jack Jansen52ac0371997-01-15 15:49:08 +0000454/*
455** Hook for embedding python.
456*/
457void
Jack Jansendff77702001-09-05 22:07:52 +0000458PyMac_Initialize(void)
Jack Jansen52ac0371997-01-15 15:49:08 +0000459{
460 int argc;
461 char **argv;
462
463 init_common(&argc, &argv, 1);
464 Py_Initialize();
465 PySys_SetArgv(argc, argv);
466}
467
Jack Jansen696c9581995-08-14 12:33:20 +0000468#endif /* USE_MAC_APPLET_SUPPORT */
469
Jack Jansen5ded1bf2001-10-30 22:48:36 +0000470#if TARGET_API_MAC_OSX /* Really: TARGET_API_MAC_CARBON */
Jack Jansen998a40a2001-09-11 13:08:10 +0000471
472static int
473locateResourcePy(char * resourceName, char * resourceURLCStr, int length) {
474 CFBundleRef mainBundle = NULL;
475 CFURLRef URL, absoluteURL;
476 CFStringRef filenameString, filepathString, rsrcString;
477 CFIndex size, i;
Jack Jansen5ded1bf2001-10-30 22:48:36 +0000478 CFArrayRef arrayRef = NULL;
479 int success = 0;
480
481#if TARGET_API_MAC_OSX
482 CFURLPathStyle thePathStyle = kCFURLPOSIXPathStyle;
483#else
484 CFURLPathStyle thePathStyle = kCFURLHFSPathStyle;
485#endif
Jack Jansen998a40a2001-09-11 13:08:10 +0000486
487 /* Get a reference to our main bundle */
488 mainBundle = CFBundleGetMainBundle();
489
Jack Jansen5ded1bf2001-10-30 22:48:36 +0000490 /* If we are running inside a bundle, look through it. Otherwise, do nothing. */
491 if (mainBundle) {
492 /* Create a CFString with the resource name in it */
493 rsrcString = CFStringCreateWithCString(0, resourceName, kCFStringEncodingMacRoman);
Jack Jansen998a40a2001-09-11 13:08:10 +0000494
Jack Jansen5ded1bf2001-10-30 22:48:36 +0000495 /* Look for py files in the main bundle by type */
496 arrayRef = CFBundleCopyResourceURLsOfType( mainBundle,
497 CFSTR("py"),
498 NULL );
Jack Jansen998a40a2001-09-11 13:08:10 +0000499
Jack Jansen5ded1bf2001-10-30 22:48:36 +0000500 /* See if there are any filename matches */
501 size = CFArrayGetCount(arrayRef);
502 for (i = 0; i < size; i++) {
503 URL = CFArrayGetValueAtIndex(arrayRef, i);
504 filenameString = CFURLCopyLastPathComponent(URL);
505 if (CFStringCompare(filenameString, rsrcString, 0) == kCFCompareEqualTo) {
506 /* We found a match, get the file's full path */
507 absoluteURL = CFURLCopyAbsoluteURL(URL);
508 filepathString = CFURLCopyFileSystemPath(absoluteURL, thePathStyle);
509 CFRelease(absoluteURL);
Jack Jansen998a40a2001-09-11 13:08:10 +0000510
Jack Jansen5ded1bf2001-10-30 22:48:36 +0000511 /* Copy the full path into the caller's character buffer */
512 success = CFStringGetCString(filepathString, resourceURLCStr, length,
513 kCFStringEncodingMacRoman);
Jack Jansen998a40a2001-09-11 13:08:10 +0000514
Jack Jansen5ded1bf2001-10-30 22:48:36 +0000515 CFRelease(filepathString);
516 }
517 CFRelease(filenameString);
518 }
519 CFRelease(arrayRef);
520 CFRelease(rsrcString);
521 }
Jack Jansen998a40a2001-09-11 13:08:10 +0000522 return success;
523}
524
Jack Jansen5ded1bf2001-10-30 22:48:36 +0000525#endif /* TARGET_API_MAC_CARBON */
526
527#if TARGET_API_MAC_OSX
528
Jack Jansendff77702001-09-05 22:07:52 +0000529int
530main(int argc, char **argv)
531{
Jack Jansen998a40a2001-09-11 13:08:10 +0000532 int i;
533 static char scriptpath[1024];
534 char *script = NULL;
535
536 /* First we see whether we have __rawmain__.py and run that if it
537 ** is there
538 */
539 if (locateResourcePy("__rawmain__.py", scriptpath, 1024)) {
540 /* If we have a raw main we don't do AppleEvent processing.
541 ** Notice that this also means we keep the -psn.... argv[1]
542 ** value intact. Not sure whether that is important to someone,
543 ** but you never know...
544 */
545 script = scriptpath;
546 } else {
547 /* Otherwise we look for __main__.py. Whether that is
548 ** found or not we also process AppleEvent arguments.
549 */
550 if (locateResourcePy("__main__.py", scriptpath, 1024))
551 script = scriptpath;
552
553 printf("original argc=%d\n", argc);
554 for(i=0; i<argc; i++) printf("original argv[%d] = \"%s\"\n", i, argv[i]);
555
556 init_common(&argc, &argv, 0);
557
558 printf("modified argc=%d\n", argc);
559 for(i=0; i<argc; i++) printf("modified argv[%d] = \"%s\"\n", i, argv[i]);
560 }
561
562 Py_Main(argc, argv, script);
563 return 0;
Jack Jansendff77702001-09-05 22:07:52 +0000564}
565
566#else
567
Jack Jansen696c9581995-08-14 12:33:20 +0000568/* For normal application */
569void
Jack Jansendff77702001-09-05 22:07:52 +0000570PyMac_InitApplication(void)
Jack Jansen696c9581995-08-14 12:33:20 +0000571{
572 int argc;
573 char **argv;
574
Jack Jansen5ded1bf2001-10-30 22:48:36 +0000575 static char scriptpath[1024];
576 char *script = NULL;
577
Jack Jansen52ac0371997-01-15 15:49:08 +0000578 init_common(&argc, &argv, 0);
Jack Jansen5ded1bf2001-10-30 22:48:36 +0000579
580#if TARGET_API_MAC_OSX /* Really: TARGET_API_MAC_CARBON */
581 /* If we are running inside of a bundle, and a __main__.py is available, use it */
582 if (locateResourcePy("__main__.py", scriptpath, 1024))
583 script = scriptpath;
584#endif
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000585
Jack Jansen696c9581995-08-14 12:33:20 +0000586 if ( argc > 1 ) {
587 /* We're running a script. Attempt to change current directory */
588 char curwd[256], *endp;
589
590 strcpy(curwd, argv[1]);
591 endp = strrchr(curwd, ':');
592 if ( endp && endp > curwd ) {
593 *endp = '\0';
594
595 chdir(curwd);
Jack Jansen2d1306b2000-04-07 09:10:49 +0000596#ifdef USE_GUSI1
Jack Jansen378815c1996-03-06 16:21:34 +0000597 /* Change MacOS's idea of wd too */
598 PyMac_FixGUSIcd();
599#endif
Jack Jansen696c9581995-08-14 12:33:20 +0000600 }
Jack Jansencbed91b2001-08-03 13:31:36 +0000601 /* Check that the first argument is a text file */
602 if ( PyMac_getfiletype(argv[1]) != 'TEXT' ) {
603 Alert(NOTASCRIPT_ID, NULL);
604 exit(0);
605 }
Jack Jansen696c9581995-08-14 12:33:20 +0000606 }
Jack Jansen5ded1bf2001-10-30 22:48:36 +0000607 Py_Main(argc, argv, script);
Jack Jansen696c9581995-08-14 12:33:20 +0000608}
Jack Jansendff77702001-09-05 22:07:52 +0000609#endif /* TARGET_API_MAC_OSX */
Jack Jansen696c9581995-08-14 12:33:20 +0000610
Jack Jansen696c9581995-08-14 12:33:20 +0000611/* Main program */
612
Jack Jansen76ceece1996-08-19 11:18:24 +0000613static void
Jack Jansen998a40a2001-09-11 13:08:10 +0000614Py_Main(int argc, char **argv, char *filename)
Jack Jansen696c9581995-08-14 12:33:20 +0000615{
Jack Jansen696c9581995-08-14 12:33:20 +0000616 int sts;
617 char *command = NULL;
Jack Jansen696c9581995-08-14 12:33:20 +0000618 FILE *fp = stdin;
Jack Jansen696c9581995-08-14 12:33:20 +0000619
Jack Jansen998a40a2001-09-11 13:08:10 +0000620 if ( filename ) {
621 /* Someone else has found our "script" already */
622 argv[0] = filename;
623 } else {
624 filename = argv[1];
625 argv++;
626 argc--;
627 }
Jack Jansen696c9581995-08-14 12:33:20 +0000628
629 if (Py_VerboseFlag ||
Jack Jansendff77702001-09-05 22:07:52 +0000630 (command == NULL && filename == NULL && isatty((int)fileno(fp))))
Jack Jansen65c3ee02000-09-08 10:20:37 +0000631 fprintf(stderr, "Python %s on %s\n%s\n",
632 Py_GetVersion(), Py_GetPlatform(), COPYRIGHT);
Jack Jansen696c9581995-08-14 12:33:20 +0000633
634 if (filename != NULL) {
635 if ((fp = fopen(filename, "r")) == NULL) {
636 fprintf(stderr, "%s: can't open file '%s'\n",
637 argv[0], filename);
Jack Jansen0168f271995-10-27 13:32:30 +0000638 PyMac_Exit(2);
Jack Jansen696c9581995-08-14 12:33:20 +0000639 }
640 }
641
Jack Jansendff77702001-09-05 22:07:52 +0000642#if !TARGET_API_MAC_OSX
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000643 /* We initialize the menubar here, hoping SIOUX is initialized by now */
Jack Jansen3469e991996-09-06 00:30:45 +0000644 PyMac_InitMenuBar();
Jack Jansendff77702001-09-05 22:07:52 +0000645#endif
646
Jack Jansen696c9581995-08-14 12:33:20 +0000647 Py_Initialize();
648
unknownd1054ef2001-07-04 22:37:19 +0000649 PyUnicode_SetDefaultEncoding(PyMac_getscript());
650
Jack Jansen998a40a2001-09-11 13:08:10 +0000651 PySys_SetArgv(argc, argv);
Jack Jansen696c9581995-08-14 12:33:20 +0000652
653 if (filename == NULL && isatty((int)fileno(fp))) {
654 FILE *fp = fopen(STARTUP, "r");
655 if (fp != NULL) {
656 (void) PyRun_SimpleFile(fp, STARTUP);
657 PyErr_Clear();
658 fclose(fp);
659 }
660 }
661 sts = PyRun_AnyFile(
662 fp, filename == NULL ? "<stdin>" : filename) != 0;
663 if (filename != NULL)
664 fclose(fp);
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000665
666 if ( filename != NULL || command != NULL )
667 sts = (run_inspect() || sts);
Jack Jansen696c9581995-08-14 12:33:20 +0000668
669 Py_Exit(sts);
670 /*NOTREACHED*/
671}
672
Jack Jansendff77702001-09-05 22:07:52 +0000673#if !TARGET_API_MAC_OSX
Jack Jansen0168f271995-10-27 13:32:30 +0000674/*
Jack Jansen8413b472000-10-19 22:02:16 +0000675** Reset the "unseen output" flag
676*/
677void
Jack Jansendff77702001-09-05 22:07:52 +0000678PyMac_OutputSeen(void)
Jack Jansen8413b472000-10-19 22:02:16 +0000679{
Jack Jansen657ba272001-02-17 22:02:07 +0000680 if ( console_output_state == STATE_UNKNOWN )
681 PyMac_InitMenuBar();
Jack Jansen8a387142001-02-11 01:08:04 +0000682 console_output_state = STATE_LASTREAD;
Jack Jansen8413b472000-10-19 22:02:16 +0000683}
684
685/*
Jack Jansen8a387142001-02-11 01:08:04 +0000686** Set the "unseen output" flag
687*/
688void
Jack Jansendff77702001-09-05 22:07:52 +0000689PyMac_OutputNotSeen(void)
Jack Jansen8a387142001-02-11 01:08:04 +0000690{
Jack Jansen657ba272001-02-17 22:02:07 +0000691 if ( console_output_state == STATE_UNKNOWN )
692 PyMac_InitMenuBar();
Jack Jansen8a387142001-02-11 01:08:04 +0000693 console_output_state = STATE_LASTWRITE;
694}
Jack Jansen15f1c082001-04-25 22:07:27 +0000695
696/*
697** Override abort() - The default one is not what we want.
698*/
699void
Jack Jansendff77702001-09-05 22:07:52 +0000700abort(void)
Jack Jansen15f1c082001-04-25 22:07:27 +0000701{
702 console_output_state = STATE_LASTWRITE;
703 PyMac_Exit(1);
704}
Jack Jansendff77702001-09-05 22:07:52 +0000705#endif /* !TARGET_API_MAC_OSX */
Jack Jansen8a387142001-02-11 01:08:04 +0000706
707/*
Jack Jansen0168f271995-10-27 13:32:30 +0000708** Terminate application
709*/
Jack Jansen76ceece1996-08-19 11:18:24 +0000710void
Jack Jansendff77702001-09-05 22:07:52 +0000711PyMac_Exit(int status)
Jack Jansen0168f271995-10-27 13:32:30 +0000712{
Jack Jansendff77702001-09-05 22:07:52 +0000713#ifdef USE_SIOUX
Jack Jansen4a5eb962000-09-22 21:50:11 +0000714 int keep = 0;
Jack Jansendff77702001-09-05 22:07:52 +0000715#endif
Jack Jansen8c693211997-01-07 16:19:42 +0000716
717#if __profile__ == 1
718 ProfilerDump("\pPython Profiler Results");
719 ProfilerTerm();
720#endif
Jack Jansen0168f271995-10-27 13:32:30 +0000721
Jack Jansen1e8557a1995-11-10 14:51:26 +0000722#ifdef USE_SIOUX
Jack Jansendc86f9e2000-10-12 21:23:19 +0000723 switch (PyMac_options.keep_console) {
Jack Jansen4a5eb962000-09-22 21:50:11 +0000724 case POPT_KEEPCONSOLE_NEVER:
725 keep = 0;
726 break;
727 case POPT_KEEPCONSOLE_OUTPUT:
Jack Jansen8a387142001-02-11 01:08:04 +0000728 if (console_output_state == STATE_LASTWRITE ||
729 console_output_state == STATE_UNKNOWN )
Jack Jansen4a5eb962000-09-22 21:50:11 +0000730 keep = 1;
731 else
732 keep = 0;
733 break;
734 case POPT_KEEPCONSOLE_ERROR:
735 keep = (status != 0);
736 break;
737 default:
738 keep = 1;
739 }
Jack Jansen1e8557a1995-11-10 14:51:26 +0000740 if (keep) {
741 SIOUXSettings.standalone = 1;
742 SIOUXSettings.autocloseonquit = 0;
Jack Jansen415571c1996-03-25 15:46:03 +0000743 SIOUXSetTitle("\p\307terminated\310");
Jack Jansen15f1c082001-04-25 22:07:27 +0000744 PyMac_RaiseConsoleWindow();
Jack Jansencaa7c461997-06-12 10:49:13 +0000745 PyMac_RestoreMenuBar();
Jack Jansene44545f1997-05-07 15:48:54 +0000746#ifdef USE_MSL
747 /*
748 ** Temporary workaround: autocloseonquit clearing does not
749 ** currently work for the MSL/GUSI combo.
750 */
751 while(getchar() > 0);
752#endif
Jack Jansen1e8557a1995-11-10 14:51:26 +0000753 }
Jack Jansen0168f271995-10-27 13:32:30 +0000754 else
755 SIOUXSettings.autocloseonquit = 1;
Jack Jansenf6865f71996-09-04 15:24:59 +0000756#endif /* USE_SIOUX */
Jack Jansen0168f271995-10-27 13:32:30 +0000757
758 exit(status);
759}
Jack Jansen696c9581995-08-14 12:33:20 +0000760
Jack Jansendff77702001-09-05 22:07:52 +0000761#if !TARGET_API_MAC_OSX
Jack Jansen696c9581995-08-14 12:33:20 +0000762/* Make the *original* argc/argv available to other modules.
763 This is rare, but it is needed by the secureware extension. */
764
765void
Jack Jansen9ae898b2000-07-11 21:16:03 +0000766Py_GetArgcArgv(int *argc,char ***argv)
Jack Jansen696c9581995-08-14 12:33:20 +0000767{
768 *argc = orig_argc;
769 *argv = orig_argv;
Guido van Rossumb0f3c821994-08-23 13:34:25 +0000770}
Jack Jansendff77702001-09-05 22:07:52 +0000771#endif
Jack Jansen1d2f8631996-08-02 15:16:16 +0000772
773/* More cruft that shouldn't really be here, used in sysmodule.c */
Jack Jansendff77702001-09-05 22:07:52 +0000774#if !TARGET_API_MAC_OSX
775/* Return the program name -- some code out there needs this. */
776char *
777Py_GetProgramFullPath(void)
778{
779 return orig_argv[0];
780}
Jack Jansen1d2f8631996-08-02 15:16:16 +0000781
782char *
Jack Jansendff77702001-09-05 22:07:52 +0000783Py_GetPrefix(void)
Jack Jansen1d2f8631996-08-02 15:16:16 +0000784{
Jack Jansenac625691997-09-08 13:22:22 +0000785 return PyMac_GetPythonDir();
Jack Jansen1d2f8631996-08-02 15:16:16 +0000786}
787
788char *
Jack Jansendff77702001-09-05 22:07:52 +0000789Py_GetExecPrefix(void)
Jack Jansen1d2f8631996-08-02 15:16:16 +0000790{
Jack Jansenac625691997-09-08 13:22:22 +0000791 return PyMac_GetPythonDir();
Jack Jansen1d2f8631996-08-02 15:16:16 +0000792}
Jack Jansen8a387142001-02-11 01:08:04 +0000793
794int
Jack Jansendff77702001-09-05 22:07:52 +0000795PyMac_GetDelayConsoleFlag(void)
Jack Jansen8a387142001-02-11 01:08:04 +0000796{
797 return (int)PyMac_options.delayconsole;
Jack Jansenff5d8aa2001-09-01 22:37:54 +0000798}
799
800#ifndef WITHOUT_UNIX_NEWLINES
801/*
802** Experimental feature (for 2.2a2): optionally allow unix newlines
803** as well as Mac newlines on input. We replace a lowlevel
804** MSL routine to accomplish this.
805*/
806void
807__convert_to_newlines(unsigned char * buf, size_t * n_ptr)
808{
809 unsigned char *p;
810 size_t n = *n_ptr;
811
812 for(p=buf; n > 0; p++, n--)
813 if ( *p == '\r' ) *p = '\n';
814 else if ( *p == '\n' && !PyMac_options.unixnewlines )
815 *p = '\r';
816}
817#endif /* WITHOUT_UNIX_NEWLINES */
Jack Jansendff77702001-09-05 22:07:52 +0000818#endif /* !TARGET_API_MAC_OSX */
Jack Jansenff5d8aa2001-09-01 22:37:54 +0000819