repl: fgets-based readline alternative

This commit is contained in:
Martin Dørum
2020-10-12 23:35:59 +02:00
parent cf49088b99
commit a4f41522fe

28
repl.c
View File

@@ -1,8 +1,36 @@
#include "tinyexpr.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifndef NO_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#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;