preserve section list order
[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
58         void *priv; /* user data */
59 };
60
61 enum ucimap_type {
62         /* types */
63         UCIMAP_SIMPLE   = 0x00,
64         UCIMAP_LIST     = 0x10,
65         UCIMAP_TYPE     = 0xf0, /* type mask */
66
67         /* subtypes */
68         UCIMAP_STRING   = 0x0,
69         UCIMAP_BOOL     = 0x1,
70         UCIMAP_INT      = 0x2,
71         UCIMAP_SECTION  = 0x3,
72         UCIMAP_CUSTOM   = 0x4,
73         UCIMAP_SUBTYPE  = 0xf, /* subtype mask */
74
75         /* automatically create lists from
76          * options with space-separated items */
77         UCIMAP_LIST_AUTO = 0x0100,
78         UCIMAP_FLAGS     = 0xff00, /* flags mask */
79 };
80
81 union ucimap_data {
82         int i;
83         bool b;
84         char *s;
85         void *ptr;
86         struct ucimap_list *list;
87 };
88
89 struct ucimap_section_data {
90         struct list_head list;
91         struct uci_map *map;
92         struct uci_sectionmap *sm;
93         const char *section_name;
94
95         /* list of allocations done by ucimap */
96         struct uci_alloc *allocmap;
97         unsigned long allocmap_len;
98
99         /* map for changed fields */
100         unsigned char *cmap;
101         bool done;
102 };
103
104
105 struct uci_listmap {
106         struct list_head list;
107         union ucimap_data data;
108 };
109
110 struct uci_sectionmap {
111         /* type string for the uci section */
112         const char *type;
113
114         /* length of the struct to map into, filled in by macro */
115         unsigned int alloc_len;
116
117         /* sectionmap offset, filled in by macro */
118         unsigned int smap_offset;
119
120         /* return a pointer to the section map data (allocate if necessary) */
121         struct ucimap_section_data *(*alloc)(struct uci_map *map,
122                 struct uci_sectionmap *sm, struct uci_section *s);
123
124         /* give the caller time to initialize the preallocated struct */
125         int (*init)(struct uci_map *map, void *section, struct uci_section *s);
126
127         /* pass the fully processed struct to the callback after the section end */
128         int (*add)(struct uci_map *map, void *section);
129
130         /* let the callback clean up its own stuff in the section */
131         int (*free)(struct uci_map *map, void *section);
132
133         /* list of option mappings for this section */
134         struct uci_optmap *options;
135         unsigned int n_options;
136         unsigned int options_size;
137 };
138
139 struct uci_optmap {
140         unsigned int offset;
141         const char *name;
142         enum ucimap_type type;
143         int (*parse)(void *section, struct uci_optmap *om, union ucimap_data *data, const char *string);
144         int (*format)(void *section, struct uci_optmap *om, union ucimap_data *data, char **string);
145         union {
146                 struct {
147                         int base;
148                         int min;
149                         int max;
150                 } i;
151                 struct {
152                         int maxlen;
153                 } s;
154                 struct uci_sectionmap *sm;
155         } data;
156 };
157
158 struct ucimap_list {
159         int n_items;
160         union ucimap_data item[];
161 };
162
163 extern int ucimap_init(struct uci_map *map);
164 extern void ucimap_cleanup(struct uci_map *map);
165 extern void ucimap_set_changed(struct ucimap_section_data *sd, void *field);
166 extern int ucimap_store_section(struct uci_map *map, struct uci_package *p, struct ucimap_section_data *sd);
167 extern void ucimap_parse(struct uci_map *map, struct uci_package *pkg);
168 extern int ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s);
169
170 #endif