char *config_get(const char *section, const char *name, const char *default_value) { struct config_value *cv; debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value); struct config *co = config_find_section(section); if(!co) co = config_create(section); cv = config_value_index_find(co, name, 0); if(!cv) { cv = config_value_create(co, name, default_value); if(!cv) return NULL; } cv->flags |= CONFIG_VALUE_USED; if((cv->flags & CONFIG_VALUE_LOADED) || (cv->flags & CONFIG_VALUE_CHANGED)) { // this is a loaded value from the config file // if it is different that the default, mark it if(!(cv->flags & CONFIG_VALUE_CHECKED)) { if(strcmp(cv->value, default_value) != 0) cv->flags |= CONFIG_VALUE_CHANGED; cv->flags |= CONFIG_VALUE_CHECKED; } } return(cv->value); }
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_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); } }
void config_add(struct config* conf, const char* section, const char* key, const char* val) { struct configsection* sect; if((sect=config_find_section(conf, section))) { struct configitem* item; if((item=config_find_item(conf, key, section))) { if(item->val) free(item->val); if(val) item->val=strdup(val); else item->val=NULL; } else item_add(sect, key, val); } else { section_new(conf, section); config_add(conf, section, key, val); } }
struct configitem* config_find_item(struct config* haystack, const char* needle, const char* section) { if(section) { struct configsection* sect; if((sect=config_find_section(haystack, section))) { int item=item_find(sect, needle); if(item != -1) return sect->item[item]; } } else { for(unsigned int i=0; i<haystack->sections; ++i) { int item=item_find(haystack->section[i], needle); if(item != -1) return haystack->section[i]->item[item]; } } return NULL; }
const char *config_set(const char *section, const char *name, const char *value) { struct config_value *cv; debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value); struct config *co = config_find_section(section); if(!co) co = config_create(section); cv = config_value_index_find(co, name, 0); if(!cv) cv = config_value_create(co, name, value); cv->flags |= CONFIG_VALUE_USED; if(strcmp(cv->value, value) != 0) { cv->flags |= CONFIG_VALUE_CHANGED; free(cv->value); cv->value = strdup(value); if(!cv->value) fatal("Cannot allocate config.value"); } return value; }
int load_config(char *filename, int overwrite_used) { int line = 0; struct config *co = NULL; pthread_rwlock_wrlock(&config_rwlock); char buffer[CONFIG_FILE_LINE_MAX + 1], *s; if(!filename) filename = CONFIG_DIR "/" CONFIG_FILENAME; FILE *fp = fopen(filename, "r"); if(!fp) { error("Cannot open file '%s'", CONFIG_DIR "/" CONFIG_FILENAME); pthread_rwlock_unlock(&config_rwlock); return 0; } while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) { buffer[CONFIG_FILE_LINE_MAX] = '\0'; line++; s = trim(buffer); if(!s) { debug(D_CONFIG, "Ignoring line %d, it is empty.", line); continue; } int len = strlen(s); if(*s == '[' && s[len - 1] == ']') { // new section s[len - 1] = '\0'; s++; co = config_find_section(s); if(!co) co = config_create(s); continue; } if(!co) { // line outside a section error("Ignoring line %d ('%s'), it is outsize all sections.", line, s); continue; } char *name = s; char *value = strchr(s, '='); if(!value) { error("Ignoring line %d ('%s'), there is no = in it.", line, s); continue; } *value = '\0'; value++; name = trim(name); value = trim(value); if(!name) { error("Ignoring line %d, name is empty.", line); continue; } if(!value) { debug(D_CONFIG, "Ignoring line %d, value is empty.", line); continue; } struct config_value *cv = config_value_index_find(co, name, 0); if(!cv) cv = config_value_create(co, name, value); else { if(((cv->flags & CONFIG_VALUE_USED) && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) { debug(D_CONFIG, "Overwriting '%s/%s'.", line, co->name, cv->name); free(cv->value); cv->value = strdup(value); if(!cv->value) fatal("Cannot allocate config.value"); } else debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name); } cv->flags |= CONFIG_VALUE_LOADED; } fclose(fp); pthread_rwlock_unlock(&config_rwlock); return 1; }
config_err_t config_parse (pstdout_state_t pstate, struct config_section *sections, struct config_arguments *cmd_args, FILE *fp) { char buf[CONFIG_PARSE_BUFLEN]; int line_num = 0; struct config_section *section = NULL; struct config_key *key; char *str, *tok; config_err_t rv = CONFIG_ERR_FATAL_ERROR; while (fgets (buf, CONFIG_PARSE_BUFLEN, fp)) { line_num++; buf[CONFIG_PARSE_BUFLEN-1] = '\0'; str = strtok (buf, " \t\n"); if (!str) { if (cmd_args->common_args.debug) pstdout_fprintf (pstate, stderr, "%d: empty line\n", line_num); continue; } if (str[0] == '#') { if (cmd_args->common_args.debug) pstdout_fprintf (pstate, stderr, "Comment on line %d\n", line_num); continue; } if (same (str, "Section")) { if (!(tok = strtok (NULL, " \t\n"))) { pstdout_fprintf (pstate, stderr, "FATAL: Error parsing line number %d\n", line_num); goto cleanup; } if (!(section = config_find_section (sections, tok))) { pstdout_fprintf (pstate, stderr, "Unknown section `%s'\n", tok); goto cleanup; } if (cmd_args->common_args.debug) pstdout_fprintf (pstate, stderr, "Entering section `%s'\n", section->section_name); continue; } /* same (str, "Section") */ if (same (str, "EndSection")) { if (!section) { pstdout_fprintf (pstate, stderr, "FATAL: encountered `%s' without a matching Section\n", str); goto cleanup; } if (cmd_args->common_args.debug) pstdout_fprintf (pstate, stderr, "Leaving section `%s'\n", section->section_name); section = NULL; continue; } /* same (str, "EndSection") */ if (!section) { pstdout_fprintf (pstate, stderr, "FATAL: Key `%s' not inside a valid Section\n", str); goto cleanup; } if (!(key = config_find_key (section, str))) { pstdout_fprintf (pstate, stderr, "Unknown key `%s' in section `%s'\n", str, section->section_name); goto cleanup; } tok = strtok (NULL, " \t\n"); if (!tok) tok = ""; if (cmd_args->common_args.debug) pstdout_fprintf (pstate, stderr, "Parsed `%s:%s=%s'\n", section->section_name, key->key_name, tok); if (config_find_keyvalue (section, key->key_name)) { pstdout_fprintf (pstate, stderr, "Key '%s' specified twice in section '%s'\n", key->key_name, section->section_name); goto cleanup; } if (config_section_add_keyvalue (pstate, section, key, tok, NULL) < 0) goto cleanup; } rv = CONFIG_ERR_SUCCESS; cleanup: return (rv); }
static config_err_t id_commit (const char *section_name, const struct config_keyvalue *kv, void *arg, int id) { bmc_config_state_data_t *state_data; fiid_obj_t obj_cmd_rs = NULL; config_err_t rv = CONFIG_ERR_FATAL_ERROR; config_err_t ret; uint8_t channel_number; uint8_t privs[CIPHER_SUITE_LEN]; uint8_t privilege; unsigned int i; assert (section_name); assert (kv); assert (arg); state_data = (bmc_config_state_data_t *)arg; if ((ret = _rmcpplus_cipher_suite_id_privilege_setup (state_data, section_name)) != CONFIG_ERR_SUCCESS) return (ret); if (!(obj_cmd_rs = fiid_obj_create (tmpl_cmd_set_lan_configuration_parameters_rs))) { pstdout_fprintf (state_data->pstate, stderr, "fiid_obj_create: %s\n", strerror (errno)); goto cleanup; } if ((ret = get_lan_channel_number (state_data, section_name, &channel_number)) != CONFIG_ERR_SUCCESS) { rv = ret; goto cleanup; } privilege = rmcpplus_priv_number (kv->value_input); memset (privs, '\0', CIPHER_SUITE_LEN); memcpy (privs, state_data->cipher_suite_priv, CIPHER_SUITE_LEN); /* achu: NOT A BUG. * * IPMI spec is_supported does not map to privileges array, you want to index at [id] not a searched [i] */ privs[id] = privilege; /* IPMI Workaround (achu) * * HP DL145 * * See comments above in _rmcpplus_cipher_suite_id_privilege_setup * surrounding HP DL145 workaround. * * B/c of the issue above, there may be illegal privilege levels * sitting in the cipher_suite_priv[] array, we need to fill them in * with the values configured by users. * * If the users didn't configure all the entries, they're out of * luck, we need to return an error. */ for (i = 0; i < CIPHER_SUITE_LEN; i++) { if (privs[i] == BMC_CONFIG_PRIVILEGE_LEVEL_SUPPORTED_BUT_NOT_READABLE) { struct config_section *section; if ((section = config_find_section (state_data->sections, section_name))) { char keynametmp[CONFIG_MAX_KEY_NAME_LEN + 1]; struct config_keyvalue *kvtmp; memset (keynametmp, '\0', CONFIG_MAX_KEY_NAME_LEN + 1); snprintf (keynametmp, CONFIG_MAX_KEY_NAME_LEN, "Maximum_Privilege_Cipher_Suite_Id_%u", i); if ((kvtmp = config_find_keyvalue (section, keynametmp))) { uint8_t privilege_tmp; privilege_tmp = rmcpplus_priv_number (kvtmp->value_input); privs[i] = privilege_tmp; } else { pstdout_fprintf (state_data->pstate, stderr, "ERROR: '%s:%s' Field Required\n", section_name, keynametmp); rv = CONFIG_ERR_NON_FATAL_ERROR; goto cleanup; } } else { /* This is a fatal error, we're already in this section, * it should be findable */ if (state_data->prog_data->args->config_args.common_args.debug) pstdout_fprintf (state_data->pstate, stderr, "Cannot find section '%s'\n", section_name); goto cleanup; } } } if (ipmi_cmd_set_lan_configuration_parameters_rmcpplus_messaging_cipher_suite_privilege_levels (state_data->ipmi_ctx, channel_number, privs[0], privs[1], privs[2], privs[3], privs[4], privs[5], privs[6], privs[7], privs[8], privs[9], privs[10], privs[11], privs[12], privs[13], privs[14], privs[15], obj_cmd_rs) < 0) { if (state_data->prog_data->args->config_args.common_args.debug) pstdout_fprintf (state_data->pstate, stderr, "ipmi_cmd_set_lan_configuration_parameters_rmcpplus_messaging_cipher_suite_privilege_levels: %s\n", ipmi_ctx_errormsg (state_data->ipmi_ctx)); if (config_is_config_param_non_fatal_error (state_data->ipmi_ctx, obj_cmd_rs, &ret)) rv = ret; goto cleanup; } /* achu: NOT A BUG. * * IPMI spec is_supported does not map to privileges array, you want to index at [id] not [i] */ state_data->cipher_suite_priv[id] = privilege; rv = CONFIG_ERR_SUCCESS; cleanup: fiid_obj_destroy (obj_cmd_rs); return (rv); }
/*------------------------------------------------------------------------- Load the game configuration and optionally emulator settings from the game config file. Input: Filename Name of the game config file. Return: 1 if read successfully or zero. ---------------------------------------------------------------------------*/ Uint32 config_load_cue_file ( char *Filename ) { char File[256]; CONFIG_FILE Config; Uint32 i; strcpy ( neogeo_toc_file_name, Filename ); if ( config_fopen ( &Config, Filename ) ) { if ( config_find_section ( &Config, "Game Info" ) ) goto config_ok; config_fclose ( &Config ); } platform_remove_trailing_slash ( Filename ); platform_get_file_name ( Filename, File ); strcpy ( neogeo_toc_file_name, Filename ); strcat ( neogeo_toc_file_name, DIRECTORY_SEPARATOR_STRING ); strcat ( neogeo_toc_file_name, File ); strcat ( neogeo_toc_file_name, ".cfg" ); if ( !config_fopen ( &Config, neogeo_toc_file_name ) ) { fprintf ( stderr, "Can't open game configuration file. (%s)\n", neogeo_toc_file_name ); return 0; } if ( !config_find_section ( &Config, "Game Info" ) ) { fprintf ( stderr, "Syntax error in game cfg file, can't find section [Game Info]\n" ); config_fclose ( &Config ); return 0; } config_ok: if ( !config_read_string ( &Config, "Name", neogeo_game_name, 256 ) ) { fprintf ( stderr, "Syntax error in game cfg file, can't read variable 'Name'\n" ); config_fclose ( &Config ); return 0; } neogeo_cdrom_first_track = 1; if ( !config_read_integer ( &Config, "Tracks", &neogeo_cdrom_last_track ) ) { fprintf ( stderr, "Syntax error in game cfg file, can't read variable 'Tracks'\n" ); config_fclose ( &Config ); return 0; } if ( ( !neogeo_cdrom_last_track ) || ( neogeo_cdrom_last_track > 99 ) ) { fprintf ( stderr, "Syntax error in game cfg file, 'Tracks' must be between 1 & 99\n" ); config_fclose ( &Config ); return 0; } if ( !config_read_integer ( &Config, "Leadout", &neogeo_cdrom_toc[0].position ) ) { fprintf ( stderr, "Syntax error in game cfg file, can't read variable 'Leadout'\n" ); config_fclose ( &Config ); return 0; } config_read_integer_default ( &Config, "Fix_DoubleDragon", &Fix_DoubleDragon, 0 ); if ( !config_find_section ( &Config, "TOC" ) ) { fprintf ( stderr, "Syntax error in game cfg file, can't find section [TOC]\n" ); config_fclose ( &Config ); return 0; } for ( i = 1; i <= neogeo_cdrom_last_track; i++ ) { sprintf ( File, "%d", i ); if ( !config_find_subsection ( &Config, File ) ) { fprintf ( stderr, "Syntax error in game cfg file, can't find subsection %s\n", File ); config_fclose ( &Config ); return 0; } if ( !config_read_integer ( &Config, "Position", &neogeo_cdrom_toc[i].position ) ) { fprintf ( stderr, "Syntax error in game cfg file, can't read Position for track %d\n", i ); config_fclose ( &Config ); return 0; } if ( !config_read_string ( &Config, "Filename", &neogeo_cdrom_toc[i].filename[0], 256 ) ) { fprintf ( stderr, "Syntax error in game cfg file, can't read Filename for track %d\n", i ); config_fclose ( &Config ); return 0; } } config_load_settings ( &Config ); config_fclose ( &Config ); return 1; }
/*------------------------------------------------------------------------- Read emulator settings from the config file. Input: Config Pointer to the config file stucture. ---------------------------------------------------------------------------*/ void config_load_settings ( CONFIG_FILE *Config ) { if ( !config_find_section ( Config, "Settings" ) ) return; config_read_bool ( Config, "Show Debugger", &neogeo_debugger_enabled ); config_read_enum ( Config, "Debugger Key", &neogeo_debugger_key, config_key_enum ); config_read_enum ( Config, "Nationality", &neogeo_machine_nationality, config_nationality_enum ); config_read_bool ( Config, "Fullscreen", &video_fullscreen ); config_read_bool ( Config, "Full Throttle", &neogeo_fullthrottle_enable ); config_read_integer ( Config, "Audio Buffer Size", &neogeo_audio_buffer_size ); config_read_integer ( Config, "Latency", &neogeo_audio_latency ); config_read_integer ( Config, "CD Volume", &neogeo_cd_volume ); if ( neogeo_cd_volume > 100 ) neogeo_cd_volume = 100; config_read_integer ( Config, "YM2610 Volume", &neogeo_ym2610_volume ); if ( neogeo_ym2610_volume > 100 ) neogeo_ym2610_volume = 100; config_read_bool ( Config, "Display Border", &video_display_border ); config_read_enum ( Config, "Window Interpolation", &video_window_interpolation, config_filter_enum ); config_read_integer ( Config, "Window Resolution X", &video_window_resolution_x ); if ( video_window_resolution_x < 640 ) video_window_resolution_x = 640; config_read_integer ( Config, "Window Resolution Y", &video_window_resolution_y ); if ( video_window_resolution_y < 480 ) video_window_resolution_y = 480; config_read_enum ( Config, "Fullscreen Interpolation", &video_fullscreen_interpolation, config_filter_enum ); config_read_integer ( Config, "Fullscreen Resolution X", &video_fullscreen_resolution_x ); if ( video_fullscreen_resolution_x < 640 ) video_fullscreen_resolution_x = 640; config_read_integer ( Config, "Fullscreen Resolution Y", &video_fullscreen_resolution_y ); if ( video_fullscreen_resolution_y < 480 ) video_fullscreen_resolution_y = 480; config_read_bool ( Config, "Vsync", &video_vsync ); if ( video_vsync ) video_lag_tolerance = 0; else video_lag_tolerance = -30; config_read_bool ( Config, "Keep Aspect Ratio", &video_keep_aspect_ratio ); config_read_enum ( Config, "Controller 1 Up Key", &neogeo_controller1_up_key, config_key_enum ); config_read_enum ( Config, "Controller 1 Down Key", &neogeo_controller1_down_key, config_key_enum ); config_read_enum ( Config, "Controller 1 Left Key", &neogeo_controller1_left_key, config_key_enum ); config_read_enum ( Config, "Controller 1 Right Key", &neogeo_controller1_right_key, config_key_enum ); config_read_enum ( Config, "Controller 1 A Key", &neogeo_controller1_a_key, config_key_enum ); config_read_enum ( Config, "Controller 1 B Key", &neogeo_controller1_b_key, config_key_enum ); config_read_enum ( Config, "Controller 1 C Key", &neogeo_controller1_c_key, config_key_enum ); config_read_enum ( Config, "Controller 1 D Key", &neogeo_controller1_d_key, config_key_enum ); config_read_enum ( Config, "Controller 1 Start Key", &neogeo_controller1_start_key, config_key_enum ); config_read_enum ( Config, "Controller 1 Select Key", &neogeo_controller1_select_key, config_key_enum ); config_read_enum ( Config, "Controller 2 Up Key", &neogeo_controller2_up_key, config_key_enum ); config_read_enum ( Config, "Controller 2 Down Key", &neogeo_controller2_down_key, config_key_enum ); config_read_enum ( Config, "Controller 2 Left Key", &neogeo_controller2_left_key, config_key_enum ); config_read_enum ( Config, "Controller 2 Right Key", &neogeo_controller2_right_key, config_key_enum ); config_read_enum ( Config, "Controller 2 A Key", &neogeo_controller2_a_key, config_key_enum ); config_read_enum ( Config, "Controller 2 B Key", &neogeo_controller2_b_key, config_key_enum ); config_read_enum ( Config, "Controller 2 C Key", &neogeo_controller2_c_key, config_key_enum ); config_read_enum ( Config, "Controller 2 D Key", &neogeo_controller2_d_key, config_key_enum ); config_read_enum ( Config, "Controller 2 Start Key", &neogeo_controller2_start_key, config_key_enum ); config_read_enum ( Config, "Controller 2 Select Key", &neogeo_controller2_select_key, config_key_enum ); if ( sdl_joystick1 ) { if ( config_find_subsection ( Config, SDL_JoystickName ( 0 ) ) ) { config_read_integer ( Config, "X Axis", &neogeo_controller1_x_axis ); config_read_integer ( Config, "Y Axis", &neogeo_controller1_y_axis ); config_read_integer ( Config, "Hat", &neogeo_controller1_hat ); config_read_integer ( Config, "A Button", &neogeo_controller1_a_button ); config_read_integer ( Config, "B Button", &neogeo_controller1_b_button ); config_read_integer ( Config, "C Button", &neogeo_controller1_c_button ); config_read_integer ( Config, "D Button", &neogeo_controller1_d_button ); config_read_integer ( Config, "Start Button", &neogeo_controller1_start_button ); config_read_integer ( Config, "Select Button", &neogeo_controller1_select_button ); fprintf ( stdout, "Loaded profile %s for joystick 0.\n", SDL_JoystickName ( 0 ) ); } else fprintf ( stdout, "Using default settings for joystick 0.\n" ); } if ( sdl_joystick2 ) { if ( config_find_subsection ( Config, SDL_JoystickName ( 1 ) ) ) { config_read_integer ( Config, "X Axis", &neogeo_controller2_x_axis ); config_read_integer ( Config, "Y Axis", &neogeo_controller2_y_axis ); config_read_integer ( Config, "Hat", &neogeo_controller2_hat ); config_read_integer ( Config, "A Button", &neogeo_controller2_a_button ); config_read_integer ( Config, "B Button", &neogeo_controller2_b_button ); config_read_integer ( Config, "C Button", &neogeo_controller2_c_button ); config_read_integer ( Config, "D Button", &neogeo_controller2_d_button ); config_read_integer ( Config, "Start Button", &neogeo_controller2_start_button ); config_read_integer ( Config, "Select Button", &neogeo_controller2_select_button ); fprintf ( stdout, "Loaded profile %s for joystick 1.\n", SDL_JoystickName ( 1 ) ); } else fprintf ( stdout, "Using default settings for joystick 1.\n" ); } }
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; }
static int _ipmi_sensors_config (pstdout_state_t pstate, const char *hostname, void *arg) { ipmi_sensors_config_state_data_t state_data; ipmi_sensors_config_prog_data_t *prog_data; int exit_code = EXIT_FAILURE; config_err_t ret = 0; int file_opened = 0; FILE *fp = NULL; /* init NULL to remove warnings */ assert (pstate); assert (arg); prog_data = (ipmi_sensors_config_prog_data_t *) arg; if (prog_data->args->config_args.common_args.flush_cache) { if (sdr_cache_flush_cache (pstate, hostname, &prog_data->args->config_args.common_args) < 0) return (EXIT_FAILURE); return (EXIT_SUCCESS); } memset (&state_data, '\0', sizeof (ipmi_sensors_config_state_data_t)); state_data.prog_data = prog_data; state_data.pstate = pstate; if (!(state_data.ipmi_ctx = ipmi_open (prog_data->progname, hostname, &(prog_data->args->config_args.common_args), state_data.pstate))) goto cleanup; if (!(state_data.sdr_ctx = ipmi_sdr_ctx_create ())) { pstdout_perror (pstate, "ipmi_sdr_ctx_create()"); goto cleanup; } if (sdr_cache_create_and_load (state_data.sdr_ctx, NULL, state_data.ipmi_ctx, hostname, &state_data.prog_data->args->config_args.common_args) < 0) goto cleanup; if (!(state_data.sections = ipmi_sensors_config_sections_create (&state_data))) goto cleanup; if (prog_data->args->config_args.action == CONFIG_ACTION_CHECKOUT) { if (prog_data->args->config_args.filename) { if (prog_data->hosts_count > 1) { pstdout_fprintf (pstate, stderr, "Cannot output multiple host checkout into a single file\n"); goto cleanup; } if (!(fp = fopen (prog_data->args->config_args.filename, "w"))) { pstdout_perror (pstate, "fopen"); goto cleanup; } file_opened++; } else fp = stdout; } else if (prog_data->args->config_args.action == CONFIG_ACTION_COMMIT || prog_data->args->config_args.action == CONFIG_ACTION_DIFF) { if (prog_data->args->config_args.filename && strcmp (prog_data->args->config_args.filename, "-")) { if (!(fp = fopen (prog_data->args->config_args.filename, "r"))) { pstdout_perror (pstate, "fopen"); goto cleanup; } file_opened++; } else fp = stdin; } /* parse if there is an input file or no pairs at all */ if ((prog_data->args->config_args.action == CONFIG_ACTION_COMMIT && prog_data->args->config_args.filename) || (prog_data->args->config_args.action == CONFIG_ACTION_COMMIT && !prog_data->args->config_args.filename && !prog_data->args->config_args.keypairs) || (prog_data->args->config_args.action == CONFIG_ACTION_DIFF && prog_data->args->config_args.filename) || (prog_data->args->config_args.action == CONFIG_ACTION_DIFF && !prog_data->args->config_args.filename && !prog_data->args->config_args.keypairs)) { if (config_parse (pstate, state_data.sections, &(prog_data->args->config_args), fp) < 0) { /* errors printed in function call */ goto cleanup; } } /* note: argp validation catches if user specified keypair and filename for a diff */ if ((prog_data->args->config_args.action == CONFIG_ACTION_CHECKOUT || prog_data->args->config_args.action == CONFIG_ACTION_COMMIT || prog_data->args->config_args.action == CONFIG_ACTION_DIFF) && prog_data->args->config_args.keypairs) { if (config_sections_insert_keyvalues (pstate, state_data.sections, prog_data->args->config_args.keypairs) < 0) { /* errors printed in function call */ goto cleanup; } } if (prog_data->args->config_args.action == CONFIG_ACTION_COMMIT || prog_data->args->config_args.action == CONFIG_ACTION_DIFF) { int num; if ((num = config_sections_validate_keyvalue_inputs (pstate, state_data.sections, &state_data)) < 0) goto cleanup; /* some errors found */ if (num) goto cleanup; } if (prog_data->args->config_args.action == CONFIG_ACTION_CHECKOUT && prog_data->args->config_args.section_strs) { struct config_section_str *sstr; sstr = prog_data->args->config_args.section_strs; while (sstr) { if (!config_find_section (state_data.sections, sstr->section_name)) { pstdout_fprintf (pstate, stderr, "Unknown section `%s'\n", sstr->section_name); goto cleanup; } sstr = sstr->next; } } switch (prog_data->args->config_args.action) { case CONFIG_ACTION_CHECKOUT: if (prog_data->args->config_args.section_strs) { struct config_section_str *sstr; /* note: argp validation catches if user specified --section * and --keypair, so all_keys_if_none_specified should be '1'. */ sstr = prog_data->args->config_args.section_strs; while (sstr) { struct config_section *s; config_err_t this_ret; if (!(s = config_find_section (state_data.sections, sstr->section_name))) { pstdout_fprintf (pstate, stderr, "## FATAL: Cannot checkout section '%s'\n", sstr->section_name); continue; } this_ret = config_checkout_section (pstate, s, &(prog_data->args->config_args), 1, fp, 75, &state_data); if (this_ret != CONFIG_ERR_SUCCESS) ret = this_ret; if (ret == CONFIG_ERR_FATAL_ERROR) break; sstr = sstr->next; } } else { int all_keys_if_none_specified = 0; if (!prog_data->args->config_args.keypairs) all_keys_if_none_specified++; ret = config_checkout (pstate, state_data.sections, &(prog_data->args->config_args), all_keys_if_none_specified, fp, 75, &state_data); } break; case CONFIG_ACTION_COMMIT: ret = config_commit (pstate, state_data.sections, &(prog_data->args->config_args), &state_data); break; case CONFIG_ACTION_DIFF: ret = config_diff (pstate, state_data.sections, &(prog_data->args->config_args), &state_data); break; case CONFIG_ACTION_LIST_SECTIONS: ret = config_output_sections_list (pstate, state_data.sections); break; } if (ret == CONFIG_ERR_FATAL_ERROR) { exit_code = CONFIG_FATAL_EXIT_VALUE; goto cleanup; } if (ret == CONFIG_ERR_NON_FATAL_ERROR) { exit_code = CONFIG_NON_FATAL_EXIT_VALUE; goto cleanup; } exit_code = EXIT_SUCCESS; cleanup: ipmi_sdr_ctx_destroy (state_data.sdr_ctx); ipmi_ctx_close (state_data.ipmi_ctx); ipmi_ctx_destroy (state_data.ipmi_ctx); config_sections_destroy (state_data.sections); if (file_opened) fclose (fp); return (exit_code); }