]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Update and cleanup the readline library support
authorOndřej Surý <ondrej@isc.org>
Tue, 11 Aug 2020 14:14:36 +0000 (16:14 +0200)
committerOndřej Surý <ondrej@isc.org>
Tue, 18 Aug 2020 08:27:14 +0000 (10:27 +0200)
This commit updates and simplifies the checks for the readline support
in nslookup and nsupdate:

  * Change the autoconf checks to pkg-config only, all supported
    libraries have accompanying .pc files now.
  * Add editline support in addition to libedit and GNU readline
  * Add isc/readline.h shim header that defines dummy readline()
    function when no readline library is available

Makefile.top
bin/dig/Makefile.am
bin/dig/nslookup.c
bin/nsupdate/Makefile.am
bin/nsupdate/nsupdate.c
configure.ac
lib/isc/Makefile.am
lib/isc/include/isc/readline.h [new file with mode: 0644]
util/copyrights

index 140ab4406e017aa4057771b6dd3d2d87d627825b..727f08bf493d32f6e60176da4597bf0013ade065 100644 (file)
@@ -33,6 +33,11 @@ LIBISC_CFLAGS +=                                     \
        $(LIBXML2_CFLAGS)
 endif HAVE_LIBXML2
 
+if HAVE_READLINE
+LIBISC_CFLAGS +=                                       \
+       $(READLINE_CFLAGS)
+endif HAVE_READLINE
+
 LIBISC_LIBS = $(top_builddir)/lib/isc/libisc.la
 
 LIBDNS_CFLAGS = \
index 7bcd2f5e6525889ee190b0ad6135f9afbc476532..538a5fdcfedc89c53f8e8f85bb1673ad5315583b 100644 (file)
@@ -25,6 +25,10 @@ libdighost_la_SOURCES = \
 
 bin_PROGRAMS = dig host nslookup
 
-nslookup_LDADD =       \
-       $(LDADD)        \
-       $(READLINE_LIB)
+nslookup_LDADD =               \
+       $(LDADD)
+
+if HAVE_READLINE
+nslookup_LDADD +=              \
+       $(READLINE_LIBS)
+endif HAVE_READLINE
index 7309c8cdbfcef07c99f1c2242f02b65c066890a0..ed9728bb7de21cd2924e42e5bf6375563aff0b10 100644 (file)
@@ -22,6 +22,7 @@
 #include <isc/netaddr.h>
 #include <isc/parseint.h>
 #include <isc/print.h>
+#include <isc/readline.h>
 #include <isc/string.h>
 #include <isc/task.h>
 #include <isc/util.h>
 
 #include "dighost.h"
 
-#if defined(HAVE_READLINE)
-#if defined(HAVE_EDIT_READLINE_READLINE_H)
-#include <edit/readline/readline.h>
-#if defined(HAVE_EDIT_READLINE_HISTORY_H)
-#include <edit/readline/history.h>
-#endif /* if defined(HAVE_EDIT_READLINE_HISTORY_H) */
-#elif defined(HAVE_EDITLINE_READLINE_H)
-#include <editline/readline.h>
-#elif defined(HAVE_READLINE_READLINE_H)
-/* Prevent deprecated functions being declared. */
-#define _FUNCTION_DEF 1
-/* Ensure rl_message() gets prototype. */
-#define USE_VARARGS   1
-#define PREFER_STDARG 1
-#include <readline/readline.h>
-#if defined(HAVE_READLINE_HISTORY_H)
-#include <readline/history.h>
-#endif /* if defined(HAVE_READLINE_HISTORY_H) */
-#endif /* if defined(HAVE_EDIT_READLINE_READLINE_H) */
-#endif /* if defined(HAVE_READLINE) */
-
 static bool short_form = true, tcpmode = false, tcpmode_set = false,
            identify = false, stats = true, comments = true,
            section_question = true, section_answer = true,
@@ -853,38 +833,27 @@ do_next_command(char *input) {
 
 static void
 get_next_command(void) {
-       char *buf;
-       char *ptr;
+       char cmdlinebuf[COMMSIZE];
+       char *cmdline, *ptr = NULL;
 
-       fflush(stdout);
-       buf = isc_mem_allocate(mctx, COMMSIZE);
        isc_app_block();
        if (interactive) {
-#ifdef HAVE_READLINE
-               ptr = readline("> ");
-               if (ptr != NULL) {
+               cmdline = ptr = readline("> ");
+               if (ptr != NULL && *ptr != 0) {
                        add_history(ptr);
                }
-#else  /* ifdef HAVE_READLINE */
-               fprintf(stderr, "> ");
-               fflush(stderr);
-               ptr = fgets(buf, COMMSIZE, stdin);
-#endif /* ifdef HAVE_READLINE */
        } else {
-               ptr = fgets(buf, COMMSIZE, stdin);
+               cmdline = fgets(cmdlinebuf, COMMSIZE, stdin);
        }
        isc_app_unblock();
-       if (ptr == NULL) {
+       if (cmdline == NULL) {
                in_use = false;
        } else {
-               do_next_command(ptr);
+               do_next_command(cmdline);
        }
-#ifdef HAVE_READLINE
-       if (interactive) {
+       if (ptr != NULL) {
                free(ptr);
        }
-#endif /* ifdef HAVE_READLINE */
-       isc_mem_free(mctx, buf);
 }
 
 ISC_NORETURN static void
index 8e2cbaa6efca1ccb992c11eeb3f007643021d3da..29fd0cfac0cc5525ec327d64af7f94fd52f96343 100644 (file)
@@ -18,8 +18,12 @@ LDADD =                              \
        $(LIBISCCFG_LIBS)       \
        $(LIBIRS_LIBS)          \
        $(LIBBIND9_LIBS)        \
-       $(READLINE_LIB)         \
        $(GSSAPI_LIBS)          \
        $(KRB5_LIBS)
 
+if HAVE_READLINE
+LDADD +=                       \
+       $(READLINE_LIBS)
+endif
+
 bin_PROGRAMS = nsupdate
index 654c6508d893737d626a8f8097b264acf0529c0d..8416760d931168298f065539e7ea725508a5bcb2 100644 (file)
@@ -36,6 +36,7 @@
 #include <isc/portset.h>
 #include <isc/print.h>
 #include <isc/random.h>
+#include <isc/readline.h>
 #include <isc/region.h>
 #include <isc/sockaddr.h>
 #include <isc/socket.h>
 
 #include <bind9/getaddresses.h>
 
-#if defined(HAVE_READLINE)
-#if defined(HAVE_EDIT_READLINE_READLINE_H)
-#include <edit/readline/readline.h>
-#if defined(HAVE_EDIT_READLINE_HISTORY_H)
-#include <edit/readline/history.h>
-#endif /* if defined(HAVE_EDIT_READLINE_HISTORY_H) */
-#elif defined(HAVE_EDITLINE_READLINE_H)
-#include <editline/readline.h>
-#else /* if defined(HAVE_EDIT_READLINE_READLINE_H) */
-/* Prevent deprecated functions being declared. */
-#define _FUNCTION_DEF 1
-/* Ensure rl_message() gets prototype. */
-#define USE_VARARGS   1
-#define PREFER_STDARG 1
-#include <readline/history.h>
-#include <readline/readline.h>
-#endif /* if defined(HAVE_EDIT_READLINE_READLINE_H) */
-#endif /* if defined(HAVE_READLINE) */
-
 #define MAXCMD      (128 * 1024)
 #define MAXWIRE             (64 * 1024)
 #define PACKETSIZE   ((64 * 1024) - 1)
@@ -2295,20 +2277,14 @@ static uint16_t
 get_next_command(void) {
        uint16_t result = STATUS_QUIT;
        char cmdlinebuf[MAXCMD];
-       char *cmdline;
+       char *cmdline = NULL, *ptr = NULL;
 
        isc_app_block();
        if (interactive) {
-#ifdef HAVE_READLINE
-               cmdline = readline("> ");
-               if (cmdline != NULL) {
-                       add_history(cmdline);
-               }
-#else  /* ifdef HAVE_READLINE */
-               fprintf(stdout, "> ");
-               fflush(stdout);
-               cmdline = fgets(cmdlinebuf, MAXCMD, input);
-#endif /* ifdef HAVE_READLINE */
+               cmdline = ptr = readline("> ");
+               if (ptr != NULL && *ptr != 0) {
+                       add_history(ptr);
+               }
        } else {
                cmdline = fgets(cmdlinebuf, MAXCMD, input);
        }
@@ -2324,11 +2300,10 @@ get_next_command(void) {
                (void)nsu_strsep(&tmp, "\r\n");
                result = do_next_command(cmdline);
        }
-#ifdef HAVE_READLINE
-       if (interactive) {
-               free(cmdline);
+       if (ptr != NULL) {
+               free(ptr);
        }
-#endif /* ifdef HAVE_READLINE */
+
        return (result);
 }
 
index edc3eefbcf689dcef4440e6908b44796170e9ba1..7f2fe3738dea9c1e16c8b083fed393e4c30a4b04 100644 (file)
@@ -976,66 +976,39 @@ AS_IF([test "$enable_tcp_fastopen" = "yes"],
 #
 AC_CHECK_FUNCS([strlcpy strlcat])
 
-AC_SUBST(READLINE_LIB)
-AC_ARG_WITH(readline,
-           AS_HELP_STRING([--with-readline[=LIBSPEC]],
-                          [specify readline library [default auto]]),
-           use_readline="$withval", use_readline="auto")
-case "$use_readline" in
-no)    ;;
-*)
-       saved_LIBS="$LIBS"
-       case "$use_readline" in
-       yes|auto) try_readline="-ledit"; or_readline="-lreadline" ;;
-       *) try_readline="$use_readline"
-       esac
-       for readline in "$try_readline" $or_readline
-       do
-               LIBS="$readline"
-               AC_MSG_NOTICE(checking for readline with $readline)
-               AC_CHECK_FUNCS(readline)
-               if test "yes" = "$ac_cv_func_readline"
-               then
-                       READLINE_LIB="$readline"
-                       break
-               fi
-               for lib in -lterminfo -ltermcap -lncurses -lcurses
-               do
-                       AC_MSG_NOTICE(checking for readline with $readline $lib)
-                       unset ac_cv_func_readline
-                       LIBS="$readline $lib"
-                       AC_CHECK_FUNCS(readline)
-                       if test "yes" = "$ac_cv_func_readline"
-                       then
-                               READLINE_LIB="$readline $lib"
-                               break
-                       fi
-               done
-               if test "yes" = "$ac_cv_func_readline"
-               then
-                       break
-               fi
-       done
-       if test "auto" != "$use_readline" &&
-          test "X$READLINE_LIB" = "X"
-       then
-               AC_MSG_ERROR([The readline library was not found.])
-       fi
-       LIBS="$saved_LIBS"
-       ;;
-esac
-if test "yes" = "$ac_cv_func_readline"
-then
-       case "$READLINE_LIB" in
-       *edit*)
-               AC_CHECK_HEADERS(editline/readline.h)
-               AC_CHECK_HEADERS(edit/readline/readline.h)
-               AC_CHECK_HEADERS(edit/readline/history.h)
-               ;;
-       esac
-       AC_CHECK_HEADERS(readline/readline.h)
-       AC_CHECK_HEADERS(readline/history.h)
-fi
+#
+# Check for readline support
+#
+
+AC_ARG_WITH([readline],
+           [AS_HELP_STRING([--with-readline=yes|no|libedit|readline],
+                           [specify readline library [default auto]])],
+           [], [with_readline="auto"])
+
+AS_CASE([$with_readline],
+       [no],[],
+       [auto],
+       [PKG_CHECK_MODULES([READLINE], [libedit],
+                          [AC_DEFINE([HAVE_READLINE_LIBEDIT], [1], [Build with libedit support])],
+                          [PKG_CHECK_MODULES([READLINE], [editline],
+                                             [AC_DEFINE([HAVE_READLINE_EDITLINE], [1], [Build with editline support.])],
+                                             [PKG_CHECK_MODULES([READLINE], [readline],
+                                                                [AC_DEFINE([HAVE_READLINE_READLINE], [1], [Build with readline support.])],
+                                                                [AS_IF([test "$with_readline" = "yes"],
+                                                                       [AC_MSG_ERROR([readline support requested, but none of the libraries have been found.])])])])])],
+       [libedit],
+       [PKG_CHECK_MODULES([READLINE], [libedit],
+                          [AC_DEFINE([HAVE_READLINE_LIBEDIT], [1], [Build with libedit support])])],
+       [editline],
+       [PKG_CHECK_MODULES([READLINE], [editline],
+                          [AC_DEFINE([HAVE_READLINE_EDITLINE], [1], [Build with editline support])])],
+       [readline],
+       [PKG_CHECK_MODULES([READLINE], [readline],
+                          [AC_DEFINE([HAVE_READLINE_READLINE], [1], [Build with readline support])])],
+       [AC_MSG_ERROR([Unknown readline '$with_readline' library requested.])])
+AM_CONDITIONAL([HAVE_READLINE], [test -n "$READLINE_LIBS"])
+AC_SUBST([READLINE_CFLAGS])
+AC_SUBST([READLINE_LIBS])
 
 #
 # Security Stuff
index 19bf5b677ec1d25334312193ef79ec3c45f5de03..cde0947bdeb3af9c17536fe42d77e6b2fd95f08b 100644 (file)
@@ -66,6 +66,7 @@ libisc_la_HEADERS =                   \
        include/isc/radix.h             \
        include/isc/random.h            \
        include/isc/ratelimiter.h       \
+       include/isc/readline.h          \
        include/isc/refcount.h          \
        include/isc/regex.h             \
        include/isc/region.h            \
diff --git a/lib/isc/include/isc/readline.h b/lib/isc/include/isc/readline.h
new file mode 100644 (file)
index 0000000..8111228
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * See the COPYRIGHT file distributed with this work for additional
+ * information regarding copyright ownership.
+ */
+
+#pragma once
+
+/*
+ * A little wrapper around readline(), add_history() and free() to make using
+ * the readline code simpler.
+ */
+
+#if defined(HAVE_READLINE_LIBEDIT)
+#include <editline/readline.h>
+#elif defined(HAVE_READLINE_EDITLINE)
+#include <editline.h>
+#elif defined(HAVE_READLINE_READLINE)
+/* Prevent deprecated functions being declared. */
+#define _FUNCTION_DEF 1
+/* Ensure rl_message() gets prototype. */
+#define USE_VARARGS   1
+#define PREFER_STDARG 1
+#include <readline/history.h>
+#include <readline/readline.h>
+#endif
+
+#if !defined(HAVE_READLINE_LIBEDIT) && !defined(HAVE_READLINE_EDITLINE) && \
+       !defined(HAVE_READLINE_READLINE)
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#define RL_MAXCMD (128 * 1024)
+
+static inline char *
+readline(const char *prompt) {
+       char *line, *buf = malloc(RL_MAXCMD);
+       fprintf(stdout, "%s", prompt);
+       fflush(stdout);
+       line = fgets(buf, RL_MAXCMD, stdin);
+       if (line == NULL) {
+               free(buf);
+               return (NULL);
+       }
+       return (buf);
+};
+
+#define add_history(line)
+
+#endif
index d50730d93eddf83374dbcb6e824996b492dd0709..6ea744aaee748c6c2e2eb1d9a4a03568888cf3b4 100644 (file)
 ./lib/isc/include/isc/radix.h                  C       2007,2008,2013,2014,2016,2018,2019,2020
 ./lib/isc/include/isc/random.h                 C       1999,2000,2001,2004,2005,2006,2007,2009,2014,2016,2017,2018,2019,2020
 ./lib/isc/include/isc/ratelimiter.h            C       1999,2000,2001,2002,2004,2005,2006,2007,2009,2014,2016,2018,2019,2020
+./lib/isc/include/isc/readline.h               C       2020
 ./lib/isc/include/isc/refcount.h               C       2001,2003,2004,2005,2006,2007,2009,2016,2017,2018,2019,2020
 ./lib/isc/include/isc/regex.h                  C       2013,2016,2018,2019,2020
 ./lib/isc/include/isc/region.h                 C       1998,1999,2000,2001,2002,2004,2005,2006,2007,2013,2016,2018,2019,2020