dc: conditionalize parts which require libm
[oweals/busybox.git] / miscutils / dc.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
4  */
5
6 #include "libbb.h"
7 #include <math.h>
8
9 /* Tiny RPN calculator, because "expr" didn't give me bitwise operations. */
10
11
12 struct globals {
13         unsigned pointer;
14         unsigned base;
15         double stack[1];
16 };
17 enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) };
18 #define G (*(struct globals*)&bb_common_bufsiz1)
19 #define pointer   (G.pointer   )
20 #define base      (G.base      )
21 #define stack     (G.stack     )
22 #define INIT_G() do { } while (0)
23
24
25 static void push(double a)
26 {
27         if (pointer >= STACK_SIZE)
28                 bb_error_msg_and_die("stack overflow");
29         stack[pointer++] = a;
30 }
31
32 static double pop(void)
33 {
34         if (pointer == 0)
35                 bb_error_msg_and_die("stack underflow");
36         return stack[--pointer];
37 }
38
39 static void add(void)
40 {
41         push(pop() + pop());
42 }
43
44 static void sub(void)
45 {
46         double subtrahend = pop();
47
48         push(pop() - subtrahend);
49 }
50
51 static void mul(void)
52 {
53         push(pop() * pop());
54 }
55
56 #if ENABLE_FEATURE_DC_LIBM
57 static void power(void)
58 {
59         double topower = pop();
60
61         push(pow(pop(), topower));
62 }
63 #endif
64
65 static void divide(void)
66 {
67         double divisor = pop();
68
69         push(pop() / divisor);
70 }
71
72 static void mod(void)
73 {
74         unsigned d = pop();
75
76         push((unsigned) pop() % d);
77 }
78
79 static void and(void)
80 {
81         push((unsigned) pop() & (unsigned) pop());
82 }
83
84 static void or(void)
85 {
86         push((unsigned) pop() | (unsigned) pop());
87 }
88
89 static void eor(void)
90 {
91         push((unsigned) pop() ^ (unsigned) pop());
92 }
93
94 static void not(void)
95 {
96         push(~(unsigned) pop());
97 }
98
99 static void set_output_base(void)
100 {
101         base = (unsigned)pop();
102         if ((base != 10) && (base != 16)) {
103                 bb_error_msg("error, base %d is not supported", base);
104                 base = 10;
105         }
106 }
107
108 static void print_base(double print)
109 {
110         if (base == 16)
111                 printf("%x\n", (unsigned)print);
112         else
113                 printf("%g\n", print);
114 }
115
116 static void print_stack_no_pop(void)
117 {
118         unsigned i = pointer;
119         while (i)
120                 print_base(stack[--i]);
121 }
122
123 static void print_no_pop(void)
124 {
125         print_base(stack[pointer-1]);
126 }
127
128 struct op {
129         const char name[4];
130         void (*function) (void);
131 };
132
133 static const struct op operators[] = {
134         {"+",   add},
135         {"add", add},
136         {"-",   sub},
137         {"sub", sub},
138         {"*",   mul},
139         {"mul", mul},
140         {"/",   divide},
141         {"div", divide},
142 #if ENABLE_FEATURE_DC_LIBM
143         {"**",  power},
144         {"exp", power},
145         {"pow", power},
146 #endif
147         {"%",   mod},
148         {"mod", mod},
149         {"and", and},
150         {"or",  or},
151         {"not", not},
152         {"eor", eor},
153         {"xor", eor},
154         {"p", print_no_pop},
155         {"f", print_stack_no_pop},
156         {"o", set_output_base},
157         { /* zero filled */ }
158 };
159
160 static void stack_machine(const char *argument)
161 {
162         char *endPointer;
163         double d;
164         const struct op *o = operators;
165
166         if (argument == 0)
167                 return;
168
169         d = strtod(argument, &endPointer);
170
171         if (endPointer != argument) {
172                 push(d);
173                 return;
174         }
175
176         while (o->name[0]) {
177                 if (strcmp(o->name, argument) == 0) {
178                         o->function();
179                         return;
180                 }
181                 o++;
182         }
183         bb_error_msg_and_die("%s: syntax error", argument);
184 }
185
186 /* return pointer to next token in buffer and set *buffer to one char
187  * past the end of the above mentioned token
188  */
189 static char *get_token(char **buffer)
190 {
191         char *current = skip_whitespace(*buffer);
192         if (*current != '\0') {
193                 *buffer = skip_non_whitespace(current);
194                 return current;
195         }
196         return NULL;
197 }
198
199 int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
200 int dc_main(int argc UNUSED_PARAM, char **argv)
201 {
202         INIT_G();
203
204         argv++;
205         if (!argv[0]) {
206                 /* take stuff from stdin if no args are given */
207                 char *line;
208                 char *cursor;
209                 char *token;
210                 while ((line = xmalloc_fgetline(stdin)) != NULL) {
211                         cursor = line;
212                         while (1) {
213                                 token = get_token(&cursor);
214                                 if (!token) break;
215                                 *cursor++ = '\0';
216                                 stack_machine(token);
217                         }
218                         free(line);
219                 }
220         } else {
221                 if (argv[0][0] == '-')
222                         bb_show_usage();
223                 do {
224                         stack_machine(*argv);
225                 } while (*++argv);
226         }
227         return EXIT_SUCCESS;
228 }