a2a82edfae51b81d65c0aac952c8cc009bf52773
[project/ubox.git] / validate / cli.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdbool.h>
5 #include <ctype.h>
6
7 #include <arpa/inet.h>
8 #include <netinet/ether.h>
9 #include <sys/stat.h>
10
11 #include <uci.h>
12
13 #include "libvalidate.h"
14
15 static void
16 print_usage(char *argv)
17 {
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);
20 }
21
22 static const char *
23 bool_to_num(const char *val)
24 {
25 if (!strcmp(val, "0") || !strcmp(val, "off") || !strcmp(val, "false") || !strcmp(val, "disabled"))
26 return "0";
27 if (!strcmp(val, "1") || !strcmp(val, "on") || !strcmp(val, "true") || !strcmp(val, "enabled"))
28 return "1";
29
30 return "";
31 }
32
33 static bool
34 parse_tuple(char *tuple, char **option, char **expr, char **def)
35 {
36 char *p;
37 bool esc;
38
39 for (esc = false, p = *option = tuple, *expr = NULL, *def = NULL; *p; p++)
40 {
41 if (!esc && *p == '\\')
42 {
43 esc = true;
44 continue;
45 }
46
47 if (!esc && *p == ':')
48 {
49 *p++ = 0;
50
51 if (!*expr)
52 *expr = p;
53 else if (!*def)
54 *def = p;
55 else
56 break;
57 }
58
59 esc = false;
60 }
61
62 return (*expr != NULL);
63 }
64
65 static void
66 escape_value(enum dt_type type, const char *val)
67 {
68 const char *p;
69
70 switch(type)
71 {
72 case DT_BOOL:
73 printf("%s", bool_to_num(val));
74 break;
75
76 case DT_STRING:
77 printf("'");
78
79 for (p = val; *p; p++)
80 if (*p == '\'')
81 printf("'\"'\"'");
82 else
83 printf("%c", *p);
84
85 printf("'");
86 break;
87
88 default:
89 printf("%s", val);
90 break;
91 }
92 }
93
94 static void
95 export_value(enum dt_type type, const char *name, const char *val)
96 {
97 if ((type == DT_INVALID) || !val || !*val)
98 {
99 printf("unset -v %s; ", name);
100 return;
101 }
102
103 printf("%s=", name);
104 escape_value(type, val);
105 printf("; ");
106 }
107
108 static void
109 validate_value(struct uci_ptr *ptr, const char *expr, const char *def)
110 {
111 int i = 0;
112 bool empty = true, first = true;
113 enum dt_type type;
114 struct uci_element *e;
115 struct uci_option *opt = ptr->o;
116
117 if (opt->type == UCI_TYPE_LIST)
118 {
119 uci_foreach_element(&opt->v.list, e)
120 {
121 if (!e->name || !*e->name)
122 continue;
123
124 empty = false;
125 break;
126 }
127
128 if (empty)
129 {
130 export_value(DT_STRING, ptr->option, def);
131 return;
132 }
133
134 uci_foreach_element(&opt->v.list, e)
135 {
136 if (!e->name || !*e->name)
137 continue;
138
139 if (first)
140 printf("%s=", ptr->option);
141 else
142 printf("\\ ");
143
144 first = false;
145 type = dt_parse(expr, e->name);
146
147 if (type != DT_INVALID)
148 escape_value(type, e->name);
149
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");
153 }
154
155 printf("; ");
156 }
157 else
158 {
159 if (!opt->v.string || !*opt->v.string)
160 {
161 export_value(DT_STRING, ptr->option, def);
162 return;
163 }
164
165 type = dt_parse(expr, opt->v.string);
166 export_value(type, ptr->option, opt->v.string);
167
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");
171 }
172 }
173
174 static void
175 validate_option(struct uci_context *ctx, char *package, char *section, char *option)
176 {
177 char *opt, *expr, *def;
178 struct uci_ptr ptr = { 0 };
179
180 if (!parse_tuple(option, &opt, &expr, &def))
181 {
182 fprintf(stderr, "%s is not a valid option\n", option);
183 return;
184 }
185
186 ptr.package = package;
187 ptr.section = section;
188 ptr.option = opt;
189
190 if (uci_lookup_ptr(ctx, &ptr, NULL, false) ||
191 !(ptr.flags & UCI_LOOKUP_COMPLETE) ||
192 (ptr.last->type != UCI_TYPE_OPTION))
193 {
194 export_value(DT_STRING, opt, def);
195 return;
196 }
197
198 validate_value(&ptr, expr, def);
199 }
200
201 int
202 main(int argc, char **argv)
203 {
204 struct uci_context *ctx;
205 struct uci_package *package;
206 char *opt, *expr, *def;
207 int len = argc - 4;
208 enum dt_type rv;
209 int i;
210
211 if (argc == 3) {
212 rv = dt_parse(argv[1], argv[2]);
213 fprintf(stderr, "%s - %s = %s\n", argv[1], argv[2], rv ? "true" : "false");
214 return rv ? 0 : 1;
215 } else if (argc < 5) {
216 print_usage(*argv);
217 return -1;
218 }
219
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\"; ");
225
226 for (i = 0; i < len; i++) {
227 if (!parse_tuple(argv[4 + i], &opt, &expr, &def))
228 continue;
229
230 printf("json_add_string \"%s\" \"%s\"; ", opt, expr);
231 }
232
233 printf("json_close_object; ");
234 printf("json_close_object; ");
235
236 return 0;
237 }
238
239 ctx = uci_alloc_context();
240 if (!ctx)
241 return -1;
242
243 if (uci_load(ctx, argv[1], &package))
244 return -1;
245
246 for (i = 0; i < len; i++)
247 validate_option(ctx, argv[1], argv[3], argv[4 + i]);
248
249 return 0;
250 }