2 * ucimap-example - sample code for the ucimap library
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
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
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.
21 struct list_head list;
32 network_init_section(struct uci_map *map, void *section, struct uci_section *s)
34 struct uci_network *net = section;
36 INIT_LIST_HEAD(&net->list);
37 net->name = s->e.name;
43 network_add_section(struct uci_map *map, void *section)
45 struct uci_network *net = section;
46 struct uci_network **nptr = map->priv;
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),
60 struct uci_sectmap network_smap[] = {
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),
71 struct uci_map network_map = {
72 .sections = network_smap,
73 .n_sections = ARRAY_SIZE(network_smap),
77 int main(int argc, char **argv)
79 struct uci_context *ctx;
80 struct uci_package *pkg;
81 struct uci_network *net = NULL;
83 ctx = uci_alloc_context();
84 ucimap_init(&network_map);
86 uci_load(ctx, "network", &pkg);
88 network_map.priv = &net;
89 ucimap_parse(&network_map, pkg);
94 printf("New network section '%s'\n"
105 (net->enabled ? "on" : "off"));
107 net->ipaddr = "2.3.4.5";
108 ucimap_set_changed(net, &net->ipaddr);
109 ucimap_store_section(&network_map, pkg, net);
113 ucimap_cleanup(&network_map);
114 uci_free_context(ctx);