Use the already existing SCANSTATE_TYPE for keyword scoping. This is a bit of back-n-forth from string to token and back to string but it eliminates the helper function and also takes care of error handling. Note that JSON parser does not validate the type string at all but relies upon the kernel to reject wrong ones. Signed-off-by: Phil Sutter --- include/rule.h | 1 - src/parser_bison.y | 28 ++++++++++++++-------------- src/rule.c | 19 ------------------- src/scanner.l | 6 ++++++ 4 files changed, 20 insertions(+), 34 deletions(-) diff --git a/include/rule.h b/include/rule.h index 4c647f732caf2..c9a683de3880e 100644 --- a/include/rule.h +++ b/include/rule.h @@ -260,7 +260,6 @@ struct chain { #define STD_PRIO_BUFSIZE 100 extern int std_prio_lookup(const char *std_prio_name, int family, int hook); -extern const char *chain_type_name_lookup(const char *name); extern const char *chain_hookname_lookup(const char *name); extern struct chain *chain_alloc(void); extern struct chain *chain_get(struct chain *chain); diff --git a/src/parser_bison.y b/src/parser_bison.y index 96d0e151b1586..767d5d7063c26 100644 --- a/src/parser_bison.y +++ b/src/parser_bison.y @@ -715,6 +715,10 @@ int nft_lex(void *, void *, void *); %token XT "xt" +%token FILTER "filter" +%token NAT "nat" +%token ROUTE "route" + %type limit_rate_pkts %type limit_rate_bytes @@ -1034,6 +1038,9 @@ int nft_lex(void *, void *, void *); %type set_elem_key_expr %destructor { expr_free($$); } set_elem_key_expr +%type chain_type +%destructor { free_const($$); } chain_type + %% input : /* empty */ @@ -2736,22 +2743,10 @@ type_identifier : STRING { $$ = $1; } | CLASSID { $$ = xstrdup("classid"); } ; -hook_spec : TYPE close_scope_type STRING HOOK STRING dev_spec prio_spec +hook_spec : TYPE chain_type close_scope_type HOOK STRING dev_spec prio_spec { - const char *chain_type = chain_type_name_lookup($3); - - if (chain_type == NULL) { - erec_queue(error(&@3, "unknown chain type"), - state->msgs); - free_const($3); - free_const($5); - expr_free($6); - expr_free($7.expr); - YYERROR; - } $0->type.loc = @3; - $0->type.str = xstrdup(chain_type); - free_const($3); + $0->type.str = xstrdup($2); $0->loc = @$; $0->hook.loc = @5; @@ -2772,6 +2767,11 @@ hook_spec : TYPE close_scope_type STRING HOOK STRING dev_spec prio_spec } ; +chain_type : FILTER { $$ = xstrdup("filter"); } + | NAT { $$ = xstrdup("nat"); } + | ROUTE { $$ = xstrdup("route"); } + ; + prio_spec : PRIORITY extended_prio_spec { $$ = $2; diff --git a/src/rule.c b/src/rule.c index a915c395acffb..d7756d2bd73dd 100644 --- a/src/rule.c +++ b/src/rule.c @@ -676,25 +676,6 @@ struct symbol *symbol_lookup_fuzzy(const struct scope *scope, return st.obj; } -static const char * const chain_type_str_array[] = { - "filter", - "nat", - "route", - NULL, -}; - -const char *chain_type_name_lookup(const char *name) -{ - int i; - - for (i = 0; chain_type_str_array[i]; i++) { - if (!strcmp(name, chain_type_str_array[i])) - return chain_type_str_array[i]; - } - - return NULL; -} - static const char * const chain_hookname_str_array[] = { "prerouting", "input", diff --git a/src/scanner.l b/src/scanner.l index 99ace05773816..b397a147ef9bd 100644 --- a/src/scanner.l +++ b/src/scanner.l @@ -858,6 +858,12 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "out" { return OUT; } } +{ + "filter" { return FILTER; } + "nat" { return NAT; } + "route" { return ROUTE; } +} + "secmark" { scanner_push_start_cond(yyscanner, SCANSTATE_SECMARK); return SECMARK; } "xt" { scanner_push_start_cond(yyscanner, SCANSTATE_XT); return XT; } -- 2.51.0