[master] 9764cc4 Add libedit support to varnishadm

Tollef Fog Heen tfheen at varnish-cache.org
Tue Feb 15 13:52:41 CET 2011


commit 9764cc4ece8c06051d4d9788474afa94d7c55a95
Author: Tollef Fog Heen <tfheen at varnish-software.com>
Date:   Tue Feb 15 13:49:26 2011 +0100

    Add libedit support to varnishadm
    
    Optional libedit support to varnishadm.  No saving of history or
    completion yet.

diff --git a/bin/varnishadm/Makefile.am b/bin/varnishadm/Makefile.am
index f5e12e9..2717acb 100644
--- a/bin/varnishadm/Makefile.am
+++ b/bin/varnishadm/Makefile.am
@@ -9,11 +9,13 @@ dist_man_MANS = varnishadm.1
 varnishadm_SOURCES = \
 	varnishadm.c
 
+varnishadm_CFLAGS = @LIBEDIT_CFLAGS@
+
 varnishadm_LDADD = \
 	$(top_builddir)/lib/libvarnish/libvarnish.la \
 	$(top_builddir)/lib/libvarnishapi/libvarnishapi.la \
 	$(top_builddir)/lib/libvarnishcompat/libvarnishcompat.la \
-	${PTHREAD_LIBS} ${NET_LIBS}
+	${PTHREAD_LIBS} ${NET_LIBS} @LIBEDIT_LIBS@
 
 varnishadm.1: $(top_srcdir)/doc/sphinx/reference/varnishadm.rst
 if HAVE_RST2MAN
diff --git a/bin/varnishadm/varnishadm.c b/bin/varnishadm/varnishadm.c
index 965d8cd..147438e 100644
--- a/bin/varnishadm/varnishadm.c
+++ b/bin/varnishadm/varnishadm.c
@@ -38,9 +38,12 @@ SVNID("$Id$")
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
-
 #include <sys/socket.h>
 
+#ifdef HAVE_LIBEDIT
+#include <editline/readline.h>
+#endif
+
 #include "cli.h"
 #include "cli_common.h"
 #include "libvarnish.h"
@@ -154,6 +157,19 @@ do_args(int sock, int argc, char * const *argv)
 	exit(1);
 }
 
+#ifdef HAVE_LIBEDIT
+/* Callback for readline, doesn't take a private pointer, so we need
+ * to have a global variable.
+ */
+static int _line_sock;
+void send_line(char *l)
+{
+	cli_write(_line_sock, l);
+	cli_write(_line_sock, "\n");
+	add_history(l);
+}
+#endif
+
 /*
  * No arguments given, simply pass bytes on stdin/stdout and CLI socket
  * Send a "banner" to varnish, to provoke a welcome message.
@@ -165,6 +181,16 @@ pass(int sock)
 	char buf[1024];
 	int i, n, m;
 
+#ifdef HAVE_LIBEDIT
+	_line_sock = sock;
+	rl_already_prompted = 1;
+	if (isatty(0)) {
+		rl_callback_handler_install("varnish> ", send_line);
+	} else {
+		rl_callback_handler_install("", send_line);
+	}
+#endif
+
 	cli_write(sock, "banner\n");
 	fds[0].fd = sock;
 	fds[0].events = POLLIN;
@@ -182,13 +208,21 @@ pass(int sock)
 				exit (0);
 			}
 			assert(n > 0);
+			/* Get rid of the prompt, kinda hackish */
+			write(1, "\r           \r", 13);
 			m = write(1, buf, n);
 			if (n != m) {
 				perror("Write error writing stdout");
 				exit (1);
 			}
+#ifdef HAVE_LIBEDIT
+			rl_forced_update_display();
+#endif
 		}
 		if (fds[1].revents & POLLIN) {
+#ifdef HAVE_LIBEDIT
+			rl_callback_read_char();
+#else
 			n = read(fds[1].fd, buf, sizeof buf);
 			if (n == 0) {
 				AZ(shutdown(sock, SHUT_WR));
@@ -196,12 +230,10 @@ pass(int sock)
 			} else if (n < 0) {
 				exit(0);
 			} else {
-				m = write(sock, buf, n);
-				if (n != m) {
-					perror("Write error writing CLI socket");
-					exit (1);
-				}
+				buf[n] = '\0';
+				cli_write(sock, buf);
 			}
+#endif
 		}
 	}
 }
diff --git a/configure.ac b/configure.ac
index 7b17ba6..f574764 100644
--- a/configure.ac
+++ b/configure.ac
@@ -121,6 +121,9 @@ fi
 AC_SUBST(PCRE_CFLAGS)
 AC_SUBST(PCRE_LIBS)
 
+PKG_CHECK_MODULES([LIBEDIT], [libedit], 
+	[AC_DEFINE([HAVE_LIBEDIT], [1], [Define we have libedit])],
+	[AC_MSG_WARN([libedit not found, disabling libedit support])])
 
 # Checks for header files.
 AC_HEADER_STDC



More information about the varnish-commit mailing list