ucimap: add new UCIMAP_LIST_AUTO for automatically converting multiple list items...
[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 #include <stdbool.h>
15 #include "uci_list.h"
16 #include "uci.h"
17
18 #ifndef ARRAY_SIZE
19 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
20 #endif
21
22 #define BITFIELD_SIZE(_fields) (((_fields) / 8) + 1)
23
24 #define CLR_BIT(_name, _bit) do { \
25 _name[(_bit) / 8] &= ~(1 << ((_bit) % 8)); \
26 } while (0)
27
28 #define SET_BIT(_name, _bit) do { \
29 _name[(_bit) / 8] |= (1 << ((_bit) % 8)); \
30 } while (0)
31
32 #define TEST_BIT(_name, _bit) \
33 (_name[(_bit) / 8] & (1 << ((_bit) % 8)))
34
35 #define UCIMAP_OPTION(_type, _field) \
36 .type = UCIMAP_CUSTOM, \
37 .name = #_field, \
38 .offset = offsetof(_type, _field)
39
40
41 #define UCIMAP_SECTION(_name, _field) \
42 .alloc_len = sizeof(_name), \
43 .smap_offset = offsetof(_name, _field)
44
45 struct uci_sectionmap;
46 struct uci_optmap;
47 struct ucimap_list;
48
49 struct uci_map {
50 struct uci_sectionmap **sections;
51 unsigned int n_sections;
52 struct list_head sdata;
53 struct list_head fixup;
54
55 void *priv; /* user data */
56 };
57
58 enum ucimap_type {
59 /* types */
60 UCIMAP_SIMPLE = 0x00,
61 UCIMAP_LIST = 0x10,
62 UCIMAP_TYPE = 0xf0, /* type mask */
63
64 /* subtypes */
65 UCIMAP_STRING = 0x0,
66 UCIMAP_BOOL = 0x1,
67 UCIMAP_INT = 0x2,
68 UCIMAP_SECTION = 0x3,
69 UCIMAP_CUSTOM = 0x4,
70 UCIMAP_SUBTYPE = 0xf, /* subtype mask */
71
72 /* automatically create lists from
73 * options with space-separated items */
74 UCIMAP_LIST_AUTO = 0x0100,
75 UCIMAP_FLAGS = 0xff00, /* flags mask */
76 };
77
78 union ucimap_data {
79 int i;
80 bool b;
81 char *s;
82 void *section;
83 struct ucimap_list *list;
84 };
85
86 struct ucimap_section_data {
87 struct list_head list;
88 struct uci_map *map;
89 struct uci_sectionmap *sm;
90 const char *section_name;
91
92 /* list of allocations done by ucimap */
93 struct uci_alloc *allocmap;
94 unsigned long allocmap_len;
95
96 /* map for changed fields */
97 unsigned char *cmap;
98 bool done;
99 };
100
101
102 struct uci_listmap {
103 struct list_head list;
104 union ucimap_data data;
105 };
106
107 struct uci_sectionmap {
108 /* type string for the uci section */
109 const char *type;
110
111 /* length of the struct to map into, filled in by macro */
112 unsigned int alloc_len;
113
114 /* sectionmap offset, filled in by macro */
115 unsigned int smap_offset;
116
117 /* return a pointer to the section map data (allocate if necessary) */
118 struct ucimap_section_data *(*alloc)(struct uci_map *map,
119 struct uci_sectionmap *sm, struct uci_section *s);
120
121 /* give the caller time to initialize the preallocated struct */
122 int (*init)(struct uci_map *map, void *section, struct uci_section *s);
123
124 /* pass the fully processed struct to the callback after the section end */
125 int (*add)(struct uci_map *map, void *section);
126
127 /* let the callback clean up its own stuff in the section */
128 int (*free)(struct uci_map *map, void *section);
129
130 /* list of option mappings for this section */
131 struct uci_optmap *options;
132 unsigned int n_options;
133 unsigned int options_size;
134 };
135
136 struct uci_optmap {
137 unsigned int offset;
138 const char *name;
139 enum ucimap_type type;
140 int (*parse)(void *section, struct uci_optmap *om, union ucimap_data *data, const char *string);
141 int (*format)(void *section, struct uci_optmap *om, union ucimap_data *data, char **string);
142 union {
143 struct {
144 int base;
145 int min;
146 int max;
147 } i;
148 struct {
149 int maxlen;
150 } s;
151 struct uci_sectionmap *sm;
152 } data;
153 };
154
155 struct ucimap_list {
156 int n_items;
157 union ucimap_data item[];
158 };
159
160 extern int ucimap_init(struct uci_map *map);
161 extern void ucimap_cleanup(struct uci_map *map);
162 extern void ucimap_set_changed(struct ucimap_section_data *sd, void *field);
163 extern int ucimap_store_section(struct uci_map *map, struct uci_package *p, struct ucimap_section_data *sd);
164 extern void ucimap_parse(struct uci_map *map, struct uci_package *pkg);
165