Changeset 3621 in MondoRescue for branches/3.3/mindi-busybox/miscutils/dc.c
- Timestamp:
- Dec 20, 2016, 4:07:32 PM (7 years ago)
- Location:
- branches/3.3
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/3.3/mindi-busybox/miscutils/dc.c
r3232 r3621 5 5 6 6 #include "libbb.h" 7 #include "common_bufsiz.h" 7 8 #include <math.h> 8 9 … … 48 49 } FIX_ALIASING; 49 50 enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) }; 50 #define G (*(struct globals*) &bb_common_bufsiz1)51 #define G (*(struct globals*)bb_common_bufsiz1) 51 52 #define pointer (G.pointer ) 52 53 #define base (G.base ) 53 54 #define stack (G.stack ) 54 55 #define INIT_G() do { \ 56 setup_common_bufsiz(); \ 55 57 base = 10; \ 56 58 } while (0) 57 59 60 61 static void check_under(void) 62 { 63 if (pointer == 0) 64 bb_error_msg_and_die("stack underflow"); 65 } 58 66 59 67 static void push(double a) … … 66 74 static double pop(void) 67 75 { 68 if (pointer == 0) 69 bb_error_msg_and_die("stack underflow"); 76 check_under(); 70 77 return stack[--pointer]; 71 78 } … … 188 195 static void print_no_pop(void) 189 196 { 197 check_under(); 190 198 print_base(stack[pointer-1]); 191 199 } … … 197 205 198 206 static const struct op operators[] = { 199 {"+", add},200 {"add", add},201 {"-", sub},202 {"sub", sub},203 {"*", mul},204 {"mul", mul},205 {"/", divide},206 {"div", divide},207 207 #if ENABLE_FEATURE_DC_LIBM 208 208 {"**", power}, … … 217 217 {"eor", eor}, 218 218 {"xor", eor}, 219 {"+", add}, 220 {"add", add}, 221 {"-", sub}, 222 {"sub", sub}, 223 {"*", mul}, 224 {"mul", mul}, 225 {"/", divide}, 226 {"div", divide}, 219 227 {"p", print_no_pop}, 220 228 {"f", print_stack_no_pop}, … … 222 230 }; 223 231 232 /* Feed the stack machine */ 224 233 static void stack_machine(const char *argument) 225 234 { 226 235 char *end; 227 double d;236 double number; 228 237 const struct op *o; 229 238 230 d = strtod(argument, &end); 231 if (end != argument && *end == '\0') { 232 push(d); 239 next: 240 number = strtod(argument, &end); 241 if (end != argument) { 242 argument = end; 243 push(number); 244 goto next; 245 } 246 247 /* We might have matched a digit, eventually advance the argument */ 248 argument = skip_whitespace(argument); 249 250 if (*argument == '\0') 233 251 return; 234 }235 252 236 253 o = operators; 237 254 do { 238 if (strcmp(o->name, argument) == 0) { 255 char *after_name = is_prefixed_with(argument, o->name); 256 if (after_name) { 257 argument = after_name; 239 258 o->function(); 240 return;259 goto next; 241 260 } 242 261 o++; … … 255 274 /* take stuff from stdin if no args are given */ 256 275 char *line; 257 char *cursor;258 char *token;259 276 while ((line = xmalloc_fgetline(stdin)) != NULL) { 260 cursor = line; 261 while (1) { 262 token = skip_whitespace(cursor); 263 if (*token == '\0') 264 break; 265 cursor = skip_non_whitespace(token); 266 if (*cursor != '\0') 267 *cursor++ = '\0'; 268 stack_machine(token); 269 } 277 stack_machine(line); 270 278 free(line); 271 279 } 272 280 } else { 273 // why? it breaks "dc -2 2 + p"274 //if (argv[0][0] == '-')275 // bb_show_usage();276 281 do { 277 282 stack_machine(*argv);
Note:
See TracChangeset
for help on using the changeset viewer.