Skip to content

Commit 64c709d

Browse files
committed
cli: parser: simplify rule options parsing
bison warns about useless rules due to conflicts, those rules are used to parse filtering rule options (mark, counter, and log). Remove the useless rules warning by moving the mark/counter/log rules logic directly into the rule_options rule, which simplify the parser.
1 parent 99bde91 commit 64c709d

1 file changed

Lines changed: 35 additions & 54 deletions

File tree

src/bfcli/parser.y

Lines changed: 35 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,6 @@
103103
%token <sval> RAW_PAYLOAD
104104

105105
// Grammar types
106-
%type <u8> log
107-
%type <bval> counter
108-
%type <u32> mark
109106
%destructor { freep(&$$); } <sval>
110107

111108
%type <hook> hook
@@ -287,25 +284,53 @@ rule : RULE matchers rule_options verdict
287284
}
288285
;
289286

290-
rule_option : %empty { $$ = (struct bf_rule_options){}; }
291-
| log
287+
rule_option : LOG LOG_HEADERS
292288
{
289+
_cleanup_free_ char *in = $2;
290+
char *tmp = in;
291+
char *saveptr;
292+
char *token;
293+
uint8_t log;
294+
295+
while ((token = strtok_r(tmp, ",", &saveptr))) {
296+
enum bf_pkthdr header;
297+
298+
if (bf_pkthdr_from_str(token, &header) < 0)
299+
bf_parse_err("unknown packet header '%s'", token);
300+
301+
log |= BF_FLAG(header);
302+
303+
tmp = NULL;
304+
}
305+
293306
$$ = (struct bf_rule_options){
294-
.log = $1,
307+
.log = log,
295308
.flags = BF_RULE_OPTION_LOG,
296309
};
297310
}
298-
| counter
311+
| COUNTER
299312
{
300313
$$ = (struct bf_rule_options){
301-
.counter = $1,
314+
.counter = true,
302315
.flags = BF_RULE_OPTION_COUNTER,
303316
};
304317
}
305-
| mark
318+
| MARK STRING
306319
{
320+
_cleanup_free_ const char *raw_mark = $2;
321+
long long mark;
322+
char *endptr;
323+
324+
mark = strtoll(raw_mark, &endptr, 0);
325+
if (*endptr)
326+
bf_parse_err("mark value '%s' can't be parsed as a positive integer", raw_mark);
327+
if (mark < 0)
328+
bf_parse_err("mark should be positive, not '%s'", raw_mark);
329+
if (mark > UINT32_MAX)
330+
bf_parse_err("mark should be at most 0x%x", UINT32_MAX);
331+
307332
$$ = (struct bf_rule_options){
308-
.mark = $1,
333+
.mark = (uint32_t)mark,
309334
.flags = BF_RULE_OPTION_MARK,
310335
};
311336
}
@@ -463,48 +488,4 @@ matcher_op : %empty { $$ = BF_MATCHER_EQ; }
463488
}
464489
;
465490

466-
log : %empty { $$ = 0; }
467-
| LOG LOG_HEADERS
468-
{
469-
_cleanup_free_ char *in = $2;
470-
char *tmp = in;
471-
char *saveptr;
472-
char *token;
473-
474-
$$ = 0;
475-
476-
while ((token = strtok_r(tmp, ",", &saveptr))) {
477-
enum bf_pkthdr header;
478-
479-
if (bf_pkthdr_from_str(token, &header) < 0)
480-
bf_parse_err("unknown packet header '%s'", token);
481-
482-
$$ |= BF_FLAG(header);
483-
484-
tmp = NULL;
485-
}
486-
}
487-
;
488-
489-
counter : %empty { $$ = false; }
490-
| COUNTER { $$ = true; }
491-
;
492-
493-
mark : MARK STRING
494-
{
495-
_cleanup_free_ const char *raw_mark = $2;
496-
long long mark;
497-
char *endptr;
498-
499-
mark = strtoll(raw_mark, &endptr, 0);
500-
if (*endptr)
501-
bf_parse_err("mark value '%s' can't be parsed as a positive integer", raw_mark);
502-
if (mark < 0)
503-
bf_parse_err("mark should be positive, not '%s'", raw_mark);
504-
if (mark > UINT32_MAX)
505-
bf_parse_err("mark should be at most 0x%x", UINT32_MAX);
506-
507-
$$ = (uint32_t)mark;
508-
}
509-
510491
%%

0 commit comments

Comments
 (0)