d384ef66fc808ce6c232a46169c8068ddf684ae9
[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 #define UCIMAP_OPTION(_type, _field) \
39 .type = UCIMAP_CUSTOM, \
40 .name = #_field, \
41 .offset = offsetof(_type, _field)
42
43
44 #define UCIMAP_SECTION(_name, _field) \
45 .alloc_len = sizeof(_name), \
46 .smap_offset = offsetof(_name, _field)
47
48 struct uci_sectionmap;
49 struct uci_optmap;
50 struct ucimap_list;
51
52 struct uci_map {
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;
58 bool parsed;
59
60 void *priv; /* user data */
61 };
62
63 enum ucimap_type {
64 /* types */
65 UCIMAP_SIMPLE = 0x00,
66 UCIMAP_LIST = 0x10,
67 UCIMAP_TYPE = 0xf0, /* type mask */
68
69 /* subtypes */
70 UCIMAP_STRING = 0x0,
71 UCIMAP_BOOL = 0x1,
72 UCIMAP_INT = 0x2,
73 UCIMAP_SECTION = 0x3,
74 UCIMAP_CUSTOM = 0x4,
75 UCIMAP_SUBTYPE = 0xf, /* subtype mask */
76
77 /* automatically create lists from
78 * options with space-separated items */
79 UCIMAP_LIST_AUTO = 0x0100,
80 UCIMAP_FLAGS = 0xff00, /* flags mask */
81 };
82
83 union ucimap_data {
84 int i;
85 bool b;
86 char *s;
87 void *ptr;
88 struct ucimap_list *list;
89 };
90
91 struct ucimap_section_data {
92 struct list_head list;
93 struct uci_map *map;
94 struct uci_sectionmap *sm;
95 const char *section_name;
96
97 /* list of allocations done by ucimap */
98 struct uci_alloc *allocmap;
99 unsigned long allocmap_len;
100
101 /* map for changed fields */
102 unsigned char *cmap;
103 bool done;
104 };
105
106
107 struct uci_listmap {
108 struct list_head list;
109 union ucimap_data data;
110 };
111
112 struct uci_sectionmap {
113 /* type string for the uci section */
114 const char *type;
115
116 /* length of the struct to map into, filled in by macro */
117 unsigned int alloc_len;
118
119 /* sectionmap offset, filled in by macro */
120 unsigned int smap_offset;
121
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);
125
126 /* give the caller time to initialize the preallocated struct */
127 int (*init)(struct uci_map *map, void *section, struct uci_section *s);
128
129 /* pass the fully processed struct to the callback after the section end */
130 int (*add)(struct uci_map *map, void *section);
131
132 /* let the callback clean up its own stuff in the section */
133 int (*free)(struct uci_map *map, void *section);
134
135 /* list of option mappings for this section */
136 struct uci_optmap *options;
137 unsigned int n_options;
138 unsigned int options_size;
139 };
140
141 struct uci_optmap {
142 unsigned int offset;
143 const char *name;
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);
147 union {
148 struct {
149 int base;
150 int min;
151 int max;
152 } i;
153 struct {
154 int maxlen;
155 } s;
156 struct uci_sectionmap *sm;
157 } data;
158 };
159
160 struct ucimap_list {
161 int n_items;
162 union ucimap_data item[];
163 };
164
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);
171
172 #endif