readline can be desactivated
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Thu, 19 Jan 2023 15:40:05 +0000 (16:40 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Thu, 19 Jan 2023 15:40:05 +0000 (16:40 +0100)
atoi.c [deleted file]
atoi.h [deleted file]
calc.c

diff --git a/atoi.c b/atoi.c
deleted file mode 100644 (file)
index 50ec270..0000000
--- a/atoi.c
+++ /dev/null
@@ -1,23 +0,0 @@
-#include "atoi.h"
-
-int atoi (char *str)
-{
-    int i = 0;
-    int s = 1;
-
-    if (*str == '-') {
-        s = 0;
-        str++;
-    }
-
-    while (*str != 0) {
-        if ((*str <'0') || (*str > '9')) {
-            return 0;
-        }
-        i = i * 10 + (int)(*(str++) - '0');
-    }
-
-    return (s) ? i : -i;
-}
-
-/* vim: set ts=4 sw=4 et: */
diff --git a/atoi.h b/atoi.h
deleted file mode 100644 (file)
index 11b8a46..0000000
--- a/atoi.h
+++ /dev/null
@@ -1,8 +0,0 @@
-#ifndef __ATOI_H__
-#define __ATOI_H__
-
-int atoi (char *str);
-
-#endif /* __ATOI_H__ */
-
-/* vim: set ts=4 sw=4 et */
diff --git a/calc.c b/calc.c
index 749280c38b3bd4dad1e8f82606e96bd1b1a7a143..d36111580c42dd49f1688850cc8aa732466bea9b 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -1,22 +1,23 @@
 /* depend: */
 /* cflags: */
-/* linker: atoi.o debug.o fdprintf.o parser.o -lm -lreadline */
+/* linker: debug.o fdprintf.o parser.o -lm -lreadline */
 
 #include <malloc.h>
-#include <readline/readline.h>
-#include <readline/history.h>
 #include <stddef.h>
 #include <stdio.h>
-//#include <stdlib.h>
-//#include <unistd.h>
+#include <unistd.h>
+
+#include <readline/readline.h>
+#include <readline/history.h>
 
-#include "atoi.h"
 #include "debug.h"
 #include "fdprintf.h"
 #include "parser.h"
 
 /* constants */
 
+#define BUFFER_SIZE 4096
+
 /* macros */
 
 #define CEIL(x, y) (((x) + (y) - 1) / (y))
@@ -26,6 +27,7 @@
 /* gobal variables */
 
 char *progname = NULL;
+int mode = 1;
 int precision = 6;
 
 /* help function */
@@ -35,6 +37,7 @@ int usage (int ret)
     int fd = ret ? stdfderr : stdfdout;
     fdprintf (fd, "usage: %s\n", progname);
     fdprintf (fd, " -h : help message\n");
+    fdprintf (fd, " -n : no readline mode (%s)\n", mode ? "yes" : "no");
     fdprintf (fd, " -p : precision (%d)\n", precision);
     fdprintf (fd, " -v : verbose level (%d)\n", verbose);
 
@@ -45,8 +48,9 @@ int usage (int ret)
 
 int main (int argc, char *argv[])
 {
-    char *buffer;
-    int i = 0;
+    char *buffer = NULL;
+    char buffer_static[BUFFER_SIZE + 1] = {0};
+    int i = 0, nb = 1;
     int ret = 0;
 
     /* program name */
@@ -71,6 +75,10 @@ int main (int argc, char *argv[])
         }
         char c = arg[1];
         switch (c) {
+        case 'n':
+            mode = 0;
+            buffer = buffer_static;
+            break;
         case 'p':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
             if (arg == NULL) {
@@ -99,39 +107,69 @@ int main (int argc, char *argv[])
 
     /* read from input stream */
 
-    while ((buffer = readline ("<= ")) != NULL) {
+    while (1) {
+        char *line[BUFFER_SIZE] = {0};
 
-        /* check empty line */
-        if (strlen (buffer) == 0) {
-            free (buffer);
-            continue;
-        } else if (strcmp (buffer, ".") == 0) {
-            return 0;
-        }
+        if (mode) {
+            if ((buffer = readline ("<= ")) == NULL) {
+                break;
+            }
 
-        /* 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);
+            /* check empty line */
+            if (strlen (buffer) == 0) {
+                free (buffer);
+                continue;
+            } else if (strcmp (buffer, ".") == 0) {
+                break;
+            }
+            line[0] = buffer;
+
+            /* 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);
+                }
             }
+        } else {
+           if (read (stdfdin, 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, PRINTOUT ("line: '%s'\n", buffer));
         }
 
         /* 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;
+        for (i = 0; (i < nb) && (ret != 1); i++) {
+            if (*line[i] == '\0') {
+                continue;
+            }
+            element_t *element = parser (line[i], NULL, 0);
+            if (element == ERROR_OP) {
+                VERBOSE (WARNING, PRINTOUT ("error while parsing: %s\n", line[i]));
+                ret = 1;
+            } else {
+                VERBOSE (INFO, print_element (element, 0));
+                PRINTOUT (format, evaluate_element (element, 0));
+                delelement (element);
+                ret = 0;
+            }
         }
 
-        free (buffer);
+        if (mode) {
+            free (buffer);
+        } else {
+            memset (buffer, 0, BUFFER_SIZE);
+        }
     }
 
     return ret;
@@ -188,7 +226,10 @@ 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 + 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rt (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 =