gui: Improve check_key_map_args().
authorAndre Noll <maan@systemlinux.org>
Tue, 7 Jan 2014 18:52:26 +0000 (18:52 +0000)
committerAndre Noll <maan@systemlinux.org>
Sun, 4 May 2014 13:48:54 +0000 (15:48 +0200)
Simplify the code a bit, make it call die() on errors and print
the invalid key map in the error case.

gui.c

diff --git a/gui.c b/gui.c
index 8ec009c..ead7520 100644 (file)
--- a/gui.c
+++ b/gui.c
@@ -824,36 +824,31 @@ err_out:
        return 0;
 }
 
-static int check_key_map_args(void)
+static void check_key_map_args_or_die(void)
 {
-       char *s;
-       int i, ret = -1;
-       char *tmp = NULL, *handler, *arg;
+       int i;
+       char *tmp = NULL;
 
        for (i = 0; i < conf.key_map_given; ++i) {
-               s = conf.key_map_arg[i];
-               if (!(*s))
-                       goto out;
+               char *handler, *arg;
+
                free(tmp);
-               tmp = para_strdup(s);
+               tmp = para_strdup(conf.key_map_arg[i]);
                if (!split_key_map(tmp, &handler, &arg))
-                       goto out;
+                       break;
                if (strlen(handler) != 1)
-                       goto out;
-               if (*handler != 'x'
-                       && *handler != 'd'
-                       && *handler != 'i'
-                       && *handler != 'p')
-                       goto out;
+                       break;
+               if (*handler != 'x' && *handler != 'd' && *handler != 'i'
+                               && *handler != 'p')
+                       break;
                if (*handler != 'i')
                        continue;
                if (find_cmd_byname(arg) < 0)
-                       goto out;
+                       break;
        }
-       ret = 0;
-out:
+       if (i != conf.key_map_given)
+               die(EXIT_FAILURE, "invalid key map: %s\n", conf.key_map_arg[i]);
        free(tmp);
-       return ret;
 }
 
 /*
@@ -1286,8 +1281,7 @@ static void com_reread_conf(void)
        gui_cmdline_parser_config_file(cf, &conf, &params);
        init_curses();
        PARA_NOTICE_LOG("config file reloaded\n");
-       if (check_key_map_args() < 0)
-               die(EXIT_FAILURE, "invalid key map\n");
+       check_key_map_args_or_die();
 }
 
 static void com_help(void)
@@ -1453,8 +1447,7 @@ int main(int argc, char *argv[])
                gui_cmdline_parser_config_file(cf, &conf, &params);
                loglevel = get_loglevel_by_name(conf.loglevel_arg);
        }
-       if (check_key_map_args() < 0)
-               die(EXIT_FAILURE, "invalid key map\n");
+       check_key_map_args_or_die();
        theme_init(conf.theme_arg, &theme);
        setup_signal_handling();
        bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);