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