blob: d58f97e91d344f2f6f378a9f050d90aab689673d [file] [log] [blame]
Gavin Howard01055ba2018-11-03 11:00:21 -06001#if 0 //TODO: use if bc is not selected
Erik Andersene49d5ec2000-02-08 19:58:47 +00002/* vi: set sw=4 ts=4: */
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00003/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02004 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00005 */
Gavin Howard01055ba2018-11-03 11:00:21 -06006 //config:config DC
7 //config: bool "dc (4.2 kb)"
8 //config: default y
9 //config: help
10 //config: Dc is a reverse-polish desk calculator which supports unlimited
11 //config: precision arithmetic.
12 //config:
13 //config:config FEATURE_DC_LIBM
14 //config: bool "Enable power and exp functions (requires libm)"
15 //config: default y
16 //config: depends on DC
17 //config: help
18 //config: Enable power and exp functions.
19 //config: NOTE: This will require libm to be present for linking.
Eric Andersencc8ed391999-10-05 16:24:54 +000020
Gavin Howard01055ba2018-11-03 11:00:21 -060021 //applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP))
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010022
Gavin Howard01055ba2018-11-03 11:00:21 -060023 //kbuild:lib-$(CONFIG_DC) += dc.o
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010024
Gavin Howard01055ba2018-11-03 11:00:21 -060025 //usage:#define dc_trivial_usage
26 //usage: "EXPRESSION..."
27 //usage:
28 //usage:#define dc_full_usage "\n\n"
29 //usage: "Tiny RPN calculator. Operations:\n"
30 //usage: "+, add, -, sub, *, mul, /, div, %, mod, "IF_FEATURE_DC_LIBM("**, exp, ")"and, or, not, xor,\n"
31 //usage: "p - print top of the stack (without popping),\n"
32 //usage: "f - print entire stack,\n"
33 //usage: "o - pop the value and set output radix (must be 10, 16, 8 or 2).\n"
34 //usage: "Examples: 'dc 2 2 add p' -> 4, 'dc 8 8 mul 2 2 + / p' -> 16"
35 //usage:
36 //usage:#define dc_example_usage
37 //usage: "$ dc 2 2 + p\n"
38 //usage: "4\n"
39 //usage: "$ dc 8 8 \\* 2 2 + / p\n"
40 //usage: "16\n"
41 //usage: "$ dc 0 1 and p\n"
42 //usage: "0\n"
43 //usage: "$ dc 0 1 or p\n"
44 //usage: "1\n"
45 //usage: "$ echo 72 9 div 8 mul p | dc\n"
46 //usage: "64\n"
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +020047
Denys Vlasenkofb4da162016-11-22 23:14:24 +010048#include "libbb.h"
49#include "common_bufsiz.h"
50#include <math.h>
51
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +020052#if 0
53typedef unsigned data_t;
54#define DATA_FMT ""
55#elif 0
56typedef unsigned long data_t;
57#define DATA_FMT "l"
58#else
59typedef unsigned long long data_t;
60#define DATA_FMT "ll"
61#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000062
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +000063
Denis Vlasenkob44c7902008-03-17 09:29:43 +000064struct globals {
65 unsigned pointer;
66 unsigned base;
67 double stack[1];
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010068} FIX_ALIASING;
Denis Vlasenkob44c7902008-03-17 09:29:43 +000069enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) };
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020070#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenkob44c7902008-03-17 09:29:43 +000071#define pointer (G.pointer )
72#define base (G.base )
73#define stack (G.stack )
Denis Vlasenkod8850f22008-12-30 10:40:05 +000074#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020075 setup_common_bufsiz(); \
Denis Vlasenkod8850f22008-12-30 10:40:05 +000076 base = 10; \
77} while (0)
Denis Vlasenkob44c7902008-03-17 09:29:43 +000078
Eric Andersencc8ed391999-10-05 16:24:54 +000079
Denys Vlasenkoc4603fb2015-05-25 13:31:25 +020080static void check_under(void)
81{
82 if (pointer == 0)
83 bb_error_msg_and_die("stack underflow");
84}
85
Erik Andersene49d5ec2000-02-08 19:58:47 +000086static void push(double a)
Eric Andersencc8ed391999-10-05 16:24:54 +000087{
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +000088 if (pointer >= STACK_SIZE)
Manuel Novoa III cad53642003-03-19 09:13:01 +000089 bb_error_msg_and_die("stack overflow");
Matt Kraai3e856ce2000-12-01 02:55:13 +000090 stack[pointer++] = a;
Eric Andersencc8ed391999-10-05 16:24:54 +000091}
92
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000093static double pop(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000094{
Denys Vlasenkoc4603fb2015-05-25 13:31:25 +020095 check_under();
Eric Andersencc8ed391999-10-05 16:24:54 +000096 return stack[--pointer];
97}
98
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000099static void add(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000100{
101 push(pop() + pop());
102}
103
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000104static void sub(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000105{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106 double subtrahend = pop();
Eric Andersencc8ed391999-10-05 16:24:54 +0000107
108 push(pop() - subtrahend);
109}
110
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000111static void mul(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000112{
113 push(pop() * pop());
114}
115
Denis Vlasenko07832302008-10-20 08:43:10 +0000116#if ENABLE_FEATURE_DC_LIBM
Eric Andersena9287742003-10-22 11:24:39 +0000117static void power(void)
118{
119 double topower = pop();
120
121 push(pow(pop(), topower));
122}
Denis Vlasenko07832302008-10-20 08:43:10 +0000123#endif
Eric Andersena9287742003-10-22 11:24:39 +0000124
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000125static void divide(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000126{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127 double divisor = pop();
128
Eric Andersencc8ed391999-10-05 16:24:54 +0000129 push(pop() / divisor);
130}
131
Eric Andersena9287742003-10-22 11:24:39 +0000132static void mod(void)
133{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200134 data_t d = pop();
Eric Andersena9287742003-10-22 11:24:39 +0000135
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200136 push((data_t) pop() % d);
Eric Andersena9287742003-10-22 11:24:39 +0000137}
138
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000139static void and(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000140{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200141 push((data_t) pop() & (data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000142}
143
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000144static void or(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000145{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200146 push((data_t) pop() | (data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000147}
148
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000149static void eor(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000150{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200151 push((data_t) pop() ^ (data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000152}
153
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000154static void not(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000155{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200156 push(~(data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000157}
158
Glenn L McGrath6d074322002-12-12 10:31:53 +0000159static void set_output_base(void)
160{
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000161 static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 };
162 unsigned b = (unsigned)pop();
163
164 base = *strchrnul(bases, b);
165 if (base == 0) {
166 bb_error_msg("error, base %u is not supported", b);
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000167 base = 10;
Glenn L McGrath6d074322002-12-12 10:31:53 +0000168 }
169}
170
171static void print_base(double print)
172{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200173 data_t x, i;
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000174
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200175 x = (data_t) print;
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000176 if (base == 10) {
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200177 if (x == print) /* exactly representable as unsigned integer */
178 printf("%"DATA_FMT"u\n", x);
179 else
180 printf("%g\n", print);
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000181 return;
182 }
183
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000184 switch (base) {
185 case 16:
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200186 printf("%"DATA_FMT"x\n", x);
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000187 break;
188 case 8:
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200189 printf("%"DATA_FMT"o\n", x);
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000190 break;
191 default: /* base 2 */
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200192 i = MAXINT(data_t) - (MAXINT(data_t) >> 1);
193 /* i is 100000...00000 */
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000194 do {
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200195 if (x & i)
196 break;
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000197 i >>= 1;
198 } while (i > 1);
199 do {
200 bb_putchar('1' - !(x & i));
201 i >>= 1;
202 } while (i);
203 bb_putchar('\n');
204 }
Glenn L McGrath6d074322002-12-12 10:31:53 +0000205}
206
207static void print_stack_no_pop(void)
208{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000209 unsigned i = pointer;
Glenn L McGrath6d074322002-12-12 10:31:53 +0000210 while (i)
211 print_base(stack[--i]);
212}
213
214static void print_no_pop(void)
215{
Denys Vlasenkoc4603fb2015-05-25 13:31:25 +0200216 check_under();
Glenn L McGrath6d074322002-12-12 10:31:53 +0000217 print_base(stack[pointer-1]);
218}
219
Eric Andersencc8ed391999-10-05 16:24:54 +0000220struct op {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000221 const char name[4];
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000222 void (*function) (void);
Eric Andersencc8ed391999-10-05 16:24:54 +0000223};
224
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225static const struct op operators[] = {
Denis Vlasenko07832302008-10-20 08:43:10 +0000226#if ENABLE_FEATURE_DC_LIBM
Eric Andersena9287742003-10-22 11:24:39 +0000227 {"**", power},
228 {"exp", power},
229 {"pow", power},
Denis Vlasenko07832302008-10-20 08:43:10 +0000230#endif
Eric Andersena9287742003-10-22 11:24:39 +0000231 {"%", mod},
232 {"mod", mod},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000233 {"and", and},
John Beppuc0352542000-06-21 18:00:46 +0000234 {"or", or},
Erik Andersen5e1189e2000-04-15 16:34:54 +0000235 {"not", not},
236 {"eor", eor},
Eric Andersena9287742003-10-22 11:24:39 +0000237 {"xor", eor},
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100238 {"+", add},
239 {"add", add},
240 {"-", sub},
241 {"sub", sub},
242 {"*", mul},
243 {"mul", mul},
244 {"/", divide},
245 {"div", divide},
Glenn L McGrath6d074322002-12-12 10:31:53 +0000246 {"p", print_no_pop},
247 {"f", print_stack_no_pop},
248 {"o", set_output_base},
Eric Andersencc8ed391999-10-05 16:24:54 +0000249};
250
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100251/* Feed the stack machine */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000252static void stack_machine(const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000253{
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100254 char *end;
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100255 double number;
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100256 const struct op *o;
Eric Andersencc8ed391999-10-05 16:24:54 +0000257
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100258 next:
259 number = strtod(argument, &end);
260 if (end != argument) {
261 argument = end;
262 push(number);
263 goto next;
Eric Andersencc8ed391999-10-05 16:24:54 +0000264 }
265
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100266 /* We might have matched a digit, eventually advance the argument */
267 argument = skip_whitespace(argument);
268
269 if (*argument == '\0')
270 return;
271
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100272 o = operators;
273 do {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100274 char *after_name = is_prefixed_with(argument, o->name);
275 if (after_name) {
276 argument = after_name;
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000277 o->function();
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100278 goto next;
Eric Andersencc8ed391999-10-05 16:24:54 +0000279 }
280 o++;
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100281 } while (o != operators + ARRAY_SIZE(operators));
282
Denys Vlasenkobd1de182009-12-30 18:37:08 +0100283 bb_error_msg_and_die("syntax error at '%s'", argument);
Eric Andersencc8ed391999-10-05 16:24:54 +0000284}
285
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000286int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000287int dc_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000288{
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000289 INIT_G();
290
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000291 argv++;
292 if (!argv[0]) {
293 /* take stuff from stdin if no args are given */
294 char *line;
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000295 while ((line = xmalloc_fgetline(stdin)) != NULL) {
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100296 stack_machine(line);
John Beppu5db60a72000-06-12 22:59:12 +0000297 free(line);
298 }
299 } else {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000300 do {
301 stack_machine(*argv);
302 } while (*++argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000303 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000304 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000305}
Gavin Howard01055ba2018-11-03 11:00:21 -0600306#endif