This commit was manufactured by cvs2svn to create tag 'r221c2'.
[python/dscho.git] / Mac / Python / macmain.c
blob71b42e9e922fa03fbf3f359863f7d80b157231c5
1 /***********************************************************
2 Copyright 1991-1997 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"
31 #include "macglue.h"
33 #ifdef WITHOUT_FRAMEWORKS
34 #include <Memory.h>
35 #include <Resources.h>
36 #include <stdio.h>
37 #include <Events.h>
38 #include <Windows.h>
39 #include <Fonts.h>
40 #include <Balloons.h>
41 #if TARGET_API_MAC_CARBON
42 #include <CFBundle.h>
43 #include <CFURL.h>
44 #include <CFString.h>
45 #include <CFBase.h>
46 #include <CFArray.h>
47 #endif /* TARGET_API_MAC_CARBON */
48 #ifdef USE_APPEARANCE
49 #include <Gestalt.h>
50 #include <Appearance.h>
51 #endif /* USE_APPEARANCE */
52 #else
53 #include <Carbon/Carbon.h>
54 #endif /* WITHOUT_FRAMEWORKS */
56 #ifdef __MWERKS__
57 #include <SIOUX.h>
58 #define USE_SIOUX
59 extern int ccommand(char ***);
60 #if __profile__ == 1
61 #include <profiler.h>
62 #endif /* __profile__ */
63 #endif /* __MWERKS__ */
65 #include <unistd.h>
66 #ifdef USE_MAC_SHARED_LIBRARY
67 extern PyMac_AddLibResources(void);
68 #endif
70 #define STARTUP "PythonStartup"
72 #define COPYRIGHT \
73 "Type \"copyright\", \"credits\" or \"license\" for more information."
75 short PyMac_AppRefNum; /* RefNum of application resource fork */
77 /* For Py_GetArgcArgv(); set by main() */
78 static char **orig_argv;
79 static int orig_argc;
81 /* A flag which remembers whether the user has acknowledged all the console
82 ** output (by typing something)
84 #define STATE_UNKNOWN 0
85 #define STATE_LASTREAD 1
86 #define STATE_LASTWRITE 2
87 int console_output_state = STATE_UNKNOWN;
89 PyMac_PrefRecord PyMac_options;
91 static void Py_Main(int, char **, char *); /* Forward */
92 void PyMac_Exit(int); /* Forward */
94 static void init_appearance(void)
96 #ifdef USE_APPEARANCE
97 OSErr err;
98 SInt32 response;
100 err = Gestalt(gestaltAppearanceAttr,&response);
101 if ( err ) goto no_appearance;
102 if ( !(response&(1<<gestaltAppearanceExists)) ) goto no_appearance;
103 /* XXXX Should we check the version? Compat-mode? */
104 PyMac_AppearanceCompliant = 1;
105 no_appearance:
106 return;
107 #endif /* USE_APPEARANCE */
109 /* Initialize the Mac toolbox world */
111 static void
112 init_mac_world(void)
114 #if !TARGET_API_MAC_CARBON
115 /* These aren't needed for carbon */
116 MaxApplZone();
117 InitGraf(&qd.thePort);
118 InitFonts();
119 InitWindows();
120 TEInit();
121 InitDialogs((long)0);
122 InitMenus();
123 #endif
124 InitCursor();
125 init_appearance();
129 ** PyMac_InteractiveOptions - Allow user to set options if option key is pressed
131 static void
132 PyMac_InteractiveOptions(PyMac_PrefRecord *p, int *argcp, char ***argvp)
134 KeyMap rmap;
135 unsigned char *map;
136 short item, type;
137 ControlHandle handle;
138 DialogPtr dialog;
139 Rect rect;
142 ** If the preferences disallows interactive options we return,
143 ** similarly of <option> isn't pressed.
145 if (p->nointopt) return;
147 GetKeys(rmap);
148 map = (unsigned char *)rmap;
149 if ( ( map[0x3a>>3] & (1<<(0x3a&7)) ) == 0 ) /* option key is 3a */
150 return;
152 dialog = GetNewDialog(OPT_DIALOG, NULL, (WindowPtr)-1);
153 if ( dialog == NULL ) {
154 printf("Option dialog not found - cannot set options\n");
155 return;
157 SetDialogDefaultItem(dialog, OPT_OK);
158 SetDialogCancelItem(dialog, OPT_CANCEL);
160 /* Set default values */
161 #define SET_OPT_ITEM(num, var) \
162 GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
163 SetControlValue(handle, (short)p->var);
165 SET_OPT_ITEM(OPT_INSPECT, inspect);
166 SET_OPT_ITEM(OPT_VERBOSE, verbose);
167 /* OPT_VERBOSEVERBOSE is default off */
168 SET_OPT_ITEM(OPT_OPTIMIZE, optimize);
169 SET_OPT_ITEM(OPT_UNBUFFERED, unbuffered);
170 SET_OPT_ITEM(OPT_DEBUGGING, debugging);
171 GetDialogItem(dialog, OPT_KEEPALWAYS, &type, (Handle *)&handle, &rect);
172 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_ALWAYS));
173 GetDialogItem(dialog, OPT_KEEPOUTPUT, &type, (Handle *)&handle, &rect);
174 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_OUTPUT));
175 GetDialogItem(dialog, OPT_KEEPERROR, &type, (Handle *)&handle, &rect);
176 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_ERROR));
177 GetDialogItem(dialog, OPT_KEEPNEVER, &type, (Handle *)&handle, &rect);
178 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_NEVER));
179 /* SET_OPT_ITEM(OPT_KEEPCONSOLE, keep_console); */
180 SET_OPT_ITEM(OPT_TABWARN, tabwarn);
181 SET_OPT_ITEM(OPT_NOSITE, nosite);
182 SET_OPT_ITEM(OPT_DIVISIONWARN, divisionwarn);
183 SET_OPT_ITEM(OPT_UNIXNEWLINES, unixnewlines);
184 /* The rest are not settable interactively */
186 #undef SET_OPT_ITEM
188 while (1) {
189 handle = NULL;
190 ModalDialog(NULL, &item);
191 if ( item == OPT_OK )
192 break;
193 if ( item == OPT_CANCEL ) {
194 DisposeDialog(dialog);
195 exit(0);
197 #if !TARGET_API_MAC_CARBON
198 if ( item == OPT_HELP ) {
199 HMSetBalloons(!HMGetBalloons());
201 #endif
202 #if !TARGET_API_MAC_OSX
203 if ( item == OPT_CMDLINE ) {
204 int old_argc = *argcp;
205 int i;
206 int new_argc, newer_argc;
207 char **new_argv, **newer_argv;
209 new_argc = ccommand(&new_argv);
210 newer_argc = (new_argc-1) + old_argc;
211 newer_argv = malloc((newer_argc+1)*sizeof(char *));
212 if( !newer_argv )
213 Py_FatalError("Cannot malloc argv\n");
214 for(i=0; i<old_argc; i++)
215 newer_argv[i] = (*argvp)[i];
216 for(i=old_argc; i<=newer_argc; i++) /* Copy the NULL too */
217 newer_argv[i] = new_argv[i-old_argc+1];
218 *argvp = newer_argv;
219 *argcp = newer_argc;
221 /* XXXX Is it not safe to use free() here, apparently */
223 #endif /* !TARGET_API_MAC_OSX */
224 #define OPT_ITEM(num, var) \
225 if ( item == (num) ) { \
226 p->var = !p->var; \
227 GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
228 SetControlValue(handle, (short)p->var); \
231 OPT_ITEM(OPT_INSPECT, inspect);
232 OPT_ITEM(OPT_VERBOSE, verbose);
233 if ( item == OPT_VERBOSEVERBOSE ) {
234 if ( p->verbose == 2 )
235 p->verbose = 1;
236 else
237 p->verbose = 2;
238 GetDialogItem(dialog, OPT_VERBOSE, &type, (Handle *)&handle, &rect);
239 SetControlValue(handle, 1);
241 GetDialogItem(dialog, OPT_VERBOSEVERBOSE, &type, (Handle *)&handle, &rect);
242 SetControlValue(handle, p->verbose == 2);
243 OPT_ITEM(OPT_OPTIMIZE, optimize);
244 OPT_ITEM(OPT_UNBUFFERED, unbuffered);
245 OPT_ITEM(OPT_DEBUGGING, debugging);
246 if ( item == OPT_KEEPALWAYS ) p->keep_console = POPT_KEEPCONSOLE_ALWAYS;
247 if ( item == OPT_KEEPOUTPUT ) p->keep_console = POPT_KEEPCONSOLE_OUTPUT;
248 if ( item == OPT_KEEPERROR ) p->keep_console = POPT_KEEPCONSOLE_ERROR;
249 if ( item == OPT_KEEPNEVER ) p->keep_console = POPT_KEEPCONSOLE_NEVER;
250 GetDialogItem(dialog, OPT_KEEPALWAYS, &type, (Handle *)&handle, &rect);
251 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_ALWAYS));
252 GetDialogItem(dialog, OPT_KEEPOUTPUT, &type, (Handle *)&handle, &rect);
253 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_OUTPUT));
254 GetDialogItem(dialog, OPT_KEEPERROR, &type, (Handle *)&handle, &rect);
255 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_ERROR));
256 GetDialogItem(dialog, OPT_KEEPNEVER, &type, (Handle *)&handle, &rect);
257 SetControlValue(handle, (short)(p->keep_console == POPT_KEEPCONSOLE_NEVER));
258 OPT_ITEM(OPT_TABWARN, tabwarn);
259 OPT_ITEM(OPT_NOSITE, nosite);
260 OPT_ITEM(OPT_DIVISIONWARN, divisionwarn);
261 OPT_ITEM(OPT_UNIXNEWLINES, unixnewlines);
263 #undef OPT_ITEM
265 DisposeDialog(dialog);
269 ** Initialization code, shared by interpreter and applets
271 static void
272 init_common(int *argcp, char ***argvp, int embedded)
274 /* Remember resource fork refnum, for later */
275 PyMac_AppRefNum = CurResFile();
277 /* Initialize toolboxes */
278 init_mac_world();
280 #ifdef USE_MAC_SHARED_LIBRARY
281 /* Add the shared library to the stack of resource files */
282 (void)PyMac_init_process_location();
283 PyMac_AddLibResources();
284 #endif
286 #if defined(USE_GUSI1)
287 /* Setup GUSI */
288 GUSIDefaultSetup();
289 PyMac_SetGUSISpin();
290 PyMac_SetGUSIOptions();
291 #endif
292 #if defined(USE_GUSI)
293 atexit(PyMac_StopGUSISpin);
294 #endif
296 #ifdef USE_SIOUX
297 /* Set various SIOUX flags. Some are changed later based on options */
298 #if 0
299 SIOUXSettings.standalone = 0; /* XXXX Attempting to keep sioux from eating events */
300 #endif
301 SIOUXSettings.asktosaveonclose = 0;
302 SIOUXSettings.showstatusline = 0;
303 SIOUXSettings.tabspaces = 4;
304 #endif
306 /* Get options from preference file (or from applet resource fork) */
307 PyMac_options.keep_console = POPT_KEEPCONSOLE_OUTPUT; /* default-default */
308 PyMac_options.unixnewlines = 1;
309 #if !TARGET_API_MAC_OSX
310 PyMac_PreferenceOptions(&PyMac_options);
311 #endif
313 if ( embedded ) {
314 static char *emb_argv[] = {"embedded-python", 0};
316 *argcp = 1;
317 *argvp = emb_argv;
318 } else {
319 /* Create argc/argv. Do it before we go into the options event loop.
320 ** In MachoPython we skip this step if we already have plausible
321 ** command line arguments.
323 #if TARGET_API_MAC_OSX
324 if (*argcp == 2 && strncmp((*argvp)[1], "-psn_", 5) == 0)
325 #endif
326 *argcp = PyMac_GetArgv(argvp, PyMac_options.noargs);
327 #if !TARGET_API_MAC_OSX
328 #ifndef NO_ARGV0_CHDIR
329 if (*argcp >= 1 && (*argvp)[0] && (*argvp)[0][0]) {
330 /* Workaround for MacOS X, which currently (DP4) doesn't set
331 ** the working folder correctly
333 char app_wd[256], *p;
335 strncpy(app_wd, (*argvp)[0], 256);
336 p = strrchr(app_wd, ':');
337 if ( p ) *p = 0;
338 chdir(app_wd);
340 #endif
341 #endif
342 /* Do interactive option setting, if allowed and <option> depressed */
343 PyMac_InteractiveOptions(&PyMac_options, argcp, argvp);
346 /* Copy selected options to where the machine-independent stuff wants it */
347 Py_VerboseFlag = PyMac_options.verbose;
348 /* Py_SuppressPrintingFlag = PyMac_options.suppress_print; */
349 Py_OptimizeFlag = PyMac_options.optimize;
350 Py_DebugFlag = PyMac_options.debugging;
351 Py_NoSiteFlag = PyMac_options.nosite;
352 Py_TabcheckFlag = PyMac_options.tabwarn;
353 Py_DivisionWarningFlag = PyMac_options.divisionwarn;
354 #if !TARGET_API_MAC_OSX
355 if ( PyMac_options.noargs ) {
356 /* don't process events at all without the scripts permission */
357 PyMacSchedParams scp;
359 PyMac_GetSchedParams(&scp);
360 scp.process_events = 0;
361 /* Should we disable command-dot as well? */
362 PyMac_SetSchedParams(&scp);
364 #endif /* !TARGET_API_MAC_OSX */
366 /* Set buffering */
367 if (PyMac_options.unbuffered) {
368 #ifndef MPW
369 setbuf(stdout, (char *)NULL);
370 setbuf(stderr, (char *)NULL);
371 #else
372 /* On MPW (3.2) unbuffered seems to hang */
373 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
374 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
375 #endif
377 #if __profile__ == 1
378 /* collectSummary or collectDetailed, timebase, #routines, max stack depth */
379 ProfilerInit(collectSummary, bestTimeBase, 8000, 250);
380 #endif
382 /* Tell the rest of python about our argc/argv */
383 orig_argc = *argcp; /* For Py_GetArgcArgv() */
384 orig_argv = *argvp;
385 Py_SetProgramName((*argvp)[0]);
389 ** Inspection mode after script/applet termination
391 static int
392 run_inspect(void)
394 int sts = 0;
396 if (PyMac_options.inspect && isatty((int)fileno(stdin)))
397 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
398 return sts;
401 #ifdef USE_MAC_APPLET_SUPPORT
402 /* Applet support */
404 /* Run a compiled Python Python script from 'PYC ' resource __main__ */
405 static int
406 run_main_resource(void)
408 Handle h;
409 long size;
410 PyObject *code;
411 PyObject *result;
413 h = GetNamedResource('PYC ', "\p__main__");
414 if (h == NULL) {
415 Alert(NOPYC_ALERT, NULL);
416 return 1;
418 size = GetResourceSizeOnDisk(h);
419 HLock(h);
420 code = PyMarshal_ReadObjectFromString(*h + 8, (int)(size - 8));
421 HUnlock(h);
422 ReleaseResource(h);
423 if (code == NULL) {
424 PyErr_Print();
425 return 1;
427 result = PyImport_ExecCodeModule("__main__", code);
428 Py_DECREF(code);
429 if (result == NULL) {
430 PyErr_Print();
431 return 1;
433 Py_DECREF(result);
434 return 0;
437 /* Initialization sequence for applets */
438 void
439 PyMac_InitApplet(void)
441 int argc;
442 char **argv;
443 int err;
445 init_common(&argc, &argv, 0);
447 Py_Initialize();
448 PySys_SetArgv(argc, argv);
450 err = run_main_resource();
452 err = (run_inspect() || err);
454 fflush(stderr);
455 fflush(stdout);
456 PyMac_Exit(err);
457 /* XXX Should we bother to Py_Exit(sts)? */
461 ** Hook for embedding python.
463 void
464 PyMac_Initialize(void)
466 int argc;
467 char **argv;
469 init_common(&argc, &argv, 1);
470 Py_Initialize();
471 PySys_SetArgv(argc, argv);
474 #endif /* USE_MAC_APPLET_SUPPORT */
476 #if TARGET_API_MAC_OSX /* Really: TARGET_API_MAC_CARBON */
478 static int
479 locateResourcePy(char * resourceName, char * resourceURLCStr, int length) {
480 CFBundleRef mainBundle = NULL;
481 CFURLRef URL, absoluteURL;
482 CFStringRef filenameString, filepathString, rsrcString;
483 CFIndex size, i;
484 CFArrayRef arrayRef = NULL;
485 int success = 0;
487 #if TARGET_API_MAC_OSX
488 CFURLPathStyle thePathStyle = kCFURLPOSIXPathStyle;
489 #else
490 CFURLPathStyle thePathStyle = kCFURLHFSPathStyle;
491 #endif
493 /* Get a reference to our main bundle */
494 mainBundle = CFBundleGetMainBundle();
496 /* If we are running inside a bundle, look through it. Otherwise, do nothing. */
497 if (mainBundle) {
498 /* Create a CFString with the resource name in it */
499 rsrcString = CFStringCreateWithCString(0, resourceName, kCFStringEncodingMacRoman);
501 /* Look for py files in the main bundle by type */
502 arrayRef = CFBundleCopyResourceURLsOfType( mainBundle,
503 CFSTR("py"),
504 NULL );
506 /* See if there are any filename matches */
507 size = CFArrayGetCount(arrayRef);
508 for (i = 0; i < size; i++) {
509 URL = CFArrayGetValueAtIndex(arrayRef, i);
510 filenameString = CFURLCopyLastPathComponent(URL);
511 if (CFStringCompare(filenameString, rsrcString, 0) == kCFCompareEqualTo) {
512 /* We found a match, get the file's full path */
513 absoluteURL = CFURLCopyAbsoluteURL(URL);
514 filepathString = CFURLCopyFileSystemPath(absoluteURL, thePathStyle);
515 CFRelease(absoluteURL);
517 /* Copy the full path into the caller's character buffer */
518 success = CFStringGetCString(filepathString, resourceURLCStr, length,
519 kCFStringEncodingMacRoman);
521 CFRelease(filepathString);
523 CFRelease(filenameString);
525 CFRelease(arrayRef);
526 CFRelease(rsrcString);
528 return success;
531 #endif /* TARGET_API_MAC_CARBON */
533 #if TARGET_API_MAC_OSX
536 main(int argc, char **argv)
538 int i;
539 static char scriptpath[1024];
540 char *script = NULL;
542 /* First we see whether we have __rawmain__.py and run that if it
543 ** is there
545 if (locateResourcePy("__rawmain__.py", scriptpath, 1024)) {
546 /* If we have a raw main we don't do AppleEvent processing.
547 ** Notice that this also means we keep the -psn.... argv[1]
548 ** value intact. Not sure whether that is important to someone,
549 ** but you never know...
551 script = scriptpath;
552 } else {
553 /* Otherwise we look for __main__.py. Whether that is
554 ** found or not we also process AppleEvent arguments.
556 if (locateResourcePy("__main__.py", scriptpath, 1024))
557 script = scriptpath;
559 printf("original argc=%d\n", argc);
560 for(i=0; i<argc; i++) printf("original argv[%d] = \"%s\"\n", i, argv[i]);
562 init_common(&argc, &argv, 0);
564 printf("modified argc=%d\n", argc);
565 for(i=0; i<argc; i++) printf("modified argv[%d] = \"%s\"\n", i, argv[i]);
568 Py_Main(argc, argv, script);
569 return 0;
572 #else
574 /* For normal application */
575 void
576 PyMac_InitApplication(void)
578 int argc;
579 char **argv;
581 static char scriptpath[1024];
582 char *script = NULL;
584 init_common(&argc, &argv, 0);
586 #if TARGET_API_MAC_OSX /* Really: TARGET_API_MAC_CARBON */
587 /* If we are running inside of a bundle, and a __main__.py is available, use it */
588 if (locateResourcePy("__main__.py", scriptpath, 1024))
589 script = scriptpath;
590 #endif
592 if ( argc > 1 ) {
593 /* We're running a script. Attempt to change current directory */
594 char curwd[256], *endp;
596 strcpy(curwd, argv[1]);
597 endp = strrchr(curwd, ':');
598 if ( endp && endp > curwd ) {
599 *endp = '\0';
601 chdir(curwd);
602 #ifdef USE_GUSI1
603 /* Change MacOS's idea of wd too */
604 PyMac_FixGUSIcd();
605 #endif
607 /* Check that the first argument is a text file */
608 if ( PyMac_getfiletype(argv[1]) != 'TEXT' ) {
609 Alert(NOTASCRIPT_ID, NULL);
610 exit(0);
613 Py_Main(argc, argv, script);
615 #endif /* TARGET_API_MAC_OSX */
617 /* Main program */
619 static void
620 Py_Main(int argc, char **argv, char *filename)
622 int sts;
623 char *command = NULL;
624 FILE *fp = stdin;
626 if ( filename ) {
627 /* Someone else has found our "script" already */
628 argv[0] = filename;
629 } else {
630 filename = argv[1];
631 argv++;
632 argc--;
635 if (Py_VerboseFlag ||
636 (command == NULL && filename == NULL && isatty((int)fileno(fp))))
637 fprintf(stderr, "Python %s on %s\n%s\n",
638 Py_GetVersion(), Py_GetPlatform(), COPYRIGHT);
640 if (filename != NULL) {
641 if ((fp = fopen(filename, "r")) == NULL) {
642 fprintf(stderr, "%s: can't open file '%s'\n",
643 argv[0], filename);
644 PyMac_Exit(2);
648 #if !TARGET_API_MAC_OSX
649 /* We initialize the menubar here, hoping SIOUX is initialized by now */
650 PyMac_InitMenuBar();
651 #endif
653 Py_Initialize();
655 PyUnicode_SetDefaultEncoding(PyMac_getscript());
657 PySys_SetArgv(argc, argv);
659 if (filename == NULL && isatty((int)fileno(fp))) {
660 FILE *fp = fopen(STARTUP, "r");
661 if (fp != NULL) {
662 (void) PyRun_SimpleFile(fp, STARTUP);
663 PyErr_Clear();
664 fclose(fp);
667 sts = PyRun_AnyFile(
668 fp, filename == NULL ? "<stdin>" : filename) != 0;
669 if (filename != NULL)
670 fclose(fp);
672 if ( filename != NULL || command != NULL )
673 sts = (run_inspect() || sts);
675 Py_Exit(sts);
676 /*NOTREACHED*/
679 #if !TARGET_API_MAC_OSX
681 ** Reset the "unseen output" flag
683 void
684 PyMac_OutputSeen(void)
686 if ( console_output_state == STATE_UNKNOWN )
687 PyMac_InitMenuBar();
688 console_output_state = STATE_LASTREAD;
692 ** Set the "unseen output" flag
694 void
695 PyMac_OutputNotSeen(void)
697 if ( console_output_state == STATE_UNKNOWN )
698 PyMac_InitMenuBar();
699 console_output_state = STATE_LASTWRITE;
703 ** Override abort() - The default one is not what we want.
705 void
706 abort(void)
708 console_output_state = STATE_LASTWRITE;
709 PyMac_Exit(1);
711 #endif /* !TARGET_API_MAC_OSX */
714 ** Terminate application
716 void
717 PyMac_Exit(int status)
719 #ifdef USE_SIOUX
720 int keep = 0;
721 #endif
723 #if __profile__ == 1
724 ProfilerDump("\pPython Profiler Results");
725 ProfilerTerm();
726 #endif
728 #ifdef USE_SIOUX
729 switch (PyMac_options.keep_console) {
730 case POPT_KEEPCONSOLE_NEVER:
731 keep = 0;
732 break;
733 case POPT_KEEPCONSOLE_OUTPUT:
734 if (console_output_state == STATE_LASTWRITE ||
735 console_output_state == STATE_UNKNOWN )
736 keep = 1;
737 else
738 keep = 0;
739 break;
740 case POPT_KEEPCONSOLE_ERROR:
741 keep = (status != 0);
742 break;
743 default:
744 keep = 1;
746 if (keep) {
747 SIOUXSettings.standalone = 1;
748 SIOUXSettings.autocloseonquit = 0;
749 SIOUXSetTitle("\p\307terminated\310");
750 PyMac_RaiseConsoleWindow();
751 PyMac_RestoreMenuBar();
752 #ifdef USE_MSL
754 ** Temporary workaround: autocloseonquit clearing does not
755 ** currently work for the MSL/GUSI combo.
757 while(getchar() > 0);
758 #endif
760 else
761 SIOUXSettings.autocloseonquit = 1;
762 #endif /* USE_SIOUX */
764 exit(status);
767 #if !TARGET_API_MAC_OSX
768 /* Make the *original* argc/argv available to other modules.
769 This is rare, but it is needed by the secureware extension. */
771 void
772 Py_GetArgcArgv(int *argc,char ***argv)
774 *argc = orig_argc;
775 *argv = orig_argv;
777 #endif
779 /* More cruft that shouldn't really be here, used in sysmodule.c */
780 #if !TARGET_API_MAC_OSX
781 /* Return the program name -- some code out there needs this. */
782 char *
783 Py_GetProgramFullPath(void)
785 return orig_argv[0];
788 char *
789 Py_GetPrefix(void)
791 return PyMac_GetPythonDir();
794 char *
795 Py_GetExecPrefix(void)
797 return PyMac_GetPythonDir();
801 PyMac_GetDelayConsoleFlag(void)
803 return (int)PyMac_options.delayconsole;
806 #ifndef WITHOUT_UNIX_NEWLINES
808 ** Experimental feature (for 2.2a2): optionally allow unix newlines
809 ** as well as Mac newlines on input. We replace a lowlevel
810 ** MSL routine to accomplish this.
812 void
813 __convert_to_newlines(unsigned char * buf, size_t * n_ptr)
815 unsigned char *p;
816 size_t n = *n_ptr;
818 for(p=buf; n > 0; p++, n--)
819 if ( *p == '\r' ) *p = '\n';
820 else if ( *p == '\n' && !PyMac_options.unixnewlines )
821 *p = '\r';
823 #endif /* WITHOUT_UNIX_NEWLINES */
824 #endif /* !TARGET_API_MAC_OSX */