remove fdprintf
[calc.git] / calc.c
diff --git a/calc.c b/calc.c
index f5d9cfdb4399d65771f3223a5a13e2c248bfb4dc..ecb01c3a6ba0ae3e1d2bb5bdb9cb516d2c3bd6a3 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -1,18 +1,22 @@
 /* depend: */
 /* cflags: */
-/* linker: */
+/* linker: debug.o parser.o -lm -lreadline */
 
-#include <assert.h>
-#include <getopt.h>
 #include <malloc.h>
+#include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 
+#include <readline/readline.h>
+#include <readline/history.h>
+
+#include "debug.h"
+#include "parser.h"
+
 /* constants */
 
-//#define BUFFER_SIZE 4096
-#define BUFFER_SIZE 256
+#define BUFFER_SIZE 4096
 
 /* macros */
 
 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
 
-/* verbose */
-
-#define ERROR 0
-#define WARNING 1
-#define INFO 2
-#define DEBUG 3
-
-#define VERBOSE(level, statement...) do { if (level <= verbose) { statement; } } while(0)
-
 /* gobal variables */
 
 char *progname = NULL;
-int verbose = 0;
+int mode = 1;
+int precision = 6;
 
 /* help function */
 
-void usage (int ret)
+int usage (int ret)
 {
-    FILE *fd = ret ? stderr : stdout;
-    fprintf (fd, "usage: %s\n", progname);
-    fprintf (fd, " -h : help message\n");
-    fprintf (fd, " -v : verbose level (%d)\n", verbose);
-
-    exit (ret);
+    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, j = 0, n;
+    char *buffer = NULL;
+    char buffer_static[BUFFER_SIZE + 1] = {0};
+    int i = 0, nb = 1;
+    int ret = 0;
+
+    /* program name */
 
     progname = argv[0];
+    while (progname[i] != '\0') {
+        if ((progname[i] == '/') || (progname[i] == '\\')) {
+            progname += i + 1;
+            i = 0;
+        } else {
+            i++;
+        }
+    }
 
-    int c;
-    while ((c = getopt(argc, argv, "hv:")) != EOF) {
+    /* argument processing */
+
+     while (argc-- > 1) {
+        char *arg = *(++argv);
+        if (arg[0] != '-') {
+            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) {
+                fprintf (stderr, "%s: missing precision\n", progname);
+                return usage (1);
+            }
+            precision = atoi (arg);
+            break;
         case 'v':
-            verbose = atoi (optarg);
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                fprintf (stderr, "%s: missing verbose level\n", progname);
+                return usage (1);
+            }
+            verbose = atoi (arg);
             break;
         case 'h':
-            VERBOSE (INFO, usage (0));
-            break;
         default:
-            VERBOSE (ERROR, usage (1));
+            return usage (c != 'h');
         }
     }
-    if (argc - optind != 0) {
-        VERBOSE (ERROR, fprintf (stderr, "%s: invalid option -- %s\n", progname, argv[optind]));
-        VERBOSE (ERROR, usage (1));
-    }
+
+    /* format */
+    char format[8] = "=> %.f\n";
+    format[4] = '0' + precision;
 
     /* read from input stream */
-    while ((n = read (STDIN_FILENO, pt, BUFFER_SIZE - (pt - buffer))) != 0) {
-        VERBOSE (DEBUG, fprintf (stdout, "read %d bytes\n", n));
-        n += (pt - buffer);
 
-        /* look for end of line */
-        for (i = 0, j = 0; i < n; i++) {
-            if (buffer[i] == '\n') {
-                buffer[i] = 0;
-                VERBOSE (DEBUG, fprintf (stdout, "line(%d): %s\n", j, buffer + j));
-                j = i + 1;
+    while (1) {
+        char *line[BUFFER_SIZE] = {0};
+
+        if (mode) {
+            if ((buffer = readline ("<= ")) == NULL) {
+                break;
             }
-        }
 
-        /* keep remainding */
-        if (j < n) {
-            for (i = 0; i < n - j; i++) {
-                buffer[i] = buffer[i + j];
+            /* check empty line */
+            if (strlen (buffer) == 0) {
+                free (buffer);
+                continue;
+            } else if (strcmp (buffer, ".") == 0) {
+                break;
             }
-            pt = buffer + n - j;
-            for (i = n - j; i < BUFFER_SIZE; i++) {
-                buffer[i] = 0;
+            line[0] = buffer;
+
+            /* add line into history */
+            add_history (buffer);
+            VERBOSE (INFO, fprintf (stdout, "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 (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));
         }
-    }
 
-    /* check that nothing is left behind */
+        /* look for end of line */
+        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, fprintf (stdout, "error while parsing: %s\n", line[i]));
+                ret = 1;
+            } else {
+                VERBOSE (INFO, print_element (element, 0));
+                fprintf (stdout, format, evaluate_element (element, 0));
+                delelement (element);
+                ret = 0;
+            }
+        }
 
-    VERBOSE (DEBUG, fprintf (stdout, "last\n"));
-    VERBOSE (DEBUG, fprintf (stdout, "line(%d): %s\n", j, buffer + j));
+        if (mode) {
+            free (buffer);
+        } else {
+            memset (buffer, 0, BUFFER_SIZE);
+        }
+    }
 
-    return 0;
+    return ret;
 }
 
 // test: calc.exe -h
 // test: calc.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
+// test: echo 1 | calc.exe -v3 | grep -q value
 // test: calc.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
 // test: calc.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
-// test: echo "foo\nbar\nfoobar" | calc.exe -v3
+// test: calc.exe error 2>&1 | grep -q 'invalid option'
+// test: calc.exe -p 2>&1 | grep -q 'missing precision'
+// test: calc.exe -v 2>&1 | grep -q 'missing verbose'
+// 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 "8 % 3" | calc.exe | grep -q '=> 2'
+// 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 "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 "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 + 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 "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 "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 "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 -n | 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 -n | grep -q 2
+// 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 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)\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 =
 
 /* vim: set ts=4 sw=4 et: */