From 99cde35bb58605e86541acd812860c222e375af2 Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Wed, 29 Aug 2012 20:29:03 -0400 Subject: [PATCH] Add basic readline history support. Bump gnulib as later versions include a free check for history.h already, then implement a wrapper for add_history that should work even when support is not available. --- Makefile.am | 4 ++-- configure.ac | 19 +++++++++++++++++++ gnulib | 2 +- m4/.gitignore | 2 ++ m4/gnulib-cache.m4 | 4 ++-- src/cdecl99.c | 39 +++++++++++++++++++++++++++------------ src/history.h | 32 ++++++++++++++++++++++++++++++++ 7 files changed, 85 insertions(+), 17 deletions(-) create mode 100644 src/history.h diff --git a/Makefile.am b/Makefile.am index 8634a09..ecfba9b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -35,8 +35,8 @@ dist_man_MANS = doc/man/cdecl99.1 doc/man/libcdecl.3 include_HEADERS = src/cdecl.h noinst_HEADERS = conf_pre.h conf_post.h src/typemap.h src/output.h src/scan.h \ - src/parse.h src/i18n.h src/normalize.h src/error.h test/declgen.h \ - test/test.h + src/parse.h src/i18n.h src/normalize.h src/error.h src/history.h \ + test/declgen.h test/test.h noinst_DATA = $(MOFILES) diff --git a/configure.ac b/configure.ac index 3be3127..0b9bab1 100644 --- a/configure.ac +++ b/configure.ac @@ -18,6 +18,25 @@ gl_EARLY LT_INIT gl_INIT +AC_CACHE_CHECK([if readline supports add_history], [dx_cv_rl_add_history], [dnl +dx_cv_rl_add_history=no +dx_save_libs=$LIBS +LIBS="$LIBS $LIBREADLINE" +AC_LINK_IFELSE([AC_LANG_PROGRAM([dnl +#include +#if HAVE_READLINE_HISTORY_H +# include +#endif +], [dnl +(*add_history)(""); +])], [dx_cv_rl_add_history=yes], [dx_cv_rl_add_history=no]) +LIBS=$dx_save_libs +]) + +AS_IF([test x"$dx_cv_rl_add_history" = x"yes"], + [AC_DEFINE([HAVE_RL_ADD_HISTORY], [1], + [Define to 1 if readline supports add_history.])]) + m4_include([lib/gnulib.mk]) DX_GLSYM_PREFIX([cdecl__]) diff --git a/gnulib b/gnulib index d63fa9d..8a73c1c 160000 --- a/gnulib +++ b/gnulib @@ -1 +1 @@ -Subproject commit d63fa9d4fe8791bca39b5108c9970e70016e7a21 +Subproject commit 8a73c1c5c8b04143c6583f2413cc46b0035a3384 diff --git a/m4/.gitignore b/m4/.gitignore index ae8c6f2..18e8714 100644 --- a/m4/.gitignore +++ b/m4/.gitignore @@ -37,6 +37,7 @@ malloc.m4 multiarch.m4 nls.m4 nocrash.m4 +off_t.m4 po.m4 printf-posix.m4 progtest.m4 @@ -49,6 +50,7 @@ stdint.m4 stdint_h.m4 stdio_h.m4 stdlib_h.m4 +sys_types_h.m4 thread.m4 threadlib.m4 tls.m4 diff --git a/m4/gnulib-cache.m4 b/m4/gnulib-cache.m4 index 05cb7ff..f5b9f5a 100644 --- a/m4/gnulib-cache.m4 +++ b/m4/gnulib-cache.m4 @@ -1,4 +1,4 @@ -# Copyright (C) 2002-2011 Free Software Foundation, Inc. +# Copyright (C) 2002-2012 Free Software Foundation, Inc. # # This file is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -50,5 +50,5 @@ gl_CONDITIONAL_DEPENDENCIES gl_LIBTOOL gl_MACRO_PREFIX([gl]) gl_PO_DOMAIN([]) -gl_WITNESS_C_DOMAIN([]) +gl_WITNESS_C_MACRO([]) gl_VC_FILES([false]) diff --git a/src/cdecl99.c b/src/cdecl99.c index d58433b..99b5d63 100644 --- a/src/cdecl99.c +++ b/src/cdecl99.c @@ -1,31 +1,33 @@ /* - * Command line utility for making sense of C declarations. - * Copyright © 2011 Nick Bowler + * Command line utility for making sense of C declarations. + * Copyright © 2011-2012 Nick Bowler * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ #include #include #include #include #include +#include #include #include #include #include #include #include "readline.h" +#include "history.h" #include "cdecl.h" #define _(x) gettext(x) @@ -398,11 +400,24 @@ static int run_command(const char *line) return -1; } +static bool is_blank_line(const char *line) +{ + for (size_t i = 0; line[i]; i++) { + if (!isblank((unsigned char)line[i])) + return false; + } + + return true; +} + static int repl(void) { char *line; for (; (line = readline("> ")); free(line)) { + if (!is_blank_line(line)) + cdecl_add_history(line); + if (!run_command(line)) break; } diff --git a/src/history.h b/src/history.h new file mode 100644 index 0000000..2d519ee --- /dev/null +++ b/src/history.h @@ -0,0 +1,32 @@ +/* + * Simple wrappers around libhistory to stub out unavailable functions. + * Copyright © 2012 Nick Bowler + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#if HAVE_READLINE_HISTORY_H +# include +#endif + +#if HAVE_RL_ADD_HISTORY +static inline void cdecl_add_history(const char *str) +{ + add_history(str); +} +#else +static inline void cdecl_add_history(const char *str) +{ +} +#endif -- 2.43.0