Commit 9764cc4e authored by Tollef Fog Heen's avatar Tollef Fog Heen

Add libedit support to varnishadm

Optional libedit support to varnishadm.  No saving of history or
completion yet.
parent 09e0e8ec
...@@ -9,11 +9,13 @@ dist_man_MANS = varnishadm.1 ...@@ -9,11 +9,13 @@ dist_man_MANS = varnishadm.1
varnishadm_SOURCES = \ varnishadm_SOURCES = \
varnishadm.c varnishadm.c
varnishadm_CFLAGS = @LIBEDIT_CFLAGS@
varnishadm_LDADD = \ varnishadm_LDADD = \
$(top_builddir)/lib/libvarnish/libvarnish.la \ $(top_builddir)/lib/libvarnish/libvarnish.la \
$(top_builddir)/lib/libvarnishapi/libvarnishapi.la \ $(top_builddir)/lib/libvarnishapi/libvarnishapi.la \
$(top_builddir)/lib/libvarnishcompat/libvarnishcompat.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 varnishadm.1: $(top_srcdir)/doc/sphinx/reference/varnishadm.rst
if HAVE_RST2MAN if HAVE_RST2MAN
......
...@@ -38,9 +38,12 @@ SVNID("$Id$") ...@@ -38,9 +38,12 @@ SVNID("$Id$")
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <sys/socket.h> #include <sys/socket.h>
#ifdef HAVE_LIBEDIT
#include <editline/readline.h>
#endif
#include "cli.h" #include "cli.h"
#include "cli_common.h" #include "cli_common.h"
#include "libvarnish.h" #include "libvarnish.h"
...@@ -154,6 +157,19 @@ do_args(int sock, int argc, char * const *argv) ...@@ -154,6 +157,19 @@ do_args(int sock, int argc, char * const *argv)
exit(1); 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 * No arguments given, simply pass bytes on stdin/stdout and CLI socket
* Send a "banner" to varnish, to provoke a welcome message. * Send a "banner" to varnish, to provoke a welcome message.
...@@ -165,6 +181,16 @@ pass(int sock) ...@@ -165,6 +181,16 @@ pass(int sock)
char buf[1024]; char buf[1024];
int i, n, m; 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"); cli_write(sock, "banner\n");
fds[0].fd = sock; fds[0].fd = sock;
fds[0].events = POLLIN; fds[0].events = POLLIN;
...@@ -182,13 +208,21 @@ pass(int sock) ...@@ -182,13 +208,21 @@ pass(int sock)
exit (0); exit (0);
} }
assert(n > 0); assert(n > 0);
/* Get rid of the prompt, kinda hackish */
write(1, "\r \r", 13);
m = write(1, buf, n); m = write(1, buf, n);
if (n != m) { if (n != m) {
perror("Write error writing stdout"); perror("Write error writing stdout");
exit (1); exit (1);
} }
#ifdef HAVE_LIBEDIT
rl_forced_update_display();
#endif
} }
if (fds[1].revents & POLLIN) { if (fds[1].revents & POLLIN) {
#ifdef HAVE_LIBEDIT
rl_callback_read_char();
#else
n = read(fds[1].fd, buf, sizeof buf); n = read(fds[1].fd, buf, sizeof buf);
if (n == 0) { if (n == 0) {
AZ(shutdown(sock, SHUT_WR)); AZ(shutdown(sock, SHUT_WR));
...@@ -196,12 +230,10 @@ pass(int sock) ...@@ -196,12 +230,10 @@ pass(int sock)
} else if (n < 0) { } else if (n < 0) {
exit(0); exit(0);
} else { } else {
m = write(sock, buf, n); buf[n] = '\0';
if (n != m) { cli_write(sock, buf);
perror("Write error writing CLI socket");
exit (1);
}
} }
#endif
} }
} }
} }
......
...@@ -121,6 +121,9 @@ fi ...@@ -121,6 +121,9 @@ fi
AC_SUBST(PCRE_CFLAGS) AC_SUBST(PCRE_CFLAGS)
AC_SUBST(PCRE_LIBS) 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. # Checks for header files.
AC_HEADER_STDC AC_HEADER_STDC
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment