-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.c
More file actions
190 lines (161 loc) · 4.64 KB
/
config.c
File metadata and controls
190 lines (161 loc) · 4.64 KB
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
/* Configuration reading and parsing. */
#include <assert.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Nicolas Devillard's INI parser. */
#include <dictionary.h>
#include <iniparser.h>
#include "config.h"
#include "entry.h"
#include "macros.h"
#define printf_arg int(*debug_printf)(char *format, ...)
#define DPRINTF(args...) \
do { \
if (debug_printf != NULL) { \
debug_printf(args); \
} \
} while (0)
static char *make_key(char *prefix, char *suffix) {
char *index = (char*)malloc(sizeof(char) *
(strlen(prefix) + strlen(":") + strlen(suffix) + 1));
if (index == NULL) {
return NULL;
}
strcpy(index, prefix);
strcat(index, ":");
strcat(index, suffix);
return index;
}
/* Wrapper around iniparser_getstring(). */
static char *get_string(dictionary *d, char *section, char *key) {
assert(d != NULL);
assert(section != NULL);
assert(key != NULL);
/* INI parser stores items indexed by "section:key" */
char *index = make_key(section, key);
if (index == NULL) {
return NULL;
}
char *value = iniparser_getstring(d, index, NULL);
free(index);
return value;
}
/* Wrapper around iniparser_getint(). */
static int get_int(dictionary *d, char *section, char *key, int notfound) {
assert(d != NULL);
assert(section != NULL);
assert(key != NULL);
char *index = make_key(section, key);
if (index == NULL) {
return notfound;
}
int i = iniparser_getint(d, index, notfound);
free(index);
return i;
}
/* Parse a string into a directory entry. An entry is expected to be in the
* form:
*
* [path]
* access = permissions
* command = command to execute
*
* Permissions should be given in chmod numerical form. The first parameter is
* taken as a location to write the parsed entry to and this function returns
* non-zero on failure.
*/
static int parse_entry(entry_t *e, dictionary *d, char *name, printf_arg) {
assert(d != NULL);
assert(name != NULL);
assert(e != NULL);
e->size = UNSPECIFIED_SIZE;
/* Copy path. */
e->path = strdup(name);
if (e->path == NULL) {
errno = ENOMEM;
goto parse_entry_fail;
}
/* Parse permissions. */
char *tmp = get_string(d, name, "access");
if (tmp == NULL) {
DPRINTF("Missing access entry\n");
goto parse_entry_fail;
}
unsigned int u, g, o;
if (sscanf(tmp, "%1u%1u%1u", &u, &g, &o) != 3 ||
u & ~(R|W|X) || g & ~(R|W|X) || o & ~(R|W|X)) {
DPRINTF("Invalid permissions entry\n");
goto parse_entry_fail;
}
e->u_r = !!(u & R); e->u_w = !!(u & W); e->u_x = !!(u & X);
e->g_r = !!(g & R); e->g_w = !!(g & W); e->g_x = !!(g & X);
e->o_r = !!(o & R); e->o_w = !!(o & W); e->o_x = !!(o & X);
/* Parse command. */
tmp = get_string(d, name, "command");
if (tmp == NULL) {
DPRINTF("Missing command entry\n");
goto parse_entry_fail;
}
e->command = strdup(tmp);
if (e->command == NULL) {
errno = ENOMEM;
goto parse_entry_fail;
}
/* Parse size. */
e->size = get_int(d, name, "size", UNSPECIFIED_SIZE);
/* Parse cacheable. */
e->cache = get_int(d, name, "cache", 0);
return 0;
parse_entry_fail:
if (e->path != NULL) free(e->path);
if (e->command != NULL) free(e->command);
return -1;
}
entry_t *parse_config(size_t *len, char *filename, printf_arg) {
assert(len != NULL);
assert(filename != NULL);
*len = 0;
entry_t *entries = NULL;
dictionary *d = NULL;
if ((d = iniparser_load(filename)) == NULL) {
goto parse_config_fail;
}
*len = iniparser_getnsec(d);
DPRINTF("%d sections found in configuration file.\n", *len);
if (*len == -1) {
goto parse_config_fail;
}
entries = (entry_t*)malloc(sizeof(entry_t) * *len);
if (entries == NULL) {
*len = 0;
goto parse_config_fail;
}
memset(entries, 0, sizeof(entry_t) * *len);
int i;
for (i = 0; i < *len; ++i) {
char *secname = iniparser_getsecname(d, i);
if (secname == NULL) {
goto parse_config_fail;
}
DPRINTF("Parsing section %s\n", secname);
if (parse_entry(&entries[i], d, secname, debug_printf) != 0) {
goto parse_config_fail;
}
}
return entries;
parse_config_fail:
assert(len != NULL);
if (entries != NULL) {
int i;
for (i = 0; i < *len; ++i) {
free(entries[i].path);
free(entries[i].command);
}
free(entries);
}
*len = PARSE_FAIL;
return NULL;
}