-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.c
177 lines (149 loc) · 4.27 KB
/
config.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
/*
* Copyright (c) 2011 Toni Spets <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "bot.h"
#include <regex.h>
static TAILQ_HEAD(_config_head, config_entry) config_head;
struct config_entry
{
char *section;
char *key;
char *value;
TAILQ_ENTRY(config_entry) config_entries;
};
const char *config_get(const char *key)
{
struct config_entry *e;
char *section = NULL;
CTX ctx;
ctx = bot_get_ctx();
if (ctx)
{
section = ctx->name;
}
TAILQ_FOREACH(e, &config_head, config_entries)
{
if (((section == NULL && e->section == NULL) || (section && e->section && strcmp(section, e->section) == 0)) && strcmp(key, e->key) == 0)
{
return e->value;
}
}
return NULL;
}
void config_regex_load(char **dst, const char *src, regmatch_t *m)
{
int len;
len = m->rm_eo - m->rm_so;
*dst = malloc(len + 1);
memcpy(*dst, src + m->rm_so, len);
*(*dst + len) = '\0';
}
void config_load(const char *file)
{
regex_t preg_section;
regex_t preg_pair;
regex_t preg_pair_q;
FILE *fh;
char buf[512];
regmatch_t pmatch[3];
struct config_entry *e;
char *section;
TAILQ_INIT(&config_head);
if (regcomp(&preg_section, "^\\[([a-z]+)\\]", REG_EXTENDED) != 0)
{
log_printf("config: error compiling section regexp\n");
}
if (regcomp(&preg_pair, "^[[:space:]]*([a-z0-9_]+)[[:space:]]*=[[:space:]]*([^;[:space:]]+)", REG_EXTENDED) != 0)
{
log_printf("config: error compiling pair regexp\n");
}
if (regcomp(&preg_pair_q, "^[[:space:]]*([a-z0-9_]+)[[:space:]]*=[[:space:]]*\"([^\"]+)\"", REG_EXTENDED) != 0)
{
log_printf("config: error compiling pair regexp\n");
}
fh = fopen(file, "r");
if (!fh)
{
log_printf("config: error opening file for reading\n");
return;
}
section = NULL;
while ( fgets(buf, 512, fh) )
{
buf[strcspn(buf, "\r\n")] = '\0';
e = NULL;
if (regexec(&preg_pair_q, buf, 3, pmatch, 0) != REG_NOMATCH)
{
e = calloc(sizeof(struct config_entry), 1);
if (section)
{
e->section = strdup(section);
}
config_regex_load(&e->key, buf, &pmatch[1]);
config_regex_load(&e->value, buf, &pmatch[2]);
}
else if (regexec(&preg_pair, buf, 3, pmatch, 0) != REG_NOMATCH)
{
e = calloc(sizeof(struct config_entry), 1);
if (section)
{
e->section = strdup(section);
}
config_regex_load(&e->key, buf, &pmatch[1]);
config_regex_load(&e->value, buf, &pmatch[2]);
}
else if (regexec(&preg_section, buf, 3, pmatch, 0) != REG_NOMATCH)
{
if (section)
{
free(section);
}
config_regex_load(§ion, buf, &pmatch[1]);
}
if (e)
{
TAILQ_INSERT_TAIL(&config_head, e, config_entries);
}
}
if (section)
{
free(section);
}
fclose(fh);
regfree(&preg_section);
regfree(&preg_pair);
regfree(&preg_pair_q);
}
void config_free()
{
struct config_entry *e;
while ( (e = TAILQ_LAST(&config_head, _config_head)) )
{
if (e->section != NULL)
{
free(e->section);
}
if (e->key != NULL)
{
free(e->key);
}
if (e->value != NULL)
{
free(e->value);
}
TAILQ_REMOVE(&config_head, e, config_entries);
free(e);
}
}