ucimap: add custom free() callbacks for options, only used on custom datatypes
[project/uci.git] / ucimap.c
index 2bff545..6d97b07 100644 (file)
--- a/ucimap.c
+++ b/ucimap.c
 #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;
@@ -27,6 +29,12 @@ struct uci_alloc {
        } data;
 };
 
+struct uci_alloc_custom {
+       void *section;
+       struct uci_optmap *om;
+       void *ptr;
+};
+
 struct uci_fixup {
        struct list_head list;
        struct uci_sectionmap *sm;
@@ -134,7 +142,7 @@ ucimap_add_alloc(struct ucimap_section_data *sd, void *ptr)
        a->data.ptr = ptr;
 }
 
-static void
+void
 ucimap_free_section(struct uci_map *map, struct ucimap_section_data *sd)
 {
        void *section;
@@ -151,6 +159,14 @@ ucimap_free_section(struct uci_map *map, struct ucimap_section_data *sd)
                ucimap_free_item(&sd->allocmap[i]);
        }
 
+       if (sd->alloc_custom) {
+               for (i = 0; i < sd->alloc_custom_len; i++) {
+                       struct uci_alloc_custom *a = &sd->alloc_custom[i];
+                       a->om->free(a->section, a->om, a->ptr);
+               }
+               free(sd->alloc_custom);
+       }
+
        free(sd->allocmap);
        free(sd);
 }
@@ -213,9 +229,10 @@ ucimap_handle_fixup(struct uci_map *map, struct uci_fixup *f)
 }
 
 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;
@@ -234,6 +251,16 @@ ucimap_add_fixup(struct uci_map *map, union ucimap_data *data, struct uci_optmap
 }
 
 static void
+ucimap_add_custom_alloc(struct ucimap_section_data *sd, struct uci_optmap *om, void *ptr)
+{
+       struct uci_alloc_custom *a = &sd->alloc_custom[sd->alloc_custom_len++];
+
+       a->section = ucimap_section_ptr(sd);
+       a->om = om;
+       a->ptr = ptr;
+}
+
+static void
 ucimap_add_value(union ucimap_data *data, struct uci_optmap *om, struct ucimap_section_data *sd, const char *str)
 {
        union ucimap_data tdata = *data;
@@ -284,7 +311,7 @@ ucimap_add_value(union ucimap_data *data, struct uci_optmap *om, struct ucimap_s
                        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;
@@ -293,6 +320,10 @@ ucimap_add_value(union ucimap_data *data, struct uci_optmap *om, struct ucimap_s
        if (om->parse) {
                if (om->parse(ucimap_section_ptr(sd), om, &tdata, str) < 0)
                        return;
+               if (ucimap_is_custom(om->type) && om->free) {
+                       if (tdata.ptr != data->ptr)
+                               ucimap_add_custom_alloc(sd, om, data->ptr);
+               }
        }
        if (ucimap_is_custom(om->type))
                return;
@@ -377,6 +408,82 @@ ucimap_add_section(struct ucimap_section_data *sd)
                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;
+}
+
+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)++;
+}
 
 int
 ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s)
@@ -385,6 +492,7 @@ ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucim
        char *section_name;
        void *section;
        int n_alloc = 2;
+       int n_alloc_custom = 0;
        int err;
 
        INIT_LIST_HEAD(&sd->list);
@@ -392,10 +500,14 @@ ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucim
        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;
                        int n_elements = 0;
+                       int n_elements_custom = 0;
                        int size;
 
                        data = ucimap_get_data(sd, om);
@@ -408,7 +520,7 @@ ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucim
 
                                if (o->type == UCI_TYPE_LIST) {
                                        uci_foreach_element(&o->v.list, tmp) {
-                                               n_elements++;
+                                               ucimap_count_alloc(om, &n_elements, &n_elements_custom);
                                        }
                                } else if ((o->type == UCI_TYPE_STRING) &&
                                           ucimap_is_list_auto(om->type)) {
@@ -421,43 +533,50 @@ ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucim
                                                        break;
 
                                                n_elements++;
+                                               ucimap_count_alloc(om, &n_elements, &n_elements_custom);
 
                                                while (*data && !isspace(*data))
                                                        data++;
                                        } while (*data);
 
                                        /* for the duplicated data string */
-                                       if (n_elements > 0)
+                                       if (n_elements)
                                                n_alloc++;
                                }
                                break;
                        }
                        /* add one more for the ucimap_list */
                        n_alloc += n_elements + 1;
+                       n_alloc_custom += n_elements_custom;
                        size = sizeof(struct ucimap_list) +
                                n_elements * sizeof(union ucimap_data);
                        data->list = malloc(size);
                        memset(data->list, 0, size);
-               } else if (ucimap_is_alloc(om->type)) {
-                       n_alloc++;
+               } else {
+                       ucimap_count_alloc(om, &n_alloc, &n_alloc_custom);
                }
        }
 
-       sd->allocmap = malloc(n_alloc * sizeof(struct uci_alloc));
+       sd->allocmap = calloc(n_alloc, sizeof(struct uci_alloc));
        if (!sd->allocmap)
                goto error_mem;
 
+       if (n_alloc_custom > 0) {
+               sd->alloc_custom = calloc(n_alloc_custom, sizeof(struct uci_alloc_custom));
+               if (!sd->alloc_custom)
+                       goto error_mem;
+       }
+
        section_name = strdup(s->e.name);
        if (!section_name)
                goto error_mem;
 
        sd->section_name = section_name;
 
-       sd->cmap = malloc(BITFIELD_SIZE(sm->n_options));
+       sd->cmap = calloc(1, BITFIELD_SIZE(sm->n_options));
        if (!sd->cmap)
                goto error_mem;
 
-       memset(sd->cmap, 0, BITFIELD_SIZE(sm->n_options));
        ucimap_add_alloc(sd, (void *)section_name);
        ucimap_add_alloc(sd, (void *)sd->cmap);
        ucimap_foreach_option(sm, om) {