nft: autocomplete for libreadline
RFC PATCH 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.
This patch adds support for keyword autocomplete for libreadline CLI only, it uses the bison push parser to step through each token and lists all all possible next tokens.
Some important notes:
- 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
--- 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 | 107 +++++++++++++++++++++++++++++++++ 7 files changed, 134 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:1
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 +107 -0
122 lines not on this page · raw message
Signature · 1 line
2.34.1
Make_global.am line 21
Quoted 2 lines · Sriram Yagnaraman
Make_global.am +1
-1-libnftables_LIBVERSION=2:0:1 +libnftables_LIBVERSION=3:0:1
^ This looks very much incorrect.
Quoted 7 lines
include/nftables/libnftables.h +1
-0+char **nft_get_expected_tokens (struct nft_ctx *nft,const char *line,const char *text);include/parser.h +2
-0+extern char **expected_matches (struct nft_ctx *nft,struct parser_state *state, +const char *text);
Quoted 10 lines
-----Original Message-----
From: Jan Engelhardt <[email protected]>
Sent: Thursday, 30 March 2023 13:51
To: Sriram Yagnaraman <[email protected]>
Cc: [email protected]; Phil Sutter <[email protected]>; Pablo Neira Ayuso <[email protected]>; Florian Westphal <[email protected]>
Subject: Re: [RFC PATCH] nft: autocomplete for libreadline
Quoted 6 lines
Make_global.am line 21
Quoted 2 lines · Sriram Yagnaraman
Make_global.am +1
-1-libnftables_LIBVERSION=2:0:1 +libnftables_LIBVERSION=3:0:1^ This looks very much incorrect.
I confess that I have no idea how this versioning works. I was unable to add the new libnftables API without changing this number. Please advise what I should use.
Quoted 10 lines
include/nftables/libnftables.h
Quoted 4 lines
include/nftables/libnftables.h +1
-0+char **nft_get_expected_tokens(struct nft_ctx *nft, const char *line, constchar *text);
include/parser.h
Quoted 3 lines
include/parser.h +1
-0+extern char **expected_matches (struct nft_ctx *nft, struct parser_state*state,
Quoted 1 line
include/parser.h +1
-0+ const char *text);
Quoted 12 lines · Sriram Yagnaraman
Quoted 12 lines
-----Original Message-----
From: Jan Engelhardt <[email protected]>
Sent: Thursday, 30 March 2023 13:51
To: Sriram Yagnaraman <[email protected]>
Cc: [email protected]; Phil Sutter <[email protected]>; Pablo Neira Ayuso <[email protected]>; Florian Westphal <[email protected]>
Subject: Re: [RFC PATCH] nft: autocomplete for libreadlineOn Thursday 2023-03-30 13:25, Sriram Yagnaraman wrote:
Quoted 7 lines
Quoted 4 lines
Make_global.am line 21
Quoted 2 lines
Make_global.am +1
-1-libnftables_LIBVERSION=2:0:1 +libnftables_LIBVERSION=3:0:1^ This looks very much incorrect.
I confess that I have no idea how this versioning works. I was unable to add the new libnftables API without changing this number. Please advise what I should use.
From the modifications to the .h files, 3:0:2 seems in order.
https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html