blob: bd93ef68bc96e21f6ab0207bf1d67169da730e2a [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
Eric Andersena9287742003-10-22 11:24:39 +000056static void power(void)
57{
58 double topower = pop();
59
60 push(pow(pop(), topower));
61}
62
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000063static void divide(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000064{
Erik Andersene49d5ec2000-02-08 19:58:47 +000065 double divisor = pop();
66
Eric Andersencc8ed391999-10-05 16:24:54 +000067 push(pop() / divisor);
68}
69
Eric Andersena9287742003-10-22 11:24:39 +000070static void mod(void)
71{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000072 unsigned d = pop();
Eric Andersena9287742003-10-22 11:24:39 +000073
Denis Vlasenkob44c7902008-03-17 09:29:43 +000074 push((unsigned) pop() % d);
Eric Andersena9287742003-10-22 11:24:39 +000075}
76
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000077static void and(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000078{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000079 push((unsigned) pop() & (unsigned) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000080}
81
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000082static void or(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000083{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000084 push((unsigned) pop() | (unsigned) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000085}
86
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000087static void eor(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000088{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000089 push((unsigned) pop() ^ (unsigned) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000090}
91
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000092static void not(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000093{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000094 push(~(unsigned) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +000095}
96
Glenn L McGrath6d074322002-12-12 10:31:53 +000097static void set_output_base(void)
98{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000099 base = (unsigned)pop();
Glenn L McGrath6d074322002-12-12 10:31:53 +0000100 if ((base != 10) && (base != 16)) {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000101 bb_error_msg("error, base %d is not supported", base);
102 base = 10;
Glenn L McGrath6d074322002-12-12 10:31:53 +0000103 }
104}
105
106static void print_base(double print)
107{
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000108 if (base == 16)
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000109 printf("%x\n", (unsigned)print);
Glenn L McGrath6d074322002-12-12 10:31:53 +0000110 else
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000111 printf("%g\n", print);
Glenn L McGrath6d074322002-12-12 10:31:53 +0000112}
113
114static void print_stack_no_pop(void)
115{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000116 unsigned i = pointer;
Glenn L McGrath6d074322002-12-12 10:31:53 +0000117 while (i)
118 print_base(stack[--i]);
119}
120
121static void print_no_pop(void)
122{
123 print_base(stack[pointer-1]);
124}
125
Eric Andersencc8ed391999-10-05 16:24:54 +0000126struct op {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000127 const char name[4];
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000128 void (*function) (void);
Eric Andersencc8ed391999-10-05 16:24:54 +0000129};
130
Erik Andersene49d5ec2000-02-08 19:58:47 +0000131static const struct op operators[] = {
John Beppuc0352542000-06-21 18:00:46 +0000132 {"+", add},
133 {"add", add},
134 {"-", sub},
135 {"sub", sub},
136 {"*", mul},
137 {"mul", mul},
138 {"/", divide},
139 {"div", divide},
Eric Andersena9287742003-10-22 11:24:39 +0000140 {"**", power},
141 {"exp", power},
142 {"pow", power},
143 {"%", mod},
144 {"mod", mod},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000145 {"and", and},
John Beppuc0352542000-06-21 18:00:46 +0000146 {"or", or},
Erik Andersen5e1189e2000-04-15 16:34:54 +0000147 {"not", not},
148 {"eor", eor},
Eric Andersena9287742003-10-22 11:24:39 +0000149 {"xor", eor},
Glenn L McGrath6d074322002-12-12 10:31:53 +0000150 {"p", print_no_pop},
151 {"f", print_stack_no_pop},
152 {"o", set_output_base},
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000153 { /* zero filled */ }
Eric Andersencc8ed391999-10-05 16:24:54 +0000154};
155
Erik Andersene49d5ec2000-02-08 19:58:47 +0000156static void stack_machine(const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000157{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000158 char *endPointer;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000159 double d;
160 const struct op *o = operators;
Eric Andersencc8ed391999-10-05 16:24:54 +0000161
Glenn L McGrath7991ad12004-07-24 06:01:52 +0000162 if (argument == 0)
Eric Andersencc8ed391999-10-05 16:24:54 +0000163 return;
Eric Andersencc8ed391999-10-05 16:24:54 +0000164
165 d = strtod(argument, &endPointer);
166
Erik Andersene49d5ec2000-02-08 19:58:47 +0000167 if (endPointer != argument) {
Eric Andersencc8ed391999-10-05 16:24:54 +0000168 push(d);
169 return;
170 }
171
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000172 while (o->name[0]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000173 if (strcmp(o->name, argument) == 0) {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000174 o->function();
Eric Andersencc8ed391999-10-05 16:24:54 +0000175 return;
176 }
177 o++;
178 }
Denis Vlasenko13858992006-10-08 12:49:22 +0000179 bb_error_msg_and_die("%s: syntax error", argument);
Eric Andersencc8ed391999-10-05 16:24:54 +0000180}
181
John Beppu5db60a72000-06-12 22:59:12 +0000182/* return pointer to next token in buffer and set *buffer to one char
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000183 * past the end of the above mentioned token
John Beppu5db60a72000-06-12 22:59:12 +0000184 */
185static char *get_token(char **buffer)
186{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000187 char *current = skip_whitespace(*buffer);
188 if (*current != '\0') {
189 *buffer = skip_non_whitespace(current);
190 return current;
John Beppu5db60a72000-06-12 22:59:12 +0000191 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000192 return NULL;
John Beppu5db60a72000-06-12 22:59:12 +0000193}
194
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000195int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000196int dc_main(int argc ATTRIBUTE_UNUSED, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000197{
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000198 INIT_G();
199
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000200 argv++;
201 if (!argv[0]) {
202 /* take stuff from stdin if no args are given */
203 char *line;
204 char *cursor;
205 char *token;
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000206 while ((line = xmalloc_fgetline(stdin)) != NULL) {
John Beppu5db60a72000-06-12 22:59:12 +0000207 cursor = line;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000208 while (1) {
John Beppu5db60a72000-06-12 22:59:12 +0000209 token = get_token(&cursor);
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000210 if (!token) break;
211 *cursor++ = '\0';
John Beppu5db60a72000-06-12 22:59:12 +0000212 stack_machine(token);
213 }
214 free(line);
215 }
216 } else {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000217 if (argv[0][0] == '-')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000218 bb_show_usage();
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000219 do {
220 stack_machine(*argv);
221 } while (*++argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000222 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000223 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000224}