d36111580c42dd49f1688850cc8aa732466bea9b
[calc.git] / calc.c
1 /* depend: */
2 /* cflags: */
3 /* linker: debug.o fdprintf.o parser.o -lm -lreadline */
4
5 #include <malloc.h>
6 #include <stddef.h>
7 #include <stdio.h>
8 #include <unistd.h>
9
10 #include <readline/readline.h>
11 #include <readline/history.h>
12
13 #include "debug.h"
14 #include "fdprintf.h"
15 #include "parser.h"
16
17 /* constants */
18
19 #define BUFFER_SIZE 4096
20
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
27 /* gobal variables */
28
29 char *progname = NULL;
30 int mode = 1;
31 int precision = 6;
32
33 /* help function */
34
35 int usage (int ret)
36 {
37 int fd = ret ? stdfderr : stdfdout;
38 fdprintf (fd, "usage: %s\n", progname);
39 fdprintf (fd, " -h : help message\n");
40 fdprintf (fd, " -n : no readline mode (%s)\n", mode ? "yes" : "no");
41 fdprintf (fd, " -p : precision (%d)\n", precision);
42 fdprintf (fd, " -v : verbose level (%d)\n", verbose);
43
44 return ret;
45 }
46
47 /* main function */
48
49 int main (int argc, char *argv[])
50 {
51 char *buffer = NULL;
52 char buffer_static[BUFFER_SIZE + 1] = {0};
53 int i = 0, nb = 1;
54 int ret = 0;
55
56 /* program name */
57
58 progname = argv[0];
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 */
69
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];
77 switch (c) {
78 case 'n':
79 mode = 0;
80 buffer = buffer_static;
81 break;
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;
90 case 'v':
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);
97 break;
98 case 'h':
99 default:
100 return usage (c != 'h');
101 }
102 }
103
104 /* format */
105 char format[8] = "=> %.f\n";
106 format[4] = '0' + precision;
107
108 /* read from input stream */
109
110 while (1) {
111 char *line[BUFFER_SIZE] = {0};
112
113 if (mode) {
114 if ((buffer = readline ("<= ")) == NULL) {
115 break;
116 }
117
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 }
135 }
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));
149 }
150
151 /* look for end of line */
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 }
166 }
167
168 if (mode) {
169 free (buffer);
170 } else {
171 memset (buffer, 0, BUFFER_SIZE);
172 }
173 }
174
175 return ret;
176 }
177
178 // test: calc.exe -h
179 // test: calc.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
180 // test: echo 1 | calc.exe -v3 | grep -q value
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) }'
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'
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'
190 // test: echo "8 % 3" | calc.exe | grep -q '=> 2'
191 // test: echo "-9 % 3.1" | calc.exe | grep -q '=> -2.8'
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'
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'
218 // test: echo "95-6.3+15" | calc.exe | grep -q '=> 1.037e2'
219 // test: echo "-cos (0) + 1" | calc.exe | grep -q '=> 0'
220 // test: echo "quit" | calc.exe | grep -q 'bye'
221 // test: echo "help" | calc.exe | grep -q 'miscellaneous'
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'
227 // test: echo "1.23456789" | calc.exe -p 3 | grep -q '1\.234'
228 // test: echo . | calc.exe
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 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sin (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 =
235
236 /* vim: set ts=4 sw=4 et: */