netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH nftables v2 0/2] Add Linenoise support to the CLI.
@ 2019-09-24  7:40 Jeremy Sowden
  2019-09-24  7:40 ` [PATCH nftables v2 1/2] cli: add linenoise CLI implementation Jeremy Sowden
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Jeremy Sowden @ 2019-09-24  7:40 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: Netfilter Devel, Sebastian Priebe

Sebastian Priebe [0] requested Linenoise support for the CLI as an
alternative to Readline, so I thought I'd have a go at providing it.
Linenoise is a minimal, zero-config, BSD licensed, Readline replacement
used in Redis, MongoDB, and Android [1].

 0 - https://lore.kernel.org/netfilter-devel/4df20614cd10434b9f91080d0862dd0c@de.sii.group/
 1 - https://github.com/antirez/linenoise/

By default, the CLI continues to be build using Readline, but passing
`--with-cli=linenoise` instead causes Linenoise to be used instead.

`nft -v` has been extended to display what CLI implementation was built
and whether mini-gmp was used.

Changes since v1:

 * dropped the linenoise source from the nftables tree and added an
   `AC_CHECK_LIB` check instead.

Changes since RFC:

 * two tidy-up patches dropped because they have already been applied;
 * source now added to include/ and src/;
 * tests/build/run-tests.sh updated to test `--with-cli=linenoise`;
 * `nft -v` extended.

Jeremy Sowden (2):
  cli: add linenoise CLI implementation.
  main: add more information to `nft -v`.

 configure.ac             | 17 ++++++++---
 include/cli.h            |  2 +-
 src/Makefile.am          |  3 ++
 src/cli.c                | 64 ++++++++++++++++++++++++++++++++++------
 src/main.c               | 28 ++++++++++++++++--
 tests/build/run-tests.sh |  4 +--
 6 files changed, 100 insertions(+), 18 deletions(-)

-- 
2.23.0


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

* [PATCH nftables v2 1/2] cli: add linenoise CLI implementation.
  2019-09-24  7:40 [PATCH nftables v2 0/2] Add Linenoise support to the CLI Jeremy Sowden
@ 2019-09-24  7:40 ` Jeremy Sowden
  2019-10-15  8:32   ` Pablo Neira Ayuso
  2019-09-24  7:40 ` [PATCH nftables v2 2/2] main: add more information to `nft -v` Jeremy Sowden
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 9+ messages in thread
From: Jeremy Sowden @ 2019-09-24  7:40 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: Netfilter Devel, Sebastian Priebe

By default, continue to use libreadline, but if `--with-cli=linenoise`
is passed to configure, build the linenoise implementation instead.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
---
 configure.ac             | 17 ++++++++---
 include/cli.h            |  2 +-
 src/cli.c                | 64 ++++++++++++++++++++++++++++++++++------
 tests/build/run-tests.sh |  4 +--
 4 files changed, 71 insertions(+), 16 deletions(-)

diff --git a/configure.ac b/configure.ac
index 68f97f090535..73654b005cd2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -68,12 +68,21 @@ AC_CHECK_LIB([gmp],[__gmpz_init], , AC_MSG_ERROR([No suitable version of libgmp
 AM_CONDITIONAL([BUILD_MINIGMP], [test "x$with_mini_gmp" = xyes])
 
 AC_ARG_WITH([cli], [AS_HELP_STRING([--without-cli],
-            [disable interactive CLI (libreadline support)])],
-            [], [with_cli=yes])
-AS_IF([test "x$with_cli" != xno], [
+            [disable interactive CLI (libreadline or linenoise support)])],
+            [], [with_cli=readline])
+
+AS_IF([test "x$with_cli" = xreadline], [
 AC_CHECK_LIB([readline], [readline], ,
-	     AC_MSG_ERROR([No suitable version of libreadline found]))
+        AC_MSG_ERROR([No suitable version of libreadline found]))
 AC_DEFINE([HAVE_LIBREADLINE], [1], [])
+],
+      [test "x$with_cli" = xlinenoise], [
+AC_CHECK_LIB([linenoise], [linenoise], ,
+        AC_MSG_ERROR([No suitable version of linenoise found]))
+AC_DEFINE([HAVE_LIBLINENOISE], [1], [])
+],
+      [test "x$with_cli" != xno], [
+AC_MSG_ERROR([unexpected CLI value: $with_cli])
 ])
 AM_CONDITIONAL([BUILD_CLI], [test "x$with_cli" != xno])
 
diff --git a/include/cli.h b/include/cli.h
index 023f004b8dab..d82517750abc 100644
--- a/include/cli.h
+++ b/include/cli.h
@@ -4,7 +4,7 @@
 #include <nftables/libnftables.h>
 #include <config.h>
 
-#ifdef HAVE_LIBREADLINE
+#if defined(HAVE_LIBREADLINE) || defined(HAVE_LIBLINENOISE)
 extern int cli_init(struct nft_ctx *nft);
 #else
 static inline int cli_init(struct nft_ctx *nft)
diff --git a/src/cli.c b/src/cli.c
index f1e89926f2bc..4c0c3e9d67c6 100644
--- a/src/cli.c
+++ b/src/cli.c
@@ -21,16 +21,36 @@
 #include <string.h>
 #include <ctype.h>
 #include <limits.h>
+#ifdef HAVE_LIBREADLINE
 #include <readline/readline.h>
 #include <readline/history.h>
+#else
+#include <linenoise.h>
+#endif
 
 #include <cli.h>
 #include <list.h>
 
 #define CMDLINE_HISTFILE	".nft.history"
+#define CMDLINE_PROMPT		"nft> "
+#define CMDLINE_QUIT		"quit"
 
-static struct nft_ctx *cli_nft;
 static char histfile[PATH_MAX];
+
+static void
+init_histfile(void)
+{
+	const char *home;
+
+	home = getenv("HOME");
+	if (home == NULL)
+		home = "";
+	snprintf(histfile, sizeof(histfile), "%s/%s", home, CMDLINE_HISTFILE);
+}
+
+#ifdef HAVE_LIBREADLINE
+
+static struct nft_ctx *cli_nft;
 static char *multiline;
 
 static char *cli_append_multiline(char *line)
@@ -100,7 +120,7 @@ static void cli_complete(char *line)
 	if (*c == '\0')
 		return;
 
-	if (!strcmp(line, "quit")) {
+	if (!strcmp(line, CMDLINE_QUIT)) {
 		cli_exit();
 		exit(0);
 	}
@@ -121,20 +141,15 @@ static char **cli_completion(const char *text, int start, int end)
 
 int cli_init(struct nft_ctx *nft)
 {
-	const char *home;
-
 	cli_nft = nft;
 	rl_readline_name = "nft";
 	rl_instream  = stdin;
 	rl_outstream = stdout;
 
-	rl_callback_handler_install("nft> ", cli_complete);
+	rl_callback_handler_install(CMDLINE_PROMPT, cli_complete);
 	rl_attempted_completion_function = cli_completion;
 
-	home = getenv("HOME");
-	if (home == NULL)
-		home = "";
-	snprintf(histfile, sizeof(histfile), "%s/%s", home, CMDLINE_HISTFILE);
+	init_histfile();
 
 	read_history(histfile);
 	history_set_pos(history_length);
@@ -150,3 +165,34 @@ void cli_exit(void)
 	rl_deprep_terminal();
 	write_history(histfile);
 }
+
+#else /* !HAVE_LIBREADLINE */
+
+int cli_init(struct nft_ctx *nft)
+{
+	int quit = 0;
+	char *line;
+
+	init_histfile();
+	linenoiseHistoryLoad(histfile);
+	linenoiseSetMultiLine(1);
+
+	while (!quit && (line = linenoise(CMDLINE_PROMPT)) != NULL) {
+		if (strcmp(line, CMDLINE_QUIT) == 0) {
+			quit = 1;
+		} else if (line[0] != '\0') {
+			linenoiseHistoryAdd(line);
+			nft_run_cmd_from_buffer(nft, line);
+		}
+		linenoiseFree(line);
+	}
+	cli_exit();
+	exit(0);
+}
+
+void cli_exit(void)
+{
+	linenoiseHistorySave(histfile);
+}
+
+#endif /* HAVE_LIBREADLINE */
diff --git a/tests/build/run-tests.sh b/tests/build/run-tests.sh
index b0560da61398..ccb62af3d8dd 100755
--- a/tests/build/run-tests.sh
+++ b/tests/build/run-tests.sh
@@ -2,8 +2,8 @@
 
 log_file="`pwd`/tests.log"
 dir=../..
-argument=( --without-cli --enable-debug --with-mini-gmp --enable-man-doc
-	    --with-xtables --with-json)
+argument=( --without-cli --with-cli=linenoise --enable-debug --with-mini-gmp
+	   --enable-man-doc --with-xtables --with-json)
 ok=0
 failed=0
 
-- 
2.23.0


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

* [PATCH nftables v2 2/2] main: add more information to `nft -v`.
  2019-09-24  7:40 [PATCH nftables v2 0/2] Add Linenoise support to the CLI Jeremy Sowden
  2019-09-24  7:40 ` [PATCH nftables v2 1/2] cli: add linenoise CLI implementation Jeremy Sowden
@ 2019-09-24  7:40 ` Jeremy Sowden
  2019-09-26  7:35 ` [PATCH nftables v2 0/2] Add Linenoise support to the CLI Phil Sutter
  2019-10-15  7:23 ` AW: " Priebe, Sebastian
  3 siblings, 0 replies; 9+ messages in thread
From: Jeremy Sowden @ 2019-09-24  7:40 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: Netfilter Devel, Sebastian Priebe

In addition to the package-version and release-name, output the CLI
implementation (if any) and whether mini-gmp was used, e.g.:

    $ ./src/nft -v
    nftables v0.9.2 (Scram)
      cli:          linenoise
      minigmp:      no

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
---
 src/Makefile.am |  3 +++
 src/main.c      | 28 ++++++++++++++++++++++++++--
 2 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index 740c21f2cac8..54aed5efb7bb 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -13,6 +13,9 @@ endif
 if BUILD_XTABLES
 AM_CPPFLAGS += ${XTABLES_CFLAGS}
 endif
+if BUILD_MINIGMP
+AM_CPPFLAGS += -DHAVE_MINIGMP
+endif
 
 AM_CFLAGS = -Wall								\
 	    -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations	\
diff --git a/src/main.c b/src/main.c
index f77d8a820a02..3334141eab35 100644
--- a/src/main.c
+++ b/src/main.c
@@ -154,6 +154,31 @@ static void show_help(const char *name)
 	name, DEFAULT_INCLUDE_PATH);
 }
 
+static void show_version(void)
+{
+	const char *cli, *minigmp;
+
+#if defined(HAVE_LIBREADLINE)
+	cli = "readline";
+#elif defined(HAVE_LIBLINENOISE)
+	cli = "linenoise";
+#else
+	cli = "no";
+#endif
+
+#if defined(HAVE_MINIGMP)
+	minigmp = "yes";
+#else
+	minigmp = "no";
+#endif
+
+	printf("%s v%s (%s)\n"
+	       "  cli:		%s\n"
+	       "  minigmp:	%s\n",
+	       PACKAGE_NAME, PACKAGE_VERSION, RELEASE_NAME,
+	       cli, minigmp);
+}
+
 static const struct {
 	const char		*name;
 	enum nft_debug_level	level;
@@ -213,8 +238,7 @@ int main(int argc, char * const *argv)
 			show_help(argv[0]);
 			exit(EXIT_SUCCESS);
 		case OPT_VERSION:
-			printf("%s v%s (%s)\n",
-			       PACKAGE_NAME, PACKAGE_VERSION, RELEASE_NAME);
+			show_version();
 			exit(EXIT_SUCCESS);
 		case OPT_CHECK:
 			nft_ctx_set_dry_run(nft, true);
-- 
2.23.0


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

* Re: [PATCH nftables v2 0/2] Add Linenoise support to the CLI.
  2019-09-24  7:40 [PATCH nftables v2 0/2] Add Linenoise support to the CLI Jeremy Sowden
  2019-09-24  7:40 ` [PATCH nftables v2 1/2] cli: add linenoise CLI implementation Jeremy Sowden
  2019-09-24  7:40 ` [PATCH nftables v2 2/2] main: add more information to `nft -v` Jeremy Sowden
@ 2019-09-26  7:35 ` Phil Sutter
  2019-10-15  7:23 ` AW: " Priebe, Sebastian
  3 siblings, 0 replies; 9+ messages in thread
From: Phil Sutter @ 2019-09-26  7:35 UTC (permalink / raw)
  To: Jeremy Sowden; +Cc: Pablo Neira Ayuso, Netfilter Devel, Sebastian Priebe

On Tue, Sep 24, 2019 at 08:40:53AM +0100, Jeremy Sowden wrote:
> Sebastian Priebe [0] requested Linenoise support for the CLI as an
> alternative to Readline, so I thought I'd have a go at providing it.
> Linenoise is a minimal, zero-config, BSD licensed, Readline replacement
> used in Redis, MongoDB, and Android [1].
> 
>  0 - https://lore.kernel.org/netfilter-devel/4df20614cd10434b9f91080d0862dd0c@de.sii.group/
>  1 - https://github.com/antirez/linenoise/
> 
> By default, the CLI continues to be build using Readline, but passing
> `--with-cli=linenoise` instead causes Linenoise to be used instead.
> 
> `nft -v` has been extended to display what CLI implementation was built
> and whether mini-gmp was used.

Series:

Acked-by: Phil Sutter <phil@nwl.cc>

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

* AW: [PATCH nftables v2 0/2] Add Linenoise support to the CLI.
  2019-09-24  7:40 [PATCH nftables v2 0/2] Add Linenoise support to the CLI Jeremy Sowden
                   ` (2 preceding siblings ...)
  2019-09-26  7:35 ` [PATCH nftables v2 0/2] Add Linenoise support to the CLI Phil Sutter
@ 2019-10-15  7:23 ` Priebe, Sebastian
  3 siblings, 0 replies; 9+ messages in thread
From: Priebe, Sebastian @ 2019-10-15  7:23 UTC (permalink / raw)
  To: Netfilter Devel; +Cc: Jeremy Sowden, Pablo Neira Ayuso

Hello,
I'd really would appreciate if this gets pulled soon.
Are there any objections?

Until now this series was acked-by Phil Sutter.

Greetings,
Sebastian

> -----Ursprüngliche Nachricht-----
> Von: Jeremy Sowden <jeremy@azazel.net>
> Gesendet: Dienstag, 24. September 2019 09:41
> An: Pablo Neira Ayuso <pablo@netfilter.org>
> Cc: Netfilter Devel <netfilter-devel@vger.kernel.org>; Priebe, Sebastian
> <sebastian.priebe@de.sii.group>
> Betreff: [PATCH nftables v2 0/2] Add Linenoise support to the CLI.
>
> Sebastian Priebe [0] requested Linenoise support for the CLI as an alternative
> to Readline, so I thought I'd have a go at providing it.
> Linenoise is a minimal, zero-config, BSD licensed, Readline replacement used
> in Redis, MongoDB, and Android [1].
>
>  0 - https://lore.kernel.org/netfilter-
> devel/4df20614cd10434b9f91080d0862dd0c@de.sii.group/
>  1 - https://github.com/antirez/linenoise/
>
> By default, the CLI continues to be build using Readline, but passing `--with-
> cli=linenoise` instead causes Linenoise to be used instead.
>
> `nft -v` has been extended to display what CLI implementation was built and
> whether mini-gmp was used.
>
> Changes since v1:
>
>  * dropped the linenoise source from the nftables tree and added an
>    `AC_CHECK_LIB` check instead.
>
> Changes since RFC:
>
>  * two tidy-up patches dropped because they have already been applied;
>  * source now added to include/ and src/;
>  * tests/build/run-tests.sh updated to test `--with-cli=linenoise`;
>  * `nft -v` extended.
>
> Jeremy Sowden (2):
>   cli: add linenoise CLI implementation.
>   main: add more information to `nft -v`.
>
>  configure.ac             | 17 ++++++++---
>  include/cli.h            |  2 +-
>  src/Makefile.am          |  3 ++
>  src/cli.c                | 64 ++++++++++++++++++++++++++++++++++------
>  src/main.c               | 28 ++++++++++++++++--
>  tests/build/run-tests.sh |  4 +--
>  6 files changed, 100 insertions(+), 18 deletions(-)
>
> --
> 2.23.0
>




SII Technologies GmbH
Geschäftsführer: Robert Bauer
Sitz der Gesellschaft: 86167 Augsburg
Registergericht: Amtsgericht Augsburg HRB 31802


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

* Re: [PATCH nftables v2 1/2] cli: add linenoise CLI implementation.
  2019-09-24  7:40 ` [PATCH nftables v2 1/2] cli: add linenoise CLI implementation Jeremy Sowden
@ 2019-10-15  8:32   ` Pablo Neira Ayuso
  2019-10-16 10:55     ` Jeremy Sowden
  0 siblings, 1 reply; 9+ messages in thread
From: Pablo Neira Ayuso @ 2019-10-15  8:32 UTC (permalink / raw)
  To: Jeremy Sowden; +Cc: Netfilter Devel, Sebastian Priebe

On Tue, Sep 24, 2019 at 08:40:54AM +0100, Jeremy Sowden wrote:
> By default, continue to use libreadline, but if `--with-cli=linenoise`
> is passed to configure, build the linenoise implementation instead.

Applied, thanks Jeremy.

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

* Re: [PATCH nftables v2 1/2] cli: add linenoise CLI implementation.
  2019-10-15  8:32   ` Pablo Neira Ayuso
@ 2019-10-16 10:55     ` Jeremy Sowden
  2019-10-16 12:19       ` Pablo Neira Ayuso
  0 siblings, 1 reply; 9+ messages in thread
From: Jeremy Sowden @ 2019-10-16 10:55 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: Netfilter Devel, Sebastian Priebe

[-- Attachment #1: Type: text/plain, Size: 618 bytes --]

On 2019-10-15, at 10:32:52 +0200, Pablo Neira Ayuso wrote:
> On Tue, Sep 24, 2019 at 08:40:54AM +0100, Jeremy Sowden wrote:
> > By default, continue to use libreadline, but if
> > `--with-cli=linenoise` is passed to configure, build the linenoise
> > implementation instead.
>
> Applied, thanks Jeremy.

Thanks, Pablo.  Don't know whether you change your mind about it, but
there was a second patch with changes to `nft -v` that you suggested:

  https://lore.kernel.org/netfilter-devel/20190924074055.4146-3-jeremy@azazel.net/

Need to find something else to do now. :) Will go and have a poke about
in Bugzilla.

J.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH nftables v2 1/2] cli: add linenoise CLI implementation.
  2019-10-16 10:55     ` Jeremy Sowden
@ 2019-10-16 12:19       ` Pablo Neira Ayuso
  2019-10-16 13:34         ` Jeremy Sowden
  0 siblings, 1 reply; 9+ messages in thread
From: Pablo Neira Ayuso @ 2019-10-16 12:19 UTC (permalink / raw)
  To: Jeremy Sowden; +Cc: Netfilter Devel, Sebastian Priebe

On Wed, Oct 16, 2019 at 11:55:02AM +0100, Jeremy Sowden wrote:
> On 2019-10-15, at 10:32:52 +0200, Pablo Neira Ayuso wrote:
> > On Tue, Sep 24, 2019 at 08:40:54AM +0100, Jeremy Sowden wrote:
> > > By default, continue to use libreadline, but if
> > > `--with-cli=linenoise` is passed to configure, build the linenoise
> > > implementation instead.
> >
> > Applied, thanks Jeremy.
> 
> Thanks, Pablo.  Don't know whether you change your mind about it, but
> there was a second patch with changes to `nft -v` that you suggested:
> 
>   https://lore.kernel.org/netfilter-devel/20190924074055.4146-3-jeremy@azazel.net/
> 
> Need to find something else to do now. :) Will go and have a poke about
> in Bugzilla.

This might be useful:

https://bugzilla.netfilter.org/show_bug.cgi?id=1374

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

* Re: [PATCH nftables v2 1/2] cli: add linenoise CLI implementation.
  2019-10-16 12:19       ` Pablo Neira Ayuso
@ 2019-10-16 13:34         ` Jeremy Sowden
  0 siblings, 0 replies; 9+ messages in thread
From: Jeremy Sowden @ 2019-10-16 13:34 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: Netfilter Devel, Sebastian Priebe

[-- Attachment #1: Type: text/plain, Size: 914 bytes --]

On 2019-10-16, at 14:19:30 +0200, Pablo Neira Ayuso wrote:
> On Wed, Oct 16, 2019 at 11:55:02AM +0100, Jeremy Sowden wrote:
> > On 2019-10-15, at 10:32:52 +0200, Pablo Neira Ayuso wrote:
> > > On Tue, Sep 24, 2019 at 08:40:54AM +0100, Jeremy Sowden wrote:
> > > > By default, continue to use libreadline, but if
> > > > `--with-cli=linenoise` is passed to configure, build the
> > > > linenoise implementation instead.
> > >
> > > Applied, thanks Jeremy.
> >
> > Thanks, Pablo.  Don't know whether you change your mind about it,
> > but there was a second patch with changes to `nft -v` that you
> > suggested:
> >
> >   https://lore.kernel.org/netfilter-devel/20190924074055.4146-3-jeremy@azazel.net/
> >
> > Need to find something else to do now. :) Will go and have a poke
> > about in Bugzilla.
>
> This might be useful:
>
> https://bugzilla.netfilter.org/show_bug.cgi?id=1374

Cheers.  I'll pick that up.

J.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

end of thread, other threads:[~2019-10-16 13:34 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-24  7:40 [PATCH nftables v2 0/2] Add Linenoise support to the CLI Jeremy Sowden
2019-09-24  7:40 ` [PATCH nftables v2 1/2] cli: add linenoise CLI implementation Jeremy Sowden
2019-10-15  8:32   ` Pablo Neira Ayuso
2019-10-16 10:55     ` Jeremy Sowden
2019-10-16 12:19       ` Pablo Neira Ayuso
2019-10-16 13:34         ` Jeremy Sowden
2019-09-24  7:40 ` [PATCH nftables v2 2/2] main: add more information to `nft -v` Jeremy Sowden
2019-09-26  7:35 ` [PATCH nftables v2 0/2] Add Linenoise support to the CLI Phil Sutter
2019-10-15  7:23 ` AW: " Priebe, Sebastian

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).