add type safety checks for option maps
[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 #define __optmap_gen_type(_type, _field) -1
40 #else
41
42 #define __compatible(_type, _field, _newtype) \
43 __builtin_types_compatible_p(typeof(&(((_type *)0)->_field)), _newtype *)
44
45 #define __list_compatible(_type, _field, __val, __else) \
46 __builtin_choose_expr(__compatible(_type, _field, struct ucimap_list *), __val, __else)
47
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, \
51 __else))
52
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, \
58 __else))))
59
60 #define __bool_compatible(_type, _field, __val, __else) \
61 __builtin_choose_expr(__compatible(_type, _field, bool), __val, __else)
62
63
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, \
69 -1))))
70
71 #endif
72
73 #define UCIMAP_OPTION(_type, _field) \
74 .type = UCIMAP_CUSTOM, \
75 .name = #_field, \
76 .offset = offsetof(_type, _field), \
77 .detected_type = __optmap_gen_type(_type, _field)
78
79
80 #define UCIMAP_SECTION(_name, _field) \
81 .alloc_len = sizeof(_name), \
82 .smap_offset = offsetof(_name, _field)
83
84 struct uci_sectionmap;
85 struct uci_optmap;
86 struct ucimap_list;
87
88 struct uci_map {
89 struct uci_sectionmap **sections;
90 unsigned int n_sections;
91 struct list_head sdata;
92 struct list_head fixup;
93 struct list_head pending;
94 bool parsed;
95
96 void *priv; /* user data */
97 };
98
99 enum ucimap_type {
100 /* types */
101 UCIMAP_SIMPLE = 0x00,
102 UCIMAP_LIST = 0x10,
103 UCIMAP_TYPE = 0xf0, /* type mask */
104
105 /* subtypes */
106 UCIMAP_STRING = 0x0,
107 UCIMAP_BOOL = 0x1,
108 UCIMAP_INT = 0x2,
109 UCIMAP_SECTION = 0x3,
110 UCIMAP_CUSTOM = 0x4,
111 UCIMAP_SUBTYPE = 0xf, /* subtype mask */
112
113 /* automatically create lists from
114 * options with space-separated items */
115 UCIMAP_LIST_AUTO = 0x0100,
116 UCIMAP_FLAGS = 0xff00, /* flags mask */
117 };
118
119 union ucimap_data {
120 int i;
121 bool b;
122 char *s;
123 void *ptr;
124 struct ucimap_list *list;
125 };
126
127 struct ucimap_section_data {
128 struct list_head list;
129 struct uci_map *map;
130 struct uci_sectionmap *sm;
131 const char *section_name;
132
133 /* list of allocations done by ucimap */
134 struct uci_alloc *allocmap;
135 unsigned long allocmap_len;
136
137 /* map for changed fields */
138 unsigned char *cmap;
139 bool done;
140 };
141
142
143 struct uci_listmap {
144 struct list_head list;
145 union ucimap_data data;
146 };
147
148 struct uci_sectionmap {
149 /* type string for the uci section */
150 const char *type;
151
152 /* length of the struct to map into, filled in by macro */
153 unsigned int alloc_len;
154
155 /* sectionmap offset, filled in by macro */
156 unsigned int smap_offset;
157
158 /* return a pointer to the section map data (allocate if necessary) */
159 struct ucimap_section_data *(*alloc)(struct uci_map *map,
160 struct uci_sectionmap *sm, struct uci_section *s);
161
162 /* give the caller time to initialize the preallocated struct */
163 int (*init)(struct uci_map *map, void *section, struct uci_section *s);
164
165 /* pass the fully processed struct to the callback after the section end */
166 int (*add)(struct uci_map *map, void *section);
167
168 /* let the callback clean up its own stuff in the section */
169 int (*free)(struct uci_map *map, void *section);
170
171 /* list of option mappings for this section */
172 struct uci_optmap *options;
173 unsigned int n_options;
174 unsigned int options_size;
175 };
176
177 struct uci_optmap {
178 unsigned int offset;
179 const char *name;
180 enum ucimap_type type;
181 int detected_type;
182 int (*parse)(void *section, struct uci_optmap *om, union ucimap_data *data, const char *string);
183 int (*format)(void *section, struct uci_optmap *om, union ucimap_data *data, char **string);
184 union {
185 struct {
186 int base;
187 int min;
188 int max;
189 } i;
190 struct {
191 int maxlen;
192 } s;
193 struct uci_sectionmap *sm;
194 } data;
195 };
196
197 struct ucimap_list {
198 int n_items;
199 union ucimap_data item[];
200 };
201
202 extern int ucimap_init(struct uci_map *map);
203 extern void ucimap_cleanup(struct uci_map *map);
204 extern void ucimap_set_changed(struct ucimap_section_data *sd, void *field);
205 extern int ucimap_store_section(struct uci_map *map, struct uci_package *p, struct ucimap_section_data *sd);
206 extern void ucimap_parse(struct uci_map *map, struct uci_package *pkg);
207 extern int ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s);
208
209 #endif