X-Git-Url: http://git.tuebingen.mpg.de/?a=blobdiff_plain;f=gui.c;h=a20750f7f300800b7255b32a50120a0526b1ff58;hb=4207df0cbc1fcaa2ddb76febf82f25a23a0fb62d;hp=9cf950650090cc263b8395c09088f7ad0e3178fd;hpb=bf6e508841396f203b9b47552029a90f8709969e;p=paraslash.git diff --git a/gui.c b/gui.c index 9cf95065..a20750f7 100644 --- 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,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, ¶ms); + 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 +930,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(); @@ -1275,34 +1309,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, ¶ms); - init_curses(); - PARA_NOTICE_LOG("config file reloaded\n"); - check_key_map_args_or_die(); + reread_conf(); } static void com_help(void) @@ -1448,30 +1457,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, ¶ms); - 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, "");