Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1 | #if 0 //TODO: use if bc is not selected |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2 | /* vi: set sw=4 ts=4: */ |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 3 | /* |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 4 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 5 | */ |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6 | //config:config DC |
| 7 | //config: bool "dc (4.2 kb)" |
| 8 | //config: default y |
| 9 | //config: help |
| 10 | //config: Dc is a reverse-polish desk calculator which supports unlimited |
| 11 | //config: precision arithmetic. |
| 12 | //config: |
| 13 | //config:config FEATURE_DC_LIBM |
| 14 | //config: bool "Enable power and exp functions (requires libm)" |
| 15 | //config: default y |
| 16 | //config: depends on DC |
| 17 | //config: help |
| 18 | //config: Enable power and exp functions. |
| 19 | //config: NOTE: This will require libm to be present for linking. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 20 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 21 | //applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP)) |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame] | 22 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 23 | //kbuild:lib-$(CONFIG_DC) += dc.o |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame] | 24 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 25 | //usage:#define dc_trivial_usage |
| 26 | //usage: "EXPRESSION..." |
| 27 | //usage: |
| 28 | //usage:#define dc_full_usage "\n\n" |
| 29 | //usage: "Tiny RPN calculator. Operations:\n" |
| 30 | //usage: "+, add, -, sub, *, mul, /, div, %, mod, "IF_FEATURE_DC_LIBM("**, exp, ")"and, or, not, xor,\n" |
| 31 | //usage: "p - print top of the stack (without popping),\n" |
| 32 | //usage: "f - print entire stack,\n" |
| 33 | //usage: "o - pop the value and set output radix (must be 10, 16, 8 or 2).\n" |
| 34 | //usage: "Examples: 'dc 2 2 add p' -> 4, 'dc 8 8 mul 2 2 + / p' -> 16" |
| 35 | //usage: |
| 36 | //usage:#define dc_example_usage |
| 37 | //usage: "$ dc 2 2 + p\n" |
| 38 | //usage: "4\n" |
| 39 | //usage: "$ dc 8 8 \\* 2 2 + / p\n" |
| 40 | //usage: "16\n" |
| 41 | //usage: "$ dc 0 1 and p\n" |
| 42 | //usage: "0\n" |
| 43 | //usage: "$ dc 0 1 or p\n" |
| 44 | //usage: "1\n" |
| 45 | //usage: "$ echo 72 9 div 8 mul p | dc\n" |
| 46 | //usage: "64\n" |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 47 | |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 48 | #include "libbb.h" |
| 49 | #include "common_bufsiz.h" |
| 50 | #include <math.h> |
| 51 | |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 52 | #if 0 |
| 53 | typedef unsigned data_t; |
| 54 | #define DATA_FMT "" |
| 55 | #elif 0 |
| 56 | typedef unsigned long data_t; |
| 57 | #define DATA_FMT "l" |
| 58 | #else |
| 59 | typedef unsigned long long data_t; |
| 60 | #define DATA_FMT "ll" |
| 61 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 62 | |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 63 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 64 | struct globals { |
| 65 | unsigned pointer; |
| 66 | unsigned base; |
| 67 | double stack[1]; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 68 | } FIX_ALIASING; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 69 | enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) }; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 70 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 71 | #define pointer (G.pointer ) |
| 72 | #define base (G.base ) |
| 73 | #define stack (G.stack ) |
Denis Vlasenko | d8850f2 | 2008-12-30 10:40:05 +0000 | [diff] [blame] | 74 | #define INIT_G() do { \ |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 75 | setup_common_bufsiz(); \ |
Denis Vlasenko | d8850f2 | 2008-12-30 10:40:05 +0000 | [diff] [blame] | 76 | base = 10; \ |
| 77 | } while (0) |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 78 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 79 | |
Denys Vlasenko | c4603fb | 2015-05-25 13:31:25 +0200 | [diff] [blame] | 80 | static void check_under(void) |
| 81 | { |
| 82 | if (pointer == 0) |
| 83 | bb_error_msg_and_die("stack underflow"); |
| 84 | } |
| 85 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 86 | static void push(double a) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 87 | { |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 88 | if (pointer >= STACK_SIZE) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 89 | bb_error_msg_and_die("stack overflow"); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 90 | stack[pointer++] = a; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 93 | static double pop(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 94 | { |
Denys Vlasenko | c4603fb | 2015-05-25 13:31:25 +0200 | [diff] [blame] | 95 | check_under(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 96 | return stack[--pointer]; |
| 97 | } |
| 98 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 99 | static void add(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 100 | { |
| 101 | push(pop() + pop()); |
| 102 | } |
| 103 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 104 | static void sub(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 105 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 106 | double subtrahend = pop(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 107 | |
| 108 | push(pop() - subtrahend); |
| 109 | } |
| 110 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 111 | static void mul(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 112 | { |
| 113 | push(pop() * pop()); |
| 114 | } |
| 115 | |
Denis Vlasenko | 0783230 | 2008-10-20 08:43:10 +0000 | [diff] [blame] | 116 | #if ENABLE_FEATURE_DC_LIBM |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 117 | static void power(void) |
| 118 | { |
| 119 | double topower = pop(); |
| 120 | |
| 121 | push(pow(pop(), topower)); |
| 122 | } |
Denis Vlasenko | 0783230 | 2008-10-20 08:43:10 +0000 | [diff] [blame] | 123 | #endif |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 124 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 125 | static void divide(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 126 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 127 | double divisor = pop(); |
| 128 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 129 | push(pop() / divisor); |
| 130 | } |
| 131 | |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 132 | static void mod(void) |
| 133 | { |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 134 | data_t d = pop(); |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 135 | |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 136 | push((data_t) pop() % d); |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 139 | static void and(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 140 | { |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 141 | push((data_t) pop() & (data_t) pop()); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 144 | static void or(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 145 | { |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 146 | push((data_t) pop() | (data_t) pop()); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 149 | static void eor(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 150 | { |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 151 | push((data_t) pop() ^ (data_t) pop()); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 154 | static void not(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 155 | { |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 156 | push(~(data_t) pop()); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 159 | static void set_output_base(void) |
| 160 | { |
Denis Vlasenko | 6b38e18 | 2008-10-30 23:25:50 +0000 | [diff] [blame] | 161 | static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 }; |
| 162 | unsigned b = (unsigned)pop(); |
| 163 | |
| 164 | base = *strchrnul(bases, b); |
| 165 | if (base == 0) { |
| 166 | bb_error_msg("error, base %u is not supported", b); |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 167 | base = 10; |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | static void print_base(double print) |
| 172 | { |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 173 | data_t x, i; |
Denis Vlasenko | 6b38e18 | 2008-10-30 23:25:50 +0000 | [diff] [blame] | 174 | |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 175 | x = (data_t) print; |
Denis Vlasenko | 6b38e18 | 2008-10-30 23:25:50 +0000 | [diff] [blame] | 176 | if (base == 10) { |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 177 | if (x == print) /* exactly representable as unsigned integer */ |
| 178 | printf("%"DATA_FMT"u\n", x); |
| 179 | else |
| 180 | printf("%g\n", print); |
Denis Vlasenko | 6b38e18 | 2008-10-30 23:25:50 +0000 | [diff] [blame] | 181 | return; |
| 182 | } |
| 183 | |
Denis Vlasenko | 6b38e18 | 2008-10-30 23:25:50 +0000 | [diff] [blame] | 184 | switch (base) { |
| 185 | case 16: |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 186 | printf("%"DATA_FMT"x\n", x); |
Denis Vlasenko | 6b38e18 | 2008-10-30 23:25:50 +0000 | [diff] [blame] | 187 | break; |
| 188 | case 8: |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 189 | printf("%"DATA_FMT"o\n", x); |
Denis Vlasenko | 6b38e18 | 2008-10-30 23:25:50 +0000 | [diff] [blame] | 190 | break; |
| 191 | default: /* base 2 */ |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 192 | i = MAXINT(data_t) - (MAXINT(data_t) >> 1); |
| 193 | /* i is 100000...00000 */ |
Denis Vlasenko | 6b38e18 | 2008-10-30 23:25:50 +0000 | [diff] [blame] | 194 | do { |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 195 | if (x & i) |
| 196 | break; |
Denis Vlasenko | 6b38e18 | 2008-10-30 23:25:50 +0000 | [diff] [blame] | 197 | i >>= 1; |
| 198 | } while (i > 1); |
| 199 | do { |
| 200 | bb_putchar('1' - !(x & i)); |
| 201 | i >>= 1; |
| 202 | } while (i); |
| 203 | bb_putchar('\n'); |
| 204 | } |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static void print_stack_no_pop(void) |
| 208 | { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 209 | unsigned i = pointer; |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 210 | while (i) |
| 211 | print_base(stack[--i]); |
| 212 | } |
| 213 | |
| 214 | static void print_no_pop(void) |
| 215 | { |
Denys Vlasenko | c4603fb | 2015-05-25 13:31:25 +0200 | [diff] [blame] | 216 | check_under(); |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 217 | print_base(stack[pointer-1]); |
| 218 | } |
| 219 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 220 | struct op { |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 221 | const char name[4]; |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 222 | void (*function) (void); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 223 | }; |
| 224 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 225 | static const struct op operators[] = { |
Denis Vlasenko | 0783230 | 2008-10-20 08:43:10 +0000 | [diff] [blame] | 226 | #if ENABLE_FEATURE_DC_LIBM |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 227 | {"**", power}, |
| 228 | {"exp", power}, |
| 229 | {"pow", power}, |
Denis Vlasenko | 0783230 | 2008-10-20 08:43:10 +0000 | [diff] [blame] | 230 | #endif |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 231 | {"%", mod}, |
| 232 | {"mod", mod}, |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 233 | {"and", and}, |
John Beppu | c035254 | 2000-06-21 18:00:46 +0000 | [diff] [blame] | 234 | {"or", or}, |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 235 | {"not", not}, |
| 236 | {"eor", eor}, |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 237 | {"xor", eor}, |
Bernhard Reutner-Fischer | 70e30e8 | 2015-02-16 17:12:04 +0100 | [diff] [blame] | 238 | {"+", add}, |
| 239 | {"add", add}, |
| 240 | {"-", sub}, |
| 241 | {"sub", sub}, |
| 242 | {"*", mul}, |
| 243 | {"mul", mul}, |
| 244 | {"/", divide}, |
| 245 | {"div", divide}, |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 246 | {"p", print_no_pop}, |
| 247 | {"f", print_stack_no_pop}, |
| 248 | {"o", set_output_base}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 249 | }; |
| 250 | |
Bernhard Reutner-Fischer | 70e30e8 | 2015-02-16 17:12:04 +0100 | [diff] [blame] | 251 | /* Feed the stack machine */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 252 | static void stack_machine(const char *argument) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 253 | { |
Denys Vlasenko | 9daf33f | 2013-01-18 13:30:13 +0100 | [diff] [blame] | 254 | char *end; |
Bernhard Reutner-Fischer | 70e30e8 | 2015-02-16 17:12:04 +0100 | [diff] [blame] | 255 | double number; |
Denys Vlasenko | 9daf33f | 2013-01-18 13:30:13 +0100 | [diff] [blame] | 256 | const struct op *o; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 257 | |
Bernhard Reutner-Fischer | 70e30e8 | 2015-02-16 17:12:04 +0100 | [diff] [blame] | 258 | next: |
| 259 | number = strtod(argument, &end); |
| 260 | if (end != argument) { |
| 261 | argument = end; |
| 262 | push(number); |
| 263 | goto next; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Bernhard Reutner-Fischer | 70e30e8 | 2015-02-16 17:12:04 +0100 | [diff] [blame] | 266 | /* We might have matched a digit, eventually advance the argument */ |
| 267 | argument = skip_whitespace(argument); |
| 268 | |
| 269 | if (*argument == '\0') |
| 270 | return; |
| 271 | |
Denys Vlasenko | 9daf33f | 2013-01-18 13:30:13 +0100 | [diff] [blame] | 272 | o = operators; |
| 273 | do { |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 274 | char *after_name = is_prefixed_with(argument, o->name); |
| 275 | if (after_name) { |
| 276 | argument = after_name; |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 277 | o->function(); |
Bernhard Reutner-Fischer | 70e30e8 | 2015-02-16 17:12:04 +0100 | [diff] [blame] | 278 | goto next; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 279 | } |
| 280 | o++; |
Denys Vlasenko | 9daf33f | 2013-01-18 13:30:13 +0100 | [diff] [blame] | 281 | } while (o != operators + ARRAY_SIZE(operators)); |
| 282 | |
Denys Vlasenko | bd1de18 | 2009-12-30 18:37:08 +0100 | [diff] [blame] | 283 | bb_error_msg_and_die("syntax error at '%s'", argument); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 286 | int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 287 | int dc_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 288 | { |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 289 | INIT_G(); |
| 290 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 291 | argv++; |
| 292 | if (!argv[0]) { |
| 293 | /* take stuff from stdin if no args are given */ |
| 294 | char *line; |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 295 | while ((line = xmalloc_fgetline(stdin)) != NULL) { |
Bernhard Reutner-Fischer | 70e30e8 | 2015-02-16 17:12:04 +0100 | [diff] [blame] | 296 | stack_machine(line); |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 297 | free(line); |
| 298 | } |
| 299 | } else { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 300 | do { |
| 301 | stack_machine(*argv); |
| 302 | } while (*++argv); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 303 | } |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 304 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 305 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 306 | #endif |