blob: 8e7a2494cf51ba1d72003d829bb77434a9b628f3 [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
Bernhard Reutner-Fischerc89982d2006-06-03 19:49:21 +00006#include "busybox.h"
John Beppu5db60a72000-06-12 22:59:12 +00007#include <ctype.h>
Eric Andersencc8ed391999-10-05 16:24:54 +00008#include <stdio.h>
9#include <stdlib.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000010#include <string.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000011#include <unistd.h>
12#include <math.h>
13
14/* Tiny RPN calculator, because "expr" didn't give me bitwise operations. */
15
Erik Andersene49d5ec2000-02-08 19:58:47 +000016static double stack[100];
17static unsigned int pointer;
Glenn L McGrath6d074322002-12-12 10:31:53 +000018static unsigned char base;
Eric Andersencc8ed391999-10-05 16:24:54 +000019
Erik Andersene49d5ec2000-02-08 19:58:47 +000020static void push(double a)
Eric Andersencc8ed391999-10-05 16:24:54 +000021{
Matt Kraai3e856ce2000-12-01 02:55:13 +000022 if (pointer >= (sizeof(stack) / sizeof(*stack)))
Manuel Novoa III cad53642003-03-19 09:13:01 +000023 bb_error_msg_and_die("stack overflow");
Matt Kraai3e856ce2000-12-01 02:55:13 +000024 stack[pointer++] = a;
Eric Andersencc8ed391999-10-05 16:24:54 +000025}
26
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000027static double pop(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000028{
Matt Kraai3e856ce2000-12-01 02:55:13 +000029 if (pointer == 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000030 bb_error_msg_and_die("stack underflow");
Eric Andersencc8ed391999-10-05 16:24:54 +000031 return stack[--pointer];
32}
33
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000034static void add(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000035{
36 push(pop() + pop());
37}
38
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000039static void sub(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000040{
Erik Andersene49d5ec2000-02-08 19:58:47 +000041 double subtrahend = pop();
Eric Andersencc8ed391999-10-05 16:24:54 +000042
43 push(pop() - subtrahend);
44}
45
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000046static void mul(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000047{
48 push(pop() * pop());
49}
50
Eric Andersena9287742003-10-22 11:24:39 +000051static void power(void)
52{
53 double topower = pop();
54
55 push(pow(pop(), topower));
56}
57
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000058static void divide(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000059{
Erik Andersene49d5ec2000-02-08 19:58:47 +000060 double divisor = pop();
61
Eric Andersencc8ed391999-10-05 16:24:54 +000062 push(pop() / divisor);
63}
64
Eric Andersena9287742003-10-22 11:24:39 +000065static void mod(void)
66{
67 unsigned int d = pop();
68
69 push((unsigned int) pop() % d);
70}
71
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000072static void and(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000073{
Erik Andersene49d5ec2000-02-08 19:58:47 +000074 push((unsigned int) pop() & (unsigned int) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000075}
76
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000077static void or(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000078{
Erik Andersene49d5ec2000-02-08 19:58:47 +000079 push((unsigned int) pop() | (unsigned int) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000080}
81
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000082static void eor(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000083{
Erik Andersene49d5ec2000-02-08 19:58:47 +000084 push((unsigned int) pop() ^ (unsigned int) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000085}
86
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000087static void not(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000088{
Erik Andersene49d5ec2000-02-08 19:58:47 +000089 push(~(unsigned int) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000090}
91
Glenn L McGrath6d074322002-12-12 10:31:53 +000092static void set_output_base(void)
93{
Eric Andersenc7bda1c2004-03-15 08:29:22 +000094 base=(unsigned char)pop();
Glenn L McGrath6d074322002-12-12 10:31:53 +000095 if ((base != 10) && (base != 16)) {
96 fprintf(stderr, "Error: base = %d is not supported.\n", base);
97 base=10;
98 }
99}
100
101static void print_base(double print)
102{
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000103 if (base == 16)
Glenn L McGrath6d074322002-12-12 10:31:53 +0000104 printf("%x\n", (unsigned int)print);
105 else
106 printf("%g\n", print);
107}
108
109static void print_stack_no_pop(void)
110{
111 unsigned int i=pointer;
112 while (i)
113 print_base(stack[--i]);
114}
115
116static void print_no_pop(void)
117{
118 print_base(stack[pointer-1]);
119}
120
Eric Andersencc8ed391999-10-05 16:24:54 +0000121struct op {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122 const char *name;
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000123 void (*function) (void);
Eric Andersencc8ed391999-10-05 16:24:54 +0000124};
125
Erik Andersene49d5ec2000-02-08 19:58:47 +0000126static const struct op operators[] = {
John Beppuc0352542000-06-21 18:00:46 +0000127 {"+", add},
128 {"add", add},
129 {"-", sub},
130 {"sub", sub},
131 {"*", mul},
132 {"mul", mul},
133 {"/", divide},
134 {"div", divide},
Eric Andersena9287742003-10-22 11:24:39 +0000135 {"**", power},
136 {"exp", power},
137 {"pow", power},
138 {"%", mod},
139 {"mod", mod},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000140 {"and", and},
John Beppuc0352542000-06-21 18:00:46 +0000141 {"or", or},
Erik Andersen5e1189e2000-04-15 16:34:54 +0000142 {"not", not},
143 {"eor", eor},
Eric Andersena9287742003-10-22 11:24:39 +0000144 {"xor", eor},
Glenn L McGrath6d074322002-12-12 10:31:53 +0000145 {"p", print_no_pop},
146 {"f", print_stack_no_pop},
147 {"o", set_output_base},
John Beppuc0352542000-06-21 18:00:46 +0000148 {0, 0}
Eric Andersencc8ed391999-10-05 16:24:54 +0000149};
150
Erik Andersene49d5ec2000-02-08 19:58:47 +0000151static void stack_machine(const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000152{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000153 char *endPointer = 0;
154 double d;
155 const struct op *o = operators;
Eric Andersencc8ed391999-10-05 16:24:54 +0000156
Glenn L McGrath7991ad12004-07-24 06:01:52 +0000157 if (argument == 0)
Eric Andersencc8ed391999-10-05 16:24:54 +0000158 return;
Eric Andersencc8ed391999-10-05 16:24:54 +0000159
160 d = strtod(argument, &endPointer);
161
Erik Andersene49d5ec2000-02-08 19:58:47 +0000162 if (endPointer != argument) {
Eric Andersencc8ed391999-10-05 16:24:54 +0000163 push(d);
164 return;
165 }
166
Erik Andersene49d5ec2000-02-08 19:58:47 +0000167 while (o->name != 0) {
168 if (strcmp(o->name, argument) == 0) {
169 (*(o->function)) ();
Eric Andersencc8ed391999-10-05 16:24:54 +0000170 return;
171 }
172 o++;
173 }
Denis Vlasenko13858992006-10-08 12:49:22 +0000174 bb_error_msg_and_die("%s: syntax error", argument);
Eric Andersencc8ed391999-10-05 16:24:54 +0000175}
176
John Beppu5db60a72000-06-12 22:59:12 +0000177/* return pointer to next token in buffer and set *buffer to one char
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000178 * past the end of the above mentioned token
John Beppu5db60a72000-06-12 22:59:12 +0000179 */
180static char *get_token(char **buffer)
181{
182 char *start = NULL;
183 char *current = *buffer;
184
185 while (isspace(*current)) { current++; }
186 if (*current != 0) {
187 start = current;
Glenn L McGrath523c1672003-08-28 22:12:53 +0000188 while (!isspace(*current) && *current != 0) { current++; }
John Beppu5db60a72000-06-12 22:59:12 +0000189 *buffer = current;
190 }
191 return start;
192}
193
194/* In Perl one might say, scalar m|\s*(\S+)\s*|g */
195static int number_of_tokens(char *buffer)
196{
197 int i = 0;
198 char *b = buffer;
199 while (get_token(&b)) { i++; }
200 return i;
201}
202
John Beppu00216792000-06-21 19:06:16 +0000203int dc_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000204{
John Beppu5db60a72000-06-12 22:59:12 +0000205 /* take stuff from stdin if no args are given */
206 if (argc <= 1) {
207 int i, len;
208 char *line = NULL;
209 char *cursor = NULL;
210 char *token = NULL;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000211 while ((line = bb_get_chomped_line_from_file(stdin))) {
John Beppu5db60a72000-06-12 22:59:12 +0000212 cursor = line;
213 len = number_of_tokens(line);
214 for (i = 0; i < len; i++) {
215 token = get_token(&cursor);
216 *cursor++ = 0;
217 stack_machine(token);
218 }
219 free(line);
220 }
221 } else {
222 if (*argv[1]=='-')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000223 bb_show_usage();
John Beppu5db60a72000-06-12 22:59:12 +0000224 while (argc >= 2) {
225 stack_machine(argv[1]);
226 argv++;
227 argc--;
228 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000229 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000230 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000231}