d36111580c42dd49f1688850cc8aa732466bea9b
3 /* linker: debug.o fdprintf.o parser.o -lm -lreadline */
10 #include <readline/readline.h>
11 #include <readline/history.h>
19 #define BUFFER_SIZE 4096
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))
29 char *progname
= NULL
;
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
);
49 int main (int argc
, char *argv
[])
52 char buffer_static
[BUFFER_SIZE
+ 1] = {0};
59 while (progname
[i
] != '\0') {
60 if ((progname
[i
] == '/') || (progname
[i
] == '\\')) {
68 /* argument processing */
71 char *arg
= *(++argv
);
73 PRINTERR ("%s: invalid option -- %s\n", progname
, arg
);
80 buffer
= buffer_static
;
83 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
85 PRINTERR ("%s: missing precision\n", progname
);
88 precision
= atoi (arg
);
91 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
93 PRINTERR ("%s: missing verbose level\n", progname
);
100 return usage (c
!= 'h');
105 char format
[8] = "=> %.f\n";
106 format
[4] = '0' + precision
;
108 /* read from input stream */
111 char *line
[BUFFER_SIZE
] = {0};
114 if ((buffer
= readline ("<= ")) == NULL
) {
118 /* check empty line */
119 if (strlen (buffer
) == 0) {
122 } else if (strcmp (buffer
, ".") == 0) {
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);
133 free_history_entry (last
);
137 if (read (stdfdin
, buffer
, BUFFER_SIZE
) == 0) {
141 char *pt
= line
[nb
++] = buffer
;
142 while (*pt
++ != '\0') {
148 VERBOSE (INFO
, PRINTOUT ("line: '%s'\n", buffer
));
151 /* look for end of line */
152 for (i
= 0; (i
< nb
) && (ret
!= 1); i
++) {
153 if (*line
[i
] == '\0') {
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
]));
161 VERBOSE (INFO
, print_element (element
, 0));
162 PRINTOUT (format
, evaluate_element (element
, 0));
163 delelement (element
);
171 memset (buffer
, 0, BUFFER_SIZE
);
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 + 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
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
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
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 =
236 /* vim: set ts=4 sw=4 et: */