2 * ucimap.h - Library for the Unified Configuration Interface
3 * Copyright (C) 2008-2009 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 Lesser General Public License version 2.1
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.
16 * This file contains ucimap, an API for mapping UCI to C data structures
26 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
29 #define BITFIELD_SIZE(_fields) (((_fields) / 8) + 1)
31 #define CLR_BIT(_name, _bit) do { \
32 _name[(_bit) / 8] &= ~(1 << ((_bit) % 8)); \
35 #define SET_BIT(_name, _bit) do { \
36 _name[(_bit) / 8] |= (1 << ((_bit) % 8)); \
39 #define TEST_BIT(_name, _bit) \
40 (_name[(_bit) / 8] & (1 << ((_bit) % 8)))
44 #define __optmap_gen_type(_type, _field) -1
47 #define likely(_expr) !!(_expr)
51 #define unlikely(_expr) !!(_expr)
56 #define __compatible(_type, _field, _newtype) \
57 __builtin_types_compatible_p(typeof(&(((_type *)0)->_field)), _newtype *)
59 #define __list_compatible(_type, _field, __val, __else) \
60 __builtin_choose_expr(__compatible(_type, _field, struct ucimap_list *), __val, __else)
62 #define __int_compatible(_type, _field, __val, __else) \
63 __builtin_choose_expr(__compatible(_type, _field, int), __val, \
64 __builtin_choose_expr(__compatible(_type, _field, unsigned int), __val, \
67 #define __string_compatible(_type, _field, __val, __else) \
68 __builtin_choose_expr(__compatible(_type, _field, char *), __val, \
69 __builtin_choose_expr(__compatible(_type, _field, unsigned char *), __val, \
70 __builtin_choose_expr(__compatible(_type, _field, const char *), __val, \
71 __builtin_choose_expr(__compatible(_type, _field, const unsigned char *), __val, \
74 #define __bool_compatible(_type, _field, __val, __else) \
75 __builtin_choose_expr(__compatible(_type, _field, bool), __val, __else)
78 #define __optmap_gen_type(_type, _field) \
79 __list_compatible(_type, _field, UCIMAP_LIST, \
80 __int_compatible(_type, _field, UCIMAP_INT, \
81 __string_compatible(_type, _field, UCIMAP_STRING, \
82 __bool_compatible(_type, _field, UCIMAP_BOOL, \
86 #define likely(x) __builtin_expect(!!(x), 1)
90 #define unlikely(x) __builtin_expect(!!(x), 0)
95 #define UCIMAP_OPTION(_type, _field) \
96 .type = UCIMAP_CUSTOM, \
98 .offset = offsetof(_type, _field), \
99 .detected_type = __optmap_gen_type(_type, _field), \
103 #define UCIMAP_SECTION(_name, _field) \
104 .alloc_len = sizeof(_name), \
105 .smap_offset = offsetof(_name, _field), \
108 struct uci_sectionmap;
114 struct ucimap_alloc_custom;
115 struct ucimap_section_data;
118 struct uci_sectionmap **sections;
119 unsigned int n_sections;
124 struct ucimap_fixup *fixup;
125 struct ucimap_fixup **fixup_tail;
126 struct ucimap_section_data *sdata;
127 struct ucimap_section_data *pending;
128 struct ucimap_section_data **sdata_tail;
133 UCIMAP_SIMPLE = 0x00,
135 UCIMAP_TYPE = 0xf0, /* type mask */
141 UCIMAP_SECTION = 0x3,
143 UCIMAP_SUBTYPE = 0xf, /* subtype mask */
145 /* automatically create lists from
146 * options with space-separated items */
147 UCIMAP_LIST_AUTO = 0x0100,
148 UCIMAP_FLAGS = 0xff00, /* flags mask */
157 struct ucimap_list *list;
160 struct ucimap_section_data {
162 struct uci_sectionmap *sm;
163 const char *section_name;
165 /* map for changed fields */
170 struct ucimap_section_data *next, **ref;
171 struct ucimap_alloc *allocmap;
172 struct ucimap_alloc_custom *alloc_custom;
173 unsigned int allocmap_len;
174 unsigned int alloc_custom_len;
177 struct uci_sectionmap {
178 /* type string for the uci section */
181 /* length of the struct to map into, filled in by macro */
182 unsigned int alloc_len;
184 /* sectionmap offset, filled in by macro */
185 unsigned int smap_offset;
187 /* return a pointer to the section map data (allocate if necessary) */
188 struct ucimap_section_data *(*alloc)(struct uci_map *map,
189 struct uci_sectionmap *sm, struct uci_section *s);
191 /* give the caller time to initialize the preallocated struct */
192 int (*init)(struct uci_map *map, void *section, struct uci_section *s);
194 /* pass the fully processed struct to the callback after the section end */
195 int (*add)(struct uci_map *map, void *section);
197 /* let the callback clean up its own stuff in the section */
198 int (*free)(struct uci_map *map, void *section);
200 /* list of option mappings for this section */
201 struct uci_optmap *options;
202 unsigned int n_options;
203 unsigned int options_size;
206 const char *type_name;
212 enum ucimap_type type;
213 int (*parse)(void *section, struct uci_optmap *om, union ucimap_data *data, const char *string);
214 int (*format)(void *section, struct uci_optmap *om, union ucimap_data *data, char **string);
215 void (*free)(void *section, struct uci_optmap *om, void *ptr);
225 struct uci_sectionmap *sm;
230 const char *type_name;
236 union ucimap_data item[];
240 * ucimap_init: initialize the ucimap data structure
241 * @map: ucimap data structure
243 * you must call this function before doing any other ucimap operation
244 * on the data structure
246 extern int ucimap_init(struct uci_map *map);
249 * ucimap_cleanup: clean up all allocated data from ucimap
250 * @map: ucimap data structure
252 extern void ucimap_cleanup(struct uci_map *map);
255 * ucimap_parse: parse all sections in an uci package using ucimap
256 * @map: ucimap data structure
259 extern void ucimap_parse(struct uci_map *map, struct uci_package *pkg);
262 * ucimap_set_changed: mark a field in a custom data structure as changed
263 * @sd: pointer to the ucimap section data
264 * @field: pointer to the field inside the custom data structure
266 * @sd must be set to the section data inside the data structure that contains @field
268 extern void ucimap_set_changed(struct ucimap_section_data *sd, void *field);
271 * ucimap_store_section: copy all changed data from the converted data structure to uci
272 * @map: ucimap data structure
273 * @p: uci package to store the changes in
274 * @sd: pointer to the ucimap section data
276 * changes are not saved or committed automatically
278 extern int ucimap_store_section(struct uci_map *map, struct uci_package *p, struct ucimap_section_data *sd);
281 * ucimap_parse_section: parse a single section
282 * @map: ucimap data structure
283 * @sm: uci section map
284 * @sd: pointer to the ucimap section data
285 * @s: pointer to the uci section
287 * this function overwrites the ucimap section data, do not use on a section
288 * that has been parsed already
290 extern int ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s);
293 * ucimap_free_section: free a data structure for a converted section
294 * @map: ucimap data structure
295 * @sd: pointer to the ucimap section data
297 * this function will clean up all data that was allocated by ucimap for this section.
298 * all references to the data structure become invalid
300 extern void ucimap_free_section(struct uci_map *map, struct ucimap_section_data *sd);
303 * ucimap_resize_list: allocate or resize a uci list
304 * @sd: pointer to the ucimap section data
305 * @list: pointer to the list field
308 * @sd must point to the data structure that contains @list.
309 * @list must point to the field containing a pointer to the list, not the list directly
310 * the memory allocated for this list is tracked for the section and freed automatically
312 extern int ucimap_resize_list(struct ucimap_section_data *sd, struct ucimap_list **list, int items);
315 * ucimap_free_item: free the allocated memory for a data structure member
316 * @sd: pointer to the ucimap section data
317 * @item: pointer to the field inside the data structure
319 * @sd must point to the data structure that contains @item.
320 * @item must point to the field containing a pointer to the allocated item
322 extern void ucimap_free_item(struct ucimap_section_data *sd, void *item);