/** * @brief Run all of the requirements in a reqlist * @param fctx the context to use (argc and argv will be updated in it) * @param reqlist the list * @param req the function to execute (currently only req_requirement_met) * @return 0 if ok */ int req_apply_reqlist(struct fun_context *fctx, cfg_opt_t *reqlist, int (*req)(struct fun_context *fctx)) { int ix = 0; char *aritystr; while ((aritystr = cfg_opt_getnstr(reqlist, ix++)) != NULL) { fctx->argc = strtoul(aritystr, NULL, 0); if (fctx->argc <= 0 || fctx->argc > FUN_MAX_ARGS) { set_last_error("Unexpected argc value in reqlist"); return -1; } int i; for (i = 0; i < fctx->argc; i++) { fctx->argv[i] = cfg_opt_getnstr(reqlist, ix++); if (fctx->argv[i] == NULL) { set_last_error("Unexpected error with reqlist"); return -1; } } // Clear out the rest of the argv entries to avoid confusion when debugging. for (; i < FUN_MAX_ARGS; i++) fctx->argv[i] = 0; if (req(fctx) < 0) return -1; } return 0; }
DLLIMPORT void cfg_opt_nprint_var(cfg_opt_t *opt, unsigned int index, FILE *fp) { const char *str; assert(opt && fp); switch(opt->type) { case CFGT_INT: fprintf(fp, "%ld", cfg_opt_getnint(opt, index)); break; case CFGT_FLOAT: fprintf(fp, "%lf", cfg_opt_getnfloat(opt, index)); break; case CFGT_STR: str = cfg_opt_getnstr(opt, index); fprintf(fp, "\"%s\"", str ? str : ""); break; case CFGT_BOOL: fprintf(fp, "%s", cfg_opt_getnbool(opt, index) ? "true" : "false"); break; case CFGT_NONE: case CFGT_SEC: case CFGT_FUNC: case CFGT_PTR: break; } }
static int cfg_validate_hostname(cfg_t *cfg, cfg_opt_t *opt) { const char *host = cfg_opt_getnstr(opt, cfg_opt_size(opt) - 1); if (strspn(host, "qwertyuiopasdfghjklzxcvbnm.-_1234567890QWERTYUIOPASDFGHJKLZXCVBNM") != strlen(host)) { cfg_error(cfg, "Invalid %s: %s", opt->name, host); return -1; } return 0; }
int validate_action(cfg_t *cfg, cfg_opt_t *opt) { cfg_opt_t *name_opt; cfg_t *action_sec = cfg_opt_getnsec(opt, 0); fail_unless(action_sec != 0); name_opt = cfg_getopt(action_sec, "name"); fail_unless(name_opt != 0); fail_unless(cfg_opt_size(name_opt) == 1); if (cfg_opt_getnstr(name_opt, 0) == NULL) { /* cfg_error(cfg, "missing required option 'name' in section %s", opt->name); */ return 1; } return 0; }
int validate_ip(cfg_t *cfg, cfg_opt_t *opt) { unsigned int i, j; for (i = 0; i < cfg_opt_size(opt); i++) { unsigned int v[4]; char *ip = cfg_opt_getnstr(opt, i); if (sscanf(ip, "%u.%u.%u.%u", v + 0, v + 1, v + 2, v + 3) != 4) { /* cfg_error(cfg, "invalid IP address %s in section %s", ip, cfg->name); */ return 1; } for (j = 0; j < 4; j++) { if (v[j] > 0xff) { return 1; } } } return 0; }
static void fwup_cfg_opt_nprint_var(cfg_opt_t *opt, unsigned int index, struct simple_string *s) { switch (opt->type) { case CFGT_INT: ssprintf(s, "%ld", cfg_opt_getnint(opt, index)); break; case CFGT_FLOAT: ssprintf(s, "%f", cfg_opt_getnfloat(opt, index)); break; case CFGT_STR: { const char *str = cfg_opt_getnstr(opt, index); ssprintf(s, "\""); while (str && *str) { if (*str == '"') ssprintf(s, "\\\""); else if (*str == '\\') ssprintf(s, "\\\\"); else ssprintf(s, "%c", *str); str++; } ssprintf(s, "\""); break; } case CFGT_BOOL: ssprintf(s, "%s", cfg_opt_getnbool(opt, index) ? "true" : "false"); break; case CFGT_NONE: case CFGT_SEC: case CFGT_FUNC: case CFGT_PTR: break; } }
DLLIMPORT void cfg_opt_nprint_var(cfg_opt_t *opt, unsigned int index, FILE *fp) { const char *str; assert(opt && fp); switch(opt->type) { case CFGT_INT: fprintf(fp, "%ld", cfg_opt_getnint(opt, index)); break; case CFGT_FLOAT: fprintf(fp, "%lf", cfg_opt_getnfloat(opt, index)); break; case CFGT_STR: str = cfg_opt_getnstr(opt, index); fprintf(fp, "\""); while (str && *str) { if(*str == '"') fprintf(fp, "\\\""); else if(*str == '\\') fprintf(fp, "\\\\"); else fprintf(fp, "%c", *str); str++; } fprintf(fp, "\""); break; case CFGT_BOOL: fprintf(fp, "%s", cfg_opt_getnbool(opt, index) ? "true" : "false"); break; case CFGT_NONE: case CFGT_SEC: case CFGT_FUNC: case CFGT_PTR: break; } }
DLLIMPORT char *cfg_getnstr(cfg_t *cfg, const char *name, unsigned int index) { return cfg_opt_getnstr(cfg_getopt(cfg, name), index); }