mirror of
https://github.com/irssi/irssi.git
synced 2025-02-02 15:08:01 -05:00
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
This commit is contained in:
parent
25845074d6
commit
7a6c3f0b7c
@ -393,16 +393,19 @@ void sig_term(int n)
|
|||||||
would be nice but would just take more space without much real benefit */
|
would be nice but would just take more space without much real benefit */
|
||||||
static unsigned int file_checksum(const char *fname)
|
static unsigned int file_checksum(const char *fname)
|
||||||
{
|
{
|
||||||
FILE *f;
|
char buf[512];
|
||||||
int n = 0;
|
int f, ret, n;
|
||||||
unsigned int checksum = 0;
|
unsigned int checksum = 0;
|
||||||
|
|
||||||
f = fopen(fname, "rb");
|
f = open(fname, O_RDONLY);
|
||||||
if (f == NULL) return 0;
|
if (f == -1) return 0;
|
||||||
|
|
||||||
while (!feof(f))
|
n = 0;
|
||||||
checksum += fgetc(f) << ((n++ & 3)*8);
|
while ((ret = read(f, buf, sizeof(buf))) > 0) {
|
||||||
fclose(f);
|
while (ret-- > 0)
|
||||||
|
checksum += buf[ret] << ((n++ & 3)*8);
|
||||||
|
}
|
||||||
|
close(f);
|
||||||
return checksum;
|
return checksum;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -439,31 +442,41 @@ int irssi_config_is_changed(const char *fname)
|
|||||||
static CONFIG_REC *parse_configfile(const char *fname)
|
static CONFIG_REC *parse_configfile(const char *fname)
|
||||||
{
|
{
|
||||||
CONFIG_REC *config;
|
CONFIG_REC *config;
|
||||||
|
struct stat statbuf;
|
||||||
|
const char *path;
|
||||||
char *real_fname;
|
char *real_fname;
|
||||||
|
|
||||||
real_fname = fname != NULL ? g_strdup(fname) :
|
real_fname = fname != NULL ? g_strdup(fname) :
|
||||||
g_strdup_printf("%s"G_DIR_SEPARATOR_S".irssi"
|
g_strdup_printf("%s"G_DIR_SEPARATOR_S".irssi"
|
||||||
G_DIR_SEPARATOR_S"config", g_get_home_dir());
|
G_DIR_SEPARATOR_S"config", g_get_home_dir());
|
||||||
config = config_open(real_fname, -1);
|
|
||||||
|
|
||||||
if (config != NULL)
|
if (stat(real_fname, &statbuf) == 0)
|
||||||
config_parse(config);
|
path = real_fname;
|
||||||
else if (fname == NULL) {
|
else {
|
||||||
/* user configuration file not found, use the default one
|
/* user configuration file not found, use the default one
|
||||||
from sysconfdir */
|
from sysconfdir */
|
||||||
config = config_open(SYSCONFDIR"/irssi/config", -1);
|
path = SYSCONFDIR"/irssi/config";
|
||||||
if (config != NULL)
|
if (stat(path, &statbuf) != 0) {
|
||||||
config_parse(config);
|
|
||||||
else {
|
|
||||||
/* no configuration file in sysconfdir ..
|
/* no configuration file in sysconfdir ..
|
||||||
use the build-in configuration */
|
use the build-in configuration */
|
||||||
config = config_open(NULL, -1);
|
path = NULL;
|
||||||
config_parse_data(config, default_config, "internal");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
irssi_config_save_state(real_fname);
|
||||||
g_free(real_fname);
|
g_free(real_fname);
|
||||||
return config;
|
return config;
|
||||||
|
@ -69,7 +69,7 @@ static void cmd_log_open(const char *data)
|
|||||||
{
|
{
|
||||||
SERVER_REC *server;
|
SERVER_REC *server;
|
||||||
GHashTable *optlist;
|
GHashTable *optlist;
|
||||||
char *targetarg, *fname, *levels;
|
char *targetarg, *fname, *levels, *servertag;
|
||||||
void *free_arg;
|
void *free_arg;
|
||||||
char window[MAX_INT_STRLEN];
|
char window[MAX_INT_STRLEN];
|
||||||
LOG_REC *log;
|
LOG_REC *log;
|
||||||
@ -86,15 +86,17 @@ static void cmd_log_open(const char *data)
|
|||||||
|
|
||||||
/* -<server tag> */
|
/* -<server tag> */
|
||||||
server = cmd_options_get_server("join", optlist, NULL);
|
server = cmd_options_get_server("join", optlist, NULL);
|
||||||
|
servertag = server == NULL ? NULL : server->tag;
|
||||||
|
|
||||||
if (g_hash_table_lookup(optlist, "window")) {
|
if (g_hash_table_lookup(optlist, "window")) {
|
||||||
/* log by window ref# */
|
/* log by window ref# */
|
||||||
ltoa(window, active_win->refnum);
|
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 {
|
} else {
|
||||||
targetarg = g_hash_table_lookup(optlist, "targets");
|
targetarg = g_hash_table_lookup(optlist, "targets");
|
||||||
if (targetarg != NULL && *targetarg != '\0')
|
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"))
|
if (g_hash_table_lookup(optlist, "autoopen"))
|
||||||
|
@ -40,7 +40,7 @@ GHashTable *default_formats;
|
|||||||
static int init_finished;
|
static int init_finished;
|
||||||
static char *init_errors;
|
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)
|
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_load(const char *setname)
|
||||||
{
|
{
|
||||||
THEME_REC *theme;
|
THEME_REC *theme, *oldtheme;
|
||||||
struct stat statbuf;
|
struct stat statbuf;
|
||||||
char *fname, *name, *p;
|
char *fname, *name, *p;
|
||||||
int modified;
|
|
||||||
|
|
||||||
name = g_strdup(setname);
|
name = g_strdup(setname);
|
||||||
p = strrchr(name, '.');
|
p = strrchr(name, '.');
|
||||||
@ -659,24 +658,24 @@ THEME_REC *theme_load(const char *setname)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
modified = theme != NULL;
|
if (theme != NULL && theme->last_modify == statbuf.st_mtime) {
|
||||||
if (theme != NULL) {
|
/* theme not modified, use the one already in memory */
|
||||||
if (theme->last_modify == statbuf.st_mtime) {
|
g_free(fname);
|
||||||
/* theme not modified, use the one already in memory */
|
g_free(name);
|
||||||
g_free(fname);
|
return theme;
|
||||||
g_free(name);
|
|
||||||
return theme;
|
|
||||||
}
|
|
||||||
|
|
||||||
theme_destroy(theme);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
oldtheme = theme;
|
||||||
theme = theme_create(fname, name);
|
theme = theme_create(fname, name);
|
||||||
theme->last_modify = statbuf.st_mtime;
|
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) {
|
if (oldtheme != NULL && theme != NULL) {
|
||||||
/* make sure no windows use the theme we just destroyed */
|
theme_destroy(oldtheme);
|
||||||
window_themes_update();
|
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);
|
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;
|
CONFIG_REC *config;
|
||||||
THEME_READ_REC rec;
|
THEME_READ_REC rec;
|
||||||
|
char *str;
|
||||||
|
|
||||||
if (data == NULL) {
|
config = config_open(data == NULL ? path : NULL, -1) ;
|
||||||
config = config_open(path, -1);
|
if (config == NULL) {
|
||||||
if (config == NULL) {
|
/* didn't exist or no access? */
|
||||||
/* didn't exist or no access? */
|
str = g_strdup_printf("Error reading theme file %s: %s",
|
||||||
theme->default_color = 0;
|
path, g_strerror(errno));
|
||||||
theme->default_bold_color = 7;
|
read_error(str);
|
||||||
return;
|
g_free(str);
|
||||||
}
|
return FALSE;
|
||||||
config_parse(config);
|
|
||||||
} else {
|
|
||||||
config = config_open(NULL, -1);
|
|
||||||
config_parse_data(config, data, "internal");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config_last_error(config) != NULL) {
|
if (data != NULL)
|
||||||
char *str;
|
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"),
|
str = g_strdup_printf(_("Ignored errors in theme %s:\n%s"),
|
||||||
theme->name, config_last_error(config));
|
theme->name, config_last_error(config));
|
||||||
if (!init_finished) {
|
read_error(str);
|
||||||
if (init_errors == NULL)
|
g_free(str);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
theme->default_color =
|
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,
|
g_hash_table_foreach(default_formats,
|
||||||
(GHFunc) theme_read_modules, &rec);
|
(GHFunc) theme_read_modules, &rec);
|
||||||
config_close(config);
|
config_close(config);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user