From 33376ef084f9855f5dd3d15e316975443c1b00cc Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Tue, 28 Feb 2023 23:31:51 +0100 Subject: [PATCH] hist command (ugly) --- calc.c | 19 ++++++++++++++++++- element.h | 4 ++-- parser.c | 14 ++++++++++++-- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/calc.c b/calc.c index cbfdcfa..98372ce 100644 --- 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 diff --git a/element.h b/element.h index 37c8213..5467978 100644 --- 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 */ diff --git a/parser.c b/parser.c index 24afb11..4d9b01a 100644 --- a/parser.c +++ b/parser.c @@ -14,6 +14,10 @@ #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; -- 2.30.2