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>
52 #define STARTUP "PythonStartup"
54 extern int Py_DebugFlag
; /* For parser.c, declared in pythonrun.c */
55 extern int Py_VerboseFlag
; /* For import.c, declared in pythonrun.c */
56 short PyMac_AppRefNum
; /* RefNum of application resource fork */
58 /* For Py_GetArgcArgv(); set by main() */
59 static char **orig_argv
;
62 PyMac_PrefRecord options
;
64 static void Py_Main
Py_PROTO((int, char **)); /* Forward */
65 void PyMac_Exit
Py_PROTO((int)); /* Forward */
67 /* Initialize the Mac toolbox world */
76 InitGraf(&qd
.thePort
);
87 ** PyMac_InteractiveOptions - Allow user to set options if option key is pressed
90 PyMac_InteractiveOptions(PyMac_PrefRecord
*p
, int *argcp
, char ***argvp
)
98 int old_argc
= *argcp
;
102 ** If the preferences disallows interactive options we return,
103 ** similarly of <option> isn't pressed.
105 if (p
->nointopt
) return;
108 map
= (unsigned char *)rmap
;
109 if ( ( map
[0x3a>>3] & (1<<(0x3a&7)) ) == 0 ) /* option key is 3a */
112 dialog
= GetNewDialog(OPT_DIALOG
, NULL
, (WindowPtr
)-1);
113 if ( dialog
== NULL
) {
114 printf("Option dialog not found - cannot set options\n");
117 SetDialogDefaultItem(dialog
, OPT_OK
);
118 SetDialogCancelItem(dialog
, OPT_CANCEL
);
120 /* Set default values */
121 #define SET_OPT_ITEM(num, var) \
122 GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
123 SetControlValue(handle, (short)p->var);
125 SET_OPT_ITEM(OPT_INSPECT
, inspect
);
126 SET_OPT_ITEM(OPT_VERBOSE
, verbose
);
127 SET_OPT_ITEM(OPT_OPTIMIZE
, optimize
);
128 SET_OPT_ITEM(OPT_UNBUFFERED
, unbuffered
);
129 SET_OPT_ITEM(OPT_DEBUGGING
, debugging
);
130 SET_OPT_ITEM(OPT_KEEPNORMAL
, keep_normal
);
131 SET_OPT_ITEM(OPT_KEEPERROR
, keep_error
);
132 SET_OPT_ITEM(OPT_OLDEXC
, oldexc
);
133 SET_OPT_ITEM(OPT_NOSITE
, nosite
);
134 /* The rest are not settable interactively */
140 ModalDialog(NULL
, &item
);
141 if ( item
== OPT_OK
)
143 if ( item
== OPT_CANCEL
) {
144 DisposeDialog(dialog
);
147 if ( item
== OPT_HELP
) {
148 HMSetBalloons(!HMGetBalloons());
150 if ( item
== OPT_CMDLINE
) {
151 int new_argc
, newer_argc
;
152 char **new_argv
, **newer_argv
;
154 new_argc
= ccommand(&new_argv
);
155 newer_argc
= (new_argc
-1) + old_argc
;
156 newer_argv
= malloc((newer_argc
+1)*sizeof(char *));
158 Py_FatalError("Cannot malloc argv\n");
159 for(i
=0; i
<old_argc
; i
++)
160 newer_argv
[i
] = (*argvp
)[i
];
161 for(i
=old_argc
; i
<=newer_argc
; i
++) /* Copy the NULL too */
162 newer_argv
[i
] = new_argv
[i
-old_argc
+1];
166 /* XXXX Is it not safe to use free() here, apparently */
168 #define OPT_ITEM(num, var) \
169 if ( item == (num) ) { \
171 GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
172 SetControlValue(handle, (short)p->var); \
175 OPT_ITEM(OPT_INSPECT
, inspect
);
176 OPT_ITEM(OPT_VERBOSE
, verbose
);
177 OPT_ITEM(OPT_OPTIMIZE
, optimize
);
178 OPT_ITEM(OPT_UNBUFFERED
, unbuffered
);
179 OPT_ITEM(OPT_DEBUGGING
, debugging
);
180 OPT_ITEM(OPT_KEEPNORMAL
, keep_normal
);
181 OPT_ITEM(OPT_KEEPERROR
, keep_error
);
182 OPT_ITEM(OPT_OLDEXC
, oldexc
);
183 OPT_ITEM(OPT_NOSITE
, nosite
);
187 DisposeDialog(dialog
);
191 ** Initialization code, shared by interpreter and applets
194 init_common(int *argcp
, char ***argvp
, int embedded
)
196 /* Remember resource fork refnum, for later */
197 PyMac_AppRefNum
= CurResFile();
199 /* Initialize toolboxes */
202 #ifdef USE_MAC_SHARED_LIBRARY
203 /* Add the shared library to the stack of resource files */
204 (void)PyMac_init_process_location();
205 PyMac_AddLibResources();
208 #if defined(USE_GUSI)
212 PyMac_SetGUSIOptions();
213 atexit(PyMac_StopGUSISpin
);
217 /* Set various SIOUX flags. Some are changed later based on options */
218 /* SIOUXSettings.standalone = 0; /* XXXX Attempting to keep sioux from eating events */
219 SIOUXSettings
.asktosaveonclose
= 0;
220 SIOUXSettings
.showstatusline
= 0;
221 SIOUXSettings
.tabspaces
= 4;
224 /* Get options from preference file (or from applet resource fork) */
225 options
.keep_error
= 1; /* default-default */
226 PyMac_PreferenceOptions(&options
);
229 static char *emb_argv
[] = {"embedded-python", 0};
234 /* Create argc/argv. Do it before we go into the options event loop. */
235 *argcp
= PyMac_GetArgv(argvp
, options
.noargs
);
237 /* Do interactive option setting, if allowed and <option> depressed */
238 PyMac_InteractiveOptions(&options
, argcp
, argvp
);
241 /* Copy selected options to where the machine-independent stuff wants it */
242 Py_VerboseFlag
= options
.verbose
;
243 /* Py_SuppressPrintingFlag = options.suppress_print; */
244 Py_OptimizeFlag
= options
.optimize
;
245 Py_DebugFlag
= options
.debugging
;
246 Py_NoSiteFlag
= options
.nosite
;
247 Py_UseClassExceptionsFlag
= !(options
.oldexc
);
248 if ( options
.noargs
) {
249 /* don't process events at all without the scripts permission */
250 PyMacSchedParams scp
;
252 PyMac_GetSchedParams(&scp
);
253 scp
.process_events
= 0;
254 /* Should we disable command-dot as well? */
255 PyMac_SetSchedParams(&scp
);
257 /* XXXX dispatch oldexc and nosite */
260 if (options
.unbuffered
) {
262 setbuf(stdout
, (char *)NULL
);
263 setbuf(stderr
, (char *)NULL
);
265 /* On MPW (3.2) unbuffered seems to hang */
266 setvbuf(stdout
, (char *)NULL
, _IOLBF
, BUFSIZ
);
267 setvbuf(stderr
, (char *)NULL
, _IOLBF
, BUFSIZ
);
271 /* collectSummary or collectDetailed, timebase, #routines, max stack depth */
272 ProfilerInit(collectSummary
, bestTimeBase
, 2000, 150);
275 /* Tell the rest of python about our argc/argv */
276 orig_argc
= *argcp
; /* For Py_GetArgcArgv() */
278 Py_SetProgramName((*argvp
)[0]);
282 ** Inspection mode after script/applet termination
289 if (options
.inspect
&& isatty((int)fileno(stdin
)))
290 sts
= PyRun_AnyFile(stdin
, "<stdin>") != 0;
294 #ifdef USE_MAC_APPLET_SUPPORT
297 /* Run a compiled Python Python script from 'PYC ' resource __main__ */
306 h
= GetNamedResource('PYC ', "\p__main__");
308 Alert(NOPYC_ALERT
, NULL
);
311 size
= GetResourceSizeOnDisk(h
);
313 code
= PyMarshal_ReadObjectFromString(*h
+ 8, (int)(size
- 8));
320 result
= PyImport_ExecCodeModule("__main__", code
);
322 if (result
== NULL
) {
330 /* Initialization sequence for applets */
338 init_common(&argc
, &argv
, 0);
341 PySys_SetArgv(argc
, argv
);
343 err
= run_main_resource();
345 err
= (run_inspect() || err
);
350 /* XXX Should we bother to Py_Exit(sts)? */
354 ** Hook for embedding python.
362 init_common(&argc
, &argv
, 1);
364 PySys_SetArgv(argc
, argv
);
367 #endif /* USE_MAC_APPLET_SUPPORT */
369 /* For normal application */
371 PyMac_InitApplication()
376 init_common(&argc
, &argv
, 0);
379 /* We're running a script. Attempt to change current directory */
380 char curwd
[256], *endp
;
382 strcpy(curwd
, argv
[1]);
383 endp
= strrchr(curwd
, ':');
384 if ( endp
&& endp
> curwd
) {
389 /* Change MacOS's idea of wd too */
405 char *command
= NULL
;
406 char *filename
= NULL
;
411 if (Py_VerboseFlag
||
412 command
== NULL
&& filename
== NULL
&& isatty((int)fileno(fp
)))
413 fprintf(stderr
, "Python %s\n%s\n",
414 Py_GetVersion(), Py_GetCopyright());
416 if (filename
!= NULL
) {
417 if ((fp
= fopen(filename
, "r")) == NULL
) {
418 fprintf(stderr
, "%s: can't open file '%s'\n",
424 /* We initialize the menubar here, hoping SIOUX is initialized by now */
429 PySys_SetArgv(argc
-1, argv
+1);
431 if (filename
== NULL
&& isatty((int)fileno(fp
))) {
432 FILE *fp
= fopen(STARTUP
, "r");
434 (void) PyRun_SimpleFile(fp
, STARTUP
);
440 fp
, filename
== NULL
? "<stdin>" : filename
) != 0;
441 if (filename
!= NULL
)
444 if ( filename
!= NULL
|| command
!= NULL
)
445 sts
= (run_inspect() || sts
);
452 ** Terminate application
461 ProfilerDump("\pPython Profiler Results");
465 keep
= options
.keep_error
;
467 keep
= options
.keep_normal
;
471 SIOUXSettings
.standalone
= 1;
472 SIOUXSettings
.autocloseonquit
= 0;
473 SIOUXSetTitle("\p\307terminated\310");
474 PyMac_RestoreMenuBar();
477 ** Temporary workaround: autocloseonquit clearing does not
478 ** currently work for the MSL/GUSI combo.
480 while(getchar() > 0);
484 SIOUXSettings
.autocloseonquit
= 1;
485 #endif /* USE_SIOUX */
487 console_options
.pause_atexit
= keep
;
493 /* Return the program name -- some code out there needs this. */
495 Py_GetProgramFullPath()
501 /* Make the *original* argc/argv available to other modules.
502 This is rare, but it is needed by the secureware extension. */
505 Py_GetArgcArgv(argc
,argv
)
513 /* More cruft that shouldn't really be here, used in sysmodule.c */
518 return PyMac_GetPythonDir();
524 return PyMac_GetPythonDir();