#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
+#include <stdio.h>
#include <ctype.h>
#include "ucimap.h"
+#include "uci_internal.h"
struct uci_alloc {
enum ucimap_type type;
int
ucimap_init(struct uci_map *map)
{
+ INIT_LIST_HEAD(&map->pending);
INIT_LIST_HEAD(&map->sdata);
INIT_LIST_HEAD(&map->fixup);
return 0;
continue;
return ucimap_section_ptr(sd);
}
+ list_for_each(p, &map->pending) {
+ sd = list_entry(p, struct ucimap_section_data, list);
+ if (sd->sm != f->sm)
+ continue;
+ if (strcmp(f->name, sd->section_name) != 0)
+ continue;
+ return ucimap_section_ptr(sd);
+ }
return NULL;
}
switch(f->type & UCIMAP_TYPE) {
case UCIMAP_SIMPLE:
- f->data->section = ptr;
+ f->data->ptr = ptr;
break;
case UCIMAP_LIST:
list = f->data->list;
- list->item[list->n_items++].section = ptr;
+ list->item[list->n_items++].ptr = ptr;
break;
}
return true;
}
static void
-ucimap_add_fixup(struct uci_map *map, union ucimap_data *data, struct uci_optmap *om, const char *str)
+ucimap_add_fixup(struct ucimap_section_data *sd, union ucimap_data *data, struct uci_optmap *om, const char *str)
{
struct uci_fixup *f, tmp;
+ struct uci_map *map = sd->map;
INIT_LIST_HEAD(&tmp.list);
tmp.sm = om->data.sm;
return;
break;
case UCIMAP_SECTION:
- ucimap_add_fixup(sd->map, data, om, str);
+ ucimap_add_fixup(sd, data, om, str);
return;
case UCIMAP_CUSTOM:
tdata.s = (char *) data;
return 0;
}
+static void
+ucimap_add_section(struct ucimap_section_data *sd)
+{
+ struct uci_map *map = sd->map;
+
+ if (sd->sm->add(map, ucimap_section_ptr(sd)) < 0)
+ ucimap_free_section(map, sd);
+ else
+ list_add_tail(&sd->list, &map->sdata);
+}
+
+static const char *ucimap_type_names[] = {
+ [UCIMAP_STRING] = "string",
+ [UCIMAP_INT] = "integer",
+ [UCIMAP_BOOL] = "boolean",
+ [UCIMAP_SECTION] = "section",
+ [UCIMAP_LIST] = "list",
+};
+
+static inline const char *
+ucimap_get_type_name(int type)
+{
+ static char buf[32];
+ const char *name;
+
+ if (ucimap_is_list(type))
+ return ucimap_type_names[UCIMAP_LIST];
+
+ name = ucimap_type_names[type & UCIMAP_SUBTYPE];
+ if (!name) {
+ sprintf(buf, "Unknown (%d)", type & UCIMAP_SUBTYPE);
+ name = buf;
+ }
+
+ return name;
+}
+
+static bool
+ucimap_check_optmap_type(struct uci_sectionmap *sm, struct uci_optmap *om)
+{
+ unsigned int type;
+
+ if (om->detected_type < 0)
+ return true;
+
+ if (ucimap_is_custom(om->type))
+ return true;
+
+ if (ucimap_is_list(om->type) !=
+ ucimap_is_list(om->detected_type))
+ goto failed;
+
+ if (ucimap_is_list(om->type))
+ return true;
+
+ type = om->type & UCIMAP_SUBTYPE;
+ switch(type) {
+ case UCIMAP_STRING:
+ case UCIMAP_INT:
+ case UCIMAP_BOOL:
+ if (type != om->detected_type)
+ goto failed;
+ break;
+ case UCIMAP_SECTION:
+ goto failed;
+ default:
+ break;
+ }
+ return true;
+
+failed:
+ DPRINTF("Invalid type in option '%s' of section type '%s', "
+ "declared type is %s, detected type is %s\n",
+ om->name, sm->type,
+ ucimap_get_type_name(om->type),
+ ucimap_get_type_name(om->detected_type));
+ return false;
+}
int
ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s)
sd->sm = sm;
ucimap_foreach_option(sm, om) {
+ if (!ucimap_check_optmap_type(sm, om))
+ continue;
+
if (ucimap_is_list(om->type)) {
union ucimap_data *data;
struct uci_element *e;
if (err)
goto error;
- list_add(&sd->list, &map->sdata);
+ if (map->parsed) {
+ ucimap_add_section(sd);
+ } else {
+ list_add_tail(&sd->list, &map->pending);
+ }
+
err = ucimap_parse_options(map, sm, sd, s);
if (err)
goto error;
ucimap_parse_section(map, sm, sd, s);
}
}
+ map->parsed = true;
+
list_for_each_safe(p, tmp, &map->fixup) {
struct uci_fixup *f = list_entry(p, struct uci_fixup, list);
ucimap_handle_fixup(map, f);
list_del(&f->list);
free(f);
}
- list_for_each_safe(p, tmp, &map->sdata) {
- struct ucimap_section_data *sd = list_entry(p, struct ucimap_section_data, list);
- void *section;
- if (sd->done)
- continue;
+ list_for_each_safe(p, tmp, &map->pending) {
+ struct ucimap_section_data *sd;
+ sd = list_entry(p, struct ucimap_section_data, list);
- section = ucimap_section_ptr(sd);
- if (sd->sm->add(map, section) != 0)
- ucimap_free_section(map, sd);
+ list_del_init(&sd->list);
+ ucimap_add_section(sd);
}
}