+ 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");
+ }