blob: 777ec1654c332dc8c08cdc12b660ec75ddc89123 [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"
14//usage: "+, add, -, sub, *, mul, /, div, %, mod, **, exp, and, or, not, eor,\n"
15//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"
18//usage: "Examples: 'dc 2 2 add' -> 4, 'dc 8 8 * 2 2 + /' -> 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[] = {
John Beppuc0352542000-06-21 18:00:46 +0000199 {"+", add},
200 {"add", add},
201 {"-", sub},
202 {"sub", sub},
203 {"*", mul},
204 {"mul", mul},
205 {"/", divide},
206 {"div", divide},
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},
Glenn L McGrath6d074322002-12-12 10:31:53 +0000219 {"p", print_no_pop},
220 {"f", print_stack_no_pop},
221 {"o", set_output_base},
Denys Vlasenko043b1e52009-09-06 12:47:55 +0200222 { "", NULL }
Eric Andersencc8ed391999-10-05 16:24:54 +0000223};
224
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225static void stack_machine(const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000226{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000227 char *endPointer;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000228 double d;
229 const struct op *o = operators;
Eric Andersencc8ed391999-10-05 16:24:54 +0000230
Eric Andersencc8ed391999-10-05 16:24:54 +0000231 d = strtod(argument, &endPointer);
232
Denys Vlasenkobd1de182009-12-30 18:37:08 +0100233 if (endPointer != argument && *endPointer == '\0') {
Eric Andersencc8ed391999-10-05 16:24:54 +0000234 push(d);
235 return;
236 }
237
Denys Vlasenkobd1de182009-12-30 18:37:08 +0100238 while (o->function) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000239 if (strcmp(o->name, argument) == 0) {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000240 o->function();
Eric Andersencc8ed391999-10-05 16:24:54 +0000241 return;
242 }
243 o++;
244 }
Denys Vlasenkobd1de182009-12-30 18:37:08 +0100245 bb_error_msg_and_die("syntax error at '%s'", argument);
Eric Andersencc8ed391999-10-05 16:24:54 +0000246}
247
John Beppu5db60a72000-06-12 22:59:12 +0000248/* return pointer to next token in buffer and set *buffer to one char
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000249 * past the end of the above mentioned token
John Beppu5db60a72000-06-12 22:59:12 +0000250 */
251static char *get_token(char **buffer)
252{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000253 char *current = skip_whitespace(*buffer);
254 if (*current != '\0') {
255 *buffer = skip_non_whitespace(current);
256 return current;
John Beppu5db60a72000-06-12 22:59:12 +0000257 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000258 return NULL;
John Beppu5db60a72000-06-12 22:59:12 +0000259}
260
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000261int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000262int dc_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000263{
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000264 INIT_G();
265
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000266 argv++;
267 if (!argv[0]) {
268 /* take stuff from stdin if no args are given */
269 char *line;
270 char *cursor;
271 char *token;
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000272 while ((line = xmalloc_fgetline(stdin)) != NULL) {
John Beppu5db60a72000-06-12 22:59:12 +0000273 cursor = line;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000274 while (1) {
John Beppu5db60a72000-06-12 22:59:12 +0000275 token = get_token(&cursor);
Denys Vlasenkobd1de182009-12-30 18:37:08 +0100276 if (!token)
277 break;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000278 *cursor++ = '\0';
John Beppu5db60a72000-06-12 22:59:12 +0000279 stack_machine(token);
280 }
281 free(line);
282 }
283 } else {
Denys Vlasenkobd1de182009-12-30 18:37:08 +0100284 // why? it breaks "dc -2 2 * p"
285 //if (argv[0][0] == '-')
286 // bb_show_usage();
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000287 do {
288 stack_machine(*argv);
289 } while (*++argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000290 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000291 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000292}