shorten callback names
[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 list_head list;
24         struct list_head alias;
25
26         const char *name;
27         const char *proto;
28         const char *ifname;
29         const char *ipaddr;
30         int test;
31         bool enabled;
32         struct ucimap_list *aliases;
33 };
34
35 struct uci_alias {
36         struct list_head list;
37
38         const char *name;
39         struct uci_network *interface;
40 };
41
42 static int
43 network_init_interface(struct uci_map *map, void *section, struct uci_section *s)
44 {
45         struct uci_network *net = section;
46
47         INIT_LIST_HEAD(&net->list);
48         INIT_LIST_HEAD(&net->alias);
49         net->name = s->e.name;
50         net->test = -1;
51         return 0;
52 }
53
54 static int
55 network_init_alias(struct uci_map *map, void *section, struct uci_section *s)
56 {
57         struct uci_alias *alias = section;
58
59         INIT_LIST_HEAD(&alias->list);
60         alias->name = s->e.name;
61         return 0;
62 }
63
64 static int
65 network_add_interface(struct uci_map *map, void *section)
66 {
67         struct uci_network *net = section;
68
69         list_add(&net->list, &ifs);
70
71         return 0;
72 }
73
74 static int
75 network_add_alias(struct uci_map *map, void *section)
76 {
77         struct uci_alias *a = section;
78
79         if (a->interface)
80                 list_add(&a->list, &a->interface->alias);
81
82         return 0;
83 }
84
85 struct my_optmap {
86         struct uci_optmap map;
87         int test;
88 };
89
90 static struct uci_sectmap network_interface;
91 static struct uci_sectmap network_alias;
92
93 static struct my_optmap network_interface_options[] = {
94         {
95                 .map = {
96                         UCIMAP_OPTION(struct uci_network, proto),
97                         .type = UCIMAP_STRING,
98                         .name = "proto",
99                         .data.s.maxlen = 32,
100                 }
101         },
102         {
103                 .map = {
104                         UCIMAP_OPTION(struct uci_network, ifname),
105                         .type = UCIMAP_STRING,
106                         .name = "ifname"
107                 }
108         },
109         {
110                 .map = {
111                         UCIMAP_OPTION(struct uci_network, ipaddr),
112                         .type = UCIMAP_STRING,
113                         .name = "ipaddr",
114                 }
115         },
116         {
117                 .map = {
118                         UCIMAP_OPTION(struct uci_network, enabled),
119                         .type = UCIMAP_BOOL,
120                         .name = "enabled",
121                 }
122         },
123         {
124                 .map = {
125                         UCIMAP_OPTION(struct uci_network, test),
126                         .type = UCIMAP_INT,
127                         .name = "test"
128                 }
129         },
130         {
131                 .map = {
132                         UCIMAP_OPTION(struct uci_network, aliases),
133                         .type = UCIMAP_LIST | UCIMAP_SECTION,
134                         .data.sm = &network_alias
135                 }
136         }
137 };
138
139 static struct uci_sectmap network_interface = {
140         .type = "interface",
141         .alloc_len = sizeof(struct uci_network),
142         .init = network_init_interface,
143         .add = network_add_interface,
144         .options = &network_interface_options[0].map,
145         .n_options = ARRAY_SIZE(network_interface_options),
146         .options_size = sizeof(struct my_optmap)
147 };
148
149 static struct uci_optmap network_alias_options[] = {
150         {
151                 UCIMAP_OPTION(struct uci_alias, interface),
152                 .type = UCIMAP_SECTION,
153                 .data.sm = &network_interface
154         }
155 };
156
157 static struct uci_sectmap network_alias = {
158         .type = "alias",
159         .options = network_alias_options,
160         .alloc_len = sizeof(struct uci_network),
161         .init = network_init_alias,
162         .add = network_add_alias,
163         .n_options = ARRAY_SIZE(network_alias_options),
164 };
165
166 static struct uci_sectmap *network_smap[] = {
167         &network_interface,
168         &network_alias,
169 };
170
171 static struct uci_map network_map = {
172         .sections = network_smap,
173         .n_sections = ARRAY_SIZE(network_smap),
174 };
175
176
177 int main(int argc, char **argv)
178 {
179         struct uci_context *ctx;
180         struct uci_package *pkg;
181         struct list_head *p, *p2;
182         struct uci_network *net;
183         struct uci_alias *alias;
184         int i;
185
186         INIT_LIST_HEAD(&ifs);
187         ctx = uci_alloc_context();
188         ucimap_init(&network_map);
189
190         uci_load(ctx, "network", &pkg);
191
192         ucimap_parse(&network_map, pkg);
193
194         list_for_each(p, &ifs) {
195                 net = list_entry(p, struct uci_network, list);
196                 printf("New network section '%s'\n"
197                         "       type: %s\n"
198                         "       ifname: %s\n"
199                         "       ipaddr: %s\n"
200                         "       test: %d\n"
201                         "       enabled: %s\n",
202                         net->name,
203                         net->proto,
204                         net->ifname,
205                         net->ipaddr,
206                         net->test,
207                         (net->enabled ? "on" : "off"));
208
209                 for (i = 0; i < net->aliases->n_items; i++) {
210                         alias = net->aliases->item[i].section;
211                         printf("New alias: %s\n", alias->name);
212                 }
213 #if 0
214                 net->ipaddr = "2.3.4.5";
215                 ucimap_set_changed(net, &net->ipaddr);
216                 ucimap_store_section(&network_map, pkg, net);
217                 uci_save(ctx, pkg);
218 #endif
219         }
220
221
222 done:
223         ucimap_cleanup(&network_map);
224         uci_free_context(ctx);
225
226         return 0;
227 }