fix history
authorLaurent Mazet <mazet@softndesign.org>
Thu, 19 Jan 2023 22:16:53 +0000 (23:16 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Thu, 19 Jan 2023 22:16:53 +0000 (23:16 +0100)
calc.c

diff --git a/calc.c b/calc.c
index ecb01c3a6ba0ae3e1d2bb5bdb9cb516d2c3bd6a3..3012267d7eb0f5a18c925f82122d787d5949dc77 100644 (file)
--- 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 */