remove ucimap-example during make clean
[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
73 union ucimap_data {
74         int i;
75         bool b;
76         char *s;
77         void *section;
78         struct ucimap_list *list;
79 };
80
81 struct ucimap_section_data {
82         struct list_head list;
83         struct uci_map *map;
84         struct uci_sectionmap *sm;
85         const char *section_name;
86
87         /* list of allocations done by ucimap */
88         struct uci_alloc *allocmap;
89         unsigned long allocmap_len;
90
91         /* map for changed fields */
92         unsigned char *cmap;
93         bool done;
94 };
95
96
97 struct uci_listmap {
98         struct list_head list;
99         union ucimap_data data;
100 };
101
102 struct uci_sectionmap {
103         /* type string for the uci section */
104         const char *type;
105
106         /* length of the struct to map into, filled in by macro */
107         unsigned int alloc_len;
108
109         /* sectionmap offset, filled in by macro */
110         unsigned int smap_offset;
111
112         /* return a pointer to the section map data (allocate if necessary) */
113         struct ucimap_section_data *(*alloc)(struct uci_map *map,
114                 struct uci_sectionmap *sm, struct uci_section *s);
115
116         /* give the caller time to initialize the preallocated struct */
117         int (*init)(struct uci_map *map, void *section, struct uci_section *s);
118
119         /* pass the fully processed struct to the callback after the section end */
120         int (*add)(struct uci_map *map, void *section);
121
122         /* let the callback clean up its own stuff in the section */
123         int (*free)(struct uci_map *map, void *section);
124
125         /* list of option mappings for this section */
126         struct uci_optmap *options;
127         unsigned int n_options;
128         unsigned int options_size;
129 };
130
131 struct uci_optmap {
132         unsigned int offset;
133         const char *name;
134         enum ucimap_type type;
135         int (*parse)(void *section, struct uci_optmap *om, union ucimap_data *data, const char *string);
136         int (*format)(void *section, struct uci_optmap *om, union ucimap_data *data, char **string);
137         union {
138                 struct {
139                         int base;
140                         int min;
141                         int max;
142                 } i;
143                 struct {
144                         int maxlen;
145                 } s;
146                 struct uci_sectionmap *sm;
147         } data;
148 };
149
150 struct ucimap_list {
151         int n_items;
152         union ucimap_data item[];
153 };
154
155 extern int ucimap_init(struct uci_map *map);
156 extern void ucimap_cleanup(struct uci_map *map);
157 extern void ucimap_set_changed(struct ucimap_section_data *sd, void *field);
158 extern int ucimap_store_section(struct uci_map *map, struct uci_package *p, struct ucimap_section_data *sd);
159 extern void ucimap_parse(struct uci_map *map, struct uci_package *pkg);
160