8 #include <netinet/ether.h>
13 #include "libvalidate.h"
16 print_usage(char *argv
)
18 fprintf(stderr
, "%s <datatype> <value>\t- validate a value against a type\n", argv
);
19 fprintf(stderr
, "%s <package> <section_type> <section_name> 'option:datatype:default' 'option:datatype:default' ...\n", argv
);
23 bool_to_num(const char *val
)
25 if (!strcmp(val
, "0") || !strcmp(val
, "off") || !strcmp(val
, "false") || !strcmp(val
, "disabled"))
27 if (!strcmp(val
, "1") || !strcmp(val
, "on") || !strcmp(val
, "true") || !strcmp(val
, "enabled"))
34 parse_tuple(char *tuple
, char **option
, char **expr
, char **def
)
39 for (esc
= false, p
= *option
= tuple
, *expr
= NULL
, *def
= NULL
; *p
; p
++)
41 if (!esc
&& *p
== '\\')
47 if (!esc
&& *p
== ':')
62 return (*expr
!= NULL
);
66 escape_value(enum dt_type type
, const char *val
)
73 printf("%s", bool_to_num(val
));
79 for (p
= val
; *p
; p
++)
95 export_value(enum dt_type type
, const char *name
, const char *val
)
97 if ((type
== DT_INVALID
) || !val
|| !*val
)
99 printf("unset -v %s; ", name
);
104 escape_value(type
, val
);
109 validate_value(struct uci_ptr
*ptr
, const char *expr
, const char *def
)
112 bool empty
= true, first
= true;
114 struct uci_element
*e
;
115 struct uci_option
*opt
= ptr
->o
;
117 if (opt
->type
== UCI_TYPE_LIST
)
119 uci_foreach_element(&opt
->v
.list
, e
)
121 if (!e
->name
|| !*e
->name
)
130 export_value(DT_STRING
, ptr
->option
, def
);
134 uci_foreach_element(&opt
->v
.list
, e
)
136 if (!e
->name
|| !*e
->name
)
140 printf("%s=", ptr
->option
);
145 type
= dt_parse(expr
, e
->name
);
147 if (type
!= DT_INVALID
)
148 escape_value(type
, e
->name
);
150 fprintf(stderr
, "%s.%s.%s[%u]=%s validates as %s with %s\n",
151 ptr
->package
, ptr
->section
, ptr
->option
, i
++, e
->name
,
152 expr
, type
? "true" : "false");
159 if (!opt
->v
.string
|| !*opt
->v
.string
)
161 export_value(DT_STRING
, ptr
->option
, def
);
165 type
= dt_parse(expr
, opt
->v
.string
);
166 export_value(type
, ptr
->option
, opt
->v
.string
);
168 fprintf(stderr
, "%s.%s.%s=%s validates as %s with %s\n",
169 ptr
->package
, ptr
->section
, ptr
->option
, opt
->v
.string
,
170 expr
, type
? "true" : "false");
175 validate_option(struct uci_context
*ctx
, char *package
, char *section
, char *option
)
177 char *opt
, *expr
, *def
;
178 struct uci_ptr ptr
= { 0 };
180 if (!parse_tuple(option
, &opt
, &expr
, &def
))
182 fprintf(stderr
, "%s is not a valid option\n", option
);
186 ptr
.package
= package
;
187 ptr
.section
= section
;
190 if (uci_lookup_ptr(ctx
, &ptr
, NULL
, false) ||
191 !(ptr
.flags
& UCI_LOOKUP_COMPLETE
) ||
192 (ptr
.last
->type
!= UCI_TYPE_OPTION
))
194 export_value(DT_STRING
, opt
, def
);
198 validate_value(&ptr
, expr
, def
);
202 main(int argc
, char **argv
)
204 struct uci_context
*ctx
;
205 struct uci_package
*package
;
206 char *opt
, *expr
, *def
;
212 rv
= dt_parse(argv
[1], argv
[2]);
213 fprintf(stderr
, "%s - %s = %s\n", argv
[1], argv
[2], rv
? "true" : "false");
215 } else if (argc
< 5) {
220 if (*argv
[3] == '\0') {
221 printf("json_add_object; ");
222 printf("json_add_string \"package\" \"%s\"; ", argv
[1]);
223 printf("json_add_string \"type\" \"%s\"; ", argv
[2]);
224 printf("json_add_object \"data\"; ");
226 for (i
= 0; i
< len
; i
++) {
227 if (!parse_tuple(argv
[4 + i
], &opt
, &expr
, &def
))
230 printf("json_add_string \"%s\" \"%s\"; ", opt
, expr
);
233 printf("json_close_object; ");
234 printf("json_close_object; ");
239 ctx
= uci_alloc_context();
243 if (uci_load(ctx
, argv
[1], &package
))
246 for (i
= 0; i
< len
; i
++)
247 validate_option(ctx
, argv
[1], argv
[3], argv
[4 + i
]);