1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
6 /* config/applet/usage bits are in bc.c */
9 //#include "common_bufsiz.h"
13 typedef unsigned data_t;
16 typedef unsigned long data_t;
19 typedef unsigned long long data_t;
28 enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) };
29 #define G (*(struct globals*)bb_common_bufsiz1)
30 #define pointer (G.pointer )
31 #define base (G.base )
32 #define stack (G.stack )
33 #define INIT_G() do { \
34 setup_common_bufsiz(); \
38 static unsigned check_under(void)
42 bb_simple_error_msg_and_die("stack underflow");
46 static void push(double a)
48 if (pointer >= STACK_SIZE)
49 bb_simple_error_msg_and_die("stack overflow");
53 static double pop(void)
55 unsigned p = check_under();
67 double subtrahend = pop();
69 push(pop() - subtrahend);
77 #if ENABLE_FEATURE_DC_LIBM
78 static void power(void)
80 double topower = pop();
82 push(pow(pop(), topower));
86 static void divide(void)
88 double divisor = pop();
90 push(pop() / divisor);
97 /* compat with dc (GNU bc 1.07.1) 1.4.1:
99 * dc: remainder by zero
103 bb_error_msg("remainder by zero");
108 /* ^^^^ without this, we simply get SIGFPE and die */
110 push((data_t) pop() % d);
113 static void and(void)
115 push((data_t) pop() & (data_t) pop());
120 push((data_t) pop() | (data_t) pop());
123 static void eor(void)
125 push((data_t) pop() ^ (data_t) pop());
128 static void not(void)
130 push(~(data_t) pop());
133 static void set_output_base(void)
135 static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 };
136 unsigned b = (unsigned)pop();
138 base = *strchrnul(bases, b);
140 bb_error_msg("error, base %u is not supported", b);
145 static void print_base(double print)
151 if (x == print) /* exactly representable as unsigned integer */
152 printf("%"DATA_FMT"u\n", x);
154 printf("%g\n", print);
160 printf("%"DATA_FMT"x\n", x);
163 printf("%"DATA_FMT"o\n", x);
165 default: /* base 2 */
166 i = MAXINT(data_t) - (MAXINT(data_t) >> 1);
167 /* i is 100000...00000 */
174 bb_putchar('1' - !(x & i));
181 static void print_stack_no_pop(void)
183 unsigned i = pointer;
185 print_base(stack[--i]);
188 static void print_no_pop(void)
190 print_base(stack[check_under()]);
195 void (*function) (void);
198 static const struct op operators[] = {
199 #if ENABLE_FEATURE_DC_LIBM
206 // logic ops are not standard, remove?
220 {"f", print_stack_no_pop},
221 {"o", set_output_base},
224 /* Feed the stack machine */
225 static void stack_machine(const char *argument)
232 number = strtod(argument, &end);
233 if (end != argument) {
239 /* We might have matched a digit, eventually advance the argument */
240 argument = skip_whitespace(argument);
242 if (*argument == '\0')
247 char *after_name = is_prefixed_with(argument, o->name);
249 argument = after_name;
254 } while (o != operators + ARRAY_SIZE(operators));
256 bb_error_msg_and_die("syntax error at '%s'", argument);
259 static void process_file(FILE *fp)
262 while ((line = xmalloc_fgetline(fp)) != NULL) {
268 int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
269 int dc_main(int argc UNUSED_PARAM, char **argv)
275 /* Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs */
277 int n = getopt(argc, argv, "e:f:");
283 stack_machine(optarg);
287 process_file(xfopen_for_read(optarg));
297 process_file(xfopen_for_read(*argv++));
299 } else if (!script) {
300 /* Take stuff from stdin if no args are given */