From 680bf70a1786360c2fa09f2360c593b9b174848b Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Wed, 18 Jan 2023 23:53:49 +0100 Subject: [PATCH] use readline --- calc.c | 79 ++++++++++++++++++++++++++------------------------------ makefile | 2 +- 2 files changed, 38 insertions(+), 43 deletions(-) diff --git a/calc.c b/calc.c index f57f1a4..0f8604c 100644 --- a/calc.c +++ b/calc.c @@ -1,11 +1,14 @@ /* depend: */ /* cflags: */ -/* linker: atoi.o debug.o fdprintf.o parser.o -lm */ +/* linker: atoi.o debug.o fdprintf.o parser.o -lm -lreadline */ -//#include +#include +#include +#include #include #include -#include +//#include +//#include #include "atoi.h" #include "debug.h" @@ -14,9 +17,6 @@ /* constants */ -//#define BUFFER_SIZE 4096 -#define BUFFER_SIZE 256 - /* macros */ #define CEIL(x, y) (((x) + (y) - 1) / (y)) @@ -43,11 +43,10 @@ int usage (int ret) /* main function */ -int main (int argc, char *argv[]) +int main (int argc, char *argv[]) { - char buffer[BUFFER_SIZE + 1] = {0}; - char *pt = buffer; - int i = 0, j = 0, n; + char *buffer; + int i = 0; int ret = 0; /* program name */ @@ -93,50 +92,46 @@ int main (int argc, char *argv[]) return usage (c != 'h'); } } - + /* format */ char format[8] = "=> %.f\n"; format[4] = '0' + precision; /* read from input stream */ - while ((n = read (stdfdin, pt, BUFFER_SIZE - (pt - buffer))) != 0) { - VERBOSE (INFO, PRINTOUT ("read %d bytes\n", n)); - n += (pt - buffer); - if ((n == 2) && (buffer[0] == '.')) { + while ((buffer = readline ("> ")) != NULL) { + + /* check empty line */ + if (strlen (buffer) == 0) { + free (buffer); + continue; + } else if (strcmp (buffer, ".") == 0) { return 0; } - /* look for end of line */ - for (i = 0, j = 0; i < n; i++) { - if (buffer[i] == '\n') { - buffer[i] = 0; - VERBOSE (INFO, PRINTOUT ("line(%d): %s\n", j, buffer + j)); - element_t *element = parser (buffer + j, NULL, 0); - if (element == ERROR_OP) { - VERBOSE (WARNING, PRINTOUT ("error while parsing: %s\n", buffer)); - ret = 1; - } else { - VERBOSE (INFO, print_element (element, 0)); - PRINTOUT (format, evaluate_element (element, 0)); - delelement (element); - ret = 0; - } - //fsync (stdfdout); - j = i + 1; + /* add line into history */ + add_history (buffer); + VERBOSE (INFO, PRINTOUT ("line (%d): %s\n", where_history (), buffer)); + if (where_history () == 10) { + HIST_ENTRY *last = remove_history (0); + if (last) { + free_history_entry (last); } } - /* keep remainding */ - if (j < n) { - for (i = 0; i < n - j; i++) { - buffer[i] = buffer[i + j]; - } - pt = buffer + n - j; - for (i = n - j; i < BUFFER_SIZE; i++) { - buffer[i] = 0; - } + /* look for end of line */ + element_t *element = parser (buffer, NULL, 0); + if (element == ERROR_OP) { + VERBOSE (WARNING, PRINTOUT ("error while parsing: %s\n", buffer)); + ret = 1; + } else { + VERBOSE (INFO, print_element (element, 0)); + PRINTOUT (format, evaluate_element (element, 0)); + delelement (element); + ret = 0; } + + free (buffer); } return ret; @@ -193,7 +188,7 @@ int main (int argc, char *argv[]) // test: echo "pow (2)" | calc.exe | grep -q 'error' // test: echo "1.23456789" | calc.exe -p 3 | grep -q '1\.234' // test: echo . | calc.exe -// test: echo -e '1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1\n1 + 1' | ./calc.exe | grep -q 6.4e1 +// test: echo -e '1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1\n1 + 1' | calc.exe | grep -q 6.4e1 // test: echo -e '-cos (1)\n1 + 1\n1 - 1\n1 * 1\n1 / 1\n3%2\n2^2\nsqrt (2)\ncos (0)\nsin (0)\natan (0)\nlog (1)\nexp (1)\nhelp\nquit' | calc.exe -v 3 | grep -q bye // test: echo -e '1 +\n1 -\n1 * 1\n1 /\n3%\n2^\nsqrt ()\ncos ()\nsin ()\natan ()\nlog ()\nexp ()\n1 + (' | calc.exe |grep -c error |xargs test 11 = diff --git a/makefile b/makefile index 08034de..fe42f87 100644 --- a/makefile +++ b/makefile @@ -131,7 +131,7 @@ valgrind_%: %.exe %.exe: %.o %.d $(call TITLE, "Building $@") - $(CC) $(LDFLAGS) $(shell ./getcomments.pl -p='linker:\s' -f='%' ${<:.o=.c}) $< -o $@ + $(CC) $(LDFLAGS) $< $(shell ./getcomments.pl -p='linker:\s' -f='%' ${<:.o=.c}) -o $@ $(call PASS, SUCCESS) ## Phony -- 2.30.2