validate: return error on failed validation
[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, "no") || !strcmp(val, "disabled"))
26                 return "0";
27         if (!strcmp(val, "1") || !strcmp(val, "on") || !strcmp(val, "true") || !strcmp(val, "yes") || !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 int
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 = DT_INVALID;
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 0;
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 0;
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         return type ? 0 : -1;
173 }
174
175 static int
176 validate_option(struct uci_context *ctx, char *package, char *section, char *option)
177 {
178         char *opt, *expr, *def;
179         struct uci_ptr ptr = { 0 };
180
181         if (!parse_tuple(option, &opt, &expr, &def))
182         {
183                 fprintf(stderr, "%s is not a valid option\n", option);
184                 return -1;
185         }
186
187         ptr.package = package;
188         ptr.section = section;
189         ptr.option = opt;
190
191         if (uci_lookup_ptr(ctx, &ptr, NULL, false) ||
192             !(ptr.flags & UCI_LOOKUP_COMPLETE) ||
193             (ptr.last->type != UCI_TYPE_OPTION))
194         {
195                 export_value(DT_STRING, opt, def);
196                 return 0;
197         }
198
199         return validate_value(&ptr, expr, def);
200 }
201
202 int
203 main(int argc, char **argv)
204 {
205         struct uci_context *ctx;
206         struct uci_package *package;
207         char *opt, *expr, *def;
208         int len = argc - 4;
209         enum dt_type rv;
210         int i, rc;
211
212         if (argc == 3) {
213                 rv = dt_parse(argv[1], argv[2]);
214                 fprintf(stderr, "%s - %s = %s\n", argv[1], argv[2], rv ? "true" : "false");
215                 return rv ? 0 : 1;
216         } else if (argc < 5) {
217                 print_usage(*argv);
218                 return -1;
219         }
220
221         if (*argv[3] == '\0') {
222                 printf("json_add_object; ");
223                 printf("json_add_string \"package\" \"%s\"; ", argv[1]);
224                 printf("json_add_string \"type\" \"%s\"; ", argv[2]);
225                 printf("json_add_object \"data\"; ");
226
227                 for (i = 0; i < len; i++) {
228                         if (!parse_tuple(argv[4 + i], &opt, &expr, &def))
229                                 continue;
230
231                         printf("json_add_string \"%s\" \"%s\"; ", opt, expr);
232                 }
233
234                 printf("json_close_object; ");
235                 printf("json_close_object; ");
236
237                 return 0;
238         }
239
240         ctx = uci_alloc_context();
241         if (!ctx)
242                 return -1;
243
244         if (uci_load(ctx, argv[1], &package))
245                 return -1;
246
247         rc = 0;
248         for (i = 0; i < len; i++) {
249                 if (validate_option(ctx, argv[1], argv[3], argv[4 + i])) {
250                         rc = -1;
251                 }
252         }
253
254         return rc;
255 }