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