blob: 193929c8e48fa3fcddb208bba3cb0ed050208eb6 [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 Vlasenkod0a071a2008-03-17 09:33:45 +000022#define INIT_G() do { \
23} while (0)
Denis Vlasenkob44c7902008-03-17 09:29:43 +000024
Eric Andersencc8ed391999-10-05 16:24:54 +000025
Erik Andersene49d5ec2000-02-08 19:58:47 +000026static void push(double a)
Eric Andersencc8ed391999-10-05 16:24:54 +000027{
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +000028 if (pointer >= STACK_SIZE)
Manuel Novoa III cad53642003-03-19 09:13:01 +000029 bb_error_msg_and_die("stack overflow");
Matt Kraai3e856ce2000-12-01 02:55:13 +000030 stack[pointer++] = a;
Eric Andersencc8ed391999-10-05 16:24:54 +000031}
32
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000033static double pop(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000034{
Matt Kraai3e856ce2000-12-01 02:55:13 +000035 if (pointer == 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000036 bb_error_msg_and_die("stack underflow");
Eric Andersencc8ed391999-10-05 16:24:54 +000037 return stack[--pointer];
38}
39
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000040static void add(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000041{
42 push(pop() + pop());
43}
44
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000045static void sub(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000046{
Erik Andersene49d5ec2000-02-08 19:58:47 +000047 double subtrahend = pop();
Eric Andersencc8ed391999-10-05 16:24:54 +000048
49 push(pop() - subtrahend);
50}
51
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000052static void mul(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000053{
54 push(pop() * pop());
55}
56
Eric Andersena9287742003-10-22 11:24:39 +000057static void power(void)
58{
59 double topower = pop();
60
61 push(pow(pop(), topower));
62}
63
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000064static void divide(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000065{
Erik Andersene49d5ec2000-02-08 19:58:47 +000066 double divisor = pop();
67
Eric Andersencc8ed391999-10-05 16:24:54 +000068 push(pop() / divisor);
69}
70
Eric Andersena9287742003-10-22 11:24:39 +000071static void mod(void)
72{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000073 unsigned d = pop();
Eric Andersena9287742003-10-22 11:24:39 +000074
Denis Vlasenkob44c7902008-03-17 09:29:43 +000075 push((unsigned) pop() % d);
Eric Andersena9287742003-10-22 11:24:39 +000076}
77
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000078static void and(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000079{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000080 push((unsigned) pop() & (unsigned) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000081}
82
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000083static void or(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000084{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000085 push((unsigned) pop() | (unsigned) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000086}
87
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000088static void eor(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000089{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000090 push((unsigned) pop() ^ (unsigned) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000091}
92
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000093static void not(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000094{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000095 push(~(unsigned) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000096}
97
Glenn L McGrath6d074322002-12-12 10:31:53 +000098static void set_output_base(void)
99{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000100 base = (unsigned)pop();
Glenn L McGrath6d074322002-12-12 10:31:53 +0000101 if ((base != 10) && (base != 16)) {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000102 bb_error_msg("error, base %d is not supported", base);
103 base = 10;
Glenn L McGrath6d074322002-12-12 10:31:53 +0000104 }
105}
106
107static void print_base(double print)
108{
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000109 if (base == 16)
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000110 printf("%x\n", (unsigned)print);
Glenn L McGrath6d074322002-12-12 10:31:53 +0000111 else
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000112 printf("%g\n", print);
Glenn L McGrath6d074322002-12-12 10:31:53 +0000113}
114
115static void print_stack_no_pop(void)
116{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000117 unsigned i = pointer;
Glenn L McGrath6d074322002-12-12 10:31:53 +0000118 while (i)
119 print_base(stack[--i]);
120}
121
122static void print_no_pop(void)
123{
124 print_base(stack[pointer-1]);
125}
126
Eric Andersencc8ed391999-10-05 16:24:54 +0000127struct op {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000128 const char name[4];
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000129 void (*function) (void);
Eric Andersencc8ed391999-10-05 16:24:54 +0000130};
131
Erik Andersene49d5ec2000-02-08 19:58:47 +0000132static const struct op operators[] = {
John Beppuc0352542000-06-21 18:00:46 +0000133 {"+", add},
134 {"add", add},
135 {"-", sub},
136 {"sub", sub},
137 {"*", mul},
138 {"mul", mul},
139 {"/", divide},
140 {"div", divide},
Eric Andersena9287742003-10-22 11:24:39 +0000141 {"**", power},
142 {"exp", power},
143 {"pow", power},
144 {"%", mod},
145 {"mod", mod},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000146 {"and", and},
John Beppuc0352542000-06-21 18:00:46 +0000147 {"or", or},
Erik Andersen5e1189e2000-04-15 16:34:54 +0000148 {"not", not},
149 {"eor", eor},
Eric Andersena9287742003-10-22 11:24:39 +0000150 {"xor", eor},
Glenn L McGrath6d074322002-12-12 10:31:53 +0000151 {"p", print_no_pop},
152 {"f", print_stack_no_pop},
153 {"o", set_output_base},
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000154 { /* zero filled */ }
Eric Andersencc8ed391999-10-05 16:24:54 +0000155};
156
Erik Andersene49d5ec2000-02-08 19:58:47 +0000157static void stack_machine(const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000158{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000159 char *endPointer;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000160 double d;
161 const struct op *o = operators;
Eric Andersencc8ed391999-10-05 16:24:54 +0000162
Glenn L McGrath7991ad12004-07-24 06:01:52 +0000163 if (argument == 0)
Eric Andersencc8ed391999-10-05 16:24:54 +0000164 return;
Eric Andersencc8ed391999-10-05 16:24:54 +0000165
166 d = strtod(argument, &endPointer);
167
Erik Andersene49d5ec2000-02-08 19:58:47 +0000168 if (endPointer != argument) {
Eric Andersencc8ed391999-10-05 16:24:54 +0000169 push(d);
170 return;
171 }
172
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000173 while (o->name[0]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000174 if (strcmp(o->name, argument) == 0) {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000175 o->function();
Eric Andersencc8ed391999-10-05 16:24:54 +0000176 return;
177 }
178 o++;
179 }
Denis Vlasenko13858992006-10-08 12:49:22 +0000180 bb_error_msg_and_die("%s: syntax error", argument);
Eric Andersencc8ed391999-10-05 16:24:54 +0000181}
182
John Beppu5db60a72000-06-12 22:59:12 +0000183/* return pointer to next token in buffer and set *buffer to one char
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000184 * past the end of the above mentioned token
John Beppu5db60a72000-06-12 22:59:12 +0000185 */
186static char *get_token(char **buffer)
187{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000188 char *current = skip_whitespace(*buffer);
189 if (*current != '\0') {
190 *buffer = skip_non_whitespace(current);
191 return current;
John Beppu5db60a72000-06-12 22:59:12 +0000192 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000193 return NULL;
John Beppu5db60a72000-06-12 22:59:12 +0000194}
195
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000196int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000197int dc_main(int argc ATTRIBUTE_UNUSED, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000198{
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000199 INIT_G();
200
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000201 argv++;
202 if (!argv[0]) {
203 /* take stuff from stdin if no args are given */
204 char *line;
205 char *cursor;
206 char *token;
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000207 while ((line = xmalloc_fgetline(stdin)) != NULL) {
John Beppu5db60a72000-06-12 22:59:12 +0000208 cursor = line;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000209 while (1) {
John Beppu5db60a72000-06-12 22:59:12 +0000210 token = get_token(&cursor);
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000211 if (!token) break;
212 *cursor++ = '\0';
John Beppu5db60a72000-06-12 22:59:12 +0000213 stack_machine(token);
214 }
215 free(line);
216 }
217 } else {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000218 if (argv[0][0] == '-')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000219 bb_show_usage();
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000220 do {
221 stack_machine(*argv);
222 } while (*++argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000223 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000224 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000225}