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
|
#include <errno.h>
#include <glob.h>
#include <limits.h>
#include <string.h>
#include <alpm.h>
#include "ini.h"
#include "util.h"
static const int ini_max_recursion = 10;
static int _parse_ini(const char *file, ini_parser_fn cb, void *data,
char **section_name, char *line, int depth)
{
FILE *fp = NULL;
int linenum = 0;
int ret = 0;
if(depth >= ini_max_recursion) {
pm_printf(ALPM_LOG_ERROR,
_("config parsing exceeded max recursion depth of %d.\n"),
ini_max_recursion);
ret = 1;
goto cleanup;
}
pm_printf(ALPM_LOG_DEBUG, "config: attempting to read file %s\n", file);
fp = fopen(file, "r");
if(fp == NULL) {
pm_printf(ALPM_LOG_ERROR, _("config file %s could not be read: %s\n"),
file, strerror(errno));
ret = 1;
goto cleanup;
}
while(fgets(line, PATH_MAX, fp)) {
char *key, *value, *ptr;
size_t line_len;
linenum++;
if((ptr = strchr(line, '#'))) {
*ptr = '\0';
}
line_len = strtrim(line);
if(line_len == 0) {
continue;
}
if(line[0] == '[' && line[line_len - 1] == ']') {
char *name;
if(line_len <= 2) {
pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: bad section name.\n"),
file, linenum);
ret = 1;
goto cleanup;
}
name = strdup(line + 1);
name[line_len - 2] = '\0';
ret = cb(file, linenum, name, NULL, NULL, data);
free(*section_name);
*section_name = name;
if(ret) {
goto cleanup;
}
continue;
}
key = line;
value = line;
strsep(&value, "=");
strtrim(key);
strtrim(value);
if(key == NULL) {
pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: syntax error in config file- missing key.\n"),
file, linenum);
ret = 1;
goto cleanup;
}
if(strcmp(key, "Include") == 0) {
glob_t globbuf;
int globret;
size_t gindex;
if(value == NULL) {
pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: directive '%s' needs a value\n"),
file, linenum, key);
ret = 1;
goto cleanup;
}
globret = glob(value, GLOB_NOCHECK, NULL, &globbuf);
switch(globret) {
case GLOB_NOSPACE:
pm_printf(ALPM_LOG_DEBUG,
"config file %s, line %d: include globbing out of space\n",
file, linenum);
break;
case GLOB_ABORTED:
pm_printf(ALPM_LOG_DEBUG,
"config file %s, line %d: include globbing read error for %s\n",
file, linenum, value);
break;
case GLOB_NOMATCH:
pm_printf(ALPM_LOG_DEBUG,
"config file %s, line %d: no include found for %s\n",
file, linenum, value);
break;
default:
for(gindex = 0; gindex < globbuf.gl_pathc; gindex++) {
pm_printf(ALPM_LOG_DEBUG, "config file %s, line %d: including %s\n",
file, linenum, globbuf.gl_pathv[gindex]);
ret =_parse_ini(globbuf.gl_pathv[gindex], cb, data,
section_name, line, depth + 1);
if(ret) {
globfree(&globbuf);
goto cleanup;
}
}
break;
}
globfree(&globbuf);
continue;
}
if((ret = cb(file, linenum, *section_name, key, value, data)) != 0) {
goto cleanup;
}
}
if(depth == 0) {
ret = cb(NULL, 0, NULL, NULL, NULL, data);
}
cleanup:
if(fp) {
fclose(fp);
}
if(depth == 0) {
free(*section_name);
*section_name = NULL;
}
pm_printf(ALPM_LOG_DEBUG, "config: finished parsing %s\n", file);
return ret;
}
int parse_ini(const char *file, ini_parser_fn cb, void *data)
{
char *section_name = NULL, line[PATH_MAX];
return _parse_ini(file, cb, data, §ion_name, line, 0);
}
|