-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
324 lines (292 loc) · 6.98 KB
/
parser.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
#include <sys/mman.h>
#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "parser.h"
char parser_error[100];
enum tokentype {
T_EOF = 0,
T_RUN,
T_INTERFACE,
T_DOMAIN,
T_LBRACE,
T_RBRACE,
T_LINEFEED,
T_SPACE,
T_QUOTE,
T_STRING
};
struct lexer {
const char *lex_path;
const char *lex_text;
const char *lex_lptr;
int lex_lnum;
size_t lex_size;
size_t lex_read;
};
struct token {
const char *tok_text;
size_t tok_size;
enum tokentype tok_type;
};
static size_t
consume_string(const char *s, size_t slen)
{
size_t i;
for (i = 0; i < slen; i++) {
if (s[i] == '{' || s[i] == '}' || s[i] == '#' ||
s[i] == ' ' || s[i] == '\t' || s[i] == '\n')
break;
}
return i;
}
static enum tokentype
next_token(struct lexer *lex, struct token *tok)
{
size_t delta;
const char *s;
s = &lex->lex_text[lex->lex_read];
delta = lex->lex_size - lex->lex_read;
if (lex->lex_read >= lex->lex_size) {
tok->tok_size = 0;
tok->tok_type = T_EOF;
}
bool in_comment = false;
for (size_t i = 0; i <= delta; i++) {
if (s[i] == '\n') {
in_comment = false;
lex->lex_lptr = s;
lex->lex_lnum++;
continue;
}
if (s[i] == '#') {
in_comment = true;
continue;
}
if (isblank(s[i]))
continue;
if (in_comment)
continue;
lex->lex_read += i;
break;
}
s = &lex->lex_text[lex->lex_read];
delta = lex->lex_size - lex->lex_read;
if (lex->lex_read >= lex->lex_size) {
tok->tok_size = 0;
tok->tok_type = T_EOF;
}
else if (delta >= (sizeof("run")-1) && s[0] == 'r' && s[1] == 'u' && s[2] == 'n') {
tok->tok_size = sizeof("run") - 1;
tok->tok_type = T_RUN;
}
else if (delta >= (sizeof("interface")-1) && s[0] == 'i' && s[1] == 'n' && s[2] == 't'
&& s[3] == 'e' && s[4] == 'r' && s[5] == 'f' && s[6] == 'a' && s[7] == 'c' && s[8] == 'e') {
tok->tok_size = sizeof("interface") - 1;
tok->tok_type = T_INTERFACE;
}
else if (delta >= (sizeof("domain")-1) && s[0] == 'd' && s[1] == 'o' &&
s[2] == 'm' && s[3] == 'a' && s[4] == 'i' && s[5] == 'n') {
tok->tok_size = sizeof("domain") - 1;
tok->tok_type = T_DOMAIN;
}
else if (*s == '{') {
tok->tok_size = 1;
tok->tok_type = T_LBRACE;
}
else if (*s == '}') {
tok->tok_size = 1;
tok->tok_type = T_RBRACE;
}
else if (*s == '"') {
size_t i;
for (i = 1; i <= delta; i++) {
if (s[i] == '"') {
tok->tok_type = T_QUOTE;
break;
}
if (s[i] == '\n') {
tok->tok_type = T_STRING;
break;
}
}
tok->tok_size = i + 1;
}
else {
tok->tok_size = consume_string(s, delta);
tok->tok_type = T_STRING;
}
tok->tok_text = s;
lex->lex_read += tok->tok_size;
return tok->tok_type;
}
static void
error(struct lexer *lex, struct token *tok, const char *e)
{
snprintf(parser_error, sizeof(parser_error), "%s:%d:%zu: %s",
lex->lex_path, lex->lex_lnum, 1 + tok->tok_text - lex->lex_lptr, e);
}
struct ast *
parse(const char *path)
{
int fd;
struct stat st;
const char *text;
struct lexer lex;
struct token tok;
struct ast_if *aif;
struct ast_dn *adn;
struct ast *ast;
if (-1 == (fd = open(path, O_RDONLY|O_CLOEXEC))) {
snprintf(parser_error, sizeof(parser_error), "open(2): %s", strerror(errno));
return NULL;
}
if (-1 == fstat(fd, &st)) {
snprintf(parser_error, sizeof(parser_error), "fstat(2): %s", strerror(errno));
close(fd);
return NULL;
}
if (MAP_FAILED == (text = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0))) {
snprintf(parser_error, sizeof(parser_error), "mmap(2): %s", strerror(errno));
close(fd);
return NULL;
}
if (NULL == (ast = malloc(sizeof(*ast)))) {
snprintf(parser_error, sizeof(parser_error), "malloc(3): %s", strerror(errno));
return NULL;
}
close(fd);
SLIST_INIT(&ast->aif_head);
madvise((void*)text, st.st_size, MADV_SEQUENTIAL);
lex.lex_path = path;
lex.lex_text = text;
lex.lex_lptr = text;
lex.lex_size = st.st_size;
lex.lex_read = 0;
lex.lex_lnum = 1;
S_TOP:
switch (next_token(&lex, &tok)) {
case T_EOF: goto S_END;
case T_RUN: goto S_RUN;
case T_INTERFACE:
if (NULL == (aif = malloc(sizeof(*aif)))) {
snprintf(parser_error, sizeof(parser_error), "malloc(3): %s", strerror(errno));
goto S_ERROR;
}
SLIST_INIT(&aif->adn_head);
SLIST_INSERT_HEAD(&ast->aif_head, aif, next);
goto S_INTERFACE;
default:
error(&lex, &tok, "invalid syntax: expected 'run' or 'interface'");
goto S_ERROR;
}
S_RUN:
switch (next_token(&lex, &tok)) {
case T_QUOTE:
tok.tok_text += 1;
tok.tok_size -= 2;
/* fall through */
case T_INTERFACE:
case T_DOMAIN:
case T_RUN:
case T_LBRACE:
case T_RBRACE:
case T_STRING:
ast->run = strndup(&tok.tok_text[0], tok.tok_size);
goto S_TOP;
default:
error(&lex, &tok, "invalid syntax: expected command after 'run'");
goto S_ERROR;
}
S_INTERFACE:
switch (next_token(&lex, &tok)) {
case T_QUOTE:
tok.tok_text += 1;
tok.tok_size -= 2;
/* fall through */
case T_STRING: {
size_t count = tok.tok_size < sizeof(aif->name) ?
tok.tok_size : sizeof(aif->name) - 1;
strncpy(aif->name, tok.tok_text, count);
aif->name[count] = '\0';
goto S_INTERFACE_STRING;
}
default:
error(&lex, &tok, "invalid syntax: expected interface name");
goto S_ERROR;
}
S_INTERFACE_STRING:
switch (next_token(&lex, &tok)) {
case T_LBRACE: goto S_INTERFACE_LBRACE;
default:
error(&lex, &tok, "invalid syntax: expected '{'");
goto S_ERROR;
}
S_INTERFACE_LBRACE:
switch (next_token(&lex, &tok)) {
case T_DOMAIN: goto S_DOMAIN;
default:
error(&lex, &tok, "invalid syntax: expected 'domain'");
goto S_ERROR;
}
S_DOMAIN:
switch (next_token(&lex, &tok)) {
case T_QUOTE:
tok.tok_text += 1;
tok.tok_size -= 2;
/* fall through */
case T_STRING:
if (NULL == (adn = malloc(sizeof(*adn) + tok.tok_size + 1))) {
snprintf(parser_error, sizeof(parser_error), "malloc(3): %s", strerror(errno));
goto S_ERROR;
}
strncpy(adn->name, tok.tok_text, tok.tok_size);
adn->name[tok.tok_size] = '\0';
SLIST_INSERT_HEAD(&aif->adn_head, adn, next);
goto S_DOMAIN_STRING;
default:
error(&lex, &tok, "invalid syntax: expected domain name");
goto S_ERROR;
}
S_DOMAIN_STRING:
switch (next_token(&lex, &tok)) {
case T_DOMAIN: goto S_DOMAIN;
case T_RBRACE: goto S_INTERFACE_RBRACE;
default:
error(&lex, &tok, "invalid syntax: expected 'domain' or '}'");
goto S_ERROR;
}
S_INTERFACE_RBRACE:
switch (next_token(&lex, &tok)) {
case T_RUN: goto S_RUN;
case T_INTERFACE: goto S_INTERFACE;
case T_EOF: goto S_END;
default:
error(&lex, &tok, "invalid syntax: expected 'run' or 'interface'");
goto S_ERROR;
}
S_ERROR:
// free that which was already parsed
while (!SLIST_EMPTY(&ast->aif_head)) {
aif = SLIST_FIRST(&ast->aif_head);
while (!SLIST_EMPTY(&aif->adn_head)) {
adn = SLIST_FIRST(&aif->adn_head);
SLIST_REMOVE_HEAD(&aif->adn_head, next);
free(adn);
}
SLIST_REMOVE_HEAD(&ast->aif_head, next);
free(aif);
}
free((char*)ast->run);
free(ast);
ast = NULL;
S_END:
munmap((void*)lex.lex_text, lex.lex_size);
return ast;
}