logread: fix reconnect logd logic
[project/ubox.git] / validate / cli.c
index c0179e7..1fc7b08 100644 (file)
@@ -19,67 +19,177 @@ print_usage(char *argv)
        fprintf(stderr, "%s <package> <section_type> <section_name> 'option:datatype:default' 'option:datatype:default' ...\n", argv);
 }
 
-static char*
-bool_to_num(char *val)
+static const char *
+bool_to_num(const char *val)
 {
-       static char val0[] = "0";
-       static char val1[] = "1";
-       static char val_none[] = "";
+       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, "yes") || !strcmp(val, "enabled"))
+               return "1";
 
-       if (!strcmp(val, "0") || !strcmp(val, "off") || !strcmp(val, "false") || !strcmp(val, "disabled"))
-               return val0;
-       if (!strcmp(val, "1") || !strcmp(val, "on") || !strcmp(val, "true") || !strcmp(val, "enabled"))
-               return val1;
+       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)
+{
+       const char *p;
+
+       switch(type)
+       {
+       case DT_BOOL:
+               printf("%s", bool_to_num(val));
+               break;
+
+       case DT_STRING:
+               printf("'");
+
+               for (p = val; *p; p++)
+                       if (*p == '\'')
+                               printf("'\"'\"'");
+                       else
+                               printf("%c", *p);
+
+               printf("'");
+               break;
+
+       default:
+               printf("%s", val);
+               break;
+       }
+}
+
+static void
+export_value(enum dt_type type, const char *name, const char *val)
+{
+       if ((type == DT_INVALID) || !val || !*val)
+       {
+               printf("unset -v %s; ", name);
+               return;
+       }
+
+       printf("%s=", name);
+       escape_value(type, val);
+       printf("; ");
+}
+
+static int
+validate_value(struct uci_ptr *ptr, const char *expr, const char *def)
+{
+       int i = 0;
+       bool empty = true;
+       enum dt_type type = DT_INVALID;
+       struct uci_element *e;
+       struct uci_option *opt = NULL;
+
+       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 (empty)
+                               printf("%s=", ptr->option);
+                       else
+                               printf("\\ ");
+
+                       empty = false;
+                       type = dt_parse(expr, e->name);
+
+                       if (type != DT_INVALID)
+                               escape_value(type, e->name);
+
+                       fprintf(stderr, "%s.%s.%s[%u]=%s validates as %s with %s\n",
+                               ptr->package, ptr->section, ptr->option, i++, e->name,
+                               expr, type ? "true" : "false");
+               }
+
+               if (!empty)
+                       printf("; ");
+       }
+       else if (opt && opt->v.string && *opt->v.string)
+       {
+               empty = false;
+               type = dt_parse(expr, opt->v.string);
+               export_value(type, ptr->option, opt->v.string);
+
+               fprintf(stderr, "%s.%s.%s=%s validates as %s with %s\n",
+                               ptr->package, ptr->section, ptr->option, opt->v.string,
+                       expr, type ? "true" : "false");
+       }
 
-       return val_none;
+       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 int
 validate_option(struct uci_context *ctx, char *package, char *section, char *option)
 {
-       char *datatype = strstr(option, ":");
-       struct uci_ptr ptr = { 0 };
-       char *val;
-       int ret = 0;
+       char *opt, *expr, *def;
+       struct uci_ptr ptr = {};
 
-       if (!datatype) {
+       if (!parse_tuple(option, &opt, &expr, &def))
+       {
                fprintf(stderr, "%s is not a valid option\n", option);
                return -1;
        }
 
-       *datatype = '\0';
-       datatype++;
-       val = strstr(datatype, ":");
-       if (val) {
-               *val = '\0';
-               val++;
-       }
-
        ptr.package = package;
        ptr.section = section;
-       ptr.option = option;
-
-       if (!uci_lookup_ptr(ctx, &ptr, NULL, false))
-               if (ptr.flags & UCI_LOOKUP_COMPLETE)
-                       if (ptr.last->type == UCI_TYPE_OPTION)
-                               if ( ptr.o->type == UCI_TYPE_STRING)
-                                       if (ptr.o->v.string)
-                                               val = ptr.o->v.string;
-
-       if (val) {
-               ret = dt_parse(datatype, val);
-               fprintf(stderr, "%s.%s.%s=%s validates as %s with %s\n", package, section, option,
-                       val, datatype, ret ? "true" : "false");
-       }
+       ptr.option = opt;
 
-       if (ret && !strcmp(datatype, "bool"))
-               printf("%s=%s; ", option, bool_to_num(val));
-       else if (ret)
-               printf("%s=%s; ", option, val);
-       else
-               printf("unset -v %s; ", option);
+       uci_lookup_ptr(ctx, &ptr, NULL, false);
 
-       return ret;
+       return validate_value(&ptr, expr, def);
 }
 
 int
@@ -87,9 +197,10 @@ main(int argc, char **argv)
 {
        struct uci_context *ctx;
        struct uci_package *package;
+       char *opt, *expr, *def;
        int len = argc - 4;
-       bool rv;
-       int i;
+       enum dt_type rv;
+       int i, rc;
 
        if (argc == 3) {
                rv = dt_parse(argv[1], argv[2]);
@@ -107,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; ");
 
@@ -132,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;
 }