2 * ucimap - library for mapping uci sections into data structures
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.
22 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
25 #define BITFIELD_SIZE(_fields) (((_fields) / 8) + 1)
27 #define CLR_BIT(_name, _bit) do { \
28 _name[(_bit) / 8] &= ~(1 << ((_bit) % 8)); \
31 #define SET_BIT(_name, _bit) do { \
32 _name[(_bit) / 8] |= (1 << ((_bit) % 8)); \
35 #define TEST_BIT(_name, _bit) \
36 (_name[(_bit) / 8] & (1 << ((_bit) % 8)))
38 #define UCIMAP_OPTION(_type, _field) \
39 .type = UCIMAP_CUSTOM, \
41 .offset = offsetof(_type, _field)
44 #define UCIMAP_SECTION(_name, _field) \
45 .alloc_len = sizeof(_name), \
46 .smap_offset = offsetof(_name, _field)
48 struct uci_sectionmap;
53 struct uci_sectionmap **sections;
54 unsigned int n_sections;
55 struct list_head sdata;
56 struct list_head fixup;
57 struct list_head pending;
60 void *priv; /* user data */
67 UCIMAP_TYPE = 0xf0, /* type mask */
75 UCIMAP_SUBTYPE = 0xf, /* subtype mask */
77 /* automatically create lists from
78 * options with space-separated items */
79 UCIMAP_LIST_AUTO = 0x0100,
80 UCIMAP_FLAGS = 0xff00, /* flags mask */
88 struct ucimap_list *list;
91 struct ucimap_section_data {
92 struct list_head list;
94 struct uci_sectionmap *sm;
95 const char *section_name;
97 /* list of allocations done by ucimap */
98 struct uci_alloc *allocmap;
99 unsigned long allocmap_len;
101 /* map for changed fields */
108 struct list_head list;
109 union ucimap_data data;
112 struct uci_sectionmap {
113 /* type string for the uci section */
116 /* length of the struct to map into, filled in by macro */
117 unsigned int alloc_len;
119 /* sectionmap offset, filled in by macro */
120 unsigned int smap_offset;
122 /* return a pointer to the section map data (allocate if necessary) */
123 struct ucimap_section_data *(*alloc)(struct uci_map *map,
124 struct uci_sectionmap *sm, struct uci_section *s);
126 /* give the caller time to initialize the preallocated struct */
127 int (*init)(struct uci_map *map, void *section, struct uci_section *s);
129 /* pass the fully processed struct to the callback after the section end */
130 int (*add)(struct uci_map *map, void *section);
132 /* let the callback clean up its own stuff in the section */
133 int (*free)(struct uci_map *map, void *section);
135 /* list of option mappings for this section */
136 struct uci_optmap *options;
137 unsigned int n_options;
138 unsigned int options_size;
144 enum ucimap_type type;
145 int (*parse)(void *section, struct uci_optmap *om, union ucimap_data *data, const char *string);
146 int (*format)(void *section, struct uci_optmap *om, union ucimap_data *data, char **string);
156 struct uci_sectionmap *sm;
162 union ucimap_data item[];
165 extern int ucimap_init(struct uci_map *map);
166 extern void ucimap_cleanup(struct uci_map *map);
167 extern void ucimap_set_changed(struct ucimap_section_data *sd, void *field);
168 extern int ucimap_store_section(struct uci_map *map, struct uci_package *p, struct ucimap_section_data *sd);
169 extern void ucimap_parse(struct uci_map *map, struct uci_package *pkg);
170 extern int ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s);