ucimap: add custom free() callbacks for options, only used on custom datatypes
[project/uci.git] / ucimap-example.c
index 0ad31d6..c59e72c 100644 (file)
 struct list_head ifs;
 
 struct uci_network {
+       struct ucimap_section_data map;
        struct list_head list;
        struct list_head alias;
 
        const char *name;
        const char *proto;
        const char *ifname;
-       unsigned char ipaddr[4];
+       unsigned char *ipaddr;
        int test;
        bool enabled;
        struct ucimap_list *aliases;
 };
 
 struct uci_alias {
+       struct ucimap_section_data map;
        struct list_head list;
 
        const char *name;
@@ -42,14 +44,18 @@ struct uci_alias {
 static int
 network_parse_ip(void *section, struct uci_optmap *om, union ucimap_data *data, const char *str)
 {
-       struct uci_network *net = section;
-       unsigned char *target = data->s;
-       unsigned int tmp[4];
+       unsigned char *target;
+       int tmp[4];
        int i;
 
        if (sscanf(str, "%d.%d.%d.%d", &tmp[0], &tmp[1], &tmp[2], &tmp[3]) != 4)
                return -1;
 
+       target = malloc(4);
+       if (!target)
+               return -1;
+
+       *data->data = target;
        for (i = 0; i < 4; i++)
                target[i] = (char) tmp[i];
 
@@ -57,6 +63,24 @@ network_parse_ip(void *section, struct uci_optmap *om, union ucimap_data *data,
 }
 
 static int
+network_format_ip(void *sction, struct uci_optmap *om, union ucimap_data *data, char **str)
+{
+       static char buf[16];
+       unsigned char *ip = (unsigned char *) data->data[0];
+
+       sprintf(buf, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
+       *str = buf;
+
+       return 0;
+}
+
+static void
+network_free_ip(void *section, struct uci_optmap *om, void *ptr)
+{
+       free(ptr);
+}
+
+static int
 network_init_interface(struct uci_map *map, void *section, struct uci_section *s)
 {
        struct uci_network *net = section;
@@ -83,7 +107,7 @@ network_add_interface(struct uci_map *map, void *section)
 {
        struct uci_network *net = section;
 
-       list_add(&net->list, &ifs);
+       list_add_tail(&net->list, &ifs);
 
        return 0;
 }
@@ -94,18 +118,26 @@ network_add_alias(struct uci_map *map, void *section)
        struct uci_alias *a = section;
 
        if (a->interface)
-               list_add(&a->list, &a->interface->alias);
+               list_add_tail(&a->list, &a->interface->alias);
 
        return 0;
 }
 
+static struct ucimap_section_data *
+network_allocate(struct uci_map *map, struct uci_sectionmap *sm, struct uci_section *s)
+{
+       struct uci_network *p = malloc(sizeof(struct uci_network));
+       memset(p, 0, sizeof(struct uci_network));
+       return &p->map;
+}
+
 struct my_optmap {
        struct uci_optmap map;
        int test;
 };
 
-static struct uci_sectmap network_interface;
-static struct uci_sectmap network_alias;
+static struct uci_sectionmap network_interface;
+static struct uci_sectionmap network_alias;
 
 static struct my_optmap network_interface_options[] = {
        {
@@ -129,6 +161,8 @@ static struct my_optmap network_interface_options[] = {
                        .type = UCIMAP_CUSTOM,
                        .name = "ipaddr",
                        .parse = network_parse_ip,
+                       .format = network_format_ip,
+                       .free = network_free_ip,
                }
        },
        {
@@ -148,15 +182,16 @@ static struct my_optmap network_interface_options[] = {
        {
                .map = {
                        UCIMAP_OPTION(struct uci_network, aliases),
-                       .type = UCIMAP_LIST | UCIMAP_SECTION,
+                       .type = UCIMAP_LIST | UCIMAP_SECTION | UCIMAP_LIST_AUTO,
                        .data.sm = &network_alias
                }
        }
 };
 
-static struct uci_sectmap network_interface = {
+static struct uci_sectionmap network_interface = {
+       UCIMAP_SECTION(struct uci_network, map),
        .type = "interface",
-       .alloc_len = sizeof(struct uci_network),
+       .alloc = network_allocate,
        .init = network_init_interface,
        .add = network_add_interface,
        .options = &network_interface_options[0].map,
@@ -172,16 +207,16 @@ static struct uci_optmap network_alias_options[] = {
        }
 };
 
-static struct uci_sectmap network_alias = {
+static struct uci_sectionmap network_alias = {
+       UCIMAP_SECTION(struct uci_alias, map),
        .type = "alias",
        .options = network_alias_options,
-       .alloc_len = sizeof(struct uci_network),
        .init = network_init_alias,
        .add = network_add_alias,
        .n_options = ARRAY_SIZE(network_alias_options),
 };
 
-static struct uci_sectmap *network_smap[] = {
+static struct uci_sectionmap *network_smap[] = {
        &network_interface,
        &network_alias,
 };
@@ -196,7 +231,7 @@ int main(int argc, char **argv)
 {
        struct uci_context *ctx;
        struct uci_package *pkg;
-       struct list_head *p, *p2;
+       struct list_head *p;
        struct uci_network *net;
        struct uci_alias *alias;
        int i;
@@ -205,12 +240,19 @@ int main(int argc, char **argv)
        ctx = uci_alloc_context();
        ucimap_init(&network_map);
 
+       uci_set_confdir(ctx, "./test/config");
        uci_load(ctx, "network", &pkg);
 
        ucimap_parse(&network_map, pkg);
 
        list_for_each(p, &ifs) {
+               const unsigned char *ipaddr;
+
                net = list_entry(p, struct uci_network, list);
+               ipaddr = net->ipaddr;
+               if (!ipaddr)
+                       ipaddr = (const unsigned char *) "\x00\x00\x00\x00";
+
                printf("New network section '%s'\n"
                        "       type: %s\n"
                        "       ifname: %s\n"
@@ -220,25 +262,23 @@ int main(int argc, char **argv)
                        net->name,
                        net->proto,
                        net->ifname,
-                       net->ipaddr[0], net->ipaddr[1],
-                       net->ipaddr[2], net->ipaddr[3],
+                       ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3],
                        net->test,
                        (net->enabled ? "on" : "off"));
 
                for (i = 0; i < net->aliases->n_items; i++) {
-                       alias = net->aliases->item[i].section;
+                       alias = net->aliases->item[i].ptr;
                        printf("New alias: %s\n", alias->name);
                }
 #if 0
-               net->ipaddr = "2.3.4.5";
-               ucimap_set_changed(net, &net->ipaddr);
-               ucimap_store_section(&network_map, pkg, net);
+               memcpy(net->ipaddr, "\x01\x03\x04\x05", 4);
+               ucimap_set_changed(&net->map, &net->ipaddr);
+               ucimap_store_section(&network_map, pkg, &net->map);
                uci_save(ctx, pkg);
 #endif
        }
 
 
-done:
        ucimap_cleanup(&network_map);
        uci_free_context(ctx);