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 | /* |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 3 | * 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] | 4 | */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 5 | |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 6 | /* config/applet/usage bits are in bc.c */ |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame] | 7 | |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 8 | //#include "libbb.h" |
| 9 | //#include "common_bufsiz.h" |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 10 | #include <math.h> |
| 11 | |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 12 | #if 0 |
| 13 | typedef unsigned data_t; |
| 14 | #define DATA_FMT "" |
| 15 | #elif 0 |
| 16 | typedef unsigned long data_t; |
| 17 | #define DATA_FMT "l" |
| 18 | #else |
| 19 | typedef unsigned long long data_t; |
| 20 | #define DATA_FMT "ll" |
| 21 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 22 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 23 | struct globals { |
| 24 | unsigned pointer; |
| 25 | unsigned base; |
| 26 | double stack[1]; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 27 | } FIX_ALIASING; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 28 | enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) }; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 29 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 30 | #define pointer (G.pointer ) |
| 31 | #define base (G.base ) |
| 32 | #define stack (G.stack ) |
Denis Vlasenko | d8850f2 | 2008-12-30 10:40:05 +0000 | [diff] [blame] | 33 | #define INIT_G() do { \ |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 34 | setup_common_bufsiz(); \ |
Denis Vlasenko | d8850f2 | 2008-12-30 10:40:05 +0000 | [diff] [blame] | 35 | base = 10; \ |
| 36 | } while (0) |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 37 | |
Denys Vlasenko | 89023b1 | 2019-05-09 15:58:46 +0200 | [diff] [blame] | 38 | static unsigned check_under(void) |
Denys Vlasenko | c4603fb | 2015-05-25 13:31:25 +0200 | [diff] [blame] | 39 | { |
Denys Vlasenko | 89023b1 | 2019-05-09 15:58:46 +0200 | [diff] [blame] | 40 | unsigned p = pointer; |
| 41 | if (p == 0) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 42 | bb_simple_error_msg_and_die("stack underflow"); |
Denys Vlasenko | 89023b1 | 2019-05-09 15:58:46 +0200 | [diff] [blame] | 43 | return p - 1; |
Denys Vlasenko | c4603fb | 2015-05-25 13:31:25 +0200 | [diff] [blame] | 44 | } |
| 45 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 46 | static void push(double a) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 47 | { |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 48 | if (pointer >= STACK_SIZE) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 49 | bb_simple_error_msg_and_die("stack overflow"); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 50 | stack[pointer++] = a; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 53 | static double pop(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 54 | { |
Denys Vlasenko | 89023b1 | 2019-05-09 15:58:46 +0200 | [diff] [blame] | 55 | unsigned p = check_under(); |
| 56 | pointer = p; |
| 57 | return stack[p]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 60 | static void add(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 61 | { |
| 62 | push(pop() + pop()); |
| 63 | } |
| 64 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 65 | static void sub(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 66 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 67 | double subtrahend = pop(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 68 | |
| 69 | push(pop() - subtrahend); |
| 70 | } |
| 71 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 72 | static void mul(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 73 | { |
| 74 | push(pop() * pop()); |
| 75 | } |
| 76 | |
Denis Vlasenko | 0783230 | 2008-10-20 08:43:10 +0000 | [diff] [blame] | 77 | #if ENABLE_FEATURE_DC_LIBM |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 78 | static void power(void) |
| 79 | { |
| 80 | double topower = pop(); |
| 81 | |
| 82 | push(pow(pop(), topower)); |
| 83 | } |
Denis Vlasenko | 0783230 | 2008-10-20 08:43:10 +0000 | [diff] [blame] | 84 | #endif |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 85 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 86 | static void divide(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 87 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 88 | double divisor = pop(); |
| 89 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 90 | push(pop() / divisor); |
| 91 | } |
| 92 | |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 93 | static void mod(void) |
| 94 | { |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 95 | data_t d = pop(); |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 96 | |
Denys Vlasenko | 1113961 | 2019-05-16 09:40:36 +0200 | [diff] [blame] | 97 | /* compat with dc (GNU bc 1.07.1) 1.4.1: |
| 98 | * $ dc -e '4 0 % p' |
| 99 | * dc: remainder by zero |
| 100 | * 0 |
| 101 | */ |
| 102 | if (d == 0) { |
Denys Vlasenko | 77a51a2 | 2020-12-29 16:53:11 +0100 | [diff] [blame] | 103 | bb_simple_error_msg("remainder by zero"); |
Denys Vlasenko | 1113961 | 2019-05-16 09:40:36 +0200 | [diff] [blame] | 104 | pop(); |
| 105 | push(0); |
| 106 | return; |
| 107 | } |
| 108 | /* ^^^^ without this, we simply get SIGFPE and die */ |
Denys Vlasenko | 89023b1 | 2019-05-09 15:58:46 +0200 | [diff] [blame] | 109 | |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 110 | push((data_t) pop() % d); |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 113 | static void and(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 114 | { |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 115 | push((data_t) pop() & (data_t) pop()); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 118 | static void or(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 119 | { |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 120 | push((data_t) pop() | (data_t) pop()); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 123 | static void eor(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 124 | { |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 125 | push((data_t) pop() ^ (data_t) pop()); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 128 | static void not(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 129 | { |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 130 | push(~(data_t) pop()); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 133 | static void set_output_base(void) |
| 134 | { |
Denis Vlasenko | 6b38e18 | 2008-10-30 23:25:50 +0000 | [diff] [blame] | 135 | static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 }; |
| 136 | unsigned b = (unsigned)pop(); |
| 137 | |
| 138 | base = *strchrnul(bases, b); |
| 139 | if (base == 0) { |
| 140 | bb_error_msg("error, base %u is not supported", b); |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 141 | base = 10; |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | static void print_base(double print) |
| 146 | { |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 147 | data_t x, i; |
Denis Vlasenko | 6b38e18 | 2008-10-30 23:25:50 +0000 | [diff] [blame] | 148 | |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 149 | x = (data_t) print; |
Denis Vlasenko | 6b38e18 | 2008-10-30 23:25:50 +0000 | [diff] [blame] | 150 | if (base == 10) { |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 151 | if (x == print) /* exactly representable as unsigned integer */ |
| 152 | printf("%"DATA_FMT"u\n", x); |
| 153 | else |
| 154 | printf("%g\n", print); |
Denis Vlasenko | 6b38e18 | 2008-10-30 23:25:50 +0000 | [diff] [blame] | 155 | return; |
| 156 | } |
| 157 | |
Denis Vlasenko | 6b38e18 | 2008-10-30 23:25:50 +0000 | [diff] [blame] | 158 | switch (base) { |
| 159 | case 16: |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 160 | printf("%"DATA_FMT"x\n", x); |
Denis Vlasenko | 6b38e18 | 2008-10-30 23:25:50 +0000 | [diff] [blame] | 161 | break; |
| 162 | case 8: |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 163 | printf("%"DATA_FMT"o\n", x); |
Denis Vlasenko | 6b38e18 | 2008-10-30 23:25:50 +0000 | [diff] [blame] | 164 | break; |
| 165 | default: /* base 2 */ |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 166 | i = MAXINT(data_t) - (MAXINT(data_t) >> 1); |
| 167 | /* i is 100000...00000 */ |
Denis Vlasenko | 6b38e18 | 2008-10-30 23:25:50 +0000 | [diff] [blame] | 168 | do { |
Denys Vlasenko | 7a07b0e | 2010-07-29 04:00:27 +0200 | [diff] [blame] | 169 | if (x & i) |
| 170 | break; |
Denis Vlasenko | 6b38e18 | 2008-10-30 23:25:50 +0000 | [diff] [blame] | 171 | i >>= 1; |
| 172 | } while (i > 1); |
| 173 | do { |
| 174 | bb_putchar('1' - !(x & i)); |
| 175 | i >>= 1; |
| 176 | } while (i); |
| 177 | bb_putchar('\n'); |
| 178 | } |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static void print_stack_no_pop(void) |
| 182 | { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 183 | unsigned i = pointer; |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 184 | while (i) |
| 185 | print_base(stack[--i]); |
| 186 | } |
| 187 | |
| 188 | static void print_no_pop(void) |
| 189 | { |
Denys Vlasenko | 89023b1 | 2019-05-09 15:58:46 +0200 | [diff] [blame] | 190 | print_base(stack[check_under()]); |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 193 | struct op { |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 194 | const char name[4]; |
Aaron Lehmann | 2dd2d7a | 2001-12-06 03:29:37 +0000 | [diff] [blame] | 195 | void (*function) (void); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 198 | static const struct op operators[] ALIGN_PTR = { |
Denis Vlasenko | 0783230 | 2008-10-20 08:43:10 +0000 | [diff] [blame] | 199 | #if ENABLE_FEATURE_DC_LIBM |
Denys Vlasenko | d0bc5fd | 2018-12-08 18:59:07 +0100 | [diff] [blame] | 200 | {"^", power}, |
| 201 | // {"exp", power}, |
| 202 | // {"pow", power}, |
Denis Vlasenko | 0783230 | 2008-10-20 08:43:10 +0000 | [diff] [blame] | 203 | #endif |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 204 | {"%", mod}, |
Denys Vlasenko | d0bc5fd | 2018-12-08 18:59:07 +0100 | [diff] [blame] | 205 | // {"mod", mod}, |
| 206 | // logic ops are not standard, remove? |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 207 | {"and", and}, |
John Beppu | c035254 | 2000-06-21 18:00:46 +0000 | [diff] [blame] | 208 | {"or", or}, |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 209 | {"not", not}, |
Eric Andersen | a928774 | 2003-10-22 11:24:39 +0000 | [diff] [blame] | 210 | {"xor", eor}, |
Bernhard Reutner-Fischer | 70e30e8 | 2015-02-16 17:12:04 +0100 | [diff] [blame] | 211 | {"+", add}, |
Denys Vlasenko | d0bc5fd | 2018-12-08 18:59:07 +0100 | [diff] [blame] | 212 | // {"add", add}, |
Bernhard Reutner-Fischer | 70e30e8 | 2015-02-16 17:12:04 +0100 | [diff] [blame] | 213 | {"-", sub}, |
Denys Vlasenko | d0bc5fd | 2018-12-08 18:59:07 +0100 | [diff] [blame] | 214 | // {"sub", sub}, |
Bernhard Reutner-Fischer | 70e30e8 | 2015-02-16 17:12:04 +0100 | [diff] [blame] | 215 | {"*", mul}, |
Denys Vlasenko | d0bc5fd | 2018-12-08 18:59:07 +0100 | [diff] [blame] | 216 | // {"mul", mul}, |
Bernhard Reutner-Fischer | 70e30e8 | 2015-02-16 17:12:04 +0100 | [diff] [blame] | 217 | {"/", divide}, |
Denys Vlasenko | d0bc5fd | 2018-12-08 18:59:07 +0100 | [diff] [blame] | 218 | // {"div", divide}, |
Glenn L McGrath | 6d07432 | 2002-12-12 10:31:53 +0000 | [diff] [blame] | 219 | {"p", print_no_pop}, |
| 220 | {"f", print_stack_no_pop}, |
| 221 | {"o", set_output_base}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 222 | }; |
| 223 | |
Bernhard Reutner-Fischer | 70e30e8 | 2015-02-16 17:12:04 +0100 | [diff] [blame] | 224 | /* Feed the stack machine */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 225 | static void stack_machine(const char *argument) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 226 | { |
Denys Vlasenko | 9daf33f | 2013-01-18 13:30:13 +0100 | [diff] [blame] | 227 | char *end; |
Bernhard Reutner-Fischer | 70e30e8 | 2015-02-16 17:12:04 +0100 | [diff] [blame] | 228 | double number; |
Denys Vlasenko | 9daf33f | 2013-01-18 13:30:13 +0100 | [diff] [blame] | 229 | const struct op *o; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 230 | |
Bernhard Reutner-Fischer | 70e30e8 | 2015-02-16 17:12:04 +0100 | [diff] [blame] | 231 | next: |
Denys Vlasenko | c2bd0b6 | 2021-03-23 13:50:02 +0100 | [diff] [blame] | 232 | //TODO: needs setlocale(LC_NUMERIC, "C")? |
Bernhard Reutner-Fischer | 70e30e8 | 2015-02-16 17:12:04 +0100 | [diff] [blame] | 233 | number = strtod(argument, &end); |
| 234 | if (end != argument) { |
| 235 | argument = end; |
| 236 | push(number); |
| 237 | goto next; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Bernhard Reutner-Fischer | 70e30e8 | 2015-02-16 17:12:04 +0100 | [diff] [blame] | 240 | /* We might have matched a digit, eventually advance the argument */ |
| 241 | argument = skip_whitespace(argument); |
| 242 | |
| 243 | if (*argument == '\0') |
| 244 | return; |
| 245 | |
Denys Vlasenko | 9daf33f | 2013-01-18 13:30:13 +0100 | [diff] [blame] | 246 | o = operators; |
| 247 | do { |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 248 | char *after_name = is_prefixed_with(argument, o->name); |
| 249 | if (after_name) { |
| 250 | argument = after_name; |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 251 | o->function(); |
Bernhard Reutner-Fischer | 70e30e8 | 2015-02-16 17:12:04 +0100 | [diff] [blame] | 252 | goto next; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 253 | } |
| 254 | o++; |
Denys Vlasenko | 9daf33f | 2013-01-18 13:30:13 +0100 | [diff] [blame] | 255 | } while (o != operators + ARRAY_SIZE(operators)); |
| 256 | |
Denys Vlasenko | bd1de18 | 2009-12-30 18:37:08 +0100 | [diff] [blame] | 257 | bb_error_msg_and_die("syntax error at '%s'", argument); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Denys Vlasenko | d0bc5fd | 2018-12-08 18:59:07 +0100 | [diff] [blame] | 260 | static void process_file(FILE *fp) |
| 261 | { |
| 262 | char *line; |
| 263 | while ((line = xmalloc_fgetline(fp)) != NULL) { |
| 264 | stack_machine(line); |
| 265 | free(line); |
| 266 | } |
| 267 | } |
| 268 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 269 | int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 270 | int dc_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 271 | { |
Denys Vlasenko | d0bc5fd | 2018-12-08 18:59:07 +0100 | [diff] [blame] | 272 | bool script = 0; |
| 273 | |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 274 | INIT_G(); |
| 275 | |
Denys Vlasenko | d0bc5fd | 2018-12-08 18:59:07 +0100 | [diff] [blame] | 276 | /* Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs */ |
| 277 | for (;;) { |
| 278 | int n = getopt(argc, argv, "e:f:"); |
| 279 | if (n <= 0) |
| 280 | break; |
| 281 | switch (n) { |
| 282 | case 'e': |
| 283 | script = 1; |
| 284 | stack_machine(optarg); |
| 285 | break; |
| 286 | case 'f': |
| 287 | script = 1; |
| 288 | process_file(xfopen_for_read(optarg)); |
| 289 | break; |
| 290 | default: |
| 291 | bb_show_usage(); |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 292 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 293 | } |
Denys Vlasenko | d0bc5fd | 2018-12-08 18:59:07 +0100 | [diff] [blame] | 294 | argv += optind; |
| 295 | |
| 296 | if (*argv) { |
| 297 | do |
| 298 | process_file(xfopen_for_read(*argv++)); |
| 299 | while (*argv); |
| 300 | } else if (!script) { |
| 301 | /* Take stuff from stdin if no args are given */ |
| 302 | process_file(stdin); |
| 303 | } |
| 304 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 305 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 306 | } |