Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 4 | */ |
| 5 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 6 | #include "libbb.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 7 | #include <math.h> |
| 8 | |
| 9 | /* Tiny RPN calculator, because "expr" didn't give me bitwise operations. */ |
| 10 | |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 11 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 12 | struct globals { |
| 13 | unsigned pointer; |
| 14 | unsigned base; |
| 15 | double stack[1]; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 16 | } FIX_ALIASING; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 17 | enum { 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 Vlasenko | d8850f2 | 2008-12-30 10:40:05 +0000 | [diff] [blame] | 22 | #define INIT_G() do { \ |
| 23 | base = 10; \ |
| 24 | } while (0) |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 25 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 26 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 27 | static void push(double a) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 28 | { |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 29 | if (pointer >= STACK_SIZE) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 30 | bb_error_msg_and_die("stack overflow"); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 31 | stack[pointer++] = a; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 34 | static double pop(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 35 | { |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 36 | if (pointer == 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 37 | bb_error_msg_and_die("stack underflow"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 38 | return stack[--pointer]; |
| 39 | } |
| 40 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 41 | static void add(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 42 | { |
| 43 | push(pop() + pop()); |
| 44 | } |
| 45 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 46 | static void sub(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 47 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 48 | double subtrahend = pop(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 49 | |
| 50 | push(pop() - subtrahend); |
| 51 | } |
| 52 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 53 | static void mul(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 54 | { |
| 55 | push(pop() * pop()); |
| 56 | } |
| 57 | |
Denis Vlasenko | 0783230 | 2008-10-20 08:43:10 +0000 | [diff] [blame] | 58 | #if ENABLE_FEATURE_DC_LIBM |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 59 | static void power(void) |
| 60 | { |
| 61 | double topower = pop(); |
| 62 | |
| 63 | push(pow(pop(), topower)); |
| 64 | } |
Denis Vlasenko | 0783230 | 2008-10-20 08:43:10 +0000 | [diff] [blame] | 65 | #endif |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 66 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 67 | static void divide(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 68 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 69 | double divisor = pop(); |
| 70 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 71 | push(pop() / divisor); |
| 72 | } |
| 73 | |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 74 | static void mod(void) |
| 75 | { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 76 | unsigned d = pop(); |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 77 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 78 | push((unsigned) pop() % d); |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 81 | static void and(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 82 | { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 83 | push((unsigned) pop() & (unsigned) pop()); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 86 | static void or(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 87 | { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 88 | push((unsigned) pop() | (unsigned) pop()); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 91 | static void eor(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 92 | { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 93 | push((unsigned) pop() ^ (unsigned) pop()); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 96 | static void not(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 97 | { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 98 | push(~(unsigned) pop()); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 101 | static void set_output_base(void) |
| 102 | { |
Denis Vlasenko | 6b38e18 | 2008-10-30 23:25:50 +0000 | [diff] [blame] | 103 | static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 }; |
| 104 | unsigned b = (unsigned)pop(); |
| 105 | |
| 106 | base = *strchrnul(bases, b); |
| 107 | if (base == 0) { |
| 108 | bb_error_msg("error, base %u is not supported", b); |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 109 | base = 10; |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | static void print_base(double print) |
| 114 | { |
Denis Vlasenko | 6b38e18 | 2008-10-30 23:25:50 +0000 | [diff] [blame] | 115 | unsigned x, i; |
| 116 | |
| 117 | if (base == 10) { |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 118 | printf("%g\n", print); |
Denis Vlasenko | 6b38e18 | 2008-10-30 23:25:50 +0000 | [diff] [blame] | 119 | return; |
| 120 | } |
| 121 | |
| 122 | x = (unsigned)print; |
| 123 | switch (base) { |
| 124 | case 16: |
| 125 | printf("%x\n", x); |
| 126 | break; |
| 127 | case 8: |
| 128 | printf("%o\n", x); |
| 129 | break; |
| 130 | default: /* base 2 */ |
| 131 | i = (unsigned)INT_MAX + 1; |
| 132 | do { |
| 133 | if (x & i) break; |
| 134 | i >>= 1; |
| 135 | } while (i > 1); |
| 136 | do { |
| 137 | bb_putchar('1' - !(x & i)); |
| 138 | i >>= 1; |
| 139 | } while (i); |
| 140 | bb_putchar('\n'); |
| 141 | } |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static void print_stack_no_pop(void) |
| 145 | { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 146 | unsigned i = pointer; |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 147 | while (i) |
| 148 | print_base(stack[--i]); |
| 149 | } |
| 150 | |
| 151 | static void print_no_pop(void) |
| 152 | { |
| 153 | print_base(stack[pointer-1]); |
| 154 | } |
| 155 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 156 | struct op { |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 157 | const char name[4]; |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 158 | void (*function) (void); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 161 | static const struct op operators[] = { |
John Beppu | c035254 | 2000-06-21 18:00:46 +0000 | [diff] [blame] | 162 | {"+", add}, |
| 163 | {"add", add}, |
| 164 | {"-", sub}, |
| 165 | {"sub", sub}, |
| 166 | {"*", mul}, |
| 167 | {"mul", mul}, |
| 168 | {"/", divide}, |
| 169 | {"div", divide}, |
Denis Vlasenko | 0783230 | 2008-10-20 08:43:10 +0000 | [diff] [blame] | 170 | #if ENABLE_FEATURE_DC_LIBM |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 171 | {"**", power}, |
| 172 | {"exp", power}, |
| 173 | {"pow", power}, |
Denis Vlasenko | 0783230 | 2008-10-20 08:43:10 +0000 | [diff] [blame] | 174 | #endif |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 175 | {"%", mod}, |
| 176 | {"mod", mod}, |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 177 | {"and", and}, |
John Beppu | c035254 | 2000-06-21 18:00:46 +0000 | [diff] [blame] | 178 | {"or", or}, |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 179 | {"not", not}, |
| 180 | {"eor", eor}, |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 181 | {"xor", eor}, |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 182 | {"p", print_no_pop}, |
| 183 | {"f", print_stack_no_pop}, |
| 184 | {"o", set_output_base}, |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 185 | { "", NULL } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 188 | static void stack_machine(const char *argument) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 189 | { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 190 | char *endPointer; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 191 | double d; |
| 192 | const struct op *o = operators; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 193 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 194 | d = strtod(argument, &endPointer); |
| 195 | |
Denys Vlasenko | bd1de18 | 2009-12-30 18:37:08 +0100 | [diff] [blame] | 196 | if (endPointer != argument && *endPointer == '\0') { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 197 | push(d); |
| 198 | return; |
| 199 | } |
| 200 | |
Denys Vlasenko | bd1de18 | 2009-12-30 18:37:08 +0100 | [diff] [blame] | 201 | while (o->function) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 202 | if (strcmp(o->name, argument) == 0) { |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 203 | o->function(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 204 | return; |
| 205 | } |
| 206 | o++; |
| 207 | } |
Denys Vlasenko | bd1de18 | 2009-12-30 18:37:08 +0100 | [diff] [blame] | 208 | bb_error_msg_and_die("syntax error at '%s'", argument); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 209 | } |
| 210 | |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 211 | /* return pointer to next token in buffer and set *buffer to one char |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 212 | * past the end of the above mentioned token |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 213 | */ |
| 214 | static char *get_token(char **buffer) |
| 215 | { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 216 | char *current = skip_whitespace(*buffer); |
| 217 | if (*current != '\0') { |
| 218 | *buffer = skip_non_whitespace(current); |
| 219 | return current; |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 220 | } |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 221 | return NULL; |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 224 | int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 225 | int dc_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 226 | { |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 227 | INIT_G(); |
| 228 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 229 | argv++; |
| 230 | if (!argv[0]) { |
| 231 | /* take stuff from stdin if no args are given */ |
| 232 | char *line; |
| 233 | char *cursor; |
| 234 | char *token; |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 235 | while ((line = xmalloc_fgetline(stdin)) != NULL) { |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 236 | cursor = line; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 237 | while (1) { |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 238 | token = get_token(&cursor); |
Denys Vlasenko | bd1de18 | 2009-12-30 18:37:08 +0100 | [diff] [blame] | 239 | if (!token) |
| 240 | break; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 241 | *cursor++ = '\0'; |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 242 | stack_machine(token); |
| 243 | } |
| 244 | free(line); |
| 245 | } |
| 246 | } else { |
Denys Vlasenko | bd1de18 | 2009-12-30 18:37:08 +0100 | [diff] [blame] | 247 | // why? it breaks "dc -2 2 * p" |
| 248 | //if (argv[0][0] == '-') |
| 249 | // bb_show_usage(); |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 250 | do { |
| 251 | stack_machine(*argv); |
| 252 | } while (*++argv); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 253 | } |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 254 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 255 | } |