]> granicus.if.org Git - python/commitdiff
- Put all options in a struct
authorJack Jansen <jack.jansen@cwi.nl>
Sat, 7 Sep 1996 17:09:31 +0000 (17:09 +0000)
committerJack Jansen <jack.jansen@cwi.nl>
Sat, 7 Sep 1996 17:09:31 +0000 (17:09 +0000)
- Unified initialization code for interpreter and applet
- Implemented new options to skip AE-processing for argc/argv and for
  disabling interactive option setting

Mac/Include/pythonresources.h
Mac/Python/macgetargv.c
Mac/Python/macgetpath.c
Mac/Python/macglue.c
Mac/Python/macmain.c

index 02607bd39b5036cacf13a6efbf36c31318843e93..514eb40b2451739bc25cf33d7cc9b6efd0f35a0d 100644 (file)
 #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 *));
 
  
index a06b100f8ba1b6ddf7c65c1ac7eaf6bcfe82f600..e0ecb8eeaae15a64632a84c422eda3b597970016 100644 (file)
@@ -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;
        
index 5c2cafda1d9ed5651f1be0c04cba90f1e281feac..466f073c6b79e38bac0aa49f74c5a32b24e6b03c 100644 (file)
@@ -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);
 
index 17bbff00ede1f7dc574b9e8ee9a846c8811c5550..3df1a8a4d9b74583e11e4da2b00eb4e113253daf 100644 (file)
@@ -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
index 095673e0f3c1f8562403213d6a79d68eeb5d409f..b4648726dd1a301e8401062290bf6549f15205a5 100644 (file)
@@ -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 */