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.
22 enum ucimap_type type;
29 struct list_head list;
30 struct uci_sectionmap *sm;
32 enum ucimap_type type;
33 union ucimap_data *data;
36 #define ucimap_foreach_option(_sm, _o) \
37 if (!(_sm)->options_size) \
38 (_sm)->options_size = sizeof(struct uci_optmap); \
39 for (_o = &(_sm)->options[0]; \
40 ((char *)(_o)) < ((char *) &(_sm)->options[0] + \
41 (_sm)->options_size * (_sm)->n_options); \
42 _o = (struct uci_optmap *) ((char *)(_o) + \
47 ucimap_is_alloc(enum ucimap_type type)
49 switch(type & UCIMAP_SUBTYPE) {
58 ucimap_is_fixup(enum ucimap_type type)
60 switch(type & UCIMAP_SUBTYPE) {
69 ucimap_is_simple(enum ucimap_type type)
71 return ((type & UCIMAP_TYPE) == UCIMAP_SIMPLE);
75 ucimap_is_list(enum ucimap_type type)
77 return ((type & UCIMAP_TYPE) == UCIMAP_LIST);
81 ucimap_is_custom(enum ucimap_type type)
83 return ((type & UCIMAP_SUBTYPE) == UCIMAP_CUSTOM);
87 ucimap_section_ptr(struct ucimap_section_data *sd)
89 return ((char *) sd - sd->sm->smap_offset);
92 static inline union ucimap_data *
93 ucimap_get_data(struct ucimap_section_data *sd, struct uci_optmap *om)
97 data = (char *) ucimap_section_ptr(sd) + om->offset;
102 ucimap_init(struct uci_map *map)
104 INIT_LIST_HEAD(&map->sdata);
105 INIT_LIST_HEAD(&map->fixup);
110 ucimap_free_item(struct uci_alloc *a)
112 switch(a->type & UCIMAP_TYPE) {
121 ucimap_add_alloc(struct ucimap_section_data *sd, void *ptr)
123 struct uci_alloc *a = &sd->allocmap[sd->allocmap_len++];
124 a->type = UCIMAP_SIMPLE;
129 ucimap_free_section(struct uci_map *map, struct ucimap_section_data *sd)
134 section = ucimap_section_ptr(sd);
135 if (!list_empty(&sd->list))
139 sd->sm->free(map, section);
141 for (i = 0; i < sd->allocmap_len; i++) {
142 ucimap_free_item(&sd->allocmap[i]);
150 ucimap_cleanup(struct uci_map *map)
152 struct list_head *ptr, *tmp;
154 list_for_each_safe(ptr, tmp, &map->sdata) {
155 struct ucimap_section_data *sd = list_entry(ptr, struct ucimap_section_data, list);
156 ucimap_free_section(map, sd);
161 ucimap_add_fixup(struct uci_map *map, union ucimap_data *data, struct uci_optmap *om, const char *str)
165 f = malloc(sizeof(struct uci_fixup));
169 INIT_LIST_HEAD(&f->list);
174 list_add(&f->list, &map->fixup);
178 ucimap_add_value(union ucimap_data *data, struct uci_optmap *om, struct ucimap_section_data *sd, const char *str)
180 union ucimap_data tdata = *data;
185 if (ucimap_is_list(om->type) && !ucimap_is_fixup(om->type))
186 data = &data->list->item[data->list->n_items++];
188 switch(om->type & UCIMAP_SUBTYPE) {
190 if ((om->data.s.maxlen > 0) &&
191 (strlen(str) > om->data.s.maxlen))
196 ucimap_add_alloc(sd, s);
200 if (strcmp(str, "on"))
202 else if (strcmp(str, "1"))
204 else if (strcmp(str, "enabled"))
206 else if (strcmp(str, "off"))
208 else if (strcmp(str, "0"))
210 else if (strcmp(str, "disabled"))
218 val = strtol(str, &eptr, om->data.i.base);
219 if (!eptr || *eptr == '\0')
225 ucimap_add_fixup(sd->map, data, om, str);
228 tdata.s = (char *) data;
232 if (om->parse(ucimap_section_ptr(sd), om, &tdata, str) < 0)
235 if (ucimap_is_custom(om->type))
237 memcpy(data, &tdata, sizeof(union ucimap_data));
242 ucimap_parse_options(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s)
244 struct uci_element *e, *l;
245 struct uci_option *o;
246 union ucimap_data *data;
248 uci_foreach_element(&s->options, e) {
249 struct uci_optmap *om = NULL, *tmp;
251 ucimap_foreach_option(sm, tmp) {
252 if (strcmp(e->name, tmp->name) == 0) {
260 data = ucimap_get_data(sd, om);
261 o = uci_to_option(e);
262 if ((o->type == UCI_TYPE_STRING) && ucimap_is_simple(om->type)) {
263 ucimap_add_value(data, om, sd, o->v.string);
264 } else if ((o->type == UCI_TYPE_LIST) && ucimap_is_list(om->type)) {
265 uci_foreach_element(&o->v.list, l) {
266 ucimap_add_value(data, om, sd, l->name);
276 ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct uci_section *s)
278 struct ucimap_section_data *sd = NULL;
279 struct uci_optmap *om;
285 sd = malloc(sm->alloc_len);
289 memset(sd, 0, sm->alloc_len);
290 INIT_LIST_HEAD(&sd->list);
294 ucimap_foreach_option(sm, om) {
295 if (ucimap_is_list(om->type)) {
296 union ucimap_data *data;
297 struct uci_element *e;
301 data = ucimap_get_data(sd, om);
302 uci_foreach_element(&s->options, e) {
303 struct uci_option *o = uci_to_option(e);
304 struct uci_element *tmp;
306 if (strcmp(e->name, om->name) != 0)
309 uci_foreach_element(&o->v.list, tmp) {
314 n_alloc += n_elements + 1;
315 size = sizeof(struct ucimap_list) +
316 n_elements * sizeof(union ucimap_data);
317 data->list = malloc(size);
318 memset(data->list, 0, size);
319 } else if (ucimap_is_alloc(om->type)) {
324 sd->allocmap = malloc(n_alloc * sizeof(struct uci_alloc));
328 section_name = strdup(s->e.name);
332 sd->section_name = section_name;
334 sd->cmap = malloc(BITFIELD_SIZE(sm->n_options));
338 memset(sd->cmap, 0, BITFIELD_SIZE(sm->n_options));
339 ucimap_add_alloc(sd, (void *)section_name);
340 ucimap_add_alloc(sd, (void *)sd->cmap);
341 ucimap_foreach_option(sm, om) {
342 if (!ucimap_is_list(om->type))
345 ucimap_add_alloc(sd, ucimap_get_data(sd, om)->list);
348 section = ucimap_section_ptr(sd);
349 err = sm->init(map, section, s);
353 list_add(&sd->list, &map->sdata);
354 err = ucimap_parse_options(map, sm, sd, s);
367 ucimap_free_section(map, sd);
372 ucimap_fill_ptr(struct uci_ptr *ptr, struct uci_section *s, const char *option)
374 struct uci_package *p = s->package;
376 memset(ptr, 0, sizeof(struct uci_ptr));
378 ptr->package = p->e.name;
381 ptr->section = s->e.name;
384 ptr->option = option;
385 return uci_lookup_ptr(p->ctx, ptr, NULL, false);
389 ucimap_set_changed(struct ucimap_section_data *sd, void *field)
391 void *section = ucimap_section_ptr(sd);
392 struct uci_sectionmap *sm = sd->sm;
393 struct uci_optmap *om;
394 int ofs = (char *)field - (char *)section;
397 ucimap_foreach_option(sm, om) {
398 if (om->offset == ofs) {
399 SET_BIT(sd->cmap, i);
407 ucimap_store_section(struct uci_map *map, struct uci_package *p, void *section)
409 char *sptr = (char *)section - sizeof(struct ucimap_section_data);
410 struct ucimap_section_data *sd = (struct ucimap_section_data *) sptr;
411 struct uci_sectionmap *sm = sd->sm;
412 struct uci_section *s = NULL;
413 struct uci_optmap *om;
414 struct uci_element *e;
419 uci_foreach_element(&p->sections, e) {
420 if (!strcmp(e->name, sd->section_name)) {
421 s = uci_to_section(e);
426 return UCI_ERR_NOTFOUND;
428 ucimap_foreach_option(sm, om) {
429 union ucimap_data *data;
431 const char *str = NULL;
433 if (ucimap_is_list(om->type))
436 data = ucimap_get_data(sd, om);
437 if (!TEST_BIT(sd->cmap, i))
440 ucimap_fill_ptr(&ptr, s, om->name);
441 switch(om->type & UCIMAP_SUBTYPE) {
446 sprintf(buf, "%d", data->i);
450 sprintf(buf, "%d", !!data->b);
458 ret = uci_set(s->package->ctx, &ptr);
462 CLR_BIT(sd->cmap, i);
470 ucimap_find_section(struct uci_map *map, struct uci_fixup *f)
472 struct ucimap_section_data *sd;
475 list_for_each(p, &map->sdata) {
476 sd = list_entry(p, struct ucimap_section_data, list);
479 if (strcmp(f->name, sd->section_name) != 0)
481 return ucimap_section_ptr(sd);
487 ucimap_parse(struct uci_map *map, struct uci_package *pkg)
489 struct uci_element *e;
490 struct list_head *p, *tmp;
493 INIT_LIST_HEAD(&map->fixup);
494 uci_foreach_element(&pkg->sections, e) {
495 struct uci_section *s = uci_to_section(e);
497 for (i = 0; i < map->n_sections; i++) {
498 if (strcmp(s->type, map->sections[i]->type) != 0)
500 ucimap_parse_section(map, map->sections[i], s);
503 list_for_each_safe(p, tmp, &map->fixup) {
504 struct uci_fixup *f = list_entry(p, struct uci_fixup, list);
505 void *ptr = ucimap_find_section(map, f);
506 struct ucimap_list *list;
511 switch(f->type & UCIMAP_TYPE) {
513 f->data->section = ptr;
516 list = f->data->list;
517 list->item[list->n_items++].section = ptr;
522 list_for_each_safe(p, tmp, &map->sdata) {
523 struct ucimap_section_data *sd = list_entry(p, struct ucimap_section_data, list);
529 section = ucimap_section_ptr(sd);
530 if (sd->sm->add(map, section) != 0)
531 ucimap_free_section(map, sd);