]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - gui.c
gui: Introduce status_post_select().
[paraslash.git] / gui.c
diff --git a/gui.c b/gui.c
index 9cf950650090cc263b8395c09088f7ad0e3178fd..318418ea1e7959ba2aeba35d7df7af01da779338 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;
@@ -640,7 +621,27 @@ print:
        return 1;
 }
 
-static int read_stat_pipe(fd_set *rfds)
+static void print_all_items(void)
+{
+       int i;
+
+       if (!curses_active())
+               return;
+       FOR_EACH_STATUS_ITEM(i)
+               print_stat_item(i);
+}
+
+static void clear_all_items(void)
+{
+       int i;
+
+       FOR_EACH_STATUS_ITEM(i) {
+               free(stat_content[i]);
+               stat_content[i] = para_strdup("");
+       }
+}
+
+static void status_post_select(fd_set *rfds)
 {
        static char *buf;
        static int bufsize, loaded;
@@ -648,11 +649,11 @@ static int read_stat_pipe(fd_set *rfds)
        size_t sz;
 
        if (stat_pipe < 0)
-               return 0;
+               return;
        if (loaded >= bufsize) {
                if (bufsize > 1000 * 1000) {
                        loaded = 0;
-                       return 0;
+                       return;
                }
                bufsize += bufsize + 1000;
                buf = para_realloc(buf, bufsize);
@@ -664,33 +665,20 @@ static int read_stat_pipe(fd_set *rfds)
        ret2 = for_each_stat_item(buf, loaded, update_item);
        if (ret < 0 || ret2 < 0) {
                loaded = 0;
-               return ret2 < 0? ret2 : ret;
+               PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret));
+               close(stat_pipe);
+               stat_pipe = -1;
+               clear_all_items();
+               free(stat_content[SI_BASENAME]);
+               stat_content[SI_BASENAME] =
+                       para_strdup("stat command terminated!?");
+               print_all_items();
+               return;
        }
        sz = ret2; /* what is left */
        if (sz > 0 && sz < loaded)
                memmove(buf, buf + loaded - sz, sz);
        loaded = sz;
-       return 1;
-}
-
-static void print_all_items(void)
-{
-       int i;
-
-       if (!curses_active())
-               return;
-       FOR_EACH_STATUS_ITEM(i)
-               print_stat_item(i);
-}
-
-static void clear_all_items(void)
-{
-       int i;
-
-       FOR_EACH_STATUS_ITEM(i) {
-               free(stat_content[i]);
-               stat_content[i] = para_strdup("");
-       }
 }
 
 /*
@@ -872,6 +860,59 @@ 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);
+}
+
+/* reread configuration, terminate on errors */
+static void reread_conf(void)
+{
+       /*
+        * gengetopt might print to stderr and exit on errors. So we have to
+        * shutdown curses first.
+        */
+       shutdown_curses();
+       parse_config_file_or_die(true /* override */);
+       init_curses();
+       print_in_bar(COLOR_MSG, "config file reloaded\n");
+}
+
 /*
  * React to various signal-related events
  */
@@ -896,7 +937,7 @@ static void handle_signal(int sig)
                return;
        case SIGUSR1:
                PARA_NOTICE_LOG("got SIGUSR1, rereading configuration\n");
-               com_reread_conf();
+               reread_conf();
                return;
        case SIGCHLD:
                check_sigchld();
@@ -1016,17 +1057,7 @@ repeat:
                        }
                }
        }
-       ret = read_stat_pipe(&rfds);
-       if (ret < 0) {
-               PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret));
-               close(stat_pipe);
-               stat_pipe = -1;
-               clear_all_items();
-               free(stat_content[SI_BASENAME]);
-               stat_content[SI_BASENAME] =
-                       para_strdup("stat command terminated!?");
-               print_all_items();
-       }
+       status_post_select(&rfds);
 check_return:
        switch (mode) {
        case COMMAND_MODE:
@@ -1275,34 +1306,9 @@ static void com_ll_incr(void)
        print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
 }
 
-/*
- * reread configuration, terminate on errors
- */
 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.
-        */
-       shutdown_curses();
-       gui_cmdline_parser_config_file(cf, &conf, &params);
-       init_curses();
-       PARA_NOTICE_LOG("config file reloaded\n");
-       check_key_map_args_or_die();
+       reread_conf();
 }
 
 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, "");