+static void
+ucimap_add_section_list(struct uci_map *map, struct ucimap_section_data *sd)
+{
+ sd->ref = map->sdata_tail;
+ *sd->ref = sd;
+ map->sdata_tail = &sd->next;
+}
+
+static void
+ucimap_add_section(struct ucimap_section_data *sd)
+{
+ struct uci_map *map = sd->map;
+
+ sd->next = NULL;
+ if (sd->sm->add(map, ucimap_section_ptr(sd)) < 0)
+ ucimap_free_section(map, sd);
+ else
+ ucimap_add_section_list(map, sd);
+}
+
+#ifdef UCI_DEBUG
+static const char *ucimap_type_names[] = {
+ [UCIMAP_STRING] = "string",
+ [UCIMAP_INT] = "integer",
+ [UCIMAP_BOOL] = "boolean",
+ [UCIMAP_SECTION] = "section",
+ [UCIMAP_LIST] = "list",
+};
+
+static 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;
+}
+#endif
+
+static bool
+ucimap_check_optmap_type(struct uci_sectionmap *sm, struct uci_optmap *om)
+{
+ unsigned int type;
+
+ if (unlikely(sm->type_name != om->type_name) &&
+ unlikely(strcmp(sm->type_name, om->type_name) != 0)) {
+ DPRINTF("Option '%s' of section type '%s' refereces unknown "
+ "section type '%s', should be '%s'.\n",
+ om->name, sm->type, om->type_name, sm->type_name);
+ return false;
+ }
+
+ 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;
+}
+
+static void
+ucimap_count_alloc(struct uci_optmap *om, int *n_alloc, int *n_custom)
+{
+ if (ucimap_is_alloc(om->type))
+ (*n_alloc)++;
+ else if (ucimap_is_custom(om->type) && om->free)
+ (*n_custom)++;
+}