Code

sysdb: Check for and use libedit/libreadline for command input.
authorSebastian Harl <sh@tokkee.org>
Fri, 13 Dec 2013 13:31:22 +0000 (14:31 +0100)
committerSebastian Harl <sh@tokkee.org>
Fri, 13 Dec 2013 13:31:22 +0000 (14:31 +0100)
configure.ac
src/Makefile.am
src/client/sysdb.c

index dde772705dc6a913524116a4e4399dd044f0b823..aedf6e8f1a907a943a86ae6a0863ead260bb3cdc 100644 (file)
@@ -184,6 +184,9 @@ if test "x$enable_strict_checks" = "xyes"; then
 fi
 AC_SUBST([STRICT_CFLAGS])
 
+m4_divert_once([HELP_ENABLE], [
+Build dependencies:])
+
 dnl Testing.
 PKG_CHECK_MODULES([CHECK], [check >= 0.9.4],
                [build_testing="yes"], [build_testing="no"])
@@ -219,6 +222,82 @@ if test "x$have_fopencookie" = "xyes"; then
        AC_DEFINE([HAVE_FOPENCOOKIE], 1)
 fi
 
+dnl readline support
+AC_ARG_WITH([readline],
+               [AS_HELP_STRING([--with-readline],
+                       [readline support (libedit/libreadline) (default: auto, prefer libedit)])],
+               [readline_support="$withval"],
+               [readline_support="auto"])
+
+if test "x$readline_support" = "xyes"; then
+       readline_support="auto"
+fi
+
+have_libedit="no"
+if test "x$readline_support" = "xauto" \
+               || test "x$readline_support" = "xlibedit"; then
+       PKG_CHECK_MODULES([LIBEDIT], [libedit],
+                       [have_libedit="yes"], [have_libedit="no"])
+       if test "x$have_libedit" = "xyes"; then
+               AC_CHECK_HEADERS([editline/readline.h], [],
+                               [AC_CHECK_HEADERS([readline.h], [],
+                                               [have_libedit="no (readline header not found"])])
+       fi
+       if test "x$have_libedit" = "xyes"; then
+               AC_CHECK_HEADERS([editline/history.h], [],
+                               [AC_CHECK_HEADERS([history.h], [],
+                                               [have_libedit="no (history header not found"])])
+       fi
+else
+       have_libedit="disabled on command-line"
+fi
+
+have_libreadline="no"
+if test "x$have_libedit" = "xno"; then
+       if test "x$readline_support" = "xauto" \
+                       || test "x$readline_support" = "xlibreadline"; then
+               AC_CHECK_LIB([readline], [readline],
+                               [have_libreadline="yes"],
+                               [have_libreadline="no (libreadline or symbol 'readline' not found)"])
+       fi
+       if test "x$have_libreadline" = "xyes"; then
+               AC_CHECK_HEADERS([readline/readline.h], [],
+                               [AC_CHECK_HEADERS([readline.h], [],
+                                               [have_libreadline="no (readline header not found"])])
+       fi
+       if test "x$have_libreadline" = "xyes"; then
+               AC_CHECK_HEADERS([readline/history.h], [],
+                               [AC_CHECK_HEADERS([history.h], [],
+                                               [have_libreadline="no (history header not found"])])
+       fi
+else
+       have_libreadline="unchecked (prefer libedit)"
+fi
+
+if test "x$have_libedit" = "xyes"; then
+       READLINE_LIBS="$LIBEDIT_LIBS"
+       READLINE_CFLAGS="$LIBEDIT_CFLAGS"
+       readline_support="libedit"
+else if test "x$have_libreadline" = "xyes"; then
+       READLINE_LIBS="-lreadline -lhistory"
+       READLINE_CFLAGS=""
+       readline_support="libreadline"
+else
+       READLINE_LIBS=""
+       READLINE_CFLAGS=""
+       if test "x$readline_support" = "xno"; then
+               AC_MSG_WARN([*** readline support disabled; disabling SysDB client])
+       else if test "x$readline_support" = "xauto"; then
+               AC_MSG_WARN([*** readline not found; disabling SysDB client])
+       else
+               AC_MSG_ERROR([readline not found])
+       fi; fi
+       readline_support="no"
+fi; fi
+AC_SUBST([READLINE_LIBS])
+AC_SUBST([READLINE_CFLAGS])
+AM_CONDITIONAL([BUILD_CLIENT], test "x$readline_support" != "no")
+
 dnl Feature checks.
 build_documentation="yes"
 
@@ -296,6 +375,8 @@ AC_MSG_RESULT([      stdio mocking:  . . . . . $have_fopencookie])
 AC_MSG_RESULT()
 AC_MSG_RESULT([  Libraries:])
 AC_MSG_RESULT([    libdbi: . . . . . . . . . . $with_libdbi])
+AC_MSG_RESULT([    libedit:  . . . . . . . . . $have_libedit])
+AC_MSG_RESULT([    libreadline:  . . . . . . . $have_libreadline])
 AC_MSG_RESULT()
 AC_MSG_RESULT([  Backends:])
 AC_MSG_RESULT([    collectd: . . . . . . . . . $enable_collectd])
index e547fa4b543dc873b94b420218303279d3d373ea..3b1de0aeb797532ee20615482151dda044a28542 100644 (file)
@@ -79,11 +79,15 @@ libsysdb_la_SOURCES += \
 libsysdb_la_LIBADD += -ldbi
 endif
 
-bin_PROGRAMS = sysdb sysdbd
+bin_PROGRAMS = sysdbd
+if BUILD_CLIENT
+bin_PROGRAMS += sysdb
 
 sysdb_SOURCES = client/sysdb.c include/client/sysdb.h
-sysdb_CFLAGS = $(AM_CFLAGS) -DBUILD_DATE="\"$$( date --utc '+%F %T' ) (UTC)\""
-sysdb_LDADD = libsysdbclient.la
+sysdb_CFLAGS = -DBUILD_DATE="\"$$( date --utc '+%F %T' ) (UTC)\"" \
+               $(AM_CFLAGS) @READLINE_CFLAGS@
+sysdb_LDADD = libsysdbclient.la @READLINE_LIBS@
+endif
 
 sysdbd_SOURCES = daemon/sysdbd.c include/sysdb.h \
                daemon/config.c include/daemon/config.h
index c2b542d458e3aa2bbb1b4d5687f801a2b8d1a914..7283a21ae9ce42321e33885b17e6d3775894c307 100644 (file)
 
 #include <pwd.h>
 
+#if HAVE_EDITLINE_READLINE_H
+#      include <editline/readline.h>
+#      if HAVE_EDITLINE_HISTORY_H
+#              include <editline/history.h>
+#      endif
+#elif HAVE_READLINE_READLINE_H
+#      include <readline/readline.h>
+#      if HAVE_READLINE_HISTORY_H
+#              include <readline/history.h>
+#      endif
+#elif HAVE_READLINE_H
+#      include <readline.h>
+#      if HAVE_HISTORY_H
+#              include <history.h>
+#      endif
+#endif /* READLINEs */
+
 #ifndef DEFAULT_SOCKET
 #      define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock"
 #endif
@@ -117,6 +134,31 @@ get_current_user(void)
        return result->pw_name;
 } /* get_current_user */
 
+static const char *
+get_homedir(const char *username)
+{
+       struct passwd pw_entry;
+       struct passwd *result = NULL;
+
+       /* needs to be static because we return a pointer into this buffer
+        * to the caller */
+       static char buf[1024];
+
+       int status;
+
+       memset(&pw_entry, 0, sizeof(pw_entry));
+       status = getpwnam_r(username, &pw_entry, buf, sizeof(buf), &result);
+
+       if (status || (! result)) {
+               char errbuf[1024];
+               sdb_log(SDB_LOG_WARNING, "Failed to determine home directory "
+                               "for user %s: %s", username,
+                               sdb_strerror(errno, errbuf, sizeof(errbuf)));
+               return NULL;
+       }
+       return result->pw_dir;
+} /* get_homedir */
+
 int
 main(int argc, char **argv)
 {
@@ -125,6 +167,9 @@ main(int argc, char **argv)
        const char *host = NULL;
        const char *user = NULL;
 
+       const char *homedir;
+       char hist_file[1024] = "";
+
        while (42) {
                int opt = getopt(argc, argv, "H:U:hV");
 
@@ -175,6 +220,42 @@ main(int argc, char **argv)
        sdb_log(SDB_LOG_INFO, "SysDB client "SDB_CLIENT_VERSION_STRING
                        SDB_CLIENT_VERSION_EXTRA"\n");
 
+       using_history();
+
+       if ((homedir = get_homedir(user))) {
+               snprintf(hist_file, sizeof(hist_file) - 1,
+                               "%s/.sysdb_history", homedir);
+               hist_file[sizeof(hist_file) - 1] = '\0';
+
+               errno = 0;
+               if (read_history(hist_file)) {
+                       char errbuf[1024];
+                       sdb_log(SDB_LOG_WARNING, "Failed to load history (%s): %s",
+                                       hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
+               }
+       }
+
+       while (42) {
+               char *line = readline("sysdb> ");
+
+               if (! line)
+                       break;
+               if (*line == '\0')
+                       continue;
+
+               if (*line != ' ')
+                       add_history(line);
+       }
+
+       if (hist_file[0] != '\0') {
+               errno = 0;
+               if (write_history(hist_file)) {
+                       char errbuf[1024];
+                       sdb_log(SDB_LOG_WARNING, "Failed to store history (%s): %s",
+                                       hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
+               }
+       }
+
        sdb_client_destroy(client);
        return 0;
 } /* main */