[821] | 1 | /* vi: set sw=4 ts=4: */
|
---|
[1765] | 2 | /*
|
---|
[2725] | 3 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[1765] | 4 | */
|
---|
| 5 |
|
---|
| 6 | #include "libbb.h"
|
---|
[3621] | 7 | #include "common_bufsiz.h"
|
---|
[821] | 8 | #include <math.h>
|
---|
| 9 |
|
---|
[2725] | 10 | //usage:#define dc_trivial_usage
|
---|
| 11 | //usage: "EXPRESSION..."
|
---|
| 12 | //usage:
|
---|
| 13 | //usage:#define dc_full_usage "\n\n"
|
---|
| 14 | //usage: "Tiny RPN calculator. Operations:\n"
|
---|
[3232] | 15 | //usage: "+, add, -, sub, *, mul, /, div, %, mod, "IF_FEATURE_DC_LIBM("**, exp, ")"and, or, not, xor,\n"
|
---|
[2725] | 16 | //usage: "p - print top of the stack (without popping),\n"
|
---|
| 17 | //usage: "f - print entire stack,\n"
|
---|
| 18 | //usage: "o - pop the value and set output radix (must be 10, 16, 8 or 2).\n"
|
---|
[3232] | 19 | //usage: "Examples: 'dc 2 2 add p' -> 4, 'dc 8 8 mul 2 2 + / p' -> 16"
|
---|
[2725] | 20 | //usage:
|
---|
| 21 | //usage:#define dc_example_usage
|
---|
| 22 | //usage: "$ dc 2 2 + p\n"
|
---|
| 23 | //usage: "4\n"
|
---|
| 24 | //usage: "$ dc 8 8 \\* 2 2 + / p\n"
|
---|
| 25 | //usage: "16\n"
|
---|
| 26 | //usage: "$ dc 0 1 and p\n"
|
---|
| 27 | //usage: "0\n"
|
---|
| 28 | //usage: "$ dc 0 1 or p\n"
|
---|
| 29 | //usage: "1\n"
|
---|
| 30 | //usage: "$ echo 72 9 div 8 mul p | dc\n"
|
---|
| 31 | //usage: "64\n"
|
---|
[821] | 32 |
|
---|
[2725] | 33 | #if 0
|
---|
| 34 | typedef unsigned data_t;
|
---|
| 35 | #define DATA_FMT ""
|
---|
| 36 | #elif 0
|
---|
| 37 | typedef unsigned long data_t;
|
---|
| 38 | #define DATA_FMT "l"
|
---|
| 39 | #else
|
---|
| 40 | typedef unsigned long long data_t;
|
---|
| 41 | #define DATA_FMT "ll"
|
---|
| 42 | #endif
|
---|
[1765] | 43 |
|
---|
[821] | 44 |
|
---|
[2725] | 45 | struct globals {
|
---|
| 46 | unsigned pointer;
|
---|
| 47 | unsigned base;
|
---|
| 48 | double stack[1];
|
---|
| 49 | } FIX_ALIASING;
|
---|
| 50 | enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) };
|
---|
[3621] | 51 | #define G (*(struct globals*)bb_common_bufsiz1)
|
---|
[2725] | 52 | #define pointer (G.pointer )
|
---|
| 53 | #define base (G.base )
|
---|
| 54 | #define stack (G.stack )
|
---|
| 55 | #define INIT_G() do { \
|
---|
[3621] | 56 | setup_common_bufsiz(); \
|
---|
[2725] | 57 | base = 10; \
|
---|
| 58 | } while (0)
|
---|
| 59 |
|
---|
| 60 |
|
---|
[3621] | 61 | static void check_under(void)
|
---|
| 62 | {
|
---|
| 63 | if (pointer == 0)
|
---|
| 64 | bb_error_msg_and_die("stack underflow");
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[821] | 67 | static void push(double a)
|
---|
| 68 | {
|
---|
[1765] | 69 | if (pointer >= STACK_SIZE)
|
---|
[821] | 70 | bb_error_msg_and_die("stack overflow");
|
---|
| 71 | stack[pointer++] = a;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | static double pop(void)
|
---|
| 75 | {
|
---|
[3621] | 76 | check_under();
|
---|
[821] | 77 | return stack[--pointer];
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | static void add(void)
|
---|
| 81 | {
|
---|
| 82 | push(pop() + pop());
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | static void sub(void)
|
---|
| 86 | {
|
---|
| 87 | double subtrahend = pop();
|
---|
| 88 |
|
---|
| 89 | push(pop() - subtrahend);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | static void mul(void)
|
---|
| 93 | {
|
---|
| 94 | push(pop() * pop());
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[2725] | 97 | #if ENABLE_FEATURE_DC_LIBM
|
---|
[821] | 98 | static void power(void)
|
---|
| 99 | {
|
---|
| 100 | double topower = pop();
|
---|
| 101 |
|
---|
| 102 | push(pow(pop(), topower));
|
---|
| 103 | }
|
---|
[2725] | 104 | #endif
|
---|
[821] | 105 |
|
---|
| 106 | static void divide(void)
|
---|
| 107 | {
|
---|
| 108 | double divisor = pop();
|
---|
| 109 |
|
---|
| 110 | push(pop() / divisor);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | static void mod(void)
|
---|
| 114 | {
|
---|
[2725] | 115 | data_t d = pop();
|
---|
[821] | 116 |
|
---|
[2725] | 117 | push((data_t) pop() % d);
|
---|
[821] | 118 | }
|
---|
| 119 |
|
---|
| 120 | static void and(void)
|
---|
| 121 | {
|
---|
[2725] | 122 | push((data_t) pop() & (data_t) pop());
|
---|
[821] | 123 | }
|
---|
| 124 |
|
---|
| 125 | static void or(void)
|
---|
| 126 | {
|
---|
[2725] | 127 | push((data_t) pop() | (data_t) pop());
|
---|
[821] | 128 | }
|
---|
| 129 |
|
---|
| 130 | static void eor(void)
|
---|
| 131 | {
|
---|
[2725] | 132 | push((data_t) pop() ^ (data_t) pop());
|
---|
[821] | 133 | }
|
---|
| 134 |
|
---|
| 135 | static void not(void)
|
---|
| 136 | {
|
---|
[2725] | 137 | push(~(data_t) pop());
|
---|
[821] | 138 | }
|
---|
| 139 |
|
---|
| 140 | static void set_output_base(void)
|
---|
| 141 | {
|
---|
[2725] | 142 | static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 };
|
---|
| 143 | unsigned b = (unsigned)pop();
|
---|
| 144 |
|
---|
| 145 | base = *strchrnul(bases, b);
|
---|
| 146 | if (base == 0) {
|
---|
| 147 | bb_error_msg("error, base %u is not supported", b);
|
---|
[1765] | 148 | base = 10;
|
---|
[821] | 149 | }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | static void print_base(double print)
|
---|
| 153 | {
|
---|
[2725] | 154 | data_t x, i;
|
---|
| 155 |
|
---|
| 156 | x = (data_t) print;
|
---|
| 157 | if (base == 10) {
|
---|
| 158 | if (x == print) /* exactly representable as unsigned integer */
|
---|
| 159 | printf("%"DATA_FMT"u\n", x);
|
---|
| 160 | else
|
---|
| 161 | printf("%g\n", print);
|
---|
| 162 | return;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | switch (base) {
|
---|
| 166 | case 16:
|
---|
| 167 | printf("%"DATA_FMT"x\n", x);
|
---|
| 168 | break;
|
---|
| 169 | case 8:
|
---|
| 170 | printf("%"DATA_FMT"o\n", x);
|
---|
| 171 | break;
|
---|
| 172 | default: /* base 2 */
|
---|
| 173 | i = MAXINT(data_t) - (MAXINT(data_t) >> 1);
|
---|
| 174 | /* i is 100000...00000 */
|
---|
| 175 | do {
|
---|
| 176 | if (x & i)
|
---|
| 177 | break;
|
---|
| 178 | i >>= 1;
|
---|
| 179 | } while (i > 1);
|
---|
| 180 | do {
|
---|
| 181 | bb_putchar('1' - !(x & i));
|
---|
| 182 | i >>= 1;
|
---|
| 183 | } while (i);
|
---|
| 184 | bb_putchar('\n');
|
---|
| 185 | }
|
---|
[821] | 186 | }
|
---|
| 187 |
|
---|
| 188 | static void print_stack_no_pop(void)
|
---|
| 189 | {
|
---|
[2725] | 190 | unsigned i = pointer;
|
---|
[821] | 191 | while (i)
|
---|
| 192 | print_base(stack[--i]);
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | static void print_no_pop(void)
|
---|
| 196 | {
|
---|
[3621] | 197 | check_under();
|
---|
[821] | 198 | print_base(stack[pointer-1]);
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | struct op {
|
---|
[1765] | 202 | const char name[4];
|
---|
[821] | 203 | void (*function) (void);
|
---|
| 204 | };
|
---|
| 205 |
|
---|
| 206 | static const struct op operators[] = {
|
---|
[2725] | 207 | #if ENABLE_FEATURE_DC_LIBM
|
---|
[821] | 208 | {"**", power},
|
---|
| 209 | {"exp", power},
|
---|
| 210 | {"pow", power},
|
---|
[2725] | 211 | #endif
|
---|
[821] | 212 | {"%", mod},
|
---|
| 213 | {"mod", mod},
|
---|
| 214 | {"and", and},
|
---|
| 215 | {"or", or},
|
---|
| 216 | {"not", not},
|
---|
| 217 | {"eor", eor},
|
---|
| 218 | {"xor", eor},
|
---|
[3621] | 219 | {"+", add},
|
---|
| 220 | {"add", add},
|
---|
| 221 | {"-", sub},
|
---|
| 222 | {"sub", sub},
|
---|
| 223 | {"*", mul},
|
---|
| 224 | {"mul", mul},
|
---|
| 225 | {"/", divide},
|
---|
| 226 | {"div", divide},
|
---|
[821] | 227 | {"p", print_no_pop},
|
---|
| 228 | {"f", print_stack_no_pop},
|
---|
| 229 | {"o", set_output_base},
|
---|
| 230 | };
|
---|
| 231 |
|
---|
[3621] | 232 | /* Feed the stack machine */
|
---|
[821] | 233 | static void stack_machine(const char *argument)
|
---|
| 234 | {
|
---|
[3232] | 235 | char *end;
|
---|
[3621] | 236 | double number;
|
---|
[3232] | 237 | const struct op *o;
|
---|
[821] | 238 |
|
---|
[3621] | 239 | next:
|
---|
| 240 | number = strtod(argument, &end);
|
---|
| 241 | if (end != argument) {
|
---|
| 242 | argument = end;
|
---|
| 243 | push(number);
|
---|
| 244 | goto next;
|
---|
[821] | 245 | }
|
---|
| 246 |
|
---|
[3621] | 247 | /* We might have matched a digit, eventually advance the argument */
|
---|
| 248 | argument = skip_whitespace(argument);
|
---|
| 249 |
|
---|
| 250 | if (*argument == '\0')
|
---|
| 251 | return;
|
---|
| 252 |
|
---|
[3232] | 253 | o = operators;
|
---|
| 254 | do {
|
---|
[3621] | 255 | char *after_name = is_prefixed_with(argument, o->name);
|
---|
| 256 | if (after_name) {
|
---|
| 257 | argument = after_name;
|
---|
[1765] | 258 | o->function();
|
---|
[3621] | 259 | goto next;
|
---|
[821] | 260 | }
|
---|
| 261 | o++;
|
---|
[3232] | 262 | } while (o != operators + ARRAY_SIZE(operators));
|
---|
| 263 |
|
---|
[2725] | 264 | bb_error_msg_and_die("syntax error at '%s'", argument);
|
---|
[821] | 265 | }
|
---|
| 266 |
|
---|
[2725] | 267 | int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 268 | int dc_main(int argc UNUSED_PARAM, char **argv)
|
---|
[821] | 269 | {
|
---|
[2725] | 270 | INIT_G();
|
---|
[821] | 271 |
|
---|
[2725] | 272 | argv++;
|
---|
| 273 | if (!argv[0]) {
|
---|
| 274 | /* take stuff from stdin if no args are given */
|
---|
| 275 | char *line;
|
---|
| 276 | while ((line = xmalloc_fgetline(stdin)) != NULL) {
|
---|
[3621] | 277 | stack_machine(line);
|
---|
[821] | 278 | free(line);
|
---|
| 279 | }
|
---|
| 280 | } else {
|
---|
[2725] | 281 | do {
|
---|
| 282 | stack_machine(*argv);
|
---|
| 283 | } while (*++argv);
|
---|
[821] | 284 | }
|
---|
| 285 | return EXIT_SUCCESS;
|
---|
| 286 | }
|
---|