ucimap: add example for using the alloc callback
[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[4];
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 struct uci_network *net = section;
48 unsigned char *target = (unsigned char *) data->s;
49 unsigned 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 for (i = 0; i < 4; i++)
56 target[i] = (char) tmp[i];
57
58 return 0;
59 }
60
61 static int
62 network_init_interface(struct uci_map *map, void *section, struct uci_section *s)
63 {
64 struct uci_network *net = section;
65
66 INIT_LIST_HEAD(&net->list);
67 INIT_LIST_HEAD(&net->alias);
68 net->name = s->e.name;
69 net->test = -1;
70 return 0;
71 }
72
73 static int
74 network_init_alias(struct uci_map *map, void *section, struct uci_section *s)
75 {
76 struct uci_alias *alias = section;
77
78 INIT_LIST_HEAD(&alias->list);
79 alias->name = s->e.name;
80 return 0;
81 }
82
83 static int
84 network_add_interface(struct uci_map *map, void *section)
85 {
86 struct uci_network *net = section;
87
88 list_add(&net->list, &ifs);
89
90 return 0;
91 }
92
93 static int
94 network_add_alias(struct uci_map *map, void *section)
95 {
96 struct uci_alias *a = section;
97
98 if (a->interface)
99 list_add(&a->list, &a->interface->alias);
100
101 return 0;
102 }
103
104 static struct ucimap_section_data *
105 network_allocate(struct uci_map *map, struct uci_sectionmap *sm, struct uci_section *s)
106 {
107 struct uci_network *p = malloc(sizeof(struct uci_network));
108 memset(p, 0, sizeof(struct uci_network));
109 return &p->map;
110 }
111
112 struct my_optmap {
113 struct uci_optmap map;
114 int test;
115 };
116
117 static struct uci_sectionmap network_interface;
118 static struct uci_sectionmap network_alias;
119
120 static struct my_optmap network_interface_options[] = {
121 {
122 .map = {
123 UCIMAP_OPTION(struct uci_network, proto),
124 .type = UCIMAP_STRING,
125 .name = "proto",
126 .data.s.maxlen = 32,
127 }
128 },
129 {
130 .map = {
131 UCIMAP_OPTION(struct uci_network, ifname),
132 .type = UCIMAP_STRING,
133 .name = "ifname"
134 }
135 },
136 {
137 .map = {
138 UCIMAP_OPTION(struct uci_network, ipaddr),
139 .type = UCIMAP_CUSTOM,
140 .name = "ipaddr",
141 .parse = network_parse_ip,
142 }
143 },
144 {
145 .map = {
146 UCIMAP_OPTION(struct uci_network, enabled),
147 .type = UCIMAP_BOOL,
148 .name = "enabled",
149 }
150 },
151 {
152 .map = {
153 UCIMAP_OPTION(struct uci_network, test),
154 .type = UCIMAP_INT,
155 .name = "test"
156 }
157 },
158 {
159 .map = {
160 UCIMAP_OPTION(struct uci_network, aliases),
161 .type = UCIMAP_LIST | UCIMAP_SECTION,
162 .data.sm = &network_alias
163 }
164 }
165 };
166
167 static struct uci_sectionmap network_interface = {
168 UCIMAP_SECTION(struct uci_network, map),
169 .type = "interface",
170 .alloc = network_allocate,
171 .init = network_init_interface,
172 .add = network_add_interface,
173 .options = &network_interface_options[0].map,
174 .n_options = ARRAY_SIZE(network_interface_options),
175 .options_size = sizeof(struct my_optmap)
176 };
177
178 static struct uci_optmap network_alias_options[] = {
179 {
180 UCIMAP_OPTION(struct uci_alias, interface),
181 .type = UCIMAP_SECTION,
182 .data.sm = &network_interface
183 }
184 };
185
186 static struct uci_sectionmap network_alias = {
187 UCIMAP_SECTION(struct uci_alias, map),
188 .type = "alias",
189 .options = network_alias_options,
190 .init = network_init_alias,
191 .add = network_add_alias,
192 .n_options = ARRAY_SIZE(network_alias_options),
193 };
194
195 static struct uci_sectionmap *network_smap[] = {
196 &network_interface,
197 &network_alias,
198 };
199
200 static struct uci_map network_map = {
201 .sections = network_smap,
202 .n_sections = ARRAY_SIZE(network_smap),
203 };
204
205
206 int main(int argc, char **argv)
207 {
208 struct uci_context *ctx;
209 struct uci_package *pkg;
210 struct list_head *p, *p2;
211 struct uci_network *net;
212 struct uci_alias *alias;
213 int i;
214
215 INIT_LIST_HEAD(&ifs);
216 ctx = uci_alloc_context();
217 ucimap_init(&network_map);
218
219 uci_load(ctx, "network", &pkg);
220
221 ucimap_parse(&network_map, pkg);
222
223 list_for_each(p, &ifs) {
224 net = list_entry(p, struct uci_network, list);
225 printf("New network section '%s'\n"
226 " type: %s\n"
227 " ifname: %s\n"
228 " ipaddr: %d.%d.%d.%d\n"
229 " test: %d\n"
230 " enabled: %s\n",
231 net->name,
232 net->proto,
233 net->ifname,
234 net->ipaddr[0], net->ipaddr[1],
235 net->ipaddr[2], net->ipaddr[3],
236 net->test,
237 (net->enabled ? "on" : "off"));
238
239 for (i = 0; i < net->aliases->n_items; i++) {
240 alias = net->aliases->item[i].section;
241 printf("New alias: %s\n", alias->name);
242 }
243 #if 0
244 net->ipaddr = "2.3.4.5";
245 ucimap_set_changed(net, &net->ipaddr);
246 ucimap_store_section(&network_map, pkg, net);
247 uci_save(ctx, pkg);
248 #endif
249 }
250
251
252 done:
253 ucimap_cleanup(&network_map);
254 uci_free_context(ctx);
255
256 return 0;
257 }