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