1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
6 //config: bool "dc (4.2 kb)"
9 //config: Dc is a reverse-polish desk calculator which supports unlimited
10 //config: precision arithmetic.
12 //config:config FEATURE_DC_LIBM
13 //config: bool "Enable power and exp functions (requires libm)"
15 //config: depends on DC
17 //config: Enable power and exp functions.
18 //config: NOTE: This will require libm to be present for linking.
20 //applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP))
22 //kbuild:lib-$(CONFIG_DC) += dc.o
24 //usage:#define dc_trivial_usage
25 //usage: "EXPRESSION..."
27 //usage:#define dc_full_usage "\n\n"
28 //usage: "Tiny RPN calculator. Operations:\n"
29 //usage: "+, add, -, sub, *, mul, /, div, %, mod, "IF_FEATURE_DC_LIBM("**, exp, ")"and, or, not, xor,\n"
30 //usage: "p - print top of the stack (without popping),\n"
31 //usage: "f - print entire stack,\n"
32 //usage: "o - pop the value and set output radix (must be 10, 16, 8 or 2).\n"
33 //usage: "Examples: 'dc 2 2 add p' -> 4, 'dc 8 8 mul 2 2 + / p' -> 16"
35 //usage:#define dc_example_usage
36 //usage: "$ dc 2 2 + p\n"
38 //usage: "$ dc 8 8 \\* 2 2 + / p\n"
40 //usage: "$ dc 0 1 and p\n"
42 //usage: "$ dc 0 1 or p\n"
44 //usage: "$ echo 72 9 div 8 mul p | dc\n"
48 #include "common_bufsiz.h"
52 typedef unsigned data_t;
55 typedef unsigned long data_t;
58 typedef unsigned long long data_t;
68 enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) };
69 #define G (*(struct globals*)bb_common_bufsiz1)
70 #define pointer (G.pointer )
71 #define base (G.base )
72 #define stack (G.stack )
73 #define INIT_G() do { \
74 setup_common_bufsiz(); \
79 static void check_under(void)
82 bb_error_msg_and_die("stack underflow");
85 static void push(double a)
87 if (pointer >= STACK_SIZE)
88 bb_error_msg_and_die("stack overflow");
92 static double pop(void)
95 return stack[--pointer];
103 static void sub(void)
105 double subtrahend = pop();
107 push(pop() - subtrahend);
110 static void mul(void)
115 #if ENABLE_FEATURE_DC_LIBM
116 static void power(void)
118 double topower = pop();
120 push(pow(pop(), topower));
124 static void divide(void)
126 double divisor = pop();
128 push(pop() / divisor);
131 static void mod(void)
135 push((data_t) pop() % d);
138 static void and(void)
140 push((data_t) pop() & (data_t) pop());
145 push((data_t) pop() | (data_t) pop());
148 static void eor(void)
150 push((data_t) pop() ^ (data_t) pop());
153 static void not(void)
155 push(~(data_t) pop());
158 static void set_output_base(void)
160 static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 };
161 unsigned b = (unsigned)pop();
163 base = *strchrnul(bases, b);
165 bb_error_msg("error, base %u is not supported", b);
170 static void print_base(double print)
176 if (x == print) /* exactly representable as unsigned integer */
177 printf("%"DATA_FMT"u\n", x);
179 printf("%g\n", print);
185 printf("%"DATA_FMT"x\n", x);
188 printf("%"DATA_FMT"o\n", x);
190 default: /* base 2 */
191 i = MAXINT(data_t) - (MAXINT(data_t) >> 1);
192 /* i is 100000...00000 */
199 bb_putchar('1' - !(x & i));
206 static void print_stack_no_pop(void)
208 unsigned i = pointer;
210 print_base(stack[--i]);
213 static void print_no_pop(void)
216 print_base(stack[pointer-1]);
221 void (*function) (void);
224 static const struct op operators[] = {
225 #if ENABLE_FEATURE_DC_LIBM
246 {"f", print_stack_no_pop},
247 {"o", set_output_base},
250 /* Feed the stack machine */
251 static void stack_machine(const char *argument)
258 number = strtod(argument, &end);
259 if (end != argument) {
265 /* We might have matched a digit, eventually advance the argument */
266 argument = skip_whitespace(argument);
268 if (*argument == '\0')
273 char *after_name = is_prefixed_with(argument, o->name);
275 argument = after_name;
280 } while (o != operators + ARRAY_SIZE(operators));
282 bb_error_msg_and_die("syntax error at '%s'", argument);
285 int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
286 int dc_main(int argc UNUSED_PARAM, char **argv)
292 /* take stuff from stdin if no args are given */
294 while ((line = xmalloc_fgetline(stdin)) != NULL) {
300 stack_machine(*argv);