void rtm_init_knobs(struct fins_module *module) { metadata_element *root = config_root_setting(module->knobs); //------------------------------------------------------------------------------------------- metadata_element *exec_elem = config_setting_add(root, OP_EXEC_STR, META_TYPE_GROUP); if (exec_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } //------------------------------------------------------------------------------------------- metadata_element *get_elem = config_setting_add(root, OP_GET_STR, META_TYPE_GROUP); if (get_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } //------------------------------------------------------------------------------------------- metadata_element *set_elem = config_setting_add(root, OP_SET_STR, META_TYPE_GROUP); if (set_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } //------------------------------------------------------------------------------------------- metadata_element *listen_elem = config_setting_add(root, OP_LISTEN_STR, META_TYPE_GROUP); if (listen_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } }
ret_code_t entry_copy_aggregate(config_setting_t * source, config_setting_t * dest, int type) { const char * setting_name; config_setting_t * new_dest; int index=0; char tag[7]; setting_name = config_setting_name(source); if(setting_name == NULL) { return RET_NOK; } new_dest=config_setting_add(dest,setting_name,type); if( new_dest == NULL ) { // Try to find an available name sprintf(tag,"A%05x",index); while((new_dest=config_setting_add(dest,setting_name,type))== NULL) { index++; sprintf(tag,"A%05x",index); } } if(!entry_copy_config(source,new_dest)) { config_setting_remove(dest,setting_name); return RET_NOK; } return RET_OK; }
void switch_init_knobs(struct fins_module *module) { metadata_element *root = config_root_setting(module->knobs); //int status; //------------------------------------------------------------------------------------------- metadata_element *exec_elem = config_setting_add(root, OP_EXEC_STR, META_TYPE_GROUP); if (exec_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } //------------------------------------------------------------------------------------------- metadata_element *get_elem = config_setting_add(root, OP_GET_STR, META_TYPE_GROUP); if (get_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } //elem_add_param(get_elem, LOGGER_GET_INTERVAL__str, LOGGER_GET_INTERVAL__id, LOGGER_GET_INTERVAL__type); //elem_add_param(get_elem, LOGGER_GET_REPEATS__str, LOGGER_GET_REPEATS__id, LOGGER_GET_REPEATS__type); //------------------------------------------------------------------------------------------- metadata_element *set_elem = config_setting_add(root, OP_SET_STR, META_TYPE_GROUP); if (set_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } //elem_add_param(set_elem, LOGGER_SET_INTERVAL__str, LOGGER_SET_INTERVAL__id, LOGGER_SET_INTERVAL__type); //elem_add_param(set_elem, LOGGER_SET_REPEATS__str, LOGGER_SET_REPEATS__id, LOGGER_SET_REPEATS__type); }
//####################################### autoconfig void ipv4_init_params(struct fins_module *module) { metadata_element *root = config_root_setting(module->params); //int status; //------------------------------------------------------------------------------------------- metadata_element *exec_elem = config_setting_add(root, "exec", CONFIG_TYPE_GROUP); if (exec_elem == NULL) { PRINT_DEBUG("todo error"); exit(-1); } //------------------------------------------------------------------------------------------- metadata_element *get_elem = config_setting_add(root, "get", CONFIG_TYPE_GROUP); if (get_elem == NULL) { PRINT_DEBUG("todo error"); exit(-1); } //elem_add_param(get_elem, LOGGER_GET_INTERVAL__str, LOGGER_GET_INTERVAL__id, LOGGER_GET_INTERVAL__type); //elem_add_param(get_elem, LOGGER_GET_REPEATS__str, LOGGER_GET_REPEATS__id, LOGGER_GET_REPEATS__type); //------------------------------------------------------------------------------------------- metadata_element *set_elem = config_setting_add(root, "set", CONFIG_TYPE_GROUP); if (set_elem == NULL) { PRINT_DEBUG("todo error"); exit(-1); } //elem_add_param(set_elem, LOGGER_SET_INTERVAL__str, LOGGER_SET_INTERVAL__id, LOGGER_SET_INTERVAL__type); //elem_add_param(set_elem, LOGGER_SET_REPEATS__str, LOGGER_SET_REPEATS__id, LOGGER_SET_REPEATS__type); }
int store_settings(char *cfg_file, int show_line_numbers, int show_lang, int show_spelling_errors, int defualt_window_width, int default_window_height, int undo_level, int show_full_path #ifdef AUTO_TAB_TOGGLE , int auto_tab #endif ){ config_t cfg; config_setting_t *root, *setting, *group, *UI; config_init(&cfg); int ret=0; root = config_root_setting(&cfg); group = config_setting_add(root,"User_Pref", CONFIG_TYPE_GROUP); setting = config_setting_add(group,"LineNumbers", CONFIG_TYPE_INT); config_setting_set_int(setting, show_line_numbers); setting = config_setting_add(group,"Spelling", CONFIG_TYPE_INT); config_setting_set_int(setting, show_spelling_errors); setting = config_setting_add(group,"ShowLang", CONFIG_TYPE_INT); config_setting_set_int(setting, show_lang); #ifdef AUTO_TAB_TOGGLE setting = config_setting_add(group,"Auto_Tab", CONFIG_TYPE_INT); config_setting_set_int(setting, auto_tab); #endif setting = config_setting_add(group,"Show_Full_Path", CONFIG_TYPE_INT); config_setting_set_int(setting, show_full_path); UI = config_setting_add(root,"User_Interface_Settings", CONFIG_TYPE_GROUP); setting = config_setting_add(UI,"Default_Window_Width", CONFIG_TYPE_INT); config_setting_set_int(setting, defualt_window_width); setting = config_setting_add(UI,"Default_Window_Height", CONFIG_TYPE_INT); config_setting_set_int(setting, default_window_height); setting = config_setting_add(UI,"Undo_Level", CONFIG_TYPE_INT); config_setting_set_int(setting, undo_level); if (! config_write_file(&cfg,(char*)cfg_file)) ret=0; else ret=1; config_destroy(&cfg); return(ret); }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ int general_settings_save_configuration (config_setting_t *setting) { config_setting_t *s = NULL ; s = config_setting_add(setting, "path_1_wire_directory", CONFIG_TYPE_STRING) ; if (s != NULL) config_setting_set_string(s, path_1_wire_directory->str); s = config_setting_add(setting, "path_data_directory", CONFIG_TYPE_STRING) ; if (s != NULL) config_setting_set_string(s, path_data_directory->str); s = config_setting_add(setting, "write_samples_interval", CONFIG_TYPE_INT) ; if (s != NULL) config_setting_set_int(s, write_samples_interval); return 0 ; } ;
static config_setting_t * create_tree(const config_t * config, config_setting_t * prev_setting, char * prev_entry, char * path, int type, va_list ap) { char * entry = NULL; char * new_path; config_setting_t * setting = NULL; config_setting_t * ret = NULL; if( prev_setting == NULL) { prev_setting = config_root_setting(config); } entry = va_arg(ap,char*); /* no more leaf */ if(entry == NULL) { /* check if leaf exists */ setting = config_lookup(config, path); if( setting == NULL) { /* create the leaf */ setting=config_setting_add(prev_setting, prev_entry,type); } return setting; } /* still not finished */ /* check if we need to create a group */ if( path ) { setting = config_lookup(config, path); if( setting == NULL ) { /* create a group with previous path */ setting = config_setting_add(prev_setting, prev_entry, CONFIG_TYPE_GROUP); if(setting == NULL) { return NULL; } } } /* update path */ if( path ) { new_path = strconcat(path,".",entry,NULL); } else { new_path = strdup(entry); } ret = create_tree(config,setting,entry,new_path,type,ap); free(new_path); return ret; }
void config_setting_copy_simple(config_setting_t *parent, const config_setting_t *src) { if (config_setting_is_aggregate(src)) { config_setting_copy_aggregate(parent, src); } else { config_setting_t *set; if( config_setting_get_member(parent, config_setting_name(src)) != NULL ) return; if ((set = config_setting_add(parent, config_setting_name(src), config_setting_type(src))) == NULL) return; if (CONFIG_TYPE_INT == config_setting_type(src)) { config_setting_set_int(set, config_setting_get_int(src)); config_setting_set_format(set, src->format); } else if (CONFIG_TYPE_INT64 == config_setting_type(src)) { config_setting_set_int64(set, config_setting_get_int64(src)); config_setting_set_format(set, src->format); } else if (CONFIG_TYPE_FLOAT == config_setting_type(src)) { config_setting_set_float(set, config_setting_get_float(src)); } else if (CONFIG_TYPE_STRING == config_setting_type(src)) { config_setting_set_string(set, config_setting_get_string(src)); } else if (CONFIG_TYPE_BOOL == config_setting_type(src)) { config_setting_set_bool(set, config_setting_get_bool(src)); } } }
int main() { /* Initialize the configuration */ config_init(&cfg); /* Load the file */ printf("loading [test.cfg].."); if (!config_load_file(&cfg, "test.cfg")) printf("failed\n"); else { printf("ok\n"); /* Add setting "foo" */ printf("add setting \"foo\".."); config_setting_t *setting = config_setting_add(cfg.root,"foo",CONFIG_TYPE_INT); if (!setting) printf("failed\n"); else { config_setting_set_int(setting,1234); printf("ok\n"); /* Save to "testfoo.cfg" */ printf("saving [testfoo.cfg].."); config_save_file(&cfg, "testfoo.cfg"); printf("ok\n"); printf("Done!\n"); } } /* Free the configuration */ config_destroy(&cfg); return 0; }
/********************** __get_unused_group_on_path find an unused group, DO NOT CREATE IT and return its name returned string must be freed return NULL on error **********************/ char * __get_unused_group_on_path(const char * table, const char * file, char * path) { char tag[7]; int index = 0; config_setting_t * setting; const config_t * config; SDL_LockMutex(entry_mutex); config = get_config(table,file); if(config==NULL) { SDL_UnlockMutex(entry_mutex); return NULL; } sprintf(tag,"A%05x",index); if(path != NULL) { setting = config_lookup(config,path); free(path); } else { setting = config_root_setting(config); } while( config_setting_add(setting,tag,CONFIG_TYPE_GROUP) == NULL ) { index++; sprintf(tag,"A%05x",index); } config_setting_remove(setting,tag); SDL_UnlockMutex(entry_mutex); return strdup(tag); }
config_setting_t *cfgSettingAdd(config_setting_t *parent, const TCHAR *name, int type) { char uName[CONFIG_UTF8_MAXSTRING]; if(name) UTF8_Encode(name, uName, CONFIG_UTF8_MAXSTRING); return config_setting_add(parent, uName, type); }
void MainDialog::configSetBool(const char* key, bool val) { config_setting_t* setting = config_lookup(&config_, key); if(!setting) { // setting not found // add a new setting for it config_setting_t* root = config_root_setting(&config_); setting = config_setting_add(root, key, CONFIG_TYPE_BOOL); } config_setting_set_bool(setting, val); }
void MainDialog::configSetFloat(const char* key, double val) { config_setting_t* setting = config_lookup(&config_, key); if(!setting) { // setting not found // add a new setting for it config_setting_t* root = config_root_setting(&config_); setting = config_setting_add(root, key, CONFIG_TYPE_FLOAT); } config_setting_set_float(setting, val); }
int main(int argc, char **argv) { static const char *output_file = "updated.cfg"; config_t cfg; config_setting_t *root, *setting, *movie; config_init(&cfg); /* Read the file. If there is an error, report it and exit. */ if(! config_read_file(&cfg, "example.cfg")) { fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); config_destroy(&cfg); return(EXIT_FAILURE); } /* Find the 'movies' setting. Add intermediate settings if they don't yet * exist. */ root = config_root_setting(&cfg); setting = config_setting_get_member(root, "inventory"); if(!setting) setting = config_setting_add(root, "inventory", CONFIG_TYPE_GROUP); setting = config_setting_get_member(setting, "movies"); if(!setting) setting = config_setting_add(setting, "movies", CONFIG_TYPE_LIST); /* Create the new movie entry. */ movie = config_setting_add(setting, NULL, CONFIG_TYPE_GROUP); setting = config_setting_add(movie, "title", CONFIG_TYPE_STRING); config_setting_set_string(setting, "Buckaroo Banzai"); setting = config_setting_add(movie, "media", CONFIG_TYPE_STRING); config_setting_set_string(setting, "DVD"); setting = config_setting_add(movie, "price", CONFIG_TYPE_FLOAT); config_setting_set_float(setting, 12.99); setting = config_setting_add(movie, "qty", CONFIG_TYPE_INT); config_setting_set_float(setting, 20); /* Write out the updated configuration. */ if(! config_write_file(&cfg, output_file)) { fprintf(stderr, "Error while writing file.\n"); config_destroy(&cfg); return(EXIT_FAILURE); } fprintf(stderr, "Updated configuration successfully written to: %s\n", output_file); config_destroy(&cfg); return(EXIT_SUCCESS); }
config_setting_t *cfgSetInt(config_setting_t *parent, const TCHAR *name, int value) { config_setting_t *setting; char uName[CONFIG_UTF8_MAXSTRING]; UTF8_Encode(name, uName, CONFIG_UTF8_MAXSTRING); setting = config_setting_add(parent, uName, CONFIG_TYPE_INT); config_setting_set_int(setting, value); return setting; }
// {{{ session_store_session() int session_store_session(const char* user, const char* sessid) { if(strlen(user)==0 || strlen(sessid)==0) return -1; char var[256]; struct config_t cfg; config_init(&cfg); char date[255]; util_get_date(date, sizeof(date), "%Y%m%d%H%M%S"); config_setting_t *cs; config_setting_t *us; if(!config_read_file(&cfg, OD_SESSION_FILE)) config_write_file(&cfg, OD_SESSION_FILE); if( !(us = config_lookup(&cfg, user)) ) { us = config_setting_add(cfg.root, user, CONFIG_TYPE_GROUP); cs = config_setting_add(us, "sessid", CONFIG_TYPE_STRING); cs = config_setting_add(us, "timestamp", CONFIG_TYPE_STRING); } snprintf(var, sizeof(var), "%s.sessid", user); if( !(cs = config_lookup(&cfg, var)) ) return 0; config_setting_set_string(cs, sessid); snprintf(var, sizeof(var), "%s.timestamp", user); if( !(cs = config_lookup(&cfg, var)) ) return 0; config_setting_set_string(cs, date); config_write_file(&cfg, OD_SESSION_FILE); config_destroy(&cfg); return 0; }
void patch_save() { config_setting_t* root_setting = config_root_setting(&patch_config); config_setting_remove(root_setting, CFG_MOD_MATRIX_CONTROLLER); config_setting_t* mod_matrix_patch = config_setting_add(root_setting, CFG_MOD_MATRIX_CONTROLLER, CONFIG_TYPE_GROUP); mod_matrix_controller_save(mod_matrix_patch); if (config_write_file(&patch_config, PATCH_FILE) != CONFIG_TRUE) { LOG_ERROR("Patch write error to %s", PATCH_FILE); } }
void rtm_init_params(struct fins_module *module) { metadata_element *root = config_root_setting(module->params); metadata_element *exec_elem = config_setting_add(root, "exec", CONFIG_TYPE_GROUP); if (exec_elem == NULL) { PRINT_DEBUG("todo error"); exit(-1); } metadata_element *get_elem = config_setting_add(root, "get", CONFIG_TYPE_GROUP); if (get_elem == NULL) { PRINT_DEBUG("todo error"); exit(-1); } metadata_element *set_elem = config_setting_add(root, "set", CONFIG_TYPE_GROUP); if (set_elem == NULL) { PRINT_DEBUG("todo error"); exit(-1); } metadata_element *sub = config_setting_add(exec_elem, "test", CONFIG_TYPE_GROUP); if (sub == NULL) { PRINT_DEBUG("todo error"); exit(-1); } metadata_element *elem = config_setting_add(sub, "key", CONFIG_TYPE_INT); if (elem == NULL) { PRINT_DEBUG("todo error"); exit(-1); } uint32_t value = 10; int status = config_setting_set_int(elem, *(int *) &value); if (status == CONFIG_FALSE) { PRINT_DEBUG("todo error"); exit(-1); } }
/** * Checks if the configuration is valid. The function checks if all * required parameters are set and adds default values if necessary. * @param cfg configuration */ void config_check(config_t *cfg) { int i, j; const char *s; double f; config_setting_t *cs, *vs; for (i = 0; defaults[i].name; i++) { /* Lookup setting group */ cs = config_lookup(cfg, defaults[i].group); if (!cs) cs = config_setting_add(config_root_setting(cfg), defaults[i].group, CONFIG_TYPE_GROUP); /* (1) Check for string */ if (defaults[i].type == CONFIG_TYPE_STRING) { if (config_setting_lookup_string(cs, defaults[i].name, &s)) continue; /* Add default value */ vs = config_setting_add(cs, defaults[i].name, CONFIG_TYPE_STRING); config_setting_set_string(vs, defaults[i].val.sval); /* (2) Check for float */ } else if (defaults[i].type == CONFIG_TYPE_FLOAT) { if (config_setting_lookup_float(cs, defaults[i].name, &f)) continue; /* Check for mis-interpreted integer */ if (config_setting_lookup_int(cs, defaults[i].name, &j)) { config_setting_remove(cs, defaults[i].name); vs = config_setting_add(cs, defaults[i].name, CONFIG_TYPE_FLOAT); config_setting_set_float(vs, (double) j); continue; } /* Add default value */ vs = config_setting_add(cs, defaults[i].name, CONFIG_TYPE_FLOAT); config_setting_set_float(vs, defaults[i].val.fval); /* (3) Check for integer */ } else if (defaults[i].type == CONFIG_TYPE_INT) { if (config_setting_lookup_int(cs, defaults[i].name, &j)) continue; /* Check for mis-interpreted float */ if (config_setting_lookup_float(cs, defaults[i].name, &f)) { config_setting_remove(cs, defaults[i].name); vs = config_setting_add(cs, defaults[i].name, CONFIG_TYPE_INT); config_setting_set_int(vs, (long) round(f)); continue; } /* Add default value */ vs = config_setting_add(cs, defaults[i].name, CONFIG_TYPE_INT); config_setting_set_int(vs, defaults[i].val.ival); } else { error("Unknown configuration type"); } } }
/******************************************************************************* 函数名称 : webauth_rdx_set_str_setting 功能描述 : 向配置结构中增加字符串类型的属性 输入参数 : config_setting_t * setting 配置结构 char* attr 配置属性名 char* val 配置属性值 输出参数 : 返 回 值 : -------------------------------------------------------------------------------- 最近一次修改记录 : 修改作者 : 王群 修改目的 : 新添加函数 修改日期 : 2010-08-16 *******************************************************************************/ void webauth_rdx_set_str_setting (config_setting_t * setting, char *attr, char *val) { config_setting_t *s; if (0 != strlen (val)) { s = config_setting_add (setting, attr, CONFIG_TYPE_STRING); if (NULL != s) { config_setting_set_string (s, val); } } return; }
/******************************************************************************* 函数名称 : webauth_rdx_set_int_setting 功能描述 : 向配置结构中增加整数型配置 输入参数 : onfig_setting_t * setting 配置结构 char* attr 配置属性名 int val 配置属性值 int def 配置默认属性 输出参数 : 返 回 值 : -------------------------------------------------------------------------------- 最近一次修改记录 : 修改作者 : 王群 修改目的 : 新添加函数 修改日期 : 2010-08-16 *******************************************************************************/ void webauth_rdx_set_int_setting (config_setting_t * setting, char *attr, int val, int def) { config_setting_t *s; if (val != def) { s = config_setting_add (setting, attr, CONFIG_TYPE_INT); if (NULL != s) { config_setting_set_int (s, val); } } return; }
config_setting_t *cfgSetString(config_setting_t *parent, const TCHAR *name, const TCHAR *value) { config_setting_t *setting; char *cfgStr; char uName[CONFIG_UTF8_MAXSTRING]; UTF8_Encode(name, uName, CONFIG_UTF8_MAXSTRING); setting = config_setting_add(parent, uName, CONFIG_TYPE_STRING); cfgStr = UTF8_Encode_Dyn(value); config_setting_set_string(setting, cfgStr); free(cfgStr); return setting; }
//------------------------------------------------------------------------------ int sensors_save_configuration (config_setting_t *setting) { config_setting_t *s = NULL ; GList *tmp = NULL ; s = config_setting_add(setting, "Sensor_List", CONFIG_TYPE_LIST) ; tmp = sensors ; while (tmp != NULL) { t_sensor_save_configuration(s, (T_Sensor*)tmp->data) ; tmp = g_list_next(tmp) ; } ; return 0 ; } ;
int main(int argc, char **argv) { static const char *output_file = "newconfig.cfg"; config_t cfg; config_setting_t *root, *setting, *group, *array; int i; config_init(&cfg); root = config_root_setting(&cfg); /* Add some settings to the configuration. */ group = config_setting_add(root, "address", CONFIG_TYPE_GROUP); setting = config_setting_add(group, "street", CONFIG_TYPE_STRING); config_setting_set_string(setting, "1 Woz Way"); setting = config_setting_add(group, "city", CONFIG_TYPE_STRING); config_setting_set_string(setting, "San Jose"); setting = config_setting_add(group, "state", CONFIG_TYPE_STRING); config_setting_set_string(setting, "CA"); setting = config_setting_add(group, "zip", CONFIG_TYPE_INT); config_setting_set_int(setting, 95110); array = config_setting_add(root, "numbers", CONFIG_TYPE_ARRAY); for(i = 0; i < 10; ++i) { setting = config_setting_add(array, NULL, CONFIG_TYPE_INT); config_setting_set_int(setting, 10 * i); } /* Write out the new configuration. */ if(! config_write_file(&cfg, output_file)) { fprintf(stderr, "Error while writing file.\n"); config_destroy(&cfg); return(EXIT_FAILURE); } fprintf(stderr, "New configuration successfully written to: %s\n", output_file); config_destroy(&cfg); return(EXIT_SUCCESS); }
void config_setting_copy_aggregate (config_setting_t *parent, const config_setting_t *src) { config_setting_t *newAgg; int i, n; newAgg = config_setting_add (parent, config_setting_name (src), config_setting_type (src)); if (newAgg == NULL) return; n = config_setting_length (src); for (i = 0; i < n; i++) { if (config_setting_is_group (src)) { config_setting_copy_simple (newAgg, config_setting_get_elem (src, i)); } else { config_setting_copy_elem (newAgg, config_setting_get_elem (src, i)); } } }
/******************************************************************************* 函数名称 : webauth_rdx_save_setting 功能描述 : 保存服务器配置 输入参数 : cli_rdx_server_conf_t * list 服务器配置列表 输出参数 : 无 返 回 值 : -------------------------------------------------------------------------------- 最近一次修改记录 : 修改作者 : 王群 修改目的 : 新添加函数 修改日期 : 2010-08-16 *******************************************************************************/ void webauth_rdx_save_setting (webauth_rdx_server_conf_t * list) { webauth_rdx_server_conf_t *server_cfg = NULL; config_t cfg; config_setting_t *server_set = NULL; config_setting_t *root_set = NULL; config_init (&cfg); root_set = config_root_setting (&cfg); for (server_cfg = list; NULL != server_cfg; server_cfg = server_cfg->next) { server_set = config_setting_add (root_set, server_cfg->name, CONFIG_TYPE_GROUP); webauth_rdx_build_server_setting (server_set, server_cfg); } config_write_file (&cfg, RADIUS_CFG_FILE); config_destroy (&cfg); return; }
gboolean get_verbose () { gboolean verbose; gchar *file; config_t config; file = g_strconcat (g_getenv ("HOME"), "/.config/gjackctl/gjackctl.conf", NULL); config_init (&config); config_read_file (&config, file); if (config_lookup_bool (&config, "gjackctl.server.verbose", &verbose) == CONFIG_FALSE) { config_setting_t *group; config_setting_t *setting; g_print ("Verbose config option not available\n"); g_print ("Creating config setting now...\n"); group = config_lookup (&config, "gjackctl.server"); setting = config_setting_add (group, "verbose", CONFIG_TYPE_BOOL); config_setting_set_bool (setting, FALSE); config_write_file (&config, file); } if (verbose == FALSE) { config_destroy (&config); return FALSE; } else { config_destroy (&config); return TRUE; } }
/** * Loads group configuration from config file into memory. * @private */ static void read_config(void) { config_setting_t *groups = NULL; const char *config_filename = "conf/groups.conf"; // FIXME hardcoded name int group_count = 0; if (conf_read_file(&pc_group_config, config_filename)) return; groups = config_lookup(&pc_group_config, "groups"); if (groups != NULL) { GroupSettings *group_settings = NULL; DBIterator *iter = NULL; int i, loop = 0; group_count = config_setting_length(groups); for (i = 0; i < group_count; ++i) { int id = 0, level = 0; const char *groupname = NULL; int log_commands = 0; config_setting_t *group = config_setting_get_elem(groups, i); if (!config_setting_lookup_int(group, "id", &id)) { ShowConfigWarning(group, "pc_groups:read_config: \"groups\" list member #%d has undefined id, removing...", i); config_setting_remove_elem(groups, i); --i; --group_count; continue; } if (id2group(id) != NULL) { ShowConfigWarning(group, "pc_groups:read_config: duplicate group id %d, removing...", i); config_setting_remove_elem(groups, i); --i; --group_count; continue; } config_setting_lookup_int(group, "level", &level); config_setting_lookup_bool(group, "log_commands", &log_commands); if (!config_setting_lookup_string(group, "name", &groupname)) { char temp[20]; config_setting_t *name = NULL; snprintf(temp, sizeof(temp), "Group %d", id); if ((name = config_setting_add(group, "name", CONFIG_TYPE_STRING)) == NULL || !config_setting_set_string(name, temp)) { ShowError("pc_groups:read_config: failed to set missing group name, id=%d, skipping... (%s:%d)\n", id, config_setting_source_file(group), config_setting_source_line(group)); continue; } config_setting_lookup_string(group, "name", &groupname); // Retrieve the pointer } if (name2group(groupname) != NULL) { ShowConfigWarning(group, "pc_groups:read_config: duplicate group name %s, removing...", groupname); config_setting_remove_elem(groups, i); --i; --group_count; continue; } CREATE(group_settings, GroupSettings, 1); group_settings->id = id; group_settings->level = level; group_settings->name = groupname; group_settings->log_commands = (bool)log_commands; group_settings->inherit = config_setting_get_member(group, "inherit"); group_settings->commands = config_setting_get_member(group, "commands"); group_settings->permissions = config_setting_get_member(group, "permissions"); group_settings->inheritance_done = false; group_settings->root = group; group_settings->group_pos = i; strdb_put(pc_groupname_db, groupname, group_settings); idb_put(pc_group_db, id, group_settings); } group_count = config_setting_length(groups); // Save number of groups // Check if all commands and permissions exist iter = db_iterator(pc_group_db); for (group_settings = dbi_first(iter); dbi_exists(iter); group_settings = dbi_next(iter)) { config_setting_t *commands = group_settings->commands, *permissions = group_settings->permissions; int count = 0, j; // Make sure there is "commands" group if (commands == NULL) commands = group_settings->commands = config_setting_add(group_settings->root, "commands", CONFIG_TYPE_GROUP); count = config_setting_length(commands); for (j = 0; j < count; ++j) { config_setting_t *command = config_setting_get_elem(commands, j); const char *name = config_setting_name(command); if (!atcommand_exists(name)) { ShowConfigWarning(command, "pc_groups:read_config: non-existent command name '%s', removing...", name); config_setting_remove(commands, name); --j; --count; } } // Make sure there is "permissions" group if (permissions == NULL) permissions = group_settings->permissions = config_setting_add(group_settings->root, "permissions", CONFIG_TYPE_GROUP); count = config_setting_length(permissions); for(j = 0; j < count; ++j) { config_setting_t *permission = config_setting_get_elem(permissions, j); const char *name = config_setting_name(permission); int p; ARR_FIND(0, ARRAYLENGTH(pc_g_permission_name), p, strcmp(pc_g_permission_name[p].name, name) == 0); if (p == ARRAYLENGTH(pc_g_permission_name)) { ShowConfigWarning(permission, "pc_groups:read_config: non-existent permission name '%s', removing...", name); config_setting_remove(permissions, name); --p; --count; } } } dbi_destroy(iter); // Apply inheritance i = 0; // counter for processed groups while (i < group_count) { iter = db_iterator(pc_group_db); for (group_settings = dbi_first(iter); dbi_exists(iter); group_settings = dbi_next(iter)) { config_setting_t *inherit = NULL, *commands = group_settings->commands, *permissions = group_settings->permissions; int j, inherit_count = 0, done = 0; if (group_settings->inheritance_done) // group already processed continue; if ((inherit = group_settings->inherit) == NULL || (inherit_count = config_setting_length(inherit)) <= 0) { // this group does not inherit from others ++i; group_settings->inheritance_done = true; continue; } for (j = 0; j < inherit_count; ++j) { GroupSettings *inherited_group = NULL; const char *groupname = config_setting_get_string_elem(inherit, j); if (groupname == NULL) { ShowConfigWarning(inherit, "pc_groups:read_config: \"inherit\" array member #%d is not a name, removing...", j); config_setting_remove_elem(inherit,j); continue; } if ((inherited_group = name2group(groupname)) == NULL) { ShowConfigWarning(inherit, "pc_groups:read_config: non-existent group name \"%s\", removing...", groupname); config_setting_remove_elem(inherit,j); continue; } if (!inherited_group->inheritance_done) continue; // we need to do that group first // Copy settings (commands/permissions) that are not defined yet if (inherited_group->commands != NULL) { int l = 0, commands_count = config_setting_length(inherited_group->commands); for (l = 0; l < commands_count; ++l) config_setting_copy(commands, config_setting_get_elem(inherited_group->commands, l)); } if (inherited_group->permissions != NULL) { int l = 0, permissions_count = config_setting_length(inherited_group->permissions); for (l = 0; l < permissions_count; ++l) config_setting_copy(permissions, config_setting_get_elem(inherited_group->permissions, l)); } ++done; // copied commands and permissions from one of inherited groups } if (done == inherit_count) { // copied commands from all of inherited groups ++i; group_settings->inheritance_done = true; // we're done with this group } } dbi_destroy(iter); if (++loop > group_count) { ShowWarning("pc_groups:read_config: Could not process inheritance rules, check your config '%s' for cycles...\n", config_filename); break; } } // while(i < group_count) // Pack permissions into GroupSettings.e_permissions for faster checking iter = db_iterator(pc_group_db); for (group_settings = dbi_first(iter); dbi_exists(iter); group_settings = dbi_next(iter)) { config_setting_t *permissions = group_settings->permissions; int c, count = config_setting_length(permissions); for (c = 0; c < count; ++c) { config_setting_t *perm = config_setting_get_elem(permissions, c); const char *name = config_setting_name(perm); int val = config_setting_get_bool(perm); int j; if (val == 0) // does not have this permission continue; ARR_FIND(0, ARRAYLENGTH(pc_g_permission_name), j, strcmp(pc_g_permission_name[j].name, name) == 0); group_settings->e_permissions |= pc_g_permission_name[j].permission; } } dbi_destroy(iter); } ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' groups in '"CL_WHITE"%s"CL_RESET"'.\n", group_count, config_filename); if( ( pc_group_max = group_count ) ) { DBIterator *iter = db_iterator(pc_group_db); GroupSettings *group_settings = NULL; int* group_ids = aMalloc( pc_group_max * sizeof(int) ); int i = 0; for (group_settings = dbi_first(iter); dbi_exists(iter); group_settings = dbi_next(iter)) { group_ids[i++] = group_settings->id; } atcommand_db_load_groups(group_ids); aFree(group_ids); dbi_destroy(iter); } }
int iteliec_soap_register (char *username, char *password) { herror_t err; SoapCtx *request; SoapCtx *response; config_t cfg; config_setting_t *setting, *root; const char *cfile; cfile = iteliec_config_file (); config_init (&cfg); /* Read the file. If there is an error, report it and exit. */ if (!config_read_file (&cfg, cfile)) { iteliec_log (ITELIEC_ERR, "Please ensure configuration file %s exists and is valid", cfile); //printf ("\n%s:%d - %s", config_error_file (&cfg), config_error_line (&cfg), config_error_text (&cfg)); config_destroy (&cfg); return; } root = config_root_setting (&cfg); /* Init */ err = soap_client_init_args (0, NULL); if (err != H_OK) { err_soap (err); return ITELIEC_ERR; } err = soap_ctx_new_with_method ("", "sendAuth", &request); if (err != H_OK) { err_soap (err); soap_client_destroy (); return ITELIEC_ERR; } /* Add login details */ soap_env_add_item (request->env, "xsd:string", "username", username); soap_env_add_item (request->env, "xsd:string", "password", password); /* Trade for response */ err = soap_client_invoke (request, &response, "http://api.iteliec.com/register/auth/ws/", ""); if (err != H_OK) { err_soap (err); soap_ctx_free (request); soap_client_destroy (); return ITELIEC_ERR; } /* Parse response */ auth_type* ret = parse_auth_response (response); if (ret->auth == true) { printf ("Success\n"); setting = config_setting_get_member (root, "api"); if(!setting) { setting = config_setting_add (root, "api", CONFIG_TYPE_GROUP); } setting = config_setting_add (setting, "hash", CONFIG_TYPE_STRING); config_setting_set_string (setting, ret->token); /* Write out the updated configuration. */ if(! config_write_file(&cfg, cfile)) { fprintf (stderr, "Error while writing file.\n"); config_destroy (&cfg); return (ITELIEC_OK); } printf("\nConfiguration file updated. Server is ready to run.\n"); } else { printf("Auth failed\n"); } /* Destroy */ soap_ctx_free (request); soap_ctx_free (response); soap_client_destroy (); config_destroy (&cfg); return ITELIEC_OK; }
/********************* return NULL on error *********************/ static char * __copy_group(const char * src_table, const char * src_file, const char * dst_table, const char * dst_file, const char * group_name, va_list ap) { const config_t * src_config = NULL; const config_t * dst_config = NULL; config_setting_t * src_setting = NULL; config_setting_t * dst_setting = NULL; char * path; char * full_path; char * new_group_name = NULL; const char * group_name_used = NULL; path = get_path(ap); full_path = add_entry_to_path(path, (char *)group_name); if(full_path == NULL) { return NULL; } SDL_LockMutex(entry_mutex); src_config = get_config(src_table,src_file); if(src_config==NULL) { SDL_UnlockMutex(entry_mutex); free(path); free(full_path); return NULL; } src_setting = config_lookup (src_config, full_path); if( src_setting == NULL ) { SDL_UnlockMutex(entry_mutex); free(path); free(full_path); return NULL; } dst_config = get_config(dst_table,dst_file); if(dst_config==NULL) { SDL_UnlockMutex(entry_mutex); free(path); free(full_path); return NULL; } dst_setting = config_lookup(dst_config, full_path); free(full_path); /* if the setting does not exist, create it */ if( dst_setting == NULL ) { group_name_used = group_name; } /* else find a new name for it */ else { new_group_name = __get_unused_group_on_path(dst_table, dst_file, path); group_name_used = new_group_name; } if( path != NULL ) { dst_setting = config_lookup(dst_config, path); } else { dst_setting = config_root_setting(dst_config); } dst_setting = config_setting_add(dst_setting,group_name_used,CONFIG_TYPE_GROUP); free(path); if(!entry_copy_config(src_setting,dst_setting)) { SDL_UnlockMutex(entry_mutex); return NULL; } SDL_UnlockMutex(entry_mutex); if(new_group_name != NULL ) { return new_group_name; } return strdup(group_name); }