fix error managment
[calc.git] / calc.c
diff --git a/calc.c b/calc.c
index f57f1a455dfba634e1ef23971892dd790ed55616..82ec9712dde481364f8359303c27313cc2915509 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -1,21 +1,23 @@
 /* depend: */
 /* cflags: */
-/* linker: atoi.o debug.o fdprintf.o parser.o -lm */
+/* linker: debug.o parser.o -lm -lreadline */
 
-//#include <malloc.h>
+#include <malloc.h>
 #include <stddef.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <unistd.h>
 
-#include "atoi.h"
+#include <readline/readline.h>
+#include <readline/history.h>
+
 #include "debug.h"
-#include "fdprintf.h"
 #include "parser.h"
 
 /* constants */
 
-//#define BUFFER_SIZE 4096
-#define BUFFER_SIZE 256
+#define BUFFER_SIZE 4096
+#define HISTORY_LEN 10
 
 /* macros */
 
 /* gobal variables */
 
 char *progname = NULL;
+int mode = 1;
 int precision = 6;
 
 /* help function */
 
 int usage (int ret)
 {
-    int fd = ret ? stdfderr : stdfdout;
-    fdprintf (fd, "usage: %s\n", progname);
-    fdprintf (fd, " -h : help message\n");
-    fdprintf (fd, " -p : precision (%d)\n", precision);
-    fdprintf (fd, " -v : verbose level (%d)\n", verbose);
+    FILE *fid = ret ? stderr : stdout;
+    fprintf (fid, "usage: %s\n", progname);
+    fprintf (fid, " -h : help message\n");
+    fprintf (fid, " -n : no readline mode (%s)\n", mode ? "yes" : "no");
+    fprintf (fid, " -p : precision (%d)\n", precision);
+    fprintf (fid, " -v : verbose level (%d)\n", verbose);
 
     return 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 = NULL;
+    char buffer_static[BUFFER_SIZE + 1] = {0};
+    int i = 0, nb = 1;
     int ret = 0;
 
     /* program name */
@@ -67,15 +71,19 @@ int main (int argc, char *argv[])
      while (argc-- > 1) {
         char *arg = *(++argv);
         if (arg[0] != '-') {
-            PRINTERR ("%s: invalid option -- %s\n", progname, arg);
+            fprintf (stderr, "%s: invalid option -- '%s'\n", progname, arg);
             return usage (1);
         }
         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) {
-                PRINTERR ("%s: missing precision\n", progname);
+                fprintf (stderr, "%s: missing precision\n", progname);
                 return usage (1);
             }
             precision = atoi (arg);
@@ -83,7 +91,7 @@ int main (int argc, char *argv[])
         case 'v':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
             if (arg == NULL) {
-                PRINTERR ("%s: missing verbose level\n", progname);
+                fprintf (stderr, "%s: missing verbose level\n", progname);
                 return usage (1);
             }
             verbose = atoi (arg);
@@ -93,50 +101,81 @@ int main (int argc, char *argv[])
             return usage (c != 'h');
         }
     }
-    
+
     /* format */
-    char format[8] = "=> %.f\n";
-    format[4] = '0' + precision;
+    char format[9] = "=> %..g\n";
+    format[5] = '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] == '.')) {
-            return 0;
-        }
+    while (1) {
+        char *line[BUFFER_SIZE] = {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;
+        if (mode) {
+            if ((buffer = readline ("<= ")) == NULL) {
+                break;
+            }
+
+            /* check empty line */
+            if (strlen (buffer) == 0) {
+                free (buffer);
+                continue;
+            } else if (strcmp (buffer, ".") == 0) {
+                break;
+            }
+            line[0] = buffer;
+            nb = 1;
+
+            /* add line into history */
+            add_history (buffer);
+            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);
                 }
-                //fsync (stdfdout);
-                j = i + 1;
             }
+        } 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 + 1;
+                }
+                pt++;
+            }
+            VERBOSE (INFO, fprintf (stdout, "line: '%s'\n", buffer));
         }
 
-        /* keep remainding */
-        if (j < n) {
-            for (i = 0; i < n - j; i++) {
-                buffer[i] = buffer[i + j];
+        /* look for end of line */
+        for (i = 0; i < nb; i++) {
+            if (*line[i] == '\0') {
+                continue;
             }
-            pt = buffer + n - j;
-            for (i = n - j; i < BUFFER_SIZE; i++) {
-                buffer[i] = 0;
+            element_t *element = parser (line[i], NULL, 0);
+            if (element == ERROR_OP) {
+                VERBOSE (WARNING, fprintf (stdout, "error while parsing: '%s'\n", line[i]); fflush (stdout));
+                ret = 1;
+            } else {
+                VERBOSE (INFO, print_element (element, 0));
+                answer = evaluate_element (element, 0);
+                fprintf (stdout, format, answer);
+                fflush (stdout);
+                delelement (element);
+                ret = 0;
             }
         }
+
+        if (mode) {
+            free (buffer);
+        } else {
+            memset (buffer, 0, BUFFER_SIZE);
+        }
     }
 
     return ret;
@@ -153,48 +192,94 @@ int main (int argc, char *argv[])
 // test: echo "1 + 2" | calc.exe | grep -q '=> 3'
 // test: echo "1 - 2" | calc.exe | grep -q '=> -1'
 // test: echo "2 * 3" | calc.exe | grep -q '=> 6'
-// test: echo "1 / 2" | calc.exe | grep -q '=> 5e-1'
+// test: echo "1 / 2" | calc.exe | grep -q '=> 0\.5'
 // test: echo "8 % 3" | calc.exe | grep -q '=> 2'
-// test: echo "-9 % 3.1" | calc.exe | grep -q '=> -2.8'
+// test: echo "-9 % 3.1" | calc.exe | grep -q '=> -2\.8'
 // test: echo "2 ^ 3" | calc.exe | grep -q '=> 8'
-// test: echo "1e-1 + 2.34e3" | calc.exe | grep -q '=> 2.3401'
-// test: echo "sqrt (2)" | calc.exe | grep -q '=> 1.414213'
+// test: echo "1e-1 + 2.34e3" | calc.exe | grep -q '=> 2340\.1'
+// test: echo "sqrt (2)" | calc.exe | grep -q '=> 1\.41421'
 // test: echo "pow (2, 3)" | calc.exe | grep -q '=> 8'
-// test: echo "cos (2)" | calc.exe | grep -q '=> -4.161468e-1'
-// test: echo "sin (2)" | calc.exe | grep -q '=> 9.092974e-1'
-// test: echo "atan (2)" | calc.exe | grep -q '=> 1.107148'
-// test: echo "exp (2)" | calc.exe | grep -q '=> 7.389056'
-// test: echo "log (2)" | calc.exe | grep -q '=> 6.931471e-1'
+// test: echo "cos (2)" | calc.exe | grep -q '=> -0\.416147'
+// test: echo "sin (2)" | calc.exe | grep -q '=> 0\.909297'
+// test: echo "atan (2)" | calc.exe | grep -q '=> 1\.10715'
+// test: echo "exp (2)" | calc.exe | grep -q '=> 7\.38906'
+// test: echo "log (2)" | calc.exe | grep -q '=> 0\.693147'
 // test: echo "2 + 3 - 4" | calc.exe | grep -q '=> 1'
-// test: echo "1 + cos (2 - 3)" | calc.exe | grep -q '=> 1.54030'
-// test: echo "cos (1 / 2) * 3" | calc.exe | grep -q '=> 2.63274'
+// test: echo "1 + cos (2 - 3)" | calc.exe | grep -q '=> 1\.5403'
+// test: echo "cos (1 / 2) * 3" | calc.exe | grep -q '=> 2\.63275'
 // test: echo "1 + 4 * (2 - 3)" | calc.exe | grep -q '=> -3'
-// test: echo "(2 - 3) / 4" | calc.exe | grep -q '=> -2.5e-1'
-// test: echo "pow (8 - 3, 4 / 3)" | calc.exe | grep -q '=> 8.549879'
+// test: echo "(2 - 3) / 4" | calc.exe | grep -q '=> -0\.25'
+// test: echo "pow (8 - 3, 4 / 3)" | calc.exe | grep -q '=> 8\.54988'
 // test: echo "1 + -2" | calc.exe | grep -q '=> -1'
 // test: echo "1 - +2" | calc.exe | grep -q '=> -1'
 // test: echo "-1 + +2" | calc.exe | grep -q '=> 1'
 // test: echo "-1+2" | calc.exe | grep -q '=> 1'
 // test: echo "1-2" | calc.exe | grep -q '=> -1'
-// test: echo "1 * 2 / 3 + 4" | calc.exe | grep -q '=> 4.666666'
-// test: echo "2 ^ 3 * 4 + 5" | calc.exe | grep -q '=> 3.7e1'
-// test: echo "2 + 3 * 4 ^ 5" | calc.exe | grep -q '=> 3.074e3'
-// test: echo "2 ^ 3 * 4 + cos(5/6)" | calc.exe | grep -q '=> 3.267241e1'
-// test: echo "95-6.3*15-1" | calc.exe | grep -q '=> -5e-1'
-// test: echo "95 - 6.3 * 15 - 1" | calc.exe | grep -q '=> -5e-1'
-// test: echo "95-6.3+15" | calc.exe | grep -q '=> 1.037e2'
-// test: echo "-cos (0) + 1" | calc.exe | grep -q '=> 0'
+// test: echo "1 * 2 / 3 + 4" | calc.exe | grep -q '=> 4\.66667'
+// test: echo "2 ^ 3 * 4 + 5" | calc.exe | grep -q '=> 37'
+// test: echo "2 + 3 * 4 ^ 5" | calc.exe | grep -q '=> 3074'
+// test: echo "2 ^ 3 * 4 + cos(5/6)" | calc.exe | grep -q '=> 32\.6724'
+// test: echo "95-6.3*15-1" | calc.exe | grep -q '=> -0.5'
+// test: echo "95 - 6.3 * 15 - 1" | calc.exe | grep -q '=> -0\.5'
+// test: echo "95-6.3+15" | calc.exe | grep -q '=> 103.7'
+// test: echo "-cos (0) + 1" | calc.exe | grep -q '=> -0'
+// test: echo "-cos(0)+1" | calc.exe | grep -q '=> -0'
 // test: echo "quit" | calc.exe | grep -q 'bye'
 // test: echo "help" | calc.exe | grep -q 'miscellaneous'
 // test: echo "1 + 2 *" | calc.exe | grep -q 'error'
 // test: echo "* 1 - 2" | calc.exe | grep -q 'error'
 // test: echo "2 + * 3" | calc.exe | grep -q 'error'
+// test: echo "2 + cos(3 *)" | calc.exe | grep -q 'error'
+// test: echo "2 + (foo)" | calc.exe | grep -q 'error'
+// test: echo "2 + cos (pi +" | calc.exe | grep -q 'error'
+// test: echo "2 + cos (pi" | calc.exe | grep -vq 'error'
+// test: echo "cos (1, 2)" | calc.exe | grep -q 'error'
 // test: echo "sqrt 2" | calc.exe | grep -q 'error'
 // 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 '-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 =
+// test: echo -e '1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 +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 1 + 1\n1 + 1' | calc.exe | grep -q 2
+// 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)\nans\ne\npi\nsto (1)\nrcl (2)\ndisp\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 12 =
+// test: echo -e '1 + 1\nans' | calc.exe -p 3 | grep -c 2 | xargs test 2 =
+// test: echo -e 'sin (pi / 2)' | calc.exe -p 4 | grep -q 1
+// test: echo -e 'e ^ 2' | calc.exe | grep -q '7\.38906'
+// test: echo -e '\n\n\n' | calc.exe | grep -qv 'error'
+// test: echo -e '\n\n\n' | calc.exe -n
+// test: echo -e '1.5\nsto (2)\n3 + rcl(2) * 4\nsto (5)' | calc.exe | grep -q 9
+// test: echo -e '1\nsto (0)\nsto (11)\nrcl (0)\nrcl (11)' | calc.exe | grep -c invalid | xargs test 4 =
+// test: echo -e '1\nsto (2)\n3\nsto (5, 7)\nsto(9)\ndisp' | calc.exe | grep -q '0 1 0 0 7 0 0 0 7 0'
+// test: echo -e '1+1 == 2' | calc.exe | grep -q '=> 1'
+// test: echo -e '1 + 1 == 2 - 0' | calc.exe | grep -q '=> 1'
+// test: echo -e '1 == 1 + 1 == 1' | calc.exe | grep -q '=> 0'
+// test: echo -e '1-cos(3*pi/4) != 0.5^-2' | calc.exe | grep -q '=> 1'
+// test: echo -e '1 >= 2' | calc.exe | grep -q '=> 0'
+// test: echo -e '2 >= 2' | calc.exe | grep -q '=> 1'
+// test: echo -e '1 <= 2' | calc.exe | grep -q '=> 1'
+// test: echo -e '2 <= 2' | calc.exe | grep -q '=> 1'
+// test: echo -e '1 > 2' | calc.exe | grep -q '=> 0'
+// test: echo -e '2 > 2' | calc.exe | grep -q '=> 0'
+// test: echo -e '1 < 2' | calc.exe | grep -q '=> 1'
+// test: echo -e '2 < 2' | calc.exe | grep -q '=> 0'
+// test: echo -e '1 == 1\n1 != 1\n1 >= 1\n1 <= 1\n1 > 1\n1 < 1\nquit' | calc.exe -v 3 | grep -q bye
+// test: echo -e '(3 == 3) & (2 > 1)' | calc.exe | grep -q '=> 1'
+// test: echo -e '(3 == 4) & (2 > 1)' | calc.exe | grep -q '=> 0'
+// test: echo -e '(3 == 3) & (2 > 2)' | calc.exe | grep -q '=> 0'
+// test: echo -e '(3 == 4) & (2 > 2)' | calc.exe | grep -q '=> 0'
+// test: echo -e '(3 == 3) | (2 > 1)' | calc.exe | grep -q '=> 1'
+// test: echo -e '(3 == 4) | (2 > 1)' | calc.exe | grep -q '=> 1'
+// test: echo -e '(3 == 3) | (2 > 2)' | calc.exe | grep -q '=> 1'
+// test: echo -e '(3 == 4) | (2 > 2)' | calc.exe | grep -q '=> 0'
+// test: echo -e '!(3 == 4)' | calc.exe | grep -q '=> 1'
+// test: echo -e '!(3 == 3)' | calc.exe | grep -q '=> 0'
+// test: echo -e '1 & 1\n1 | 1\n!(1)\nquit' | calc.exe -v 3 | grep -q bye
+// test: echo -e '(3 == 3) & (4 > 2)' | calc.exe | grep -q '=> 1'
+// test: echo -e '3 == 3 & 4 > 2' | calc.exe | grep -q '=> 1'
+// test: echo -e '1 + quit' | calc.exe 2>&1 | grep -q error
+// test: echo -e 'cos (quit)' | calc.exe 2>&1 | grep -q error
+// test: echo -e '(quit)' | calc.exe 2>&1 | grep -q error
 
 /* vim: set ts=4 sw=4 et: */