From 7a6c3f0b7c98c380e2225c358020704bdad73fa7 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Tue, 20 Feb 2001 01:54:14 +0000 Subject: [PATCH] Errors reading/writing config and theme files are now handled properly and printed to screen git-svn-id: http://svn.irssi.org/repos/irssi/trunk@1266 dbcabf3a-b0e7-0310-adc4-f8d773084564 --- src/core/settings.c | 51 ++++++++++++-------- src/fe-common/core/fe-log.c | 8 ++-- src/fe-common/core/themes.c | 92 ++++++++++++++++++++----------------- 3 files changed, 86 insertions(+), 65 deletions(-) diff --git a/src/core/settings.c b/src/core/settings.c index 065c70ea..1ddc6fa6 100644 --- a/src/core/settings.c +++ b/src/core/settings.c @@ -393,16 +393,19 @@ void sig_term(int n) would be nice but would just take more space without much real benefit */ static unsigned int file_checksum(const char *fname) { - FILE *f; - int n = 0; + char buf[512]; + int f, ret, n; unsigned int checksum = 0; - f = fopen(fname, "rb"); - if (f == NULL) return 0; + f = open(fname, O_RDONLY); + if (f == -1) return 0; - while (!feof(f)) - checksum += fgetc(f) << ((n++ & 3)*8); - fclose(f); + n = 0; + while ((ret = read(f, buf, sizeof(buf))) > 0) { + while (ret-- > 0) + checksum += buf[ret] << ((n++ & 3)*8); + } + close(f); return checksum; } @@ -439,31 +442,41 @@ int irssi_config_is_changed(const char *fname) static CONFIG_REC *parse_configfile(const char *fname) { CONFIG_REC *config; + struct stat statbuf; + const char *path; char *real_fname; real_fname = fname != NULL ? g_strdup(fname) : g_strdup_printf("%s"G_DIR_SEPARATOR_S".irssi" G_DIR_SEPARATOR_S"config", g_get_home_dir()); - config = config_open(real_fname, -1); - if (config != NULL) - config_parse(config); - else if (fname == NULL) { + if (stat(real_fname, &statbuf) == 0) + path = real_fname; + else { /* user configuration file not found, use the default one from sysconfdir */ - config = config_open(SYSCONFDIR"/irssi/config", -1); - if (config != NULL) - config_parse(config); - else { + path = SYSCONFDIR"/irssi/config"; + if (stat(path, &statbuf) != 0) { /* no configuration file in sysconfdir .. use the build-in configuration */ - config = config_open(NULL, -1); - config_parse_data(config, default_config, "internal"); + path = NULL; } - - config_change_file_name(config, real_fname, 0660); } + config = config_open(path, -1); + if (config == NULL) { + last_config_error_msg = + g_strdup_printf("Error opening configuration file %s: %s", + path, g_strerror(errno)); + config = config_open(NULL, -1); + } + + if (path != NULL) + config_parse(config); + else + config_parse_data(config, default_config, "internal"); + + config_change_file_name(config, real_fname, 0660); irssi_config_save_state(real_fname); g_free(real_fname); return config; diff --git a/src/fe-common/core/fe-log.c b/src/fe-common/core/fe-log.c index 4278f6a9..434e094e 100644 --- a/src/fe-common/core/fe-log.c +++ b/src/fe-common/core/fe-log.c @@ -69,7 +69,7 @@ static void cmd_log_open(const char *data) { SERVER_REC *server; GHashTable *optlist; - char *targetarg, *fname, *levels; + char *targetarg, *fname, *levels, *servertag; void *free_arg; char window[MAX_INT_STRLEN]; LOG_REC *log; @@ -86,15 +86,17 @@ static void cmd_log_open(const char *data) /* - */ server = cmd_options_get_server("join", optlist, NULL); + servertag = server == NULL ? NULL : server->tag; if (g_hash_table_lookup(optlist, "window")) { /* log by window ref# */ ltoa(window, active_win->refnum); - log_item_add(log, LOG_ITEM_WINDOW_REFNUM, window, server->tag); + log_item_add(log, LOG_ITEM_WINDOW_REFNUM, window, + servertag); } else { targetarg = g_hash_table_lookup(optlist, "targets"); if (targetarg != NULL && *targetarg != '\0') - log_add_targets(log, targetarg, server->tag); + log_add_targets(log, targetarg, servertag); } if (g_hash_table_lookup(optlist, "autoopen")) diff --git a/src/fe-common/core/themes.c b/src/fe-common/core/themes.c index cf91e95a..317b3bf5 100644 --- a/src/fe-common/core/themes.c +++ b/src/fe-common/core/themes.c @@ -40,7 +40,7 @@ GHashTable *default_formats; static int init_finished; static char *init_errors; -static void theme_read(THEME_REC *theme, const char *path, const char *data); +static int theme_read(THEME_REC *theme, const char *path, const char *data); THEME_REC *theme_create(const char *path, const char *name) { @@ -631,10 +631,9 @@ static void window_themes_update(void) THEME_REC *theme_load(const char *setname) { - THEME_REC *theme; + THEME_REC *theme, *oldtheme; struct stat statbuf; char *fname, *name, *p; - int modified; name = g_strdup(setname); p = strrchr(name, '.'); @@ -659,24 +658,24 @@ THEME_REC *theme_load(const char *setname) } } - modified = theme != NULL; - if (theme != NULL) { - if (theme->last_modify == statbuf.st_mtime) { - /* theme not modified, use the one already in memory */ - g_free(fname); - g_free(name); - return theme; - } - - theme_destroy(theme); + if (theme != NULL && theme->last_modify == statbuf.st_mtime) { + /* theme not modified, use the one already in memory */ + g_free(fname); + g_free(name); + return theme; } + oldtheme = theme; theme = theme_create(fname, name); theme->last_modify = statbuf.st_mtime; - theme_read(theme, theme->path, NULL); + if (!theme_read(theme, theme->path, NULL)) { + /* error reading .theme file */ + theme_destroy(theme); + theme = NULL; + } - if (modified) { - /* make sure no windows use the theme we just destroyed */ + if (oldtheme != NULL && theme != NULL) { + theme_destroy(oldtheme); window_themes_update(); } @@ -696,42 +695,47 @@ static void theme_read_modules(const char *module, void *value, theme_init_module(rec->theme, module, rec->config); } -static void theme_read(THEME_REC *theme, const char *path, const char *data) +static void read_error(const char *str) +{ + char *old; + + if (init_finished) + printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "%s", str); + else if (init_errors == NULL) + init_errors = g_strdup(str); + else { + old = init_errors; + init_errors = g_strconcat(init_errors, "\n", str, NULL); + g_free(old); + } +} + +static int theme_read(THEME_REC *theme, const char *path, const char *data) { CONFIG_REC *config; THEME_READ_REC rec; + char *str; - if (data == NULL) { - config = config_open(path, -1); - if (config == NULL) { - /* didn't exist or no access? */ - theme->default_color = 0; - theme->default_bold_color = 7; - return; - } - config_parse(config); - } else { - config = config_open(NULL, -1); - config_parse_data(config, data, "internal"); + config = config_open(data == NULL ? path : NULL, -1) ; + if (config == NULL) { + /* didn't exist or no access? */ + str = g_strdup_printf("Error reading theme file %s: %s", + path, g_strerror(errno)); + read_error(str); + g_free(str); + return FALSE; } - if (config_last_error(config) != NULL) { - char *str; + if (data != NULL) + config_parse_data(config, data, "internal"); + else + config_parse(config); + if (config_last_error(config) != NULL) { str = g_strdup_printf(_("Ignored errors in theme %s:\n%s"), theme->name, config_last_error(config)); - if (!init_finished) { - if (init_errors == NULL) - init_errors = str; - else { - init_errors = g_strconcat(init_errors, "\n", - str, NULL); - g_free(str); - } - } else { - signal_emit("gui dialog", 2, "error", str); - g_free(str); - } + read_error(str); + g_free(str); } theme->default_color = @@ -746,6 +750,8 @@ static void theme_read(THEME_REC *theme, const char *path, const char *data) g_hash_table_foreach(default_formats, (GHFunc) theme_read_modules, &rec); config_close(config); + + return TRUE; } typedef struct {