ee5c189447123cf823e53a4c6f274cc82a2a139e
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 escape_value(enum dt_type type
, const char *val
)
41 printf("%s", bool_to_num(val
));
47 for (p
= val
; *p
; p
++)
63 export_value(enum dt_type type
, const char *name
, const char *val
)
65 if ((type
== DT_INVALID
) || !val
|| !*val
)
67 printf("unset -v %s; ", name
);
72 escape_value(type
, val
);
77 validate_value(struct uci_ptr
*ptr
, const char *expr
, const char *def
)
80 bool empty
= true, first
= true;
82 struct uci_element
*e
;
83 struct uci_option
*opt
= ptr
->o
;
85 if (opt
->type
== UCI_TYPE_LIST
)
87 uci_foreach_element(&opt
->v
.list
, e
)
89 if (!e
->name
|| !*e
->name
)
98 export_value(DT_STRING
, ptr
->option
, def
);
102 uci_foreach_element(&opt
->v
.list
, e
)
104 if (!e
->name
|| !*e
->name
)
108 printf("%s=", ptr
->option
);
113 type
= dt_parse(expr
, e
->name
);
115 if (type
!= DT_INVALID
)
116 escape_value(type
, e
->name
);
118 fprintf(stderr
, "%s.%s.%s[%u]=%s validates as %s with %s\n",
119 ptr
->package
, ptr
->section
, ptr
->option
, i
++, e
->name
,
120 expr
, type
? "true" : "false");
127 if (!opt
->v
.string
|| !*opt
->v
.string
)
129 export_value(DT_STRING
, ptr
->option
, def
);
133 type
= dt_parse(expr
, opt
->v
.string
);
134 export_value(type
, ptr
->option
, opt
->v
.string
);
136 fprintf(stderr
, "%s.%s.%s=%s validates as %s with %s\n",
137 ptr
->package
, ptr
->section
, ptr
->option
, opt
->v
.string
,
138 expr
, type
? "true" : "false");
143 validate_option(struct uci_context
*ctx
, char *package
, char *section
, char *option
)
146 struct uci_ptr ptr
= { 0 };
148 if ((expr
= strchr(option
, ':')) == NULL
)
150 fprintf(stderr
, "%s is not a valid option\n", option
);
156 if ((def
= strrchr(expr
, ':')) != NULL
)
159 ptr
.package
= package
;
160 ptr
.section
= section
;
163 if (uci_lookup_ptr(ctx
, &ptr
, NULL
, false) ||
164 !(ptr
.flags
& UCI_LOOKUP_COMPLETE
) ||
165 (ptr
.last
->type
!= UCI_TYPE_OPTION
))
167 export_value(DT_STRING
, option
, def
);
171 validate_value(&ptr
, expr
, def
);
175 main(int argc
, char **argv
)
177 struct uci_context
*ctx
;
178 struct uci_package
*package
;
184 rv
= dt_parse(argv
[1], argv
[2]);
185 fprintf(stderr
, "%s - %s = %s\n", argv
[1], argv
[2], rv
? "true" : "false");
187 } else if (argc
< 5) {
192 if (*argv
[3] == '\0') {
193 printf("json_add_object; ");
194 printf("json_add_string \"package\" \"%s\"; ", argv
[1]);
195 printf("json_add_string \"type\" \"%s\"; ", argv
[2]);
196 printf("json_add_object \"data\"; ");
198 for (i
= 0; i
< len
; i
++) {
199 char *datatype
= strstr(argv
[4 + i
], ":");
206 def
= strstr(datatype
, ":");
209 printf("json_add_string \"%s\" \"%s\"; ", argv
[4 + i
], datatype
);
211 printf("json_close_object; ");
212 printf("json_close_object; ");
217 ctx
= uci_alloc_context();
221 if (uci_load(ctx
, argv
[1], &package
))
224 for (i
= 0; i
< len
; i
++)
225 validate_option(ctx
, argv
[1], argv
[3], argv
[4 + i
]);