blob: 547456ffc4c04a7cb1c5ec95e6109cdb471bf072 [file] [log] [blame]
Guido van Rossumb0f3c821994-08-23 13:34:25 +00001/***********************************************************
Guido van Rossum99546991995-01-08 14:33:34 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The 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"
Guido van Rossumb0f3c821994-08-23 13:34:25 +000031
Jack Jansen696c9581995-08-14 12:33:20 +000032#include <Memory.h>
33#include <Resources.h>
Guido van Rossumb0f3c821994-08-23 13:34:25 +000034#include <stdio.h>
Jack Jansen696c9581995-08-14 12:33:20 +000035#include <Events.h>
36#include <Windows.h>
37#include <Desk.h>
Guido van Rossumb0f3c821994-08-23 13:34:25 +000038
Jack Jansenc76fd391995-02-02 14:27:31 +000039#ifdef __MWERKS__
40#include <SIOUX.h>
41#endif
42
Jack Jansen0168f271995-10-27 13:32:30 +000043#ifdef THINK_C
44#include <console.h>
45#endif
46
Jack Jansen696c9581995-08-14 12:33:20 +000047#define STARTUP "PythonStartup"
Jack Jansenbac428d1994-12-14 13:47:30 +000048
Jack Jansen696c9581995-08-14 12:33:20 +000049extern int Py_DebugFlag; /* For parser.c, declared in pythonrun.c */
50extern int Py_VerboseFlag; /* For import.c, declared in pythonrun.c */
51extern int Py_SuppressPrintingFlag; /* For ceval.c, declared in pythonrun.c */
52
53
54/* Subroutines that live in their own file */
Jack Jansen85c9aea1995-10-23 13:57:03 +000055extern char *getversion Py_PROTO((void));
56extern char *getcopyright Py_PROTO((void));
Jack Jansen696c9581995-08-14 12:33:20 +000057
58
59/* For getprogramname(); set by main() */
60static char *argv0;
61
62/* For getargcargv(); set by main() */
63static char **orig_argv;
64static int orig_argc;
65
Jack Jansen0168f271995-10-27 13:32:30 +000066/* Flags indicating whether stdio window should stay open on termination */
67static int keep_normal;
68static int keep_error = 1;
69
Jack Jansen696c9581995-08-14 12:33:20 +000070#ifdef USE_MAC_APPLET_SUPPORT
71/* Applet support */
72
73/* Run a compiled Python Python script from 'PYC ' resource __main__ */
74static int
75run_main_resource()
76{
77 Handle h;
78 long size;
79 PyObject *code;
80 PyObject *result;
81
82 h = GetNamedResource('PYC ', "\p__main__");
83 if (h == NULL) {
84 Alert(NOPYC_ALERT, NULL);
85 return 1;
86 }
87 size = GetResourceSizeOnDisk(h);
88 HLock(h);
89 code = PyMarshal_ReadObjectFromString(*h + 8, (int)(size - 8));
90 HUnlock(h);
91 ReleaseResource(h);
92 if (code == NULL) {
93 PyErr_Print();
94 return 1;
95 }
96 result = PyImport_ExecCodeModule("__main__", code);
97 Py_DECREF(code);
98 if (result == NULL) {
99 PyErr_Print();
100 return 1;
101 }
102 Py_DECREF(result);
103 return 0;
104}
105
106/* Initialization sequence for applets */
107void
108PyMac_InitApplet()
109{
Guido van Rossumb0f3c821994-08-23 13:34:25 +0000110 int argc;
111 char **argv;
Jack Jansen696c9581995-08-14 12:33:20 +0000112 int err;
113
Jack Jansenf950f8d1995-02-13 11:35:34 +0000114#ifdef USE_MAC_SHARED_LIBRARY
115 PyMac_AddLibResources();
116#endif
117#ifdef __MWERKS__
118 SIOUXSettings.asktosaveonclose = 0;
119 SIOUXSettings.showstatusline = 0;
120 SIOUXSettings.tabspaces = 4;
121#endif
Jack Jansen696c9581995-08-14 12:33:20 +0000122 argc = PyMac_GetArgv(&argv);
123 Py_Initialize();
124 PySys_SetArgv(argc, argv);
125 err = run_main_resource();
126 fflush(stderr);
127 fflush(stdout);
Jack Jansen0168f271995-10-27 13:32:30 +0000128 PyMac_Exit(err);
Jack Jansen696c9581995-08-14 12:33:20 +0000129 /* XXX Should we bother to Py_Exit(sts)? */
130}
131
132#endif /* USE_MAC_APPLET_SUPPORT */
133
134/* For normal application */
135void
136PyMac_InitApplication()
137{
138 int argc;
139 char **argv;
140
141#ifdef USE_MAC_SHARED_LIBRARY
142 PyMac_AddLibResources();
143#endif
144#ifdef __MWERKS__
145 SIOUXSettings.asktosaveonclose = 0;
146 SIOUXSettings.showstatusline = 0;
147 SIOUXSettings.tabspaces = 4;
148#endif
149 argc = PyMac_GetArgv(&argv);
150 if ( argc > 1 ) {
151 /* We're running a script. Attempt to change current directory */
152 char curwd[256], *endp;
153
154 strcpy(curwd, argv[1]);
155 endp = strrchr(curwd, ':');
156 if ( endp && endp > curwd ) {
157 *endp = '\0';
158
159 chdir(curwd);
160 }
161 }
162 Py_Main(argc, argv);
163}
164
165/*
166** PyMac_InteractiveOptions - Allow user to set options if option key is pressed
167*/
168void
169PyMac_InteractiveOptions(int *inspect, int *verbose, int *suppress_print,
Jack Jansen0168f271995-10-27 13:32:30 +0000170 int *unbuffered, int *debugging, int *keep_normal,
171 int *keep_error)
Jack Jansen696c9581995-08-14 12:33:20 +0000172{
173 KeyMap rmap;
174 unsigned char *map;
175 short item, type;
176 ControlHandle handle;
177 DialogPtr dialog;
178 Rect rect;
179
180 GetKeys(rmap);
181 map = (unsigned char *)rmap;
182 if ( ( map[0x3a>>3] & (1<<(0x3a&7)) ) == 0 ) /* option key is 3a */
183 return;
184
185 dialog = GetNewDialog(OPT_DIALOG, NULL, (WindowPtr)-1);
186 if ( dialog == NULL ) {
187 printf("Option dialog not found - cannot set options\n");
188 return;
189 }
Jack Jansen0168f271995-10-27 13:32:30 +0000190
191 /* Set keep-open-on-error */
192 GetDialogItem(dialog, OPT_KEEPERROR, &type, (Handle *)&handle, &rect);
193 SetCtlValue(handle, *keep_error);
194
Jack Jansen696c9581995-08-14 12:33:20 +0000195 while (1) {
196 handle = NULL;
197 ModalDialog(NULL, &item);
198 if ( item == OPT_OK )
199 break;
200 if ( item == OPT_CANCEL ) {
201 DisposDialog(dialog);
202 exit(0);
203 }
204#define OPT_ITEM(num, var) \
205 if ( item == (num) ) { \
206 *(var) = !*(var); \
207 GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
208 SetCtlValue(handle, (short)*(var)); \
209 }
210
211 OPT_ITEM(OPT_INSPECT, inspect);
212 OPT_ITEM(OPT_VERBOSE, verbose);
213 OPT_ITEM(OPT_SUPPRESS, suppress_print);
214 OPT_ITEM(OPT_UNBUFFERED, unbuffered);
215 OPT_ITEM(OPT_DEBUGGING, debugging);
Jack Jansen0168f271995-10-27 13:32:30 +0000216 OPT_ITEM(OPT_KEEPNORMAL, keep_normal);
217 OPT_ITEM(OPT_KEEPERROR, keep_error);
Jack Jansen696c9581995-08-14 12:33:20 +0000218
219#undef OPT_ITEM
220 }
221 DisposDialog(dialog);
222}
223/* Main program */
224
225int
226Py_Main(argc, argv)
227 int argc;
228 char **argv;
229{
Jack Jansen696c9581995-08-14 12:33:20 +0000230 int sts;
231 char *command = NULL;
232 char *filename = NULL;
233 FILE *fp = stdin;
Jack Jansen696c9581995-08-14 12:33:20 +0000234 int inspect = 0;
235 int unbuffered = 0;
236
237 orig_argc = argc; /* For getargcargv() */
238 orig_argv = argv;
239 argv0 = argv[0]; /* For getprogramname() */
240
241 PyMac_InteractiveOptions(&inspect, &Py_VerboseFlag, &Py_SuppressPrintingFlag,
Jack Jansen0168f271995-10-27 13:32:30 +0000242 &unbuffered, &Py_DebugFlag, &keep_normal, &keep_error);
Jack Jansen696c9581995-08-14 12:33:20 +0000243
244 if (unbuffered) {
245#ifndef MPW
246 setbuf(stdout, (char *)NULL);
247 setbuf(stderr, (char *)NULL);
248#else
249 /* On MPW (3.2) unbuffered seems to hang */
250 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
251 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
252#endif
253 }
254
255 filename = argv[1];
256
257 if (Py_VerboseFlag ||
258 command == NULL && filename == NULL && isatty((int)fileno(fp)))
259 fprintf(stderr, "Python %s\n%s\n",
260 getversion(), getcopyright());
261
262 if (filename != NULL) {
263 if ((fp = fopen(filename, "r")) == NULL) {
264 fprintf(stderr, "%s: can't open file '%s'\n",
265 argv[0], filename);
Jack Jansen0168f271995-10-27 13:32:30 +0000266 PyMac_Exit(2);
Jack Jansen696c9581995-08-14 12:33:20 +0000267 }
268 }
269
270 Py_Initialize();
271
272 PySys_SetArgv(argc-1, argv+1);
273
274 if (filename == NULL && isatty((int)fileno(fp))) {
275 FILE *fp = fopen(STARTUP, "r");
276 if (fp != NULL) {
277 (void) PyRun_SimpleFile(fp, STARTUP);
278 PyErr_Clear();
279 fclose(fp);
280 }
281 }
282 sts = PyRun_AnyFile(
283 fp, filename == NULL ? "<stdin>" : filename) != 0;
284 if (filename != NULL)
285 fclose(fp);
286
287 if (inspect && isatty((int)fileno(stdin)) &&
288 (filename != NULL || command != NULL))
289 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
290
291 Py_Exit(sts);
292 /*NOTREACHED*/
293}
294
Jack Jansen0168f271995-10-27 13:32:30 +0000295/*
296** Terminate application
297*/
298PyMac_Exit(status)
299 int status;
300{
301 int keep;
302
303 if ( status )
304 keep = keep_error;
305 else
306 keep = keep_normal;
307
308#ifdef __MWERKS__
309 if (keep)
310 printf("\n[Terminated]\n");
311 else
312 SIOUXSettings.autocloseonquit = 1;
313#endif
314#ifdef THINK_C
315 console_options.pause_atexit = keep;
316#endif
317
318 exit(status);
319}
Jack Jansen696c9581995-08-14 12:33:20 +0000320
321/* Return the program name -- some code out there needs this. */
322
323char *
324getprogramname()
325{
326 return argv0;
327}
328
329
330/* Make the *original* argc/argv available to other modules.
331 This is rare, but it is needed by the secureware extension. */
332
333void
334getargcargv(argc,argv)
335 int *argc;
336 char ***argv;
337{
338 *argc = orig_argc;
339 *argv = orig_argv;
Guido van Rossumb0f3c821994-08-23 13:34:25 +0000340}