blob: ef93c20bad0fc5207e59e2f8b7538e9a2ce7e869 [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/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00004 */
Eric Andersencc8ed391999-10-05 16:24:54 +00005
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01006/* config/applet/usage bits are in bc.c */
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +01007
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01008//#include "libbb.h"
9//#include "common_bufsiz.h"
Denys Vlasenkofb4da162016-11-22 23:14:24 +010010#include <math.h>
11
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +020012#if 0
13typedef unsigned data_t;
14#define DATA_FMT ""
15#elif 0
16typedef unsigned long data_t;
17#define DATA_FMT "l"
18#else
19typedef unsigned long long data_t;
20#define DATA_FMT "ll"
21#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000022
Denis Vlasenkob44c7902008-03-17 09:29:43 +000023struct globals {
24 unsigned pointer;
25 unsigned base;
26 double stack[1];
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010027} FIX_ALIASING;
Denis Vlasenkob44c7902008-03-17 09:29:43 +000028enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) };
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020029#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenkob44c7902008-03-17 09:29:43 +000030#define pointer (G.pointer )
31#define base (G.base )
32#define stack (G.stack )
Denis Vlasenkod8850f22008-12-30 10:40:05 +000033#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020034 setup_common_bufsiz(); \
Denis Vlasenkod8850f22008-12-30 10:40:05 +000035 base = 10; \
36} while (0)
Denis Vlasenkob44c7902008-03-17 09:29:43 +000037
Denys Vlasenko89023b12019-05-09 15:58:46 +020038static unsigned check_under(void)
Denys Vlasenkoc4603fb2015-05-25 13:31:25 +020039{
Denys Vlasenko89023b12019-05-09 15:58:46 +020040 unsigned p = pointer;
41 if (p == 0)
James Byrne69374872019-07-02 11:35:03 +020042 bb_simple_error_msg_and_die("stack underflow");
Denys Vlasenko89023b12019-05-09 15:58:46 +020043 return p - 1;
Denys Vlasenkoc4603fb2015-05-25 13:31:25 +020044}
45
Erik Andersene49d5ec2000-02-08 19:58:47 +000046static void push(double a)
Eric Andersencc8ed391999-10-05 16:24:54 +000047{
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +000048 if (pointer >= STACK_SIZE)
James Byrne69374872019-07-02 11:35:03 +020049 bb_simple_error_msg_and_die("stack overflow");
Matt Kraai3e856ce2000-12-01 02:55:13 +000050 stack[pointer++] = a;
Eric Andersencc8ed391999-10-05 16:24:54 +000051}
52
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000053static double pop(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000054{
Denys Vlasenko89023b12019-05-09 15:58:46 +020055 unsigned p = check_under();
56 pointer = p;
57 return stack[p];
Eric Andersencc8ed391999-10-05 16:24:54 +000058}
59
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000060static void add(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000061{
62 push(pop() + pop());
63}
64
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000065static void sub(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000066{
Erik Andersene49d5ec2000-02-08 19:58:47 +000067 double subtrahend = pop();
Eric Andersencc8ed391999-10-05 16:24:54 +000068
69 push(pop() - subtrahend);
70}
71
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000072static void mul(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000073{
74 push(pop() * pop());
75}
76
Denis Vlasenko07832302008-10-20 08:43:10 +000077#if ENABLE_FEATURE_DC_LIBM
Eric Andersena9287742003-10-22 11:24:39 +000078static void power(void)
79{
80 double topower = pop();
81
82 push(pow(pop(), topower));
83}
Denis Vlasenko07832302008-10-20 08:43:10 +000084#endif
Eric Andersena9287742003-10-22 11:24:39 +000085
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +000086static void divide(void)
Eric Andersencc8ed391999-10-05 16:24:54 +000087{
Erik Andersene49d5ec2000-02-08 19:58:47 +000088 double divisor = pop();
89
Eric Andersencc8ed391999-10-05 16:24:54 +000090 push(pop() / divisor);
91}
92
Eric Andersena9287742003-10-22 11:24:39 +000093static void mod(void)
94{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +020095 data_t d = pop();
Eric Andersena9287742003-10-22 11:24:39 +000096
Denys Vlasenko11139612019-05-16 09:40:36 +020097 /* compat with dc (GNU bc 1.07.1) 1.4.1:
98 * $ dc -e '4 0 % p'
99 * dc: remainder by zero
100 * 0
101 */
102 if (d == 0) {
103 bb_error_msg("remainder by zero");
104 pop();
105 push(0);
106 return;
107 }
108 /* ^^^^ without this, we simply get SIGFPE and die */
Denys Vlasenko89023b12019-05-09 15:58:46 +0200109
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200110 push((data_t) pop() % d);
Eric Andersena9287742003-10-22 11:24:39 +0000111}
112
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000113static void and(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000114{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200115 push((data_t) pop() & (data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000116}
117
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000118static void or(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000119{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200120 push((data_t) pop() | (data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000121}
122
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000123static void eor(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000124{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200125 push((data_t) pop() ^ (data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000126}
127
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000128static void not(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000129{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200130 push(~(data_t) pop());
Eric Andersencc8ed391999-10-05 16:24:54 +0000131}
132
Glenn L McGrath6d074322002-12-12 10:31:53 +0000133static void set_output_base(void)
134{
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000135 static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 };
136 unsigned b = (unsigned)pop();
137
138 base = *strchrnul(bases, b);
139 if (base == 0) {
140 bb_error_msg("error, base %u is not supported", b);
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000141 base = 10;
Glenn L McGrath6d074322002-12-12 10:31:53 +0000142 }
143}
144
145static void print_base(double print)
146{
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200147 data_t x, i;
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000148
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200149 x = (data_t) print;
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000150 if (base == 10) {
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200151 if (x == print) /* exactly representable as unsigned integer */
152 printf("%"DATA_FMT"u\n", x);
153 else
154 printf("%g\n", print);
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000155 return;
156 }
157
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000158 switch (base) {
159 case 16:
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200160 printf("%"DATA_FMT"x\n", x);
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000161 break;
162 case 8:
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200163 printf("%"DATA_FMT"o\n", x);
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000164 break;
165 default: /* base 2 */
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200166 i = MAXINT(data_t) - (MAXINT(data_t) >> 1);
167 /* i is 100000...00000 */
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000168 do {
Denys Vlasenko7a07b0e2010-07-29 04:00:27 +0200169 if (x & i)
170 break;
Denis Vlasenko6b38e182008-10-30 23:25:50 +0000171 i >>= 1;
172 } while (i > 1);
173 do {
174 bb_putchar('1' - !(x & i));
175 i >>= 1;
176 } while (i);
177 bb_putchar('\n');
178 }
Glenn L McGrath6d074322002-12-12 10:31:53 +0000179}
180
181static void print_stack_no_pop(void)
182{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000183 unsigned i = pointer;
Glenn L McGrath6d074322002-12-12 10:31:53 +0000184 while (i)
185 print_base(stack[--i]);
186}
187
188static void print_no_pop(void)
189{
Denys Vlasenko89023b12019-05-09 15:58:46 +0200190 print_base(stack[check_under()]);
Glenn L McGrath6d074322002-12-12 10:31:53 +0000191}
192
Eric Andersencc8ed391999-10-05 16:24:54 +0000193struct op {
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000194 const char name[4];
Aaron Lehmann2dd2d7a2001-12-06 03:29:37 +0000195 void (*function) (void);
Eric Andersencc8ed391999-10-05 16:24:54 +0000196};
197
Erik Andersene49d5ec2000-02-08 19:58:47 +0000198static const struct op operators[] = {
Denis Vlasenko07832302008-10-20 08:43:10 +0000199#if ENABLE_FEATURE_DC_LIBM
Denys Vlasenkod0bc5fd2018-12-08 18:59:07 +0100200 {"^", power},
201// {"exp", power},
202// {"pow", power},
Denis Vlasenko07832302008-10-20 08:43:10 +0000203#endif
Eric Andersena9287742003-10-22 11:24:39 +0000204 {"%", mod},
Denys Vlasenkod0bc5fd2018-12-08 18:59:07 +0100205// {"mod", mod},
206 // logic ops are not standard, remove?
Erik Andersene49d5ec2000-02-08 19:58:47 +0000207 {"and", and},
John Beppuc0352542000-06-21 18:00:46 +0000208 {"or", or},
Erik Andersen5e1189e2000-04-15 16:34:54 +0000209 {"not", not},
Eric Andersena9287742003-10-22 11:24:39 +0000210 {"xor", eor},
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100211 {"+", add},
Denys Vlasenkod0bc5fd2018-12-08 18:59:07 +0100212// {"add", add},
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100213 {"-", sub},
Denys Vlasenkod0bc5fd2018-12-08 18:59:07 +0100214// {"sub", sub},
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100215 {"*", mul},
Denys Vlasenkod0bc5fd2018-12-08 18:59:07 +0100216// {"mul", mul},
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100217 {"/", divide},
Denys Vlasenkod0bc5fd2018-12-08 18:59:07 +0100218// {"div", divide},
Glenn L McGrath6d074322002-12-12 10:31:53 +0000219 {"p", print_no_pop},
220 {"f", print_stack_no_pop},
221 {"o", set_output_base},
Eric Andersencc8ed391999-10-05 16:24:54 +0000222};
223
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100224/* Feed the stack machine */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225static void stack_machine(const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000226{
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100227 char *end;
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100228 double number;
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100229 const struct op *o;
Eric Andersencc8ed391999-10-05 16:24:54 +0000230
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100231 next:
232 number = strtod(argument, &end);
233 if (end != argument) {
234 argument = end;
235 push(number);
236 goto next;
Eric Andersencc8ed391999-10-05 16:24:54 +0000237 }
238
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100239 /* We might have matched a digit, eventually advance the argument */
240 argument = skip_whitespace(argument);
241
242 if (*argument == '\0')
243 return;
244
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100245 o = operators;
246 do {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100247 char *after_name = is_prefixed_with(argument, o->name);
248 if (after_name) {
249 argument = after_name;
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000250 o->function();
Bernhard Reutner-Fischer70e30e82015-02-16 17:12:04 +0100251 goto next;
Eric Andersencc8ed391999-10-05 16:24:54 +0000252 }
253 o++;
Denys Vlasenko9daf33f2013-01-18 13:30:13 +0100254 } while (o != operators + ARRAY_SIZE(operators));
255
Denys Vlasenkobd1de182009-12-30 18:37:08 +0100256 bb_error_msg_and_die("syntax error at '%s'", argument);
Eric Andersencc8ed391999-10-05 16:24:54 +0000257}
258
Denys Vlasenkod0bc5fd2018-12-08 18:59:07 +0100259static void process_file(FILE *fp)
260{
261 char *line;
262 while ((line = xmalloc_fgetline(fp)) != NULL) {
263 stack_machine(line);
264 free(line);
265 }
266}
267
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000268int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000269int dc_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000270{
Denys Vlasenkod0bc5fd2018-12-08 18:59:07 +0100271 bool script = 0;
272
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000273 INIT_G();
274
Denys Vlasenkod0bc5fd2018-12-08 18:59:07 +0100275 /* Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs */
276 for (;;) {
277 int n = getopt(argc, argv, "e:f:");
278 if (n <= 0)
279 break;
280 switch (n) {
281 case 'e':
282 script = 1;
283 stack_machine(optarg);
284 break;
285 case 'f':
286 script = 1;
287 process_file(xfopen_for_read(optarg));
288 break;
289 default:
290 bb_show_usage();
John Beppu5db60a72000-06-12 22:59:12 +0000291 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000292 }
Denys Vlasenkod0bc5fd2018-12-08 18:59:07 +0100293 argv += optind;
294
295 if (*argv) {
296 do
297 process_file(xfopen_for_read(*argv++));
298 while (*argv);
299 } else if (!script) {
300 /* Take stuff from stdin if no args are given */
301 process_file(stdin);
302 }
303
Matt Kraai3e856ce2000-12-01 02:55:13 +0000304 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000305}