Skip to contentSkip to conversation

Add Linenoise support to the CLI.

PATCH In review Jeremy Sowden 4 participants 12 messages Sep 21, 2019 – Sep 24, 2019 netfilter

lore
Share & tools
lore
#0 lore raw

cover 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/[email protected]/
1 - https://github.com/antirez/linenoise/

The upstream repo doesn't contain the infrastructure for building or installing libraries. I've taken a look at how Redis and MongoDB handle this, and they both include the upstream sources in their trees (MongoDB actually uses a C++ fork, Linenoise NG [2]), so I've done the same.

2 - https://github.com/arangodb/linenoise-ng

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 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 (3):
src, include: add upstream linenoise source.
cli: add linenoise CLI implementation.
main: add more information to nft -v.

 configure.ac             |   14 +-
 include/Makefile.am      |    1 +
 include/cli.h            |    2 +-
 include/linenoise.h      |   73 +++
 src/Makefile.am          |   12 +
 src/cli.c                |   64 +-
 src/linenoise.c          | 1201 ++++++++++++++++++++++++++++++++++++++
 src/main.c               |   28 +-
 tests/build/run-tests.sh |    4 +-
 9 files changed, 1381 insertions(+), 18 deletions(-)
 create mode 100644 include/linenoise.h
 create mode 100644 src/linenoise.c
Signature · 1 line
2.23.0
#1 lore raw

PATCH 1/3 src, include: add upstream linenoise source.

Linenoise is a minimal, zero-config, BSD licensed, readline replacement used in Redis, MongoDB, and Android:

https://github.com/antirez/linenoise/

The upstream repo doesn't contain the infrastructure for building or installing libraries. There was a 1.0 release made in 2015, but there have been a number of bug-fixes committed since. Therefore, add the latest upstream source:

4a961c010872 ("Merge pull request #151 from hoelzro/master")

to the nftables tree.

---
 include/Makefile.am |    1 +
 include/linenoise.h |   73 +++
 src/linenoise.c     | 1201 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 1275 insertions(+)
 create mode 100644 include/linenoise.h
 create mode 100644 src/linenoise.c
include/Makefile.am +1 -0
index 04a4a619a530..ab056630b777 100644
@@ -2,6 +2,7 @@ SUBDIRS =		linux		\
 			nftables
 
 noinst_HEADERS = 	cli.h		\
+			linenoise.h	\
 			cache.h		\
 			datatype.h	\
 			expression.h	\
include/linenoise.h new file +73 -0

73 lines not on this page · raw message

src/linenoise.c new file +1201 -0

1,201 lines not on this page · raw message

Signature · 1 line
2.23.0
#2 lore raw

Re: [PATCH nftables 1/3] src, include: add upstream linenoise source.

Quoted 8 lines · Jeremy Sowden

https://github.com/antirez/linenoise/

The upstream repo doesn't contain the infrastructure for building or installing libraries. There was a 1.0 release made in 2015, but there have been a number of bug-fixes committed since. Therefore, add the latest upstream source:

src/linenoise.c | 1201 +++++++++++++++++++++++++++++++++++++++++++

That seems like a recipe to end up with stale code. For a distribution, it's static linking worsened by another degree.

(https://fedoraproject.org/wiki/Bundled_Libraries?rd=Packaging:Bundled_Libraries
)

#3 lore raw

Re: [PATCH nftables 1/3] src, include: add upstream linenoise source.

Quoted 10 lines · Jan Engelhardt
Quoted 6 lines · Jeremy Sowden

https://github.com/antirez/linenoise/

The upstream repo doesn't contain the infrastructure for building or installing libraries. There was a 1.0 release made in 2015, but there have been a number of bug-fixes committed since. Therefore, add the latest upstream source:

Quoted 6 lines
Quoted 1 line

src/linenoise.c | 1201 +++++++++++++++++++++++++++++++++++++++++++

That seems like a recipe to end up with stale code. For a distribution, it's static linking worsened by another degree.

(https://fedoraproject.org/wiki/Bundled_Libraries?rd=Packaging:Bundled_Libraries)

I thought this is like mini-gmp.c? Are distributors packaging this as a library?

#4 lore raw

Re: [PATCH nftables 1/3] src, include: add upstream linenoise source.

Quoted 11 lines · Pablo Neira Ayuso
Quoted 10 lines · Jan Engelhardt
Quoted 6 lines · Jeremy Sowden

https://github.com/antirez/linenoise/

The upstream repo doesn't contain the infrastructure for building or installing libraries. There was a 1.0 release made in 2015, but there have been a number of bug-fixes committed since. Therefore, add the latest upstream source:

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

src/linenoise.c | 1201 +++++++++++++++++++++++++++++++++++++++++++

That seems like a recipe to end up with stale code. For a distribution, it's static linking worsened by another degree.

(https://fedoraproject.org/wiki/Bundled_Libraries?rd=Packaging:Bundled_Libraries)

I thought this is like mini-gmp.c?

That was also my impression.

Quoted 1 line

Are distributors packaging this as a library?

It turns out that Fedora has packaged an old fork of it, which is also available in EPEL, and I missed it. Apologies. There's nothing in Debian or Ubuntu.

How about adding an AC_CHECK_LIB([linenoise], ...) check and falling back to the bundled copy?

J.

#5 lore raw

Re: [PATCH nftables 1/3] src, include: add upstream linenoise source.

Quoted 13 lines · Jeremy Sowden
Quoted 12 lines · Pablo Neira Ayuso
Quoted 11 lines · Jan Engelhardt
Quoted 8 lines · Jeremy Sowden

https://github.com/antirez/linenoise/

The upstream repo doesn't contain the infrastructure for building or installing libraries. There was a 1.0 release made in 2015, but there have been a number of bug-fixes committed since. Therefore, add the latest upstream source:

src/linenoise.c | 1201 +++++++++++++++++++++++++++++++++++++++++++

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

That seems like a recipe to end up with stale code. For a distribution, it's static linking worsened by another degree.

(https://fedoraproject.org/wiki/Bundled_Libraries?rd=Packaging:Bundled_Libraries)

I thought this is like mini-gmp.c?

That was also my impression.

The other thing I forgot to reiterate this morning is that linenoise support was requested for embedded environments where readline is not an option. I don't expect it to be used in general-purpose distro's which _do_ have readline available.

J.

#6 lore raw

Re: [PATCH nftables 1/3] src, include: add upstream linenoise source.

In reply to Pablo Neira Ayuso
Quoted 9 lines · Pablo Neira Ayuso
Quoted 6 lines
Quoted 1 line

src/linenoise.c | 1201 +++++++++++++++++++++++++++++++++++++++++++

That seems like a recipe to end up with stale code. For a distribution, it's static linking worsened by another degree.

(https://fedoraproject.org/wiki/Bundled_Libraries?rd=Packaging:Bundled_Libraries)

I thought this is like mini-gmp.c? Are distributors packaging this as a library?

Yes; No.

After an update to a static library, a distro would have to rebuild dependent packages and then distribute that. Doable, but cumbersome.

But bundled code evades even that. If there is a problem, all instances of the "static library" would need updating. Doable, but even more cumbersome.

Basically the question is: how is NF going to guarantee that linenoise (or mini-gmp for that matter) are always up to date?

#7 lore raw

Re: [PATCH nftables 1/3] src, include: add upstream linenoise source.

Quoted 15 lines · Jan Engelhardt
Quoted 9 lines · Pablo Neira Ayuso
Quoted 6 lines
Quoted 1 line

src/linenoise.c | 1201 +++++++++++++++++++++++++++++++++++++++++++

That seems like a recipe to end up with stale code. For a distribution, it's static linking worsened by another degree.

(https://fedoraproject.org/wiki/Bundled_Libraries?rd=Packaging:Bundled_Libraries)

I thought this is like mini-gmp.c? Are distributors packaging this as a library?

Yes; No.

Quoted 8 lines

After an update to a static library, a distro would have to rebuild dependent packages and then distribute that. Doable, but cumbersome.

But bundled code evades even that. If there is a problem, all instances of the "static library" would need updating. Doable, but even more cumbersome.

Basically the question is: how is NF going to guarantee that linenoise (or mini-gmp for that matter) are always up to date?

It seems to me that mini-gmp.c was designed to be used like we do.

For the linenoise case, given that there's already a package in Fedora, I'm fine to go for AC_CHECK_LIB([linenoise], ...) and _not_ including the copy in our tree. Probably other distributions might provide a package soon for this library.

#8 lore raw

Re: [PATCH nftables 1/3] src, include: add upstream linenoise source.

Quoted 21 lines · Pablo Neira Ayuso
Quoted 20 lines · Jan Engelhardt
Quoted 9 lines · Pablo Neira Ayuso
Quoted 6 lines
Quoted 1 line

src/linenoise.c | 1201 +++++++++++++++++++++++++++++++++++++++++++

That seems like a recipe to end up with stale code. For a distribution, it's static linking worsened by another degree.

(https://fedoraproject.org/wiki/Bundled_Libraries?rd=Packaging:Bundled_Libraries)

I thought this is like mini-gmp.c? Are distributors packaging this as a library?

Yes; No.

After an update to a static library, a distro would have to rebuild dependent packages and then distribute that. Doable, but cumbersome.

But bundled code evades even that. If there is a problem, all instances of the "static library" would need updating. Doable, but even more cumbersome.

Quoted 8 lines
Quoted 2 lines

Basically the question is: how is NF going to guarantee that linenoise (or mini-gmp for that matter) are always up to date?

It seems to me that mini-gmp.c was designed to be used like we do.

For the linenoise case, given that there's already a package in Fedora, I'm fine to go for AC_CHECK_LIB([linenoise], ...) and _not_ including the copy in our tree.

Righto. Will send out v2 in a bit.

Quoted 2 lines

Probably other distributions might provide a package soon for this library.

I've nearly finished a package for Debian. Will see if anyone fancies sponsoring it.

J.

#9 lore raw

AW: [PATCH nftables 1/3] src, include: add upstream linenoise source.

Hello Jeremy,

I just wanted to thank you for picking up my feature request. I really appreciate your work.

I would also vote for the AC_CHECK_LIB([linenoise], ...) variant instead of including the source. SQLite also does it this way.
Furthermore we already have a linenoise library in our BSP. This way we can reuse it for nftables.

I'm looking forward for the next release including the support.

Thx
Sebastian

Quoted 43 lines

-----Ursprüngliche Nachricht-----
Von: Jeremy Sowden <[email protected]>
Gesendet: Montag, 23. September 2019 20:15
An: Pablo Neira Ayuso <[email protected]>
Cc: Jan Engelhardt <[email protected]>; Netfilter Devel <netfilter- [email protected]>; Priebe, Sebastian <[email protected]>
Betreff: Re: [PATCH nftables 1/3] src, include: add upstream linenoise source.

Quoted 3 lines · Pablo Neira Ayuso
Quoted 2 lines · Jan Engelhardt
Quoted 1 line · Pablo Neira Ayuso
Quoted 1 line
Quoted 1 line

src/linenoise.c | 1201

+++++++++++++++++++++++++++++++++++++++++++

Quoted 19 lines
Quoted 19 lines
Quoted 9 lines
Quoted 6 lines

That seems like a recipe to end up with stale code. For a distribution, it's static linking worsened by another degree.

(https://fedoraproject.org/wiki/Bundled_Libraries?rd=Packaging:Bu
ndled_Libraries)

I thought this is like mini-gmp.c? Are distributors packaging this as a library?

Yes; No.

After an update to a static library, a distro would have to rebuild dependent packages and then distribute that. Doable, but cumbersome.

But bundled code evades even that. If there is a problem, all instances of the "static library" would need updating. Doable, but even more cumbersome.

Quoted 8 lines
Quoted 2 lines

Basically the question is: how is NF going to guarantee that linenoise (or mini-gmp for that matter) are always up to date?

It seems to me that mini-gmp.c was designed to be used like we do.

For the linenoise case, given that there's already a package in Fedora, I'm fine to go for AC_CHECK_LIB([linenoise], ...) and _not_ including the copy in our tree.

Righto. Will send out v2 in a bit.

Quoted 7 lines
Quoted 2 lines

Probably other distributions might provide a package soon for this library.

I've nearly finished a package for Debian. Will see if anyone fancies sponsoring it.

J.

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

#10 lore raw

PATCH 2/3 cli: add linenoise CLI implementation.

In reply to Jeremy Sowden

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

---
 configure.ac             | 14 ++++++---
 include/cli.h            |  2 +-
 src/Makefile.am          |  9 ++++++
 src/cli.c                | 64 ++++++++++++++++++++++++++++++++++------
 tests/build/run-tests.sh |  4 +--
 5 files changed, 77 insertions(+), 16 deletions(-)
configure.ac +10 -4
index 68f97f090535..27cd6f301c43 100644
@@ -68,14 +68,20 @@ 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], [],
+      [test "x$with_cli" != xno], [
+AC_MSG_ERROR([unexpected CLI value: $with_cli])
 ])
 AM_CONDITIONAL([BUILD_CLI], [test "x$with_cli" != xno])
+AM_CONDITIONAL([BUILD_CLI_LINENOISE], [test "x$with_cli" = xlinenoise])
 
 AC_ARG_WITH([xtables], [AS_HELP_STRING([--with-xtables],
             [Use libxtables for iptables interaction])],
include/cli.h +1 -1
index 023f004b8dab..ea1bd3267d64 100644
@@ -4,7 +4,7 @@
 #include <nftables/libnftables.h>
 #include <config.h>
 
-#ifdef HAVE_LIBREADLINE
+#if defined(HAVE_LIBREADLINE) || defined(HAVE_LINENOISE)
 extern int cli_init(struct nft_ctx *nft);
 #else
 static inline int cli_init(struct nft_ctx *nft)
src/Makefile.am +9 -0
index 740c21f2cac8..5c2ecbd87288 100644
@@ -13,6 +13,9 @@ endif
 if BUILD_XTABLES
 AM_CPPFLAGS += ${XTABLES_CFLAGS}
 endif
+if BUILD_CLI_LINENOISE
+AM_CPPFLAGS += -DHAVE_LINENOISE
+endif
 
 AM_CFLAGS = -Wall								\
 	    -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations	\
@@ -98,6 +101,12 @@ nft_SOURCES = main.c
 
 if BUILD_CLI
 nft_SOURCES += cli.c
+if BUILD_CLI_LINENOISE
+noinst_LTLIBRARIES += liblinenoise.la
+liblinenoise_la_SOURCES = linenoise.c
+liblinenoise_la_CFLAGS = -Wall -W
+libnftables_la_LIBADD += liblinenoise.la
+endif
 endif
 
 if BUILD_JSON
src/cli.c +55 -9

101 lines not on this page · raw message

tests/build/run-tests.sh +2 -2
index b0560da61398..ccb62af3d8dd 100755
@@ -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
 
Signature · 1 line
2.23.0
#11 lore raw

PATCH 3/3 main: add more information to `nft -v`.

In reply to Jeremy Sowden

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
---
 src/Makefile.am |  3 +++
 src/main.c      | 28 ++++++++++++++++++++++++++--
 2 files changed, 29 insertions(+), 2 deletions(-)
src/Makefile.am +3 -0
index 5c2ecbd87288..780f8c6b2b0b 100644
@@ -16,6 +16,9 @@ endif
 if BUILD_CLI_LINENOISE
 AM_CPPFLAGS += -DHAVE_LINENOISE
 endif
+if BUILD_MINIGMP
+AM_CPPFLAGS += -DHAVE_MINIGMP
+endif
 
 AM_CFLAGS = -Wall								\
 	    -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations	\
src/main.c +26 -2
index f77d8a820a02..866f66e288e2 100644
@@ -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_LINENOISE)
+	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);
Signature · 1 line
2.23.0