X-Git-Url: https://git.draconx.ca/gitweb/cdecl99.git/blobdiff_plain/a0976f87118cc8d9ccc7a79f2ef29935798284e1..4251b4bd0087752ceddbf95a6a8c928ab2207844:/src/cdecl99.c diff --git a/src/cdecl99.c b/src/cdecl99.c index 5ddc889..f7eed62 100644 --- a/src/cdecl99.c +++ b/src/cdecl99.c @@ -29,7 +29,6 @@ #include #include -#include #include #include @@ -40,6 +39,24 @@ #include "copysym.h" #include "options.h" +#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; @@ -107,27 +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(void) -{ - char *line; - - for (; (line = readline("> ")); free(line)) { - if (!is_blank_line(line)) - add_history(line); - - if (run_command(line, true) > 0) - break; - } - - free(line); - return 0; -} - static int repl_cmdline(unsigned count, char **commands) { int ret = 0; @@ -144,32 +140,58 @@ static int repl_cmdline(unsigned count, char **commands) return ret; } -static int repl_noninteractive(void) +static int do_getline(char **linebuf, size_t *n) { - int rc, ret = 0, saved_errno; - char *line = NULL; - size_t n; + ssize_t rc; - while (getline(&line, &n, stdin) >= 0) { - char *c = strchr(line, '\n'); - if (c) - *c = '\0'; + if ((rc = getline(linebuf, n, stdin)) < 0) { + if (ferror(stdin)) + print_error("%s", strerror(errno)); + return 0; + } - rc = run_command(line, false); - if (rc < 0) - ret = -1; - else if (rc > 0) - break; + if (rc-- && (*linebuf)[rc] == '\n') + (*linebuf)[rc] = '\0'; + return 1; +} + +static int do_readline(char **linebuf, size_t *n, int interactive) +{ +#if !HAVE_READLINE + if (interactive) { + fputs("> ", stdout); + fflush(stdout); } - saved_errno = errno; - free(line); + return do_getline(linebuf, n); +#else + if (!interactive) + return do_getline(linebuf, n); - if (ferror(stdin)) { - print_error("%s", strerror(saved_errno)); - return -1; + free(*linebuf); + if (!(*linebuf = readline("> "))) + return 0; + + do_add_history(*linebuf); + return 1; +#endif +} + +static int repl(int interactive) +{ + char *line = NULL; + int ret = 0; + size_t n; + + while (do_readline(&line, &n, interactive)) { + int rc = run_command(line, interactive); + if (rc > 0) + break; + else if (rc < 0) + ret = -!interactive; } + free(line); return ret; } @@ -270,12 +292,10 @@ int main(int argc, char **argv) case INIT_EXIT_FAILURE: return EXIT_FAILURE; } - if (interactive) - rc = repl(); - else if (execute) + if (execute) rc = repl_cmdline(execute, argv); else - rc = repl_noninteractive(); + rc = repl(interactive); if (rc != 0) return EXIT_FAILURE;