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