blob: 112f6df3ff866ccc3d1834b1810c0cb957f2dd00 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
John Beppu5db60a72000-06-12 22:59:12 +00002#include <ctype.h>
Eric Andersencc8ed391999-10-05 16:24:54 +00003#include <stdio.h>
4#include <stdlib.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +00005#include <string.h>
Eric Andersencc8ed391999-10-05 16:24:54 +00006#include <unistd.h>
7#include <math.h>
Eric Andersencbe31da2001-02-20 06:14:08 +00008#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +00009
10/* Tiny RPN calculator, because "expr" didn't give me bitwise operations. */
11
Erik Andersene49d5ec2000-02-08 19:58:47 +000012static double stack[100];
13static unsigned int pointer;
Glenn L McGrath6d074322002-12-12 10:31:53 +000014static unsigned char base;
Eric Andersencc8ed391999-10-05 16:24:54 +000015
Erik Andersene49d5ec2000-02-08 19:58:47 +000016static void push(double a)
Eric Andersencc8ed391999-10-05 16:24:54 +000017{
Matt Kraai3e856ce2000-12-01 02:55:13 +000018 if (pointer >= (sizeof(stack) / sizeof(*stack)))
Manuel Novoa III cad53642003-03-19 09:13:01 +000019 bb_error_msg_and_die("stack overflow");
Matt Kraai3e856ce2000-12-01 02:55:13 +000020 stack[pointer++] = a;
Eric Andersencc8ed391999-10-05 16:24:54 +000021}
22
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000023static double pop(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000024{
Matt Kraai3e856ce2000-12-01 02:55:13 +000025 if (pointer == 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000026 bb_error_msg_and_die("stack underflow");
Eric Andersencc8ed391999-10-05 16:24:54 +000027 return stack[--pointer];
28}
29
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000030static void add(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000031{
32 push(pop() + pop());
33}
34
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000035static void sub(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000036{
Erik Andersene49d5ec2000-02-08 19:58:47 +000037 double subtrahend = pop();
Eric Andersencc8ed391999-10-05 16:24:54 +000038
39 push(pop() - subtrahend);
40}
41
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000042static void mul(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000043{
44 push(pop() * pop());
45}
46
Eric Andersena9287742003-10-22 11:24:39 +000047static void power(void)
48{
49 double topower = pop();
50
51 push(pow(pop(), topower));
52}
53
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000054static void divide(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000055{
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 double divisor = pop();
57
Eric Andersencc8ed391999-10-05 16:24:54 +000058 push(pop() / divisor);
59}
60
Eric Andersena9287742003-10-22 11:24:39 +000061static void mod(void)
62{
63 unsigned int d = pop();
64
65 push((unsigned int) pop() % d);
66}
67
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000068static void and(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000069{
Erik Andersene49d5ec2000-02-08 19:58:47 +000070 push((unsigned int) pop() & (unsigned int) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000071}
72
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000073static void or(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000074{
Erik Andersene49d5ec2000-02-08 19:58:47 +000075 push((unsigned int) pop() | (unsigned int) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000076}
77
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000078static void eor(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000079{
Erik Andersene49d5ec2000-02-08 19:58:47 +000080 push((unsigned int) pop() ^ (unsigned int) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000081}
82
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000083static void not(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000084{
Erik Andersene49d5ec2000-02-08 19:58:47 +000085 push(~(unsigned int) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000086}
87
Glenn L McGrath6d074322002-12-12 10:31:53 +000088static void set_output_base(void)
89{
Eric Andersenc7bda1c2004-03-15 08:29:22 +000090 base=(unsigned char)pop();
Glenn L McGrath6d074322002-12-12 10:31:53 +000091 if ((base != 10) && (base != 16)) {
92 fprintf(stderr, "Error: base = %d is not supported.\n", base);
93 base=10;
94 }
95}
96
97static void print_base(double print)
98{
Eric Andersenc7bda1c2004-03-15 08:29:22 +000099 if (base == 16)
Glenn L McGrath6d074322002-12-12 10:31:53 +0000100 printf("%x\n", (unsigned int)print);
101 else
102 printf("%g\n", print);
103}
104
105static void print_stack_no_pop(void)
106{
107 unsigned int i=pointer;
108 while (i)
109 print_base(stack[--i]);
110}
111
112static void print_no_pop(void)
113{
114 print_base(stack[pointer-1]);
115}
116
Eric Andersencc8ed391999-10-05 16:24:54 +0000117struct op {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000118 const char *name;
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000119 void (*function) (void);
Eric Andersencc8ed391999-10-05 16:24:54 +0000120};
121
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122static const struct op operators[] = {
John Beppuc0352542000-06-21 18:00:46 +0000123 {"+", add},
124 {"add", add},
125 {"-", sub},
126 {"sub", sub},
127 {"*", mul},
128 {"mul", mul},
129 {"/", divide},
130 {"div", divide},
Eric Andersena9287742003-10-22 11:24:39 +0000131 {"**", power},
132 {"exp", power},
133 {"pow", power},
134 {"%", mod},
135 {"mod", mod},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000136 {"and", and},
John Beppuc0352542000-06-21 18:00:46 +0000137 {"or", or},
Erik Andersen5e1189e2000-04-15 16:34:54 +0000138 {"not", not},
139 {"eor", eor},
Eric Andersena9287742003-10-22 11:24:39 +0000140 {"xor", eor},
Glenn L McGrath6d074322002-12-12 10:31:53 +0000141 {"p", print_no_pop},
142 {"f", print_stack_no_pop},
143 {"o", set_output_base},
John Beppuc0352542000-06-21 18:00:46 +0000144 {0, 0}
Eric Andersencc8ed391999-10-05 16:24:54 +0000145};
146
Erik Andersene49d5ec2000-02-08 19:58:47 +0000147static void stack_machine(const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000148{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000149 char *endPointer = 0;
150 double d;
151 const struct op *o = operators;
Eric Andersencc8ed391999-10-05 16:24:54 +0000152
Glenn L McGrath7991ad12004-07-24 06:01:52 +0000153 if (argument == 0)
Eric Andersencc8ed391999-10-05 16:24:54 +0000154 return;
Eric Andersencc8ed391999-10-05 16:24:54 +0000155
156 d = strtod(argument, &endPointer);
157
Erik Andersene49d5ec2000-02-08 19:58:47 +0000158 if (endPointer != argument) {
Eric Andersencc8ed391999-10-05 16:24:54 +0000159 push(d);
160 return;
161 }
162
Erik Andersene49d5ec2000-02-08 19:58:47 +0000163 while (o->name != 0) {
164 if (strcmp(o->name, argument) == 0) {
165 (*(o->function)) ();
Eric Andersencc8ed391999-10-05 16:24:54 +0000166 return;
167 }
168 o++;
169 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000170 bb_error_msg_and_die("%s: syntax error.", argument);
Eric Andersencc8ed391999-10-05 16:24:54 +0000171}
172
John Beppu5db60a72000-06-12 22:59:12 +0000173/* return pointer to next token in buffer and set *buffer to one char
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000174 * past the end of the above mentioned token
John Beppu5db60a72000-06-12 22:59:12 +0000175 */
176static char *get_token(char **buffer)
177{
178 char *start = NULL;
179 char *current = *buffer;
180
181 while (isspace(*current)) { current++; }
182 if (*current != 0) {
183 start = current;
Glenn L McGrath523c1672003-08-28 22:12:53 +0000184 while (!isspace(*current) && *current != 0) { current++; }
John Beppu5db60a72000-06-12 22:59:12 +0000185 *buffer = current;
186 }
187 return start;
188}
189
190/* In Perl one might say, scalar m|\s*(\S+)\s*|g */
191static int number_of_tokens(char *buffer)
192{
193 int i = 0;
194 char *b = buffer;
195 while (get_token(&b)) { i++; }
196 return i;
197}
198
John Beppu00216792000-06-21 19:06:16 +0000199int dc_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000200{
John Beppu5db60a72000-06-12 22:59:12 +0000201 /* take stuff from stdin if no args are given */
202 if (argc <= 1) {
203 int i, len;
204 char *line = NULL;
205 char *cursor = NULL;
206 char *token = NULL;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000207 while ((line = bb_get_chomped_line_from_file(stdin))) {
John Beppu5db60a72000-06-12 22:59:12 +0000208 cursor = line;
209 len = number_of_tokens(line);
210 for (i = 0; i < len; i++) {
211 token = get_token(&cursor);
212 *cursor++ = 0;
213 stack_machine(token);
214 }
215 free(line);
216 }
217 } else {
218 if (*argv[1]=='-')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000219 bb_show_usage();
John Beppu5db60a72000-06-12 22:59:12 +0000220 while (argc >= 2) {
221 stack_machine(argv[1]);
222 argv++;
223 argc--;
224 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000225 }
226 stack_machine(0);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000227 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000228}