int settings_reread(const char *fname) { CONFIG_REC *tempconfig; char *str; str = fname == NULL ? NULL : convert_home(fname); tempconfig = parse_configfile(str); g_free_not_null(str); if (tempconfig == NULL) { signal_emit("gui dialog", 2, "error", g_strerror(errno)); return FALSE; } if (config_last_error(tempconfig) != NULL) { str = g_strdup_printf("Errors in configuration file:\n%s", config_last_error(tempconfig)); signal_emit("gui dialog", 2, "error", str); g_free(str); config_close(tempconfig); return FALSE; } config_close(mainconfig); mainconfig = tempconfig; config_last_modifycounter = mainconfig->modifycounter; signal_emit("setup changed", 0); signal_emit("setup reread", 1, mainconfig->fname); return TRUE; }
int settings_reread(const char *fname) { CONFIG_REC *tempconfig; char *str; if (fname == NULL) fname = "~/.irssi/config"; str = convert_home(fname); tempconfig = parse_configfile(str); g_free(str); if (tempconfig == NULL) { signal_emit("gui dialog", 2, "error", g_strerror(errno)); return FALSE; } if (config_last_error(tempconfig) != NULL) { str = g_strdup_printf(_("Errors in configuration file:\n%s"), config_last_error(tempconfig)); signal_emit("gui dialog", 2, "error", str); g_free(str); config_close(tempconfig); return FALSE; } config_close(mainconfig); mainconfig = tempconfig; signal_emit("setup changed", 0); signal_emit("setup reread", 0); return TRUE; }
static void init_configfile(void) { struct stat statbuf; char *str; if (stat(get_irssi_dir(), &statbuf) != 0) { /* ~/.irssi not found, create it. */ if (g_mkdir_with_parents(get_irssi_dir(), 0700) != 0) { g_error("Couldn't create %s directory: %s", get_irssi_dir(), g_strerror(errno)); } } else if (!S_ISDIR(statbuf.st_mode)) { g_error("%s is not a directory.\n" "You should remove it with command: rm %s", get_irssi_dir(), get_irssi_dir()); } mainconfig = parse_configfile(NULL); config_last_modifycounter = mainconfig->modifycounter; /* any errors? */ if (config_last_error(mainconfig) != NULL) { str = g_strdup_printf("Ignored errors in configuration file:\n%s", config_last_error(mainconfig)); signal_emit("gui dialog", 2, "error", str); g_free(str); } signal(SIGTERM, sig_term); }
static void init_configfile(void) { struct stat statbuf; char *str; str = g_strdup_printf("%s/.irssi", g_get_home_dir()); if (stat(str, &statbuf) != 0) { /* ~/.irssi not found, create it. */ if (mkdir(str, 0700) != 0) { g_error(_("Couldn't create %s/.irssi directory"), g_get_home_dir()); } } else if (!S_ISDIR(statbuf.st_mode)) { g_error(_("%s/.irssi is not a directory.\n" "You should remove it with command: rm ~/.irssi"), g_get_home_dir()); } g_free(str); mainconfig = parse_configfile(NULL); /* any errors? */ if (config_last_error(mainconfig) != NULL) { last_error_msg = g_strdup_printf(_("Ignored errors in configuration " "file:\n%s"), config_last_error(mainconfig)); signal_add("irssi init finished", (SIGNAL_FUNC) sig_print_config_error); } signal(SIGTERM, sig_term); }
static int theme_read(THEME_REC *theme, const char *path, const char *data) { CONFIG_REC *config; THEME_READ_REC rec; char *str; 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 (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)); read_error(str); g_free(str); } theme->default_color = config_get_int(config, NULL, "default_color", -1); theme->info_eol = config_get_bool(config, NULL, "info_eol", FALSE); /* FIXME: remove after 0.7.99 */ if (theme->default_color == 0 && config_get_int(config, NULL, "default_real_color", -1) != -1) theme->default_color = -1; theme_read_replaces(config, theme); if (data == NULL) { /* get the default abstracts from default theme. */ CONFIG_REC *default_config; default_config = config_open(NULL, -1); config_parse_data(default_config, default_theme, "internal"); theme_read_abstracts(default_config, theme); config_close(default_config); } theme_read_abstracts(config, theme); rec.theme = theme; rec.config = config; g_hash_table_foreach(default_formats, (GHFunc) theme_read_modules, &rec); config_close(config); return TRUE; }
int settings_save(const char *fname) { char *str; if (config_write(mainconfig, fname, 0660) == 0) return TRUE; /* error */ str = g_strdup_printf(_("Couldn't save configuration file: %s"), config_last_error(mainconfig)); signal_emit("gui dialog", 2, "error", str); g_free(str); return FALSE; }
int settings_save(const char *fname, int autosave) { char *str; int error; if (fname == NULL) fname = mainconfig->fname; error = config_write(mainconfig, fname, 0660) != 0; irssi_config_save_state(fname); config_last_modifycounter = mainconfig->modifycounter; if (error) { str = g_strdup_printf("Couldn't save configuration file: %s", config_last_error(mainconfig)); signal_emit("gui dialog", 2, "error", str); g_free(str); } signal_emit("setup saved", 2, fname, GINT_TO_POINTER(autosave)); return !error; }
static void theme_save(THEME_REC *theme, int save_all) { CONFIG_REC *config; THEME_SAVE_REC data; char *path; int ok; config = config_open(theme->path, -1); if (config != NULL) config_parse(config); else { if (g_ascii_strcasecmp(theme->name, "default") == 0) { config = config_open(NULL, -1); config_parse_data(config, default_theme, "internal"); config_change_file_name(config, theme->path, 0660); } else { config = config_open(theme->path, 0660); if (config == NULL) return; config_parse(config); } } data.config = config; data.save_all = save_all; g_hash_table_foreach(theme->modules, (GHFunc) module_save, &data); /* always save the theme to ~/.irssi/ */ path = g_strdup_printf("%s/%s", get_irssi_dir(), g_basename(theme->path)); ok = config_write(config, path, 0660) == 0; printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, ok ? TXT_THEME_SAVED : TXT_THEME_SAVE_FAILED, path, config_last_error(config)); g_free(path); config_close(config); }
static bool setup_from_config(struct lwan *lwan, const char *path) { struct config *conf; struct config_line line; bool has_listener = false; char path_buf[PATH_MAX]; if (!path) path = lwan_get_config_path(path_buf, sizeof(path_buf)); lwan_status_info("Loading configuration file: %s", path); conf = config_open(path); if (!conf) return false; if (!lwan_trie_init(&lwan->url_map_trie, destroy_urlmap)) return false; while (config_read_line(conf, &line)) { switch (line.type) { case CONFIG_LINE_TYPE_LINE: if (streq(line.key, "keep_alive_timeout")) { lwan->config.keep_alive_timeout = (unsigned short)parse_long( line.value, default_config.keep_alive_timeout); } else if (streq(line.key, "quiet")) { lwan->config.quiet = parse_bool(line.value, default_config.quiet); } else if (streq(line.key, "reuse_port")) { lwan->config.reuse_port = parse_bool(line.value, default_config.reuse_port); } else if (streq(line.key, "proxy_protocol")) { lwan->config.proxy_protocol = parse_bool(line.value, default_config.proxy_protocol); } else if (streq(line.key, "allow_cors")) { lwan->config.allow_cors = parse_bool(line.value, default_config.allow_cors); } else if (streq(line.key, "expires")) { lwan->config.expires = parse_time_period(line.value, default_config.expires); } else if (streq(line.key, "error_template")) { free(lwan->config.error_template); lwan->config.error_template = strdup(line.value); } else if (streq(line.key, "threads")) { long n_threads = parse_long(line.value, default_config.n_threads); if (n_threads < 0) config_error(conf, "Invalid number of threads: %ld", n_threads); lwan->config.n_threads = (unsigned short int)n_threads; } else if (streq(line.key, "max_post_data_size")) { long max_post_data_size = parse_long( line.value, (long)default_config.max_post_data_size); if (max_post_data_size < 0) config_error(conf, "Negative maximum post data size"); else if (max_post_data_size > 128 * (1 << 20)) config_error(conf, "Maximum post data can't be over 128MiB"); lwan->config.max_post_data_size = (size_t)max_post_data_size; } else if (streq(line.key, "allow_temp_files")) { lwan->config.allow_post_temp_file = !!strstr(line.value, "post"); } else { config_error(conf, "Unknown config key: %s", line.key); } break; case CONFIG_LINE_TYPE_SECTION: if (streq(line.key, "listener")) { if (!has_listener) { parse_listener(conf, &line, lwan); has_listener = true; } else { config_error(conf, "Only one listener supported"); } } else if (streq(line.key, "straitjacket")) { lwan_straitjacket_enforce_from_config(conf); } else { config_error(conf, "Unknown section type: %s", line.key); } break; case CONFIG_LINE_TYPE_SECTION_END: config_error(conf, "Unexpected section end"); } } if (config_last_error(conf)) { lwan_status_critical("Error on config file \"%s\", line %d: %s", path, config_cur_line(conf), config_last_error(conf)); } config_close(conf); return true; }
static void parse_listener_prefix(struct config *c, struct config_line *l, struct lwan *lwan, const struct lwan_module *module, void *handler) { struct lwan_url_map url_map = {}; struct hash *hash = hash_str_new(free, free); char *prefix = strdupa(l->value); struct config *isolated; isolated = config_isolate_section(c, l); if (!isolated) { config_error(c, "Could not isolate configuration file"); goto out; } while (config_read_line(c, l)) { switch (l->type) { case CONFIG_LINE_TYPE_LINE: hash_add(hash, strdup(l->key), strdup(l->value)); break; case CONFIG_LINE_TYPE_SECTION: if (streq(l->key, "authorization")) { parse_listener_prefix_authorization(c, l, &url_map); } else if (!config_skip_section(c, l)) { config_error(c, "Could not skip section"); goto out; } break; case CONFIG_LINE_TYPE_SECTION_END: goto add_map; } } config_error(c, "Expecting section end while parsing prefix"); goto out; add_map: assert((handler && !module) || (!handler && module)); if (handler) { url_map.handler = handler; url_map.flags |= HANDLER_PARSE_MASK | HANDLER_DATA_IS_HASH_TABLE; url_map.data = hash; url_map.module = NULL; hash = NULL; } else if (module->create_from_hash && module->handle_request) { url_map.data = module->create_from_hash(prefix, hash); if (!url_map.data) { config_error(c, "Could not create module instance"); goto out; } if (module->parse_conf && !module->parse_conf(url_map.data, isolated)) { const char *msg = config_last_error(isolated); config_error(c, "Error from module: %s", msg ? msg : "Unknown"); goto out; } url_map.handler = module->handle_request; url_map.flags |= module->flags; url_map.module = module; } else if (UNLIKELY(!module->create_from_hash)) { config_error(c, "Module isn't prepared to load settings from a file; " "create_from_hash() method isn't present"); goto out; } else if (UNLIKELY(!module->handle_request)) { config_error(c, "Module does not have handle_request() method"); goto out; } add_url_map(&lwan->url_map_trie, prefix, &url_map); out: hash_free(hash); config_close(isolated); }
static void parse_listener_prefix(struct config *c, struct config_line *l, struct lwan *lwan, const struct lwan_module *module, void *handler) { struct lwan_url_map url_map = {}; struct hash *hash = hash_str_new(free, free); char *prefix = strdupa(l->value); struct config *isolated; isolated = config_isolate_section(c, l); if (!isolated) { config_error(c, "Could not isolate configuration file"); goto out; } while (config_read_line(c, l)) { switch (l->type) { case CONFIG_LINE_TYPE_LINE: if (streq(l->key, "module")) { if (module) { config_error(c, "Module already specified"); goto out; } module = find_module(l->value); if (!module) { config_error(c, "Could not find module \"%s\"", l->value); goto out; } } else if (streq(l->key, "handler")) { if (handler) { config_error(c, "Handler already specified"); goto out; } handler = find_handler(l->value); if (!handler) { config_error(c, "Could not find handler \"%s\"", l->value); goto out; } } else { hash_add(hash, strdup(l->key), strdup(l->value)); } break; case CONFIG_LINE_TYPE_SECTION: if (streq(l->key, "authorization")) { parse_listener_prefix_authorization(c, l, &url_map); } else { if (!config_skip_section(c, l)) { config_error(c, "Could not skip section"); goto out; } } break; case CONFIG_LINE_TYPE_SECTION_END: goto add_map; } } config_error(c, "Expecting section end while parsing prefix"); goto out; add_map: if (module == handler && !handler) { config_error(c, "Missing module or handler"); goto out; } if (module && handler) { config_error(c, "Handler and module are mutually exclusive"); goto out; } if (handler) { url_map.handler = handler; url_map.flags |= HANDLER_PARSE_MASK | HANDLER_DATA_IS_HASH_TABLE; url_map.data = hash; url_map.module = NULL; hash = NULL; } else if (module && module->create_from_hash && module->handle_request) { url_map.data = module->create_from_hash(prefix, hash); if (module->parse_conf && !module->parse_conf(url_map.data, isolated)) { const char *msg = config_last_error(isolated); config_error(c, "Error from module: %s", msg ? msg : "Unknown"); goto out; } url_map.handler = module->handle_request; url_map.flags |= module->flags; url_map.module = module; } else { config_error(c, "Invalid handler"); goto out; } add_url_map(&lwan->url_map_trie, prefix, &url_map); out: hash_free(hash); config_close(isolated); }