]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - gui.c
gui: Make curses_log() work also when curses is not active.
[paraslash.git] / gui.c
diff --git a/gui.c b/gui.c
index 96043da05b5f9b602e229f131b5248c7f43d6190..6e3c3e1edf36549917a17bd58511692d8bed7d8b 100644 (file)
--- a/gui.c
+++ b/gui.c
@@ -84,9 +84,6 @@ struct gui_command {
 
 static struct gui_theme theme;
 
-static int _argc;
-static char **_argv;
-
 static void com_help(void);
 static void com_reread_conf(void);
 static void com_enlarge_top_win(void);
@@ -386,7 +383,6 @@ static int first_visible_rbe(unsigned *lines)
                int rbe_lines;
                if (!rbe)
                        return i - 1;
-//             fprintf(stderr, "found: %s\n", rbe->msg);
                rbe_lines = NUM_LINES(rbe->len);
                if (rbe_lines > bot.lines)
                        return -1;
@@ -470,7 +466,6 @@ static void rb_add_entry(int color, char *msg)
        new->len = len;
        new->msg = msg;
        old = ringbuffer_add(bot_win_rb, new);
-//     fprintf(stderr, "added: %s\n", new->msg);
        if (old) {
                free(old->msg);
                free(old);
@@ -519,29 +514,22 @@ static int loglevel;
 
 static __printf_2_3 void curses_log(int ll, const char *fmt,...)
 {
-       int color;
-       char *msg;
        va_list ap;
-       unsigned bytes;
 
-       if (ll < loglevel || !curses_active())
+       if (ll < loglevel)
                return;
-       switch (ll) {
-               case LL_DEBUG:
-               case LL_INFO:
-               case LL_NOTICE:
-                       color = COLOR_MSG;
-                       break;
-               default:
-                       color = COLOR_ERRMSG;
-       }
        va_start(ap, fmt);
-       bytes = xvasprintf(&msg, fmt, ap);
+       if (curses_active()) {
+               int color = ll <= LL_NOTICE? COLOR_MSG : COLOR_ERRMSG;
+               char *msg;
+               unsigned bytes = xvasprintf(&msg, fmt, ap);
+               if (bytes > 0 && msg[bytes - 1] == '\n')
+                       msg[bytes - 1] = '\0'; /* cut trailing newline */
+               rb_add_entry(color, msg);
+               wrefresh(bot.win);
+       } else if (cmd_pid <= 0) /* no external command running */
+               vfprintf(stderr, fmt, ap);
        va_end(ap);
-       if (bytes > 0 && msg[bytes - 1] == '\n')
-               msg[bytes - 1] = '\0'; /* cut trailing newline */
-       rb_add_entry(color, msg);
-       wrefresh(bot.win);
 }
 __printf_2_3 void (*para_log)(int, const char*, ...) = curses_log;
 
@@ -1250,7 +1238,6 @@ static void com_scroll_up(void)
                        break;
                rbe_lines = NUM_LINES(rbe->len);
                lines += rbe_lines;
-//             fprintf(stderr, "msg: %s\n", rbe->msg);
                wattron(bot.win, COLOR_PAIR(rbe->color));
                waddstr(bot.win, "\n");
                waddstr(bot.win, rbe->msg);
@@ -1306,7 +1293,6 @@ static void com_reread_conf(void)
                return;
        }
        PARA_INFO_LOG("rereading command line options and config file\n");
-       gui_cmdline_parser_ext(_argc, _argv, &conf, &params);
        /*
         * Despite .print_errors is set to 0, gengetopt will print to stderr
         * anyway, and exit on errors. So we have to shutdown curses first.
@@ -1462,20 +1448,15 @@ int main(int argc, char *argv[])
        int ret;
        char *cf;
 
-       _argc = argc;
-       _argv = argv;
-
        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) {
-               fprintf(stderr, "can not read config file %s\n",
+       if (!cf && conf.config_file_given)
+               msg_n_exit(EXIT_FAILURE, "can not read config file %s\n",
                        conf.config_file_arg);
-               exit(EXIT_FAILURE);
-       }
        if (cf) {
                struct gui_cmdline_parser_params params = {
                        .override = 0,
@@ -1487,10 +1468,8 @@ 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) {
-               fprintf(stderr, "invalid key map\n");
-               exit(EXIT_FAILURE);
-       }
+       if (check_key_map_args() < 0)
+               msg_n_exit(EXIT_FAILURE, "invalid key map\n");
        theme_init(conf.theme_arg, &theme);
        setup_signal_handling();
        bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);