Skip to contentSkip to conversation

Add Linenoise support to the CLI.

PATCH Superseded Jeremy Sowden 1 participant 5 messages Sep 24, 2019 netfilter

v1 of 2 Superseded Latest is v2 →

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

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
#3 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
#4 lore raw
In reply to Jeremy Sowden
Quoted 33 lines · Jeremy Sowden

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.

Quoted 12 lines
 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

Whoops! Sent the old version out again by mistake. v2 to follow.

J.