From 35e469db6688963e04008855e63c7a10ced253c7 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Tue, 9 Nov 2021 16:37:17 +0100 Subject: [PATCH] dnsdist: Make libedit support optional --- pdns/dnsdist-console.cc | 121 +++++++++++++++------- pdns/dnsdist.cc | 5 + pdns/dnsdistdist/configure.ac | 6 +- pdns/dnsdistdist/m4/pdns_check_libedit.m4 | 32 +++++- 4 files changed, 126 insertions(+), 38 deletions(-) diff --git a/pdns/dnsdist-console.cc b/pdns/dnsdist-console.cc index 7682d432a8..5a7de283b0 100644 --- a/pdns/dnsdist-console.cc +++ b/pdns/dnsdist-console.cc @@ -19,12 +19,14 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include "config.h" #include // we need this to get the home directory of the current user #include #include +#ifdef HAVE_LIBEDIT #if defined (__OpenBSD__) || defined(__NetBSD__) // If this is not undeffed, __attribute__ wil be redefined by /usr/include/readline/rlstdc.h #undef __STRICT_ANSI__ @@ -33,6 +35,7 @@ #else #include #endif +#endif /* HAVE_LIBEDIT */ #include "ext/json11/json11.hpp" @@ -108,6 +111,7 @@ static void feedConfigDelta(const std::string& line) g_confDelta.emplace_back(now, line); } +#ifdef HAVE_LIBEDIT static string historyFile(const bool &ignoreHOME = false) { string ret; @@ -127,6 +131,7 @@ static string historyFile(const bool &ignoreHOME = false) ret.append("/.dnsdist_history"); return ret; } +#endif /* HAVE_LIBEDIT */ static bool getMsgLen32(int fd, uint32_t* len) { @@ -245,12 +250,14 @@ void doClient(ComboAddress server, const std::string& command) return; } +#ifdef HAVE_LIBEDIT string histfile = historyFile(); { ifstream history(histfile); string line; - while(getline(history, line)) + while (getline(history, line)) { add_history(line.c_str()); + } } ofstream history(histfile, std::ios_base::app); string lastline; @@ -282,43 +289,79 @@ void doClient(ComboAddress server, const std::string& command) break; } } +#else + errlog("Client mode requested but libedit support is not available"); +#endif /* HAVE_LIBEDIT */ close(fd); } +#ifdef HAVE_LIBEDIT +static std::optional getNextConsoleLine(ofstream& history, std::string& lastline) +{ + char* sline = readline("> "); + rl_bind_key('\t', rl_complete); + if (!sline) { + return std::nullopt; + } + + string line(sline); + if (!line.empty() && line != lastline) { + add_history(sline); + history << sline < getNextConsoleLine() +{ + std::string line; + if (!std::getline(std::cin, line)) { + return std::nullopt; + } + return line; +} +#endif /* HAVE_LIBEDIT */ + void doConsole() { +#ifdef HAVE_LIBEDIT string histfile = historyFile(true); { ifstream history(histfile); string line; - while(getline(history, line)) + while (getline(history, line)) { add_history(line.c_str()); + } } ofstream history(histfile, std::ios_base::app); string lastline; - for(;;) { - char* sline = readline("> "); - rl_bind_key('\t',rl_complete); - if(!sline) +#endif /* HAVE_LIBEDIT */ + + for (;;) { +#ifdef HAVE_LIBEDIT + auto line = getNextConsoleLine(history, lastline); +#else /* HAVE_LIBEDIT */ + auto line = getNextConsoleLine(); +#endif /* HAVE_LIBEDIT */ + if (!line) { break; - - string line(sline); - if(!line.empty() && line != lastline) { - add_history(sline); - history << sline < > > - >(withReturn ? ("return "+line) : line); - if(ret) { + >(withReturn ? ("return "+*line) : *line); + if (ret) { if (const auto dsValue = boost::get>(&*ret)) { if (*dsValue) { cout<<(*dsValue)->getName()<(&*ret)) { cout<<*strValue< >(&*ret)) { + else if (const auto um = boost::get >(&*ret)) { using namespace json11; Json::object o; for(const auto& v : *um) @@ -357,42 +400,48 @@ void doConsole() cout<@]), + [with_libedit=$enableval], + [with_libedit=yes] + ) + AC_MSG_RESULT([$with_libedit]) + + AS_IF([test "x$with_libedit" != "xno"], [ + AS_IF([test "x$with_libedit" = "xyes" -o "x$with_libedit" = "xauto"], [ + PKG_CHECK_MODULES([LIBEDIT], [libedit], [ + [HAVE_LIBEDIT=1] + AC_DEFINE([HAVE_LIBEDIT], [1], [Define to 1 if you have libedit]) + save_CFLAGS=$CFLAGS + save_LIBS=$LIBS + CFLAGS="$LIBEDIT_CFLAGS $CFLAGS" + LIBS="$LIBEDIT_LIBS $LIBS" + CFLAGS=$save_CFLAGS + LIBS=$save_LIBS + + ], [ : ]) + ]) + ]) + AM_CONDITIONAL([HAVE_LIBEDIT], [test "x$LIBEDIT_LIBS" != "x"]) + AS_IF([test "x$with_libedit" = "xyes"], [ + AS_IF([test x"$LIBEDIT_LIBS" = "x"], [ + AC_MSG_ERROR([libedit support requested but library not found]) + ]) + ]) ]) -- 2.47.2