1 /***********************************************************
2 Copyright 1991-1995 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"
33 #include <Resources.h>
48 #define STARTUP "PythonStartup"
50 extern int Py_DebugFlag
; /* For parser.c, declared in pythonrun.c */
51 extern int Py_VerboseFlag
; /* For import.c, declared in pythonrun.c */
52 extern int Py_SuppressPrintingFlag
; /* For ceval.c, declared in pythonrun.c */
55 /* Subroutines that live in their own file */
56 extern char *getversion
Py_PROTO((void));
57 extern char *getcopyright
Py_PROTO((void));
60 /* For getprogramname(); set by main() */
63 /* For getargcargv(); set by main() */
64 static char **orig_argv
;
67 /* Flags indicating whether stdio window should stay open on termination */
68 static int keep_normal
;
69 static int keep_error
= 1;
71 #ifdef USE_MAC_APPLET_SUPPORT
74 /* Run a compiled Python Python script from 'PYC ' resource __main__ */
83 h
= GetNamedResource('PYC ', "\p__main__");
85 Alert(NOPYC_ALERT
, NULL
);
88 size
= GetResourceSizeOnDisk(h
);
90 code
= PyMarshal_ReadObjectFromString(*h
+ 8, (int)(size
- 8));
97 result
= PyImport_ExecCodeModule("__main__", code
);
107 /* Initialization sequence for applets */
115 #ifdef USE_MAC_SHARED_LIBRARY
116 PyMac_AddLibResources();
119 SIOUXSettings
.asktosaveonclose
= 0;
120 SIOUXSettings
.showstatusline
= 0;
121 SIOUXSettings
.tabspaces
= 4;
123 argc
= PyMac_GetArgv(&argv
);
125 PySys_SetArgv(argc
, argv
);
126 err
= run_main_resource();
130 /* XXX Should we bother to Py_Exit(sts)? */
133 #endif /* USE_MAC_APPLET_SUPPORT */
135 /* For normal application */
137 PyMac_InitApplication()
142 #ifdef USE_MAC_SHARED_LIBRARY
143 PyMac_AddLibResources();
146 SIOUXSettings
.asktosaveonclose
= 0;
147 SIOUXSettings
.showstatusline
= 0;
148 SIOUXSettings
.tabspaces
= 4;
150 argc
= PyMac_GetArgv(&argv
);
152 /* We're running a script. Attempt to change current directory */
153 char curwd
[256], *endp
;
155 strcpy(curwd
, argv
[1]);
156 endp
= strrchr(curwd
, ':');
157 if ( endp
&& endp
> curwd
) {
167 ** PyMac_InteractiveOptions - Allow user to set options if option key is pressed
170 PyMac_InteractiveOptions(int *inspect
, int *verbose
, int *suppress_print
,
171 int *unbuffered
, int *debugging
, int *keep_normal
,
177 ControlHandle handle
;
182 map
= (unsigned char *)rmap
;
183 if ( ( map
[0x3a>>3] & (1<<(0x3a&7)) ) == 0 ) /* option key is 3a */
186 dialog
= GetNewDialog(OPT_DIALOG
, NULL
, (WindowPtr
)-1);
187 if ( dialog
== NULL
) {
188 printf("Option dialog not found - cannot set options\n");
192 /* Set keep-open-on-error */
193 GetDialogItem(dialog
, OPT_KEEPERROR
, &type
, (Handle
*)&handle
, &rect
);
194 SetCtlValue(handle
, *keep_error
);
198 ModalDialog(NULL
, &item
);
199 if ( item
== OPT_OK
)
201 if ( item
== OPT_CANCEL
) {
202 DisposDialog(dialog
);
205 #define OPT_ITEM(num, var) \
206 if ( item == (num) ) { \
208 GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
209 SetCtlValue(handle, (short)*(var)); \
212 OPT_ITEM(OPT_INSPECT
, inspect
);
213 OPT_ITEM(OPT_VERBOSE
, verbose
);
214 OPT_ITEM(OPT_SUPPRESS
, suppress_print
);
215 OPT_ITEM(OPT_UNBUFFERED
, unbuffered
);
216 OPT_ITEM(OPT_DEBUGGING
, debugging
);
217 OPT_ITEM(OPT_KEEPNORMAL
, keep_normal
);
218 OPT_ITEM(OPT_KEEPERROR
, keep_error
);
222 DisposDialog(dialog
);
232 char *command
= NULL
;
233 char *filename
= NULL
;
238 orig_argc
= argc
; /* For getargcargv() */
240 argv0
= argv
[0]; /* For getprogramname() */
242 PyMac_InteractiveOptions(&inspect
, &Py_VerboseFlag
, &Py_SuppressPrintingFlag
,
243 &unbuffered
, &Py_DebugFlag
, &keep_normal
, &keep_error
);
247 setbuf(stdout
, (char *)NULL
);
248 setbuf(stderr
, (char *)NULL
);
250 /* On MPW (3.2) unbuffered seems to hang */
251 setvbuf(stdout
, (char *)NULL
, _IOLBF
, BUFSIZ
);
252 setvbuf(stderr
, (char *)NULL
, _IOLBF
, BUFSIZ
);
258 if (Py_VerboseFlag
||
259 command
== NULL
&& filename
== NULL
&& isatty((int)fileno(fp
)))
260 fprintf(stderr
, "Python %s\n%s\n",
261 getversion(), getcopyright());
263 if (filename
!= NULL
) {
264 if ((fp
= fopen(filename
, "r")) == NULL
) {
265 fprintf(stderr
, "%s: can't open file '%s'\n",
273 PySys_SetArgv(argc
-1, argv
+1);
275 if (filename
== NULL
&& isatty((int)fileno(fp
))) {
276 FILE *fp
= fopen(STARTUP
, "r");
278 (void) PyRun_SimpleFile(fp
, STARTUP
);
284 fp
, filename
== NULL
? "<stdin>" : filename
) != 0;
285 if (filename
!= NULL
)
288 if (inspect
&& isatty((int)fileno(stdin
)) &&
289 (filename
!= NULL
|| command
!= NULL
))
290 sts
= PyRun_AnyFile(stdin
, "<stdin>") != 0;
297 ** Terminate application
311 SIOUXSettings
.standalone
= 1;
312 SIOUXSettings
.autocloseonquit
= 0;
313 SIOUXSetTitle("\pÇterminatedÈ");
316 SIOUXSettings
.autocloseonquit
= 1;
319 console_options
.pause_atexit
= keep
;
325 /* Return the program name -- some code out there needs this. */
334 /* Make the *original* argc/argv available to other modules.
335 This is rare, but it is needed by the secureware extension. */
338 getargcargv(argc
,argv
)