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)))
39 #define __optmap_gen_type(_type, _field) -1
42 #define __compatible(_type, _field, _newtype) \
43 __builtin_types_compatible_p(typeof(&(((_type *)0)->_field)), _newtype *)
45 #define __list_compatible(_type, _field, __val, __else) \
46 __builtin_choose_expr(__compatible(_type, _field, struct ucimap_list *), __val, __else)
48 #define __int_compatible(_type, _field, __val, __else) \
49 __builtin_choose_expr(__compatible(_type, _field, int), __val, \
50 __builtin_choose_expr(__compatible(_type, _field, unsigned int), __val, \
53 #define __string_compatible(_type, _field, __val, __else) \
54 __builtin_choose_expr(__compatible(_type, _field, char *), __val, \
55 __builtin_choose_expr(__compatible(_type, _field, unsigned char *), __val, \
56 __builtin_choose_expr(__compatible(_type, _field, const char *), __val, \
57 __builtin_choose_expr(__compatible(_type, _field, const unsigned char *), __val, \
60 #define __bool_compatible(_type, _field, __val, __else) \
61 __builtin_choose_expr(__compatible(_type, _field, bool), __val, __else)
64 #define __optmap_gen_type(_type, _field) \
65 __list_compatible(_type, _field, UCIMAP_LIST, \
66 __int_compatible(_type, _field, UCIMAP_INT, \
67 __string_compatible(_type, _field, UCIMAP_STRING, \
68 __bool_compatible(_type, _field, UCIMAP_BOOL, \
73 #define UCIMAP_OPTION(_type, _field) \
74 .type = UCIMAP_CUSTOM, \
76 .offset = offsetof(_type, _field), \
77 .detected_type = __optmap_gen_type(_type, _field)
80 #define UCIMAP_SECTION(_name, _field) \
81 .alloc_len = sizeof(_name), \
82 .smap_offset = offsetof(_name, _field)
84 struct uci_sectionmap
;
88 struct uci_alloc_custom
;
91 struct uci_sectionmap
**sections
;
92 unsigned int n_sections
;
93 struct list_head sdata
;
94 struct list_head fixup
;
95 struct list_head pending
;
98 void *priv
; /* user data */
103 UCIMAP_SIMPLE
= 0x00,
105 UCIMAP_TYPE
= 0xf0, /* type mask */
111 UCIMAP_SECTION
= 0x3,
113 UCIMAP_SUBTYPE
= 0xf, /* subtype mask */
115 /* automatically create lists from
116 * options with space-separated items */
117 UCIMAP_LIST_AUTO
= 0x0100,
118 UCIMAP_FLAGS
= 0xff00, /* flags mask */
127 struct ucimap_list
*list
;
130 struct ucimap_section_data
{
131 struct list_head list
;
133 struct uci_sectionmap
*sm
;
134 const char *section_name
;
136 /* list of allocations done by ucimap */
137 struct uci_alloc
*allocmap
;
138 struct uci_alloc_custom
*alloc_custom
;
139 unsigned int allocmap_len
;
140 unsigned int alloc_custom_len
;
142 /* map for changed fields */
149 struct list_head list
;
150 union ucimap_data data
;
153 struct uci_sectionmap
{
154 /* type string for the uci section */
157 /* length of the struct to map into, filled in by macro */
158 unsigned int alloc_len
;
160 /* sectionmap offset, filled in by macro */
161 unsigned int smap_offset
;
163 /* return a pointer to the section map data (allocate if necessary) */
164 struct ucimap_section_data
*(*alloc
)(struct uci_map
*map
,
165 struct uci_sectionmap
*sm
, struct uci_section
*s
);
167 /* give the caller time to initialize the preallocated struct */
168 int (*init
)(struct uci_map
*map
, void *section
, struct uci_section
*s
);
170 /* pass the fully processed struct to the callback after the section end */
171 int (*add
)(struct uci_map
*map
, void *section
);
173 /* let the callback clean up its own stuff in the section */
174 int (*free
)(struct uci_map
*map
, void *section
);
176 /* list of option mappings for this section */
177 struct uci_optmap
*options
;
178 unsigned int n_options
;
179 unsigned int options_size
;
185 enum ucimap_type type
;
187 int (*parse
)(void *section
, struct uci_optmap
*om
, union ucimap_data
*data
, const char *string
);
188 int (*format
)(void *section
, struct uci_optmap
*om
, union ucimap_data
*data
, char **string
);
189 void (*free
)(void *section
, struct uci_optmap
*om
, void *ptr
);
199 struct uci_sectionmap
*sm
;
205 union ucimap_data item
[];
208 extern int ucimap_init(struct uci_map
*map
);
209 extern void ucimap_cleanup(struct uci_map
*map
);
210 extern void ucimap_set_changed(struct ucimap_section_data
*sd
, void *field
);
211 extern int ucimap_store_section(struct uci_map
*map
, struct uci_package
*p
, struct ucimap_section_data
*sd
);
212 extern void ucimap_parse(struct uci_map
*map
, struct uci_package
*pkg
);
213 extern int ucimap_parse_section(struct uci_map
*map
, struct uci_sectionmap
*sm
, struct ucimap_section_data
*sd
, struct uci_section
*s
);
214 extern void ucimap_free_section(struct uci_map
*map
, struct ucimap_section_data
*sd
);