1
2
3
4
5
6#include "libbb.h"
7#include <math.h>
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32#if 0
33typedef unsigned data_t;
34#define DATA_FMT ""
35#elif 0
36typedef unsigned long data_t;
37#define DATA_FMT "l"
38#else
39typedef unsigned long long data_t;
40#define DATA_FMT "ll"
41#endif
42
43
44struct globals {
45 unsigned pointer;
46 unsigned base;
47 double stack[1];
48} FIX_ALIASING;
49enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) };
50#define G (*(struct globals*)&bb_common_bufsiz1)
51#define pointer (G.pointer )
52#define base (G.base )
53#define stack (G.stack )
54#define INIT_G() do { \
55 base = 10; \
56} while (0)
57
58
59static void push(double a)
60{
61 if (pointer >= STACK_SIZE)
62 bb_error_msg_and_die("stack overflow");
63 stack[pointer++] = a;
64}
65
66static double pop(void)
67{
68 if (pointer == 0)
69 bb_error_msg_and_die("stack underflow");
70 return stack[--pointer];
71}
72
73static void add(void)
74{
75 push(pop() + pop());
76}
77
78static void sub(void)
79{
80 double subtrahend = pop();
81
82 push(pop() - subtrahend);
83}
84
85static void mul(void)
86{
87 push(pop() * pop());
88}
89
90#if ENABLE_FEATURE_DC_LIBM
91static void power(void)
92{
93 double topower = pop();
94
95 push(pow(pop(), topower));
96}
97#endif
98
99static void divide(void)
100{
101 double divisor = pop();
102
103 push(pop() / divisor);
104}
105
106static void mod(void)
107{
108 data_t d = pop();
109
110 push((data_t) pop() % d);
111}
112
113static void and(void)
114{
115 push((data_t) pop() & (data_t) pop());
116}
117
118static void or(void)
119{
120 push((data_t) pop() | (data_t) pop());
121}
122
123static void eor(void)
124{
125 push((data_t) pop() ^ (data_t) pop());
126}
127
128static void not(void)
129{
130 push(~(data_t) pop());
131}
132
133static void set_output_base(void)
134{
135 static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 };
136 unsigned b = (unsigned)pop();
137
138 base = *strchrnul(bases, b);
139 if (base == 0) {
140 bb_error_msg("error, base %u is not supported", b);
141 base = 10;
142 }
143}
144
145static void print_base(double print)
146{
147 data_t x, i;
148
149 x = (data_t) print;
150 if (base == 10) {
151 if (x == print)
152 printf("%"DATA_FMT"u\n", x);
153 else
154 printf("%g\n", print);
155 return;
156 }
157
158 switch (base) {
159 case 16:
160 printf("%"DATA_FMT"x\n", x);
161 break;
162 case 8:
163 printf("%"DATA_FMT"o\n", x);
164 break;
165 default:
166 i = MAXINT(data_t) - (MAXINT(data_t) >> 1);
167
168 do {
169 if (x & i)
170 break;
171 i >>= 1;
172 } while (i > 1);
173 do {
174 bb_putchar('1' - !(x & i));
175 i >>= 1;
176 } while (i);
177 bb_putchar('\n');
178 }
179}
180
181static void print_stack_no_pop(void)
182{
183 unsigned i = pointer;
184 while (i)
185 print_base(stack[--i]);
186}
187
188static void print_no_pop(void)
189{
190 print_base(stack[pointer-1]);
191}
192
193struct op {
194 const char name[4];
195 void (*function) (void);
196};
197
198static const struct op operators[] = {
199 {"+", add},
200 {"add", add},
201 {"-", sub},
202 {"sub", sub},
203 {"*", mul},
204 {"mul", mul},
205 {"/", divide},
206 {"div", divide},
207#if ENABLE_FEATURE_DC_LIBM
208 {"**", power},
209 {"exp", power},
210 {"pow", power},
211#endif
212 {"%", mod},
213 {"mod", mod},
214 {"and", and},
215 {"or", or},
216 {"not", not},
217 {"eor", eor},
218 {"xor", eor},
219 {"p", print_no_pop},
220 {"f", print_stack_no_pop},
221 {"o", set_output_base},
222 { "", NULL }
223};
224
225static void stack_machine(const char *argument)
226{
227 char *endPointer;
228 double d;
229 const struct op *o = operators;
230
231 d = strtod(argument, &endPointer);
232
233 if (endPointer != argument && *endPointer == '\0') {
234 push(d);
235 return;
236 }
237
238 while (o->function) {
239 if (strcmp(o->name, argument) == 0) {
240 o->function();
241 return;
242 }
243 o++;
244 }
245 bb_error_msg_and_die("syntax error at '%s'", argument);
246}
247
248int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
249int dc_main(int argc UNUSED_PARAM, char **argv)
250{
251 INIT_G();
252
253 argv++;
254 if (!argv[0]) {
255
256 char *line;
257 char *cursor;
258 char *token;
259 while ((line = xmalloc_fgetline(stdin)) != NULL) {
260 cursor = line;
261 while (1) {
262 token = skip_whitespace(cursor);
263 if (*token == '\0')
264 break;
265 cursor = skip_non_whitespace(token);
266 if (*cursor != '\0')
267 *cursor++ = '\0';
268 stack_machine(token);
269 }
270 free(line);
271 }
272 } else {
273
274
275
276 do {
277 stack_machine(*argv);
278 } while (*++argv);
279 }
280 return EXIT_SUCCESS;
281}
282