hist command (ugly)
authorLaurent Mazet <mazet@softndesign.org>
Tue, 28 Feb 2023 22:31:51 +0000 (23:31 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Tue, 28 Feb 2023 22:31:51 +0000 (23:31 +0100)
calc.c
element.h
parser.c

diff --git a/calc.c b/calc.c
index cbfdcfa26724b2032b3c753caf987a909f67b246..98372cef9f0c62d66a04b05619775ecb043d3661 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -99,6 +99,22 @@ int edit_hook ()
     return rl_insert_text (edit_line);
 }
 
+/* history management */
+
+void history ()
+{
+    HIST_ENTRY **entries = history_list ();
+    if (entries == NULL) {
+        VERBOSE (WARNING, fprintf (stdout, "no history\n"));
+    } else {
+        int i = 0;
+        while (entries[i] != NULL) {
+            printf ("%d: %s\n", history_length - i, entries[i]->line);
+            i++;
+        }
+    }
+}
+
 /* main function */
 
 int main (int argc, char *argv[])
@@ -343,6 +359,7 @@ int main (int argc, char *argv[])
 // 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 -e '1 + 1\nhist' | calc.exe | grep -q '2: 1 + 1'
 // 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'
@@ -360,7 +377,7 @@ int main (int argc, char *argv[])
 // 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 64
 // 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)\ntan (0)\nacos (0)\nasin (0)\natan (0)\nln (1)\nlog (1)\nexp (1)\nabs (-1)\nceil (1.2)\nfloor (-1.2)\nans\ne\npi\nsto (1)\nrcl (2)\ndisp\nhelp\nquit' | calc.exe -n -v 3 | grep -q bye
+// test: echo -e '-cos (1)\n1 + 1\n1 - 1\n1 * 1\n1 / 1\n3%2\n2^2\nsqrt (2)\ncos (0)\nsin (0)\ntan (0)\nacos (0)\nasin (0)\natan (0)\nln (1)\nlog (1)\nexp (1)\nabs (-1)\nceil (1.2)\nfloor (-1.2)\nans\ne\npi\nsto (1)\nrcl (2)\ndisp\nhelp\nhist\nquit' | calc.exe -n -v 3 | grep -q bye
 // test: echo -e '1 +\n1 -\n1 * 1\n1 /\n3%\n2^\nsqrt ()\ncos ()\nsin ()\ntan ()\nacos ()\nasin ()\natan ()\nln ()\nlog ()\nexp ()\nabs ()\nceil ()\nfloor ()\n1 + (\n1+2(\n1+2cos\n1+2pi' | calc.exe | grep -c error | xargs test 22 =
 // 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
index 37c82136a7e11477b5e513de3619553f1c39f28c..54679784a1b819a1c8ff65b829318cd2e37a55f9 100644 (file)
--- a/element.h
+++ b/element.h
@@ -15,7 +15,7 @@ typedef enum {
     Erfc, Erf,
     Abs, Ceil, Floor,
     Store, Recall, Inc, Dec, Disp, Memory, Clear,
-    Quit, Help,
+    Quit, Help, History,
     Ans, E, Pi,
     Equal, Diff, Ge, Le, Gt, Lt,
     And, Or, Not,
@@ -23,7 +23,7 @@ typedef enum {
     Prog, Arg, Call, List, Edit, Del,
     Get, Length, Pop, Push, Put, Set, Show,
     Max, Mean, Median, Min, Order, Prod, Sum, Variance,
-    Precision, Base, Deg, Grad, Rad
+    Precision, Base, Deg, Grad, Rad,
 } func_t;
 
 /* keyword type */
index 24afb116c98f19a4a2d0e2724a4d52b6982984fb..4d9b01a23b8a81bb438ba989f9e0df76d85a0ceb 100644 (file)
--- a/parser.c
+++ b/parser.c
 
 #include "parser.h"
 
+/* external definition */
+
+extern void history ();
+
 /* global variables */
 
 double answer = 0;
@@ -64,7 +68,7 @@ keyword_t operators[NB_OPERATORS] = {
     { "|",   Or, 2, 1, -2}
 };
 
-#define NB_FUNCTIONS 55
+#define NB_FUNCTIONS 56
 keyword_t functions[NB_FUNCTIONS] = {
     { "sqrt", Sqr, 1, 4, 5},
     { "pow",  Pow, 2, 3, 5},
@@ -91,6 +95,7 @@ keyword_t functions[NB_FUNCTIONS] = {
     { "clr",  Clear, 0, 3, 9},
     { "quit", Quit, 0, 4, 9},
     { "help", Help, 0, 4, 9},
+    { "hist", History, 0, 4, 9},
     { "!",    Not, 1, 1, 6},
     { "cond", Cond, 3, 4, 5},
     { "while", While, 2, 5, 5},
@@ -489,6 +494,7 @@ void print_element (element_t *root, int level)
     case Clear: func = "Clear"; break;
     case Quit: func = "Quit"; break;
     case Help: func = "Help"; break;
+    case History: func = "History"; break;
     case Ans: func = "Ans"; break;
     case Pi: func = "Pi"; break;
     case E: func = "E"; break;
@@ -626,7 +632,9 @@ void help (void)
     printf ("stack func.:");
     printf (" max mean med min ord prod sum var\n");
     printf ("control management:");
-    printf (" base deg format grad help quit rad\n");
+    printf (" base format help hist quit\n");
+    printf ("angle management:");
+    printf (" deg grad rad\n");
     printf ("constants:");
     printf (" ans e pi\n");
 }
@@ -762,6 +770,7 @@ double evaluate_element (element_t *root, char mask)
     case Clear:
     case Quit:
     case Help:
+    case History:
     case Ans:
     case Pi:
     case E:
@@ -861,6 +870,7 @@ double evaluate_element (element_t *root, char mask)
     case Clear: clear (); break;
     case Quit: quit (); break;
     case Help: help (); break;
+    case History: history (); break;
     case Ans: return answer;
     case Pi: return M_PI;
     case E: return M_E;