]> granicus.if.org Git - nethack/commitdiff
allow a deferred windowport init during options processing
authornhmall <nhmall@nethack.org>
Mon, 10 Dec 2018 05:44:18 +0000 (00:44 -0500)
committernhmall <nhmall@nethack.org>
Mon, 10 Dec 2018 05:44:18 +0000 (00:44 -0500)
When options processing encountered OPTIONS=windowtype:X,
the code would immediately attempt to switch over to that
windowtype right in the midst of options processing.

This stores the chosen option into
chosen_window[WINTYPELEN]
thus allowing the startup code to choose it after
options processing has been completed.

include/flag.h
src/options.c
sys/winnt/windmain.c

index a459c5bb99c0b04898040ef5b0f7ed080ea66a4d..6a8ce3da2b5826ed75edea5a64fce767fbb38b29 100644 (file)
@@ -221,6 +221,10 @@ enum getloc_filters {
     NUM_GFILTER
 };
 
+/* max size of a windowtype option */
+#define WINTYPELEN 16
+char chosen_windowtype[WINTYPELEN];
+
 struct debug_flags {
     boolean test;
 #ifdef TTY_GRAPHICS
@@ -445,6 +449,9 @@ struct instance_flags {
     short soko_prize_type2;     /* amulet of reflection */
     struct debug_flags debug;
     boolean windowtype_locked;  /* windowtype can't change from configfile */
+    boolean windowtype_deferred; /* pick a windowport and store it in
+                                    chosen_windowport[], but do not switch to
+                                    it in the midst of options processing */
 };
 
 /*
index 9dafa31bf7e0db7160d316b4d32a3606fb75bc2b..922acce124404e8ca6ac29e4b0c7ecc1f2ec325b 100644 (file)
@@ -20,7 +20,6 @@ NEARDATA struct instance_flags iflags; /* provide linkage */
 #endif
 
 #define BACKWARD_COMPAT
-#define WINTYPELEN 16
 
 #ifdef DEFAULT_WC_TILED_MAP
 #define PREFER_TILED TRUE
@@ -3519,10 +3518,14 @@ boolean tinitial, tfrom_file;
             bad_negation(fullname, FALSE);
             return FALSE;
         } else if ((op = string_for_env_opt(fullname, opts, FALSE)) != 0) {
-            char buf[WINTYPELEN];
+            if (!iflags.windowtype_deferred) {
+                char buf[WINTYPELEN];
 
-            nmcpy(buf, op, WINTYPELEN);
-            choose_windows(buf);
+                nmcpy(buf, op, WINTYPELEN);
+                choose_windows(buf);
+            } else {
+                nmcpy(chosen_windowtype, op, WINTYPELEN);
+           }
         } else
             return FALSE;
         return retval;
index 6b4b0491c35b2097885c6f6d9d1f7f5fc5e70a72..d8b7c805699efec629048de7e07611a8fd87b143 100644 (file)
@@ -22,6 +22,7 @@ char *NDECL(exename);
 boolean NDECL(fakeconsole);
 void NDECL(freefakeconsole);
 E void FDECL(nethack_exit, (int));
+E char chosen_windowtype[WINTYPELEN];   /* flag.h */
 #if defined(MSWIN_GRAPHICS)
 E void NDECL(mswin_destroy_reg);
 #endif
@@ -36,6 +37,7 @@ void FDECL(windows_getlin, (const char *, char *));
 extern int NDECL(windows_console_custom_nhgetch);
 
 char orgdir[PATHLEN];
+char *dir;
 boolean getreturn_enabled;
 extern int redirect_stdout;       /* from sys/share/pcsys.c */
 extern int GUILaunched;
@@ -60,8 +62,8 @@ int argc;
 char *argv[];
 {
     boolean resuming = FALSE; /* assume new game */
-    register int fd;
-    register char *dir;
+    int fd;
+    char *windowtype = NULL;
     char *envp = NULL;
     char *sptr = NULL;
     char fnamebuf[BUFSZ], encodedfnamebuf[BUFSZ];
@@ -205,8 +207,10 @@ _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);*/
     if (GUILaunched || IsDebuggerPresent()) {
         getreturn_enabled = TRUE;
     }
+
     check_recordfile((char *) 0);
-    initoptions();
+    iflags.windowtype_deferred = TRUE;
+    initoptions();                  
     if (!validate_prefix_locations(failbuf)) {
         raw_printf("Some invalid directory locations were specified:\n\t%s\n",
                    failbuf);
@@ -214,119 +218,18 @@ _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);*/
     }
     if (!hackdir[0])
         Strcpy(hackdir, orgdir);
-    if (argc > 1) {
-        if (argcheck(argc, argv, ARG_VERSION) == 2)
-            nethack_exit(EXIT_SUCCESS);
-
-        if (argcheck(argc, argv, ARG_DEBUG) == 1) {
-            argc--;
-            argv++;
-       }
-       if (argcheck(argc, argv, ARG_WINDOWS) == 1) {
-           argc--;
-           argv++;
-       }
-        if (argc > 1 && !strncmp(argv[1], "-d", 2) && argv[1][2] != 'e') {
-            /* avoid matching "-dec" for DECgraphics; since the man page
-             * says -d directory, hope nobody's using -desomething_else
-             */
-            argc--;
-            argv++;
-            dir = argv[0] + 2;
-            if (*dir == '=' || *dir == ':')
-                dir++;
-            if (!*dir && argc > 1) {
-                argc--;
-                argv++;
-                dir = argv[0];
-            }
-            if (!*dir)
-                error("Flag -d must be followed by a directory name.");
-            Strcpy(hackdir, dir);
-        }
-        if (argc > 1) {
-#if 0
-#if !defined(TTY_GRAPHICS)
-            int sfd = 0;
-            boolean tmpconsole = FALSE;
-            hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
-#endif
-#endif /* 0 */
-
-            /*
-             * Now we know the directory containing 'record' and
-             * may do a prscore().
-             */
-            if (!strncmp(argv[1], "-s", 2)) {
-#if 0
-#if !defined(TTY_GRAPHICS)
-                /*
-                 * Check to see if we're redirecting to a file.
-                 */
-                sfd = (int) _fileno(stdout);
-                redirect_stdout = (sfd >= 0) ? !isatty(sfd) : 0;
-
-                if (!redirect_stdout && !hStdOut) {
-                    raw_printf(
-                        "-s is not supported for the Graphical Interface\n");
-                    nethack_exit(EXIT_SUCCESS);
-                }
-#endif
-#endif /* 0 */
-#ifdef SYSCF
-                initoptions();
-#endif
-                prscore(argc, argv);
-
-#if 0
-#if !defined(TTY_GRAPHICS)
-                if (tmpconsole) {
-                    getreturn("to exit");
-                    freefakeconsole();
-                    tmpconsole = FALSE;
-                }
-#endif
-#endif /* 0 */
-                nethack_exit(EXIT_SUCCESS);
-            }
-            if (GUILaunched) {
-                if (!strncmpi(argv[1], "-clearreg", 6)) { /* clear registry */
-                    mswin_destroy_reg();
-                    nethack_exit(EXIT_SUCCESS);
-                }
-            }
-            /* Don't initialize the full window system just to print usage */
-            if (!strncmp(argv[1], "-?", 2) || !strncmp(argv[1], "/?", 2)) {
-                nhusage();
-#if 0
-#if !defined(TTY_GRAPHICS)
-                if (tmpconsole) {
-                    getreturn("to exit");
-                    freefakeconsole();
-                    tmpconsole = FALSE;
-                }
-#endif
-#endif
-                nethack_exit(EXIT_SUCCESS);
-            }
-        }
-    }
-
+    process_options(argc, argv);
+    
 /*
  * It seems you really want to play.
  */
 
-    /* In 3.6.0, several ports process options before they init
-     * the window port. This allows settings that impact window
-     * ports to be specified or read from the sys or user config files.
-     */
-    process_options(argc, argv);
-
     if (argc >= 1
         && !strcmpi(default_window_sys, "mswin")
         && (strstri(argv[0], "nethackw.exe") || GUILaunched))
             iflags.windowtype_locked = TRUE;
 
+    windowtype = default_window_sys;
     if (!iflags.windowtype_locked) {
 #if defined(TTY_GRAPHICS)
         Strcpy(default_window_sys, "tty");
@@ -335,9 +238,11 @@ _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);*/
         Strcpy(default_window_sys, "curses");    
 #endif /* CURSES */
 #endif /* TTY */
+        if (iflags.windowtype_deferred && chosen_windowtype[0])
+            windowtype = chosen_windowtype;
     }
+    choose_windows(windowtype);
 
-    choose_windows(default_window_sys);
     if (!dlb_init()) {
         pline(
             "%s\n%s\n%s\n%s\n\nNetHack was unable to open the required file "
@@ -459,6 +364,63 @@ char *argv[];
     /*
      * Process options.
      */
+    if (argc > 1) {
+        if (argcheck(argc, argv, ARG_VERSION) == 2)
+            nethack_exit(EXIT_SUCCESS);
+
+        if (argcheck(argc, argv, ARG_DEBUG) == 1) {
+            argc--;
+            argv++;
+       }
+       if (argcheck(argc, argv, ARG_WINDOWS) == 1) {
+           argc--;
+           argv++;
+       }
+        if (argc > 1 && !strncmp(argv[1], "-d", 2) && argv[1][2] != 'e') {
+            /* avoid matching "-dec" for DECgraphics; since the man page
+             * says -d directory, hope nobody's using -desomething_else
+             */
+            argc--;
+            argv++;
+            dir = argv[0] + 2;
+            if (*dir == '=' || *dir == ':')
+                dir++;
+            if (!*dir && argc > 1) {
+                argc--;
+                argv++;
+                dir = argv[0];
+            }
+            if (!*dir)
+                error("Flag -d must be followed by a directory name.");
+            Strcpy(hackdir, dir);
+        }
+
+        if (argc > 1) {
+            /*
+             * Now we know the directory containing 'record' and
+             * may do a prscore().
+             */
+            if (!strncmp(argv[1], "-s", 2)) {
+#ifdef SYSCF
+                initoptions();
+#endif
+                prscore(argc, argv);
+
+                nethack_exit(EXIT_SUCCESS);
+            }
+            if (GUILaunched) {
+                if (!strncmpi(argv[1], "-clearreg", 6)) { /* clear registry */
+                    mswin_destroy_reg();
+                    nethack_exit(EXIT_SUCCESS);
+                }
+            }
+            /* Don't initialize the full window system just to print usage */
+            if (!strncmp(argv[1], "-?", 2) || !strncmp(argv[1], "/?", 2)) {
+                nhusage();
+                nethack_exit(EXIT_SUCCESS);
+            }
+        }
+    }
     while (argc > 1 && argv[1][0] == '-') {
         argv++;
         argc--;
@@ -528,18 +490,12 @@ char *argv[];
                     flags.initrace = i;
             }
             break;
-#if 0
         case 'w': /* windowtype */
-#if defined(TTY_GRAPHICS)
-            if (!strncmpi(&argv[0][2], "tty", 3)) {
-                nttty_open(1);
-            }
-#endif
             config_error_init(FALSE, "command line", FALSE);
-            choose_windows(&argv[0][2]);
+            if (strlen(&argv[0][2]) < (WINTYPELEN - 1))
+                Strcpy(chosen_windowtype, &argv[0][2]);
             config_error_done();
             break;
-#endif
         case '@':
             flags.randomall = 1;
             break;