1 /***********************************************************
2 Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* Python interpreter main program */
28 #include "pythonresources.h"
34 #include <Resources.h>
42 #include <Appearance.h>
43 #endif /* USE_APPEARANCE */
56 #define STARTUP "PythonStartup"
58 extern int Py_DebugFlag
; /* For parser.c, declared in pythonrun.c */
59 extern int Py_VerboseFlag
; /* For import.c, declared in pythonrun.c */
60 short PyMac_AppRefNum
; /* RefNum of application resource fork */
62 /* For Py_GetArgcArgv(); set by main() */
63 static char **orig_argv
;
66 PyMac_PrefRecord options
;
68 static void Py_Main
Py_PROTO((int, char **)); /* Forward */
69 void PyMac_Exit
Py_PROTO((int)); /* Forward */
71 static void init_appearance()
77 err
= Gestalt(gestaltAppearanceAttr
,&response
);
78 if ( err
) goto no_appearance
;
79 if ( !(response
&(1<<gestaltAppearanceExists
)) ) goto no_appearance
;
80 /* XXXX Should we check the version? Compat-mode? */
81 PyMac_AppearanceCompliant
= 1;
84 #endif /* USE_APPEARANCE */
86 /* Initialize the Mac toolbox world */
95 InitGraf(&qd
.thePort
);
107 ** PyMac_InteractiveOptions - Allow user to set options if option key is pressed
110 PyMac_InteractiveOptions(PyMac_PrefRecord
*p
, int *argcp
, char ***argvp
)
115 ControlHandle handle
;
118 int old_argc
= *argcp
;
122 ** If the preferences disallows interactive options we return,
123 ** similarly of <option> isn't pressed.
125 if (p
->nointopt
) return;
128 map
= (unsigned char *)rmap
;
129 if ( ( map
[0x3a>>3] & (1<<(0x3a&7)) ) == 0 ) /* option key is 3a */
132 dialog
= GetNewDialog(OPT_DIALOG
, NULL
, (WindowPtr
)-1);
133 if ( dialog
== NULL
) {
134 printf("Option dialog not found - cannot set options\n");
137 SetDialogDefaultItem(dialog
, OPT_OK
);
138 SetDialogCancelItem(dialog
, OPT_CANCEL
);
140 /* Set default values */
141 #define SET_OPT_ITEM(num, var) \
142 GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
143 SetControlValue(handle, (short)p->var);
145 SET_OPT_ITEM(OPT_INSPECT
, inspect
);
146 SET_OPT_ITEM(OPT_VERBOSE
, verbose
);
147 SET_OPT_ITEM(OPT_OPTIMIZE
, optimize
);
148 SET_OPT_ITEM(OPT_UNBUFFERED
, unbuffered
);
149 SET_OPT_ITEM(OPT_DEBUGGING
, debugging
);
150 SET_OPT_ITEM(OPT_KEEPNORMAL
, keep_normal
);
151 SET_OPT_ITEM(OPT_KEEPERROR
, keep_error
);
152 SET_OPT_ITEM(OPT_OLDEXC
, oldexc
);
153 SET_OPT_ITEM(OPT_NOSITE
, nosite
);
154 /* The rest are not settable interactively */
160 ModalDialog(NULL
, &item
);
161 if ( item
== OPT_OK
)
163 if ( item
== OPT_CANCEL
) {
164 DisposeDialog(dialog
);
167 if ( item
== OPT_HELP
) {
168 HMSetBalloons(!HMGetBalloons());
170 if ( item
== OPT_CMDLINE
) {
171 int new_argc
, newer_argc
;
172 char **new_argv
, **newer_argv
;
174 new_argc
= ccommand(&new_argv
);
175 newer_argc
= (new_argc
-1) + old_argc
;
176 newer_argv
= malloc((newer_argc
+1)*sizeof(char *));
178 Py_FatalError("Cannot malloc argv\n");
179 for(i
=0; i
<old_argc
; i
++)
180 newer_argv
[i
] = (*argvp
)[i
];
181 for(i
=old_argc
; i
<=newer_argc
; i
++) /* Copy the NULL too */
182 newer_argv
[i
] = new_argv
[i
-old_argc
+1];
186 /* XXXX Is it not safe to use free() here, apparently */
188 #define OPT_ITEM(num, var) \
189 if ( item == (num) ) { \
191 GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
192 SetControlValue(handle, (short)p->var); \
195 OPT_ITEM(OPT_INSPECT
, inspect
);
196 OPT_ITEM(OPT_VERBOSE
, verbose
);
197 OPT_ITEM(OPT_OPTIMIZE
, optimize
);
198 OPT_ITEM(OPT_UNBUFFERED
, unbuffered
);
199 OPT_ITEM(OPT_DEBUGGING
, debugging
);
200 OPT_ITEM(OPT_KEEPNORMAL
, keep_normal
);
201 OPT_ITEM(OPT_KEEPERROR
, keep_error
);
202 OPT_ITEM(OPT_OLDEXC
, oldexc
);
203 OPT_ITEM(OPT_NOSITE
, nosite
);
207 DisposeDialog(dialog
);
211 ** Initialization code, shared by interpreter and applets
214 init_common(int *argcp
, char ***argvp
, int embedded
)
216 /* Remember resource fork refnum, for later */
217 PyMac_AppRefNum
= CurResFile();
219 /* Initialize toolboxes */
222 #ifdef USE_MAC_SHARED_LIBRARY
223 /* Add the shared library to the stack of resource files */
224 (void)PyMac_init_process_location();
225 PyMac_AddLibResources();
228 #if defined(USE_GUSI)
232 PyMac_SetGUSIOptions();
233 atexit(PyMac_StopGUSISpin
);
237 /* Set various SIOUX flags. Some are changed later based on options */
238 /* SIOUXSettings.standalone = 0; /* XXXX Attempting to keep sioux from eating events */
239 SIOUXSettings
.asktosaveonclose
= 0;
240 SIOUXSettings
.showstatusline
= 0;
241 SIOUXSettings
.tabspaces
= 4;
244 /* Get options from preference file (or from applet resource fork) */
245 options
.keep_error
= 1; /* default-default */
246 PyMac_PreferenceOptions(&options
);
249 static char *emb_argv
[] = {"embedded-python", 0};
254 /* Create argc/argv. Do it before we go into the options event loop. */
255 *argcp
= PyMac_GetArgv(argvp
, options
.noargs
);
257 /* Do interactive option setting, if allowed and <option> depressed */
258 PyMac_InteractiveOptions(&options
, argcp
, argvp
);
261 /* Copy selected options to where the machine-independent stuff wants it */
262 Py_VerboseFlag
= options
.verbose
;
263 /* Py_SuppressPrintingFlag = options.suppress_print; */
264 Py_OptimizeFlag
= options
.optimize
;
265 Py_DebugFlag
= options
.debugging
;
266 Py_NoSiteFlag
= options
.nosite
;
267 Py_UseClassExceptionsFlag
= !(options
.oldexc
);
268 if ( options
.noargs
) {
269 /* don't process events at all without the scripts permission */
270 PyMacSchedParams scp
;
272 PyMac_GetSchedParams(&scp
);
273 scp
.process_events
= 0;
274 /* Should we disable command-dot as well? */
275 PyMac_SetSchedParams(&scp
);
277 /* XXXX dispatch oldexc and nosite */
280 if (options
.unbuffered
) {
282 setbuf(stdout
, (char *)NULL
);
283 setbuf(stderr
, (char *)NULL
);
285 /* On MPW (3.2) unbuffered seems to hang */
286 setvbuf(stdout
, (char *)NULL
, _IOLBF
, BUFSIZ
);
287 setvbuf(stderr
, (char *)NULL
, _IOLBF
, BUFSIZ
);
291 /* collectSummary or collectDetailed, timebase, #routines, max stack depth */
292 ProfilerInit(collectSummary
, bestTimeBase
, 8000, 250);
295 /* Tell the rest of python about our argc/argv */
296 orig_argc
= *argcp
; /* For Py_GetArgcArgv() */
298 Py_SetProgramName((*argvp
)[0]);
302 ** Inspection mode after script/applet termination
309 if (options
.inspect
&& isatty((int)fileno(stdin
)))
310 sts
= PyRun_AnyFile(stdin
, "<stdin>") != 0;
314 #ifdef USE_MAC_APPLET_SUPPORT
317 /* Run a compiled Python Python script from 'PYC ' resource __main__ */
326 h
= GetNamedResource('PYC ', "\p__main__");
328 Alert(NOPYC_ALERT
, NULL
);
331 size
= GetResourceSizeOnDisk(h
);
333 code
= PyMarshal_ReadObjectFromString(*h
+ 8, (int)(size
- 8));
340 result
= PyImport_ExecCodeModule("__main__", code
);
342 if (result
== NULL
) {
350 /* Initialization sequence for applets */
358 init_common(&argc
, &argv
, 0);
361 PySys_SetArgv(argc
, argv
);
363 err
= run_main_resource();
365 err
= (run_inspect() || err
);
370 /* XXX Should we bother to Py_Exit(sts)? */
374 ** Hook for embedding python.
382 init_common(&argc
, &argv
, 1);
384 PySys_SetArgv(argc
, argv
);
387 #endif /* USE_MAC_APPLET_SUPPORT */
389 /* For normal application */
391 PyMac_InitApplication()
396 init_common(&argc
, &argv
, 0);
399 /* We're running a script. Attempt to change current directory */
400 char curwd
[256], *endp
;
402 strcpy(curwd
, argv
[1]);
403 endp
= strrchr(curwd
, ':');
404 if ( endp
&& endp
> curwd
) {
409 /* Change MacOS's idea of wd too */
425 char *command
= NULL
;
426 char *filename
= NULL
;
431 if (Py_VerboseFlag
||
432 command
== NULL
&& filename
== NULL
&& isatty((int)fileno(fp
)))
433 fprintf(stderr
, "Python %s\n%s\n",
434 Py_GetVersion(), Py_GetCopyright());
436 if (filename
!= NULL
) {
437 if ((fp
= fopen(filename
, "r")) == NULL
) {
438 fprintf(stderr
, "%s: can't open file '%s'\n",
444 /* We initialize the menubar here, hoping SIOUX is initialized by now */
449 PySys_SetArgv(argc
-1, argv
+1);
451 if (filename
== NULL
&& isatty((int)fileno(fp
))) {
452 FILE *fp
= fopen(STARTUP
, "r");
454 (void) PyRun_SimpleFile(fp
, STARTUP
);
460 fp
, filename
== NULL
? "<stdin>" : filename
) != 0;
461 if (filename
!= NULL
)
464 if ( filename
!= NULL
|| command
!= NULL
)
465 sts
= (run_inspect() || sts
);
472 ** Terminate application
481 ProfilerDump("\pPython Profiler Results");
485 keep
= options
.keep_error
;
487 keep
= options
.keep_normal
;
491 SIOUXSettings
.standalone
= 1;
492 SIOUXSettings
.autocloseonquit
= 0;
493 SIOUXSetTitle("\p\307terminated\310");
494 PyMac_RestoreMenuBar();
497 ** Temporary workaround: autocloseonquit clearing does not
498 ** currently work for the MSL/GUSI combo.
500 while(getchar() > 0);
504 SIOUXSettings
.autocloseonquit
= 1;
505 #endif /* USE_SIOUX */
507 console_options
.pause_atexit
= keep
;
513 /* Return the program name -- some code out there needs this. */
515 Py_GetProgramFullPath()
521 /* Make the *original* argc/argv available to other modules.
522 This is rare, but it is needed by the secureware extension. */
525 Py_GetArgcArgv(argc
,argv
)
533 /* More cruft that shouldn't really be here, used in sysmodule.c */
538 return PyMac_GetPythonDir();
544 return PyMac_GetPythonDir();