/* return: -1: invalid, 0: valid */ int cb_validate_svcinfo(cfg_t *cfg, cfg_opt_t *opt) { char *name; /* only validate the last svcinfo */ cfg_t *sec = cfg_opt_getnsec(opt, cfg_opt_size(opt) - 1); if(!sec) { cfg_error(cfg, "section is NULL?!"); return -1; } if (cfg_title(sec) == NULL) { cfg_error(cfg, "every svcinfo must have a name, but %d seems not", cfg_opt_size(opt) - 1); return -1; } name = cfg_getstr(sec, "name"); if(name == 0 || strlen(name) >= MAX_SVC_NAME_LEN) { cfg_error(cfg, "name option must be set for svcinfo '%s'" " or strlen(name)(%zd) >= len(%u)", cfg_title(sec), strlen(name), MAX_SVC_NAME_LEN); return -1; } if(cfg_getint(sec, "gameid") <= 0) { cfg_error(cfg, "gameid option must be set for svcinfo '%s'", cfg_title(sec)); return -1; } return 0; }
DLLIMPORT cfg_t *cfg_opt_gettsec(cfg_opt_t *opt, const char *title) { unsigned int i, n; assert(opt && title); if(!is_set(CFGF_TITLE, opt->flags)) return 0; n = cfg_opt_size(opt); for(i = 0; i < n; i++) { cfg_t *sec = cfg_opt_getnsec(opt, i); assert(sec && sec->title); if(is_set(CFGF_NOCASE, opt->flags)) { if(strcasecmp(title, sec->title) == 0) return sec; } else { if(strcmp(title, sec->title) == 0) return sec; } } return 0; }
/* validates a bookmark section (host option required) */ int conf_validate_bookmark(cfg_t *cfg, cfg_opt_t *opt) { cfg_t *bookmark = cfg_opt_getnsec(opt, cfg_opt_size(opt) - 1); if(cfg_size(bookmark, "host") == 0) { cfg_error(cfg, "missing required option 'host' in bookmark"); return -1; } return 0; }
static int cfg_validate_port(cfg_t *cfg, cfg_opt_t *opt) { unsigned int port = cfg_opt_getnint(opt, cfg_opt_size(opt) - 1); if (port > USHRT_MAX) { cfg_error(cfg, "Invalid %s: %s", opt->name, port); return -1; } return 0; }
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_unsigned_int(cfg_t *cfg, cfg_opt_t *opt) { int value = cfg_opt_getnint(opt, cfg_opt_size(opt) - 1); if(value < 0) { cfg_error(cfg, "integer option '%s' must be positive in section '%s'", opt->name, cfg->name); return -1; } return 0; }
static int validate_require_nonnegative(cfg_t *cfg, cfg_opt_t *opt) { if (opt->type == CFGT_INT) { long int value = cfg_opt_getnint(opt, cfg_opt_size(opt) - 1); if (value < 0) { cfg_error(cfg, "Value for option %s can't be negative in %s section \"%s\"", opt->name, cfg->name, cfg_title(cfg)); return -1; } } else if (opt->type == CFGT_FLOAT) { double value = cfg_opt_getnfloat(opt, cfg_opt_size(opt) - 1); if (value < 0.0) { cfg_error(cfg, "Value for option %s can't be negative in %s section \"%s\"", opt->name, cfg->name, cfg_title(cfg)); return -1; } } return 0; }
int validate_speed(cfg_t *cfg, cfg_opt_t *opt) { unsigned int i; for (i = 0; i < cfg_opt_size(opt); i++) { if (cfg_opt_getnint(opt, i) <= 0) { /* cfg_error(cfg, "speed must be positive in section %s", cfg->name); */ return 1; } } return 0; }
int cb_validate_bookmark(cfg_t *cfg, cfg_opt_t *opt) { /* only validate the last bookmark */ cfg_t *sec = cfg_opt_getnsec(opt, cfg_opt_size(opt) - 1); if(!sec) { cfg_error(cfg, "section is NULL!?"); return -1; } if(cfg_getstr(sec, "machine") == 0) { cfg_error(cfg, "machine option must be set for bookmark '%s'", cfg_title(sec)); 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; }
DLLIMPORT void cfg_opt_rmnsec(cfg_opt_t *opt, unsigned int index) { unsigned int n; cfg_value_t *val; assert(opt && opt->type == CFGT_SEC); n = cfg_opt_size(opt); if (index >= n) return; val = cfg_opt_getval(opt, index); if (index + 1 != n) { /* not removing last, move the tail */ memmove(&opt->values[index], &opt->values[index + 1], sizeof(opt->values[index]) * (n - index - 1)); } --opt->nvalues; val->section->path = 0; cfg_free(val->section); free(val); }
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; }
DLLIMPORT unsigned int cfg_size(cfg_t *cfg, const char *name) { return cfg_opt_size(cfg_getopt(cfg, name)); }
DLLIMPORT void cfg_opt_print_indent(cfg_opt_t *opt, FILE *fp, int indent) { assert(opt && fp); if(opt->type == CFGT_SEC) { cfg_t *sec; unsigned int i; for(i = 0; i < cfg_opt_size(opt); i++) { sec = cfg_opt_getnsec(opt, i); cfg_indent(fp, indent); if(is_set(CFGF_TITLE, opt->flags)) fprintf(fp, "%s \"%s\" {\n", opt->name, cfg_title(sec)); else fprintf(fp, "%s {\n", opt->name); cfg_print_indent(sec, fp, indent + 1); cfg_indent(fp, indent); fprintf(fp, "}\n"); } } else if(opt->type != CFGT_FUNC && opt->type != CFGT_NONE) { if(is_set(CFGF_LIST, opt->flags)) { unsigned int i; cfg_indent(fp, indent); fprintf(fp, "%s = {", opt->name); if(opt->nvalues) { if(opt->pf) opt->pf(opt, 0, fp); else cfg_opt_nprint_var(opt, 0, fp); for(i = 1; i < opt->nvalues; i++) { fprintf(fp, ", "); if(opt->pf) opt->pf(opt, i, fp); else cfg_opt_nprint_var(opt, i, fp); } } fprintf(fp, "}"); } else { cfg_indent(fp, indent); /* comment out the option if is not set */ if(opt->simple_value) { if(opt->type == CFGT_STR && *((char **)opt->simple_value) == 0) fprintf(fp, "# "); } else { if(cfg_opt_size(opt) == 0 || ( opt->type == CFGT_STR && (opt->values[0]->string == 0 || opt->values[0]->string[0] == 0))) fprintf(fp, "# "); } fprintf(fp, "%s = ", opt->name); if(opt->pf) opt->pf(opt, 0, fp); else cfg_opt_nprint_var(opt, 0, fp); } fprintf(fp, "\n"); } else if(opt->pf) { cfg_indent(fp, indent); opt->pf(opt, 0, fp); fprintf(fp, "\n"); } }
void fwup_cfg_opt_to_string(cfg_opt_t *opt, struct simple_string *s) { if (!opt) return; if (opt->type == CFGT_SEC) { for (unsigned int i = 0; i < cfg_opt_size(opt); i++) { ptrdiff_t section_start_offset = s->p - s->str; cfg_t *sec = cfg_opt_getnsec(opt, i); if (is_set(CFGF_TITLE, opt->flags)) ssprintf(s, "%s \"%s\" {\n", opt->name, cfg_title(sec)); else ssprintf(s, "%s {\n", opt->name); ptrdiff_t before_offset = s->p - s->str; fwup_cfg_print(sec, s); if (s->p - s->str == before_offset && strcmp(opt->name, "task") != 0) { // Section was empty, so rewind output string. s->p = s->str + section_start_offset; } else { // Non-empty section or a "task", so close out. ssprintf(s, "}\n"); } } } else if (opt->type != CFGT_FUNC && opt->type != CFGT_NONE) { if (is_set(CFGF_LIST, opt->flags) && opt->nvalues) { ssprintf(s, "%s = {", opt->name); fwup_cfg_opt_nprint_var(opt, 0, s); for (unsigned int i = 1; i < opt->nvalues; i++) { ssprintf(s, ", "); fwup_cfg_opt_nprint_var(opt, i, s); } ssprintf(s, "}\n"); } else { // if not set, don't print if (opt->simple_value.ptr) { if (opt->type == CFGT_STR && *opt->simple_value.string == 0) return; } else { if (cfg_opt_size(opt) == 0 || (opt->type == CFGT_STR && (opt->values[0]->string == 0 || opt->values[0]->string[0] == 0))) return; } // Don't print out defaults. if (cfg_is_default(opt)) return; // Don't print assertions if (strncmp("assert-", opt->name, 7) == 0) return; // Skip host-path (see note in top comment) if (strcmp("host-path", opt->name) == 0 || strcmp("bootstrap-code-host-path", opt->name) == 0) return; ssprintf(s, "%s=", opt->name); fwup_cfg_opt_nprint_var(opt, 0, s); ssprintf(s, "\n"); } } }