ucimap: add custom free() callbacks for options, only used on custom datatypes
[project/uci.git] / ucimap-example.c
1 /*
2 * ucimap-example - sample code for the ucimap library
3 * Copyright (C) 2008 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
20 struct list_head ifs;
21
22 struct uci_network {
23 struct ucimap_section_data map;
24 struct list_head list;
25 struct list_head alias;
26
27 const char *name;
28 const char *proto;
29 const char *ifname;
30 unsigned char *ipaddr;
31 int test;
32 bool enabled;
33 struct ucimap_list *aliases;
34 };
35
36 struct uci_alias {
37 struct ucimap_section_data map;
38 struct list_head list;
39
40 const char *name;
41 struct uci_network *interface;
42 };
43
44 static int
45 network_parse_ip(void *section, struct uci_optmap *om, union ucimap_data *data, const char *str)
46 {
47 unsigned char *target;
48 int tmp[4];
49 int i;
50
51 if (sscanf(str, "%d.%d.%d.%d", &tmp[0], &tmp[1], &tmp[2], &tmp[3]) != 4)
52 return -1;
53
54 target = malloc(4);
55 if (!target)
56 return -1;
57
58 *data->data = target;
59 for (i = 0; i < 4; i++)
60 target[i] = (char) tmp[i];
61
62 return 0;
63 }
64
65 static int
66 network_format_ip(void *sction, struct uci_optmap *om, union ucimap_data *data, char **str)
67 {
68 static char buf[16];
69 unsigned char *ip = (unsigned char *) data->data[0];
70
71 sprintf(buf, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
72 *str = buf;
73
74 return 0;
75 }
76
77 static void
78 network_free_ip(void *section, struct uci_optmap *om, void *ptr)
79 {
80 free(ptr);
81 }
82
83 static int
84 network_init_interface(struct uci_map *map, void *section, struct uci_section *s)
85 {
86 struct uci_network *net = section;
87
88 INIT_LIST_HEAD(&net->list);
89 INIT_LIST_HEAD(&net->alias);
90 net->name = s->e.name;
91 net->test = -1;
92 return 0;
93 }
94
95 static int
96 network_init_alias(struct uci_map *map, void *section, struct uci_section *s)
97 {
98 struct uci_alias *alias = section;
99
100 INIT_LIST_HEAD(&alias->list);
101 alias->name = s->e.name;
102 return 0;
103 }
104
105 static int
106 network_add_interface(struct uci_map *map, void *section)
107 {
108 struct uci_network *net = section;
109
110 list_add_tail(&net->list, &ifs);
111
112 return 0;
113 }
114
115 static int
116 network_add_alias(struct uci_map *map, void *section)
117 {
118 struct uci_alias *a = section;
119
120 if (a->interface)
121 list_add_tail(&a->list, &a->interface->alias);
122
123 return 0;
124 }
125
126 static struct ucimap_section_data *
127 network_allocate(struct uci_map *map, struct uci_sectionmap *sm, struct uci_section *s)
128 {
129 struct uci_network *p = malloc(sizeof(struct uci_network));
130 memset(p, 0, sizeof(struct uci_network));
131 return &p->map;
132 }
133
134 struct my_optmap {
135 struct uci_optmap map;
136 int test;
137 };
138
139 static struct uci_sectionmap network_interface;
140 static struct uci_sectionmap network_alias;
141
142 static struct my_optmap network_interface_options[] = {
143 {
144 .map = {
145 UCIMAP_OPTION(struct uci_network, proto),
146 .type = UCIMAP_STRING,
147 .name = "proto",
148 .data.s.maxlen = 32,
149 }
150 },
151 {
152 .map = {
153 UCIMAP_OPTION(struct uci_network, ifname),
154 .type = UCIMAP_STRING,
155 .name = "ifname"
156 }
157 },
158 {
159 .map = {
160 UCIMAP_OPTION(struct uci_network, ipaddr),
161 .type = UCIMAP_CUSTOM,
162 .name = "ipaddr",
163 .parse = network_parse_ip,
164 .format = network_format_ip,
165 .free = network_free_ip,
166 }
167 },
168 {
169 .map = {
170 UCIMAP_OPTION(struct uci_network, enabled),
171 .type = UCIMAP_BOOL,
172 .name = "enabled",
173 }
174 },
175 {
176 .map = {
177 UCIMAP_OPTION(struct uci_network, test),
178 .type = UCIMAP_INT,
179 .name = "test"
180 }
181 },
182 {
183 .map = {
184 UCIMAP_OPTION(struct uci_network, aliases),
185 .type = UCIMAP_LIST | UCIMAP_SECTION | UCIMAP_LIST_AUTO,
186 .data.sm = &network_alias
187 }
188 }
189 };
190
191 static struct uci_sectionmap network_interface = {
192 UCIMAP_SECTION(struct uci_network, map),
193 .type = "interface",
194 .alloc = network_allocate,
195 .init = network_init_interface,
196 .add = network_add_interface,
197 .options = &network_interface_options[0].map,
198 .n_options = ARRAY_SIZE(network_interface_options),
199 .options_size = sizeof(struct my_optmap)
200 };
201
202 static struct uci_optmap network_alias_options[] = {
203 {
204 UCIMAP_OPTION(struct uci_alias, interface),
205 .type = UCIMAP_SECTION,
206 .data.sm = &network_interface
207 }
208 };
209
210 static struct uci_sectionmap network_alias = {
211 UCIMAP_SECTION(struct uci_alias, map),
212 .type = "alias",
213 .options = network_alias_options,
214 .init = network_init_alias,
215 .add = network_add_alias,
216 .n_options = ARRAY_SIZE(network_alias_options),
217 };
218
219 static struct uci_sectionmap *network_smap[] = {
220 &network_interface,
221 &network_alias,
222 };
223
224 static struct uci_map network_map = {
225 .sections = network_smap,
226 .n_sections = ARRAY_SIZE(network_smap),
227 };
228
229
230 int main(int argc, char **argv)
231 {
232 struct uci_context *ctx;
233 struct uci_package *pkg;
234 struct list_head *p;
235 struct uci_network *net;
236 struct uci_alias *alias;
237 int i;
238
239 INIT_LIST_HEAD(&ifs);
240 ctx = uci_alloc_context();
241 ucimap_init(&network_map);
242
243 uci_set_confdir(ctx, "./test/config");
244 uci_load(ctx, "network", &pkg);
245
246 ucimap_parse(&network_map, pkg);
247
248 list_for_each(p, &ifs) {
249 const unsigned char *ipaddr;
250
251 net = list_entry(p, struct uci_network, list);
252 ipaddr = net->ipaddr;
253 if (!ipaddr)
254 ipaddr = (const unsigned char *) "\x00\x00\x00\x00";
255
256 printf("New network section '%s'\n"
257 " type: %s\n"
258 " ifname: %s\n"
259 " ipaddr: %d.%d.%d.%d\n"
260 " test: %d\n"
261 " enabled: %s\n",
262 net->name,
263 net->proto,
264 net->ifname,
265 ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3],
266 net->test,
267 (net->enabled ? "on" : "off"));
268
269 for (i = 0; i < net->aliases->n_items; i++) {
270 alias = net->aliases->item[i].ptr;
271 printf("New alias: %s\n", alias->name);
272 }
273 #if 0
274 memcpy(net->ipaddr, "\x01\x03\x04\x05", 4);
275 ucimap_set_changed(&net->map, &net->ipaddr);
276 ucimap_store_section(&network_map, pkg, &net->map);
277 uci_save(ctx, pkg);
278 #endif
279 }
280
281
282 ucimap_cleanup(&network_map);
283 uci_free_context(ctx);
284
285 return 0;
286 }