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