4309a76d7680503af92a76f3184eed975eb305b5
[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 uci_network {
21 struct list_head list;
22
23 const char *name;
24 const char *proto;
25 const char *ifname;
26 const char *ipaddr;
27 int test;
28 bool enabled;
29 };
30
31 static int
32 network_init_section(struct uci_map *map, void *section, struct uci_section *s)
33 {
34 struct uci_network *net = section;
35
36 INIT_LIST_HEAD(&net->list);
37 net->name = s->e.name;
38 net->test = -1;
39 return 0;
40 }
41
42 static int
43 network_add_section(struct uci_map *map, void *section)
44 {
45 struct uci_network *net = section;
46 struct uci_network **nptr = map->priv;
47
48 *nptr = net;
49 return 0;
50 }
51
52 struct uci_optmap network_smap_options[] = {
53 OPTMAP_OPTION(UCIMAP_STRING, struct uci_network, proto, .data.s.maxlen = 32),
54 OPTMAP_OPTION(UCIMAP_STRING, struct uci_network, ifname),
55 OPTMAP_OPTION(UCIMAP_STRING, struct uci_network, ipaddr),
56 OPTMAP_OPTION(UCIMAP_BOOL, struct uci_network, enabled),
57 OPTMAP_OPTION(UCIMAP_INT, struct uci_network, test),
58 };
59
60 struct uci_sectmap network_smap[] = {
61 {
62 .type = "interface",
63 .options = network_smap_options,
64 .alloc_len = sizeof(struct uci_network),
65 .init_section = network_init_section,
66 .add_section = network_add_section,
67 .n_options = ARRAY_SIZE(network_smap_options),
68 }
69 };
70
71 struct uci_map network_map = {
72 .sections = network_smap,
73 .n_sections = ARRAY_SIZE(network_smap),
74 };
75
76
77 int main(int argc, char **argv)
78 {
79 struct uci_context *ctx;
80 struct uci_package *pkg;
81 struct uci_network *net = NULL;
82
83 ctx = uci_alloc_context();
84 ucimap_init(&network_map);
85
86 uci_load(ctx, "network", &pkg);
87
88 network_map.priv = &net;
89 ucimap_parse(&network_map, pkg);
90
91 if (!net)
92 goto done;
93
94 printf("New network section '%s'\n"
95 " type: %s\n"
96 " ifname: %s\n"
97 " ipaddr: %s\n"
98 " test: %d\n"
99 " enabled: %s\n",
100 net->name,
101 net->proto,
102 net->ifname,
103 net->ipaddr,
104 net->test,
105 (net->enabled ? "on" : "off"));
106
107 net->ipaddr = "2.3.4.5";
108 ucimap_set_changed(net, &net->ipaddr);
109 ucimap_store_section(&network_map, pkg, net);
110 uci_save(ctx, pkg);
111
112 done:
113 ucimap_cleanup(&network_map);
114 uci_free_context(ctx);
115
116 return 0;
117 }