From 9420423900a2e4312c570632f7531483dea604a2 Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Mon, 4 Jan 2021 21:24:51 +0100 Subject: cli: add libedit support Extend cli to support for libedit readline shim code: ./configure --with-cli=editline Signed-off-by: Pablo Neira Ayuso --- configure.ac | 7 ++++++- include/cli.h | 2 +- src/cli.c | 36 ++++++++++++++++++++++++++++-------- src/main.c | 4 +++- tests/build/run-tests.sh | 2 +- 5 files changed, 39 insertions(+), 12 deletions(-) diff --git a/configure.ac b/configure.ac index 1b493541..77322dc1 100644 --- a/configure.ac +++ b/configure.ac @@ -68,7 +68,7 @@ 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 or linenoise support)])], + [disable interactive CLI (libreadline, editline or linenoise support)])], [], [with_cli=readline]) AS_IF([test "x$with_cli" = xreadline], [ @@ -80,6 +80,11 @@ AC_DEFINE([HAVE_LIBREADLINE], [1], []) AC_CHECK_LIB([linenoise], [linenoise], , AC_MSG_ERROR([No suitable version of linenoise found])) AC_DEFINE([HAVE_LIBLINENOISE], [1], []) +], + [test "x$with_cli" = xeditline], [ +AC_CHECK_LIB([edit], [readline], , + AC_MSG_ERROR([No suitable version of libedit found])) +AC_DEFINE([HAVE_LIBEDIT], [1], []) ], [test "x$with_cli" != xno], [ AC_MSG_ERROR([unexpected CLI value: $with_cli]) diff --git a/include/cli.h b/include/cli.h index d8251775..609ed2ed 100644 --- a/include/cli.h +++ b/include/cli.h @@ -4,7 +4,7 @@ #include #include -#if defined(HAVE_LIBREADLINE) || defined(HAVE_LIBLINENOISE) +#if defined(HAVE_LIBREADLINE) || defined(HAVE_LIBEDIT) || 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 4c0c3e9d..45811595 100644 --- a/src/cli.c +++ b/src/cli.c @@ -24,6 +24,9 @@ #ifdef HAVE_LIBREADLINE #include #include +#elif defined(HAVE_LIBEDIT) +#include +#include #else #include #endif @@ -48,7 +51,26 @@ init_histfile(void) snprintf(histfile, sizeof(histfile), "%s/%s", home, CMDLINE_HISTFILE); } -#ifdef HAVE_LIBREADLINE +#if defined(HAVE_LIBREADLINE) +static void nft_rl_prompt_save(void) +{ + rl_save_prompt(); + rl_clear_message(); + rl_set_prompt(".... "); +} +#define nft_rl_prompt_restore rl_restore_prompt +#elif defined(HAVE_LIBEDIT) +static void nft_rl_prompt_save(void) +{ + rl_set_prompt(".... "); +} +static void nft_rl_prompt_restore(void) +{ + rl_set_prompt("nft> "); +} +#endif + +#if defined(HAVE_LIBREADLINE) || defined(HAVE_LIBEDIT) static struct nft_ctx *cli_nft; static char *multiline; @@ -72,9 +94,7 @@ static char *cli_append_multiline(char *line) if (multiline == NULL) { multiline = line; - rl_save_prompt(); - rl_clear_message(); - rl_set_prompt(".... "); + nft_rl_prompt_save(); } else { len += strlen(multiline); s = malloc(len + 1); @@ -93,7 +113,7 @@ static char *cli_append_multiline(char *line) if (complete) { line = multiline; multiline = NULL; - rl_restore_prompt(); + nft_rl_prompt_restore(); } return line; } @@ -142,7 +162,7 @@ static char **cli_completion(const char *text, int start, int end) int cli_init(struct nft_ctx *nft) { cli_nft = nft; - rl_readline_name = "nft"; + rl_readline_name = (char *)"nft"; rl_instream = stdin; rl_outstream = stdout; @@ -166,7 +186,7 @@ void cli_exit(void) write_history(histfile); } -#else /* !HAVE_LIBREADLINE */ +#else /* HAVE_LINENOISE */ int cli_init(struct nft_ctx *nft) { @@ -195,4 +215,4 @@ void cli_exit(void) linenoiseHistorySave(histfile); } -#endif /* HAVE_LIBREADLINE */ +#endif /* HAVE_LINENOISE */ diff --git a/src/main.c b/src/main.c index 3c26f510..0a1c4775 100644 --- a/src/main.c +++ b/src/main.c @@ -227,8 +227,10 @@ static void show_version(void) { const char *cli, *minigmp, *json, *xt; -#if defined(HAVE_LIBREADLINE) +#if defined(HAVE_READLINE) cli = "readline"; +#elif defined(HAVE_LIBEDIT) + cli = "editline"; #elif defined(HAVE_LIBLINENOISE) cli = "linenoise"; #else diff --git a/tests/build/run-tests.sh b/tests/build/run-tests.sh index ccb62af3..9ce93a8e 100755 --- a/tests/build/run-tests.sh +++ b/tests/build/run-tests.sh @@ -2,7 +2,7 @@ log_file="`pwd`/tests.log" dir=../.. -argument=( --without-cli --with-cli=linenoise --enable-debug --with-mini-gmp +argument=( --without-cli --with-cli=linenoise --with-cli=editline --enable-debug --with-mini-gmp --enable-man-doc --with-xtables --with-json) ok=0 failed=0 -- cgit v1.2.3