]> pilppa.org Git - uci.git/blobdiff - ucimap-example.c
add sanity checks for optmap section type vs sectionmap type
[uci.git] / ucimap-example.c
index 9fc71e2e104e0e1d6c1fc8023356aad5640ce7ce..c59e72c9b3e67281a7282ceac3edd97119e6da75 100644 (file)
@@ -27,7 +27,7 @@ struct uci_network {
        const char *name;
        const char *proto;
        const char *ifname;
-       unsigned char ipaddr[4];
+       unsigned char *ipaddr;
        int test;
        bool enabled;
        struct ucimap_list *aliases;
@@ -44,20 +44,42 @@ 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 = (unsigned char *) 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];
 
        return 0;
 }
 
+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)
 {
@@ -85,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;
 }
@@ -96,7 +118,7 @@ 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;
 }
@@ -139,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,
                }
        },
        {
@@ -158,7 +182,7 @@ 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
                }
        }
@@ -207,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;
@@ -216,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"
@@ -231,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);