blob: d4cd49998fa679bde0b0eb06eb99440ebe003bc3 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00002/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00004 */
Denys Vlasenkofb4da162016-11-22 23:14:24 +01005//config:config DC
Denys Vlasenko4eed2c62017-07-18 22:01:24 +02006//config: bool "dc (4.2 kb)"
Denys Vlasenkofb4da162016-11-22 23:14:24 +01007//config: default y
8//config: help
9//config: Dc is a reverse-polish desk calculator which supports unlimited
10//config: precision arithmetic.
11//config:
12//config:config FEATURE_DC_LIBM
13//config: bool "Enable power and exp functions (requires libm)"
14//config: default y
15//config: depends on DC
16//config: help
17//config: Enable power and exp functions.
18//config: NOTE: This will require libm to be present for linking.
Eric Andersencc8ed391999-10-05 16:24:54 +000019
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010020//applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP))
21
22//kbuild:lib-$(CONFIG_DC) += dc.o
23
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +020024//usage:#define dc_trivial_usage
Denys Vlasenko506d3a92010-07-29 04:06:07 +020025//usage: "EXPRESSION..."
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +020026//usage:
27//usage:#define dc_full_usage "\n\n"
28//usage: "Tiny RPN calculator. Operations:\n"
Denys Vlasenko9daf33f2013-01-18 13:30:13 +010029//usage: "+, add, -, sub, *, mul, /, div, %, mod, "IF_FEATURE_DC_LIBM("**, exp, ")"and, or, not, xor,\n"
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +020030//usage: "p - print top of the stack (without popping),\n"
Denys Vlasenko506d3a92010-07-29 04:06:07 +020031//usage: "f - print entire stack,\n"
32//usage: "o - pop the value and set output radix (must be 10, 16, 8 or 2).\n"
Denys Vlasenko9daf33f2013-01-18 13:30:13 +010033//usage: "Examples: 'dc 2 2 add p' -> 4, 'dc 8 8 mul 2 2 + / p' -> 16"
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +020034//usage:
35//usage:#define dc_example_usage
36//usage: "$ dc 2 2 + p\n"
37//usage: "4\n"
38//usage: "$ dc 8 8 \\* 2 2 + / p\n"
39//usage: "16\n"
40//usage: "$ dc 0 1 and p\n"
41//usage: "0\n"
42//usage: "$ dc 0 1 or p\n"
43//usage: "1\n"
44//usage: "$ echo 72 9 div 8 mul p | dc\n"
45//usage: "64\n"
46
Denys Vlasenkofb4da162016-11-22 23:14:24 +010047#include "libbb.h"
48#include "common_bufsiz.h"
49#include <math.h>
50
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +020051#if 0
52typedef unsigned data_t;
53#define DATA_FMT ""
54#elif 0
55typedef unsigned long data_t;
56#define DATA_FMT "l"
57#else
58typedef unsigned long long data_t;
59#define DATA_FMT "ll"
60#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000061
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +000062
Denis Vlasenkob44c7902008-03-17 09:29:43 +000063struct globals {
64 unsigned pointer;
65 unsigned base;
66 double stack[1];
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010067} FIX_ALIASING;
Denis Vlasenkob44c7902008-03-17 09:29:43 +000068enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) };
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020069#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenkob44c7902008-03-17 09:29:43 +000070#define pointer (G.pointer )
71#define base (G.base )
72#define stack (G.stack )
Denis Vlasenkod8850f22008-12-30 10:40:05 +000073#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020074 setup_common_bufsiz(); \
Denis Vlasenkod8850f22008-12-30 10:40:05 +000075 base = 10; \
76} while (0)
Denis Vlasenkob44c7902008-03-17 09:29:43 +000077
Eric Andersencc8ed391999-10-05 16:24:54 +000078
Denys Vlasenkoc4603fb2015-05-25 13:31:25 +020079static void check_under(void)
80{
81 if (pointer == 0)
82 bb_error_msg_and_die("stack underflow");
83}
84
Erik Andersene49d5ec2000-02-08 19:58:47 +000085static void push(double a)
Eric Andersencc8ed391999-10-05 16:24:54 +000086{
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +000087 if (pointer >= STACK_SIZE)
Manuel Novoa III cad53642003-03-19 09:13:01 +000088 bb_error_msg_and_die("stack overflow");
Matt Kraai3e856ce2000-12-01 02:55:13 +000089 stack[pointer++] = a;
Eric Andersencc8ed391999-10-05 16:24:54 +000090}
91
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000092static double pop(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000093{
Denys Vlasenkoc4603fb2015-05-25 13:31:25 +020094 check_under();
Eric Andersencc8ed391999-10-05 16:24:54 +000095 return stack[--pointer];
96}
97
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000098static void add(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000099{
100 push(pop() + pop());
101}
102
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000103static void sub(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000104{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000105 double subtrahend = pop();
Eric Andersencc8ed391999-10-05 16:24:54 +0000106
107 push(pop() - subtrahend);
108}
109
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000110static void mul(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000111{
112 push(pop() * pop());
113}
114
Denis Vlasenko07832302008-10-20 08:43:10 +0000115#if ENABLE_FEATURE_DC_LIBM
Eric Andersena9287742003-10-22 11:24:39 +0000116static void power(void)
117{
118 double topower = pop();
119
120 push(pow(pop(), topower));
121}
Denis Vlasenko07832302008-10-20 08:43:10 +0000122#endif
Eric Andersena9287742003-10-22 11:24:39 +0000123
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000124static void divide(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000125{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000126 double divisor = pop();
127
Eric Andersencc8ed391999-10-05 16:24:54 +0000128 push(pop() / divisor);
129}
130
Eric Andersena9287742003-10-22 11:24:39 +0000131static void mod(void)
132{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200133 data_t d = pop();
Eric Andersena9287742003-10-22 11:24:39 +0000134
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200135 push((data_t) pop() % d);
Eric Andersena9287742003-10-22 11:24:39 +0000136}
137
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000138static void and(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000139{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200140 push((data_t) pop() & (data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000141}
142
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000143static void or(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000144{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200145 push((data_t) pop() | (data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000146}
147
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000148static void eor(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000149{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200150 push((data_t) pop() ^ (data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000151}
152
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000153static void not(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000154{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200155 push(~(data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000156}
157
Glenn L McGrath6d074322002-12-12 10:31:53 +0000158static void set_output_base(void)
159{
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000160 static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 };
161 unsigned b = (unsigned)pop();
162
163 base = *strchrnul(bases, b);
164 if (base == 0) {
165 bb_error_msg("error, base %u is not supported", b);
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000166 base = 10;
Glenn L McGrath6d074322002-12-12 10:31:53 +0000167 }
168}
169
170static void print_base(double print)
171{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200172 data_t x, i;
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000173
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200174 x = (data_t) print;
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000175 if (base == 10) {
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200176 if (x == print) /* exactly representable as unsigned integer */
177 printf("%"DATA_FMT"u\n", x);
178 else
179 printf("%g\n", print);
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000180 return;
181 }
182
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000183 switch (base) {
184 case 16:
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200185 printf("%"DATA_FMT"x\n", x);
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000186 break;
187 case 8:
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200188 printf("%"DATA_FMT"o\n", x);
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000189 break;
190 default: /* base 2 */
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200191 i = MAXINT(data_t) - (MAXINT(data_t) >> 1);
192 /* i is 100000...00000 */
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000193 do {
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200194 if (x & i)
195 break;
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000196 i >>= 1;
197 } while (i > 1);
198 do {
199 bb_putchar('1' - !(x & i));
200 i >>= 1;
201 } while (i);
202 bb_putchar('\n');
203 }
Glenn L McGrath6d074322002-12-12 10:31:53 +0000204}
205
206static void print_stack_no_pop(void)
207{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000208 unsigned i = pointer;
Glenn L McGrath6d074322002-12-12 10:31:53 +0000209 while (i)
210 print_base(stack[--i]);
211}
212
213static void print_no_pop(void)
214{
Denys Vlasenkoc4603fb2015-05-25 13:31:25 +0200215 check_under();
Glenn L McGrath6d074322002-12-12 10:31:53 +0000216 print_base(stack[pointer-1]);
217}
218
Eric Andersencc8ed391999-10-05 16:24:54 +0000219struct op {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000220 const char name[4];
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000221 void (*function) (void);
Eric Andersencc8ed391999-10-05 16:24:54 +0000222};
223
Erik Andersene49d5ec2000-02-08 19:58:47 +0000224static const struct op operators[] = {
Denis Vlasenko07832302008-10-20 08:43:10 +0000225#if ENABLE_FEATURE_DC_LIBM
Eric Andersena9287742003-10-22 11:24:39 +0000226 {"**", power},
227 {"exp", power},
228 {"pow", power},
Denis Vlasenko07832302008-10-20 08:43:10 +0000229#endif
Eric Andersena9287742003-10-22 11:24:39 +0000230 {"%", mod},
231 {"mod", mod},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000232 {"and", and},
John Beppuc0352542000-06-21 18:00:46 +0000233 {"or", or},
Erik Andersen5e1189e2000-04-15 16:34:54 +0000234 {"not", not},
235 {"eor", eor},
Eric Andersena9287742003-10-22 11:24:39 +0000236 {"xor", eor},
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100237 {"+", add},
238 {"add", add},
239 {"-", sub},
240 {"sub", sub},
241 {"*", mul},
242 {"mul", mul},
243 {"/", divide},
244 {"div", divide},
Glenn L McGrath6d074322002-12-12 10:31:53 +0000245 {"p", print_no_pop},
246 {"f", print_stack_no_pop},
247 {"o", set_output_base},
Eric Andersencc8ed391999-10-05 16:24:54 +0000248};
249
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100250/* Feed the stack machine */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000251static void stack_machine(const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000252{
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100253 char *end;
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100254 double number;
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100255 const struct op *o;
Eric Andersencc8ed391999-10-05 16:24:54 +0000256
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100257 next:
258 number = strtod(argument, &end);
259 if (end != argument) {
260 argument = end;
261 push(number);
262 goto next;
Eric Andersencc8ed391999-10-05 16:24:54 +0000263 }
264
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100265 /* We might have matched a digit, eventually advance the argument */
266 argument = skip_whitespace(argument);
267
268 if (*argument == '\0')
269 return;
270
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100271 o = operators;
272 do {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100273 char *after_name = is_prefixed_with(argument, o->name);
274 if (after_name) {
275 argument = after_name;
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000276 o->function();
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100277 goto next;
Eric Andersencc8ed391999-10-05 16:24:54 +0000278 }
279 o++;
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100280 } while (o != operators + ARRAY_SIZE(operators));
281
Denys Vlasenkobd1de182009-12-30 18:37:08 +0100282 bb_error_msg_and_die("syntax error at '%s'", argument);
Eric Andersencc8ed391999-10-05 16:24:54 +0000283}
284
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000285int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000286int dc_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000287{
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000288 INIT_G();
289
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000290 argv++;
291 if (!argv[0]) {
292 /* take stuff from stdin if no args are given */
293 char *line;
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000294 while ((line = xmalloc_fgetline(stdin)) != NULL) {
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100295 stack_machine(line);
John Beppu5db60a72000-06-12 22:59:12 +0000296 free(line);
297 }
298 } else {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000299 do {
300 stack_machine(*argv);
301 } while (*++argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000302 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000303 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000304}