gui: Unify config file parsing.
authorAndre Noll <maan@systemlinux.org>
Tue, 7 Jan 2014 22:04:21 +0000 (22:04 +0000)
committerAndre Noll <maan@systemlinux.org>
Sun, 4 May 2014 13:48:54 +0000 (15:48 +0200)
The gui config file is parsed on startup, when the reload command
is executed or when SIGUSR1 is received. In all cases we set up a
gengetopt params structure and pass its address to the gengetopt
command line parser.

This commit moves the common code to the new parse_config_file_or_die()
which does the right thing for both the startup and the reload
case. The new function also contains the logic of configfile_exists(),
so this latter function can be removed.

gui.c

diff --git a/gui.c b/gui.c
index 9cf9506..e53d5c7 100644 (file)
--- a/gui.c
+++ b/gui.c
@@ -51,6 +51,7 @@ static pid_t cmd_pid;
 static int command_fds[2] = {-1, -1};
 static int stat_pipe = -1;
 static struct gui_args_info conf;
+static int loglevel;
 
 enum {GETCH_MODE, COMMAND_MODE, EXTERNAL_MODE};
 
@@ -287,24 +288,6 @@ static char *km_keyname(int c)
        return buf;
 }
 
-static char *configfile_exists(void)
-{
-       static char *config_file;
-       char *tmp;
-
-       if (!conf.config_file_given) {
-               if (!config_file) {
-                       char *home = para_homedir();
-                       config_file = make_message("%s/.paraslash/gui.conf",
-                               home);
-                       free(home);
-               }
-               tmp = config_file;
-       } else
-               tmp = conf.config_file_arg;
-       return file_exists(tmp)? tmp: NULL;
-}
-
 /* Print given number of spaces to curses window. */
 static void add_spaces(WINDOW* win, unsigned int num)
 {
@@ -534,8 +517,6 @@ static int add_output_line(char *line, void *data)
        return 1;
 }
 
-static int loglevel;
-
 static __printf_2_3 void curses_log(int ll, const char *fmt,...)
 {
        va_list ap;
@@ -872,6 +853,46 @@ static void check_key_map_args_or_die(void)
        free(tmp);
 }
 
+static void parse_config_file_or_die(bool override)
+{
+       bool err;
+       char *config_file;
+       struct gui_cmdline_parser_params params = {
+               .override = override,
+               .initialize = 0,
+               .check_required = !override,
+               .check_ambiguity = 0,
+               .print_errors = 1,
+       };
+
+       if (conf.config_file_given)
+               config_file = para_strdup(conf.config_file_arg);
+       else {
+               char *home = para_homedir();
+               config_file = make_message("%s/.paraslash/gui.conf", home);
+               free(home);
+       }
+       if (!file_exists(config_file)) {
+               if (!conf.config_file_given)
+                       err = false;
+               else {
+                       PARA_EMERG_LOG("config file %s does not exist\n",
+                               config_file);
+                       err = true;
+               }
+               goto out;
+       }
+       gui_cmdline_parser_config_file(config_file, &conf, &params);
+       loglevel = get_loglevel_by_name(conf.loglevel_arg);
+       check_key_map_args_or_die();
+       theme_init(conf.theme_arg, &theme);
+       err = false;
+out:
+       free(config_file);
+       if (err)
+               exit(EXIT_FAILURE);
+}
+
 /*
  * React to various signal-related events
  */
@@ -1280,29 +1301,14 @@ static void com_ll_incr(void)
  */
 static void com_reread_conf(void)
 {
-       char *cf = configfile_exists();
-       struct gui_cmdline_parser_params params = {
-               .override = 1,
-               .initialize = 1,
-               .check_required = 0,
-               .check_ambiguity = 0,
-               .print_errors = 0,
-       };
-
-       if (!cf) {
-               PARA_WARNING_LOG("there is no configuration to read\n");
-               return;
-       }
-       PARA_INFO_LOG("rereading command line options and config file\n");
        /*
-        * Despite .print_errors is set to 0, gengetopt will print to stderr
-        * anyway, and exit on errors. So we have to shutdown curses first.
+        * gengetopt might print to stderr and exit on errors. So we have to
+        * shutdown curses first.
         */
        shutdown_curses();
-       gui_cmdline_parser_config_file(cf, &conf, &params);
+       parse_config_file_or_die(true /* override */);
        init_curses();
-       PARA_NOTICE_LOG("config file reloaded\n");
-       check_key_map_args_or_die();
+       print_in_bar(COLOR_MSG, "config file reloaded\n");
 }
 
 static void com_help(void)
@@ -1448,30 +1454,13 @@ __noreturn static void print_help_and_die(void)
 int main(int argc, char *argv[])
 {
        int ret;
-       char *cf;
 
        gui_cmdline_parser(argc, argv, &conf); /* exits on errors */
        loglevel = get_loglevel_by_name(conf.loglevel_arg);
        version_handle_flag("gui", conf.version_given);
        if (conf.help_given || conf.detailed_help_given)
                print_help_and_die();
-       cf = configfile_exists();
-       if (!cf && conf.config_file_given)
-               die(EXIT_FAILURE, "can not read config file %s\n",
-                       conf.config_file_arg);
-       if (cf) {
-               struct gui_cmdline_parser_params params = {
-                       .override = 0,
-                       .initialize = 0,
-                       .check_required = 0,
-                       .check_ambiguity = 0,
-                       .print_errors = 1,
-               };
-               gui_cmdline_parser_config_file(cf, &conf, &params);
-               loglevel = get_loglevel_by_name(conf.loglevel_arg);
-       }
-       check_key_map_args_or_die();
-       theme_init(conf.theme_arg, &theme);
+       parse_config_file_or_die(false /* override */);
        setup_signal_handling();
        bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
        setlocale(LC_CTYPE, "");