X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fubox.git;a=blobdiff_plain;f=validate%2Fcli.c;h=f03c8a16f38dfb276e1633b5d1a21da876ebad59;hp=97978a036bb6e97726d0056f226b49e0e8f2a1ff;hb=8488bb5bfdaf6ab4771622c57a62aad5fd491f06;hpb=b7d977e9800692765445c7806d05d87046c34cb6;ds=sidebyside diff --git a/validate/cli.c b/validate/cli.c index 97978a0..f03c8a1 100644 --- a/validate/cli.c +++ b/validate/cli.c @@ -22,14 +22,46 @@ print_usage(char *argv) static const char * bool_to_num(const char *val) { - if (!strcmp(val, "0") || !strcmp(val, "off") || !strcmp(val, "false") || !strcmp(val, "disabled")) + if (!strcmp(val, "0") || !strcmp(val, "off") || !strcmp(val, "false") || !strcmp(val, "no") || !strcmp(val, "disabled")) return "0"; - if (!strcmp(val, "1") || !strcmp(val, "on") || !strcmp(val, "true") || !strcmp(val, "enabled")) + if (!strcmp(val, "1") || !strcmp(val, "on") || !strcmp(val, "true") || !strcmp(val, "yes") || !strcmp(val, "enabled")) return "1"; return ""; } +static bool +parse_tuple(char *tuple, char **option, char **expr, char **def) +{ + char *p; + bool esc; + + for (esc = false, p = *option = tuple, *expr = NULL, *def = NULL; *p; p++) + { + if (!esc && *p == '\\') + { + esc = true; + continue; + } + + if (!esc && *p == ':') + { + *p++ = 0; + + if (!*expr) + *expr = p; + else if (!*def) + *def = p; + else + break; + } + + esc = false; + } + + return (*expr != NULL); +} + static void escape_value(enum dt_type type, const char *val) { @@ -73,43 +105,32 @@ export_value(enum dt_type type, const char *name, const char *val) printf("; "); } -static void +static int validate_value(struct uci_ptr *ptr, const char *expr, const char *def) { int i = 0; - bool empty = true, first = true; - enum dt_type type; + bool empty = true; + enum dt_type type = DT_INVALID; struct uci_element *e; - struct uci_option *opt = ptr->o; + struct uci_option *opt = NULL; - if (opt->type == UCI_TYPE_LIST) - { - uci_foreach_element(&opt->v.list, e) - { - if (!e->name || !*e->name) - continue; - - empty = false; - break; - } - - if (empty) - { - export_value(DT_STRING, ptr->option, def); - return; - } + if ((ptr->flags & UCI_LOOKUP_COMPLETE) && + (ptr->last->type == UCI_TYPE_OPTION)) + opt = ptr->o; + if (opt && opt->type == UCI_TYPE_LIST) + { uci_foreach_element(&opt->v.list, e) { if (!e->name || !*e->name) continue; - if (first) + if (empty) printf("%s=", ptr->option); else printf("\\ "); - first = false; + empty = false; type = dt_parse(expr, e->name); if (type != DT_INVALID) @@ -120,16 +141,12 @@ validate_value(struct uci_ptr *ptr, const char *expr, const char *def) expr, type ? "true" : "false"); } - printf("; "); + if (!empty) + printf("; "); } - else + else if (opt && opt->v.string && *opt->v.string) { - if (!opt->v.string || !*opt->v.string) - { - export_value(DT_STRING, ptr->option, def); - return; - } - + empty = false; type = dt_parse(expr, opt->v.string); export_value(type, ptr->option, opt->v.string); @@ -137,38 +154,42 @@ validate_value(struct uci_ptr *ptr, const char *expr, const char *def) ptr->package, ptr->section, ptr->option, opt->v.string, expr, type ? "true" : "false"); } + + if (empty) + { + type = dt_parse(expr, def); + + if (type == DT_INVALID) + type = DT_STRING; + + export_value(type, ptr->option, def); + + fprintf(stderr, "%s.%s.%s is unset and defaults to %s %s\n", + ptr->package, ptr->section, ptr->option, expr, def); + } + + return type ? 0 : -1; } -static void +static int validate_option(struct uci_context *ctx, char *package, char *section, char *option) { - char *def, *expr; + char *opt, *expr, *def; struct uci_ptr ptr = { 0 }; - if ((expr = strchr(option, ':')) == NULL) + if (!parse_tuple(option, &opt, &expr, &def)) { fprintf(stderr, "%s is not a valid option\n", option); - return; + return -1; } - *expr++ = 0; - - if ((def = strrchr(expr, ':')) != NULL) - *def++ = 0; - ptr.package = package; ptr.section = section; - ptr.option = option; + ptr.option = opt; - if (uci_lookup_ptr(ctx, &ptr, NULL, false) || - !(ptr.flags & UCI_LOOKUP_COMPLETE) || - (ptr.last->type != UCI_TYPE_OPTION)) - { - export_value(DT_STRING, option, def); - return; - } + uci_lookup_ptr(ctx, &ptr, NULL, false); - validate_value(&ptr, expr, def); + return validate_value(&ptr, expr, def); } int @@ -176,9 +197,10 @@ main(int argc, char **argv) { struct uci_context *ctx; struct uci_package *package; + char *opt, *expr, *def; int len = argc - 4; enum dt_type rv; - int i; + int i, rc; if (argc == 3) { rv = dt_parse(argv[1], argv[2]); @@ -196,18 +218,12 @@ main(int argc, char **argv) printf("json_add_object \"data\"; "); for (i = 0; i < len; i++) { - char *datatype = strstr(argv[4 + i], ":"); - char *def; - - if (!datatype) + if (!parse_tuple(argv[4 + i], &opt, &expr, &def)) continue; - *datatype = '\0'; - datatype++; - def = strstr(datatype, ":"); - if (def) - *def = '\0'; - printf("json_add_string \"%s\" \"%s\"; ", argv[4 + i], datatype); + + printf("json_add_string \"%s\" \"%s\"; ", opt, expr); } + printf("json_close_object; "); printf("json_close_object; "); @@ -221,8 +237,12 @@ main(int argc, char **argv) if (uci_load(ctx, argv[1], &package)) return -1; - for (i = 0; i < len; i++) - validate_option(ctx, argv[1], argv[3], argv[4 + i]); + rc = 0; + for (i = 0; i < len; i++) { + if (validate_option(ctx, argv[1], argv[3], argv[4 + i])) { + rc = -1; + } + } - return 0; + return rc; }