From 7dbbdc150292fa9076ce8e3b2c6a8937214d03e0 Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Fri, 30 Nov 2018 18:04:10 +0100 Subject: src: provide suggestion for misspelled object name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use this from the lookup path, to check for misspellings: # nft add table filter # nft add chain filtre test Error: No such file or directory; did you mean table ‘filter’ in family ip? add chain filtre test ^^^^^^ Signed-off-by: Pablo Neira Ayuso --- src/rule.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'src/rule.c') diff --git a/src/rule.c b/src/rule.c index 1fffa39a..c244d0ba 100644 --- a/src/rule.c +++ b/src/rule.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -354,18 +355,24 @@ struct set *set_lookup_fuzzy(const char *set_name, const struct nft_cache *cache, const struct table **t) { + struct string_misspell_state st; struct table *table; struct set *set; + string_misspell_init(&st); + list_for_each_entry(table, &cache->list, list) { list_for_each_entry(set, &table->sets, list) { if (!strcmp(set->handle.set.name, set_name)) { *t = table; return set; } + if (string_misspell_update(set->handle.set.name, + set_name, set, &st)) + *t = table; } } - return NULL; + return st.obj; } struct set *set_lookup_global(uint32_t family, const char *table, @@ -784,18 +791,24 @@ struct chain *chain_lookup_fuzzy(const struct handle *h, const struct nft_cache *cache, const struct table **t) { + struct string_misspell_state st; struct table *table; struct chain *chain; + string_misspell_init(&st); + list_for_each_entry(table, &cache->list, list) { list_for_each_entry(chain, &table->chains, list) { if (!strcmp(chain->handle.chain.name, h->chain.name)) { *t = table; return chain; } + if (string_misspell_update(chain->handle.chain.name, + h->chain.name, chain, &st)) + *t = table; } } - return NULL; + return st.obj; } const char *family2str(unsigned int family) @@ -1142,13 +1155,19 @@ struct table *table_lookup(const struct handle *h, struct table *table_lookup_fuzzy(const struct handle *h, const struct nft_cache *cache) { + struct string_misspell_state st; struct table *table; + string_misspell_init(&st); + list_for_each_entry(table, &cache->list, list) { if (!strcmp(table->handle.table.name, h->table.name)) return table; + + string_misspell_update(table->handle.table.name, + h->table.name, table, &st); } - return NULL; + return st.obj; } const char *table_flags_name[TABLE_FLAGS_MAX] = { -- cgit v1.2.3