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>
43 #define STARTUP "PythonStartup"
45 extern int Py_DebugFlag
; /* For parser.c, declared in pythonrun.c */
46 extern int Py_VerboseFlag
; /* For import.c, declared in pythonrun.c */
47 extern int Py_SuppressPrintingFlag
; /* For ceval.c, declared in pythonrun.c */
50 /* Subroutines that live in their own file */
51 extern char *getversion();
52 extern char *getcopyright();
55 /* For getprogramname(); set by main() */
58 /* For getargcargv(); set by main() */
59 static char **orig_argv
;
62 #ifdef USE_MAC_APPLET_SUPPORT
65 /* Run a compiled Python Python script from 'PYC ' resource __main__ */
74 h
= GetNamedResource('PYC ', "\p__main__");
76 Alert(NOPYC_ALERT
, NULL
);
79 size
= GetResourceSizeOnDisk(h
);
81 code
= PyMarshal_ReadObjectFromString(*h
+ 8, (int)(size
- 8));
88 result
= PyImport_ExecCodeModule("__main__", code
);
98 /* Initialization sequence for applets */
106 #ifdef USE_MAC_SHARED_LIBRARY
107 PyMac_AddLibResources();
110 SIOUXSettings
.asktosaveonclose
= 0;
111 SIOUXSettings
.showstatusline
= 0;
112 SIOUXSettings
.tabspaces
= 4;
114 argc
= PyMac_GetArgv(&argv
);
116 PySys_SetArgv(argc
, argv
);
117 err
= run_main_resource();
122 SIOUXSettings
.autocloseonquit
= 1;
124 printf("\n[Terminated]\n");
126 /* XXX Should we bother to Py_Exit(sts)? */
129 #endif /* USE_MAC_APPLET_SUPPORT */
131 /* For normal application */
133 PyMac_InitApplication()
138 #ifdef USE_MAC_SHARED_LIBRARY
139 PyMac_AddLibResources();
142 SIOUXSettings
.asktosaveonclose
= 0;
143 SIOUXSettings
.showstatusline
= 0;
144 SIOUXSettings
.tabspaces
= 4;
146 argc
= PyMac_GetArgv(&argv
);
148 /* We're running a script. Attempt to change current directory */
149 char curwd
[256], *endp
;
151 strcpy(curwd
, argv
[1]);
152 endp
= strrchr(curwd
, ':');
153 if ( endp
&& endp
> curwd
) {
163 ** PyMac_InteractiveOptions - Allow user to set options if option key is pressed
166 PyMac_InteractiveOptions(int *inspect
, int *verbose
, int *suppress_print
,
167 int *unbuffered
, int *debugging
)
172 ControlHandle handle
;
177 map
= (unsigned char *)rmap
;
178 if ( ( map
[0x3a>>3] & (1<<(0x3a&7)) ) == 0 ) /* option key is 3a */
181 dialog
= GetNewDialog(OPT_DIALOG
, NULL
, (WindowPtr
)-1);
182 if ( dialog
== NULL
) {
183 printf("Option dialog not found - cannot set options\n");
188 ModalDialog(NULL
, &item
);
189 if ( item
== OPT_OK
)
191 if ( item
== OPT_CANCEL
) {
192 DisposDialog(dialog
);
195 #define OPT_ITEM(num, var) \
196 if ( item == (num) ) { \
198 GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
199 SetCtlValue(handle, (short)*(var)); \
202 OPT_ITEM(OPT_INSPECT
, inspect
);
203 OPT_ITEM(OPT_VERBOSE
, verbose
);
204 OPT_ITEM(OPT_SUPPRESS
, suppress_print
);
205 OPT_ITEM(OPT_UNBUFFERED
, unbuffered
);
206 OPT_ITEM(OPT_DEBUGGING
, debugging
);
210 DisposDialog(dialog
);
221 char *command
= NULL
;
222 char *filename
= NULL
;
228 orig_argc
= argc
; /* For getargcargv() */
230 argv0
= argv
[0]; /* For getprogramname() */
232 PyMac_InteractiveOptions(&inspect
, &Py_VerboseFlag
, &Py_SuppressPrintingFlag
,
233 &unbuffered
, &Py_DebugFlag
);
238 setbuf(stdout
, (char *)NULL
);
239 setbuf(stderr
, (char *)NULL
);
241 /* On MPW (3.2) unbuffered seems to hang */
242 setvbuf(stdout
, (char *)NULL
, _IOLBF
, BUFSIZ
);
243 setvbuf(stderr
, (char *)NULL
, _IOLBF
, BUFSIZ
);
249 if (Py_VerboseFlag
||
250 command
== NULL
&& filename
== NULL
&& isatty((int)fileno(fp
)))
251 fprintf(stderr
, "Python %s\n%s\n",
252 getversion(), getcopyright());
254 if (filename
!= NULL
) {
255 if ((fp
= fopen(filename
, "r")) == NULL
) {
256 fprintf(stderr
, "%s: can't open file '%s'\n",
264 PySys_SetArgv(argc
-1, argv
+1);
266 if (filename
== NULL
&& isatty((int)fileno(fp
))) {
267 FILE *fp
= fopen(STARTUP
, "r");
269 (void) PyRun_SimpleFile(fp
, STARTUP
);
275 fp
, filename
== NULL
? "<stdin>" : filename
) != 0;
276 if (filename
!= NULL
)
279 if (inspect
&& isatty((int)fileno(stdin
)) &&
280 (filename
!= NULL
|| command
!= NULL
))
281 sts
= PyRun_AnyFile(stdin
, "<stdin>") != 0;
288 /* Return the program name -- some code out there needs this. */
297 /* Make the *original* argc/argv available to other modules.
298 This is rare, but it is needed by the secureware extension. */
301 getargcargv(argc
,argv
)