blob: 4d92bc3d04244359849d663bbd31611f041ee927 [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 { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020056 setup_common_bufsiz(); \
Denis Vlasenkod8850f22008-12-30 10:40:05 +000057 base = 10; \
58} while (0)
Denis Vlasenkob44c7902008-03-17 09:29:43 +000059
Eric Andersencc8ed391999-10-05 16:24:54 +000060
Denys Vlasenkoc4603fb2015-05-25 13:31:25 +020061static void check_under(void)
62{
63 if (pointer == 0)
64 bb_error_msg_and_die("stack underflow");
65}
66
Erik Andersene49d5ec2000-02-08 19:58:47 +000067static void push(double a)
Eric Andersencc8ed391999-10-05 16:24:54 +000068{
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +000069 if (pointer >= STACK_SIZE)
Manuel Novoa III cad53642003-03-19 09:13:01 +000070 bb_error_msg_and_die("stack overflow");
Matt Kraai3e856ce2000-12-01 02:55:13 +000071 stack[pointer++] = a;
Eric Andersencc8ed391999-10-05 16:24:54 +000072}
73
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000074static double pop(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000075{
Denys Vlasenkoc4603fb2015-05-25 13:31:25 +020076 check_under();
Eric Andersencc8ed391999-10-05 16:24:54 +000077 return stack[--pointer];
78}
79
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000080static void add(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000081{
82 push(pop() + pop());
83}
84
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000085static void sub(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000086{
Erik Andersene49d5ec2000-02-08 19:58:47 +000087 double subtrahend = pop();
Eric Andersencc8ed391999-10-05 16:24:54 +000088
89 push(pop() - subtrahend);
90}
91
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000092static void mul(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000093{
94 push(pop() * pop());
95}
96
Denis Vlasenko07832302008-10-20 08:43:10 +000097#if ENABLE_FEATURE_DC_LIBM
Eric Andersena9287742003-10-22 11:24:39 +000098static void power(void)
99{
100 double topower = pop();
101
102 push(pow(pop(), topower));
103}
Denis Vlasenko07832302008-10-20 08:43:10 +0000104#endif
Eric Andersena9287742003-10-22 11:24:39 +0000105
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000106static void divide(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000107{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000108 double divisor = pop();
109
Eric Andersencc8ed391999-10-05 16:24:54 +0000110 push(pop() / divisor);
111}
112
Eric Andersena9287742003-10-22 11:24:39 +0000113static void mod(void)
114{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200115 data_t d = pop();
Eric Andersena9287742003-10-22 11:24:39 +0000116
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200117 push((data_t) pop() % d);
Eric Andersena9287742003-10-22 11:24:39 +0000118}
119
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000120static void and(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000121{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200122 push((data_t) pop() & (data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000123}
124
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000125static void or(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000126{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200127 push((data_t) pop() | (data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000128}
129
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000130static void eor(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000131{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200132 push((data_t) pop() ^ (data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000133}
134
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000135static void not(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000136{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200137 push(~(data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000138}
139
Glenn L McGrath6d074322002-12-12 10:31:53 +0000140static void set_output_base(void)
141{
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000142 static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 };
143 unsigned b = (unsigned)pop();
144
145 base = *strchrnul(bases, b);
146 if (base == 0) {
147 bb_error_msg("error, base %u is not supported", b);
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000148 base = 10;
Glenn L McGrath6d074322002-12-12 10:31:53 +0000149 }
150}
151
152static void print_base(double print)
153{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200154 data_t x, i;
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000155
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200156 x = (data_t) print;
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000157 if (base == 10) {
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200158 if (x == print) /* exactly representable as unsigned integer */
159 printf("%"DATA_FMT"u\n", x);
160 else
161 printf("%g\n", print);
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000162 return;
163 }
164
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000165 switch (base) {
166 case 16:
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200167 printf("%"DATA_FMT"x\n", x);
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000168 break;
169 case 8:
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200170 printf("%"DATA_FMT"o\n", x);
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000171 break;
172 default: /* base 2 */
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200173 i = MAXINT(data_t) - (MAXINT(data_t) >> 1);
174 /* i is 100000...00000 */
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000175 do {
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200176 if (x & i)
177 break;
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000178 i >>= 1;
179 } while (i > 1);
180 do {
181 bb_putchar('1' - !(x & i));
182 i >>= 1;
183 } while (i);
184 bb_putchar('\n');
185 }
Glenn L McGrath6d074322002-12-12 10:31:53 +0000186}
187
188static void print_stack_no_pop(void)
189{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000190 unsigned i = pointer;
Glenn L McGrath6d074322002-12-12 10:31:53 +0000191 while (i)
192 print_base(stack[--i]);
193}
194
195static void print_no_pop(void)
196{
Denys Vlasenkoc4603fb2015-05-25 13:31:25 +0200197 check_under();
Glenn L McGrath6d074322002-12-12 10:31:53 +0000198 print_base(stack[pointer-1]);
199}
200
Eric Andersencc8ed391999-10-05 16:24:54 +0000201struct op {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000202 const char name[4];
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000203 void (*function) (void);
Eric Andersencc8ed391999-10-05 16:24:54 +0000204};
205
Erik Andersene49d5ec2000-02-08 19:58:47 +0000206static const struct op operators[] = {
Denis Vlasenko07832302008-10-20 08:43:10 +0000207#if ENABLE_FEATURE_DC_LIBM
Eric Andersena9287742003-10-22 11:24:39 +0000208 {"**", power},
209 {"exp", power},
210 {"pow", power},
Denis Vlasenko07832302008-10-20 08:43:10 +0000211#endif
Eric Andersena9287742003-10-22 11:24:39 +0000212 {"%", mod},
213 {"mod", mod},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000214 {"and", and},
John Beppuc0352542000-06-21 18:00:46 +0000215 {"or", or},
Erik Andersen5e1189e2000-04-15 16:34:54 +0000216 {"not", not},
217 {"eor", eor},
Eric Andersena9287742003-10-22 11:24:39 +0000218 {"xor", eor},
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100219 {"+", add},
220 {"add", add},
221 {"-", sub},
222 {"sub", sub},
223 {"*", mul},
224 {"mul", mul},
225 {"/", divide},
226 {"div", divide},
Glenn L McGrath6d074322002-12-12 10:31:53 +0000227 {"p", print_no_pop},
228 {"f", print_stack_no_pop},
229 {"o", set_output_base},
Eric Andersencc8ed391999-10-05 16:24:54 +0000230};
231
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100232/* Feed the stack machine */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000233static void stack_machine(const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000234{
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100235 char *end;
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100236 double number;
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100237 const struct op *o;
Eric Andersencc8ed391999-10-05 16:24:54 +0000238
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100239 next:
240 number = strtod(argument, &end);
241 if (end != argument) {
242 argument = end;
243 push(number);
244 goto next;
Eric Andersencc8ed391999-10-05 16:24:54 +0000245 }
246
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100247 /* We might have matched a digit, eventually advance the argument */
248 argument = skip_whitespace(argument);
249
250 if (*argument == '\0')
251 return;
252
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100253 o = operators;
254 do {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100255 char *after_name = is_prefixed_with(argument, o->name);
256 if (after_name) {
257 argument = after_name;
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000258 o->function();
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100259 goto next;
Eric Andersencc8ed391999-10-05 16:24:54 +0000260 }
261 o++;
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100262 } while (o != operators + ARRAY_SIZE(operators));
263
Denys Vlasenkobd1de182009-12-30 18:37:08 +0100264 bb_error_msg_and_die("syntax error at '%s'", argument);
Eric Andersencc8ed391999-10-05 16:24:54 +0000265}
266
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000267int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000268int dc_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000269{
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000270 INIT_G();
271
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000272 argv++;
273 if (!argv[0]) {
274 /* take stuff from stdin if no args are given */
275 char *line;
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000276 while ((line = xmalloc_fgetline(stdin)) != NULL) {
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100277 stack_machine(line);
John Beppu5db60a72000-06-12 22:59:12 +0000278 free(line);
279 }
280 } else {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000281 do {
282 stack_machine(*argv);
283 } while (*++argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000284 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000285 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000286}