-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheparse.y
449 lines (406 loc) · 11.5 KB
/
eparse.y
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
* configk: an easy way to edit kernel configuration files and templates
* Copyright (C) 2023-2024 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See COPYING file or <http://www.gnu.org/licenses/> for more details.
*/
%locations
%define lr.type ielr
%define api.pure full
%define api.prefix {ee}
%define parse.error verbose
%parse-param {unsigned char cmd}
%parse-param {char **val}
%initial-action { ifctx = 0; }
%union {
int num;
char *txt;
};
%token <txt> EE_CONFID EE_VALUE EE_RANGE
%token <txt> EE_IF EE_OR EE_AND
%token <txt> EE_NE EE_LT EE_LE EE_GT EE_GE
%token <txt> EE_BM EE_EM EE_MARG
%token <txt> EE_TAB EE_EOL
%start expression
%type <num> expr expression
%type <txt> macroexpr rangexpr
%left '=' EE_NE EE_LT EE_LE EE_GT EE_GE
%left EE_OR EE_AND
%right EE_IF
%precedence NEG
%{
#include <stdio.h>
#include "configk.h"
static int8_t is_enabled(const char *);
static cEntry *get_centry(const char *);
int8_t eval_expression(uint8_t, const char *, char **);
void yyerror(YYLTYPE *, uint8_t, char **, char const *);
uint8_t ifctx = 0;
extern char *gstr;
extern char *types[];
extern int yylex(YYSTYPE *, YYLTYPE *);
%}
%%
expression:
%empty { $$ = 0; }
| expression expr { return $$ ? $$ && $2 : $2; }
| expression expr ';' {
if ($2 > 0 && (cmd == EXPR_DEFAULT || cmd == EXPR_RANGE))
return $2;
$$ = $$ ? $$ && $2 : $2;
}
;
expr:
EE_CONFID {
$$ = eval_expression(ifctx ? ifctx : cmd, $1, val);
if (opts & OUT_VERBOSE)
fprintf(stderr, "%s(%d) ", $1, $$);
$$ = ($$ <= 0) ? 0 : $$;
free($1);
}
| EE_CONFID EE_IF { ifctx = 1; } expr {
ifctx = 0;
$$ = $4;
if ($$)
$$ = eval_expression(cmd, $1, val);
if (opts & OUT_VERBOSE)
fprintf(stderr, "%s(%d) ", $1, $$);
$$ = ($$ <= 0) ? 0 : $$;
free($1);
}
| EE_VALUE {
$$ = 0;
if (val) {
free(*val);
*val = strdup($1);
if (opts & OUT_VERBOSE)
fprintf(stderr, "v(%s) ", $1);
$$ = 1;
}
free($1);
}
| EE_VALUE EE_IF { ifctx = 1; } expr {
ifctx = 0;
$$ = $4;
if ($$ && val) {
free(*val);
*val = strdup($1);
if (opts & OUT_VERBOSE)
fprintf(stderr, "v(%s) ", $1);
$$ = 1;
}
free($1);
}
| EE_CONFID '=' EE_CONFID {
int8_t e1 = eval_expression(1, $1, val);
int8_t e3 = eval_expression(1, $3, val);
if (opts & OUT_VERBOSE)
fprintf(stderr, "(%s(%d) = %s(%d)) ", $1, e1, $3, e3);
e1 = (e1 <= 0) ? 0 : e1;
e3 = (e3 <= 0) ? 0 : e3;
$$ = (e1 == e3);
free($1); free($3);
}
| EE_CONFID '=' EE_VALUE {
$$ = 0;
cEntry *t1 = get_centry($1);
if (t1) {
char *v1 = t1->opt_value;
$$ = !strcmp(v1, $3);
if (opts & OUT_VERBOSE)
fprintf(stderr, "(%s(%s) = v(%s)) ", $1, v1, $3);
}
free($1); free($3);
}
| EE_CONFID EE_NE EE_CONFID {
int8_t e1 = eval_expression(1, $1, val);
int8_t e3 = eval_expression(1, $3, val);
if (opts & OUT_VERBOSE)
fprintf(stderr, "(%s(%d) != %s(%d)) ", $1, e1, $3, e3);
e1 = (e1 <= 0) ? 0 : e1;
e3 = (e3 <= 0) ? 0 : e3;
$$ = (e1 != e3);
free($1); free($3);
}
| EE_CONFID EE_NE EE_VALUE {
$$ = 0;
cEntry *t = get_centry($1);
if (t) {
$$ = strcmp(t->opt_value, $3);
if (opts & OUT_VERBOSE)
fprintf(stderr, "(%s(%s) != v(%s)) ", $1, t->opt_value, $3);
}
free($1); free($3);
}
| EE_CONFID EE_GE EE_CONFID {
$$ = 0;
cEntry *t1 = get_centry($1);
cEntry *t3 = get_centry($3);
if (t1 && t3) {
char *v1 = t1->opt_value;
char *v3 = t3->opt_value;
$$ = strcmp(v1, v3) >= 0 ? 1 : 0;
if (opts & OUT_VERBOSE)
fprintf(stderr, "(%s(%s) >= %s(%s)) ", $1, v1, $3, v3);
}
free($1); free($3);
}
| EE_CONFID EE_GE EE_VALUE {
$$ = 0;
cEntry *t1 = get_centry($1);
if (t1) {
char *v1 = t1->opt_value;
$$ = strcmp(v1, $3) >= 0 ? 1 : 0;
if (opts & OUT_VERBOSE)
fprintf(stderr, "(%s(%s) >= v(%s)) ", $1, v1, $3);
}
free($1); free($3);
}
| EE_CONFID EE_LE EE_CONFID { $$ = 0;
$$ = 0;
cEntry *t1 = get_centry($1);
cEntry *t3 = get_centry($3);
if (t1 && t3) {
char *v1 = t1->opt_value;
char *v3 = t3->opt_value;
$$ = strcmp(v1, v3) <= 0 ? 1 : 0;
if (opts & OUT_VERBOSE)
fprintf(stderr, "(%s(%s) <= %s(%s)) ", $1, v1, $3, v3);
}
free($1); free($3);
}
| EE_CONFID EE_LE EE_VALUE {
$$ = 0;
cEntry *t1 = get_centry($1);
if (t1) {
char *v1 = t1->opt_value;
$$ = strcmp(v1, $3) <= 0 ? 1 : 0;
if (opts & OUT_VERBOSE)
fprintf(stderr, "(%s(%s) <= v(%s)) ", $1, v1, $3);
}
free($1); free($3);
}
| EE_CONFID EE_GT EE_CONFID {
$$ = 0;
cEntry *t1 = get_centry($1);
cEntry *t3 = get_centry($3);
if (t1 && t3) {
char *v1 = t1->opt_value;
char *v3 = t3->opt_value;
$$ = strcmp(v1, v3) > 0 ? 1 : 0;
if (opts & OUT_VERBOSE)
fprintf(stderr, "(%s(%s) > %s(%s)) ", $1, v1, $3, v3);
}
free($1); free($3);
}
| EE_CONFID EE_GT EE_VALUE {
$$ = 0;
cEntry *t1 = get_centry($1);
if (t1) {
char *v1 = t1->opt_value;
$$ = strcmp(v1, $3) > 0 ? 1 : 0;
if (opts & OUT_VERBOSE)
fprintf(stderr, "(%s(%s) > v(%s)) ", $1, v1, $3);
}
free($1); free($3);
}
| EE_CONFID EE_LT EE_CONFID {
$$ = 0;
cEntry *t1 = get_centry($1);
cEntry *t3 = get_centry($3);
if (t1 && t3) {
char *v1 = t1->opt_value;
char *v3 = t3->opt_value;
$$ = strcmp(v1, v3) < 0 ? 1 : 0;
if (opts & OUT_VERBOSE)
fprintf(stderr, "(%s(%s) < %s(%s)) ", $1, v1, $3, v3);
}
free($1); free($3);
}
| EE_CONFID EE_LT EE_VALUE {
$$ = 0;
cEntry *t1 = get_centry($1);
if (t1) {
char *v1 = t1->opt_value;
$$ = strcmp(v1, $3) < 0 ? 1 : 0;
if (opts & OUT_VERBOSE)
fprintf(stderr, "(%s(%s) < v(%s)) ", $1, v1, $3);
}
free($1); free($3);
}
| '!' expr %prec NEG { $$ = !$2; }
| expr EE_OR expr { $$ = ($1 || $3); }
| expr EE_AND expr { $$ = ($1 && $3); }
| EE_BM expr EE_EM { $$ = $2; }
| EE_BM macroexpr EE_EM {
$$=0;
if (opts & OUT_VERBOSE)
fprintf(stderr, "$(%s)\n", $2);
free($2);
}
| EE_BM macroexpr EE_EM EE_IF { ifctx=1; } expr {
$$=0;
if (opts & OUT_VERBOSE)
fprintf(stderr, "$(%s) if %d\n", $2, $6);
free($2);
}
| EE_RANGE rangexpr {
$$ = 0;
if (val) {
snprintf(*val, 64, "%s", $2);
$$ = 1;
}
free($2);
}
| EE_RANGE rangexpr EE_IF { ifctx = 1; } expr {
ifctx = 0;
$$ = $5;
if ($$ && val)
snprintf(*val, 64, "%s", $2);
else if (!$$)
snprintf(*val, 64, "0 0");
free($2);
}
;
macroexpr:
EE_MARG { $$ = $1; }
| EE_MARG ',' EE_MARG {
int len = strlen($1) + strlen($3) + 3;
char *s = calloc(len, sizeof(uint8_t));
snprintf(s, len, "%s,%s", $1, $3);
$$ = s;
free($1); free($3);
}
| EE_MARG ',' EE_MARG ',' EE_MARG {
int len = strlen($1) + strlen($3) + strlen($5) + 4;
char *s = calloc(len, sizeof(uint8_t));
snprintf(s, len, "%s,%s,%s", $1, $3, $5);
$$ = s;
free($1); free($3); free($5);
}
| EE_MARG ',' EE_MARG ',' EE_MARG ',' EE_MARG {
int len = strlen($1) + strlen($3) + strlen($5) + strlen($7) + 5;
char *s = calloc(len, sizeof(uint8_t));
snprintf(s, len, "%s,%s,%s,%s", $1, $3, $5, $7);
$$ = s;
free($1); free($3); free($5); free($7);
}
;
rangexpr:
EE_VALUE EE_VALUE {
int len = strlen($1) + strlen($2) + 2;
char *s = calloc(len, sizeof(uint8_t));
snprintf(s, len, "%s %s", $1, $2);
$$ = s;
free($1); free($2);
}
| EE_VALUE EE_CONFID {
char *s = NULL;
int len = strlen($1) + 2;
cEntry *t1 = get_centry($2);
if (t1 && t1->opt_status) {
len += strlen(t1->opt_value);
s = calloc(len, sizeof(uint8_t));
snprintf(s, len, "%s %s", $1, t1->opt_value);
}
else {
s = calloc(len, sizeof(uint8_t));
snprintf(s, len, "%s 0", $1);
}
$$ = s;
free($1); free($2);
}
| EE_CONFID EE_CONFID {
char *s = NULL;
cEntry *t1 = get_centry($1);
cEntry *t2 = get_centry($2);
if (t1 && t1->opt_status && t2 && t2->opt_status) {
int len = strlen(t1->opt_value) + strlen(t2->opt_value) + 2;
s = calloc(len, sizeof(uint8_t));
snprintf(s, len, "%s %s", t1->opt_value, t2->opt_value);
}
else {
uint8_t len = 5;
s = calloc(len, sizeof(uint8_t));
snprintf(s, len, "0 0");
}
$$ = s;
free($1); free($2);
}
;
%%
void
yyerror(YYLTYPE *loc, uint8_t etype, char **val, char const *serr)
{
warnx("%s => %d:%d: %s:%s", __func__, loc->last_line, etype, *val, serr);
}
static cEntry *
get_centry(const char *sopt)
{
cNode *c = hsearch_kconfigs(sopt);
if (!c)
return NULL;
return (cEntry *)c->data;
}
static int8_t
is_enabled(const char *sopt)
{
cEntry *t = get_centry(sopt);
if (!t)
//warnx("%s: option %s not found", __func__, sopt);
return -1;
if (!t->opt_status)
return t->opt_status;
if (t->opt_value && (CBOOL == t->opt_type || CTRISTATE == t->opt_type))
{
if ('n' == *t->opt_value || 'N' == *t->opt_value)
return 0;
else if ('m' == *t->opt_value || 'M' == *t->opt_value)
return 1;
else
return 2;
}
return t->opt_status;
}
int8_t
get_default_value(const char *sopt, char **val)
{
cNode *c = hsearch_kconfigs(sopt);
if (!c)
return 0;
cEntry *t = (cEntry *)c->data;
if (t->opt_status && val)
{
free(*val);
*val = strdup(t->opt_value);
}
return t->opt_status;
}
int8_t
eval_expression(uint8_t cmd, const char *opt, char **val)
{
int8_t r = 0;
switch (cmd)
{
case EXPR_DEFAULT:
r = get_default_value(opt, val);
break;
case ENABLE_CONFIG:
case TOGGLE_CONFIG:
case DISABLE_CONFIG:
r = toggle_configs(opt, cmd, *val, true);
break;
default:
r = is_enabled(opt);
}
return r;
}