static void rpc_set(rpc_t* rpc, void* c) { str group, var; int i, err; char *ch; unsigned int *group_id; if (rpc->scan(c, "SS", &group, &var) < 2) return; if (get_group_id(&group, &group_id)) { rpc->fault(c, 400, "Wrong group syntax. Use either \"group\", or \"group[id]\""); return; } if (rpc->scan(c, "d", &i) == 1) err = cfg_set_now_int(ctx, &group, group_id, &var, i); else if (rpc->scan(c, "s", &ch) == 1) err = cfg_set_now_string(ctx, &group, group_id, &var, ch); else return; /* error */ if (err) { rpc->fault(c, 400, "Failed to set the variable"); return; } }
static void rpc_set_now_int(rpc_t* rpc, void* c) { str group, var; int i; unsigned int *group_id; if (rpc->scan(c, "SSd", &group, &var, &i) < 3) return; if (get_group_id(&group, &group_id)) { rpc->fault(c, 400, "Wrong group syntax. Use either \"group\", or \"group[id]\""); return; } if (cfg_set_now_int(ctx, &group, group_id, &var, i)) { rpc->fault(c, 400, "Failed to set the variable"); return; } }
/* translate name using translation table, returns 0..not found, 1..success, -1..error */ static int find_cfg_var(str *group_name, char *def_name, db_res_t *transl_res) { db_rec_t *transl_rec; int ret = -1; DBG(MODULE_NAME": find_cfg_var('%.*s', '%s', ...)\n", group_name->len, group_name->s, def_name); transl_rec = db_first(transl_res); /* iterate through each candidate where cfg def may be found */ while (transl_rec) { static db_cmd_t* cmd; db_rec_t *rec; db_res_t *res; db_fld_t params[3], cols[2]; memset(cols, 0, sizeof(cols)); cols[0].name = GETCSTR(transl_rec->fld[3], def_cfg_table_value_field); cols[0].type = DB_NONE; memset(params, 0, sizeof(params)); params[0].name = GETCSTR(transl_rec->fld[1], def_cfg_table_group_name_field); params[0].type = DB_STR; params[0].op = DB_EQ; params[1].name = GETCSTR(transl_rec->fld[2], def_cfg_table_name_field); params[1].type = DB_CSTR; params[1].op = DB_EQ; DBG(MODULE_NAME": exec_transl: looking in '%s'\n", GETCSTR(transl_rec->fld[0], def_cfg_table)); cmd = db_cmd(DB_GET, db_cntx, GETCSTR(transl_rec->fld[0], def_cfg_table), cols, params, NULL); if (!cmd) { ERR(MODULE_NAME": Error preparing query '%s'\n", transl_tbl); return -1; } cmd->match[0].flags &= ~DB_NULL; cmd->match[0].v.lstr = *group_name; cmd->match[1].flags &= ~DB_NULL; cmd->match[1].v.cstr = def_name; // FIXME: proprietary code! db_setopt(cmd, "key", "bySerGroup"); db_setopt(cmd, "key_omit", 0); if (db_exec(&res, cmd) < 0) { ERR(MODULE_NAME": Error executing query '%s'\n", transl_tbl); db_cmd_free(cmd); return -1; } rec = db_first(res); if (rec) { /* var found in config table */ str def_name_s; def_name_s.s = def_name; def_name_s.len = strlen(def_name); DBG(MODULE_NAME": exec_transl: found record, type:%d\n", rec->fld[0].type); /* read and set cfg var */ switch (rec->fld[0].type) { case DB_STR: if (cfg_set_now(cfg_ctx, group_name, NULL /* group id */, &def_name_s, &rec->fld[0].v.lstr, CFG_VAR_STR) < 0) goto err; break; case DB_CSTR: if (cfg_set_now_string(cfg_ctx, group_name, NULL /* group id */, &def_name_s, rec->fld[0].v.cstr) < 0) goto err; break; case DB_INT: if (cfg_set_now_int(cfg_ctx, group_name, NULL /* group id */, &def_name_s, rec->fld[0].v.int4) < 0) goto err; break; default: ERR(MODULE_NAME": unexpected field type (%d), table:'%s', field:'%s'\n", rec->fld[0].type, GETCSTR(transl_rec->fld[0], def_cfg_table), GETCSTR(transl_rec->fld[3], def_cfg_table_value_field) ); goto err; } ret = 1; err: db_res_free(res); db_cmd_free(cmd); return ret; } db_res_free(res); db_cmd_free(cmd); transl_rec = db_next(transl_res); } return 0; }