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
, "no") || !strcmp(val
, "disabled"))
27 if (!strcmp(val
, "1") || !strcmp(val
, "on") || !strcmp(val
, "true") || !strcmp(val
, "yes") || !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
)
113 enum dt_type type
= DT_INVALID
;
114 struct uci_element
*e
;
115 struct uci_option
*opt
= NULL
;
117 if ((ptr
->flags
& UCI_LOOKUP_COMPLETE
) &&
118 (ptr
->last
->type
== UCI_TYPE_OPTION
))
121 if (opt
&& opt
->type
== UCI_TYPE_LIST
)
123 uci_foreach_element(&opt
->v
.list
, e
)
125 if (!e
->name
|| !*e
->name
)
129 printf("%s=", ptr
->option
);
134 type
= dt_parse(expr
, e
->name
);
136 if (type
!= DT_INVALID
)
137 escape_value(type
, e
->name
);
139 fprintf(stderr
, "%s.%s.%s[%u]=%s validates as %s with %s\n",
140 ptr
->package
, ptr
->section
, ptr
->option
, i
++, e
->name
,
141 expr
, type
? "true" : "false");
147 else if (opt
&& opt
->v
.string
&& *opt
->v
.string
)
150 type
= dt_parse(expr
, opt
->v
.string
);
151 export_value(type
, ptr
->option
, opt
->v
.string
);
153 fprintf(stderr
, "%s.%s.%s=%s validates as %s with %s\n",
154 ptr
->package
, ptr
->section
, ptr
->option
, opt
->v
.string
,
155 expr
, type
? "true" : "false");
160 type
= dt_parse(expr
, def
);
162 if (type
== DT_INVALID
)
165 export_value(type
, ptr
->option
, def
);
167 fprintf(stderr
, "%s.%s.%s is unset and defaults to %s %s\n",
168 ptr
->package
, ptr
->section
, ptr
->option
, expr
, def
);
171 return type
? 0 : -1;
175 validate_option(struct uci_context
*ctx
, char *package
, char *section
, char *option
)
177 char *opt
, *expr
, *def
;
178 struct uci_ptr ptr
= {};
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 uci_lookup_ptr(ctx
, &ptr
, NULL
, false);
192 return validate_value(&ptr
, expr
, def
);
196 main(int argc
, char **argv
)
198 struct uci_context
*ctx
;
199 struct uci_package
*package
;
200 char *opt
, *expr
, *def
;
206 rv
= dt_parse(argv
[1], argv
[2]);
207 fprintf(stderr
, "%s - %s = %s\n", argv
[1], argv
[2], rv
? "true" : "false");
209 } else if (argc
< 5) {
214 if (*argv
[3] == '\0') {
215 printf("json_add_object; ");
216 printf("json_add_string \"package\" \"%s\"; ", argv
[1]);
217 printf("json_add_string \"type\" \"%s\"; ", argv
[2]);
218 printf("json_add_object \"data\"; ");
220 for (i
= 0; i
< len
; i
++) {
221 if (!parse_tuple(argv
[4 + i
], &opt
, &expr
, &def
))
224 printf("json_add_string \"%s\" \"%s\"; ", opt
, expr
);
227 printf("json_close_object; ");
228 printf("json_close_object; ");
233 ctx
= uci_alloc_context();
237 if (uci_load(ctx
, argv
[1], &package
))
241 for (i
= 0; i
< len
; i
++) {
242 if (validate_option(ctx
, argv
[1], argv
[3], argv
[4 + i
])) {