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(char *val
)
25 static char val0
[] = "0";
26 static char val1
[] = "1";
27 static char val_none
[] = "";
29 if (!strcmp(val
, "0") || !strcmp(val
, "off") || !strcmp(val
, "false") || !strcmp(val
, "disabled"))
31 if (!strcmp(val
, "1") || !strcmp(val
, "on") || !strcmp(val
, "true") || !strcmp(val
, "enabled"))
38 validate_option(struct uci_context
*ctx
, char *package
, char *section
, char *option
)
40 char *datatype
= strstr(option
, ":");
41 struct uci_ptr ptr
= { 0 };
46 fprintf(stderr
, "%s is not a valid option\n", option
);
52 val
= strstr(datatype
, ":");
58 ptr
.package
= package
;
59 ptr
.section
= section
;
62 if (!uci_lookup_ptr(ctx
, &ptr
, NULL
, false))
63 if (ptr
.flags
& UCI_LOOKUP_COMPLETE
)
64 if (ptr
.last
->type
== UCI_TYPE_OPTION
)
65 if ( ptr
.o
->type
== UCI_TYPE_STRING
)
67 val
= ptr
.o
->v
.string
;
70 ret
= dt_parse(datatype
, val
);
71 fprintf(stderr
, "%s.%s.%s=%s validates as %s with %s\n", package
, section
, option
,
72 val
, datatype
, ret
? "true" : "false");
75 if (ret
&& !strcmp(datatype
, "bool"))
76 printf("%s=%s; ", option
, bool_to_num(val
));
78 printf("%s=%s; ", option
, val
);
80 printf("unset -v %s; ", option
);
86 main(int argc
, char **argv
)
88 struct uci_context
*ctx
;
89 struct uci_package
*package
;
95 rv
= dt_parse(argv
[1], argv
[2]);
96 fprintf(stderr
, "%s - %s = %s\n", argv
[1], argv
[2], rv
? "true" : "false");
98 } else if (argc
< 5) {
103 if (*argv
[3] == '\0') {
104 printf("json_add_object; ");
105 printf("json_add_string \"package\" \"%s\"; ", argv
[1]);
106 printf("json_add_string \"type\" \"%s\"; ", argv
[2]);
107 printf("json_add_object \"data\"; ");
109 for (i
= 0; i
< len
; i
++) {
110 char *datatype
= strstr(argv
[4 + i
], ":");
117 def
= strstr(datatype
, ":");
120 printf("json_add_string \"%s\" \"%s\"; ", argv
[4 + i
], datatype
);
122 printf("json_close_object; ");
123 printf("json_close_object; ");
128 ctx
= uci_alloc_context();
132 if (uci_load(ctx
, argv
[1], &package
))
135 for (i
= 0; i
< len
; i
++)
136 validate_option(ctx
, argv
[1], argv
[3], argv
[4 + i
]);