Magellan Linux

Annotation of /trunk/mkinitrd-magellan/busybox/miscutils/dc.c

Parent Directory Parent Directory | Revision Log Revision Log


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