blob: 3fbb89f5b88960819fba39a144cfa7cad4c13bbe [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"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +02007#include "common_bufsiz.h"
Eric Andersencc8ed391999-10-05 16:24:54 +00008#include <math.h>
9
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +020010//usage:#define dc_trivial_usage
Denys Vlasenko506d3a92010-07-29 04:06:07 +020011//usage: "EXPRESSION..."
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +020012//usage:
13//usage:#define dc_full_usage "\n\n"
14//usage: "Tiny RPN calculator. Operations:\n"
Denys Vlasenko9daf33f2013-01-18 13:30:13 +010015//usage: "+, add, -, sub, *, mul, /, div, %, mod, "IF_FEATURE_DC_LIBM("**, exp, ")"and, or, not, xor,\n"
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +020016//usage: "p - print top of the stack (without popping),\n"
Denys Vlasenko506d3a92010-07-29 04:06:07 +020017//usage: "f - print entire stack,\n"
18//usage: "o - pop the value and set output radix (must be 10, 16, 8 or 2).\n"
Denys Vlasenko9daf33f2013-01-18 13:30:13 +010019//usage: "Examples: 'dc 2 2 add p' -> 4, 'dc 8 8 mul 2 2 + / p' -> 16"
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +020020//usage:
21//usage:#define dc_example_usage
22//usage: "$ dc 2 2 + p\n"
23//usage: "4\n"
24//usage: "$ dc 8 8 \\* 2 2 + / p\n"
25//usage: "16\n"
26//usage: "$ dc 0 1 and p\n"
27//usage: "0\n"
28//usage: "$ dc 0 1 or p\n"
29//usage: "1\n"
30//usage: "$ echo 72 9 div 8 mul p | dc\n"
31//usage: "64\n"
32
33#if 0
34typedef unsigned data_t;
35#define DATA_FMT ""
36#elif 0
37typedef unsigned long data_t;
38#define DATA_FMT "l"
39#else
40typedef unsigned long long data_t;
41#define DATA_FMT "ll"
42#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000043
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +000044
Denis Vlasenkob44c7902008-03-17 09:29:43 +000045struct globals {
46 unsigned pointer;
47 unsigned base;
48 double stack[1];
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010049} FIX_ALIASING;
Denis Vlasenkob44c7902008-03-17 09:29:43 +000050enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) };
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020051#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenkob44c7902008-03-17 09:29:43 +000052#define pointer (G.pointer )
53#define base (G.base )
54#define stack (G.stack )
Denis Vlasenkod8850f22008-12-30 10:40:05 +000055#define INIT_G() do { \
56 base = 10; \
57} while (0)
Denis Vlasenkob44c7902008-03-17 09:29:43 +000058
Eric Andersencc8ed391999-10-05 16:24:54 +000059
Denys Vlasenkoc4603fb2015-05-25 13:31:25 +020060static void check_under(void)
61{
62 if (pointer == 0)
63 bb_error_msg_and_die("stack underflow");
64}
65
Erik Andersene49d5ec2000-02-08 19:58:47 +000066static void push(double a)
Eric Andersencc8ed391999-10-05 16:24:54 +000067{
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +000068 if (pointer >= STACK_SIZE)
Manuel Novoa III cad53642003-03-19 09:13:01 +000069 bb_error_msg_and_die("stack overflow");
Matt Kraai3e856ce2000-12-01 02:55:13 +000070 stack[pointer++] = a;
Eric Andersencc8ed391999-10-05 16:24:54 +000071}
72
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000073static double pop(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000074{
Denys Vlasenkoc4603fb2015-05-25 13:31:25 +020075 check_under();
Eric Andersencc8ed391999-10-05 16:24:54 +000076 return stack[--pointer];
77}
78
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000079static void add(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000080{
81 push(pop() + pop());
82}
83
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000084static void sub(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000085{
Erik Andersene49d5ec2000-02-08 19:58:47 +000086 double subtrahend = pop();
Eric Andersencc8ed391999-10-05 16:24:54 +000087
88 push(pop() - subtrahend);
89}
90
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000091static void mul(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000092{
93 push(pop() * pop());
94}
95
Denis Vlasenko07832302008-10-20 08:43:10 +000096#if ENABLE_FEATURE_DC_LIBM
Eric Andersena9287742003-10-22 11:24:39 +000097static void power(void)
98{
99 double topower = pop();
100
101 push(pow(pop(), topower));
102}
Denis Vlasenko07832302008-10-20 08:43:10 +0000103#endif
Eric Andersena9287742003-10-22 11:24:39 +0000104
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000105static void divide(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000106{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000107 double divisor = pop();
108
Eric Andersencc8ed391999-10-05 16:24:54 +0000109 push(pop() / divisor);
110}
111
Eric Andersena9287742003-10-22 11:24:39 +0000112static void mod(void)
113{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200114 data_t d = pop();
Eric Andersena9287742003-10-22 11:24:39 +0000115
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200116 push((data_t) pop() % d);
Eric Andersena9287742003-10-22 11:24:39 +0000117}
118
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000119static void and(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000120{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200121 push((data_t) pop() & (data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000122}
123
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000124static void or(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000125{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200126 push((data_t) pop() | (data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000127}
128
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000129static void eor(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000130{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200131 push((data_t) pop() ^ (data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000132}
133
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000134static void not(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000135{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200136 push(~(data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000137}
138
Glenn L McGrath6d074322002-12-12 10:31:53 +0000139static void set_output_base(void)
140{
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000141 static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 };
142 unsigned b = (unsigned)pop();
143
144 base = *strchrnul(bases, b);
145 if (base == 0) {
146 bb_error_msg("error, base %u is not supported", b);
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000147 base = 10;
Glenn L McGrath6d074322002-12-12 10:31:53 +0000148 }
149}
150
151static void print_base(double print)
152{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200153 data_t x, i;
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000154
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200155 x = (data_t) print;
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000156 if (base == 10) {
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200157 if (x == print) /* exactly representable as unsigned integer */
158 printf("%"DATA_FMT"u\n", x);
159 else
160 printf("%g\n", print);
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000161 return;
162 }
163
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000164 switch (base) {
165 case 16:
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200166 printf("%"DATA_FMT"x\n", x);
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000167 break;
168 case 8:
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200169 printf("%"DATA_FMT"o\n", x);
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000170 break;
171 default: /* base 2 */
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200172 i = MAXINT(data_t) - (MAXINT(data_t) >> 1);
173 /* i is 100000...00000 */
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000174 do {
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200175 if (x & i)
176 break;
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000177 i >>= 1;
178 } while (i > 1);
179 do {
180 bb_putchar('1' - !(x & i));
181 i >>= 1;
182 } while (i);
183 bb_putchar('\n');
184 }
Glenn L McGrath6d074322002-12-12 10:31:53 +0000185}
186
187static void print_stack_no_pop(void)
188{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000189 unsigned i = pointer;
Glenn L McGrath6d074322002-12-12 10:31:53 +0000190 while (i)
191 print_base(stack[--i]);
192}
193
194static void print_no_pop(void)
195{
Denys Vlasenkoc4603fb2015-05-25 13:31:25 +0200196 check_under();
Glenn L McGrath6d074322002-12-12 10:31:53 +0000197 print_base(stack[pointer-1]);
198}
199
Eric Andersencc8ed391999-10-05 16:24:54 +0000200struct op {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000201 const char name[4];
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000202 void (*function) (void);
Eric Andersencc8ed391999-10-05 16:24:54 +0000203};
204
Erik Andersene49d5ec2000-02-08 19:58:47 +0000205static const struct op operators[] = {
Denis Vlasenko07832302008-10-20 08:43:10 +0000206#if ENABLE_FEATURE_DC_LIBM
Eric Andersena9287742003-10-22 11:24:39 +0000207 {"**", power},
208 {"exp", power},
209 {"pow", power},
Denis Vlasenko07832302008-10-20 08:43:10 +0000210#endif
Eric Andersena9287742003-10-22 11:24:39 +0000211 {"%", mod},
212 {"mod", mod},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000213 {"and", and},
John Beppuc0352542000-06-21 18:00:46 +0000214 {"or", or},
Erik Andersen5e1189e2000-04-15 16:34:54 +0000215 {"not", not},
216 {"eor", eor},
Eric Andersena9287742003-10-22 11:24:39 +0000217 {"xor", eor},
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100218 {"+", add},
219 {"add", add},
220 {"-", sub},
221 {"sub", sub},
222 {"*", mul},
223 {"mul", mul},
224 {"/", divide},
225 {"div", divide},
Glenn L McGrath6d074322002-12-12 10:31:53 +0000226 {"p", print_no_pop},
227 {"f", print_stack_no_pop},
228 {"o", set_output_base},
Eric Andersencc8ed391999-10-05 16:24:54 +0000229};
230
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100231/* Feed the stack machine */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000232static void stack_machine(const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000233{
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100234 char *end;
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100235 double number;
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100236 const struct op *o;
Eric Andersencc8ed391999-10-05 16:24:54 +0000237
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100238 next:
239 number = strtod(argument, &end);
240 if (end != argument) {
241 argument = end;
242 push(number);
243 goto next;
Eric Andersencc8ed391999-10-05 16:24:54 +0000244 }
245
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100246 /* We might have matched a digit, eventually advance the argument */
247 argument = skip_whitespace(argument);
248
249 if (*argument == '\0')
250 return;
251
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100252 o = operators;
253 do {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100254 char *after_name = is_prefixed_with(argument, o->name);
255 if (after_name) {
256 argument = after_name;
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000257 o->function();
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100258 goto next;
Eric Andersencc8ed391999-10-05 16:24:54 +0000259 }
260 o++;
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100261 } while (o != operators + ARRAY_SIZE(operators));
262
Denys Vlasenkobd1de182009-12-30 18:37:08 +0100263 bb_error_msg_and_die("syntax error at '%s'", argument);
Eric Andersencc8ed391999-10-05 16:24:54 +0000264}
265
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000266int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000267int dc_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000268{
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000269 INIT_G();
270
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000271 argv++;
272 if (!argv[0]) {
273 /* take stuff from stdin if no args are given */
274 char *line;
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000275 while ((line = xmalloc_fgetline(stdin)) != NULL) {
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100276 stack_machine(line);
John Beppu5db60a72000-06-12 22:59:12 +0000277 free(line);
278 }
279 } else {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000280 do {
281 stack_machine(*argv);
282 } while (*++argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000283 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000284 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000285}