ucimap: ignore unhandled data structure changes for now
[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 OPTMAP_OPTION(_maptype, _type, _field, ...) \
36         { \
37                 .type = _maptype, \
38                 .name = #_field, \
39                 .offset = offsetof(_type, _field), \
40                 __VA_ARGS__  \
41         }
42
43 struct uci_sectmap;
44 struct uci_optmap;
45 struct ucimap_list;
46
47 struct uci_map {
48         struct uci_sectmap **sections;
49         unsigned int n_sections;
50         struct list_head sdata;
51         struct list_head fixup;
52
53         void *priv; /* user data */
54 };
55
56 enum ucimap_type {
57         /* types */
58         UCIMAP_SIMPLE   = 0x00,
59         UCIMAP_LIST     = 0x10,
60         UCIMAP_TYPE     = 0xf0, /* type mask */
61
62         /* subtypes */
63         UCIMAP_STRING   = 0x0,
64         UCIMAP_BOOL     = 0x1,
65         UCIMAP_INT      = 0x2,
66         UCIMAP_SECTION  = 0x3,
67         UCIMAP_SUBTYPE  = 0xf, /* subtype mask */
68 };
69
70 union ucimap_data {
71         int i;
72         bool b;
73         const char *s;
74         void *section;
75         struct ucimap_list *list;
76 };
77
78 struct uci_listmap {
79         struct list_head list;
80         union ucimap_data data;
81 };
82
83 struct uci_sectmap {
84         /* type string for the uci section */
85         const char *type;
86
87         /* length of the struct to map into */
88         unsigned int alloc_len;
89
90         /* give the caller time to initialize the preallocated struct */
91         int (*init_section)(struct uci_map *map, void *section, struct uci_section *s);
92
93         /* pass the fully processed struct to the callback after the section end */
94         int (*add_section)(struct uci_map *map, void *section);
95
96         /* let the callback clean up its own stuff in the section */
97         int (*free_section)(struct uci_map *map, void *section);
98
99         /* list of option mappings for this section */
100         struct uci_optmap *options;
101         unsigned int n_options;
102         unsigned int options_size;
103 };
104
105 struct uci_optmap {
106         unsigned int offset;
107         const char *name;
108         enum ucimap_type type;
109         union {
110                 struct {
111                         int base;
112                         int min;
113                         int max;
114                 } i;
115                 struct {
116                         int maxlen;
117                 } s;
118                 struct uci_sectmap *sm;
119         } data;
120 };
121
122 struct ucimap_list {
123         int n_items;
124         union ucimap_data item[];
125 };
126
127 extern int ucimap_init(struct uci_map *map);
128 extern void ucimap_cleanup(struct uci_map *map);
129 extern void ucimap_set_changed(void *section, void *field);
130 extern int ucimap_store_section(struct uci_map *map, struct uci_package *p, void *section);
131 extern void ucimap_parse(struct uci_map *map, struct uci_package *pkg);
132