-
Notifications
You must be signed in to change notification settings - Fork 364
/
scfg.c
235 lines (187 loc) · 5.23 KB
/
scfg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "os/mynewt.h"
#include "scfg/scfg.h"
#define SCFG_MAX_SETTING_ID_LEN 64
#define SCFG_MAX_NUM_STR_LEN (sizeof "18446744073709551615")
#define SCFG_FOREACH_SETTING(group, s) \
for (s = (group)->settings; s->name != NULL; s++)
static void
scfg_setting_id(const char *group_name, const char *setting_name, char *buf)
{
int setting_len;
int group_len;
int off;
group_len = strlen(group_name);
setting_len = strlen(setting_name);
/* <group>/<setting> */
assert(group_len + 1 + setting_len <= SCFG_MAX_SETTING_ID_LEN);
off = 0;
memcpy(&buf[off], group_name, group_len);
off += group_len;
buf[off] = '/';
off++;
memcpy(&buf[off], setting_name, setting_len);
off += setting_len;
buf[off] = '\0';
off++;
}
static struct scfg_setting *
scfg_find_setting_by_name(const struct scfg_group *group,
const char *setting_name)
{
const struct scfg_setting *setting;
SCFG_FOREACH_SETTING(group, setting) {
if (strcmp(setting->name, setting_name) == 0) {
return (struct scfg_setting *)setting;
}
}
return NULL;
}
static struct scfg_setting *
scfg_find_setting_by_val(const struct scfg_group *group, const void *val)
{
const struct scfg_setting *setting;
SCFG_FOREACH_SETTING(group, setting) {
if (setting->val == val) {
return (struct scfg_setting *)setting;
}
}
return NULL;
}
/**
* Conf get handler. Converts a setting's underlying variable to a string.
*/
static char *
scfg_handler_get(int argc, char **argv, char *buf, int max_len, void *arg)
{
const struct scfg_setting *setting;
struct scfg_group *group;
group = arg;
if (argc < 1) {
return NULL;
}
setting = scfg_find_setting_by_name(group, argv[0]);
if (setting == NULL) {
return NULL;
}
return conf_str_from_value(setting->type, setting->val, buf, max_len);
}
/**
* Conf set handler. Converts from a string-representation and writes the
* result to the setting's underlying variable.
*/
static int
scfg_handler_set(int argc, char **argv, char *val, void *arg)
{
const struct scfg_setting *setting;
struct scfg_group *group;
int rc;
group = arg;
if (argc < 1) {
return SYS_EINVAL;
}
setting = scfg_find_setting_by_name(group, argv[0]);
if (setting == NULL) {
return SYS_ENOENT;
}
rc = conf_value_from_str(val, setting->type, setting->val,
setting->max_len);
if (rc != 0) {
return os_error_to_sys(rc);
}
return 0;
}
static int
scfg_handler_export(void (*func)(char *name, char *val),
enum conf_export_tgt tgt, void *arg)
{
const struct scfg_setting *setting;
struct scfg_group *group;
char val_buf[SCFG_MAX_NUM_STR_LEN];
char id_buf[SCFG_MAX_SETTING_ID_LEN];
char *val;
group = arg;
SCFG_FOREACH_SETTING(group, setting) {
scfg_setting_id(group->handler.ch_name, setting->name, id_buf);
if (setting->f_private) {
val = "<set>";
} else {
val = conf_str_from_value(setting->type, setting->val,
val_buf, sizeof val_buf);
}
func(id_buf, val);
}
return 0;
}
int
scfg_save_setting(const struct scfg_group *group,
const struct scfg_setting *setting)
{
char id_buf[SCFG_MAX_SETTING_ID_LEN];
char val_buf[SCFG_MAX_NUM_STR_LEN];
char *val;
int rc;
val = conf_str_from_value(setting->type, setting->val,
val_buf, sizeof val_buf);
if (val == NULL) {
return SYS_EUNKNOWN;
}
scfg_setting_id(group->handler.ch_name, setting->name, id_buf);
rc = conf_save_one(id_buf, val);
if (rc != 0) {
return os_error_to_sys(rc);
}
return 0;
}
int
scfg_save_name(const struct scfg_group *group, const char *setting_name)
{
const struct scfg_setting *setting;
setting = scfg_find_setting_by_name(group, setting_name);
if (setting == NULL) {
return SYS_ENOENT;
}
return scfg_save_setting(group, setting);
}
int
scfg_save_val(const struct scfg_group *group, const void *val)
{
const struct scfg_setting *setting;
setting = scfg_find_setting_by_val(group, val);
if (setting == NULL) {
return SYS_ENOENT;
}
return scfg_save_setting(group, setting);
}
int
scfg_register(struct scfg_group *group, char *name)
{
const struct scfg_setting *setting;
int rc;
SCFG_FOREACH_SETTING(group, setting) {
switch (setting->type) {
case CONF_INT8:
case CONF_INT16:
case CONF_INT32:
case CONF_INT64:
case CONF_STRING:
case CONF_BOOL:
break;
default:
/* We don't know how to (de)serialize the other data types. */
return SYS_EINVAL;
}
}
group->handler = (struct conf_handler) {
.ch_name = name,
.ch_get_ext = scfg_handler_get,
.ch_set_ext = scfg_handler_set,
.ch_export_ext = scfg_handler_export,
.ch_arg = group,
.ch_ext = 1,
};
rc = conf_register(&group->handler);
if (rc != 0) {
return os_error_to_sys(rc);
}
return 0;
}