bump major version
[project/uci.git] / ucimap.c
1 /*
2 * ucimap - library for mapping uci sections into data structures
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14 #include <strings.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include "ucimap.h"
19
20 struct uci_alloc {
21 enum ucimap_type type;
22 union {
23 void **ptr;
24 struct list_head *list;
25 } data;
26 };
27
28 struct uci_fixup {
29 struct list_head list;
30 struct uci_sectmap *sm;
31 const char *name;
32 struct uci_alloc target;
33 };
34
35 struct uci_sectmap_data {
36 struct list_head list;
37 struct uci_map *map;
38 struct uci_sectmap *sm;
39 const char *section_name;
40
41 /* list of allocations done by ucimap */
42 struct uci_alloc *allocmap;
43 unsigned long allocmap_len;
44
45 /* map for changed fields */
46 unsigned char *cmap;
47 bool done;
48 };
49
50
51 int
52 ucimap_init(struct uci_map *map)
53 {
54 INIT_LIST_HEAD(&map->sdata);
55 INIT_LIST_HEAD(&map->fixup);
56 return 0;
57 }
58
59 static void
60 ucimap_free_item(struct uci_alloc *a)
61 {
62 struct list_head *p, *tmp;
63 switch(a->type & UCIMAP_TYPE) {
64 case UCIMAP_SIMPLE:
65 free(a->data.ptr);
66 break;
67 case UCIMAP_LIST:
68 list_for_each_safe(p, tmp, a->data.list) {
69 struct uci_listmap *l = list_entry(p, struct uci_listmap, list);
70 list_del(p);
71 free(l);
72 }
73 break;
74 }
75 }
76
77 static inline void
78 ucimap_add_alloc(struct uci_alloc *a, void *ptr)
79 {
80 a->type = UCIMAP_SIMPLE;
81 a->data.ptr = ptr;
82 }
83
84 static void
85 ucimap_free_section(struct uci_map *map, struct uci_sectmap_data *sd)
86 {
87 void *section = sd;
88 int i;
89
90 section = (char *) section + sizeof(struct uci_sectmap_data);
91 if (!list_empty(&sd->list))
92 list_del(&sd->list);
93
94 if (sd->sm->free_section)
95 sd->sm->free_section(map, section);
96
97 for (i = 0; i < sd->allocmap_len; i++) {
98 ucimap_free_item(&sd->allocmap[i]);
99 }
100
101 free(sd->allocmap);
102 free(sd);
103 }
104
105 void
106 ucimap_cleanup(struct uci_map *map)
107 {
108 struct list_head *ptr, *tmp;
109
110 list_for_each_safe(ptr, tmp, &map->sdata) {
111 struct uci_sectmap_data *sd = list_entry(ptr, struct uci_sectmap_data, list);
112 ucimap_free_section(map, sd);
113 }
114 }
115
116 static void
117 ucimap_add_fixup(struct uci_map *map, void *data, struct uci_optmap *om, const char *str)
118 {
119 struct uci_fixup *f;
120
121 f = malloc(sizeof(struct uci_fixup));
122 if (!f)
123 return;
124
125 INIT_LIST_HEAD(&f->list);
126 f->sm = om->data.sm;
127 f->name = str;
128 f->target.type = om->type;
129 f->target.data.ptr = data;
130 list_add(&f->list, &map->fixup);
131 }
132
133 static void
134 ucimap_add_value(union uci_datamap *data, struct uci_optmap *om, struct uci_sectmap_data *sd, const char *str)
135 {
136 union uci_datamap tdata;
137 struct list_head *list = NULL;
138 char *eptr = NULL;
139 char *s;
140 int val;
141
142 if ((om->type & UCIMAP_TYPE) == UCIMAP_LIST) {
143 if ((om->type & UCIMAP_SUBTYPE) == UCIMAP_SECTION) {
144 ucimap_add_fixup(sd->map, data, om, str);
145 return;
146 }
147 memset(&tdata, 0, sizeof(tdata));
148 list = &data->list;
149 data = &tdata;
150 }
151
152 switch(om->type & UCIMAP_SUBTYPE) {
153 case UCIMAP_STRING:
154 if ((om->data.s.maxlen > 0) &&
155 (strlen(str) > om->data.s.maxlen))
156 return;
157
158 s = strdup(str);
159 data->s = s;
160 ucimap_add_alloc(&sd->allocmap[sd->allocmap_len++], s);
161 break;
162 case UCIMAP_BOOL:
163 val = -1;
164 if (strcmp(str, "on"))
165 val = true;
166 else if (strcmp(str, "1"))
167 val = true;
168 else if (strcmp(str, "enabled"))
169 val = true;
170 else if (strcmp(str, "off"))
171 val = false;
172 else if (strcmp(str, "0"))
173 val = false;
174 else if (strcmp(str, "disabled"))
175 val = false;
176 if (val == -1)
177 return;
178
179 data->b = val;
180 break;
181 case UCIMAP_INT:
182 val = strtol(str, &eptr, om->data.i.base);
183 if (!eptr || *eptr == '\0')
184 data->i = val;
185 else
186 return;
187 break;
188 case UCIMAP_SECTION:
189 ucimap_add_fixup(sd->map, data, om, str);
190 break;
191 }
192
193 if ((om->type & UCIMAP_TYPE) == UCIMAP_LIST) {
194 struct uci_listmap *item;
195
196 item = malloc(sizeof(struct uci_listmap));
197 if (!item)
198 return;
199
200 INIT_LIST_HEAD(&item->list);
201 memcpy(&item->data, &tdata, sizeof(tdata));
202 list_add(&item->list, list);
203 }
204 }
205
206
207 static int
208 ucimap_parse_options(struct uci_map *map, struct uci_sectmap *sm, struct uci_sectmap_data *sd, struct uci_section *s)
209 {
210 struct uci_element *e, *l;
211 struct uci_option *o;
212 unsigned long section;
213 union uci_datamap *data;
214 int i;
215
216 section = (unsigned long) sd + sizeof(struct uci_sectmap_data);
217 uci_foreach_element(&s->options, e) {
218 struct uci_optmap *om = NULL;
219
220 for (i = 0; i < sm->n_options; i++) {
221 if (strcmp(e->name, sm->options[i].name) == 0) {
222 om = &sm->options[i];
223 break;
224 }
225 }
226 if (!om)
227 continue;
228
229 data = (union uci_datamap *) (section + om->offset);
230 o = uci_to_option(e);
231 if ((o->type == UCI_TYPE_STRING) && ((om->type & UCIMAP_TYPE) == UCIMAP_SIMPLE)) {
232 ucimap_add_value(data, om, sd, o->v.string);
233 continue;
234 }
235 if ((o->type == UCI_TYPE_LIST) && ((om->type & UCIMAP_TYPE) == UCIMAP_LIST)) {
236 struct list_head *list;
237
238 list = (struct list_head *) (section + om->offset);
239 INIT_LIST_HEAD(list);
240 sd->allocmap[sd->allocmap_len].type = UCIMAP_LIST;
241 sd->allocmap[sd->allocmap_len++].data.list = list;
242 uci_foreach_element(&o->v.list, l) {
243 ucimap_add_value(data, om, sd, l->name);
244 }
245 continue;
246 }
247 }
248
249 return 0;
250 }
251
252
253 static int
254 ucimap_parse_section(struct uci_map *map, struct uci_sectmap *sm, struct uci_section *s)
255 {
256 struct uci_sectmap_data *sd = NULL;
257 void *section = NULL;
258 int err;
259
260 sd = malloc(sm->alloc_len + sizeof(struct uci_sectmap_data));
261 if (!sd)
262 return UCI_ERR_MEM;
263
264 memset(sd, 0, sm->alloc_len + sizeof(struct uci_sectmap_data));
265 INIT_LIST_HEAD(&sd->list);
266
267 sd->map = map;
268 sd->sm = sm;
269 sd->allocmap = malloc(sm->n_options * sizeof(struct uci_alloc));
270 if (!sd->allocmap)
271 goto error_mem;
272
273 sd->section_name = strdup(s->e.name);
274 if (!sd->section_name)
275 goto error_mem;
276
277 sd->cmap = malloc(BITFIELD_SIZE(sm->n_options));
278 if (!sd->cmap)
279 goto error_mem;
280
281 memset(sd->cmap, 0, BITFIELD_SIZE(sm->n_options));
282 ucimap_add_alloc(&sd->allocmap[sd->allocmap_len++], (void *)sd->section_name);
283 ucimap_add_alloc(&sd->allocmap[sd->allocmap_len++], (void *)sd->cmap);
284
285 section = (char *)sd + sizeof(struct uci_sectmap_data);
286
287 err = sm->init_section(map, section, s);
288 if (err)
289 goto error;
290
291 list_add(&sd->list, &map->sdata);
292 err = ucimap_parse_options(map, sm, sd, s);
293 if (err)
294 goto error;
295
296 return 0;
297
298 error_mem:
299 if (sd->allocmap)
300 free(sd->allocmap);
301 free(sd);
302 return UCI_ERR_MEM;
303
304 error:
305 ucimap_free_section(map, sd);
306 return err;
307 }
308
309 static int
310 ucimap_fill_ptr(struct uci_ptr *ptr, struct uci_section *s, const char *option)
311 {
312 struct uci_package *p = s->package;
313
314 memset(ptr, 0, sizeof(struct uci_ptr));
315
316 ptr->package = p->e.name;
317 ptr->p = p;
318
319 ptr->section = s->e.name;
320 ptr->s = s;
321
322 ptr->option = option;
323 return uci_lookup_ptr(p->ctx, ptr, NULL, false);
324 }
325
326 void
327 ucimap_set_changed(void *section, void *field)
328 {
329 char *sptr = (char *)section - sizeof(struct uci_sectmap_data);
330 struct uci_sectmap_data *sd = (struct uci_sectmap_data *) sptr;
331 struct uci_sectmap *sm = sd->sm;
332 int ofs = (char *)field - (char *)section;
333 int i;
334
335 for (i = 0; i < sm->n_options; i++) {
336 if (sm->options[i].offset == ofs) {
337 SET_BIT(sd->cmap, i);
338 break;
339 }
340 }
341 }
342
343 int
344 ucimap_store_section(struct uci_map *map, struct uci_package *p, void *section)
345 {
346 char *sptr = (char *)section - sizeof(struct uci_sectmap_data);
347 struct uci_sectmap_data *sd = (struct uci_sectmap_data *) sptr;
348 struct uci_sectmap *sm = sd->sm;
349 struct uci_section *s = NULL;
350 struct uci_element *e;
351 struct uci_ptr ptr;
352 int i, ret;
353
354 uci_foreach_element(&p->sections, e) {
355 if (!strcmp(e->name, sd->section_name)) {
356 s = uci_to_section(e);
357 break;
358 }
359 }
360 if (!s)
361 return UCI_ERR_NOTFOUND;
362
363 for (i = 0; i < sm->n_options; i++) {
364 struct uci_optmap *om = &sm->options[i];
365 static char buf[32];
366 const char *str = NULL;
367 void *p = (char *)section + om->offset;
368
369 if (!TEST_BIT(sd->cmap, i))
370 continue;
371
372 ucimap_fill_ptr(&ptr, s, om->name);
373 switch(om->type & UCIMAP_SUBTYPE) {
374 case UCIMAP_STRING:
375 str = *((char **) p);
376 break;
377 case UCIMAP_INT:
378 sprintf(buf, "%d", *((int *) p));
379 str = buf;
380 break;
381 case UCIMAP_BOOL:
382 sprintf(buf, "%d", !!*((bool *)p));
383 str = buf;
384 break;
385 }
386 ptr.value = str;
387
388 ret = uci_set(s->package->ctx, &ptr);
389 if (ret)
390 return ret;
391
392 CLR_BIT(sd->cmap, i);
393 }
394
395 return 0;
396 }
397
398 void *
399 ucimap_find_section(struct uci_map *map, struct uci_fixup *f)
400 {
401 struct uci_sectmap_data *sd;
402 struct list_head *p;
403 void *ret;
404
405 list_for_each(p, &map->sdata) {
406 sd = list_entry(p, struct uci_sectmap_data, list);
407 if (sd->sm != f->sm)
408 continue;
409 if (strcmp(f->name, sd->section_name) != 0)
410 continue;
411 ret = (char *)sd + sizeof(struct uci_sectmap_data);
412 return ret;
413 }
414 return NULL;
415 }
416
417 void
418 ucimap_parse(struct uci_map *map, struct uci_package *pkg)
419 {
420 struct uci_element *e;
421 struct list_head *p, *tmp;
422 int i;
423
424 INIT_LIST_HEAD(&map->fixup);
425 uci_foreach_element(&pkg->sections, e) {
426 struct uci_section *s = uci_to_section(e);
427
428 for (i = 0; i < map->n_sections; i++) {
429 if (strcmp(s->type, map->sections[i]->type) != 0)
430 continue;
431 ucimap_parse_section(map, map->sections[i], s);
432 }
433 }
434 list_for_each_safe(p, tmp, &map->fixup) {
435 struct uci_fixup *f = list_entry(p, struct uci_fixup, list);
436 void *ptr = ucimap_find_section(map, f);
437 struct uci_listmap *li;
438
439 if (!ptr)
440 continue;
441
442 switch(f->target.type & UCIMAP_TYPE) {
443 case UCIMAP_SIMPLE:
444 *f->target.data.ptr = ptr;
445 break;
446 case UCIMAP_LIST:
447 li = malloc(sizeof(struct uci_listmap));
448 memset(li, 0, sizeof(struct uci_listmap));
449 INIT_LIST_HEAD(&li->list);
450 li->data.section = ptr;
451 list_add(&li->list, f->target.data.list);
452 break;
453 }
454 }
455 list_for_each_safe(p, tmp, &map->sdata) {
456 struct uci_sectmap_data *sd = list_entry(p, struct uci_sectmap_data, list);
457 void *section;
458
459 if (sd->done)
460 continue;
461
462 section = (char *) sd + sizeof(struct uci_sectmap_data);
463 if (sd->sm->add_section(map, section) != 0)
464 ucimap_free_section(map, sd);
465 }
466 }