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