[PATCH nft 1/2] cli: use plain readline() interface

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Instead of the alternate interface [1].

I spent a bit of time debugging an issue with libedit support
9420423900a2 ("cli: add libedit support") that broke tests/shell.

This is the reproducer:

 # nft -i << EOF
 list ruleset
 EOF

which makes rl_callback_read_char() loop forever on read() as shown by
strace. The rl_line_buffer variable does not accumulate the typed
characters as it should when redirecting the standard input for some
reason.

Given our interactive interface is fairly simple at this stage, switch
to use the readline() interface instead of rl_callback_read_char().

[1] https://docs.freebsd.org/info/readline/readline.info.Alternate_Interface.html

Signed-off-by: Pablo Neira Ayuso <pablo@xxxxxxxxxxxxx>
---
 src/cli.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/src/cli.c b/src/cli.c
index 45811595fc77..f46971b848e5 100644
--- a/src/cli.c
+++ b/src/cli.c
@@ -154,28 +154,29 @@ static void cli_complete(char *line)
 	free(line);
 }
 
-static char **cli_completion(const char *text, int start, int end)
-{
-	return NULL;
-}
-
 int cli_init(struct nft_ctx *nft)
 {
+	char *line;
+
 	cli_nft = nft;
 	rl_readline_name = (char *)"nft";
 	rl_instream  = stdin;
 	rl_outstream = stdout;
 
-	rl_callback_handler_install(CMDLINE_PROMPT, cli_complete);
-	rl_attempted_completion_function = cli_completion;
-
 	init_histfile();
 
 	read_history(histfile);
 	history_set_pos(history_length);
 
-	while (true)
-		rl_callback_read_char();
+	rl_set_prompt(CMDLINE_PROMPT);
+	while ((line = readline(rl_prompt)) != NULL) {
+		line = cli_append_multiline(line);
+		if (!line)
+			continue;
+
+		cli_complete(line);
+	}
+
 	return 0;
 }
 
-- 
2.20.1





[Index of Archives]     [Netfitler Users]     [Berkeley Packet Filter]     [LARTC]     [Bugtraq]     [Yosemite Forum]

  Powered by Linux