blob: ff2bc3bcef6869eacb8be7799cc97657b65e5d13 [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];
16};
17enum { 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},
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000185 { /* zero filled */ }
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
Glenn L McGrath7991ad12004-07-24 06:01:52 +0000194 if (argument == 0)
Eric Andersencc8ed391999-10-05 16:24:54 +0000195 return;
Eric Andersencc8ed391999-10-05 16:24:54 +0000196
197 d = strtod(argument, &endPointer);
198
Erik Andersene49d5ec2000-02-08 19:58:47 +0000199 if (endPointer != argument) {
Eric Andersencc8ed391999-10-05 16:24:54 +0000200 push(d);
201 return;
202 }
203
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000204 while (o->name[0]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000205 if (strcmp(o->name, argument) == 0) {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000206 o->function();
Eric Andersencc8ed391999-10-05 16:24:54 +0000207 return;
208 }
209 o++;
210 }
Denis Vlasenko13858992006-10-08 12:49:22 +0000211 bb_error_msg_and_die("%s: syntax error", argument);
Eric Andersencc8ed391999-10-05 16:24:54 +0000212}
213
John Beppu5db60a72000-06-12 22:59:12 +0000214/* return pointer to next token in buffer and set *buffer to one char
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000215 * past the end of the above mentioned token
John Beppu5db60a72000-06-12 22:59:12 +0000216 */
217static char *get_token(char **buffer)
218{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000219 char *current = skip_whitespace(*buffer);
220 if (*current != '\0') {
221 *buffer = skip_non_whitespace(current);
222 return current;
John Beppu5db60a72000-06-12 22:59:12 +0000223 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000224 return NULL;
John Beppu5db60a72000-06-12 22:59:12 +0000225}
226
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000227int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000228int dc_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000229{
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000230 INIT_G();
231
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000232 argv++;
233 if (!argv[0]) {
234 /* take stuff from stdin if no args are given */
235 char *line;
236 char *cursor;
237 char *token;
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000238 while ((line = xmalloc_fgetline(stdin)) != NULL) {
John Beppu5db60a72000-06-12 22:59:12 +0000239 cursor = line;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000240 while (1) {
John Beppu5db60a72000-06-12 22:59:12 +0000241 token = get_token(&cursor);
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000242 if (!token) break;
243 *cursor++ = '\0';
John Beppu5db60a72000-06-12 22:59:12 +0000244 stack_machine(token);
245 }
246 free(line);
247 }
248 } else {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000249 if (argv[0][0] == '-')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000250 bb_show_usage();
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000251 do {
252 stack_machine(*argv);
253 } while (*++argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000254 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000255 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000256}