From 6344b1c057ee93e8c24480278f954f2932cc2a4a Mon Sep 17 00:00:00 2001 From: tmeissner Date: Thu, 2 May 2019 14:22:09 +0200 Subject: [PATCH] use readline instead of fputs() & fgets() --- chapter_04/Makefile | 2 +- chapter_04/prompt.c | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/chapter_04/Makefile b/chapter_04/Makefile index fd95b78..b4af8a5 100644 --- a/chapter_04/Makefile +++ b/chapter_04/Makefile @@ -2,7 +2,7 @@ all: prompt %: %.c - cc -std=c11 -Wall $@.c -o $@ + cc -std=c11 -Wall $@.c -ledit -o $@ .PHONY: clean diff --git a/chapter_04/prompt.c b/chapter_04/prompt.c index 10c8048..5df22c0 100644 --- a/chapter_04/prompt.c +++ b/chapter_04/prompt.c @@ -1,8 +1,7 @@ #include +#include - -/* Declare a buffer for user input of size 2048 */ -static char input[2048]; +#include int main(int argc, char const *argv[]) @@ -13,14 +12,17 @@ int main(int argc, char const *argv[]) /* In a never ending loop */ while (1) { - /* Output our prompt */ - fputs("lispy> ", stdout); + /* Output our prompt and get input */ + char* input = readline("lispy> "); - /* Read a line of user input of max size 2014 */ - fgets(input, 2048, stdin); + /* Add input to history */ + add_history(input); /* Echo input back to user */ - printf("No you're a %s", input); + printf("No you're a %s\n", input); + + /* Free retrieved input */ + free(input); } return 0;