blob: cb4b1e9b1ab847fa42e453652d5e5deab889d2c3 [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/*
3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
4 */
5
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00006#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +00007#include <math.h>
8
9/* Tiny RPN calculator, because "expr" didn't give me bitwise operations. */
10
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +000011
Denis Vlasenkob44c7902008-03-17 09:29:43 +000012struct globals {
13 unsigned pointer;
14 unsigned base;
15 double stack[1];
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010016} FIX_ALIASING;
Denis Vlasenkob44c7902008-03-17 09:29:43 +000017enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) };
18#define G (*(struct globals*)&bb_common_bufsiz1)
19#define pointer (G.pointer )
20#define base (G.base )
21#define stack (G.stack )
Denis Vlasenkod8850f22008-12-30 10:40:05 +000022#define INIT_G() do { \
23 base = 10; \
24} while (0)
Denis Vlasenkob44c7902008-03-17 09:29:43 +000025
Eric Andersencc8ed391999-10-05 16:24:54 +000026
Erik Andersene49d5ec2000-02-08 19:58:47 +000027static void push(double a)
Eric Andersencc8ed391999-10-05 16:24:54 +000028{
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +000029 if (pointer >= STACK_SIZE)
Manuel Novoa III cad53642003-03-19 09:13:01 +000030 bb_error_msg_and_die("stack overflow");
Matt Kraai3e856ce2000-12-01 02:55:13 +000031 stack[pointer++] = a;
Eric Andersencc8ed391999-10-05 16:24:54 +000032}
33
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000034static double pop(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000035{
Matt Kraai3e856ce2000-12-01 02:55:13 +000036 if (pointer == 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000037 bb_error_msg_and_die("stack underflow");
Eric Andersencc8ed391999-10-05 16:24:54 +000038 return stack[--pointer];
39}
40
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000041static void add(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000042{
43 push(pop() + pop());
44}
45
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000046static void sub(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000047{
Erik Andersene49d5ec2000-02-08 19:58:47 +000048 double subtrahend = pop();
Eric Andersencc8ed391999-10-05 16:24:54 +000049
50 push(pop() - subtrahend);
51}
52
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000053static void mul(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000054{
55 push(pop() * pop());
56}
57
Denis Vlasenko07832302008-10-20 08:43:10 +000058#if ENABLE_FEATURE_DC_LIBM
Eric Andersena9287742003-10-22 11:24:39 +000059static void power(void)
60{
61 double topower = pop();
62
63 push(pow(pop(), topower));
64}
Denis Vlasenko07832302008-10-20 08:43:10 +000065#endif
Eric Andersena9287742003-10-22 11:24:39 +000066
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000067static void divide(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000068{
Erik Andersene49d5ec2000-02-08 19:58:47 +000069 double divisor = pop();
70
Eric Andersencc8ed391999-10-05 16:24:54 +000071 push(pop() / divisor);
72}
73
Eric Andersena9287742003-10-22 11:24:39 +000074static void mod(void)
75{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000076 unsigned d = pop();
Eric Andersena9287742003-10-22 11:24:39 +000077
Denis Vlasenkob44c7902008-03-17 09:29:43 +000078 push((unsigned) pop() % d);
Eric Andersena9287742003-10-22 11:24:39 +000079}
80
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000081static void and(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000082{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000083 push((unsigned) pop() & (unsigned) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000084}
85
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000086static void or(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000087{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000088 push((unsigned) pop() | (unsigned) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000089}
90
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000091static void eor(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000092{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000093 push((unsigned) pop() ^ (unsigned) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000094}
95
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000096static void not(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000097{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000098 push(~(unsigned) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000099}
100
Glenn L McGrath6d074322002-12-12 10:31:53 +0000101static void set_output_base(void)
102{
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000103 static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 };
104 unsigned b = (unsigned)pop();
105
106 base = *strchrnul(bases, b);
107 if (base == 0) {
108 bb_error_msg("error, base %u is not supported", b);
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000109 base = 10;
Glenn L McGrath6d074322002-12-12 10:31:53 +0000110 }
111}
112
113static void print_base(double print)
114{
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000115 unsigned x, i;
116
117 if (base == 10) {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000118 printf("%g\n", print);
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000119 return;
120 }
121
122 x = (unsigned)print;
123 switch (base) {
124 case 16:
125 printf("%x\n", x);
126 break;
127 case 8:
128 printf("%o\n", x);
129 break;
130 default: /* base 2 */
131 i = (unsigned)INT_MAX + 1;
132 do {
133 if (x & i) break;
134 i >>= 1;
135 } while (i > 1);
136 do {
137 bb_putchar('1' - !(x & i));
138 i >>= 1;
139 } while (i);
140 bb_putchar('\n');
141 }
Glenn L McGrath6d074322002-12-12 10:31:53 +0000142}
143
144static void print_stack_no_pop(void)
145{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000146 unsigned i = pointer;
Glenn L McGrath6d074322002-12-12 10:31:53 +0000147 while (i)
148 print_base(stack[--i]);
149}
150
151static void print_no_pop(void)
152{
153 print_base(stack[pointer-1]);
154}
155
Eric Andersencc8ed391999-10-05 16:24:54 +0000156struct op {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000157 const char name[4];
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000158 void (*function) (void);
Eric Andersencc8ed391999-10-05 16:24:54 +0000159};
160
Erik Andersene49d5ec2000-02-08 19:58:47 +0000161static const struct op operators[] = {
John Beppuc0352542000-06-21 18:00:46 +0000162 {"+", add},
163 {"add", add},
164 {"-", sub},
165 {"sub", sub},
166 {"*", mul},
167 {"mul", mul},
168 {"/", divide},
169 {"div", divide},
Denis Vlasenko07832302008-10-20 08:43:10 +0000170#if ENABLE_FEATURE_DC_LIBM
Eric Andersena9287742003-10-22 11:24:39 +0000171 {"**", power},
172 {"exp", power},
173 {"pow", power},
Denis Vlasenko07832302008-10-20 08:43:10 +0000174#endif
Eric Andersena9287742003-10-22 11:24:39 +0000175 {"%", mod},
176 {"mod", mod},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000177 {"and", and},
John Beppuc0352542000-06-21 18:00:46 +0000178 {"or", or},
Erik Andersen5e1189e2000-04-15 16:34:54 +0000179 {"not", not},
180 {"eor", eor},
Eric Andersena9287742003-10-22 11:24:39 +0000181 {"xor", eor},
Glenn L McGrath6d074322002-12-12 10:31:53 +0000182 {"p", print_no_pop},
183 {"f", print_stack_no_pop},
184 {"o", set_output_base},
Denys Vlasenko043b1e52009-09-06 12:47:55 +0200185 { "", NULL }
Eric Andersencc8ed391999-10-05 16:24:54 +0000186};
187
Erik Andersene49d5ec2000-02-08 19:58:47 +0000188static void stack_machine(const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000189{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000190 char *endPointer;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000191 double d;
192 const struct op *o = operators;
Eric Andersencc8ed391999-10-05 16:24:54 +0000193
Eric Andersencc8ed391999-10-05 16:24:54 +0000194 d = strtod(argument, &endPointer);
195
Denys Vlasenkobd1de182009-12-30 18:37:08 +0100196 if (endPointer != argument && *endPointer == '\0') {
Eric Andersencc8ed391999-10-05 16:24:54 +0000197 push(d);
198 return;
199 }
200
Denys Vlasenkobd1de182009-12-30 18:37:08 +0100201 while (o->function) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000202 if (strcmp(o->name, argument) == 0) {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000203 o->function();
Eric Andersencc8ed391999-10-05 16:24:54 +0000204 return;
205 }
206 o++;
207 }
Denys Vlasenkobd1de182009-12-30 18:37:08 +0100208 bb_error_msg_and_die("syntax error at '%s'", argument);
Eric Andersencc8ed391999-10-05 16:24:54 +0000209}
210
John Beppu5db60a72000-06-12 22:59:12 +0000211/* return pointer to next token in buffer and set *buffer to one char
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000212 * past the end of the above mentioned token
John Beppu5db60a72000-06-12 22:59:12 +0000213 */
214static char *get_token(char **buffer)
215{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000216 char *current = skip_whitespace(*buffer);
217 if (*current != '\0') {
218 *buffer = skip_non_whitespace(current);
219 return current;
John Beppu5db60a72000-06-12 22:59:12 +0000220 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000221 return NULL;
John Beppu5db60a72000-06-12 22:59:12 +0000222}
223
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000224int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000225int dc_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000226{
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000227 INIT_G();
228
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000229 argv++;
230 if (!argv[0]) {
231 /* take stuff from stdin if no args are given */
232 char *line;
233 char *cursor;
234 char *token;
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000235 while ((line = xmalloc_fgetline(stdin)) != NULL) {
John Beppu5db60a72000-06-12 22:59:12 +0000236 cursor = line;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000237 while (1) {
John Beppu5db60a72000-06-12 22:59:12 +0000238 token = get_token(&cursor);
Denys Vlasenkobd1de182009-12-30 18:37:08 +0100239 if (!token)
240 break;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000241 *cursor++ = '\0';
John Beppu5db60a72000-06-12 22:59:12 +0000242 stack_machine(token);
243 }
244 free(line);
245 }
246 } else {
Denys Vlasenkobd1de182009-12-30 18:37:08 +0100247 // why? it breaks "dc -2 2 * p"
248 //if (argv[0][0] == '-')
249 // bb_show_usage();
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000250 do {
251 stack_machine(*argv);
252 } while (*++argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000253 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000254 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000255}