[pcp] readline support for dbpmda

Andrew Wansink ajwans at evostor.com
Mon Jan 18 01:33:06 CST 2010


commit 28784aaf1e9cfad19f23a9669681a48d91e6c030
Author: Andrew Wansink <ajwans at evostor.com>
Date:   Mon Jan 18 18:30:08 2010 +1100

    Add readline support to dbpdma.

diff --git a/configure.in b/configure.in
index 5554437..cd016db 100644
--- a/configure.in
+++ b/configure.in
@@ -1726,6 +1726,20 @@ int main () {
 }
 ], AC_DEFINE(HAVE_STRFTIME_z) AC_MSG_RESULT(yes) , AC_MSG_RESULT(no),  0)
 
+dnl check if readline needs -lreadline to work
+lib_for_readline=
+AC_CHECK_FUNCS(readline)
+if test $ac_cv_func_readline = no
+then
+    AC_CHECK_LIB(readline, readline)
+    if test $ac_cv_lib_readline_readline = yes
+    then
+	lib_for_readline=-lreadline
+    fi
+fi
+AC_SUBST(lib_for_readline)
+
+
 dnl
 dnl output files
 dnl
diff --git a/src/dbpmda/src/GNUmakefile b/src/dbpmda/src/GNUmakefile
index 859177b..005809b 100644
--- a/src/dbpmda/src/GNUmakefile
+++ b/src/dbpmda/src/GNUmakefile
@@ -27,7 +27,7 @@ LFILES	= lex.l
 YFILES	= gram.y
 
 LDIRT	= *.log foo.* gram.h $(YFILES:%.y=%.tab.?) $(LFILES:%.l=%.c)
-LLDLIBS	= -lpcp $(LIB_FOR_DLOPEN)
+LLDLIBS	= -lpcp $(LIB_FOR_DLOPEN) $(LIB_FOR_READLINE)
 
 default:	$(CMDTARGET)
 
diff --git a/src/dbpmda/src/dbpmda.c b/src/dbpmda/src/dbpmda.c
index 1f42ce2..4205453 100644
--- a/src/dbpmda/src/dbpmda.c
+++ b/src/dbpmda/src/dbpmda.c
@@ -21,6 +21,14 @@
 #include "gram.h"
 #include <ctype.h>
 
+#if HAVE_LIBREADLINE
+#include <readline/readline.h>
+#include <readline/history.h>
+
+struct yy_buffer_state;
+
+#endif
+
 char		*configfile = NULL;
 __pmLogCtl	logctl;
 int		parse_done = 0;
@@ -46,6 +54,11 @@ main(int argc, char **argv)
     int			errflag = 0;
     char		*endnum;
     int			i;
+#if HAVE_LIBREADLINE
+    char		*readline_str;
+    char		str[1024];
+    struct yy_buffer_state *buf;
+#endif
 
     __pmSetProgname(argv[0]);
 
@@ -156,7 +169,23 @@ main(int argc, char **argv)
 
     for ( ; ; ) {
 	initmetriclist();
+
+#if HAVE_LIBREADLINE
+	readline_str = readline("> ");
+	if (readline_str && *readline_str)
+		add_history(readline_str);
+	/* put back the newline that readline stripped off */
+	sprintf(str, "%s\n", readline_str);
+	buf = yy_scan_string(str);
+	yy_switch_to_buffer(buf);
+#endif
+
 	yyparse();
+
+#if HAVE_LIBREADLINE
+	yy_delete_buffer(buf);
+	free(readline_str);
+#endif
 	if (yywrap()) {
 	    if (iflag)
 		putchar('\n');
diff --git a/src/include/builddefs.in b/src/include/builddefs.in
index d934852..abaf8cc 100644
--- a/src/include/builddefs.in
+++ b/src/include/builddefs.in
@@ -181,6 +181,7 @@ LIB_FOR_BASENAME = @lib_for_basename@
 LIB_FOR_DLOPEN = @lib_for_dlopen@
 LIB_FOR_REGEX = @lib_for_regex@
 LIB_FOR_MATH = @lib_for_math@
+LIB_FOR_READLINE = @lib_for_readline@
 
 SHELL = /bin/sh
 IMAGES_DIR = $(TOPDIR)/all-images
diff --git a/src/include/platform_defs.h.in b/src/include/platform_defs.h.in
index 88f39bd..e5948ca 100644
--- a/src/include/platform_defs.h.in
+++ b/src/include/platform_defs.h.in
@@ -137,6 +137,7 @@ extern "C" {
 #undef HAVE_SYS_ENDIAN_H
 #undef HAVE_SYS_MACHINE_H
 #undef HAVE_MACHINE_ENDIAN_H
+#undef HAVE_LIBREADLINE
 
 #if defined(HAVE_MALLOC_H)
 #include <malloc.h>



More information about the pcp mailing list