blob: ffc3f8df4ea7f327eb448fd257e5615d2982cd04 [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 Vlasenko74324c82007-06-04 10:16:52 +000011enum { STACK_SIZE = COMMON_BUFSIZE / sizeof(double) };
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +000012
13#define stack ((double*)&bb_common_bufsiz1)
Erik Andersene49d5ec2000-02-08 19:58:47 +000014static unsigned int pointer;
Glenn L McGrath6d074322002-12-12 10:31:53 +000015static unsigned char base;
Eric Andersencc8ed391999-10-05 16:24:54 +000016
Erik Andersene49d5ec2000-02-08 19:58:47 +000017static void push(double a)
Eric Andersencc8ed391999-10-05 16:24:54 +000018{
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +000019 if (pointer >= STACK_SIZE)
Manuel Novoa III cad53642003-03-19 09:13:01 +000020 bb_error_msg_and_die("stack overflow");
Matt Kraai3e856ce2000-12-01 02:55:13 +000021 stack[pointer++] = a;
Eric Andersencc8ed391999-10-05 16:24:54 +000022}
23
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000024static double pop(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000025{
Matt Kraai3e856ce2000-12-01 02:55:13 +000026 if (pointer == 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000027 bb_error_msg_and_die("stack underflow");
Eric Andersencc8ed391999-10-05 16:24:54 +000028 return stack[--pointer];
29}
30
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000031static void add(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000032{
33 push(pop() + pop());
34}
35
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000036static void sub(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000037{
Erik Andersene49d5ec2000-02-08 19:58:47 +000038 double subtrahend = pop();
Eric Andersencc8ed391999-10-05 16:24:54 +000039
40 push(pop() - subtrahend);
41}
42
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000043static void mul(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000044{
45 push(pop() * pop());
46}
47
Eric Andersena9287742003-10-22 11:24:39 +000048static void power(void)
49{
50 double topower = pop();
51
52 push(pow(pop(), topower));
53}
54
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000055static void divide(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000056{
Erik Andersene49d5ec2000-02-08 19:58:47 +000057 double divisor = pop();
58
Eric Andersencc8ed391999-10-05 16:24:54 +000059 push(pop() / divisor);
60}
61
Eric Andersena9287742003-10-22 11:24:39 +000062static void mod(void)
63{
64 unsigned int d = pop();
65
66 push((unsigned int) pop() % d);
67}
68
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000069static void and(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000070{
Erik Andersene49d5ec2000-02-08 19:58:47 +000071 push((unsigned int) pop() & (unsigned int) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000072}
73
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000074static void or(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000075{
Erik Andersene49d5ec2000-02-08 19:58:47 +000076 push((unsigned int) pop() | (unsigned int) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000077}
78
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000079static void eor(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000080{
Erik Andersene49d5ec2000-02-08 19:58:47 +000081 push((unsigned int) pop() ^ (unsigned int) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000082}
83
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000084static void not(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000085{
Erik Andersene49d5ec2000-02-08 19:58:47 +000086 push(~(unsigned int) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000087}
88
Glenn L McGrath6d074322002-12-12 10:31:53 +000089static void set_output_base(void)
90{
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +000091 base = (unsigned char)pop();
Glenn L McGrath6d074322002-12-12 10:31:53 +000092 if ((base != 10) && (base != 16)) {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +000093 bb_error_msg("error, base %d is not supported", base);
94 base = 10;
Glenn L McGrath6d074322002-12-12 10:31:53 +000095 }
96}
97
98static void print_base(double print)
99{
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000100 if (base == 16)
Glenn L McGrath6d074322002-12-12 10:31:53 +0000101 printf("%x\n", (unsigned int)print);
102 else
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000103 printf("%g\n", print);
Glenn L McGrath6d074322002-12-12 10:31:53 +0000104}
105
106static void print_stack_no_pop(void)
107{
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000108 unsigned int i = pointer;
Glenn L McGrath6d074322002-12-12 10:31:53 +0000109 while (i)
110 print_base(stack[--i]);
111}
112
113static void print_no_pop(void)
114{
115 print_base(stack[pointer-1]);
116}
117
Eric Andersencc8ed391999-10-05 16:24:54 +0000118struct op {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000119 const char name[4];
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000120 void (*function) (void);
Eric Andersencc8ed391999-10-05 16:24:54 +0000121};
122
Erik Andersene49d5ec2000-02-08 19:58:47 +0000123static const struct op operators[] = {
John Beppuc0352542000-06-21 18:00:46 +0000124 {"+", add},
125 {"add", add},
126 {"-", sub},
127 {"sub", sub},
128 {"*", mul},
129 {"mul", mul},
130 {"/", divide},
131 {"div", divide},
Eric Andersena9287742003-10-22 11:24:39 +0000132 {"**", power},
133 {"exp", power},
134 {"pow", power},
135 {"%", mod},
136 {"mod", mod},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000137 {"and", and},
John Beppuc0352542000-06-21 18:00:46 +0000138 {"or", or},
Erik Andersen5e1189e2000-04-15 16:34:54 +0000139 {"not", not},
140 {"eor", eor},
Eric Andersena9287742003-10-22 11:24:39 +0000141 {"xor", eor},
Glenn L McGrath6d074322002-12-12 10:31:53 +0000142 {"p", print_no_pop},
143 {"f", print_stack_no_pop},
144 {"o", set_output_base},
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000145 {"", 0}
Eric Andersencc8ed391999-10-05 16:24:54 +0000146};
147
Erik Andersene49d5ec2000-02-08 19:58:47 +0000148static void stack_machine(const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000149{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000150 char *endPointer = 0;
151 double d;
152 const struct op *o = operators;
Eric Andersencc8ed391999-10-05 16:24:54 +0000153
Glenn L McGrath7991ad12004-07-24 06:01:52 +0000154 if (argument == 0)
Eric Andersencc8ed391999-10-05 16:24:54 +0000155 return;
Eric Andersencc8ed391999-10-05 16:24:54 +0000156
157 d = strtod(argument, &endPointer);
158
Erik Andersene49d5ec2000-02-08 19:58:47 +0000159 if (endPointer != argument) {
Eric Andersencc8ed391999-10-05 16:24:54 +0000160 push(d);
161 return;
162 }
163
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000164 while (o->name[0]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000165 if (strcmp(o->name, argument) == 0) {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000166 o->function();
Eric Andersencc8ed391999-10-05 16:24:54 +0000167 return;
168 }
169 o++;
170 }
Denis Vlasenko13858992006-10-08 12:49:22 +0000171 bb_error_msg_and_die("%s: syntax error", argument);
Eric Andersencc8ed391999-10-05 16:24:54 +0000172}
173
John Beppu5db60a72000-06-12 22:59:12 +0000174/* return pointer to next token in buffer and set *buffer to one char
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000175 * past the end of the above mentioned token
John Beppu5db60a72000-06-12 22:59:12 +0000176 */
177static char *get_token(char **buffer)
178{
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000179 char *start = NULL;
180 char *current;
John Beppu5db60a72000-06-12 22:59:12 +0000181
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000182 current = skip_whitespace(*buffer);
John Beppu5db60a72000-06-12 22:59:12 +0000183 if (*current != 0) {
184 start = current;
Bernhard Reutner-Fischerde17ece2007-04-10 09:38:35 +0000185 current = skip_non_whitespace(current);
John Beppu5db60a72000-06-12 22:59:12 +0000186 *buffer = current;
187 }
188 return start;
189}
190
191/* In Perl one might say, scalar m|\s*(\S+)\s*|g */
192static int number_of_tokens(char *buffer)
193{
194 int i = 0;
195 char *b = buffer;
196 while (get_token(&b)) { i++; }
197 return i;
198}
199
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000200int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
John Beppu00216792000-06-21 19:06:16 +0000201int dc_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000202{
John Beppu5db60a72000-06-12 22:59:12 +0000203 /* take stuff from stdin if no args are given */
204 if (argc <= 1) {
205 int i, len;
206 char *line = NULL;
207 char *cursor = NULL;
208 char *token = NULL;
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000209 while ((line = xmalloc_getline(stdin))) {
John Beppu5db60a72000-06-12 22:59:12 +0000210 cursor = line;
211 len = number_of_tokens(line);
212 for (i = 0; i < len; i++) {
213 token = get_token(&cursor);
214 *cursor++ = 0;
215 stack_machine(token);
216 }
217 free(line);
218 }
219 } else {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000220 if (*argv[1] == '-')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000221 bb_show_usage();
John Beppu5db60a72000-06-12 22:59:12 +0000222 while (argc >= 2) {
223 stack_machine(argv[1]);
224 argv++;
225 argc--;
226 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000227 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000228 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000229}