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