]> granicus.if.org Git - nethack/commitdiff
duplicate options parsing for menu keys
authorPatR <rankin@nethack.org>
Wed, 10 Mar 2021 21:17:19 +0000 (13:17 -0800)
committerPatR <rankin@nethack.org>
Wed, 10 Mar 2021 21:17:19 +0000 (13:17 -0800)
I'm sure that this could be improved but it is better than before....

src/invent.c
src/options.c

index 2da82e7a390f3664afdc4bf372fba2a5a5b2b14e..e1426e80320f90ed54d12cf60cfd22466671e82c 100644 (file)
@@ -5,10 +5,6 @@
 
 #include "hack.h"
 
-#ifndef C /* same as cmd.c */
-#define C(c) (0x1f & (c))
-#endif
-
 #define NOINVSYM '#'
 #define CONTAINED_SYM '>' /* designator for inside a container */
 #define HANDS_SYM '-'
index 9d62633b3d5c86ca8acf0fc72dff42b339873f52..2dc6a2acf727e363c8f35d823e008408a577eb7e 100644 (file)
@@ -241,7 +241,8 @@ static void complain_about_duplicate(int);
 static int length_without_val(const char *, int len);
 static void determine_ambiguities(void);
 static int check_misc_menu_command(char *, char *);
-int spcfn_misc_menu_cmd(int, int, boolean, char *, char *);
+static int shared_menu_optfn(int, int, boolean, char *, char *);
+static int spcfn_misc_menu_cmd(int, int, boolean, char *, char *);
 
 static const char *attr2attrname(int);
 static void basic_menu_colors(boolean);
@@ -1459,9 +1460,11 @@ optfn_map_mode(int optidx, int req, boolean negated, char *opts, char *op)
     return optn_ok;
 }
 
+/* all the key assignment options for menu_* commands are identical
+   but optlist.h treats them as distinct rather than sharing one */
 static int
-optfn_menu_deselect_all(int optidx UNUSED, int req, boolean negated UNUSED,
-                        char *opts, char *op UNUSED)
+shared_menu_optfn(int optidx UNUSED, int req, boolean negated UNUSED,
+                   char *opts, char *op)
 {
     if (req == do_init) {
         return optn_ok;
@@ -1483,264 +1486,113 @@ optfn_menu_deselect_all(int optidx UNUSED, int req, boolean negated UNUSED,
 }
 
 static int
-optfn_menu_deselect_page(int optidx UNUSED, int req, boolean negated UNUSED,
-                         char *opts, char *op UNUSED)
+optfn_menu_deselect_all(int optidx, int req, boolean negated,
+                        char *opts, char *op)
 {
-    if (req == do_init) {
-        return optn_ok;
-    }
-    if (req == do_set) {
-        int res = check_misc_menu_command(opts, op);
-
-        if (res < 0)
-            return optn_err;
-        return spcfn_misc_menu_cmd(res, req, negated, opts, op);
-    }
-    if (req == get_val) {
-        if (!opts)
-            return optn_err;
-        Sprintf(opts, "%s", to_be_done);
-        return optn_ok;
-    }
-    return optn_ok;
+    return shared_menu_optfn(optidx, req, negated, opts, op);
 }
 
 static int
-optfn_menu_first_page(int optidx UNUSED, int req, boolean negated UNUSED,
-                      char *opts, char *op UNUSED)
+optfn_menu_deselect_page(int optidx, int req, boolean negated,
+                         char *opts, char *op)
 {
-    if (req == do_init) {
-        return optn_ok;
-    }
-    if (req == do_set) {
-        int res = check_misc_menu_command(opts, op);
-
-        if (res < 0)
-            return optn_err;
-        return spcfn_misc_menu_cmd(res, req, negated, opts, op);
-    }
-    if (req == get_val) {
-        if (!opts)
-            return optn_err;
-        Sprintf(opts, "%s", to_be_done);
-        return optn_ok;
-    }
-    return optn_ok;
+    return shared_menu_optfn(optidx, req, negated, opts, op);
 }
 
 static int
-optfn_menu_headings(int optidx, int req, boolean negated UNUSED,
-                    char *opts, char *op UNUSED)
+optfn_menu_first_page(int optidx, int req, boolean negated,
+                      char *opts, char *op)
 {
-    int tmpattr;
-
-    if (req == do_init) {
-        return optn_ok;
-    }
-    if (req == do_set) {
-        if ((opts = string_for_env_opt(allopt[optidx].name, opts, FALSE))
-            == empty_optstr) {
-            return optn_err;
-        }
-        tmpattr = match_str2attr(opts, TRUE);
-        if (tmpattr == -1)
-            return optn_err;
-        iflags.menu_headings = tmpattr;
-        return optn_ok;
-    }
-    if (req == get_val) {
-        if (!opts)
-            return optn_err;
-        Sprintf(opts, "%s", attr2attrname(iflags.menu_headings));
-        return optn_ok;
-    }
-    if (req == do_handler) {
-        return handler_menu_headings();
-    }
-    return optn_ok;
+    return shared_menu_optfn(optidx, req, negated, opts, op);
 }
 
 static int
-optfn_menu_invert_all(int optidx UNUSED, int req, boolean negated UNUSED,
-                      char *opts, char *op UNUSED)
+optfn_menu_invert_all(int optidx, int req, boolean negated,
+                      char *opts, char *op)
 {
-    if (req == do_init) {
-        return optn_ok;
-    }
-    if (req == do_set) {
-        int res = check_misc_menu_command(opts, op);
-
-        if (res < 0)
-            return optn_err;
-        return spcfn_misc_menu_cmd(res, req, negated, opts, op);
-    }
-    if (req == get_val) {
-        if (!opts)
-            return optn_err;
-        Sprintf(opts, "%s", to_be_done);
-        return optn_ok;
-    }
-    return optn_ok;
+    return shared_menu_optfn(optidx, req, negated, opts, op);
 }
 
 static int
-optfn_menu_invert_page(int optidx UNUSED, int req, boolean negated UNUSED,
-                       char *opts, char *op UNUSED)
+optfn_menu_invert_page(int optidx, int req, boolean negated,
+                       char *opts, char *op)
 {
-    if (req == do_init) {
-        return optn_ok;
-    }
-    if (req == do_set) {
-        int res = check_misc_menu_command(opts, op);
-
-        if (res < 0)
-            return optn_err;
-        return spcfn_misc_menu_cmd(res, req, negated, opts, op);
-    }
-    if (req == get_val) {
-        if (!opts)
-            return optn_err;
-        Sprintf(opts, "%s", to_be_done);
-        return optn_ok;
-    }
-    return optn_ok;
+    return shared_menu_optfn(optidx, req, negated, opts, op);
 }
 
 static int
-optfn_menu_last_page(int optidx UNUSED, int req, boolean negated UNUSED,
-                     char *opts, char *op UNUSED)
+optfn_menu_last_page(int optidx, int req, boolean negated,
+                     char *opts, char *op)
 {
-    if (req == do_init) {
-        return optn_ok;
-    }
-    if (req == do_set) {
-        int res = check_misc_menu_command(opts, op);
-
-        if (res < 0)
-            return optn_err;
-        return spcfn_misc_menu_cmd(res, req, negated, opts, op);
-    }
-    if (req == get_val) {
-        if (!opts)
-            return optn_err;
-        Sprintf(opts, "%s", to_be_done);
-        return optn_ok;
-    }
-    return optn_ok;
+    return shared_menu_optfn(optidx, req, negated, opts, op);
 }
 
 static int
-optfn_menu_next_page(int optidx UNUSED, int req, boolean negated UNUSED,
-                     char *opts, char *op UNUSED)
+optfn_menu_next_page(int optidx , int req, boolean negated,
+                     char *opts, char *op)
 {
-    if (req == do_init) {
-        return optn_ok;
-    }
-    if (req == do_set) {
-        int res = check_misc_menu_command(opts, op);
-
-        if (res < 0)
-            return optn_err;
-        return spcfn_misc_menu_cmd(res, req, negated, opts, op);
-    }
-    if (req == get_val) {
-        if (!opts)
-            return optn_err;
-        Sprintf(opts, "%s", to_be_done);
-        return optn_ok;
-    }
-    return optn_ok;
+    return shared_menu_optfn(optidx, req, negated, opts, op);
 }
 
 static int
-optfn_menu_previous_page(int optidx UNUSED, int req, boolean negated UNUSED,
-                         char *opts, char *op UNUSED)
+optfn_menu_previous_page(int optidx, int req, boolean negated,
+                         char *opts, char *op)
 {
-    if (req == do_init) {
-        return optn_ok;
-    }
-    if (req == do_set) {
-        int res = check_misc_menu_command(opts, op);
-
-        if (res < 0)
-            return optn_err;
-        return spcfn_misc_menu_cmd(res, req, negated, opts, op);
-    }
-    if (req == get_val) {
-        if (!opts)
-            return optn_err;
-        Sprintf(opts, "%s", to_be_done);
-        return optn_ok;
-    }
-    return optn_ok;
+    return shared_menu_optfn(optidx, req, negated, opts, op);
 }
 
 static int
-optfn_menu_search(int optidx UNUSED, int req, boolean negated UNUSED,
-                  char *opts, char *op UNUSED)
+optfn_menu_search(int optidx, int req, boolean negated,
+                  char *opts, char *op)
 {
-    if (req == do_init) {
-        return optn_ok;
-    }
-    if (req == do_set) {
-        int res = check_misc_menu_command(opts, op);
-
-        if (res < 0)
-            return optn_err;
-        return spcfn_misc_menu_cmd(res, req, negated, opts, op);
-    }
-    if (req == get_val) {
-        if (!opts)
-            return optn_err;
-        Sprintf(opts, "%s", to_be_done);
-        return optn_ok;
-    }
-    return optn_ok;
+    return shared_menu_optfn(optidx, req, negated, opts, op);
 }
 
 static int
-optfn_menu_select_all(int optidx UNUSED, int req, boolean negated UNUSED,
-                      char *opts, char *op UNUSED)
+optfn_menu_select_all(int optidx, int req, boolean negated,
+                      char *opts, char *op)
 {
-    if (req == do_init) {
-        return optn_ok;
-    }
-    if (req == do_set) {
-        int res = check_misc_menu_command(opts, op);
+    return shared_menu_optfn(optidx, req, negated, opts, op);
+}
 
-        if (res < 0)
-            return optn_err;
-        return spcfn_misc_menu_cmd(res, req, negated, opts, op);
-    }
-    if (req == get_val) {
-        if (!opts)
-            return optn_err;
-        Sprintf(opts, "%s", to_be_done);
-        return optn_ok;
-    }
-    return optn_ok;
+static int
+optfn_menu_select_page(int optidx, int req, boolean negated,
+                       char *opts, char *op)
+{
+    return shared_menu_optfn(optidx, req, negated, opts, op);
 }
 
+/* end of shared key assignments for menu commands */
+
 static int
-optfn_menu_select_page(int optidx UNUSED, int req, boolean negated UNUSED,
-                       char *opts, char *op UNUSED)
+optfn_menu_headings(int optidx, int req, boolean negated UNUSED,
+                    char *opts, char *op UNUSED)
 {
+    int tmpattr;
+
     if (req == do_init) {
         return optn_ok;
     }
     if (req == do_set) {
-        int res = check_misc_menu_command(opts, op);
-
-        if (res < 0)
+        if ((opts = string_for_env_opt(allopt[optidx].name, opts, FALSE))
+            == empty_optstr) {
             return optn_err;
-        return spcfn_misc_menu_cmd(res, req, negated, opts, op);
+        }
+        tmpattr = match_str2attr(opts, TRUE);
+        if (tmpattr == -1)
+            return optn_err;
+        iflags.menu_headings = tmpattr;
+        return optn_ok;
     }
     if (req == get_val) {
         if (!opts)
             return optn_err;
-        Sprintf(opts, "%s", to_be_done);
+        Sprintf(opts, "%s", attr2attrname(iflags.menu_headings));
         return optn_ok;
     }
+    if (req == do_handler) {
+        return handler_menu_headings();
+    }
     return optn_ok;
 }
 
@@ -4296,7 +4148,7 @@ optfn_boolean(int optidx, int req, boolean negated, char *opts, char *op)
     return optn_ok;
 }
 
-int
+static int
 spcfn_misc_menu_cmd(int midx, int req, boolean negated, char *opts, char *op)
 {
     if (req == do_init) {