/** Allow a config option to be added to rc_handle from inside a program * * @param rh a handle to parsed configuration. * @param option_name the name of the option. * @param option_val the value to be added. * @param source typically should be %__FILE__ or %__func__ for logging purposes. * @param line %__LINE__ for logging purposes. * @return 0 on success, -1 on failure. */ int rc_add_config(rc_handle *rh, char const *option_name, char const *option_val, char const *source, int line) { OPTION *option; if ((option = find_option(rh, option_name, OT_ANY)) == NULL) { rc_log(LOG_ERR, "ERROR: unrecognized option: %s", option_name); return -1; } if (option->status != ST_UNDEF) { rc_log(LOG_ERR, "ERROR: duplicate option: %s", option_name); return -1; } switch (option->type) { case OT_STR: if (set_option_str(source, line, option, option_val) < 0) { return -1; } break; case OT_INT: if (set_option_int(source, line, option, option_val) < 0) { return -1; } break; case OT_SRV: if (set_option_srv(source, line, option, option_val) < 0) { return -1; } break; case OT_AUO: if (set_option_auo(source, line, option, option_val) < 0) { return -1; } break; default: rc_log(LOG_CRIT, "rc_add_config: impossible case branch!"); abort(); } if (strcmp(option->name, "bindaddr") == 0) { memset(&rh->own_bind_addr, 0, sizeof(rh->own_bind_addr)); rh->own_bind_addr_set = 0; rc_own_bind_addr(rh, &rh->own_bind_addr); rh->own_bind_addr_set = 1; } return 0; }
int rc_add_config(rc_handle *rh, const char *option_name, const char *option_val, const char *source, const int line) { OPTION *option; if ((option = find_option(rh, option_name, OT_ANY)) == NULL) { rc_log(LOG_ERR, "ERROR: unrecognized option: %s", option_name); return -1; } if (option->status != ST_UNDEF) { rc_log(LOG_ERR, "ERROR: duplicate option: %s", option_name); return -1; } switch (option->type) { case OT_STR: if (set_option_str(source, line, option, option_val) < 0) { return -1; } break; case OT_INT: if (set_option_int(source, line, option, option_val) < 0) { return -1; } break; case OT_SRV: if (set_option_srv(source, line, option, option_val) < 0) { return -1; } break; case OT_AUO: if (set_option_auo(source, line, option, option_val) < 0) { return -1; } break; default: rc_log(LOG_CRIT, "rc_read_config: impossible case branch!"); abort(); } return 0; }
int rc_read_config(char *filename) { FILE *configfd; char buffer[512], *p; OPTION *option; int line, pos; if ((configfd = fopen(filename,"r")) == NULL) { errorlog("rc_read_config: can't open %s: %m", filename); return (-1); } line = 0; while ((fgets(buffer, sizeof(buffer), configfd) != NULL)) { line++; p = buffer; if ((*p == '\n') || (*p == '#') || (*p == '\0')) continue; p[strlen(p)-1] = '\0'; if ((pos = strcspn(p, "\t ")) == 0) { errorlog("%s: line %d: bogus format: %s", filename, line, p); return (-1); } p[pos] = '\0'; if ((option = find_option(p, OT_ANY)) == NULL) { errorlog("%s: line %d: unrecognized keyword: %s", filename, line, p); return (-1); } if (option->status != ST_UNDEF) { errorlog("%s: line %d: duplicate option line: %s", filename, line, p); return (-1); } p += pos+1; while (isspace(*p)) p++; switch (option->type) { case OT_STR: if (set_option_str(filename, line, option, p) < 0) return (-1); break; case OT_INT: if (set_option_int(filename, line, option, p) < 0) return (-1); break; case OT_SRV: if (set_option_srv(filename, line, option, p) < 0) return (-1); break; case OT_AUO: if (set_option_auo(filename, line, option, p) < 0) return (-1); break; default: fatallog("rc_read_config: impossible case branch!"); abort(); } } fclose(configfd); return test_config(filename); }
rc_handle * rc_read_config(const char *filename) { FILE *configfd; char buffer[512], *p; OPTION *option; int line; size_t pos; rc_handle *rh; rh = rc_new(); if (rh == NULL) return NULL; rh->config_options = malloc(sizeof(config_options_default)); if (rh->config_options == NULL) { rc_log(LOG_CRIT, "rc_read_config: out of memory"); rc_destroy(rh); return NULL; } memcpy(rh->config_options, &config_options_default, sizeof(config_options_default)); if ((configfd = fopen(filename,"r")) == NULL) { rc_log(LOG_ERR,"rc_read_config: can't open %s: %s", filename, strerror(errno)); rc_destroy(rh); return NULL; } line = 0; while ((fgets(buffer, sizeof(buffer), configfd) != NULL)) { line++; p = buffer; if ((*p == '\n') || (*p == '#') || (*p == '\0')) continue; p[strlen(p)-1] = '\0'; if ((pos = strcspn(p, "\t ")) == 0) { rc_log(LOG_ERR, "%s: line %d: bogus format: %s", filename, line, p); fclose(configfd); rc_destroy(rh); return NULL; } p[pos] = '\0'; if ((option = find_option(rh, p, OT_ANY)) == NULL) { rc_log(LOG_ERR, "%s: line %d: unrecognized keyword: %s", filename, line, p); fclose(configfd); rc_destroy(rh); return NULL; } if (option->status != ST_UNDEF) { rc_log(LOG_ERR, "%s: line %d: duplicate option line: %s", filename, line, p); fclose(configfd); rc_destroy(rh); return NULL; } p += pos+1; while (isspace(*p)) p++; pos = strlen(p) - 1; while(pos >= 0 && isspace(p[pos])) pos--; p[pos + 1] = '\0'; switch (option->type) { case OT_STR: if (set_option_str(filename, line, option, p) < 0) { fclose(configfd); rc_destroy(rh); return NULL; } break; case OT_INT: if (set_option_int(filename, line, option, p) < 0) { fclose(configfd); rc_destroy(rh); return NULL; } break; case OT_SRV: if (set_option_srv(filename, line, option, p) < 0) { fclose(configfd); rc_destroy(rh); return NULL; } break; case OT_AUO: if (set_option_auo(filename, line, option, p) < 0) { fclose(configfd); rc_destroy(rh); return NULL; } break; default: rc_log(LOG_CRIT, "rc_read_config: impossible case branch!"); abort(); } } fclose(configfd); if (test_config(rh, filename) == -1) { rc_destroy(rh); return NULL; } return rh; }