From 4251b4bd0087752ceddbf95a6a8c928ab2207844 Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Tue, 4 Jul 2023 21:27:47 -0400 Subject: [PATCH] cdecl99: Don't check blank lines if no history support. Move the whole "if not blank, then add history" logic under the #ifdef, so it can be eliminated completely if add_history is not available at compile time. This probably makes no major difference on GNU/Linux, since GCC knows that strspn has no side effects and can drop the call as dead code, but this may help with less sophisticated toolchains. --- src/cdecl99.c | 22 +++++++++++++++------- src/cdecl99.h | 8 -------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/cdecl99.c b/src/cdecl99.c index d7ddd77..f7eed62 100644 --- a/src/cdecl99.c +++ b/src/cdecl99.c @@ -42,6 +42,20 @@ #if HAVE_READLINE_READLINE_H # include #endif +#if HAVE_RL_ADD_HISTORY && HAVE_READLINE_HISTORY_H +# include + +/* call add_history only if the line is non-blank */ +static void do_add_history(const char *line) +{ + if (line[strspn(line, " \t")]) + add_history(line); +} +#else +static void do_add_history(const char *line) +{ +} +#endif static const char *progname = "cdecl99"; static bool interactive = true; @@ -110,11 +124,6 @@ static void print_help(const struct option *lopts) printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT); } -static int is_blank_line(const char *line) -{ - return !line[strspn(line, " \t")]; -} - static int repl_cmdline(unsigned count, char **commands) { int ret = 0; @@ -163,8 +172,7 @@ static int do_readline(char **linebuf, size_t *n, int interactive) if (!(*linebuf = readline("> "))) return 0; - if (!is_blank_line(*linebuf)) - add_history(*linebuf); + do_add_history(*linebuf); return 1; #endif } diff --git a/src/cdecl99.h b/src/cdecl99.h index 7ed2407..0f36f7b 100644 --- a/src/cdecl99.h +++ b/src/cdecl99.h @@ -29,12 +29,4 @@ int run_command_declare(const char *cmdarg); void print_error(const char *fmt, ...); -#if HAVE_RL_ADD_HISTORY && HAVE_READLINE_HISTORY_H -# include -#else -static inline void add_history(const char *str) -{ -} -#endif - #endif -- 2.43.2