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]; |
| 16 | }; |
| 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}, |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 185 | { /* zero filled */ } |
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 | |
Glenn L McGrath | 7991ad1 | 2004-07-24 06:01:52 +0000 | [diff] [blame] | 194 | if (argument == 0) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 195 | return; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 196 | |
| 197 | d = strtod(argument, &endPointer); |
| 198 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 199 | if (endPointer != argument) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 200 | push(d); |
| 201 | return; |
| 202 | } |
| 203 | |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 204 | while (o->name[0]) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 205 | if (strcmp(o->name, argument) == 0) { |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 206 | o->function(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 207 | return; |
| 208 | } |
| 209 | o++; |
| 210 | } |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 211 | bb_error_msg_and_die("%s: syntax error", argument); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 212 | } |
| 213 | |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 214 | /* 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] | 215 | * past the end of the above mentioned token |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 216 | */ |
| 217 | static char *get_token(char **buffer) |
| 218 | { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 219 | char *current = skip_whitespace(*buffer); |
| 220 | if (*current != '\0') { |
| 221 | *buffer = skip_non_whitespace(current); |
| 222 | return current; |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 223 | } |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 224 | return NULL; |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 227 | int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 228 | int dc_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 229 | { |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 230 | INIT_G(); |
| 231 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 232 | argv++; |
| 233 | if (!argv[0]) { |
| 234 | /* take stuff from stdin if no args are given */ |
| 235 | char *line; |
| 236 | char *cursor; |
| 237 | char *token; |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 238 | while ((line = xmalloc_fgetline(stdin)) != NULL) { |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 239 | cursor = line; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 240 | while (1) { |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 241 | token = get_token(&cursor); |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 242 | if (!token) break; |
| 243 | *cursor++ = '\0'; |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 244 | stack_machine(token); |
| 245 | } |
| 246 | free(line); |
| 247 | } |
| 248 | } else { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 249 | if (argv[0][0] == '-') |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 250 | bb_show_usage(); |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 251 | do { |
| 252 | stack_machine(*argv); |
| 253 | } while (*++argv); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 254 | } |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 255 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 256 | } |