21aa83e56320bcbe1fc72248a4ee00a1fd20a91e
[project/uci.git] / ucimap.h
1 /*
2 * ucimap - library for mapping uci sections into data structures
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 #ifndef __UCIMAP_H
15 #define __UCIMAP_H
16
17 #include <stdbool.h>
18 #include "uci_list.h"
19 #include "uci.h"
20
21 #ifndef ARRAY_SIZE
22 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
23 #endif
24
25 #define BITFIELD_SIZE(_fields) (((_fields) / 8) + 1)
26
27 #define CLR_BIT(_name, _bit) do { \
28 _name[(_bit) / 8] &= ~(1 << ((_bit) % 8)); \
29 } while (0)
30
31 #define SET_BIT(_name, _bit) do { \
32 _name[(_bit) / 8] |= (1 << ((_bit) % 8)); \
33 } while (0)
34
35 #define TEST_BIT(_name, _bit) \
36 (_name[(_bit) / 8] & (1 << ((_bit) % 8)))
37
38 #ifndef __GNUC__
39
40 #define __optmap_gen_type(_type, _field) -1
41
42 #ifndef likely
43 #define likely(_expr) !!(_expr)
44 #endif
45
46 #ifndef unlikely
47 #define unlikely(_expr) !!(_expr)
48 #endif
49
50 #else /* __GNUC__ */
51
52 #define __compatible(_type, _field, _newtype) \
53 __builtin_types_compatible_p(typeof(&(((_type *)0)->_field)), _newtype *)
54
55 #define __list_compatible(_type, _field, __val, __else) \
56 __builtin_choose_expr(__compatible(_type, _field, struct ucimap_list *), __val, __else)
57
58 #define __int_compatible(_type, _field, __val, __else) \
59 __builtin_choose_expr(__compatible(_type, _field, int), __val, \
60 __builtin_choose_expr(__compatible(_type, _field, unsigned int), __val, \
61 __else))
62
63 #define __string_compatible(_type, _field, __val, __else) \
64 __builtin_choose_expr(__compatible(_type, _field, char *), __val, \
65 __builtin_choose_expr(__compatible(_type, _field, unsigned char *), __val, \
66 __builtin_choose_expr(__compatible(_type, _field, const char *), __val, \
67 __builtin_choose_expr(__compatible(_type, _field, const unsigned char *), __val, \
68 __else))))
69
70 #define __bool_compatible(_type, _field, __val, __else) \
71 __builtin_choose_expr(__compatible(_type, _field, bool), __val, __else)
72
73
74 #define __optmap_gen_type(_type, _field) \
75 __list_compatible(_type, _field, UCIMAP_LIST, \
76 __int_compatible(_type, _field, UCIMAP_INT, \
77 __string_compatible(_type, _field, UCIMAP_STRING, \
78 __bool_compatible(_type, _field, UCIMAP_BOOL, \
79 -1))))
80
81 #ifndef likely
82 #define likely(x) __builtin_expect(!!(x), 1)
83 #endif
84
85 #ifndef unlikely
86 #define unlikely(x) __builtin_expect(!!(x), 0)
87 #endif
88
89 #endif /* __GNUC__ */
90
91 #define UCIMAP_OPTION(_type, _field) \
92 .type = UCIMAP_CUSTOM, \
93 .name = #_field, \
94 .offset = offsetof(_type, _field), \
95 .detected_type = __optmap_gen_type(_type, _field), \
96 .type_name = #_type
97
98
99 #define UCIMAP_SECTION(_name, _field) \
100 .alloc_len = sizeof(_name), \
101 .smap_offset = offsetof(_name, _field), \
102 .type_name = #_name
103
104 struct uci_sectionmap;
105 struct uci_optmap;
106 struct ucimap_list;
107 struct uci_alloc;
108 struct uci_alloc_custom;
109
110 struct uci_map {
111 struct uci_sectionmap **sections;
112 unsigned int n_sections;
113 struct list_head sdata;
114 struct list_head fixup;
115 struct list_head pending;
116 bool parsed;
117
118 void *priv; /* user data */
119 };
120
121 enum ucimap_type {
122 /* types */
123 UCIMAP_SIMPLE = 0x00,
124 UCIMAP_LIST = 0x10,
125 UCIMAP_TYPE = 0xf0, /* type mask */
126
127 /* subtypes */
128 UCIMAP_STRING = 0x0,
129 UCIMAP_BOOL = 0x1,
130 UCIMAP_INT = 0x2,
131 UCIMAP_SECTION = 0x3,
132 UCIMAP_CUSTOM = 0x4,
133 UCIMAP_SUBTYPE = 0xf, /* subtype mask */
134
135 /* automatically create lists from
136 * options with space-separated items */
137 UCIMAP_LIST_AUTO = 0x0100,
138 UCIMAP_FLAGS = 0xff00, /* flags mask */
139 };
140
141 union ucimap_data {
142 int i;
143 bool b;
144 char *s;
145 void *ptr;
146 void **data;
147 struct ucimap_list *list;
148 };
149
150 struct ucimap_section_data {
151 struct list_head list;
152 struct uci_map *map;
153 struct uci_sectionmap *sm;
154 const char *section_name;
155
156 /* list of allocations done by ucimap */
157 struct uci_alloc *allocmap;
158 struct uci_alloc_custom *alloc_custom;
159 unsigned int allocmap_len;
160 unsigned int alloc_custom_len;
161
162 /* map for changed fields */
163 unsigned char *cmap;
164 bool done;
165 };
166
167 struct uci_sectionmap {
168 /* type string for the uci section */
169 const char *type;
170
171 /* length of the struct to map into, filled in by macro */
172 unsigned int alloc_len;
173
174 /* sectionmap offset, filled in by macro */
175 unsigned int smap_offset;
176
177 /* return a pointer to the section map data (allocate if necessary) */
178 struct ucimap_section_data *(*alloc)(struct uci_map *map,
179 struct uci_sectionmap *sm, struct uci_section *s);
180
181 /* give the caller time to initialize the preallocated struct */
182 int (*init)(struct uci_map *map, void *section, struct uci_section *s);
183
184 /* pass the fully processed struct to the callback after the section end */
185 int (*add)(struct uci_map *map, void *section);
186
187 /* let the callback clean up its own stuff in the section */
188 int (*free)(struct uci_map *map, void *section);
189
190 /* list of option mappings for this section */
191 struct uci_optmap *options;
192 unsigned int n_options;
193 unsigned int options_size;
194
195 /* internal */
196 const char *type_name;
197 };
198
199 struct uci_optmap {
200 unsigned int offset;
201 const char *name;
202 enum ucimap_type type;
203 int (*parse)(void *section, struct uci_optmap *om, union ucimap_data *data, const char *string);
204 int (*format)(void *section, struct uci_optmap *om, union ucimap_data *data, char **string);
205 void (*free)(void *section, struct uci_optmap *om, void *ptr);
206 union {
207 struct {
208 int base;
209 int min;
210 int max;
211 } i;
212 struct {
213 int maxlen;
214 } s;
215 struct uci_sectionmap *sm;
216 } data;
217
218 /* internal */
219 int detected_type;
220 const char *type_name;
221 };
222
223 struct ucimap_list {
224 int n_items;
225 int size;
226 union ucimap_data item[];
227 };
228
229 extern int ucimap_init(struct uci_map *map);
230 extern void ucimap_cleanup(struct uci_map *map);
231 extern void ucimap_set_changed(struct ucimap_section_data *sd, void *field);
232 extern int ucimap_store_section(struct uci_map *map, struct uci_package *p, struct ucimap_section_data *sd);
233 extern void ucimap_parse(struct uci_map *map, struct uci_package *pkg);
234 extern int ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s);
235 extern void ucimap_free_section(struct uci_map *map, struct ucimap_section_data *sd);
236 extern int ucimap_resize_list(struct ucimap_section_data *sd, struct ucimap_list **list, int items);
237 extern void ucimap_free_item(struct ucimap_section_data *sd, void *item);
238
239 #endif