]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
src: introduce simple hints on incorrect identifier
authorPablo Neira Ayuso <pablo@netfilter.org>
Sat, 1 Dec 2018 11:06:46 +0000 (12:06 +0100)
committerPablo Neira Ayuso <pablo@netfilter.org>
Sat, 1 Dec 2018 11:09:24 +0000 (12:09 +0100)
 # cat test.nft
 define test = "1.2.3.4"

 table ip x {
        chain y {
                ip saddr $text
        }
 }
 # nft -f test.nft
 test.nft:5:13-16: Error: unknown identifier 'text'; did you mean identifier ‘test’?
                 ip saddr $text
                           ^^^^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/rule.h
src/parser_bison.y
src/rule.c

index 88fed62e7cba329d0b040b24383b72f50e8319f0..dc5e5b87f93311e59d7c953b98e8776bac66e176 100644 (file)
@@ -112,6 +112,8 @@ extern void symbol_bind(struct scope *scope, const char *identifier,
 extern int symbol_unbind(const struct scope *scope, const char *identifier);
 extern struct symbol *symbol_lookup(const struct scope *scope,
                                    const char *identifier);
+struct symbol *symbol_lookup_fuzzy(const struct scope *scope,
+                                  const char *identifier);
 struct symbol *symbol_get(const struct scope *scope, const char *identifier);
 
 enum table_flags {
index dfe3068376241b8b56ba1a39d94f853e02fd6ea7..e73e1ecd080506310fff14ddf3d1305202df4836 100644 (file)
@@ -3078,8 +3078,16 @@ variable_expr            :       '$'     identifier
 
                                sym = symbol_get(scope, $2);
                                if (!sym) {
-                                       erec_queue(error(&@2, "unknown identifier '%s'", $2),
-                                                  state->msgs);
+                                       sym = symbol_lookup_fuzzy(scope, $2);
+                                       if (sym) {
+                                               erec_queue(error(&@2, "unknown identifier '%s'; "
+                                                                     "did you mean identifier ‘%s’?",
+                                                                     $2, sym->identifier),
+                                                          state->msgs);
+                                       } else {
+                                               erec_queue(error(&@2, "unknown identifier '%s'", $2),
+                                                          state->msgs);
+                                       }
                                        xfree($2);
                                        YYERROR;
                                }
index 0a3c1970c83a459ee3cddd4ff83378d74c893ecb..ad3001294c65e0f951d8d05339c16287fc86a20f 100644 (file)
@@ -692,6 +692,24 @@ struct symbol *symbol_lookup(const struct scope *scope, const char *identifier)
        return NULL;
 }
 
+struct symbol *symbol_lookup_fuzzy(const struct scope *scope,
+                                  const char *identifier)
+{
+       struct string_misspell_state st;
+       struct symbol *sym;
+
+       string_misspell_init(&st);
+
+       while (scope != NULL) {
+               list_for_each_entry(sym, &scope->symbols, list)
+                       string_misspell_update(sym->identifier, identifier,
+                                              sym, &st);
+
+               scope = scope->parent;
+       }
+       return st.obj;
+}
+
 static const char * const chain_type_str_array[] = {
        "filter",
        "nat",