From 4338271f1a9dbea2e2bbbf8f40821dd85e049166 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Tue, 4 Dec 2007 18:27:52 +0000 Subject: [PATCH] Tue Dec 4 17:47:01 UTC 2007 Richard W.M. Jones * configure.in: curses is not actually required to build libvirt * configure.in, src/virsh.c: Make readline optional. If not available then virsh is built without support for command line editing. --- ChangeLog | 7 +++++++ configure.in | 10 ++-------- src/virsh.c | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 810c0109a1..e687305851 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Tue Dec 4 17:47:01 UTC 2007 Richard W.M. Jones + + * configure.in: curses is not actually required to build libvirt + * configure.in, src/virsh.c: Make readline optional. If not + available then virsh is built without support for command + line editing. + Mon Dec 3 20:44:01 CET 2007 Jim Meyering Avoid compile failure when HAVE_AVAHI is not defined. diff --git a/configure.in b/configure.in index a8457202d6..6297483063 100644 --- a/configure.in +++ b/configure.in @@ -383,16 +383,10 @@ AC_SUBST(AVAHI_CFLAGS) AC_SUBST(AVAHI_LIBS) dnl virsh libraries -AC_CHECK_LIB(curses, initscr, - [VIRSH_LIBS="$VIRSH_LIBS -lcurses"], - [AC_CHECK_LIB(ncurses, initscr, - [VIRSH_LIBS="$VIRSH_LIBS -lncurses"], - [AC_MSG_ERROR([curses/ncurses library not found])], - [$VIRSH_LIBS])], - [$VIRSH_LIBS]) +AC_CHECK_HEADERS([readline/readline.h]) AC_CHECK_LIB(readline, main, [VIRSH_LIBS="$VIRSH_LIBS -lreadline"], - [AC_MSG_ERROR([readline library not found])], + [AC_MSG_WARN([readline library not found])], [$VIRSH_LIBS]) AC_SUBST(VIRSH_LIBS) diff --git a/src/virsh.c b/src/virsh.c index ccd85be0e1..207a0ddc72 100644 --- a/src/virsh.c +++ b/src/virsh.c @@ -13,6 +13,8 @@ * $Id$ */ +#include "config.h" + #include "libvirt/libvirt.h" #include "libvirt/virterror.h" #include @@ -39,10 +41,11 @@ #include #include +#ifdef HAVE_READLINE_READLINE_H #include #include +#endif -#include "config.h" #include "internal.h" #include "console.h" @@ -4656,6 +4659,8 @@ vshCloseLogFile(vshControl *ctl) } } +#ifdef HAVE_READLINE_READLINE_H + /* ----------------- * Readline stuff * ----------------- @@ -4773,6 +4778,41 @@ vshReadlineInit(void) rl_attempted_completion_function = vshReadlineCompletion; } +static char * +vshReadline (vshControl *ctl ATTRIBUTE_UNUSED, const char *prompt) +{ + return readline (prompt); +} + +#else /* !HAVE_READLINE_READLINE_H */ + +static void +vshReadlineInit (void) +{ + /* empty */ +} + +static char * +vshReadline (vshControl *ctl, const char *prompt) +{ + char line[1024]; + char *r; + int len; + + fputs (prompt, stdout); + r = fgets (line, sizeof line, stdin); + if (r == NULL) return NULL; /* EOF */ + + /* Chomp trailing \n */ + len = strlen (r); + if (len > 0 && r[len-1] == '\n') + r[len-1] = '\0'; + + return vshStrdup (ctl, r); +} + +#endif /* !HAVE_READLINE_READLINE_H */ + /* * Deinitliaze virsh */ @@ -5013,11 +5053,13 @@ main(int argc, char **argv) vshReadlineInit(); do { ctl->cmdstr = - readline(ctl->uid == 0 ? VSH_PROMPT_RW : VSH_PROMPT_RO); + vshReadline(ctl, ctl->uid == 0 ? VSH_PROMPT_RW : VSH_PROMPT_RO); if (ctl->cmdstr == NULL) break; /* EOF */ if (*ctl->cmdstr) { +#if HAVE_READLINE_READLINE_H add_history(ctl->cmdstr); +#endif if (vshCommandParse(ctl, ctl->cmdstr)) vshCommandRun(ctl, ctl->cmd); }