blob: 0ac658d9c31d76312de367cf15e641fdabdb4276 [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
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000117static void print(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000118{
Glenn L McGrath6d074322002-12-12 10:31:53 +0000119 print_base(pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000120}
121
122struct op {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000123 const char *name;
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000124 void (*function) (void);
Eric Andersencc8ed391999-10-05 16:24:54 +0000125};
126
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127static const struct op operators[] = {
John Beppuc0352542000-06-21 18:00:46 +0000128 {"+", add},
129 {"add", add},
130 {"-", sub},
131 {"sub", sub},
132 {"*", mul},
133 {"mul", mul},
134 {"/", divide},
135 {"div", divide},
Eric Andersena9287742003-10-22 11:24:39 +0000136 {"**", power},
137 {"exp", power},
138 {"pow", power},
139 {"%", mod},
140 {"mod", mod},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000141 {"and", and},
John Beppuc0352542000-06-21 18:00:46 +0000142 {"or", or},
Erik Andersen5e1189e2000-04-15 16:34:54 +0000143 {"not", not},
144 {"eor", eor},
Eric Andersena9287742003-10-22 11:24:39 +0000145 {"xor", eor},
Glenn L McGrath6d074322002-12-12 10:31:53 +0000146 {"p", print_no_pop},
147 {"f", print_stack_no_pop},
148 {"o", set_output_base},
John Beppuc0352542000-06-21 18:00:46 +0000149 {0, 0}
Eric Andersencc8ed391999-10-05 16:24:54 +0000150};
151
Erik Andersene49d5ec2000-02-08 19:58:47 +0000152static void stack_machine(const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000153{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000154 char *endPointer = 0;
155 double d;
156 const struct op *o = operators;
Eric Andersencc8ed391999-10-05 16:24:54 +0000157
Erik Andersene49d5ec2000-02-08 19:58:47 +0000158 if (argument == 0) {
Eric Andersencc8ed391999-10-05 16:24:54 +0000159 print();
160 return;
161 }
162
163 d = strtod(argument, &endPointer);
164
Erik Andersene49d5ec2000-02-08 19:58:47 +0000165 if (endPointer != argument) {
Eric Andersencc8ed391999-10-05 16:24:54 +0000166 push(d);
167 return;
168 }
169
Erik Andersene49d5ec2000-02-08 19:58:47 +0000170 while (o->name != 0) {
171 if (strcmp(o->name, argument) == 0) {
172 (*(o->function)) ();
Eric Andersencc8ed391999-10-05 16:24:54 +0000173 return;
174 }
175 o++;
176 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000177 bb_error_msg_and_die("%s: syntax error.", argument);
Eric Andersencc8ed391999-10-05 16:24:54 +0000178}
179
John Beppu5db60a72000-06-12 22:59:12 +0000180/* return pointer to next token in buffer and set *buffer to one char
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000181 * past the end of the above mentioned token
John Beppu5db60a72000-06-12 22:59:12 +0000182 */
183static char *get_token(char **buffer)
184{
185 char *start = NULL;
186 char *current = *buffer;
187
188 while (isspace(*current)) { current++; }
189 if (*current != 0) {
190 start = current;
Glenn L McGrath523c1672003-08-28 22:12:53 +0000191 while (!isspace(*current) && *current != 0) { current++; }
John Beppu5db60a72000-06-12 22:59:12 +0000192 *buffer = current;
193 }
194 return start;
195}
196
197/* In Perl one might say, scalar m|\s*(\S+)\s*|g */
198static int number_of_tokens(char *buffer)
199{
200 int i = 0;
201 char *b = buffer;
202 while (get_token(&b)) { i++; }
203 return i;
204}
205
John Beppu00216792000-06-21 19:06:16 +0000206int dc_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000207{
John Beppu5db60a72000-06-12 22:59:12 +0000208 /* take stuff from stdin if no args are given */
209 if (argc <= 1) {
210 int i, len;
211 char *line = NULL;
212 char *cursor = NULL;
213 char *token = NULL;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000214 while ((line = bb_get_chomped_line_from_file(stdin))) {
John Beppu5db60a72000-06-12 22:59:12 +0000215 cursor = line;
216 len = number_of_tokens(line);
217 for (i = 0; i < len; i++) {
218 token = get_token(&cursor);
219 *cursor++ = 0;
220 stack_machine(token);
221 }
222 free(line);
223 }
224 } else {
225 if (*argv[1]=='-')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000226 bb_show_usage();
John Beppu5db60a72000-06-12 22:59:12 +0000227 while (argc >= 2) {
228 stack_machine(argv[1]);
229 argv++;
230 argc--;
231 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000232 }
233 stack_machine(0);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000234 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000235}