From a4f41522fe944c592b11e8e9ee936079a97dda78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20D=C3=B8rum?= Date: Mon, 12 Oct 2020 23:35:59 +0200 Subject: [PATCH] repl: fgets-based readline alternative --- repl.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/repl.c b/repl.c index 6a29d33..ac03cea 100644 --- a/repl.c +++ b/repl.c @@ -1,8 +1,36 @@ #include "tinyexpr.h" #include #include +#include + +#ifndef NO_READLINE #include #include +#else +static char *readline(const char *prompt) { + fprintf(stderr, "%s", prompt); + char buf[1024]; + char *line = fgets(buf, sizeof(buf), stdin); + if (line == NULL && feof(stdin)) { + return NULL; + } else if (line == NULL) { + perror("fgets"); + return NULL; + } + + size_t len = strlen(line); + if (line[len - 1] == '\n') { + line[len - 1] = '\0'; + len -= 1; + } + + line = malloc(len + 1); + strcpy(line, buf); + return line; +} + +static void add_history(const char *line) {} +#endif static int eval(const char *str) { int err = 0;