ucimap: get rid of duplicate initialization warnings
[project/uci.git] / ucimap-example.c
1 /*
2  * ucimap-example - sample code for the ucimap library
3  * Copyright (C) 2008-2009 Felix Fietkau <nbd@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14 #include <strings.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <ucimap.h>
19 #include "list.h"
20
21 struct list_head ifs;
22
23 struct uci_network {
24         struct ucimap_section_data map;
25         struct list_head list;
26         struct list_head alias;
27
28         const char *name;
29         const char *proto;
30         const char *ifname;
31         unsigned char *ipaddr;
32         int test;
33         bool enabled;
34         struct ucimap_list *aliases;
35 };
36
37 struct uci_alias {
38         struct ucimap_section_data map;
39         struct list_head list;
40
41         const char *name;
42         struct uci_network *interface;
43 };
44
45 static int
46 network_parse_ip(void *section, struct uci_optmap *om, union ucimap_data *data, const char *str)
47 {
48         unsigned char *target;
49         int tmp[4];
50         int i;
51
52         if (sscanf(str, "%d.%d.%d.%d", &tmp[0], &tmp[1], &tmp[2], &tmp[3]) != 4)
53                 return -1;
54
55         target = malloc(4);
56         if (!target)
57                 return -1;
58
59         data->ptr = target;
60         for (i = 0; i < 4; i++)
61                 target[i] = (char) tmp[i];
62
63         return 0;
64 }
65
66 static int
67 network_format_ip(void *section, struct uci_optmap *om, union ucimap_data *data, char **str)
68 {
69         static char buf[16];
70         unsigned char *ip = (unsigned char *) data->ptr;
71
72         if (ip) {
73                 sprintf(buf, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
74                 *str = buf;
75         } else {
76                 *str = NULL;
77         }
78
79         return 0;
80 }
81
82 static void
83 network_free_ip(void *section, struct uci_optmap *om, void *ptr)
84 {
85         free(ptr);
86 }
87
88 static int
89 network_init_interface(struct uci_map *map, void *section, struct uci_section *s)
90 {
91         struct uci_network *net = section;
92
93         INIT_LIST_HEAD(&net->list);
94         INIT_LIST_HEAD(&net->alias);
95         net->name = s->e.name;
96         net->test = -1;
97         return 0;
98 }
99
100 static int
101 network_init_alias(struct uci_map *map, void *section, struct uci_section *s)
102 {
103         struct uci_alias *alias = section;
104
105         INIT_LIST_HEAD(&alias->list);
106         alias->name = s->e.name;
107         return 0;
108 }
109
110 static int
111 network_add_interface(struct uci_map *map, void *section)
112 {
113         struct uci_network *net = section;
114
115         list_add_tail(&net->list, &ifs);
116
117         return 0;
118 }
119
120 static int
121 network_add_alias(struct uci_map *map, void *section)
122 {
123         struct uci_alias *a = section;
124
125         if (a->interface)
126                 list_add_tail(&a->list, &a->interface->alias);
127
128         return 0;
129 }
130
131 static struct ucimap_section_data *
132 network_allocate(struct uci_map *map, struct uci_sectionmap *sm, struct uci_section *s)
133 {
134         struct uci_network *p = malloc(sizeof(struct uci_network));
135         if (!p)
136                 return NULL;
137         memset(p, 0, sizeof(struct uci_network));
138         return &p->map;
139 }
140
141 struct my_optmap {
142         struct uci_optmap map;
143         int test;
144 };
145
146 static struct uci_sectionmap network_interface;
147 static struct uci_sectionmap network_alias;
148
149 static struct my_optmap network_interface_options[] = {
150         {
151                 .map = {
152                         UCIMAP_OPTION(struct uci_network, proto),
153                         .type = UCIMAP_STRING,
154                         .data.s.maxlen = 32,
155                 }
156         },
157         {
158                 .map = {
159                         UCIMAP_OPTION(struct uci_network, ifname),
160                         .type = UCIMAP_STRING,
161                 }
162         },
163         {
164                 .map = {
165                         UCIMAP_OPTION(struct uci_network, ipaddr),
166                         .type = UCIMAP_CUSTOM,
167                         .parse = network_parse_ip,
168                         .format = network_format_ip,
169                         .free = network_free_ip,
170                 }
171         },
172         {
173                 .map = {
174                         UCIMAP_OPTION(struct uci_network, enabled),
175                         .type = UCIMAP_BOOL,
176                 }
177         },
178         {
179                 .map = {
180                         UCIMAP_OPTION(struct uci_network, test),
181                         .type = UCIMAP_INT,
182                 }
183         },
184         {
185                 .map = {
186                         UCIMAP_OPTION(struct uci_network, aliases),
187                         .type = UCIMAP_LIST | UCIMAP_SECTION | UCIMAP_LIST_AUTO,
188                         .data.sm = &network_alias
189                 }
190         }
191 };
192
193 static struct uci_sectionmap network_interface = {
194         UCIMAP_SECTION(struct uci_network, map),
195         .type = "interface",
196         .alloc = network_allocate,
197         .init = network_init_interface,
198         .add = network_add_interface,
199         .options = &network_interface_options[0].map,
200         .n_options = ARRAY_SIZE(network_interface_options),
201         .options_size = sizeof(struct my_optmap)
202 };
203
204 static struct uci_optmap network_alias_options[] = {
205         {
206                 UCIMAP_OPTION(struct uci_alias, interface),
207                 .type = UCIMAP_SECTION,
208                 .data.sm = &network_interface
209         }
210 };
211
212 static struct uci_sectionmap network_alias = {
213         UCIMAP_SECTION(struct uci_alias, map),
214         .type = "alias",
215         .options = network_alias_options,
216         .init = network_init_alias,
217         .add = network_add_alias,
218         .n_options = ARRAY_SIZE(network_alias_options),
219 };
220
221 static struct uci_sectionmap *network_smap[] = {
222         &network_interface,
223         &network_alias,
224 };
225
226 static struct uci_map network_map = {
227         .sections = network_smap,
228         .n_sections = ARRAY_SIZE(network_smap),
229 };
230
231
232 int main(int argc, char **argv)
233 {
234         struct uci_context *ctx;
235         struct uci_package *pkg;
236         struct list_head *p;
237         struct uci_network *net;
238         struct uci_alias *alias;
239         bool set = false;
240         int i;
241
242         INIT_LIST_HEAD(&ifs);
243         ctx = uci_alloc_context();
244         if (!ctx)
245                 return -1;
246         ucimap_init(&network_map);
247
248         if ((argc >= 2) && !strcmp(argv[1], "-s")) {
249                 uci_set_savedir(ctx, "./test/save");
250                 set = true;
251         }
252
253         uci_set_confdir(ctx, "./test/config");
254         uci_load(ctx, "network", &pkg);
255
256         ucimap_parse(&network_map, pkg);
257
258         list_for_each(p, &ifs) {
259                 const unsigned char *ipaddr;
260                 int n_aliases = 0;
261
262                 net = list_entry(p, struct uci_network, list);
263                 ipaddr = net->ipaddr;
264                 if (!ipaddr)
265                         ipaddr = (const unsigned char *) "\x00\x00\x00\x00";
266
267                 printf("New network section '%s'\n"
268                         "       type: %s\n"
269                         "       ifname: %s\n"
270                         "       ipaddr: %d.%d.%d.%d\n"
271                         "       test: %d\n"
272                         "       enabled: %s\n",
273                         net->name,
274                         net->proto,
275                         net->ifname,
276                         ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3],
277                         net->test,
278                         (net->enabled ? "on" : "off"));
279
280                 if (net->aliases->n_items > 0) {
281                         printf("Configured aliases:");
282                         for (i = 0; i < net->aliases->n_items; i++) {
283                                 alias = net->aliases->item[i].ptr;
284                                 printf(" %s", alias->name);
285                         }
286                         printf("\n");
287                 }
288                 list_for_each_entry(alias, &net->alias, list) {
289                         n_aliases++;
290                         for (i = 0; i < net->aliases->n_items; i++) {
291                                 if (alias == net->aliases->item[i].ptr)
292                                         goto next_alias;
293                         }
294                         printf("New alias: %s\n", alias->name);
295 next_alias:
296                         continue;
297                 }
298                 if (set && !strcmp(net->name, "lan")) {
299                         ucimap_free_item(&net->map, &net->ipaddr);
300                         ucimap_set_changed(&net->map, &net->ipaddr);
301                         ucimap_resize_list(&net->map, &net->aliases, n_aliases);
302                         net->aliases->n_items = 0;
303                         list_for_each_entry(alias, &net->alias, list) {
304                                 net->aliases->item[net->aliases->n_items++].ptr = alias;
305                         }
306                         ucimap_set_changed(&net->map, &net->aliases);
307                         ucimap_store_section(&network_map, pkg, &net->map);
308                         uci_save(ctx, pkg);
309                 }
310         }
311
312
313         ucimap_cleanup(&network_map);
314         uci_free_context(ctx);
315
316         return 0;
317 }