]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
lldpcli: make readline library optional.
authorVincent Bernat <bernat@luffy.cx>
Sun, 6 Jan 2013 10:43:33 +0000 (11:43 +0100)
committerVincent Bernat <bernat@luffy.cx>
Sun, 6 Jan 2013 10:43:33 +0000 (11:43 +0100)
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
configure.ac
m4/ax_lib_readline.m4
src/client/client.h
src/client/lldpcli.c

index 37dac0f61059da606fbda3699edbcbf81b301e8f..3c24649a32b2b58c928624b79128ca493c9e4868 100644 (file)
--- 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.
index 50ca5b4c2a4b4af6f61882e6aee65ec7b4d8a161..b55bd9a56f0697f32310811cf856e72ede1da05e 100644 (file)
@@ -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 <<EOF
   C Compiler.....: $CC $CFLAGS $CPPFLAGS
   Linker.........: $LD $LDFLAGS $LIBS
   Libevent.......: $libevent
+  Readline.......: ${ax_cv_lib_readline}
  Optional features:
   SNMP support...: ${with_snmp-no}
   CDP............: $enable_cdp
index 439db7f1d3d7f650567e0d26ca110c2356634ffb..4fc4af3624fcf7936921e2f4e59f334b4876efe8 100644 (file)
 #     #    include <readline.h>
 #     #  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
index 524612ffb17f849f7aafd632a5bea55f3d5e6e17..d437490290c72bba3a07f527f1da85a2e288c2e6 100644 (file)
 #    include <readline.h>
 #  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
index 641fe9f8ab9e7c39b704b17a01800003ad548bc8..b7d2938391b0f749bb2aea5347f44b5d4f2bd7e0 100644 (file)
@@ -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 */