netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH nft 0/2] libedit support followup
@ 2021-01-06 13:30 Pablo Neira Ayuso
  2021-01-06 13:30 ` [PATCH nft 1/2] cli: use plain readline() interface with libedit Pablo Neira Ayuso
  2021-01-06 13:30 ` [PATCH nft 2/2] main: fix typo in cli definition Pablo Neira Ayuso
  0 siblings, 2 replies; 3+ messages in thread
From: Pablo Neira Ayuso @ 2021-01-06 13:30 UTC (permalink / raw)
  To: netfilter-devel

Hi,

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

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

 src/cli.c  | 39 +++++++++++++++++++++++++++++++++++----
 src/main.c |  2 +-
 2 files changed, 36 insertions(+), 5 deletions(-)

-- 
2.20.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH nft 1/2] cli: use plain readline() interface with libedit
  2021-01-06 13:30 [PATCH nft 0/2] libedit support followup Pablo Neira Ayuso
@ 2021-01-06 13:30 ` Pablo Neira Ayuso
  2021-01-06 13:30 ` [PATCH nft 2/2] main: fix typo in cli definition Pablo Neira Ayuso
  1 sibling, 0 replies; 3+ messages in thread
From: Pablo Neira Ayuso @ 2021-01-06 13:30 UTC (permalink / raw)
  To: netfilter-devel

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@netfilter.org>
---
 src/cli.c | 39 +++++++++++++++++++++++++++++++++++----
 1 file changed, 35 insertions(+), 4 deletions(-)

diff --git a/src/cli.c b/src/cli.c
index 45811595fc77..4845e5cf1454 100644
--- a/src/cli.c
+++ b/src/cli.c
@@ -154,6 +154,16 @@ static void cli_complete(char *line)
 	free(line);
 }
 
+void cli_exit(void)
+{
+	rl_callback_handler_remove();
+	rl_deprep_terminal();
+	write_history(histfile);
+}
+#endif
+
+#if defined(HAVE_LIBREADLINE)
+
 static char **cli_completion(const char *text, int start, int end)
 {
 	return NULL;
@@ -179,11 +189,32 @@ int cli_init(struct nft_ctx *nft)
 	return 0;
 }
 
-void cli_exit(void)
+#elif defined(HAVE_LIBEDIT)
+
+int cli_init(struct nft_ctx *nft)
 {
-	rl_callback_handler_remove();
-	rl_deprep_terminal();
-	write_history(histfile);
+	char *line;
+
+	cli_nft = nft;
+	rl_readline_name = (char *)"nft";
+	rl_instream  = stdin;
+	rl_outstream = stdout;
+
+	init_histfile();
+
+	read_history(histfile);
+	history_set_pos(history_length);
+
+	rl_set_prompt(CMDLINE_PROMPT);
+	while ((line = readline(rl_prompt)) != NULL) {
+		line = cli_append_multiline(line);
+		if (!line)
+			continue;
+
+		cli_complete(line);
+	}
+
+	return 0;
 }
 
 #else /* HAVE_LINENOISE */
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH nft 2/2] main: fix typo in cli definition
  2021-01-06 13:30 [PATCH nft 0/2] libedit support followup Pablo Neira Ayuso
  2021-01-06 13:30 ` [PATCH nft 1/2] cli: use plain readline() interface with libedit Pablo Neira Ayuso
@ 2021-01-06 13:30 ` Pablo Neira Ayuso
  1 sibling, 0 replies; 3+ messages in thread
From: Pablo Neira Ayuso @ 2021-01-06 13:30 UTC (permalink / raw)
  To: netfilter-devel

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

Fixes: 9420423900a2 ("cli: add libedit support")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
 src/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/main.c b/src/main.c
index 0a1c47758d1a..80cf1acf0f7f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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";
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-01-06 13:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-06 13:30 [PATCH nft 0/2] libedit support followup Pablo Neira Ayuso
2021-01-06 13:30 ` [PATCH nft 1/2] cli: use plain readline() interface with libedit Pablo Neira Ayuso
2021-01-06 13:30 ` [PATCH nft 2/2] main: fix typo in cli definition Pablo Neira Ayuso

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).