-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
mk_rconf.c
344 lines (287 loc) · 9.37 KB
/
mk_rconf.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Monkey HTTP Server
* ==================
* Copyright 2001-2015 Monkey Software LLC <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <mk_core/mk_rconf.h>
#include <mk_core/mk_utils.h>
#include <mk_core/mk_string.h>
#include <mk_core/mk_list.h>
/* Raise a configuration schema error */
static void mk_config_error(const char *path, int line, const char *msg)
{
mk_err("File %s", path);
mk_err("Error in line %i: %s", line, msg);
exit(EXIT_FAILURE);
}
/* Raise a warning */
static void mk_rconf_warning(const char *path, int line, const char *msg)
{
mk_warn("Config file warning '%s':\n"
"\t\t\t\tat line %i: %s",
path, line, msg);
}
/* Returns a configuration section by [section name] */
struct mk_rconf_section *mk_rconf_section_get(struct mk_rconf *conf,
const char *name)
{
struct mk_list *head;
struct mk_rconf_section *section;
mk_list_foreach(head, &conf->sections) {
section = mk_list_entry(head, struct mk_rconf_section, _head);
if (strcasecmp(section->name, name) == 0) {
return section;
}
}
return NULL;
}
/* Register a key/value entry in the last section available of the struct */
static void mk_rconf_section_entry_add(struct mk_rconf *conf,
const char *key, const char *val)
{
struct mk_rconf_section *section;
struct mk_rconf_entry *new;
struct mk_list *head = &conf->sections;
if (mk_list_is_empty(&conf->sections) == 0) {
mk_err("Error: there are not sections available on %s!", conf->file);
return;
}
/* Last section */
section = mk_list_entry_last(head, struct mk_rconf_section, _head);
/* Alloc new entry */
new = mk_mem_alloc(sizeof(struct mk_rconf_entry));
new->key = mk_string_dup(key);
new->val = mk_string_dup(val);
mk_list_add(&new->_head, §ion->entries);
}
/* Create a configuration schema */
struct mk_rconf *mk_rconf_create(const char *name)
{
struct mk_rconf *conf = NULL;
/* Alloc configuration node */
conf = mk_mem_alloc_z(sizeof(struct mk_rconf));
conf->created = time(NULL);
conf->file = mk_string_dup(name);
mk_list_init(&conf->sections);
return conf;
}
struct mk_rconf *mk_rconf_open(const char *path)
{
int i;
int len;
int line = 0;
int indent_len = -1;
int n_keys = 0;
char buf[1024];
char *section = NULL;
char *indent = NULL;
char *key, *val;
struct mk_rconf *conf = NULL;
struct mk_rconf_section *current = NULL;
FILE *f;
if (!path) {
mk_warn("[config] invalid path file");
return NULL;
}
/* Open configuration file */
if ((f = fopen(path, "r")) == NULL) {
mk_warn("[config] I cannot open %s file", path);
return NULL;
}
/* Alloc configuration node */
conf = mk_mem_alloc_z(sizeof(struct mk_rconf));
conf->created = time(NULL);
conf->file = mk_string_dup(path);
mk_list_init(&conf->sections);
/* looking for configuration directives */
while (fgets(buf, sizeof(buf) - 1, f)) {
len = strlen(buf);
if (buf[len - 1] == '\n') {
buf[--len] = 0;
if (len && buf[len - 1] == '\r') {
buf[--len] = 0;
}
}
/* Line number */
line++;
if (!buf[0]) {
continue;
}
/* Skip commented lines */
if (buf[0] == '#') {
continue;
}
/* Section definition */
if (buf[0] == '[') {
int end = -1;
end = mk_string_char_search(buf, ']', len);
if (end > 0) {
/*
* Before to add a new section, lets check the previous
* one have at least one key set
*/
if (current && n_keys == 0) {
mk_rconf_warning(path, line, "Previous section did not have keys");
}
/* Create new section */
section = mk_string_copy_substr(buf, 1, end);
current = mk_rconf_section_add(conf, section);
mk_mem_free(section);
n_keys = 0;
continue;
}
else {
mk_config_error(path, line, "Bad header definition");
}
}
/* No separator defined */
if (!indent) {
i = 0;
do { i++; } while (i < len && isblank(buf[i]));
indent = mk_string_copy_substr(buf, 0, i);
indent_len = strlen(indent);
/* Blank indented line */
if (i == len) {
continue;
}
}
/* Validate indentation level */
if (strncmp(buf, indent, indent_len) != 0 ||
isblank(buf[indent_len]) != 0) {
mk_config_error(path, line, "Invalid indentation level");
}
if (buf[indent_len] == '#' || indent_len == len) {
continue;
}
/* Get key and val */
i = mk_string_char_search(buf + indent_len, ' ', len - indent_len);
key = mk_string_copy_substr(buf + indent_len, 0, i);
val = mk_string_copy_substr(buf + indent_len + i, 1, len - indent_len - i);
if (!key || !val || i < 0) {
mk_config_error(path, line, "Each key must have a value");
}
/* Trim strings */
mk_string_trim(&key);
mk_string_trim(&val);
/* Register entry: key and val are copied as duplicated */
mk_rconf_section_entry_add(conf, key, val);
/* Free temporal key and val */
mk_mem_free(key);
mk_mem_free(val);
n_keys++;
}
if (section && n_keys == 0) {
/* No key, no warning */
}
/*
struct mk_config_section *s;
struct mk_rconf_entry *e;
s = conf->section;
while(s) {
printf("\n[%s]", s->name);
e = s->entry;
while(e) {
printf("\n %s = %s", e->key, e->val);
e = e->next;
}
s = s->next;
}
fflush(stdout);
*/
fclose(f);
if (indent) mk_mem_free(indent);
return conf;
}
void mk_rconf_free(struct mk_rconf *conf)
{
struct mk_list *head, *tmp;
struct mk_rconf_section *section;
/* Free sections */
mk_list_foreach_safe(head, tmp, &conf->sections) {
section = mk_list_entry(head, struct mk_rconf_section, _head);
mk_list_del(§ion->_head);
/* Free section entries */
mk_rconf_free_entries(section);
/* Free section node */
mk_mem_free(section->name);
mk_mem_free(section);
}
if (conf->file) {
mk_mem_free(conf->file);
}
mk_mem_free(conf);
}
void mk_rconf_free_entries(struct mk_rconf_section *section)
{
struct mk_rconf_entry *entry;
struct mk_list *head, *tmp;
mk_list_foreach_safe(head, tmp, §ion->entries) {
entry = mk_list_entry(head, struct mk_rconf_entry, _head);
mk_list_del(&entry->_head);
/* Free memory assigned */
mk_mem_free(entry->key);
mk_mem_free(entry->val);
mk_mem_free(entry);
}
}
/* Register a new section into the configuration struct */
struct mk_rconf_section *mk_rconf_section_add(struct mk_rconf *conf,
char *name)
{
struct mk_rconf_section *new;
/* Alloc section node */
new = mk_mem_alloc(sizeof(struct mk_rconf_section));
new->name = mk_string_dup(name);
mk_list_init(&new->entries);
mk_list_add(&new->_head, &conf->sections);
return new;
}
/* Return the value of a key of a specific section */
void *mk_rconf_section_get_key(struct mk_rconf_section *section,
char *key, int mode)
{
int on, off;
struct mk_rconf_entry *entry;
struct mk_list *head;
mk_list_foreach(head, §ion->entries) {
entry = mk_list_entry(head, struct mk_rconf_entry, _head);
if (strcasecmp(entry->key, key) == 0) {
switch (mode) {
case MK_RCONF_STR:
return (void *) mk_string_dup(entry->val);
case MK_RCONF_NUM:
return (void *) strtol(entry->val, (char **) NULL, 10);
case MK_RCONF_BOOL:
on = strcasecmp(entry->val, MK_RCONF_ON);
off = strcasecmp(entry->val, MK_RCONF_OFF);
if (on != 0 && off != 0) {
return (void *) -1;
}
else if (on >= 0) {
return (void *) MK_TRUE;
}
else {
return (void *) MK_FALSE;
}
case MK_RCONF_LIST:
return (void *)mk_string_split_line(entry->val);
}
}
}
return NULL;
}