blob: dfbba40ab956748a7c4d712354b7909ee94e7ad6 (
plain)
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
|
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include "config.h"
#include "macros.h"
#include "misc.h"
void cleanup_config(config *conf) {
for (int i = 0; config_keywords[i] != NULL; ++i) {
void *cfg = get_keyword_offset_ptr(config_keywords[i], conf);
if (config_keywords[i]->type == TYPE_STRING && *(void **)cfg != NULL) {
free(*(void **)cfg);
}
}
free(conf);
}
int check_port(void *ctx, void *ret, const keyword *key, keyword_val val) {
int *error = (int *)ctx;
if (key->type == TYPE_STRING) {
int port = strtol(val.str, NULL, 0);
if (port > 0 || port <= 65535) {
*error = 0;
return 0;
} else {
log(LOG_ERR, "Invalid port %d. (Valid port must be between 1, and 65535.)", port);
}
} else {
log(LOG_ERR, "Keyword \"%s\" doesn't return a string.", key->key);
}
*error = 1;
return -1;
}
config *parse_config(const char *filename) {
int error = 0;
int type_error = 0;
long filesize = 0;
config *cfg;
char *buf = read_file(filename, &filesize);
char *tmp = buf;
if (buf == NULL) {
return NULL;
}
cfg = calloc(1, sizeof(config));
if (cfg == NULL) {
free(buf);
return NULL;
}
if (error = parse_key_value_file(cfg, &type_error, config_keywords, buf, "=", NULL)) {
if (type_error || (error >= 3 && error > 7)) {
cleanup_config(cfg);
free(buf);
return NULL;
}
}
free(buf);
return cfg;
}
|