From 3a7a78ff0034cc6f82774a28ba8db6dfe99278e1 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Thu, 19 Jan 2023 23:16:53 +0100 Subject: [PATCH] fix history --- calc.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/calc.c b/calc.c index ecb01c3..3012267 100644 --- a/calc.c +++ b/calc.c @@ -17,6 +17,7 @@ /* constants */ #define BUFFER_SIZE 4096 +#define HISTORY_LEN 10 /* macros */ @@ -126,26 +127,27 @@ int main (int argc, char *argv[]) /* add line into history */ add_history (buffer); - VERBOSE (INFO, fprintf (stdout, "line (%d): '%s'\n", where_history (), buffer)); - if (where_history () == 10) { + VERBOSE (INFO, fprintf (stdout, "line (%d/%d): '%s'\n", + where_history (), history_length, buffer)); + if (history_length > HISTORY_LEN) { HIST_ENTRY *last = remove_history (0); if (last) { free_history_entry (last); } } } else { - if (read (STDIN_FILENO, buffer, BUFFER_SIZE) == 0) { - break; - } - nb = 0; - char *pt = line[nb++] = buffer; - while (*pt++ != '\0') { - if (*pt == '\n') { - *pt = '\0'; - line[nb++] = ++pt; - } - } - VERBOSE (INFO, fprintf (stdout, "line: '%s'\n", buffer)); + if (read (STDIN_FILENO, buffer, BUFFER_SIZE) == 0) { + break; + } + nb = 0; + char *pt = line[nb++] = buffer; + while (*pt++ != '\0') { + if (*pt == '\n') { + *pt = '\0'; + line[nb++] = ++pt; + } + } + VERBOSE (INFO, fprintf (stdout, "line: '%s'\n", buffer)); } /* look for end of line */ -- 2.30.2