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 | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 11 | enum { STACK_SIZE = COMMON_BUFSIZE / sizeof(double) }; |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 12 | |
| 13 | #define stack ((double*)&bb_common_bufsiz1) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 14 | static unsigned int pointer; |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 15 | static unsigned char base; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 16 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 17 | static void push(double a) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 18 | { |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 19 | if (pointer >= STACK_SIZE) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 20 | bb_error_msg_and_die("stack overflow"); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 21 | stack[pointer++] = a; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 22 | } |
| 23 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 24 | static double pop(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 25 | { |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 26 | if (pointer == 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 27 | bb_error_msg_and_die("stack underflow"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 28 | return stack[--pointer]; |
| 29 | } |
| 30 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 31 | static void add(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 32 | { |
| 33 | push(pop() + pop()); |
| 34 | } |
| 35 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 36 | static void sub(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 37 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 38 | double subtrahend = pop(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 39 | |
| 40 | push(pop() - subtrahend); |
| 41 | } |
| 42 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 43 | static void mul(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 44 | { |
| 45 | push(pop() * pop()); |
| 46 | } |
| 47 | |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 48 | static void power(void) |
| 49 | { |
| 50 | double topower = pop(); |
| 51 | |
| 52 | push(pow(pop(), topower)); |
| 53 | } |
| 54 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 55 | static void divide(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 56 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 57 | double divisor = pop(); |
| 58 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 59 | push(pop() / divisor); |
| 60 | } |
| 61 | |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 62 | static void mod(void) |
| 63 | { |
| 64 | unsigned int d = pop(); |
| 65 | |
| 66 | push((unsigned int) pop() % d); |
| 67 | } |
| 68 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 69 | static void and(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 70 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 71 | push((unsigned int) pop() & (unsigned int) pop()); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 74 | static void or(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 75 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 76 | push((unsigned int) pop() | (unsigned int) pop()); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 79 | static void eor(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 80 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 81 | push((unsigned int) pop() ^ (unsigned int) pop()); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 84 | static void not(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 85 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 86 | push(~(unsigned int) pop()); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 89 | static void set_output_base(void) |
| 90 | { |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 91 | base = (unsigned char)pop(); |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 92 | if ((base != 10) && (base != 16)) { |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 93 | bb_error_msg("error, base %d is not supported", base); |
| 94 | base = 10; |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | static void print_base(double print) |
| 99 | { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 100 | if (base == 16) |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 101 | printf("%x\n", (unsigned int)print); |
| 102 | else |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 103 | printf("%g\n", print); |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static void print_stack_no_pop(void) |
| 107 | { |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 108 | unsigned int i = pointer; |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 109 | while (i) |
| 110 | print_base(stack[--i]); |
| 111 | } |
| 112 | |
| 113 | static void print_no_pop(void) |
| 114 | { |
| 115 | print_base(stack[pointer-1]); |
| 116 | } |
| 117 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 118 | struct op { |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 119 | const char name[4]; |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 120 | void (*function) (void); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 123 | static const struct op operators[] = { |
John Beppu | c035254 | 2000-06-21 18:00:46 +0000 | [diff] [blame] | 124 | {"+", add}, |
| 125 | {"add", add}, |
| 126 | {"-", sub}, |
| 127 | {"sub", sub}, |
| 128 | {"*", mul}, |
| 129 | {"mul", mul}, |
| 130 | {"/", divide}, |
| 131 | {"div", divide}, |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 132 | {"**", power}, |
| 133 | {"exp", power}, |
| 134 | {"pow", power}, |
| 135 | {"%", mod}, |
| 136 | {"mod", mod}, |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 137 | {"and", and}, |
John Beppu | c035254 | 2000-06-21 18:00:46 +0000 | [diff] [blame] | 138 | {"or", or}, |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 139 | {"not", not}, |
| 140 | {"eor", eor}, |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 141 | {"xor", eor}, |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 142 | {"p", print_no_pop}, |
| 143 | {"f", print_stack_no_pop}, |
| 144 | {"o", set_output_base}, |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 145 | {"", 0} |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 146 | }; |
| 147 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 148 | static void stack_machine(const char *argument) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 149 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 150 | char *endPointer = 0; |
| 151 | double d; |
| 152 | const struct op *o = operators; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 153 | |
Glenn L McGrath | 7991ad1 | 2004-07-24 06:01:52 +0000 | [diff] [blame] | 154 | if (argument == 0) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 155 | return; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 156 | |
| 157 | d = strtod(argument, &endPointer); |
| 158 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 159 | if (endPointer != argument) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 160 | push(d); |
| 161 | return; |
| 162 | } |
| 163 | |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 164 | while (o->name[0]) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 165 | if (strcmp(o->name, argument) == 0) { |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 166 | o->function(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 167 | return; |
| 168 | } |
| 169 | o++; |
| 170 | } |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 171 | bb_error_msg_and_die("%s: syntax error", argument); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 172 | } |
| 173 | |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 174 | /* 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] | 175 | * past the end of the above mentioned token |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 176 | */ |
| 177 | static char *get_token(char **buffer) |
| 178 | { |
Denis Vlasenko | d18a3a2 | 2006-10-25 12:46:03 +0000 | [diff] [blame] | 179 | char *start = NULL; |
| 180 | char *current; |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 181 | |
Denis Vlasenko | d18a3a2 | 2006-10-25 12:46:03 +0000 | [diff] [blame] | 182 | current = skip_whitespace(*buffer); |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 183 | if (*current != 0) { |
| 184 | start = current; |
Bernhard Reutner-Fischer | de17ece | 2007-04-10 09:38:35 +0000 | [diff] [blame] | 185 | current = skip_non_whitespace(current); |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 186 | *buffer = current; |
| 187 | } |
| 188 | return start; |
| 189 | } |
| 190 | |
| 191 | /* In Perl one might say, scalar m|\s*(\S+)\s*|g */ |
| 192 | static int number_of_tokens(char *buffer) |
| 193 | { |
| 194 | int i = 0; |
| 195 | char *b = buffer; |
| 196 | while (get_token(&b)) { i++; } |
| 197 | return i; |
| 198 | } |
| 199 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 200 | int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
John Beppu | 0021679 | 2000-06-21 19:06:16 +0000 | [diff] [blame] | 201 | int dc_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 202 | { |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 203 | /* take stuff from stdin if no args are given */ |
| 204 | if (argc <= 1) { |
| 205 | int i, len; |
| 206 | char *line = NULL; |
| 207 | char *cursor = NULL; |
| 208 | char *token = NULL; |
Denis Vlasenko | 2d5ca60 | 2006-10-12 22:43:20 +0000 | [diff] [blame] | 209 | while ((line = xmalloc_getline(stdin))) { |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 210 | cursor = line; |
| 211 | len = number_of_tokens(line); |
| 212 | for (i = 0; i < len; i++) { |
| 213 | token = get_token(&cursor); |
| 214 | *cursor++ = 0; |
| 215 | stack_machine(token); |
| 216 | } |
| 217 | free(line); |
| 218 | } |
| 219 | } else { |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 220 | if (*argv[1] == '-') |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 221 | bb_show_usage(); |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 222 | while (argc >= 2) { |
| 223 | stack_machine(argv[1]); |
| 224 | argv++; |
| 225 | argc--; |
| 226 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 227 | } |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 228 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 229 | } |