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
|
%{
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#define __USE_ISOC99
#include <limits.h>
#include "cli_val.h"
extern int yy_cli_def_lineno;
extern char *yy_cli_def_text;
static vtw_def *parse_defp;
static int parse_status;
static boolean cli_def_type_only=0; /*{ if (cli_def_type_only) YYACCEPT;}*/
/* XXX: sigh, the -p flag to yacc should do this for us kkkk*/
#define yystacksize tpltstacksize
#define yysslim tpltsslim
/* forward prototypes */
extern int yy_cli_parse_parse();
extern int yy_cli_def_lex();
extern void yy_cli_parse_error(const char *);
static void cli_deferror(const char *);
extern FILE *yy_cli_def_in;
#define YYDEBUG 1
#define yy_cli_parse_lex yy_cli_def_lex
%}
%token EOL
%token MULTI
%token TAG
%token TYPE
%token HELP
%token DEFAULT
%token PRIORITY
%token ENUMERATION
%token CHELP
%token ALLOWED
%token VHELP
%token PATTERN
%token EXEC
%token SYNTAX
%token COMMIT
%token CHECK
%token DUMMY
%left SEMI
%token <val>VALUE
%token <type>TYPE_DEF
%token <strp>VAR
%token <strp> STRING
%token <strp> EX_STRING
%token SYNTAX_ERROR
%token <action>ACTION
%left ASSIGN
%left OR
%left AND
%right NOT
%left COMMA
%nonassoc <cond>COND
%token RP
%token LP
%type <nodep> val
%type <nodep> exp
%type <val> val0
%type <nodep> action
%type <nodep> action0
%union {
char *strp;
valstruct val;
vtw_type_e type;
vtw_cond_e cond;
vtw_node *nodep;
vtw_act_type action;
}
%%
input: tag
| EOL input
| tag otherinput
;
otherinput: type EOL
| cause EOL
| otherinput type EOL
| otherinput cause EOL
| otherinput EOL
| EOL
| syntax_error
;
tag: /* empty */
| TAG EOL {parse_defp->tag = TRUE;}
| TAG VALUE {
parse_defp->tag = TRUE;
char *tmp = $2.val;
long long int cval = 0;
char *endp = NULL;
errno = 0;
cval = strtoll(tmp, &endp, 10);
if (($2.val_type != INT_TYPE)
|| (errno == ERANGE
&& (cval == LLONG_MAX || cval == LLONG_MIN))
|| (errno != 0 && cval == 0)
|| (*endp != '\0') || (cval < 0) || (cval > UINT_MAX)) {
yy_cli_parse_error((const char *)
"Tag must be <u32>\n");
} else {
parse_defp->def_tag = cval;
}
}
| MULTI EOL {parse_defp->multi = TRUE;}
| MULTI VALUE
{
parse_defp->multi = TRUE;
char *tmp = $2.val;
long long int cval = 0;
char *endp = NULL;
errno = 0;
cval = strtoll(tmp, &endp, 10);
if (($2.val_type != INT_TYPE)
|| (errno == ERANGE
&& (cval == LLONG_MAX || cval == LLONG_MIN))
|| (errno != 0 && cval == 0)
|| (*endp != '\0') || (cval < 0) || (cval > UINT_MAX)) {
yy_cli_parse_error((const char *)
"Tag must be <u32>\n");
} else {
parse_defp->def_multi = cval;
}
}
;
type: TYPE TYPE_DEF COMMA TYPE_DEF
{
parse_defp->def_type = $2;
parse_defp->def_type2 = $4;
}
;
type: TYPE TYPE_DEF SEMI STRING
{ parse_defp->def_type = $2;
parse_defp->def_type_help = $4; }
;
type: TYPE TYPE_DEF
{ parse_defp->def_type = $2;
}
;
cause: help_cause
| default_cause
| priority_stmt
| enumeration_stmt
| chelp_stmt
| allowed_stmt
| vhelp_stmt
| syntax_cause
| ACTION action { append(parse_defp->actions + $1, $2, 0);}
| dummy_stmt
;
dummy_stmt: DUMMY STRING { /* ignored */ }
;
help_cause: HELP STRING
{ parse_defp->def_node_help = $2; /* no semantics for now */
}
default_cause: DEFAULT VALUE
{
if ($2.val_type != parse_defp->def_type)
yy_cli_parse_error((const char *)"Bad default\n");
parse_defp->def_default = $2.val;
}
default_cause: DEFAULT STRING
{
if (TEXT_TYPE != parse_defp->def_type)
yy_cli_parse_error((const char *)"Bad default\n");
parse_defp->def_default = $2;
}
priority_stmt: PRIORITY VALUE
{
char *tmp = $2.val;
long long int cval = 0;
char *endp = NULL;
errno = 0;
cval = strtoll(tmp, &endp, 10);
if ((errno == ERANGE
&& (cval == LLONG_MAX || cval == LLONG_MIN))
|| (errno != 0 && cval == 0)
|| (*endp != '\0') || (cval < 0) || (cval > UINT_MAX)) {
parse_defp->def_priority_ext = tmp;
} else {
parse_defp->def_priority = cval;
}
}
enumeration_stmt: ENUMERATION STRING
{
parse_defp->def_enumeration = $2;
}
chelp_stmt: CHELP STRING
{
parse_defp->def_comp_help = $2;
}
allowed_stmt: ALLOWED STRING
{
parse_defp->def_allowed = $2;
}
vhelp_stmt: VHELP STRING
{
if (!(parse_defp->def_val_help)) {
/* first string */
parse_defp->def_val_help = $2;
} else {
/* subsequent strings */
char *optr = parse_defp->def_val_help;
int olen = strlen(parse_defp->def_val_help);
char *nptr = $2;
int nlen = strlen(nptr);
int len = olen + 1 /* "\n" */ + nlen + 1 /* 0 */;
char *mptr = (char *) malloc(len);
memcpy(mptr, optr, olen);
mptr[olen] = '\n';
memcpy(&(mptr[olen + 1]), nptr, nlen);
mptr[len - 1] = 0;
parse_defp->def_val_help = mptr;
free(optr);
free(nptr);
}
/* result is a '\n'-delimited string for val_help */
}
syntax_cause: SYNTAX exp {append(parse_defp->actions + syntax_act, $2, 0);}
;
syntax_cause: COMMIT exp {append(parse_defp->actions + syntax_act, $2, 1);}
;
action0: STRING { $$ = make_node(EXEC_OP, make_str_node($1),NULL);}
;
action: action0
| action0 SEMI STRING
{$$ = make_node(HELP_OP, $1, make_str_node($3));}
| exp
;
exp: LP exp RP
{$$=$2;}
| exp AND exp {$$ = make_node(AND_OP,$1,$3);}
| exp OR exp {$$ = make_node(OR_OP,$1,$3);}
| val COND val
{$$ = make_node(COND_OP,$1,$3);$$->vtw_node_aux = $2;}
| PATTERN VAR STRING
{ $$ = make_node(PATTERN_OP,make_var_node($2),
make_str_node($3));}
| EXEC STRING
{ $$ = make_node(EXEC_OP,make_str_node($2),NULL);}
| NOT exp {$$ = make_node(NOT_OP,$2,NULL);}
| exp SEMI STRING
{$$ = make_node(HELP_OP, $1, make_str_node($3));}
| VAR ASSIGN val
{$$ = make_node(ASSIGN_OP, make_var_node($1), $3);}
;
val: VAR {$$ = make_var_node($1);}
| val0 {$$ = make_val_node(&($1));}
| EX_STRING {$$=make_str_node0($1, B_QUOTE_OP);}
;
val0: VALUE
| val0 COMMA val0 { add_val(&($1), &($3)); $$=$1; }
| STRING {$$ = str2val($1);}
;
syntax_error: SYNTAX_ERROR {
cli_deferror("syntax error");
}
;
%%
const char *parse_path;
int parse_def(vtw_def *defp, const char *path, boolean type_only)
{
int status;
/* always zero vtw_def struct */
memset(defp, 0, sizeof(vtw_def));
yy_cli_def_lineno = 1;
parse_status = 0;
parse_defp = defp;
cli_def_type_only = type_only;
yy_cli_def_in = fopen(path, "r");
#if 0
yy_cli_parse_debug = 1;
#endif
if (!yy_cli_def_in)
return -5;
parse_path = path;
status = yy_cli_parse_parse(); /* 0 is OK */
fclose(yy_cli_def_in);
return status;
}
static void
cli_deferror(const char *s)
{
printf("Error: %s in file [%s], line %d, last token [%s]\n",s, parse_path,
yy_cli_def_lineno, yy_cli_def_text);
}
void yy_cli_parse_error(const char *s)
{
cli_deferror(s);
}
|