readline can be desactivated
[calc.git] / calc.c
CommitLineData
ec15bdbc
LM
1/* depend: */
2/* cflags: */
5898ff24 3/* linker: debug.o fdprintf.o parser.o -lm -lreadline */
ec15bdbc 4
680bf70a 5#include <malloc.h>
01f02c41 6#include <stddef.h>
ec15bdbc 7#include <stdio.h>
5898ff24
LM
8#include <unistd.h>
9
10#include <readline/readline.h>
11#include <readline/history.h>
ec15bdbc 12
bc97a989 13#include "debug.h"
01f02c41 14#include "fdprintf.h"
bc97a989 15#include "parser.h"
01f02c41 16
ec15bdbc
LM
17/* constants */
18
5898ff24
LM
19#define BUFFER_SIZE 4096
20
ec15bdbc
LM
21/* macros */
22
23#define CEIL(x, y) (((x) + (y) - 1) / (y))
24#define MIN(x, y) (((x) < (y)) ? (x) : (y))
25#define MAX(x, y) (((x) > (y)) ? (x) : (y))
26
ec15bdbc
LM
27/* gobal variables */
28
29char *progname = NULL;
5898ff24 30int mode = 1;
4ee92d9d 31int precision = 6;
ec15bdbc
LM
32
33/* help function */
34
01f02c41 35int usage (int ret)
ec15bdbc 36{
01f02c41
LM
37 int fd = ret ? stdfderr : stdfdout;
38 fdprintf (fd, "usage: %s\n", progname);
39 fdprintf (fd, " -h : help message\n");
5898ff24 40 fdprintf (fd, " -n : no readline mode (%s)\n", mode ? "yes" : "no");
4ee92d9d 41 fdprintf (fd, " -p : precision (%d)\n", precision);
01f02c41 42 fdprintf (fd, " -v : verbose level (%d)\n", verbose);
ec15bdbc 43
01f02c41 44 return ret;
ec15bdbc
LM
45}
46
47/* main function */
48
680bf70a 49int main (int argc, char *argv[])
ec15bdbc 50{
5898ff24
LM
51 char *buffer = NULL;
52 char buffer_static[BUFFER_SIZE + 1] = {0};
53 int i = 0, nb = 1;
efdfb543 54 int ret = 0;
01f02c41
LM
55
56 /* program name */
ec15bdbc
LM
57
58 progname = argv[0];
01f02c41
LM
59 while (progname[i] != '\0') {
60 if ((progname[i] == '/') || (progname[i] == '\\')) {
61 progname += i + 1;
62 i = 0;
63 } else {
64 i++;
65 }
66 }
67
68 /* argument processing */
ec15bdbc 69
01f02c41
LM
70 while (argc-- > 1) {
71 char *arg = *(++argv);
72 if (arg[0] != '-') {
73 PRINTERR ("%s: invalid option -- %s\n", progname, arg);
74 return usage (1);
75 }
76 char c = arg[1];
ec15bdbc 77 switch (c) {
5898ff24
LM
78 case 'n':
79 mode = 0;
80 buffer = buffer_static;
81 break;
4ee92d9d
LM
82 case 'p':
83 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
84 if (arg == NULL) {
85 PRINTERR ("%s: missing precision\n", progname);
86 return usage (1);
87 }
88 precision = atoi (arg);
89 break;
ec15bdbc 90 case 'v':
01f02c41
LM
91 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
92 if (arg == NULL) {
93 PRINTERR ("%s: missing verbose level\n", progname);
94 return usage (1);
95 }
96 verbose = atoi (arg);
ec15bdbc
LM
97 break;
98 case 'h':
ec15bdbc 99 default:
01f02c41 100 return usage (c != 'h');
ec15bdbc
LM
101 }
102 }
680bf70a 103
4ee92d9d
LM
104 /* format */
105 char format[8] = "=> %.f\n";
106 format[4] = '0' + precision;
ec15bdbc
LM
107
108 /* read from input stream */
01f02c41 109
5898ff24
LM
110 while (1) {
111 char *line[BUFFER_SIZE] = {0};
680bf70a 112
5898ff24
LM
113 if (mode) {
114 if ((buffer = readline ("<= ")) == NULL) {
115 break;
116 }
ec15bdbc 117
5898ff24
LM
118 /* check empty line */
119 if (strlen (buffer) == 0) {
120 free (buffer);
121 continue;
122 } else if (strcmp (buffer, ".") == 0) {
123 break;
124 }
125 line[0] = buffer;
126
127 /* add line into history */
128 add_history (buffer);
129 VERBOSE (INFO, PRINTOUT ("line (%d): '%s'\n", where_history (), buffer));
130 if (where_history () == 10) {
131 HIST_ENTRY *last = remove_history (0);
132 if (last) {
133 free_history_entry (last);
134 }
ec15bdbc 135 }
5898ff24
LM
136 } else {
137 if (read (stdfdin, buffer, BUFFER_SIZE) == 0) {
138 break;
139 }
140 nb = 0;
141 char *pt = line[nb++] = buffer;
142 while (*pt++ != '\0') {
143 if (*pt == '\n') {
144 *pt = '\0';
145 line[nb++] = ++pt;
146 }
147 }
148 VERBOSE (INFO, PRINTOUT ("line: '%s'\n", buffer));
ec15bdbc
LM
149 }
150
680bf70a 151 /* look for end of line */
5898ff24
LM
152 for (i = 0; (i < nb) && (ret != 1); i++) {
153 if (*line[i] == '\0') {
154 continue;
155 }
156 element_t *element = parser (line[i], NULL, 0);
157 if (element == ERROR_OP) {
158 VERBOSE (WARNING, PRINTOUT ("error while parsing: %s\n", line[i]));
159 ret = 1;
160 } else {
161 VERBOSE (INFO, print_element (element, 0));
162 PRINTOUT (format, evaluate_element (element, 0));
163 delelement (element);
164 ret = 0;
165 }
ec15bdbc 166 }
680bf70a 167
5898ff24
LM
168 if (mode) {
169 free (buffer);
170 } else {
171 memset (buffer, 0, BUFFER_SIZE);
172 }
ec15bdbc
LM
173 }
174
efdfb543 175 return ret;
ec15bdbc
LM
176}
177
178// test: calc.exe -h
179// test: calc.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
b449884c 180// test: echo 1 | calc.exe -v3 | grep -q value
ec15bdbc
LM
181// test: calc.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
182// test: calc.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
8a33b741
LM
183// test: calc.exe error 2>&1 | grep -q 'invalid option'
184// test: calc.exe -p 2>&1 | grep -q 'missing precision'
185// test: calc.exe -v 2>&1 | grep -q 'missing verbose'
f2927108
LM
186// test: echo "1 + 2" | calc.exe | grep -q '=> 3'
187// test: echo "1 - 2" | calc.exe | grep -q '=> -1'
188// test: echo "2 * 3" | calc.exe | grep -q '=> 6'
189// test: echo "1 / 2" | calc.exe | grep -q '=> 5e-1'
c47a9298
LM
190// test: echo "8 % 3" | calc.exe | grep -q '=> 2'
191// test: echo "-9 % 3.1" | calc.exe | grep -q '=> -2.8'
f2927108
LM
192// test: echo "2 ^ 3" | calc.exe | grep -q '=> 8'
193// test: echo "1e-1 + 2.34e3" | calc.exe | grep -q '=> 2.3401'
194// test: echo "sqrt (2)" | calc.exe | grep -q '=> 1.414213'
195// test: echo "pow (2, 3)" | calc.exe | grep -q '=> 8'
196// test: echo "cos (2)" | calc.exe | grep -q '=> -4.161468e-1'
197// test: echo "sin (2)" | calc.exe | grep -q '=> 9.092974e-1'
198// test: echo "atan (2)" | calc.exe | grep -q '=> 1.107148'
199// test: echo "exp (2)" | calc.exe | grep -q '=> 7.389056'
200// test: echo "log (2)" | calc.exe | grep -q '=> 6.931471e-1'
201// test: echo "2 + 3 - 4" | calc.exe | grep -q '=> 1'
202// test: echo "1 + cos (2 - 3)" | calc.exe | grep -q '=> 1.54030'
203// test: echo "cos (1 / 2) * 3" | calc.exe | grep -q '=> 2.63274'
204// test: echo "1 + 4 * (2 - 3)" | calc.exe | grep -q '=> -3'
205// test: echo "(2 - 3) / 4" | calc.exe | grep -q '=> -2.5e-1'
206// test: echo "pow (8 - 3, 4 / 3)" | calc.exe | grep -q '=> 8.549879'
207// test: echo "1 + -2" | calc.exe | grep -q '=> -1'
208// test: echo "1 - +2" | calc.exe | grep -q '=> -1'
209// test: echo "-1 + +2" | calc.exe | grep -q '=> 1'
210// test: echo "-1+2" | calc.exe | grep -q '=> 1'
211// test: echo "1-2" | calc.exe | grep -q '=> -1'
212// test: echo "1 * 2 / 3 + 4" | calc.exe | grep -q '=> 4.666666'
213// test: echo "2 ^ 3 * 4 + 5" | calc.exe | grep -q '=> 3.7e1'
214// test: echo "2 + 3 * 4 ^ 5" | calc.exe | grep -q '=> 3.074e3'
215// test: echo "2 ^ 3 * 4 + cos(5/6)" | calc.exe | grep -q '=> 3.267241e1'
fd88e359
ML
216// test: echo "95-6.3*15-1" | calc.exe | grep -q '=> -5e-1'
217// test: echo "95 - 6.3 * 15 - 1" | calc.exe | grep -q '=> -5e-1'
b449884c 218// test: echo "95-6.3+15" | calc.exe | grep -q '=> 1.037e2'
3b4b0bbe 219// test: echo "-cos (0) + 1" | calc.exe | grep -q '=> 0'
89cf0955
LM
220// test: echo "quit" | calc.exe | grep -q 'bye'
221// test: echo "help" | calc.exe | grep -q 'miscellaneous'
031d7bba
LM
222// test: echo "1 + 2 *" | calc.exe | grep -q 'error'
223// test: echo "* 1 - 2" | calc.exe | grep -q 'error'
224// test: echo "2 + * 3" | calc.exe | grep -q 'error'
225// test: echo "sqrt 2" | calc.exe | grep -q 'error'
226// test: echo "pow (2)" | calc.exe | grep -q 'error'
4ee92d9d 227// test: echo "1.23456789" | calc.exe -p 3 | grep -q '1\.234'
8a33b741 228// test: echo . | calc.exe
5898ff24
LM
229// 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
230// 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
680bf70a 231// 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
5898ff24 232// 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
8a33b741
LM
233// 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
234// 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 =
ec15bdbc
LM
235
236/* vim: set ts=4 sw=4 et: */