summaryrefslogtreecommitdiffstats
path: root/Mac
diff options
context:
space:
mode:
authorJack Jansen <jack.jansen@cwi.nl>1996-09-07 17:09:31 (GMT)
committerJack Jansen <jack.jansen@cwi.nl>1996-09-07 17:09:31 (GMT)
commit7d5f9e847a75c65f97068b3fd7980e62717c16fc (patch)
tree92ba6ec03558f4759c402221e7d7bae2ca7ee5eb /Mac
parent6d653ab1b6463957f0c4d9059e778132c65123fa (diff)
downloadcpython-7d5f9e847a75c65f97068b3fd7980e62717c16fc.zip
cpython-7d5f9e847a75c65f97068b3fd7980e62717c16fc.tar.gz
cpython-7d5f9e847a75c65f97068b3fd7980e62717c16fc.tar.bz2
- Put all options in a struct
- Unified initialization code for interpreter and applet - Implemented new options to skip AE-processing for argc/argv and for disabling interactive option setting
Diffstat (limited to 'Mac')
-rw-r--r--Mac/Include/pythonresources.h16
-rw-r--r--Mac/Python/macgetargv.c11
-rw-r--r--Mac/Python/macgetpath.c21
-rw-r--r--Mac/Python/macglue.c6
-rw-r--r--Mac/Python/macmain.c298
5 files changed, 196 insertions, 156 deletions
diff --git a/Mac/Include/pythonresources.h b/Mac/Include/pythonresources.h
index 02607bd..514eb40 100644
--- a/Mac/Include/pythonresources.h
+++ b/Mac/Include/pythonresources.h
@@ -95,13 +95,23 @@
#define POPT_NOINTOPT 7 /* Not settable interactively */
#define POPT_NOARGS 8 /* Not settable interactively */
+typedef struct PyMac_PrefRecord {
+ unsigned char inspect;
+ unsigned char verbose;
+ unsigned char suppress_print;
+ unsigned char unbuffered;
+ unsigned char debugging;
+ unsigned char keep_normal;
+ unsigned char keep_error;
+ unsigned char nointopt;
+ unsigned char noargs;
+} PyMac_PrefRecord;
+
/* The GUSI options resources */
#define GUSIOPTIONS_ID 10240
#define GUSIOPTIONSOVERRIDE_ID 10241
/* From macgetpath.c: */
-void PyMac_PreferenceOptions Py_PROTO((int *inspect, int *verbose, int *suppress_print,
- int *unbuffered, int *debugging, int *keep_normal,
- int *keep_error));
+void PyMac_PreferenceOptions Py_PROTO((PyMac_PrefRecord *));
diff --git a/Mac/Python/macgetargv.c b/Mac/Python/macgetargv.c
index a06b100..e0ecb8e 100644
--- a/Mac/Python/macgetargv.c
+++ b/Mac/Python/macgetargv.c
@@ -295,16 +295,19 @@ event_loop()
/* Get the argv vector, return argc */
int
-PyMac_GetArgv(pargv)
+PyMac_GetArgv(pargv, noevents)
char ***pargv;
+ int noevents;
{
arg_count = 0;
arg_vector[arg_count++] = strdup(get_application_name());
- set_ae_handlers();
- event_loop();
- reset_ae_handlers();
+ if( !noevents ) {
+ set_ae_handlers();
+ event_loop();
+ reset_ae_handlers();
+ }
arg_vector[arg_count] = NULL;
diff --git a/Mac/Python/macgetpath.c b/Mac/Python/macgetpath.c
index 5c2cafd..466f073 100644
--- a/Mac/Python/macgetpath.c
+++ b/Mac/Python/macgetpath.c
@@ -310,9 +310,7 @@ out:
#endif /* !USE_BUILTIN_PATH */
void
-PyMac_PreferenceOptions(int *inspect, int *verbose, int *suppress_print,
- int *unbuffered, int *debugging, int *keep_normal,
- int *keep_error)
+PyMac_PreferenceOptions(PyMac_PrefRecord *pr)
{
short oldrh, prefrh = -1;
Handle handle;
@@ -339,14 +337,15 @@ PyMac_PreferenceOptions(int *inspect, int *verbose, int *suppress_print,
size = GetHandleSize(handle);
p = (char *)*handle;
- if ( size > POPT_INSPECT ) *inspect = p[POPT_INSPECT];
- if ( size > POPT_VERBOSE ) *verbose = p[POPT_VERBOSE];
- if ( size > POPT_SUPPRESS ) *suppress_print = p[POPT_SUPPRESS];
- if ( size > POPT_UNBUFFERED ) *unbuffered = p[POPT_UNBUFFERED];
- if ( size > POPT_DEBUGGING ) *debugging = p[POPT_DEBUGGING];
- if ( size > POPT_KEEPNORM ) *keep_normal = p[POPT_KEEPNORM];
- if ( size > POPT_KEEPERR ) *keep_error = p[POPT_KEEPERR];
- /* The rest are not implemented yet */
+ if ( size > POPT_INSPECT ) pr->inspect = p[POPT_INSPECT];
+ if ( size > POPT_VERBOSE ) pr->verbose = p[POPT_VERBOSE];
+ if ( size > POPT_SUPPRESS ) pr->suppress_print = p[POPT_SUPPRESS];
+ if ( size > POPT_UNBUFFERED ) pr->unbuffered = p[POPT_UNBUFFERED];
+ if ( size > POPT_DEBUGGING ) pr->debugging = p[POPT_DEBUGGING];
+ if ( size > POPT_KEEPNORM ) pr->keep_normal = p[POPT_KEEPNORM];
+ if ( size > POPT_KEEPERR ) pr->keep_error = p[POPT_KEEPERR];
+ if ( size > POPT_NOINTOPT ) pr->nointopt = p[POPT_NOINTOPT];
+ if ( size > POPT_NOARGS ) pr->noargs = p[POPT_NOARGS];
HUnlock(handle);
diff --git a/Mac/Python/macglue.c b/Mac/Python/macglue.c
index 17bbff0..3df1a8a 100644
--- a/Mac/Python/macglue.c
+++ b/Mac/Python/macglue.c
@@ -178,9 +178,11 @@ PyMac_FixGUSIcd()
return;
}
-#ifdef __CFM68K__
+/*
+** SpinCursor (needed by GUSI) drags in heaps of stuff, so we
+** provide a dummy here.
+*/
void SpinCursor(short x) { /* Dummy */ }
-#endif /* __CFM68K */
/*
** Replacement GUSI Spin function
diff --git a/Mac/Python/macmain.c b/Mac/Python/macmain.c
index 095673e..b464872 100644
--- a/Mac/Python/macmain.c
+++ b/Mac/Python/macmain.c
@@ -60,16 +60,15 @@ extern char *Py_GetVersion Py_PROTO((void));
extern char *Py_GetCopyright Py_PROTO((void));
-/* For Py_GetProgramName(); set by main() */
-static char *argv0;
+/* #define OBSOLETE_ARGCARGV 1 /* I think this is not needed anymore... */
+#ifdef OBSOLETE_ARGCARGV
/* For Py_GetArgcArgv(); set by main() */
static char **orig_argv;
static int orig_argc;
+#endif
-/* Flags indicating whether stdio window should stay open on termination */
-static int keep_normal;
-static int keep_error = 1;
+PyMac_PrefRecord options;
static void Py_Main Py_PROTO((int, char **)); /* Forward */
void PyMac_Exit Py_PROTO((int)); /* Forward */
@@ -93,10 +92,108 @@ init_mac_world()
#endif
}
-/* Initialization code shared by interpreter and applets */
+/*
+** PyMac_InteractiveOptions - Allow user to set options if option key is pressed
+*/
+static void
+PyMac_InteractiveOptions(PyMac_PrefRecord *p, int *argcp, char ***argvp)
+{
+ KeyMap rmap;
+ unsigned char *map;
+ short item, type;
+ ControlHandle handle;
+ DialogPtr dialog;
+ Rect rect;
+ int old_argc = *argcp;
+ int i;
+
+ /*
+ ** If the preferences disallows interactive options we return,
+ ** similarly of <option> isn't pressed.
+ */
+ if (p->nointopt) return;
+
+ GetKeys(rmap);
+ map = (unsigned char *)rmap;
+ if ( ( map[0x3a>>3] & (1<<(0x3a&7)) ) == 0 ) /* option key is 3a */
+ return;
+
+ dialog = GetNewDialog(OPT_DIALOG, NULL, (WindowPtr)-1);
+ if ( dialog == NULL ) {
+ printf("Option dialog not found - cannot set options\n");
+ return;
+ }
+ SetDialogDefaultItem(dialog, OPT_OK);
+ SetDialogCancelItem(dialog, OPT_CANCEL);
+
+ /* Set default values */
+#define SET_OPT_ITEM(num, var) \
+ GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
+ SetCtlValue(handle, (short)p->var);
+
+ SET_OPT_ITEM(OPT_INSPECT, inspect);
+ SET_OPT_ITEM(OPT_VERBOSE, verbose);
+ SET_OPT_ITEM(OPT_SUPPRESS, suppress_print);
+ SET_OPT_ITEM(OPT_UNBUFFERED, unbuffered);
+ SET_OPT_ITEM(OPT_DEBUGGING, debugging);
+ SET_OPT_ITEM(OPT_KEEPNORMAL, keep_normal);
+ SET_OPT_ITEM(OPT_KEEPERROR, keep_error);
+ /* The rest are not settable interactively */
+
+#undef SET_OPT_ITEM
+
+ while (1) {
+ handle = NULL;
+ ModalDialog(NULL, &item);
+ if ( item == OPT_OK )
+ break;
+ if ( item == OPT_CANCEL ) {
+ DisposDialog(dialog);
+ exit(0);
+ }
+ if ( item == OPT_CMDLINE ) {
+ int new_argc, newer_argc;
+ char **new_argv, **newer_argv;
+
+ new_argc = ccommand(&new_argv);
+ newer_argc = (new_argc-1) + old_argc;
+ newer_argv = malloc((newer_argc+1)*sizeof(char *));
+ if( !newer_argv )
+ Py_FatalError("Cannot malloc argv\n");
+ for(i=0; i<old_argc; i++)
+ newer_argv[i] = (*argvp)[i];
+ for(i=old_argc; i<=newer_argc; i++) /* Copy the NULL too */
+ newer_argv[i] = new_argv[i-old_argc+1];
+ *argvp = newer_argv;
+ *argcp = newer_argc;
+
+ /* XXXX Is it not safe to use free() here, apparently */
+ }
+#define OPT_ITEM(num, var) \
+ if ( item == (num) ) { \
+ p->var = !p->var; \
+ GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
+ SetCtlValue(handle, (short)p->var); \
+ }
+
+ OPT_ITEM(OPT_INSPECT, inspect);
+ OPT_ITEM(OPT_VERBOSE, verbose);
+ OPT_ITEM(OPT_SUPPRESS, suppress_print);
+ OPT_ITEM(OPT_UNBUFFERED, unbuffered);
+ OPT_ITEM(OPT_DEBUGGING, debugging);
+ OPT_ITEM(OPT_KEEPNORMAL, keep_normal);
+ OPT_ITEM(OPT_KEEPERROR, keep_error);
+
+#undef OPT_ITEM
+ }
+ DisposDialog(dialog);
+}
+/*
+** Initialization code, shared by interpreter and applets
+*/
static void
-init_common()
+init_common(int *argcp, char ***argvp)
{
/* Remember resource fork refnum, for later */
PyMac_AppRefNum = CurResFile();
@@ -123,8 +220,48 @@ init_common()
SIOUXSettings.tabspaces = 4;
#endif
+ /* Get options from preference file (or from applet resource fork) */
+ options.keep_error = 1; /* default-default */
+ PyMac_PreferenceOptions(&options);
+
+ /* Create argc/argv. Do it before we go into the options event loop. */
+ *argcp = PyMac_GetArgv(argvp, options.noargs);
+
+ /* Do interactive option setting, if allowed and <option> depressed */
+ PyMac_InteractiveOptions(&options, argcp, argvp);
+
+ /* Copy selected options to where the machine-independent stuff wants it */
+ Py_VerboseFlag = options.verbose;
+ Py_SuppressPrintingFlag = options.suppress_print;
+ Py_DebugFlag = options.debugging;
+ if ( options.noargs )
+ PyMac_DoYieldEnabled = 0;
+
+ /* Set buffering */
+ if (options.unbuffered) {
+#ifndef MPW
+ setbuf(stdout, (char *)NULL);
+ setbuf(stderr, (char *)NULL);
+#else
+ /* On MPW (3.2) unbuffered seems to hang */
+ setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
+ setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
+#endif
+ }
}
+/*
+** Inspection mode after script/applet termination
+*/
+static int
+run_inspect()
+{
+ int sts = 0;
+
+ if (options.inspect && isatty((int)fileno(stdin)))
+ sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
+ return sts;
+}
#ifdef USE_MAC_APPLET_SUPPORT
/* Applet support */
@@ -170,11 +307,15 @@ PyMac_InitApplet()
char **argv;
int err;
- init_common();
- argc = PyMac_GetArgv(&argv);
+ init_common(&argc, &argv);
+
Py_Initialize();
PySys_SetArgv(argc, argv);
+
err = run_main_resource();
+
+ err = (run_inspect() || err);
+
fflush(stderr);
fflush(stdout);
PyMac_Exit(err);
@@ -190,8 +331,8 @@ PyMac_InitApplication()
int argc;
char **argv;
- init_common();
- argc = PyMac_GetArgv(&argv);
+ init_common(&argc, &argv);
+
if ( argc > 1 ) {
/* We're running a script. Attempt to change current directory */
char curwd[256], *endp;
@@ -211,103 +352,6 @@ PyMac_InitApplication()
Py_Main(argc, argv);
}
-/*
-** PyMac_InteractiveOptions - Allow user to set options if option key is pressed
-*/
-void
-PyMac_InteractiveOptions(int *inspect, int *verbose, int *suppress_print,
- int *unbuffered, int *debugging, int *keep_normal,
- int *keep_error, int *argcp, char ***argvp)
-{
- KeyMap rmap;
- unsigned char *map;
- short item, type;
- ControlHandle handle;
- DialogPtr dialog;
- Rect rect;
- int old_argc = *argcp;
- int i;
-
- /* Default-defaults: */
- *keep_error = 1;
- /* Get default settings from our preference file */
- PyMac_PreferenceOptions(inspect, verbose, suppress_print,
- unbuffered, debugging, keep_normal, keep_error);
- /* If option is pressed override these */
- GetKeys(rmap);
- map = (unsigned char *)rmap;
- if ( ( map[0x3a>>3] & (1<<(0x3a&7)) ) == 0 ) /* option key is 3a */
- return;
-
- dialog = GetNewDialog(OPT_DIALOG, NULL, (WindowPtr)-1);
- if ( dialog == NULL ) {
- printf("Option dialog not found - cannot set options\n");
- return;
- }
- SetDialogDefaultItem(dialog, OPT_OK);
- SetDialogCancelItem(dialog, OPT_CANCEL);
-
- /* Set default values */
-#define SET_OPT_ITEM(num, var) \
- GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
- SetCtlValue(handle, (short)*(var));
-
- SET_OPT_ITEM(OPT_INSPECT, inspect);
- SET_OPT_ITEM(OPT_VERBOSE, verbose);
- SET_OPT_ITEM(OPT_SUPPRESS, suppress_print);
- SET_OPT_ITEM(OPT_UNBUFFERED, unbuffered);
- SET_OPT_ITEM(OPT_DEBUGGING, debugging);
- SET_OPT_ITEM(OPT_KEEPNORMAL, keep_normal);
- SET_OPT_ITEM(OPT_KEEPERROR, keep_error);
-
-#undef SET_OPT_ITEM
-
- while (1) {
- handle = NULL;
- ModalDialog(NULL, &item);
- if ( item == OPT_OK )
- break;
- if ( item == OPT_CANCEL ) {
- DisposDialog(dialog);
- exit(0);
- }
- if ( item == OPT_CMDLINE ) {
- int new_argc, newer_argc;
- char **new_argv, **newer_argv;
-
- new_argc = ccommand(&new_argv);
- newer_argc = (new_argc-1) + old_argc;
- newer_argv = malloc((newer_argc+1)*sizeof(char *));
- if( !newer_argv )
- Py_FatalError("Cannot malloc argv\n");
- for(i=0; i<old_argc; i++)
- newer_argv[i] = (*argvp)[i];
- for(i=old_argc; i<=newer_argc; i++) /* Copy the NULL too */
- newer_argv[i] = new_argv[i-old_argc+1];
- *argvp = newer_argv;
- *argcp = newer_argc;
-
- /* XXXX Is it not safe to use free() here, apparently */
- }
-#define OPT_ITEM(num, var) \
- if ( item == (num) ) { \
- *(var) = !*(var); \
- GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \
- SetCtlValue(handle, (short)*(var)); \
- }
-
- OPT_ITEM(OPT_INSPECT, inspect);
- OPT_ITEM(OPT_VERBOSE, verbose);
- OPT_ITEM(OPT_SUPPRESS, suppress_print);
- OPT_ITEM(OPT_UNBUFFERED, unbuffered);
- OPT_ITEM(OPT_DEBUGGING, debugging);
- OPT_ITEM(OPT_KEEPNORMAL, keep_normal);
- OPT_ITEM(OPT_KEEPERROR, keep_error);
-
-#undef OPT_ITEM
- }
- DisposDialog(dialog);
-}
/* Main program */
static void
@@ -319,27 +363,11 @@ Py_Main(argc, argv)
char *command = NULL;
char *filename = NULL;
FILE *fp = stdin;
- int inspect = 0;
- int unbuffered = 0;
-
- PyMac_InteractiveOptions(&inspect, &Py_VerboseFlag, &Py_SuppressPrintingFlag,
- &unbuffered, &Py_DebugFlag, &keep_normal, &keep_error, &argc, &argv);
+#ifdef OBSOLETE_ARGCARGV
orig_argc = argc; /* For Py_GetArgcArgv() */
orig_argv = argv;
- argv0 = argv[0]; /* For Py_GetProgramName() */
-
- if (unbuffered) {
-#ifndef MPW
- setbuf(stdout, (char *)NULL);
- setbuf(stderr, (char *)NULL);
-#else
- /* On MPW (3.2) unbuffered seems to hang */
- setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
- setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
#endif
- }
-
filename = argv[1];
if (Py_VerboseFlag ||
@@ -355,10 +383,7 @@ Py_Main(argc, argv)
}
}
- /*
- ** For reasons I don't fully understand we cannot insert our
- ** menu earlier. Leave it here, we hope to be rid of Sioux soon anyway.
- */
+ /* We initialize the menubar here, hoping SIOUX is initialized by now */
PyMac_InitMenuBar();
Py_Initialize();
@@ -377,10 +402,9 @@ Py_Main(argc, argv)
fp, filename == NULL ? "<stdin>" : filename) != 0;
if (filename != NULL)
fclose(fp);
-
- if (inspect && isatty((int)fileno(stdin)) &&
- (filename != NULL || command != NULL))
- sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
+
+ if ( filename != NULL || command != NULL )
+ sts = (run_inspect() || sts);
Py_Exit(sts);
/*NOTREACHED*/
@@ -396,9 +420,9 @@ PyMac_Exit(status)
int keep;
if ( status )
- keep = keep_error;
+ keep = options.keep_error;
else
- keep = keep_normal;
+ keep = options.keep_normal;
#ifdef USE_SIOUX
if (keep) {
@@ -423,12 +447,13 @@ PyMac_Exit(status)
exit(status);
}
+#ifdef OBSOLETE_ARGCARGV
/* Return the program name -- some code out there needs this. */
char *
Py_GetProgramName()
{
- return argv0;
+ return orig_argv[0];
}
@@ -443,6 +468,7 @@ Py_GetArgcArgv(argc,argv)
*argc = orig_argc;
*argv = orig_argv;
}
+#endif /* OBSOLETE_ARGCARGV */
/* More cruft that shouldn't really be here, used in sysmodule.c */