blob: 3d878ed4328bd5f5a67c2751733c6625dc721985 [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>
Jack Jansen1e8557a1995-11-10 14:51:26 +000041#define USE_SIOUX
Jack Jansenc76fd391995-02-02 14:27:31 +000042#endif
43
Jack Jansen0168f271995-10-27 13:32:30 +000044#ifdef THINK_C
45#include <console.h>
46#endif
47
Jack Jansen696c9581995-08-14 12:33:20 +000048#define STARTUP "PythonStartup"
Jack Jansenbac428d1994-12-14 13:47:30 +000049
Jack Jansen696c9581995-08-14 12:33:20 +000050extern int Py_DebugFlag; /* For parser.c, declared in pythonrun.c */
51extern int Py_VerboseFlag; /* For import.c, declared in pythonrun.c */
52extern int Py_SuppressPrintingFlag; /* For ceval.c, declared in pythonrun.c */
53
54
55/* Subroutines that live in their own file */
Jack Jansen85c9aea1995-10-23 13:57:03 +000056extern char *getversion Py_PROTO((void));
57extern char *getcopyright Py_PROTO((void));
Jack Jansen696c9581995-08-14 12:33:20 +000058
59
60/* For getprogramname(); set by main() */
61static char *argv0;
62
63/* For getargcargv(); set by main() */
64static char **orig_argv;
65static int orig_argc;
66
Jack Jansen0168f271995-10-27 13:32:30 +000067/* Flags indicating whether stdio window should stay open on termination */
68static int keep_normal;
69static int keep_error = 1;
70
Jack Jansen01fbc681996-02-28 15:42:47 +000071/* Initialize the Mac toolbox world */
72
73static void
74init_mac_world()
75{
76#ifdef THINK_C
77 printf("\n");
78#else
79 MaxApplZone();
80 InitGraf(&qd.thePort);
81 InitFonts();
82 InitWindows();
83 TEInit();
84 InitDialogs((long)0);
85 InitMenus();
86 InitCursor();
87#endif
88}
89
90/* Initialization code shared by interpreter and applets */
91
92static void
93init_common()
94{
95
96 /* Initialize toolboxes */
97 init_mac_world();
98
99#ifdef USE_MAC_SHARED_LIBRARY
100 /* Add the shared library to the stack of resource files */
101 PyMac_AddLibResources();
102#endif
103
104#if defined(USE_GUSI)
105 /* Setup GUSI */
106 GUSIDefaultSetup();
107#endif
108
109#ifdef USE_SIOUX
110 /* Set various SIOUX flags. Some are changed later based on options */
111 SIOUXSettings.asktosaveonclose = 0;
112 SIOUXSettings.showstatusline = 0;
113 SIOUXSettings.tabspaces = 4;
114#endif
115
116}
117
118
Jack Jansen696c9581995-08-14 12:33:20 +0000119#ifdef USE_MAC_APPLET_SUPPORT
120/* Applet support */
121
122/* Run a compiled Python Python script from 'PYC ' resource __main__ */
123static int
124run_main_resource()
125{
126 Handle h;
127 long size;
128 PyObject *code;
129 PyObject *result;
130
131 h = GetNamedResource('PYC ', "\p__main__");
132 if (h == NULL) {
133 Alert(NOPYC_ALERT, NULL);
134 return 1;
135 }
136 size = GetResourceSizeOnDisk(h);
137 HLock(h);
138 code = PyMarshal_ReadObjectFromString(*h + 8, (int)(size - 8));
139 HUnlock(h);
140 ReleaseResource(h);
141 if (code == NULL) {
142 PyErr_Print();
143 return 1;
144 }
145 result = PyImport_ExecCodeModule("__main__", code);
146 Py_DECREF(code);
147 if (result == NULL) {
148 PyErr_Print();
149 return 1;
150 }
151 Py_DECREF(result);
152 return 0;
153}
154
155/* Initialization sequence for applets */
156void
157PyMac_InitApplet()
158{
Guido van Rossumb0f3c821994-08-23 13:34:25 +0000159 int argc;
160 char **argv;
Jack Jansen696c9581995-08-14 12:33:20 +0000161 int err;
162
Jack Jansen01fbc681996-02-28 15:42:47 +0000163 init_common();
Jack Jansen696c9581995-08-14 12:33:20 +0000164 argc = PyMac_GetArgv(&argv);
165 Py_Initialize();
166 PySys_SetArgv(argc, argv);
167 err = run_main_resource();
168 fflush(stderr);
169 fflush(stdout);
Jack Jansen0168f271995-10-27 13:32:30 +0000170 PyMac_Exit(err);
Jack Jansen696c9581995-08-14 12:33:20 +0000171 /* XXX Should we bother to Py_Exit(sts)? */
172}
173
174#endif /* USE_MAC_APPLET_SUPPORT */
175
176/* For normal application */
177void
178PyMac_InitApplication()
179{
180 int argc;
181 char **argv;
182
Jack Jansen01fbc681996-02-28 15:42:47 +0000183 init_common();
Jack Jansen696c9581995-08-14 12:33:20 +0000184 argc = PyMac_GetArgv(&argv);
185 if ( argc > 1 ) {
186 /* We're running a script. Attempt to change current directory */
187 char curwd[256], *endp;
188
189 strcpy(curwd, argv[1]);
190 endp = strrchr(curwd, ':');
191 if ( endp && endp > curwd ) {
192 *endp = '\0';
193
194 chdir(curwd);
195 }
196 }
197 Py_Main(argc, argv);
198}
199
200/*
201** PyMac_InteractiveOptions - Allow user to set options if option key is pressed
202*/
203void
204PyMac_InteractiveOptions(int *inspect, int *verbose, int *suppress_print,
Jack Jansen0168f271995-10-27 13:32:30 +0000205 int *unbuffered, int *debugging, int *keep_normal,
206 int *keep_error)
Jack Jansen696c9581995-08-14 12:33:20 +0000207{
208 KeyMap rmap;
209 unsigned char *map;
210 short item, type;
211 ControlHandle handle;
212 DialogPtr dialog;
213 Rect rect;
214
Jack Jansena4b7e141996-02-21 16:46:57 +0000215 /* Default-defaults: */
216 *keep_error = 1;
217 /* Get default settings from our preference file */
Jack Jansen01fbc681996-02-28 15:42:47 +0000218 PyMac_PreferenceOptions(inspect, verbose, suppress_print,
219 unbuffered, debugging, keep_normal, keep_error);
Jack Jansena4b7e141996-02-21 16:46:57 +0000220 /* If option is pressed override these */
Jack Jansen696c9581995-08-14 12:33:20 +0000221 GetKeys(rmap);
222 map = (unsigned char *)rmap;
223 if ( ( map[0x3a>>3] & (1<<(0x3a&7)) ) == 0 ) /* option key is 3a */
224 return;
225
226 dialog = GetNewDialog(OPT_DIALOG, NULL, (WindowPtr)-1);
227 if ( dialog == NULL ) {
228 printf("Option dialog not found - cannot set options\n");
229 return;
230 }
Jack Jansen0168f271995-10-27 13:32:30 +0000231
Jack Jansena4b7e141996-02-21 16:46:57 +0000232 /* Set default values */
233#define SET_OPT_ITEM(num, var) \
234 GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
235 SetCtlValue(handle, (short)*(var));
236
237 SET_OPT_ITEM(OPT_INSPECT, inspect);
238 SET_OPT_ITEM(OPT_VERBOSE, verbose);
239 SET_OPT_ITEM(OPT_SUPPRESS, suppress_print);
240 SET_OPT_ITEM(OPT_UNBUFFERED, unbuffered);
241 SET_OPT_ITEM(OPT_DEBUGGING, debugging);
242 SET_OPT_ITEM(OPT_KEEPNORMAL, keep_normal);
243 SET_OPT_ITEM(OPT_KEEPERROR, keep_error);
244
245#undef SET_OPT_ITEM
Jack Jansen0168f271995-10-27 13:32:30 +0000246
Jack Jansen696c9581995-08-14 12:33:20 +0000247 while (1) {
248 handle = NULL;
249 ModalDialog(NULL, &item);
250 if ( item == OPT_OK )
251 break;
252 if ( item == OPT_CANCEL ) {
253 DisposDialog(dialog);
254 exit(0);
255 }
256#define OPT_ITEM(num, var) \
257 if ( item == (num) ) { \
258 *(var) = !*(var); \
259 GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
260 SetCtlValue(handle, (short)*(var)); \
261 }
262
263 OPT_ITEM(OPT_INSPECT, inspect);
264 OPT_ITEM(OPT_VERBOSE, verbose);
265 OPT_ITEM(OPT_SUPPRESS, suppress_print);
266 OPT_ITEM(OPT_UNBUFFERED, unbuffered);
267 OPT_ITEM(OPT_DEBUGGING, debugging);
Jack Jansen0168f271995-10-27 13:32:30 +0000268 OPT_ITEM(OPT_KEEPNORMAL, keep_normal);
269 OPT_ITEM(OPT_KEEPERROR, keep_error);
Jack Jansen696c9581995-08-14 12:33:20 +0000270
271#undef OPT_ITEM
272 }
273 DisposDialog(dialog);
274}
275/* Main program */
276
277int
278Py_Main(argc, argv)
279 int argc;
280 char **argv;
281{
Jack Jansen696c9581995-08-14 12:33:20 +0000282 int sts;
283 char *command = NULL;
284 char *filename = NULL;
285 FILE *fp = stdin;
Jack Jansen696c9581995-08-14 12:33:20 +0000286 int inspect = 0;
287 int unbuffered = 0;
288
289 orig_argc = argc; /* For getargcargv() */
290 orig_argv = argv;
291 argv0 = argv[0]; /* For getprogramname() */
292
293 PyMac_InteractiveOptions(&inspect, &Py_VerboseFlag, &Py_SuppressPrintingFlag,
Jack Jansen0168f271995-10-27 13:32:30 +0000294 &unbuffered, &Py_DebugFlag, &keep_normal, &keep_error);
Jack Jansen696c9581995-08-14 12:33:20 +0000295
296 if (unbuffered) {
297#ifndef MPW
298 setbuf(stdout, (char *)NULL);
299 setbuf(stderr, (char *)NULL);
300#else
301 /* On MPW (3.2) unbuffered seems to hang */
302 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
303 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
304#endif
305 }
306
307 filename = argv[1];
308
309 if (Py_VerboseFlag ||
310 command == NULL && filename == NULL && isatty((int)fileno(fp)))
311 fprintf(stderr, "Python %s\n%s\n",
312 getversion(), getcopyright());
313
314 if (filename != NULL) {
315 if ((fp = fopen(filename, "r")) == NULL) {
316 fprintf(stderr, "%s: can't open file '%s'\n",
317 argv[0], filename);
Jack Jansen0168f271995-10-27 13:32:30 +0000318 PyMac_Exit(2);
Jack Jansen696c9581995-08-14 12:33:20 +0000319 }
320 }
321
322 Py_Initialize();
323
324 PySys_SetArgv(argc-1, argv+1);
325
326 if (filename == NULL && isatty((int)fileno(fp))) {
327 FILE *fp = fopen(STARTUP, "r");
328 if (fp != NULL) {
329 (void) PyRun_SimpleFile(fp, STARTUP);
330 PyErr_Clear();
331 fclose(fp);
332 }
333 }
334 sts = PyRun_AnyFile(
335 fp, filename == NULL ? "<stdin>" : filename) != 0;
336 if (filename != NULL)
337 fclose(fp);
338
339 if (inspect && isatty((int)fileno(stdin)) &&
340 (filename != NULL || command != NULL))
341 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
342
343 Py_Exit(sts);
344 /*NOTREACHED*/
345}
346
Jack Jansen0168f271995-10-27 13:32:30 +0000347/*
348** Terminate application
349*/
350PyMac_Exit(status)
351 int status;
352{
353 int keep;
354
355 if ( status )
356 keep = keep_error;
357 else
358 keep = keep_normal;
359
Jack Jansen1e8557a1995-11-10 14:51:26 +0000360#ifdef USE_SIOUX
361 if (keep) {
362 SIOUXSettings.standalone = 1;
363 SIOUXSettings.autocloseonquit = 0;
Jack Jansena4b7e141996-02-21 16:46:57 +0000364 SIOUXSetTitle("\pĀ«terminatedĀ»");
Jack Jansen1e8557a1995-11-10 14:51:26 +0000365 }
Jack Jansen0168f271995-10-27 13:32:30 +0000366 else
367 SIOUXSettings.autocloseonquit = 1;
368#endif
369#ifdef THINK_C
370 console_options.pause_atexit = keep;
371#endif
372
373 exit(status);
374}
Jack Jansen696c9581995-08-14 12:33:20 +0000375
376/* Return the program name -- some code out there needs this. */
377
378char *
379getprogramname()
380{
381 return argv0;
382}
383
384
385/* Make the *original* argc/argv available to other modules.
386 This is rare, but it is needed by the secureware extension. */
387
388void
389getargcargv(argc,argv)
390 int *argc;
391 char ***argv;
392{
393 *argc = orig_argc;
394 *argv = orig_argv;
Guido van Rossumb0f3c821994-08-23 13:34:25 +0000395}