partial while feature
[calc.git] / calc.c
CommitLineData
ec15bdbc
LM
1/* depend: */
2/* cflags: */
04d68907 3/* linker: debug.o parser.o -lm -lreadline */
ec15bdbc 4
680bf70a 5#include <malloc.h>
01f02c41 6#include <stddef.h>
ec15bdbc 7#include <stdio.h>
04d68907 8#include <stdlib.h>
5898ff24
LM
9#include <unistd.h>
10
11#include <readline/readline.h>
12#include <readline/history.h>
ec15bdbc 13
bc97a989 14#include "debug.h"
bc97a989 15#include "parser.h"
01f02c41 16
ec15bdbc
LM
17/* constants */
18
5898ff24 19#define BUFFER_SIZE 4096
3a7a78ff 20#define HISTORY_LEN 10
5898ff24 21
ec15bdbc
LM
22/* macros */
23
24#define CEIL(x, y) (((x) + (y) - 1) / (y))
25#define MIN(x, y) (((x) < (y)) ? (x) : (y))
26#define MAX(x, y) (((x) > (y)) ? (x) : (y))
27
ec15bdbc
LM
28/* gobal variables */
29
30char *progname = NULL;
5898ff24 31int mode = 1;
4ee92d9d 32int precision = 6;
ec15bdbc
LM
33
34/* help function */
35
01f02c41 36int usage (int ret)
ec15bdbc 37{
04d68907
LM
38 FILE *fid = ret ? stderr : stdout;
39 fprintf (fid, "usage: %s\n", progname);
40 fprintf (fid, " -h : help message\n");
41 fprintf (fid, " -n : no readline mode (%s)\n", mode ? "yes" : "no");
42 fprintf (fid, " -p : precision (%d)\n", precision);
43 fprintf (fid, " -v : verbose level (%d)\n", verbose);
ec15bdbc 44
01f02c41 45 return ret;
ec15bdbc
LM
46}
47
48/* main function */
49
680bf70a 50int main (int argc, char *argv[])
ec15bdbc 51{
5898ff24
LM
52 char *buffer = NULL;
53 char buffer_static[BUFFER_SIZE + 1] = {0};
54 int i = 0, nb = 1;
efdfb543 55 int ret = 0;
01f02c41
LM
56
57 /* program name */
ec15bdbc
LM
58
59 progname = argv[0];
01f02c41
LM
60 while (progname[i] != '\0') {
61 if ((progname[i] == '/') || (progname[i] == '\\')) {
62 progname += i + 1;
63 i = 0;
64 } else {
65 i++;
66 }
67 }
68
69 /* argument processing */
ec15bdbc 70
01f02c41
LM
71 while (argc-- > 1) {
72 char *arg = *(++argv);
73 if (arg[0] != '-') {
b89cbb39
LM
74 VERBOSE (ERROR, fprintf (stderr, "%s: invalid option -- '%s'\n", progname, arg); usage (1));
75 return 1;
01f02c41
LM
76 }
77 char c = arg[1];
ec15bdbc 78 switch (c) {
5898ff24
LM
79 case 'n':
80 mode = 0;
81 buffer = buffer_static;
82 break;
4ee92d9d
LM
83 case 'p':
84 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
85 if (arg == NULL) {
b89cbb39
LM
86 VERBOSE (ERROR, fprintf (stderr, "%s: missing precision\n", progname); usage (1));
87 return 1;
4ee92d9d
LM
88 }
89 precision = atoi (arg);
90 break;
ec15bdbc 91 case 'v':
01f02c41
LM
92 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
93 if (arg == NULL) {
b89cbb39
LM
94 VERBOSE (ERROR, fprintf (stderr, "%s: missing verbose level\n", progname); usage (1));
95 return 1;
01f02c41
LM
96 }
97 verbose = atoi (arg);
ec15bdbc
LM
98 break;
99 case 'h':
ec15bdbc 100 default:
01f02c41 101 return usage (c != 'h');
ec15bdbc
LM
102 }
103 }
680bf70a 104
4ee92d9d 105 /* format */
5075f6ea
LM
106 char format[9] = "=> %..g\n";
107 format[5] = '0' + precision;
ec15bdbc
LM
108
109 /* read from input stream */
01f02c41 110
5898ff24
LM
111 while (1) {
112 char *line[BUFFER_SIZE] = {0};
680bf70a 113
5898ff24
LM
114 if (mode) {
115 if ((buffer = readline ("<= ")) == NULL) {
116 break;
117 }
ec15bdbc 118
5898ff24
LM
119 /* check empty line */
120 if (strlen (buffer) == 0) {
121 free (buffer);
122 continue;
123 } else if (strcmp (buffer, ".") == 0) {
124 break;
125 }
126 line[0] = buffer;
941d160e 127 nb = 1;
5898ff24
LM
128
129 /* add line into history */
130 add_history (buffer);
3a7a78ff
LM
131 VERBOSE (INFO, fprintf (stdout, "line (%d/%d): '%s'\n",
132 where_history (), history_length, buffer));
133 if (history_length > HISTORY_LEN) {
5898ff24
LM
134 HIST_ENTRY *last = remove_history (0);
135 if (last) {
136 free_history_entry (last);
137 }
ec15bdbc 138 }
5898ff24 139 } else {
3a7a78ff
LM
140 if (read (STDIN_FILENO, buffer, BUFFER_SIZE) == 0) {
141 break;
142 }
143 nb = 0;
144 char *pt = line[nb++] = buffer;
32741902 145 while (*pt != '\0') {
3a7a78ff
LM
146 if (*pt == '\n') {
147 *pt = '\0';
32741902 148 line[nb++] = pt + 1;
3a7a78ff 149 }
32741902 150 pt++;
3a7a78ff
LM
151 }
152 VERBOSE (INFO, fprintf (stdout, "line: '%s'\n", buffer));
ec15bdbc
LM
153 }
154
680bf70a 155 /* look for end of line */
941d160e 156 for (i = 0; i < nb; i++) {
5898ff24
LM
157 if (*line[i] == '\0') {
158 continue;
159 }
160 element_t *element = parser (line[i], NULL, 0);
161 if (element == ERROR_OP) {
32741902 162 VERBOSE (WARNING, fprintf (stdout, "error while parsing: '%s'\n", line[i]); fflush (stdout));
5898ff24
LM
163 ret = 1;
164 } else {
165 VERBOSE (INFO, print_element (element, 0));
5075f6ea
LM
166 answer = evaluate_element (element, 0);
167 fprintf (stdout, format, answer);
87621fe1 168 fflush (stdout);
5898ff24
LM
169 delelement (element);
170 ret = 0;
171 }
ec15bdbc 172 }
680bf70a 173
5898ff24
LM
174 if (mode) {
175 free (buffer);
176 } else {
177 memset (buffer, 0, BUFFER_SIZE);
178 }
ec15bdbc
LM
179 }
180
efdfb543 181 return ret;
ec15bdbc
LM
182}
183
184// test: calc.exe -h
185// test: calc.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
b449884c 186// test: echo 1 | calc.exe -v3 | grep -q value
ec15bdbc
LM
187// test: calc.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
188// test: calc.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
8a33b741
LM
189// test: calc.exe error 2>&1 | grep -q 'invalid option'
190// test: calc.exe -p 2>&1 | grep -q 'missing precision'
191// test: calc.exe -v 2>&1 | grep -q 'missing verbose'
f2927108
LM
192// test: echo "1 + 2" | calc.exe | grep -q '=> 3'
193// test: echo "1 - 2" | calc.exe | grep -q '=> -1'
194// test: echo "2 * 3" | calc.exe | grep -q '=> 6'
941d160e 195// test: echo "1 / 2" | calc.exe | grep -q '=> 0\.5'
c47a9298 196// test: echo "8 % 3" | calc.exe | grep -q '=> 2'
941d160e 197// test: echo "-9 % 3.1" | calc.exe | grep -q '=> -2\.8'
f2927108 198// test: echo "2 ^ 3" | calc.exe | grep -q '=> 8'
941d160e
LM
199// test: echo "1e-1 + 2.34e3" | calc.exe | grep -q '=> 2340\.1'
200// test: echo "sqrt (2)" | calc.exe | grep -q '=> 1\.41421'
f2927108 201// test: echo "pow (2, 3)" | calc.exe | grep -q '=> 8'
941d160e
LM
202// test: echo "cos (2)" | calc.exe | grep -q '=> -0\.416147'
203// test: echo "sin (2)" | calc.exe | grep -q '=> 0\.909297'
204// test: echo "atan (2)" | calc.exe | grep -q '=> 1\.10715'
205// test: echo "exp (2)" | calc.exe | grep -q '=> 7\.38906'
206// test: echo "log (2)" | calc.exe | grep -q '=> 0\.693147'
f2927108 207// test: echo "2 + 3 - 4" | calc.exe | grep -q '=> 1'
941d160e
LM
208// test: echo "1 + cos (2 - 3)" | calc.exe | grep -q '=> 1\.5403'
209// test: echo "cos (1 / 2) * 3" | calc.exe | grep -q '=> 2\.63275'
f2927108 210// test: echo "1 + 4 * (2 - 3)" | calc.exe | grep -q '=> -3'
941d160e
LM
211// test: echo "(2 - 3) / 4" | calc.exe | grep -q '=> -0\.25'
212// test: echo "pow (8 - 3, 4 / 3)" | calc.exe | grep -q '=> 8\.54988'
f2927108
LM
213// test: echo "1 + -2" | calc.exe | grep -q '=> -1'
214// test: echo "1 - +2" | calc.exe | grep -q '=> -1'
215// test: echo "-1 + +2" | calc.exe | grep -q '=> 1'
216// test: echo "-1+2" | calc.exe | grep -q '=> 1'
217// test: echo "1-2" | calc.exe | grep -q '=> -1'
941d160e
LM
218// test: echo "1 * 2 / 3 + 4" | calc.exe | grep -q '=> 4\.66667'
219// test: echo "2 ^ 3 * 4 + 5" | calc.exe | grep -q '=> 37'
220// test: echo "2 + 3 * 4 ^ 5" | calc.exe | grep -q '=> 3074'
221// test: echo "2 ^ 3 * 4 + cos(5/6)" | calc.exe | grep -q '=> 32\.6724'
222// test: echo "95-6.3*15-1" | calc.exe | grep -q '=> -0.5'
223// test: echo "95 - 6.3 * 15 - 1" | calc.exe | grep -q '=> -0\.5'
224// test: echo "95-6.3+15" | calc.exe | grep -q '=> 103.7'
225// test: echo "-cos (0) + 1" | calc.exe | grep -q '=> -0'
32741902 226// test: echo "-cos(0)+1" | calc.exe | grep -q '=> -0'
89cf0955
LM
227// test: echo "quit" | calc.exe | grep -q 'bye'
228// test: echo "help" | calc.exe | grep -q 'miscellaneous'
031d7bba
LM
229// test: echo "1 + 2 *" | calc.exe | grep -q 'error'
230// test: echo "* 1 - 2" | calc.exe | grep -q 'error'
231// test: echo "2 + * 3" | calc.exe | grep -q 'error'
7fe742c9
LM
232// test: echo "2 + cos(3 *)" | calc.exe | grep -q 'error'
233// test: echo "2 + (foo)" | calc.exe | grep -q 'error'
686c4bc2 234// test: echo "2 + cos (pi +" | calc.exe | grep -q 'error'
3c0db5bc
LM
235// test: echo "2 + cos (pi" | calc.exe | grep -q 'error'
236// test: echo "(2 + " | calc.exe | grep -q 'error'
7fe742c9 237// test: echo "cos (1, 2)" | calc.exe | grep -q 'error'
031d7bba
LM
238// test: echo "sqrt 2" | calc.exe | grep -q 'error'
239// test: echo "pow (2)" | calc.exe | grep -q 'error'
4ee92d9d 240// test: echo "1.23456789" | calc.exe -p 3 | grep -q '1\.234'
8a33b741 241// test: echo . | calc.exe
941d160e 242// test: echo -e '1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 +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+ 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1\n1 + 1' | calc.exe | grep -q 2
6ba1dd0f 246// 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
686c4bc2 247// 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 =
4b16fb43
LM
248// test: echo -e '1 + 1\nans' | calc.exe -p 3 | grep -c 2 | xargs test 2 =
249// test: echo -e 'sin (pi / 2)' | calc.exe -p 4 | grep -q 1
471da7c9 250// test: echo -e 'e ^ 2' | calc.exe | grep -q '7\.38906'
4b16fb43
LM
251// test: echo -e '\n\n\n' | calc.exe | grep -qv 'error'
252// test: echo -e '\n\n\n' | calc.exe -n
6ba1dd0f
LM
253// test: echo -e '1.5\nsto (2)\n3 + rcl(2) * 4\nsto (5)' | calc.exe | grep -q 9
254// test: echo -e '1\nsto (0)\nsto (11)\nrcl (0)\nrcl (11)' | calc.exe | grep -c invalid | xargs test 4 =
3639df66 255// 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'
7fe742c9 256// test: echo -e '1+1 == 2' | calc.exe | grep -q '=> 1'
ca3e2a2f
LM
257// test: echo -e '1 + 1 == 2 - 0' | calc.exe | grep -q '=> 1'
258// test: echo -e '1 == 1 + 1 == 1' | calc.exe | grep -q '=> 0'
259// test: echo -e '1-cos(3*pi/4) != 0.5^-2' | calc.exe | grep -q '=> 1'
7fe742c9
LM
260// test: echo -e '1 >= 2' | calc.exe | grep -q '=> 0'
261// test: echo -e '2 >= 2' | calc.exe | grep -q '=> 1'
262// test: echo -e '1 <= 2' | calc.exe | grep -q '=> 1'
263// test: echo -e '2 <= 2' | calc.exe | grep -q '=> 1'
264// test: echo -e '1 > 2' | calc.exe | grep -q '=> 0'
265// test: echo -e '2 > 2' | calc.exe | grep -q '=> 0'
266// test: echo -e '1 < 2' | calc.exe | grep -q '=> 1'
267// test: echo -e '2 < 2' | calc.exe | grep -q '=> 0'
268// test: echo -e '1 == 1\n1 != 1\n1 >= 1\n1 <= 1\n1 > 1\n1 < 1\nquit' | calc.exe -v 3 | grep -q bye
ca3e2a2f
LM
269// test: echo -e '(3 == 3) & (2 > 1)' | calc.exe | grep -q '=> 1'
270// test: echo -e '(3 == 4) & (2 > 1)' | calc.exe | grep -q '=> 0'
271// test: echo -e '(3 == 3) & (2 > 2)' | calc.exe | grep -q '=> 0'
272// test: echo -e '(3 == 4) & (2 > 2)' | calc.exe | grep -q '=> 0'
273// test: echo -e '(3 == 3) | (2 > 1)' | calc.exe | grep -q '=> 1'
274// test: echo -e '(3 == 4) | (2 > 1)' | calc.exe | grep -q '=> 1'
275// test: echo -e '(3 == 3) | (2 > 2)' | calc.exe | grep -q '=> 1'
276// test: echo -e '(3 == 4) | (2 > 2)' | calc.exe | grep -q '=> 0'
277// test: echo -e '!(3 == 4)' | calc.exe | grep -q '=> 1'
278// test: echo -e '!(3 == 3)' | calc.exe | grep -q '=> 0'
ce6627f2 279// test: echo -e '1 & 1\n1 | 1\n!1\nquit' | calc.exe -v 3 | grep -qv error
ca3e2a2f
LM
280// test: echo -e '(3 == 3) & (4 > 2)' | calc.exe | grep -q '=> 1'
281// test: echo -e '3 == 3 & 4 > 2' | calc.exe | grep -q '=> 1'
67b13e3d
LM
282// test: echo -e '1 + quit' | calc.exe 2>&1 | grep -q error
283// test: echo -e 'cos (quit)' | calc.exe 2>&1 | grep -q error
284// test: echo -e '(quit)' | calc.exe 2>&1 | grep -q error
ce6627f2 285// test: echo -e 'cos 3.14\n!\n! 3 4' | calc.exe 2>&1 | grep -c error | xargs test 3 =
7e98ca87 286// test: echo -e 'sto (2, 3)\ncond (rcl (2) > 2, log (64), exp (75 / 10))' | calc.exe | grep -q '=> 4\.15888'
94b4e517 287// test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, log (64), exp (75 / 10))' | calc.exe | grep -q '=> 1808\.04'
7e98ca87 288// test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, log (64))' | calc.exe | grep -q '=> 0'
94b4e517
LM
289// test: echo -e 'cond (0, 1, 2)' | calc.exe -v 3 | grep -q Cond
290// test: echo -e 'cond\ncond (\ncond (1 >0,'| calc.exe 2>&1 | grep -c error | xargs test 3 =
7e98ca87
LM
291// test: echo -e 'sto (1, 4)\ninc (1)\ninc (1)\ndec (1)\ninc (1)\nrcl (1) == 6' | calc.exe -v 3 | grep -q '=> 1'
292// test: echo -e 'inc\ninc (\ndec\ndec (' | calc.exe 2>&1 | grep -c error | xargs test 4 =
293// test: echo -e 'inc (11)\ndec (0)' | calc.exe 2>&1 | grep -c invalid | xargs test 2 =
2a688642 294// test: echo -e 'whl (inc (1) < 3, sto (rcl (2) + 3))' | calc.exe | xargs test 16 =
ec15bdbc
LM
295
296/* vim: set ts=4 sw=4 et: */