2 * ucimap - library for mapping uci sections into data structures
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
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
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.
23 #include "uci_internal.h"
26 enum ucimap_type type;
32 struct uci_alloc_custom {
34 struct uci_optmap *om;
39 struct list_head list;
40 struct uci_sectionmap *sm;
42 enum ucimap_type type;
43 union ucimap_data *data;
46 #define ucimap_foreach_option(_sm, _o) \
47 if (!(_sm)->options_size) \
48 (_sm)->options_size = sizeof(struct uci_optmap); \
49 for (_o = &(_sm)->options[0]; \
50 ((char *)(_o)) < ((char *) &(_sm)->options[0] + \
51 (_sm)->options_size * (_sm)->n_options); \
52 _o = (struct uci_optmap *) ((char *)(_o) + \
57 ucimap_is_alloc(enum ucimap_type type)
59 switch(type & UCIMAP_SUBTYPE) {
68 ucimap_is_fixup(enum ucimap_type type)
70 switch(type & UCIMAP_SUBTYPE) {
79 ucimap_is_simple(enum ucimap_type type)
81 return ((type & UCIMAP_TYPE) == UCIMAP_SIMPLE);
85 ucimap_is_list(enum ucimap_type type)
87 return ((type & UCIMAP_TYPE) == UCIMAP_LIST);
91 ucimap_is_list_auto(enum ucimap_type type)
93 return ucimap_is_list(type) && !!(type & UCIMAP_LIST_AUTO);
97 ucimap_is_custom(enum ucimap_type type)
99 return ((type & UCIMAP_SUBTYPE) == UCIMAP_CUSTOM);
103 ucimap_section_ptr(struct ucimap_section_data *sd)
105 return ((char *) sd - sd->sm->smap_offset);
108 static inline union ucimap_data *
109 ucimap_get_data(struct ucimap_section_data *sd, struct uci_optmap *om)
113 data = (char *) ucimap_section_ptr(sd) + om->offset;
118 ucimap_init(struct uci_map *map)
120 INIT_LIST_HEAD(&map->pending);
121 INIT_LIST_HEAD(&map->sdata);
122 INIT_LIST_HEAD(&map->fixup);
127 ucimap_free_item(struct uci_alloc *a)
129 switch(a->type & UCIMAP_TYPE) {
138 ucimap_add_alloc(struct ucimap_section_data *sd, void *ptr)
140 struct uci_alloc *a = &sd->allocmap[sd->allocmap_len++];
141 a->type = UCIMAP_SIMPLE;
146 ucimap_free_section(struct uci_map *map, struct ucimap_section_data *sd)
151 section = ucimap_section_ptr(sd);
152 if (!list_empty(&sd->list))
156 sd->sm->free(map, section);
158 for (i = 0; i < sd->allocmap_len; i++) {
159 ucimap_free_item(&sd->allocmap[i]);
162 if (sd->alloc_custom) {
163 for (i = 0; i < sd->alloc_custom_len; i++) {
164 struct uci_alloc_custom *a = &sd->alloc_custom[i];
165 a->om->free(a->section, a->om, a->ptr);
167 free(sd->alloc_custom);
175 ucimap_cleanup(struct uci_map *map)
177 struct list_head *ptr, *tmp;
179 list_for_each_safe(ptr, tmp, &map->sdata) {
180 struct ucimap_section_data *sd = list_entry(ptr, struct ucimap_section_data, list);
181 ucimap_free_section(map, sd);
186 ucimap_find_section(struct uci_map *map, struct uci_fixup *f)
188 struct ucimap_section_data *sd;
191 list_for_each(p, &map->sdata) {
192 sd = list_entry(p, struct ucimap_section_data, list);
195 if (strcmp(f->name, sd->section_name) != 0)
197 return ucimap_section_ptr(sd);
199 list_for_each(p, &map->pending) {
200 sd = list_entry(p, struct ucimap_section_data, list);
203 if (strcmp(f->name, sd->section_name) != 0)
205 return ucimap_section_ptr(sd);
211 ucimap_handle_fixup(struct uci_map *map, struct uci_fixup *f)
213 void *ptr = ucimap_find_section(map, f);
214 struct ucimap_list *list;
219 switch(f->type & UCIMAP_TYPE) {
224 list = f->data->list;
225 list->item[list->n_items++].ptr = ptr;
232 ucimap_add_fixup(struct ucimap_section_data *sd, union ucimap_data *data, struct uci_optmap *om, const char *str)
234 struct uci_fixup *f, tmp;
235 struct uci_map *map = sd->map;
237 INIT_LIST_HEAD(&tmp.list);
238 tmp.sm = om->data.sm;
242 if (ucimap_handle_fixup(map, &tmp))
245 f = malloc(sizeof(struct uci_fixup));
249 memcpy(f, &tmp, sizeof(tmp));
250 list_add_tail(&f->list, &map->fixup);
254 ucimap_add_custom_alloc(struct ucimap_section_data *sd, struct uci_optmap *om, void *ptr)
256 struct uci_alloc_custom *a = &sd->alloc_custom[sd->alloc_custom_len++];
258 a->section = ucimap_section_ptr(sd);
264 ucimap_add_value(union ucimap_data *data, struct uci_optmap *om, struct ucimap_section_data *sd, const char *str)
266 union ucimap_data tdata = *data;
272 if (ucimap_is_list(om->type) && !ucimap_is_fixup(om->type))
273 data = &data->list->item[data->list->n_items++];
275 switch(om->type & UCIMAP_SUBTYPE) {
277 if ((om->data.s.maxlen > 0) &&
278 (strlen(str) > om->data.s.maxlen))
283 ucimap_add_alloc(sd, s);
286 if (!strcmp(str, "on"))
288 else if (!strcmp(str, "1"))
290 else if (!strcmp(str, "enabled"))
292 else if (!strcmp(str, "off"))
294 else if (!strcmp(str, "0"))
296 else if (!strcmp(str, "disabled"))
304 lval = strtol(str, &eptr, om->data.i.base);
305 if (lval < INT_MIN || lval > INT_MAX)
308 if (!eptr || *eptr == '\0')
309 tdata.i = (int) lval;
314 ucimap_add_fixup(sd, data, om, str);
317 tdata.s = (char *) data;
321 if (om->parse(ucimap_section_ptr(sd), om, &tdata, str) < 0)
323 if (ucimap_is_custom(om->type) && om->free) {
324 if (tdata.ptr != data->ptr)
325 ucimap_add_custom_alloc(sd, om, data->ptr);
328 if (ucimap_is_custom(om->type))
330 memcpy(data, &tdata, sizeof(union ucimap_data));
335 ucimap_convert_list(union ucimap_data *data, struct uci_optmap *om, struct ucimap_section_data *sd, const char *str)
343 ucimap_add_alloc(sd, s);
353 while (*s && !isspace(*s))
361 ucimap_add_value(data, om, sd, p);
366 ucimap_parse_options(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s)
368 struct uci_element *e, *l;
369 struct uci_option *o;
370 union ucimap_data *data;
372 uci_foreach_element(&s->options, e) {
373 struct uci_optmap *om = NULL, *tmp;
375 ucimap_foreach_option(sm, tmp) {
376 if (strcmp(e->name, tmp->name) == 0) {
384 data = ucimap_get_data(sd, om);
385 o = uci_to_option(e);
386 if ((o->type == UCI_TYPE_STRING) && ucimap_is_simple(om->type)) {
387 ucimap_add_value(data, om, sd, o->v.string);
388 } else if ((o->type == UCI_TYPE_LIST) && ucimap_is_list(om->type)) {
389 uci_foreach_element(&o->v.list, l) {
390 ucimap_add_value(data, om, sd, l->name);
392 } else if ((o->type == UCI_TYPE_STRING) && ucimap_is_list_auto(om->type)) {
393 ucimap_convert_list(data, om, sd, o->v.string);
401 ucimap_add_section(struct ucimap_section_data *sd)
403 struct uci_map *map = sd->map;
405 if (sd->sm->add(map, ucimap_section_ptr(sd)) < 0)
406 ucimap_free_section(map, sd);
408 list_add_tail(&sd->list, &map->sdata);
411 static const char *ucimap_type_names[] = {
412 [UCIMAP_STRING] = "string",
413 [UCIMAP_INT] = "integer",
414 [UCIMAP_BOOL] = "boolean",
415 [UCIMAP_SECTION] = "section",
416 [UCIMAP_LIST] = "list",
419 static inline const char *
420 ucimap_get_type_name(int type)
425 if (ucimap_is_list(type))
426 return ucimap_type_names[UCIMAP_LIST];
428 name = ucimap_type_names[type & UCIMAP_SUBTYPE];
430 sprintf(buf, "Unknown (%d)", type & UCIMAP_SUBTYPE);
438 ucimap_check_optmap_type(struct uci_sectionmap *sm, struct uci_optmap *om)
442 if (om->detected_type < 0)
445 if (ucimap_is_custom(om->type))
448 if (ucimap_is_list(om->type) !=
449 ucimap_is_list(om->detected_type))
452 if (ucimap_is_list(om->type))
455 type = om->type & UCIMAP_SUBTYPE;
460 if (type != om->detected_type)
471 DPRINTF("Invalid type in option '%s' of section type '%s', "
472 "declared type is %s, detected type is %s\n",
474 ucimap_get_type_name(om->type),
475 ucimap_get_type_name(om->detected_type));
480 ucimap_count_alloc(struct uci_optmap *om, int *n_alloc, int *n_custom)
482 if (ucimap_is_alloc(om->type))
484 else if (ucimap_is_custom(om->type) && om->free)
489 ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s)
491 struct uci_optmap *om;
495 int n_alloc_custom = 0;
498 INIT_LIST_HEAD(&sd->list);
502 ucimap_foreach_option(sm, om) {
503 if (!ucimap_check_optmap_type(sm, om))
506 if (ucimap_is_list(om->type)) {
507 union ucimap_data *data;
508 struct uci_element *e;
510 int n_elements_custom = 0;
513 data = ucimap_get_data(sd, om);
514 uci_foreach_element(&s->options, e) {
515 struct uci_option *o = uci_to_option(e);
516 struct uci_element *tmp;
518 if (strcmp(e->name, om->name) != 0)
521 if (o->type == UCI_TYPE_LIST) {
522 uci_foreach_element(&o->v.list, tmp) {
523 ucimap_count_alloc(om, &n_elements, &n_elements_custom);
525 } else if ((o->type == UCI_TYPE_STRING) &&
526 ucimap_is_list_auto(om->type)) {
527 const char *data = o->v.string;
529 while (isspace(*data))
536 ucimap_count_alloc(om, &n_elements, &n_elements_custom);
538 while (*data && !isspace(*data))
542 /* for the duplicated data string */
548 /* add one more for the ucimap_list */
549 n_alloc += n_elements + 1;
550 n_alloc_custom += n_elements_custom;
551 size = sizeof(struct ucimap_list) +
552 n_elements * sizeof(union ucimap_data);
553 data->list = malloc(size);
554 memset(data->list, 0, size);
556 ucimap_count_alloc(om, &n_alloc, &n_alloc_custom);
560 sd->allocmap = calloc(n_alloc, sizeof(struct uci_alloc));
564 if (n_alloc_custom > 0) {
565 sd->alloc_custom = calloc(n_alloc_custom, sizeof(struct uci_alloc_custom));
566 if (!sd->alloc_custom)
570 section_name = strdup(s->e.name);
574 sd->section_name = section_name;
576 sd->cmap = calloc(1, BITFIELD_SIZE(sm->n_options));
580 ucimap_add_alloc(sd, (void *)section_name);
581 ucimap_add_alloc(sd, (void *)sd->cmap);
582 ucimap_foreach_option(sm, om) {
583 if (!ucimap_is_list(om->type))
586 ucimap_add_alloc(sd, ucimap_get_data(sd, om)->list);
589 section = ucimap_section_ptr(sd);
590 err = sm->init(map, section, s);
595 ucimap_add_section(sd);
597 list_add_tail(&sd->list, &map->pending);
600 err = ucimap_parse_options(map, sm, sd, s);
613 ucimap_free_section(map, sd);
618 ucimap_fill_ptr(struct uci_ptr *ptr, struct uci_section *s, const char *option)
620 struct uci_package *p = s->package;
622 memset(ptr, 0, sizeof(struct uci_ptr));
624 ptr->package = p->e.name;
627 ptr->section = s->e.name;
630 ptr->option = option;
631 return uci_lookup_ptr(p->ctx, ptr, NULL, false);
635 ucimap_set_changed(struct ucimap_section_data *sd, void *field)
637 void *section = ucimap_section_ptr(sd);
638 struct uci_sectionmap *sm = sd->sm;
639 struct uci_optmap *om;
640 int ofs = (char *)field - (char *)section;
643 ucimap_foreach_option(sm, om) {
644 if (om->offset == ofs) {
645 SET_BIT(sd->cmap, i);
653 ucimap_store_section(struct uci_map *map, struct uci_package *p, struct ucimap_section_data *sd)
655 struct uci_sectionmap *sm = sd->sm;
656 struct uci_section *s = NULL;
657 struct uci_optmap *om;
658 struct uci_element *e;
663 uci_foreach_element(&p->sections, e) {
664 if (!strcmp(e->name, sd->section_name)) {
665 s = uci_to_section(e);
670 return UCI_ERR_NOTFOUND;
672 ucimap_foreach_option(sm, om) {
673 union ucimap_data *data;
678 if (ucimap_is_list(om->type))
681 data = ucimap_get_data(sd, om);
682 if (!TEST_BIT(sd->cmap, i - 1))
685 ucimap_fill_ptr(&ptr, s, om->name);
686 switch(om->type & UCIMAP_SUBTYPE) {
691 sprintf(buf, "%d", data->i);
695 sprintf(buf, "%d", !!data->b);
704 union ucimap_data tdata, *data;
706 data = ucimap_get_data(sd, om);
707 if (ucimap_is_custom(om->type)) {
708 tdata.s = (char *)data;
712 if (om->format(ucimap_section_ptr(sd), om, data, &str) < 0)
719 ret = uci_set(s->package->ctx, &ptr);
723 CLR_BIT(sd->cmap, i - 1);
730 ucimap_parse(struct uci_map *map, struct uci_package *pkg)
732 struct uci_element *e;
733 struct list_head *p, *tmp;
736 INIT_LIST_HEAD(&map->fixup);
737 uci_foreach_element(&pkg->sections, e) {
738 struct uci_section *s = uci_to_section(e);
740 for (i = 0; i < map->n_sections; i++) {
741 struct uci_sectionmap *sm = map->sections[i];
742 struct ucimap_section_data *sd;
744 if (strcmp(s->type, map->sections[i]->type) != 0)
748 sd = sm->alloc(map, sm, s);
749 memset(sd, 0, sizeof(struct ucimap_section_data));
751 sd = malloc(sm->alloc_len);
752 memset(sd, 0, sm->alloc_len);
757 ucimap_parse_section(map, sm, sd, s);
762 list_for_each_safe(p, tmp, &map->fixup) {
763 struct uci_fixup *f = list_entry(p, struct uci_fixup, list);
764 ucimap_handle_fixup(map, f);
769 list_for_each_safe(p, tmp, &map->pending) {
770 struct ucimap_section_data *sd;
771 sd = list_entry(p, struct ucimap_section_data, list);
773 list_del_init(&sd->list);
774 ucimap_add_section(sd);