blob: f94d6fa6b301c17119cfe0bcabfc6c227c24e880 [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
Erik Andersene49d5ec2000-02-08 19:58:47 +000059static void push(double a)
Eric Andersencc8ed391999-10-05 16:24:54 +000060{
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +000061 if (pointer >= STACK_SIZE)
Manuel Novoa III cad53642003-03-19 09:13:01 +000062 bb_error_msg_and_die("stack overflow");
Matt Kraai3e856ce2000-12-01 02:55:13 +000063 stack[pointer++] = a;
Eric Andersencc8ed391999-10-05 16:24:54 +000064}
65
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000066static double pop(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000067{
Matt Kraai3e856ce2000-12-01 02:55:13 +000068 if (pointer == 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000069 bb_error_msg_and_die("stack underflow");
Eric Andersencc8ed391999-10-05 16:24:54 +000070 return stack[--pointer];
71}
72
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000073static void add(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000074{
75 push(pop() + pop());
76}
77
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000078static void sub(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000079{
Erik Andersene49d5ec2000-02-08 19:58:47 +000080 double subtrahend = pop();
Eric Andersencc8ed391999-10-05 16:24:54 +000081
82 push(pop() - subtrahend);
83}
84
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000085static void mul(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000086{
87 push(pop() * pop());
88}
89
Denis Vlasenko07832302008-10-20 08:43:10 +000090#if ENABLE_FEATURE_DC_LIBM
Eric Andersena9287742003-10-22 11:24:39 +000091static void power(void)
92{
93 double topower = pop();
94
95 push(pow(pop(), topower));
96}
Denis Vlasenko07832302008-10-20 08:43:10 +000097#endif
Eric Andersena9287742003-10-22 11:24:39 +000098
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000099static void divide(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000100{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000101 double divisor = pop();
102
Eric Andersencc8ed391999-10-05 16:24:54 +0000103 push(pop() / divisor);
104}
105
Eric Andersena9287742003-10-22 11:24:39 +0000106static void mod(void)
107{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200108 data_t d = pop();
Eric Andersena9287742003-10-22 11:24:39 +0000109
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200110 push((data_t) pop() % d);
Eric Andersena9287742003-10-22 11:24:39 +0000111}
112
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000113static void and(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000114{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200115 push((data_t) pop() & (data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000116}
117
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000118static void or(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 eor(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 not(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000129{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200130 push(~(data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000131}
132
Glenn L McGrath6d074322002-12-12 10:31:53 +0000133static void set_output_base(void)
134{
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000135 static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 };
136 unsigned b = (unsigned)pop();
137
138 base = *strchrnul(bases, b);
139 if (base == 0) {
140 bb_error_msg("error, base %u is not supported", b);
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000141 base = 10;
Glenn L McGrath6d074322002-12-12 10:31:53 +0000142 }
143}
144
145static void print_base(double print)
146{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200147 data_t x, i;
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000148
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200149 x = (data_t) print;
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000150 if (base == 10) {
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200151 if (x == print) /* exactly representable as unsigned integer */
152 printf("%"DATA_FMT"u\n", x);
153 else
154 printf("%g\n", print);
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000155 return;
156 }
157
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000158 switch (base) {
159 case 16:
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200160 printf("%"DATA_FMT"x\n", x);
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000161 break;
162 case 8:
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200163 printf("%"DATA_FMT"o\n", x);
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000164 break;
165 default: /* base 2 */
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200166 i = MAXINT(data_t) - (MAXINT(data_t) >> 1);
167 /* i is 100000...00000 */
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000168 do {
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200169 if (x & i)
170 break;
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000171 i >>= 1;
172 } while (i > 1);
173 do {
174 bb_putchar('1' - !(x & i));
175 i >>= 1;
176 } while (i);
177 bb_putchar('\n');
178 }
Glenn L McGrath6d074322002-12-12 10:31:53 +0000179}
180
181static void print_stack_no_pop(void)
182{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000183 unsigned i = pointer;
Glenn L McGrath6d074322002-12-12 10:31:53 +0000184 while (i)
185 print_base(stack[--i]);
186}
187
188static void print_no_pop(void)
189{
190 print_base(stack[pointer-1]);
191}
192
Eric Andersencc8ed391999-10-05 16:24:54 +0000193struct op {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000194 const char name[4];
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000195 void (*function) (void);
Eric Andersencc8ed391999-10-05 16:24:54 +0000196};
197
Erik Andersene49d5ec2000-02-08 19:58:47 +0000198static const struct op operators[] = {
Denis Vlasenko07832302008-10-20 08:43:10 +0000199#if ENABLE_FEATURE_DC_LIBM
Eric Andersena9287742003-10-22 11:24:39 +0000200 {"**", power},
201 {"exp", power},
202 {"pow", power},
Denis Vlasenko07832302008-10-20 08:43:10 +0000203#endif
Eric Andersena9287742003-10-22 11:24:39 +0000204 {"%", mod},
205 {"mod", mod},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000206 {"and", and},
John Beppuc0352542000-06-21 18:00:46 +0000207 {"or", or},
Erik Andersen5e1189e2000-04-15 16:34:54 +0000208 {"not", not},
209 {"eor", eor},
Eric Andersena9287742003-10-22 11:24:39 +0000210 {"xor", eor},
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100211 {"+", add},
212 {"add", add},
213 {"-", sub},
214 {"sub", sub},
215 {"*", mul},
216 {"mul", mul},
217 {"/", divide},
218 {"div", divide},
Glenn L McGrath6d074322002-12-12 10:31:53 +0000219 {"p", print_no_pop},
220 {"f", print_stack_no_pop},
221 {"o", set_output_base},
Eric Andersencc8ed391999-10-05 16:24:54 +0000222};
223
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100224/* Feed the stack machine */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225static void stack_machine(const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000226{
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100227 char *end;
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100228 double number;
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100229 const struct op *o;
Eric Andersencc8ed391999-10-05 16:24:54 +0000230
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100231 next:
232 number = strtod(argument, &end);
233 if (end != argument) {
234 argument = end;
235 push(number);
236 goto next;
Eric Andersencc8ed391999-10-05 16:24:54 +0000237 }
238
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100239 /* We might have matched a digit, eventually advance the argument */
240 argument = skip_whitespace(argument);
241
242 if (*argument == '\0')
243 return;
244
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100245 o = operators;
246 do {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100247 char *after_name = is_prefixed_with(argument, o->name);
248 if (after_name) {
249 argument = after_name;
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000250 o->function();
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100251 goto next;
Eric Andersencc8ed391999-10-05 16:24:54 +0000252 }
253 o++;
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100254 } while (o != operators + ARRAY_SIZE(operators));
255
Denys Vlasenkobd1de182009-12-30 18:37:08 +0100256 bb_error_msg_and_die("syntax error at '%s'", argument);
Eric Andersencc8ed391999-10-05 16:24:54 +0000257}
258
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000259int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000260int dc_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000261{
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000262 INIT_G();
263
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000264 argv++;
265 if (!argv[0]) {
266 /* take stuff from stdin if no args are given */
267 char *line;
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000268 while ((line = xmalloc_fgetline(stdin)) != NULL) {
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100269 stack_machine(line);
John Beppu5db60a72000-06-12 22:59:12 +0000270 free(line);
271 }
272 } else {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000273 do {
274 stack_machine(*argv);
275 } while (*++argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000276 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000277 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000278}