blob: 7d5886eb2f9f5e3111f0c59fd36ac374550ec116 [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 Vlasenko7049ff82008-06-25 09:53:17 +000022#define INIT_G() do { } while (0)
Denis Vlasenkob44c7902008-03-17 09:29:43 +000023
Eric Andersencc8ed391999-10-05 16:24:54 +000024
Erik Andersene49d5ec2000-02-08 19:58:47 +000025static void push(double a)
Eric Andersencc8ed391999-10-05 16:24:54 +000026{
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +000027 if (pointer >= STACK_SIZE)
Manuel Novoa III cad53642003-03-19 09:13:01 +000028 bb_error_msg_and_die("stack overflow");
Matt Kraai3e856ce2000-12-01 02:55:13 +000029 stack[pointer++] = a;
Eric Andersencc8ed391999-10-05 16:24:54 +000030}
31
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000032static double pop(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000033{
Matt Kraai3e856ce2000-12-01 02:55:13 +000034 if (pointer == 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000035 bb_error_msg_and_die("stack underflow");
Eric Andersencc8ed391999-10-05 16:24:54 +000036 return stack[--pointer];
37}
38
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000039static void add(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000040{
41 push(pop() + pop());
42}
43
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000044static void sub(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000045{
Erik Andersene49d5ec2000-02-08 19:58:47 +000046 double subtrahend = pop();
Eric Andersencc8ed391999-10-05 16:24:54 +000047
48 push(pop() - subtrahend);
49}
50
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000051static void mul(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000052{
53 push(pop() * pop());
54}
55
Denis Vlasenko07832302008-10-20 08:43:10 +000056#if ENABLE_FEATURE_DC_LIBM
Eric Andersena9287742003-10-22 11:24:39 +000057static void power(void)
58{
59 double topower = pop();
60
61 push(pow(pop(), topower));
62}
Denis Vlasenko07832302008-10-20 08:43:10 +000063#endif
Eric Andersena9287742003-10-22 11:24:39 +000064
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000065static void divide(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000066{
Erik Andersene49d5ec2000-02-08 19:58:47 +000067 double divisor = pop();
68
Eric Andersencc8ed391999-10-05 16:24:54 +000069 push(pop() / divisor);
70}
71
Eric Andersena9287742003-10-22 11:24:39 +000072static void mod(void)
73{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000074 unsigned d = pop();
Eric Andersena9287742003-10-22 11:24:39 +000075
Denis Vlasenkob44c7902008-03-17 09:29:43 +000076 push((unsigned) pop() % d);
Eric Andersena9287742003-10-22 11:24:39 +000077}
78
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000079static void and(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000080{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000081 push((unsigned) pop() & (unsigned) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000082}
83
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000084static void or(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000085{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000086 push((unsigned) pop() | (unsigned) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000087}
88
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000089static void eor(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000090{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000091 push((unsigned) pop() ^ (unsigned) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000092}
93
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000094static void not(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000095{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000096 push(~(unsigned) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000097}
98
Glenn L McGrath6d074322002-12-12 10:31:53 +000099static void set_output_base(void)
100{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000101 base = (unsigned)pop();
Glenn L McGrath6d074322002-12-12 10:31:53 +0000102 if ((base != 10) && (base != 16)) {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000103 bb_error_msg("error, base %d is not supported", base);
104 base = 10;
Glenn L McGrath6d074322002-12-12 10:31:53 +0000105 }
106}
107
108static void print_base(double print)
109{
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000110 if (base == 16)
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000111 printf("%x\n", (unsigned)print);
Glenn L McGrath6d074322002-12-12 10:31:53 +0000112 else
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000113 printf("%g\n", print);
Glenn L McGrath6d074322002-12-12 10:31:53 +0000114}
115
116static void print_stack_no_pop(void)
117{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000118 unsigned i = pointer;
Glenn L McGrath6d074322002-12-12 10:31:53 +0000119 while (i)
120 print_base(stack[--i]);
121}
122
123static void print_no_pop(void)
124{
125 print_base(stack[pointer-1]);
126}
127
Eric Andersencc8ed391999-10-05 16:24:54 +0000128struct op {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000129 const char name[4];
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000130 void (*function) (void);
Eric Andersencc8ed391999-10-05 16:24:54 +0000131};
132
Erik Andersene49d5ec2000-02-08 19:58:47 +0000133static const struct op operators[] = {
John Beppuc0352542000-06-21 18:00:46 +0000134 {"+", add},
135 {"add", add},
136 {"-", sub},
137 {"sub", sub},
138 {"*", mul},
139 {"mul", mul},
140 {"/", divide},
141 {"div", divide},
Denis Vlasenko07832302008-10-20 08:43:10 +0000142#if ENABLE_FEATURE_DC_LIBM
Eric Andersena9287742003-10-22 11:24:39 +0000143 {"**", power},
144 {"exp", power},
145 {"pow", power},
Denis Vlasenko07832302008-10-20 08:43:10 +0000146#endif
Eric Andersena9287742003-10-22 11:24:39 +0000147 {"%", mod},
148 {"mod", mod},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000149 {"and", and},
John Beppuc0352542000-06-21 18:00:46 +0000150 {"or", or},
Erik Andersen5e1189e2000-04-15 16:34:54 +0000151 {"not", not},
152 {"eor", eor},
Eric Andersena9287742003-10-22 11:24:39 +0000153 {"xor", eor},
Glenn L McGrath6d074322002-12-12 10:31:53 +0000154 {"p", print_no_pop},
155 {"f", print_stack_no_pop},
156 {"o", set_output_base},
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000157 { /* zero filled */ }
Eric Andersencc8ed391999-10-05 16:24:54 +0000158};
159
Erik Andersene49d5ec2000-02-08 19:58:47 +0000160static void stack_machine(const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000161{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000162 char *endPointer;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000163 double d;
164 const struct op *o = operators;
Eric Andersencc8ed391999-10-05 16:24:54 +0000165
Glenn L McGrath7991ad12004-07-24 06:01:52 +0000166 if (argument == 0)
Eric Andersencc8ed391999-10-05 16:24:54 +0000167 return;
Eric Andersencc8ed391999-10-05 16:24:54 +0000168
169 d = strtod(argument, &endPointer);
170
Erik Andersene49d5ec2000-02-08 19:58:47 +0000171 if (endPointer != argument) {
Eric Andersencc8ed391999-10-05 16:24:54 +0000172 push(d);
173 return;
174 }
175
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000176 while (o->name[0]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000177 if (strcmp(o->name, argument) == 0) {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000178 o->function();
Eric Andersencc8ed391999-10-05 16:24:54 +0000179 return;
180 }
181 o++;
182 }
Denis Vlasenko13858992006-10-08 12:49:22 +0000183 bb_error_msg_and_die("%s: syntax error", argument);
Eric Andersencc8ed391999-10-05 16:24:54 +0000184}
185
John Beppu5db60a72000-06-12 22:59:12 +0000186/* return pointer to next token in buffer and set *buffer to one char
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000187 * past the end of the above mentioned token
John Beppu5db60a72000-06-12 22:59:12 +0000188 */
189static char *get_token(char **buffer)
190{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000191 char *current = skip_whitespace(*buffer);
192 if (*current != '\0') {
193 *buffer = skip_non_whitespace(current);
194 return current;
John Beppu5db60a72000-06-12 22:59:12 +0000195 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000196 return NULL;
John Beppu5db60a72000-06-12 22:59:12 +0000197}
198
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000199int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000200int dc_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000201{
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000202 INIT_G();
203
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000204 argv++;
205 if (!argv[0]) {
206 /* take stuff from stdin if no args are given */
207 char *line;
208 char *cursor;
209 char *token;
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000210 while ((line = xmalloc_fgetline(stdin)) != NULL) {
John Beppu5db60a72000-06-12 22:59:12 +0000211 cursor = line;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000212 while (1) {
John Beppu5db60a72000-06-12 22:59:12 +0000213 token = get_token(&cursor);
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000214 if (!token) break;
215 *cursor++ = '\0';
John Beppu5db60a72000-06-12 22:59:12 +0000216 stack_machine(token);
217 }
218 free(line);
219 }
220 } else {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000221 if (argv[0][0] == '-')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000222 bb_show_usage();
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000223 do {
224 stack_machine(*argv);
225 } while (*++argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000226 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000227 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000228}