diff options
author | Pablo Neira Ayuso <pablo@netfilter.org> | 2018-11-30 18:04:10 +0100 |
---|---|---|
committer | Pablo Neira Ayuso <pablo@netfilter.org> | 2018-11-30 19:10:38 +0100 |
commit | 7dbbdc150292fa9076ce8e3b2c6a8937214d03e0 (patch) | |
tree | 43b41cc835eef37a216239146c856de29d1a77e3 /src/rule.c | |
parent | fc931169d9cec785aea65b3146c80afdb994798a (diff) |
src: provide suggestion for misspelled object name
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 <pablo@netfilter.org>
Diffstat (limited to 'src/rule.c')
-rw-r--r-- | src/rule.c | 25 |
1 files changed, 22 insertions, 3 deletions
@@ -22,6 +22,7 @@ #include <netdb.h> #include <netlink.h> #include <mnl.h> +#include <misspell.h> #include <json.h> #include <libnftnl/common.h> @@ -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] = { |