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