blob: 9c74172ba4f41dafef23b562ae527e28008bb2b4 [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 */
5
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00006#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +00007#include <math.h>
8
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +02009//usage:#define dc_trivial_usage
Denys Vlasenko506d3a92010-07-29 04:06:07 +020010//usage: "EXPRESSION..."
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +020011//usage:
12//usage:#define dc_full_usage "\n\n"
13//usage: "Tiny RPN calculator. Operations:\n"
Denys Vlasenko9daf33f2013-01-18 13:30:13 +010014//usage: "+, add, -, sub, *, mul, /, div, %, mod, "IF_FEATURE_DC_LIBM("**, exp, ")"and, or, not, xor,\n"
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +020015//usage: "p - print top of the stack (without popping),\n"
Denys Vlasenko506d3a92010-07-29 04:06:07 +020016//usage: "f - print entire stack,\n"
17//usage: "o - pop the value and set output radix (must be 10, 16, 8 or 2).\n"
Denys Vlasenko9daf33f2013-01-18 13:30:13 +010018//usage: "Examples: 'dc 2 2 add p' -> 4, 'dc 8 8 mul 2 2 + / p' -> 16"
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +020019//usage:
20//usage:#define dc_example_usage
21//usage: "$ dc 2 2 + p\n"
22//usage: "4\n"
23//usage: "$ dc 8 8 \\* 2 2 + / p\n"
24//usage: "16\n"
25//usage: "$ dc 0 1 and p\n"
26//usage: "0\n"
27//usage: "$ dc 0 1 or p\n"
28//usage: "1\n"
29//usage: "$ echo 72 9 div 8 mul p | dc\n"
30//usage: "64\n"
31
32#if 0
33typedef unsigned data_t;
34#define DATA_FMT ""
35#elif 0
36typedef unsigned long data_t;
37#define DATA_FMT "l"
38#else
39typedef unsigned long long data_t;
40#define DATA_FMT "ll"
41#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000042
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +000043
Denis Vlasenkob44c7902008-03-17 09:29:43 +000044struct globals {
45 unsigned pointer;
46 unsigned base;
47 double stack[1];
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010048} FIX_ALIASING;
Denis Vlasenkob44c7902008-03-17 09:29:43 +000049enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) };
50#define G (*(struct globals*)&bb_common_bufsiz1)
51#define pointer (G.pointer )
52#define base (G.base )
53#define stack (G.stack )
Denis Vlasenkod8850f22008-12-30 10:40:05 +000054#define INIT_G() do { \
55 base = 10; \
56} while (0)
Denis Vlasenkob44c7902008-03-17 09:29:43 +000057
Eric Andersencc8ed391999-10-05 16:24:54 +000058
Denys Vlasenkoc4603fb2015-05-25 13:31:25 +020059static void check_under(void)
60{
61 if (pointer == 0)
62 bb_error_msg_and_die("stack underflow");
63}
64
Erik Andersene49d5ec2000-02-08 19:58:47 +000065static void push(double a)
Eric Andersencc8ed391999-10-05 16:24:54 +000066{
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +000067 if (pointer >= STACK_SIZE)
Manuel Novoa III cad53642003-03-19 09:13:01 +000068 bb_error_msg_and_die("stack overflow");
Matt Kraai3e856ce2000-12-01 02:55:13 +000069 stack[pointer++] = a;
Eric Andersencc8ed391999-10-05 16:24:54 +000070}
71
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000072static double pop(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000073{
Denys Vlasenkoc4603fb2015-05-25 13:31:25 +020074 check_under();
Eric Andersencc8ed391999-10-05 16:24:54 +000075 return stack[--pointer];
76}
77
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000078static void add(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000079{
80 push(pop() + pop());
81}
82
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000083static void sub(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000084{
Erik Andersene49d5ec2000-02-08 19:58:47 +000085 double subtrahend = pop();
Eric Andersencc8ed391999-10-05 16:24:54 +000086
87 push(pop() - subtrahend);
88}
89
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000090static void mul(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000091{
92 push(pop() * pop());
93}
94
Denis Vlasenko07832302008-10-20 08:43:10 +000095#if ENABLE_FEATURE_DC_LIBM
Eric Andersena9287742003-10-22 11:24:39 +000096static void power(void)
97{
98 double topower = pop();
99
100 push(pow(pop(), topower));
101}
Denis Vlasenko07832302008-10-20 08:43:10 +0000102#endif
Eric Andersena9287742003-10-22 11:24:39 +0000103
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000104static void divide(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000105{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106 double divisor = pop();
107
Eric Andersencc8ed391999-10-05 16:24:54 +0000108 push(pop() / divisor);
109}
110
Eric Andersena9287742003-10-22 11:24:39 +0000111static void mod(void)
112{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200113 data_t d = pop();
Eric Andersena9287742003-10-22 11:24:39 +0000114
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200115 push((data_t) pop() % d);
Eric Andersena9287742003-10-22 11:24:39 +0000116}
117
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000118static void and(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000119{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200120 push((data_t) pop() & (data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000121}
122
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000123static void or(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000124{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200125 push((data_t) pop() | (data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000126}
127
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000128static void eor(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000129{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200130 push((data_t) pop() ^ (data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000131}
132
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000133static void not(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000134{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200135 push(~(data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000136}
137
Glenn L McGrath6d074322002-12-12 10:31:53 +0000138static void set_output_base(void)
139{
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000140 static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 };
141 unsigned b = (unsigned)pop();
142
143 base = *strchrnul(bases, b);
144 if (base == 0) {
145 bb_error_msg("error, base %u is not supported", b);
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000146 base = 10;
Glenn L McGrath6d074322002-12-12 10:31:53 +0000147 }
148}
149
150static void print_base(double print)
151{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200152 data_t x, i;
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000153
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200154 x = (data_t) print;
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000155 if (base == 10) {
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200156 if (x == print) /* exactly representable as unsigned integer */
157 printf("%"DATA_FMT"u\n", x);
158 else
159 printf("%g\n", print);
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000160 return;
161 }
162
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000163 switch (base) {
164 case 16:
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200165 printf("%"DATA_FMT"x\n", x);
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000166 break;
167 case 8:
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200168 printf("%"DATA_FMT"o\n", x);
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000169 break;
170 default: /* base 2 */
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200171 i = MAXINT(data_t) - (MAXINT(data_t) >> 1);
172 /* i is 100000...00000 */
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000173 do {
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200174 if (x & i)
175 break;
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000176 i >>= 1;
177 } while (i > 1);
178 do {
179 bb_putchar('1' - !(x & i));
180 i >>= 1;
181 } while (i);
182 bb_putchar('\n');
183 }
Glenn L McGrath6d074322002-12-12 10:31:53 +0000184}
185
186static void print_stack_no_pop(void)
187{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000188 unsigned i = pointer;
Glenn L McGrath6d074322002-12-12 10:31:53 +0000189 while (i)
190 print_base(stack[--i]);
191}
192
193static void print_no_pop(void)
194{
Denys Vlasenkoc4603fb2015-05-25 13:31:25 +0200195 check_under();
Glenn L McGrath6d074322002-12-12 10:31:53 +0000196 print_base(stack[pointer-1]);
197}
198
Eric Andersencc8ed391999-10-05 16:24:54 +0000199struct op {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000200 const char name[4];
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000201 void (*function) (void);
Eric Andersencc8ed391999-10-05 16:24:54 +0000202};
203
Erik Andersene49d5ec2000-02-08 19:58:47 +0000204static const struct op operators[] = {
Denis Vlasenko07832302008-10-20 08:43:10 +0000205#if ENABLE_FEATURE_DC_LIBM
Eric Andersena9287742003-10-22 11:24:39 +0000206 {"**", power},
207 {"exp", power},
208 {"pow", power},
Denis Vlasenko07832302008-10-20 08:43:10 +0000209#endif
Eric Andersena9287742003-10-22 11:24:39 +0000210 {"%", mod},
211 {"mod", mod},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000212 {"and", and},
John Beppuc0352542000-06-21 18:00:46 +0000213 {"or", or},
Erik Andersen5e1189e2000-04-15 16:34:54 +0000214 {"not", not},
215 {"eor", eor},
Eric Andersena9287742003-10-22 11:24:39 +0000216 {"xor", eor},
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100217 {"+", add},
218 {"add", add},
219 {"-", sub},
220 {"sub", sub},
221 {"*", mul},
222 {"mul", mul},
223 {"/", divide},
224 {"div", divide},
Glenn L McGrath6d074322002-12-12 10:31:53 +0000225 {"p", print_no_pop},
226 {"f", print_stack_no_pop},
227 {"o", set_output_base},
Eric Andersencc8ed391999-10-05 16:24:54 +0000228};
229
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100230/* Feed the stack machine */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000231static void stack_machine(const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000232{
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100233 char *end;
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100234 double number;
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100235 const struct op *o;
Eric Andersencc8ed391999-10-05 16:24:54 +0000236
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100237 next:
238 number = strtod(argument, &end);
239 if (end != argument) {
240 argument = end;
241 push(number);
242 goto next;
Eric Andersencc8ed391999-10-05 16:24:54 +0000243 }
244
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100245 /* We might have matched a digit, eventually advance the argument */
246 argument = skip_whitespace(argument);
247
248 if (*argument == '\0')
249 return;
250
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100251 o = operators;
252 do {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100253 char *after_name = is_prefixed_with(argument, o->name);
254 if (after_name) {
255 argument = after_name;
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000256 o->function();
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100257 goto next;
Eric Andersencc8ed391999-10-05 16:24:54 +0000258 }
259 o++;
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100260 } while (o != operators + ARRAY_SIZE(operators));
261
Denys Vlasenkobd1de182009-12-30 18:37:08 +0100262 bb_error_msg_and_die("syntax error at '%s'", argument);
Eric Andersencc8ed391999-10-05 16:24:54 +0000263}
264
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000265int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000266int dc_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000267{
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000268 INIT_G();
269
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000270 argv++;
271 if (!argv[0]) {
272 /* take stuff from stdin if no args are given */
273 char *line;
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000274 while ((line = xmalloc_fgetline(stdin)) != NULL) {
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100275 stack_machine(line);
John Beppu5db60a72000-06-12 22:59:12 +0000276 free(line);
277 }
278 } else {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000279 do {
280 stack_machine(*argv);
281 } while (*++argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000282 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000283 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000284}