From 35f6f4fb73605bf7fe27332c640265ec65e8b50a Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Sun, 6 Jan 2013 11:43:33 +0100 Subject: [PATCH] lldpcli: make readline library optional. And improve compatibility. We should be compatible with BSD libedit. However, we are not compatible with older versions that lack `rl_insert_text()` and `rl_delete_text()`. --- README.md | 4 ++++ configure.ac | 22 +++++++++++++++++++--- m4/ax_lib_readline.m4 | 14 +++++++++++++- src/client/client.h | 6 ++---- src/client/lldpcli.c | 22 +++++++++++++++++++--- 5 files changed, 57 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 37dac0f6..3c24649a 100644 --- a/README.md +++ b/README.md @@ -149,3 +149,7 @@ lldpd is distributed under the ISC license: > WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN > ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF > OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +Also, `lldpcli` will be linked to GNU Readline (which is GPL licensed) +if available. To avoid this, use `--without-readline` as a configure +option. diff --git a/configure.ac b/configure.ac index 50ca5b4c..b55bd9a5 100644 --- a/configure.ac +++ b/configure.ac @@ -100,12 +100,27 @@ PKG_CHECK_MODULES([CHECK], [check >= 0.9.4], [have_check=yes], [have_check=no]) # Libevent lldp_CHECK_LIBEVENT -# readline (or something similar) -AX_LIB_READLINE - ####################### ### Options +# Readline +AC_ARG_WITH([readline], + AS_HELP_STRING( + [--with-readline], + [Enable the use of readline-like library @<:@default=check@:>@]), + [], + [with_readline=check]) +if test x"$with_readline" != x"no"; then + AX_LIB_READLINE + if test x"$with_readline" != x"check"; then + if test x"$ax_cv_lib_readline" = x"no"; then + AC_MSG_FAILURE([*** no readline support found]) + fi + fi +else + ax_cv_lib_readline="no" +fi + # SNMP AC_ARG_WITH([snmp], AS_HELP_STRING( @@ -177,6 +192,7 @@ cat < # # else /* !defined(HAVE_READLINE_H) */ # extern char *readline (); +# extern int rl_insert_text(const char*); +# extern int rl_delete_text(int, int); +# extern void rl_forced_update_display(void); +# extern int rl_bind_key(int, int(*f)(int, int)); # # endif /* !defined(HAVE_READLINE_H) */ # char *cmdline = NULL; # #else /* !defined(HAVE_READLINE_READLINE_H) */ @@ -58,6 +62,9 @@ # and this notice are preserved. This file is offered as-is, without any # warranty. +# Modified version. Original version is available here: +# http://www.gnu.org/software/autoconf-archive/ax_lib_readline.html + #serial 6 AU_ALIAS([VL_LIB_READLINE], [AX_LIB_READLINE]) @@ -73,7 +80,12 @@ AC_DEFUN([AX_LIB_READLINE], [ TRY_LIB="-l$readline_lib -l$termcap_lib" fi LIBS="$ORIG_LIBS $TRY_LIB" - AC_TRY_LINK_FUNC(readline, ax_cv_lib_readline="$TRY_LIB") + for readline_func in readline rl_insert_text rl_delete_text rl_forced_update_display; do + AC_TRY_LINK_FUNC($readline_func, ax_cv_lib_readline="$TRY_LIB", ax_cv_lib_readline="") + if test -z "$ax_cv_lib_readline"; then + break + fi + done if test -n "$ax_cv_lib_readline"; then break fi diff --git a/src/client/client.h b/src/client/client.h index 524612ff..d4374902 100644 --- a/src/client/client.h +++ b/src/client/client.h @@ -36,13 +36,11 @@ # include # else extern char *readline(); -extern char *rl_line_buffer +extern char *rl_line_buffer; extern int rl_point; extern int rl_insert_text(const char*); extern int rl_delete_text(int, int); -extern int rl_ding(void); -extern int rl_crlf(void); -extern int rl_on_new_line(void); +extern void rl_forced_update_display(void); extern int rl_bind_key(int, int(*f)(int, int)); # endif #endif diff --git a/src/client/lldpcli.c b/src/client/lldpcli.c index 641fe9f8..b7d29383 100644 --- a/src/client/lldpcli.c +++ b/src/client/lldpcli.c @@ -116,6 +116,7 @@ cmd_update(struct lldpctl_conn_t *conn, struct writer *w, return 1; } +#ifdef HAVE_LIBREADLINE static int _cmd_complete(int all) { @@ -145,9 +146,8 @@ _cmd_complete(int all) goto end; } /* No completion or several completion available. */ - if (!all) rl_ding(); - rl_crlf(); - rl_on_new_line(); + fprintf(stderr, "\n"); + rl_forced_update_display(); rc = 0; end: free(line); @@ -166,6 +166,18 @@ cmd_help(int count, int ch) { return _cmd_complete(1); } +#else +static char* +readline() +{ + static char line[2048]; + fprintf(stderr, "%s", prompt()); + fflush(stderr); + if (fgets(line, sizeof(line) - 2, stdin) == NULL) + return NULL; + return line; +} +#endif static struct cmd_node* register_commands() @@ -252,9 +264,11 @@ main(int argc, char *argv[]) /* More arguments! */ must_exit = 1; } else { +#ifdef HAVE_LIBREADLINE /* Shell session */ rl_bind_key('?', cmd_help); rl_bind_key('\t', cmd_complete); +#endif } /* Make a connection */ @@ -283,7 +297,9 @@ main(int argc, char *argv[]) continue; } if (cargc == 0) continue; +#ifdef HAVE_READLINE_HISTORY add_history(line); +#endif } /* Init output formatter */ -- 2.39.5