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