Added ref to Misc/NEWS file; added hint to run regen script on Linux.
[python/dscho.git] / Mac / Python / macmain.c
blobf6b1dee4d9f6ace7d2a5bc50cb0a3a82fa0c81d4
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
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 */
27 #include "Python.h"
28 #include "pythonresources.h"
29 #include "import.h"
30 #include "marshal.h"
32 #include <Memory.h>
33 #include <Resources.h>
34 #include <stdio.h>
35 #include <Events.h>
36 #include <Windows.h>
37 #include <Desk.h>
39 #ifdef __MWERKS__
40 #include <SIOUX.h>
41 #define USE_SIOUX
42 #endif
44 #ifdef THINK_C
45 #include <console.h>
46 #endif
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() */
61 static char *argv0;
63 /* For getargcargv(); set by main() */
64 static char **orig_argv;
65 static int orig_argc;
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
72 /* Applet support */
74 /* Run a compiled Python Python script from 'PYC ' resource __main__ */
75 static int
76 run_main_resource()
78 Handle h;
79 long size;
80 PyObject *code;
81 PyObject *result;
83 h = GetNamedResource('PYC ', "\p__main__");
84 if (h == NULL) {
85 Alert(NOPYC_ALERT, NULL);
86 return 1;
88 size = GetResourceSizeOnDisk(h);
89 HLock(h);
90 code = PyMarshal_ReadObjectFromString(*h + 8, (int)(size - 8));
91 HUnlock(h);
92 ReleaseResource(h);
93 if (code == NULL) {
94 PyErr_Print();
95 return 1;
97 result = PyImport_ExecCodeModule("__main__", code);
98 Py_DECREF(code);
99 if (result == NULL) {
100 PyErr_Print();
101 return 1;
103 Py_DECREF(result);
104 return 0;
107 /* Initialization sequence for applets */
108 void
109 PyMac_InitApplet()
111 int argc;
112 char **argv;
113 int err;
115 #ifdef USE_MAC_SHARED_LIBRARY
116 PyMac_AddLibResources();
117 #endif
118 #ifdef USE_SIOUX
119 SIOUXSettings.asktosaveonclose = 0;
120 SIOUXSettings.showstatusline = 0;
121 SIOUXSettings.tabspaces = 4;
122 #endif
123 argc = PyMac_GetArgv(&argv);
124 Py_Initialize();
125 PySys_SetArgv(argc, argv);
126 err = run_main_resource();
127 fflush(stderr);
128 fflush(stdout);
129 PyMac_Exit(err);
130 /* XXX Should we bother to Py_Exit(sts)? */
133 #endif /* USE_MAC_APPLET_SUPPORT */
135 /* For normal application */
136 void
137 PyMac_InitApplication()
139 int argc;
140 char **argv;
142 #ifdef USE_MAC_SHARED_LIBRARY
143 PyMac_AddLibResources();
144 #endif
145 #ifdef USE_SIOUX
146 SIOUXSettings.asktosaveonclose = 0;
147 SIOUXSettings.showstatusline = 0;
148 SIOUXSettings.tabspaces = 4;
149 #endif
150 argc = PyMac_GetArgv(&argv);
151 if ( argc > 1 ) {
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 ) {
158 *endp = '\0';
160 chdir(curwd);
163 Py_Main(argc, argv);
167 ** PyMac_InteractiveOptions - Allow user to set options if option key is pressed
169 void
170 PyMac_InteractiveOptions(int *inspect, int *verbose, int *suppress_print,
171 int *unbuffered, int *debugging, int *keep_normal,
172 int *keep_error)
174 KeyMap rmap;
175 unsigned char *map;
176 short item, type;
177 ControlHandle handle;
178 DialogPtr dialog;
179 Rect rect;
181 GetKeys(rmap);
182 map = (unsigned char *)rmap;
183 if ( ( map[0x3a>>3] & (1<<(0x3a&7)) ) == 0 ) /* option key is 3a */
184 return;
186 dialog = GetNewDialog(OPT_DIALOG, NULL, (WindowPtr)-1);
187 if ( dialog == NULL ) {
188 printf("Option dialog not found - cannot set options\n");
189 return;
192 /* Set keep-open-on-error */
193 GetDialogItem(dialog, OPT_KEEPERROR, &type, (Handle *)&handle, &rect);
194 SetCtlValue(handle, *keep_error);
196 while (1) {
197 handle = NULL;
198 ModalDialog(NULL, &item);
199 if ( item == OPT_OK )
200 break;
201 if ( item == OPT_CANCEL ) {
202 DisposDialog(dialog);
203 exit(0);
205 #define OPT_ITEM(num, var) \
206 if ( item == (num) ) { \
207 *(var) = !*(var); \
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);
220 #undef OPT_ITEM
222 DisposDialog(dialog);
224 /* Main program */
227 Py_Main(argc, argv)
228 int argc;
229 char **argv;
231 int sts;
232 char *command = NULL;
233 char *filename = NULL;
234 FILE *fp = stdin;
235 int inspect = 0;
236 int unbuffered = 0;
238 orig_argc = argc; /* For getargcargv() */
239 orig_argv = argv;
240 argv0 = argv[0]; /* For getprogramname() */
242 PyMac_InteractiveOptions(&inspect, &Py_VerboseFlag, &Py_SuppressPrintingFlag,
243 &unbuffered, &Py_DebugFlag, &keep_normal, &keep_error);
245 if (unbuffered) {
246 #ifndef MPW
247 setbuf(stdout, (char *)NULL);
248 setbuf(stderr, (char *)NULL);
249 #else
250 /* On MPW (3.2) unbuffered seems to hang */
251 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
252 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
253 #endif
256 filename = argv[1];
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",
266 argv[0], filename);
267 PyMac_Exit(2);
271 Py_Initialize();
273 PySys_SetArgv(argc-1, argv+1);
275 if (filename == NULL && isatty((int)fileno(fp))) {
276 FILE *fp = fopen(STARTUP, "r");
277 if (fp != NULL) {
278 (void) PyRun_SimpleFile(fp, STARTUP);
279 PyErr_Clear();
280 fclose(fp);
283 sts = PyRun_AnyFile(
284 fp, filename == NULL ? "<stdin>" : filename) != 0;
285 if (filename != NULL)
286 fclose(fp);
288 if (inspect && isatty((int)fileno(stdin)) &&
289 (filename != NULL || command != NULL))
290 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
292 Py_Exit(sts);
293 /*NOTREACHED*/
297 ** Terminate application
299 PyMac_Exit(status)
300 int status;
302 int keep;
304 if ( status )
305 keep = keep_error;
306 else
307 keep = keep_normal;
309 #ifdef USE_SIOUX
310 if (keep) {
311 SIOUXSettings.standalone = 1;
312 SIOUXSettings.autocloseonquit = 0;
313 SIOUXSetTitle("\pÇterminatedÈ");
315 else
316 SIOUXSettings.autocloseonquit = 1;
317 #endif
318 #ifdef THINK_C
319 console_options.pause_atexit = keep;
320 #endif
322 exit(status);
325 /* Return the program name -- some code out there needs this. */
327 char *
328 getprogramname()
330 return argv0;
334 /* Make the *original* argc/argv available to other modules.
335 This is rare, but it is needed by the secureware extension. */
337 void
338 getargcargv(argc,argv)
339 int *argc;
340 char ***argv;
342 *argc = orig_argc;
343 *argv = orig_argv;