/** Parse config file part passed as unitype object * @retval 0 success * @retval 1 error */ static int parse_config_ut(struct mg *mg, ut *cfg) { thash *t; char *key, *key2; ut *subcfg; int num1, num2; t = ut_thash(cfg); /* parse global config */ thash_iter_loop(t, key, subcfg) { if (isdigit(key[0])) continue; if (streq(key, "id")) { mg->options.myid = ut_int(subcfg); } else if (streq(key, "stats")) { mg->options.stats = ut_int(subcfg); } else if (streq(key, "sync")) { mg->options.sync = ut_int(subcfg); } else if (streq(key, "root")) { mg->options.stats_root = ut_char(subcfg); } else if (streq(key, "session")) { mg->options.stats_sess = ut_char(subcfg); } else if (streq(key, "dump")) { mg->options.dump = ut_bool(subcfg); } else if (streq(key, "dump-size")) { mg->options.dumpsize = ut_int(subcfg); } else if (streq(key, "dump-beacons")) { mg->options.dumpb = ut_bool(subcfg); } else if (streq(key, "svc-ifname")) { mg->options.svc_ifname = ut_char(subcfg); } else { dbg(0, "unrecognized configuration file option: %s\n", key); return 1; } } /* parse per-node config */ thash_iter_loop(t, key, subcfg) { if (!(isdigit(key[0]) && ut_type(subcfg) == T_HASH)) continue; /* parse list consisting of ranges and lists of ids */ while (isdigit(key[0])) { num1 = strtol(key, &key2, 10); if (key2[0] == '-') { /* range */ num2 = strtol(key2+1, &key, 10); key++; } else if (key2[0] == ',') { /* list */ num2 = 0; key = key2 + 1; } else { /* single */ num2 = 0; key = key2; } /* if its for me... */ if (mg->options.myid == num1 || (num2 > 0 && (mg->options.myid >= num1 && mg->options.myid <= num2))) { parse_config_ut(mg, subcfg); } } } return 0; }
/* this probably needs a wise rewrite */ static char *fill_query(struct req *req, char *orig_query, tlist *data) { int i, qs; enum fq_state { NORMAL, INQ } state = NORMAL; xstr *query; MYSQL *conn; ut *arg, *el, *el2; tlist *list, *list2; bool atleastone, atleastone2; #define iskeyw(a) (sizeof(a) == i - qs && strncmp((a), orig_query + qs + 1, sizeof(a) - 1) == 0) /* XXX: uses list and el */ #define appendlist(utlist) do { \ atleastone = false; \ xstr_append(query, "("); \ list = ut_tlist(utlist); \ TLIST_ITER_LOOP(list, el) { \ if (atleastone) \ xstr_append_char(query, ','); \ xstr_append(query, \ pb("\"%s\"", escape(conn, ut_xstr(el)))); \ atleastone = true; \ } \ xstr_append(query, ")"); \ } while(0); conn = uthp_ptr(req->prv, "sqler", "conn"); query = xstr_create("", req); tlist_reset(data); for (i = 0; orig_query[i]; i++) { switch (state) { case NORMAL: if (orig_query[i] == '?') { qs = i; state = INQ; } else { xstr_append_char(query, orig_query[i]); } break; case INQ: if (orig_query[i] == '?') { if (iskeyw("login")) { xstr_append(query, pb("\"%s\"", uthp_char(req->prv, "sqler", "login"))); } else if (iskeyw("role")) { xstr_append(query, pb("\"%s\"", uthp_char(req->prv, "sqler", "role"))); } else { /* probably needs an arg */ arg = tlist_iter(data); if (arg) { if (iskeyw("int")) { xstr_append(query, pb("%d", ut_int(arg))); } else if (iskeyw("str")) { xstr_append(query, pb("\"%s\"", escape(conn, ut_xstr(arg)))); } else if (iskeyw("dbl")) { xstr_append(query, pb("%g", ut_double(arg))); } else if (iskeyw("login")) { xstr_append(query, pb("\"%s\"", uthp_char(req->prv, "sqler", "login"))); } else if (iskeyw("role")) { xstr_append(query, pb("\"%s\"", uthp_char(req->prv, "sqler", "role"))); } else if (iskeyw("array")) { appendlist(arg); } else if (iskeyw("arrays")) { atleastone2 = false; list2 = ut_tlist(arg); TLIST_ITER_LOOP(list2, el2) { if (atleastone2) xstr_append_char(query, ','); appendlist(el2); atleastone2 = true; } } /* XXX: arg eaten by unrecognizible substitution */ } } state = NORMAL; } else if (orig_query[i] < 'a' || orig_query[i] > 'z') { rollback: while (qs <= i) xstr_append_char(query, orig_query[qs++]); state = NORMAL; } break; }