forked from keirf/disk-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.c
344 lines (311 loc) · 9.17 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
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
/*
* disk-analyse/config.c
*
* Parse config file which defines allowed formats for particular disks.
*
* Written in 2011 by Keir Fraser
*/
#include <stdint.h>
#include <stdarg.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <libdisk/disk.h>
#include <libdisk/util.h>
#include "common.h"
#define NR_TRACKS 200
#define DEF_DIR PREFIX "/share/disk-analyse"
#define DEF_FIL "formats"
struct token {
enum { STR, NUM, CHR, EOL } type;
union {
char str[128];
struct {
unsigned int start, end, step;
} num;
int ch;
} u;
};
static struct file_info {
FILE *f;
char *name;
unsigned int line;
struct file_info *next;
} *fi;
static void parse_err(const char *f, ...)
{
char errs[128];
va_list args;
va_start(args, f);
vsnprintf(errs, sizeof(errs), f, args);
va_end(args);
errx(1, "error at %s:%u: %s", fi->name, fi->line, errs);
}
static int mygetc(void)
{
int c = fgetc(fi->f);
if (c == '\n')
fi->line++;
return c;
}
static void myungetc(int c)
{
if (c == '\n')
fi->line--;
ungetc(c, fi->f);
}
static void parse_token(struct token *t)
{
int c;
while (isspace(c = mygetc()) && (c != '\n'))
continue;
retry:
if (isdigit(c)) {
t->type = NUM;
t->u.num.start = c - '0';
while (isdigit(c = mygetc()))
t->u.num.start = t->u.num.start * 10 + c - '0';
t->u.num.end = t->u.num.start;
t->u.num.step = 1;
if (c == '-') {
t->u.num.end = 0;
while (isdigit(c = mygetc()))
t->u.num.end = t->u.num.end * 10 + c - '0';
if (t->u.num.end < t->u.num.start)
parse_err("bad range %u-%u", t->u.num.start, t->u.num.end);
}
if (c == '/') {
t->u.num.step = 0;
while (isdigit(c = mygetc()))
t->u.num.step = t->u.num.step * 10 + c - '0';
}
myungetc(c);
} else if (c == '"') {
char *p = t->u.str;
t->type = STR;
while ((c = mygetc()) != '"') {
if ((c == '\n') || (c == '\r') || (c == EOF))
parse_err("unexpected newline or end-of-file in string");
*p++ = c;
if ((p - t->u.str) >= (sizeof(t->u.str)-1))
parse_err("string too long");
}
*p = '\0';
} else if (isalpha(c)) {
char *p = t->u.str;
t->type = STR;
*p++ = c;
while (isalnum(c = mygetc()) || (c == '_')) {
*p++ = c;
if ((p - t->u.str) >= (sizeof(t->u.str)-1))
parse_err("string too long");
}
*p = '\0';
myungetc(c);
} else if (c == '\\') { /* ignore EOL at line break */
while (isspace(c = mygetc()) && (c != '\n'))
continue;
if (c != '\n')
parse_err("expected newline after backslash");
while (isspace(c = mygetc()) && (c != '\n'))
continue;
goto retry;
} else if (c == '#') { /* ignore until EOL */
while (((c = mygetc()) != EOF) && (c != '\n'))
continue;
goto retry;
} else if ((c == EOF) || (c == '\n')) {
t->type = EOL;
t->u.ch = c;
} else {
t->type = CHR;
t->u.ch = c;
}
}
static struct file_info *open_file(char *name)
{
struct file_info *fi = memalloc(sizeof(*fi));
fi->line = 1;
if (name[0] != '/') {
char *path;
if ((path = getcwd(NULL, 0)) == NULL)
err(1, NULL);
fi->name = memalloc(strlen(path) + strlen(name) + 2);
sprintf(fi->name, "%s/%s", path, name);
free(path);
if ((fi->f = fopen(fi->name, "r")) == NULL) {
memfree(fi->name);
fi->name = memalloc(strlen(DEF_DIR) + strlen(name) + 2);
sprintf(fi->name, "%s/%s", DEF_DIR, name);
fi->f = fopen(fi->name, "r");
}
} else {
fi->name = memalloc(strlen(name) + 1);
strcpy(fi->name, name);
fi->f = fopen(fi->name, "r");
}
if (fi->f == NULL) {
memfree(fi->name);
memfree(fi);
fi = NULL;
}
return fi;
}
void close_file(struct file_info *fi)
{
if (fi->f != NULL)
fclose(fi->f);
memfree(fi->name);
memfree(fi);
}
struct format_list *realloc_format_list(struct format_list *old)
{
struct format_list *list;
unsigned int max = old ? old->max*2 : 4;
list = memalloc(sizeof(*list) + (max-1)*2);
if (old) {
memcpy(list, old, sizeof(*list) + (old->max-1)*2);
memfree(old);
}
list->max = max;
return list;
}
struct format_list **parse_config(char *config, char *specifier)
{
unsigned int i;
struct format_list **formats, ignore_list;
struct token t;
char *spec;
formats = memalloc(NR_TRACKS * sizeof(*formats));
if (specifier == NULL)
specifier = "default";
spec = memalloc(strlen(specifier)+1);
strcpy(spec, specifier);
if ((fi = open_file(config ? : DEF_FIL)) == NULL)
errx(1, "could not open config file \"%s\"", config ? : DEF_FIL);
for (;;) {
parse_token(&t);
if ((t.type == EOL) && (t.u.ch == EOF)) {
struct file_info *fi2 = fi->next;
if (fi2 == NULL)
parse_err("no match for \"%s\"", spec);
close_file(fi);
fi = fi2;
} else if (t.type != STR) {
/* nothing */
} else if (!strcmp("INCLUDE", t.u.str)) {
struct file_info *fi2;
parse_token(&t);
if (t.type != STR)
parse_err("expected string after INCLUDE");
if ((fi2 = open_file(t.u.str)) == NULL)
parse_err("could not open config file \"%s\"", t.u.str);
fi2->next = fi;
fi = fi2;
t.type = EOL;
} else if (!strcmp(spec, t.u.str)) {
parse_token(&t);
if ((t.type == CHR) && (t.u.ch == '=')) {
parse_token(&t);
if (t.type != STR)
parse_err("expected string after =");
if (verbose)
printf("Format \"%s\" -> \"%s\"\n", spec, t.u.str);
memfree(spec);
spec = memalloc(strlen(t.u.str)+1);
strcpy(spec, t.u.str);
} else if ((t.type == STR) && !strcmp(t.u.str, "WARN")) {
while (t.type != EOL)
parse_token(&t);
parse_token(&t);
if (t.type != STR)
parse_err("expected string after WARN");
printf("*** WARNING: %s\n", t.u.str);
} else {
goto found;
}
}
while (t.type != EOL)
parse_token(&t);
}
found:
if (verbose)
printf("Found format \"%s\"\n", spec);
for (;;) {
const char *fmtname;
unsigned int start, end, step;
struct format_list *list = realloc_format_list(NULL);
while (t.type != EOL)
parse_token(&t);
parse_token(&t);
if ((t.type == CHR) && (t.u.ch == '*')) {
t.type = NUM;
t.u.num.start = 0;
t.u.num.end = NR_TRACKS-1;
t.u.num.step = 1;
}
if (t.type != NUM) {
memfree(list);
break;
}
start = t.u.num.start;
end = t.u.num.end;
step = t.u.num.step;
if ((start >= NR_TRACKS) || (end >= NR_TRACKS))
parse_err("bad track range %u-%u", start, end);
for (;;) {
parse_token(&t);
if (t.type == EOL)
break;
if (list == &ignore_list)
parse_err("'ignore' must be sole format specifier");
if (t.type != STR)
parse_err("expected format string");
if (!strcmp("ignore", t.u.str)) {
if (list->nr != 0)
parse_err("'ignore' must be sole format specifier");
memfree(list);
list = &ignore_list;
} else {
for (i = 0;
(fmtname = disk_get_format_id_name(i)) != NULL;
i++)
if (!strcmp(fmtname, t.u.str))
break;
if (fmtname == NULL)
parse_err("bad format name \"%s\"", t.u.str);
if (list->nr == list->max)
list = realloc_format_list(list);
list->ent[list->nr++] = i;
}
}
if ((list->nr == 0) && (list != &ignore_list))
parse_err("empty format list");
for (i = start; i <= end; i += step)
if (formats[i] == NULL)
formats[i] = list;
}
for (i = 0; i < NR_TRACKS; i++) {
if (formats[i] == NULL)
parse_err("no format specified for track %u", i);
if (formats[i] == &ignore_list)
formats[i] = NULL;
}
memfree(spec);
while (fi != NULL) {
struct file_info *fi2 = fi->next;
close_file(fi);
fi = fi2;
}
return formats;
}
/*
* Local variables:
* mode: C
* c-file-style: "Linux"
* c-basic-offset: 4
* tab-width: 4
* indent-tabs-mode: nil
* End:
*/