nft: autocomplete for libreadline
RFC PATCH v2 1/1 nft: autocomplete for libreadline
The lack for autocomplete in interactive mode is a bit annoying, and there doesn't seem to be an easy way to support it using bash completion. Here is a proposal to use bison to guess the next tokens.
Add support for autocomplete for libreadline CLI.
This patch uses the bison push parser to step through each token and list all possible next tokens.
- First word autocomplete doesn't work, since the last token flex passes is TOKEN_EOF, and the parser is default state
- Number of special tokens are specifically skipped, and only symbols that are escaped by \" or \' are considered valid keywords
Changes since v1:
- Fix libtool version as suggested by Jan
- Add YYSYMBOL_NUM to special token list
--- Make_global.am | 2 +- include/nftables/libnftables.h | 2 + include/parser.h | 3 + src/cli.c | 5 +- src/libnftables.c | 13 ++++ src/libnftables.map | 4 ++ src/parser_bison.y | 108 +++++++++++++++++++++++++++++++++ 7 files changed, 135 insertions(+), 2 deletions(-)
Make_global.am +1 -1
@@ -18,4 +18,4 @@ # set age to 0. # </snippet> # -libnftables_LIBVERSION=2:0:1 +libnftables_LIBVERSION=3:0:2
include/nftables/libnftables.h +2 -0
@@ -92,6 +92,8 @@ void nft_ctx_clear_vars(struct nft_ctx *ctx);int nft_run_cmd_from_buffer (struct nft_ctx *nft,const char *buf);int nft_run_cmd_from_filename (struct nft_ctx *nft,const char *filename); +char **nft_get_expected_tokens (struct nft_ctx *nft,const char *line,const char *text); + #ifdef __cplusplus }/* extern "C" */ #endif
include/parser.h +3 -0
@@ -98,6 +98,9 @@ extern void parser_init(struct nft_ctx *nft, struct parser_state *state, struct scope *top_scope);extern int nft_parse (struct nft_ctx *ctx,void *,struct parser_state *state); +extern char **expected_matches (struct nft_ctx *nft,struct parser_state *state, +const char *text); +extern void *scanner_init (struct parser_state *state);extern void scanner_destroy (struct nft_ctx *nft);
src/cli.c +4 -1
@@ -158,7 +158,10 @@ static void cli_complete(char *line)static char **cli_completion (const char *text,int start,int end) { -return NULL ; +char *line =strndup (rl_line_buffer, (size_t )start); + + rl_attempted_completion_over =1 ; +return nft_get_expected_tokens (cli_nft, line, text); }int cli_init (struct nft_ctx *nft)
src/libnftables.c +13 -0
@@ -555,6 +555,19 @@ static int nft_evaluate(struct nft_ctx *nft, struct list_head *msgs,return 0 ; } +EXPORT_SYMBOL (nft_get_expected_tokens); +char **nft_get_expected_tokens (struct nft_ctx *nft,const char *line,const char *text) +{ +LIST_HEAD (msgs); +LIST_HEAD (cmds); + +parser_init (nft, nft->state, &msgs, &cmds, nft->top_scope); + nft->scanner =scanner_init (nft->state); +scanner_push_buffer (nft->scanner, &indesc_cmdline, line); + +return expected_matches (nft, nft->state, text); +} +EXPORT_SYMBOL (nft_run_cmd_from_buffer);int nft_run_cmd_from_buffer (struct nft_ctx *nft,const char *buf) {
src/libnftables.map +4 -0
@@ -33,3 +33,7 @@ LIBNFTABLES_3 { nft_ctx_set_optimize; nft_ctx_get_optimize; } LIBNFTABLES_2; + +LIBNFTABLES_4 { + nft_get_expected_tokens; +} LIBNFTABLES_3;
src/parser_bison.y +108 -0
123 lines not on this page · raw message
Signature · 1 line
2.34.1