Commit 7d5f9e84 authored by Jack Jansen's avatar Jack Jansen

- 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
parent 6d653ab1
...@@ -95,13 +95,23 @@ ...@@ -95,13 +95,23 @@
#define POPT_NOINTOPT 7 /* Not settable interactively */ #define POPT_NOINTOPT 7 /* Not settable interactively */
#define POPT_NOARGS 8 /* 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 */ /* The GUSI options resources */
#define GUSIOPTIONS_ID 10240 #define GUSIOPTIONS_ID 10240
#define GUSIOPTIONSOVERRIDE_ID 10241 #define GUSIOPTIONSOVERRIDE_ID 10241
/* From macgetpath.c: */ /* From macgetpath.c: */
void PyMac_PreferenceOptions Py_PROTO((int *inspect, int *verbose, int *suppress_print, void PyMac_PreferenceOptions Py_PROTO((PyMac_PrefRecord *));
int *unbuffered, int *debugging, int *keep_normal,
int *keep_error));
...@@ -295,16 +295,19 @@ event_loop() ...@@ -295,16 +295,19 @@ event_loop()
/* Get the argv vector, return argc */ /* Get the argv vector, return argc */
int int
PyMac_GetArgv(pargv) PyMac_GetArgv(pargv, noevents)
char ***pargv; char ***pargv;
int noevents;
{ {
arg_count = 0; arg_count = 0;
arg_vector[arg_count++] = strdup(get_application_name()); arg_vector[arg_count++] = strdup(get_application_name());
set_ae_handlers(); if( !noevents ) {
event_loop(); set_ae_handlers();
reset_ae_handlers(); event_loop();
reset_ae_handlers();
}
arg_vector[arg_count] = NULL; arg_vector[arg_count] = NULL;
......
...@@ -310,9 +310,7 @@ out: ...@@ -310,9 +310,7 @@ out:
#endif /* !USE_BUILTIN_PATH */ #endif /* !USE_BUILTIN_PATH */
void void
PyMac_PreferenceOptions(int *inspect, int *verbose, int *suppress_print, PyMac_PreferenceOptions(PyMac_PrefRecord *pr)
int *unbuffered, int *debugging, int *keep_normal,
int *keep_error)
{ {
short oldrh, prefrh = -1; short oldrh, prefrh = -1;
Handle handle; Handle handle;
...@@ -339,14 +337,15 @@ PyMac_PreferenceOptions(int *inspect, int *verbose, int *suppress_print, ...@@ -339,14 +337,15 @@ PyMac_PreferenceOptions(int *inspect, int *verbose, int *suppress_print,
size = GetHandleSize(handle); size = GetHandleSize(handle);
p = (char *)*handle; p = (char *)*handle;
if ( size > POPT_INSPECT ) *inspect = p[POPT_INSPECT]; if ( size > POPT_INSPECT ) pr->inspect = p[POPT_INSPECT];
if ( size > POPT_VERBOSE ) *verbose = p[POPT_VERBOSE]; if ( size > POPT_VERBOSE ) pr->verbose = p[POPT_VERBOSE];
if ( size > POPT_SUPPRESS ) *suppress_print = p[POPT_SUPPRESS]; if ( size > POPT_SUPPRESS ) pr->suppress_print = p[POPT_SUPPRESS];
if ( size > POPT_UNBUFFERED ) *unbuffered = p[POPT_UNBUFFERED]; if ( size > POPT_UNBUFFERED ) pr->unbuffered = p[POPT_UNBUFFERED];
if ( size > POPT_DEBUGGING ) *debugging = p[POPT_DEBUGGING]; if ( size > POPT_DEBUGGING ) pr->debugging = p[POPT_DEBUGGING];
if ( size > POPT_KEEPNORM ) *keep_normal = p[POPT_KEEPNORM]; if ( size > POPT_KEEPNORM ) pr->keep_normal = p[POPT_KEEPNORM];
if ( size > POPT_KEEPERR ) *keep_error = p[POPT_KEEPERR]; if ( size > POPT_KEEPERR ) pr->keep_error = p[POPT_KEEPERR];
/* The rest are not implemented yet */ if ( size > POPT_NOINTOPT ) pr->nointopt = p[POPT_NOINTOPT];
if ( size > POPT_NOARGS ) pr->noargs = p[POPT_NOARGS];
HUnlock(handle); HUnlock(handle);
......
...@@ -178,9 +178,11 @@ PyMac_FixGUSIcd() ...@@ -178,9 +178,11 @@ PyMac_FixGUSIcd()
return; return;
} }
#ifdef __CFM68K__ /*
** SpinCursor (needed by GUSI) drags in heaps of stuff, so we
** provide a dummy here.
*/
void SpinCursor(short x) { /* Dummy */ } void SpinCursor(short x) { /* Dummy */ }
#endif /* __CFM68K */
/* /*
** Replacement GUSI Spin function ** Replacement GUSI Spin function
......
...@@ -60,16 +60,15 @@ extern char *Py_GetVersion Py_PROTO((void)); ...@@ -60,16 +60,15 @@ extern char *Py_GetVersion Py_PROTO((void));
extern char *Py_GetCopyright Py_PROTO((void)); extern char *Py_GetCopyright Py_PROTO((void));
/* For Py_GetProgramName(); set by main() */ /* #define OBSOLETE_ARGCARGV 1 /* I think this is not needed anymore... */
static char *argv0;
#ifdef OBSOLETE_ARGCARGV
/* For Py_GetArgcArgv(); set by main() */ /* For Py_GetArgcArgv(); set by main() */
static char **orig_argv; static char **orig_argv;
static int orig_argc; static int orig_argc;
#endif
/* Flags indicating whether stdio window should stay open on termination */ PyMac_PrefRecord options;
static int keep_normal;
static int keep_error = 1;
static void Py_Main Py_PROTO((int, char **)); /* Forward */ static void Py_Main Py_PROTO((int, char **)); /* Forward */
void PyMac_Exit Py_PROTO((int)); /* Forward */ void PyMac_Exit Py_PROTO((int)); /* Forward */
...@@ -93,10 +92,108 @@ init_mac_world() ...@@ -93,10 +92,108 @@ init_mac_world()
#endif #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 static void
init_common() init_common(int *argcp, char ***argvp)
{ {
/* Remember resource fork refnum, for later */ /* Remember resource fork refnum, for later */
PyMac_AppRefNum = CurResFile(); PyMac_AppRefNum = CurResFile();
...@@ -123,8 +220,48 @@ init_common() ...@@ -123,8 +220,48 @@ init_common()
SIOUXSettings.tabspaces = 4; SIOUXSettings.tabspaces = 4;
#endif #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 #ifdef USE_MAC_APPLET_SUPPORT
/* Applet support */ /* Applet support */
...@@ -170,11 +307,15 @@ PyMac_InitApplet() ...@@ -170,11 +307,15 @@ PyMac_InitApplet()
char **argv; char **argv;
int err; int err;
init_common(); init_common(&argc, &argv);
argc = PyMac_GetArgv(&argv);
Py_Initialize(); Py_Initialize();
PySys_SetArgv(argc, argv); PySys_SetArgv(argc, argv);
err = run_main_resource(); err = run_main_resource();
err = (run_inspect() || err);
fflush(stderr); fflush(stderr);
fflush(stdout); fflush(stdout);
PyMac_Exit(err); PyMac_Exit(err);
...@@ -190,8 +331,8 @@ PyMac_InitApplication() ...@@ -190,8 +331,8 @@ PyMac_InitApplication()
int argc; int argc;
char **argv; char **argv;
init_common(); init_common(&argc, &argv);
argc = PyMac_GetArgv(&argv);
if ( argc > 1 ) { if ( argc > 1 ) {
/* We're running a script. Attempt to change current directory */ /* We're running a script. Attempt to change current directory */
char curwd[256], *endp; char curwd[256], *endp;
...@@ -211,103 +352,6 @@ PyMac_InitApplication() ...@@ -211,103 +352,6 @@ PyMac_InitApplication()
Py_Main(argc, argv); 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 */ /* Main program */
static void static void
...@@ -319,27 +363,11 @@ Py_Main(argc, argv) ...@@ -319,27 +363,11 @@ Py_Main(argc, argv)
char *command = NULL; char *command = NULL;
char *filename = NULL; char *filename = NULL;
FILE *fp = stdin; 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_argc = argc; /* For Py_GetArgcArgv() */
orig_argv = argv; 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 #endif
}
filename = argv[1]; filename = argv[1];
if (Py_VerboseFlag || if (Py_VerboseFlag ||
...@@ -355,10 +383,7 @@ Py_Main(argc, argv) ...@@ -355,10 +383,7 @@ Py_Main(argc, argv)
} }
} }
/* /* We initialize the menubar here, hoping SIOUX is initialized by now */
** 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.
*/
PyMac_InitMenuBar(); PyMac_InitMenuBar();
Py_Initialize(); Py_Initialize();
...@@ -377,10 +402,9 @@ Py_Main(argc, argv) ...@@ -377,10 +402,9 @@ Py_Main(argc, argv)
fp, filename == NULL ? "<stdin>" : filename) != 0; fp, filename == NULL ? "<stdin>" : filename) != 0;
if (filename != NULL) if (filename != NULL)
fclose(fp); fclose(fp);
if (inspect && isatty((int)fileno(stdin)) && if ( filename != NULL || command != NULL )
(filename != NULL || command != NULL)) sts = (run_inspect() || sts);
sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
Py_Exit(sts); Py_Exit(sts);
/*NOTREACHED*/ /*NOTREACHED*/
...@@ -396,9 +420,9 @@ PyMac_Exit(status) ...@@ -396,9 +420,9 @@ PyMac_Exit(status)
int keep; int keep;
if ( status ) if ( status )
keep = keep_error; keep = options.keep_error;
else else
keep = keep_normal; keep = options.keep_normal;
#ifdef USE_SIOUX #ifdef USE_SIOUX
if (keep) { if (keep) {
...@@ -423,12 +447,13 @@ PyMac_Exit(status) ...@@ -423,12 +447,13 @@ PyMac_Exit(status)
exit(status); exit(status);
} }
#ifdef OBSOLETE_ARGCARGV
/* Return the program name -- some code out there needs this. */ /* Return the program name -- some code out there needs this. */
char * char *
Py_GetProgramName() Py_GetProgramName()
{ {
return argv0; return orig_argv[0];
} }
...@@ -443,6 +468,7 @@ Py_GetArgcArgv(argc,argv) ...@@ -443,6 +468,7 @@ Py_GetArgcArgv(argc,argv)
*argc = orig_argc; *argc = orig_argc;
*argv = orig_argv; *argv = orig_argv;
} }
#endif /* OBSOLETE_ARGCARGV */
/* More cruft that shouldn't really be here, used in sysmodule.c */ /* More cruft that shouldn't really be here, used in sysmodule.c */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment