Skip to contentSkip to conversation

follow up work on the libedit support

PATCH New Pablo Neira Ayuso 1 participant 3 messages Jan 6, 2021 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

cover follow up work on the libedit support

Hi,

This is a follow up to address a few issues with libedit readline
support: tests/shell now works fine.

Pablo Neira Ayuso (2):
cli: use plain readline() interface
main: fix typo in cli definition

 src/cli.c  | 21 +++++++++++----------
 src/main.c |  2 +-
 2 files changed, 12 insertions(+), 11 deletions(-)
Signature · 1 line
2.20.1
author maintainer
#1 lore raw

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

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

---
 src/cli.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
src/cli.c +11 -10
index 45811595fc77..f46971b848e5 100644
@@ -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;
 }
 
Signature · 1 line
2.20.1
author maintainer
#2 lore raw

PATCH 2/2 main: fix typo in cli definition

In reply to Pablo Neira Ayuso

9420423900a2 ("cli: add libedit support") updated HAVE_LIBREADLINE to HAVE_READLINE by mistake.

Fixes 9420423900a2 ("cli: add libedit support")
---
 src/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/main.c +1 -1
index 0a1c47758d1a..80cf1acf0f7f 100644
@@ -227,7 +227,7 @@ static void show_version(void)
 {
 	const char *cli, *minigmp, *json, *xt;
 
-#if defined(HAVE_READLINE)
+#if defined(HAVE_LIBREADLINE)
 	cli = "readline";
 #elif defined(HAVE_LIBEDIT)
 	cli = "editline";
Signature · 1 line
2.20.1