int32_t init_config(void) { FILE *fp = open_config_file_or_die(cs_conf); const struct config_sections *cur_section = oscam_conf; // Global char *token; if (!cs_malloc(&token, MAXLINESIZE)) return 1; config_sections_set_defaults(oscam_conf, &cfg); int line = 0; int valid_section = 1; while (fgets(token, MAXLINESIZE, fp)) { ++line; int len = strlen(trim(token)); if (len < 3) // a=b or [a] are at least 3 chars continue; if (token[0] == '#') // Skip comments continue; if (token[0] == '[' && token[len - 1] == ']') { token[len - 1] = '\0'; valid_section = 0; const struct config_sections *newconf = config_find_section(oscam_conf, token + 1); if (config_section_is_active(newconf) && cur_section) { config_list_apply_fixups(cur_section->config, &cfg); cur_section = newconf; valid_section = 1; } if (!newconf) { fprintf(stderr, "WARNING: %s line %d unknown section [%s].\n", cs_conf, line, token + 1); continue; } if (!config_section_is_active(newconf)) { fprintf(stderr, "WARNING: %s line %d section [%s] is ignored (support not compiled in).\n", cs_conf, line, newconf->section); } continue; } if (!valid_section) continue; char *value = strchr(token, '='); if (!value) // No = found, well go on continue; *value++ ='\0'; char *tvalue = trim(value); char *ttoken = trim(strtolower(token)); if (cur_section && !config_list_parse(cur_section->config, ttoken, tvalue, &cfg)) { fprintf(stderr, "WARNING: %s line %d section [%s] contains unknown setting '%s=%s'\n", cs_conf, line, cur_section->section, ttoken, tvalue); } } free(token); fclose(fp); if (cur_section) config_list_apply_fixups(cur_section->config, &cfg); return 0; }
void config_sections_set_defaults(const struct config_sections *conf, void *var) { const struct config_sections *sec; for (sec = conf; sec && sec->section; sec++) { if (config_section_is_active(sec)) config_list_set_defaults(sec->config, var); } }
void config_sections_free(const struct config_sections *conf, void *var) { const struct config_sections *sec; for (sec = conf; sec && sec->section; sec++) { if (config_section_is_active(sec)) { config_list_free_values(sec->config, var); } } }
void config_sections_save(const struct config_sections *conf, FILE *f, void *var) { const struct config_sections *sec; for (sec = conf; sec && sec->section; sec++) { if (config_section_is_active(sec) && config_list_should_be_saved(sec->config, var)) { fprintf(f, "[%s]\n", sec->section); config_list_apply_fixups(sec->config, var); config_list_save(f, sec->config, var, cfg.http_full_cfg); fprintf(f, "\n"); } } }
void config_set_value(const struct config_sections *conf, char *section, const char *token, char *value, void *var) { const struct config_sections *sec = config_find_section(conf, section); if (!sec) { fprintf(stderr, "WARNING: Unknown section '%s'.\n", section); return; } if (config_section_is_active(sec)) { if (!config_list_parse(sec->config, token, value, var)) { fprintf(stderr, "WARNING: In section [%s] unknown setting '%s=%s' tried.\n", section, token, value); } } else { fprintf(stderr, "WARNING: Section is not active '%s'.\n", section); } }
int32_t init_config(void) { FILE *fp; if(config_enabled(WEBIF)) { fp = open_config_file(cs_conf); } else { fp = open_config_file_or_die(cs_conf); } const struct config_sections *cur_section = oscam_conf; // Global char *token; config_sections_set_defaults(oscam_conf, &cfg); if(!fp) { // no oscam.conf but webif is included in build, set it up for lan access and tweak defaults #ifdef WEBIF cfg.http_port = DEFAULT_HTTP_PORT; chk_iprange(cs_strdup(DEFAULT_HTTP_ALLOW), &cfg.http_allowed); #endif NULLFREE(cfg.logfile); cfg.logtostdout = 1; #ifdef HAVE_DVBAPI cfg.dvbapi_enabled = 1; #endif return 0; } if(!cs_malloc(&token, MAXLINESIZE)) { return 1; } int line = 0; int valid_section = 1; while(fgets(token, MAXLINESIZE, fp)) { ++line; int len = strlen(trim(token)); if(len < 3) // a=b or [a] are at least 3 chars { continue; } if(token[0] == '#') // Skip comments { continue; } if(token[0] == '[' && token[len - 1] == ']') { token[len - 1] = '\0'; valid_section = 0; const struct config_sections *newconf = config_find_section(oscam_conf, token + 1); if(config_section_is_active(newconf) && cur_section) { config_list_apply_fixups(cur_section->config, &cfg); cur_section = newconf; valid_section = 1; } if(!newconf) { fprintf(stderr, "WARNING: %s line %d unknown section [%s].\n", cs_conf, line, token + 1); continue; } if(!config_section_is_active(newconf)) { fprintf(stderr, "WARNING: %s line %d section [%s] is ignored (support not compiled in).\n", cs_conf, line, newconf->section); } continue; } if(!valid_section) { continue; } char *value = strchr(token, '='); if(!value) // No = found, well go on { continue; } *value++ = '\0'; char *tvalue = trim(value); char *ttoken = trim(strtolower(token)); if(cur_section && !config_list_parse(cur_section->config, ttoken, tvalue, &cfg)) { fprintf(stderr, "WARNING: %s line %d section [%s] contains unknown setting '%s=%s'\n", cs_conf, line, cur_section->section, ttoken, tvalue); } } free(token); fclose(fp); if(cur_section) { config_list_apply_fixups(cur_section->config, &cfg); } return 0; }