Changeset 3621 in MondoRescue for branches/3.3/mindi-busybox/miscutils/dc.c


Ignore:
Timestamp:
Dec 20, 2016, 4:07:32 PM (7 years ago)
Author:
Bruno Cornec
Message:

New 3?3 banch for incorporation of latest busybox 1.25. Changing minor version to handle potential incompatibilities.

Location:
branches/3.3
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/3.3/mindi-busybox/miscutils/dc.c

    r3232 r3621  
    55
    66#include "libbb.h"
     7#include "common_bufsiz.h"
    78#include <math.h>
    89
     
    4849} FIX_ALIASING;
    4950enum { 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)
    5152#define pointer   (G.pointer   )
    5253#define base      (G.base      )
    5354#define stack     (G.stack     )
    5455#define INIT_G() do { \
     56    setup_common_bufsiz(); \
    5557    base = 10; \
    5658} while (0)
    5759
     60
     61static void check_under(void)
     62{
     63    if (pointer == 0)
     64        bb_error_msg_and_die("stack underflow");
     65}
    5866
    5967static void push(double a)
     
    6674static double pop(void)
    6775{
    68     if (pointer == 0)
    69         bb_error_msg_and_die("stack underflow");
     76    check_under();
    7077    return stack[--pointer];
    7178}
     
    188195static void print_no_pop(void)
    189196{
     197    check_under();
    190198    print_base(stack[pointer-1]);
    191199}
     
    197205
    198206static const struct op operators[] = {
    199     {"+",   add},
    200     {"add", add},
    201     {"-",   sub},
    202     {"sub", sub},
    203     {"*",   mul},
    204     {"mul", mul},
    205     {"/",   divide},
    206     {"div", divide},
    207207#if ENABLE_FEATURE_DC_LIBM
    208208    {"**",  power},
     
    217217    {"eor", eor},
    218218    {"xor", eor},
     219    {"+",   add},
     220    {"add", add},
     221    {"-",   sub},
     222    {"sub", sub},
     223    {"*",   mul},
     224    {"mul", mul},
     225    {"/",   divide},
     226    {"div", divide},
    219227    {"p", print_no_pop},
    220228    {"f", print_stack_no_pop},
     
    222230};
    223231
     232/* Feed the stack machine */
    224233static void stack_machine(const char *argument)
    225234{
    226235    char *end;
    227     double d;
     236    double number;
    228237    const struct op *o;
    229238
    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')
    233251        return;
    234     }
    235252
    236253    o = operators;
    237254    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;
    239258            o->function();
    240             return;
     259            goto next;
    241260        }
    242261        o++;
     
    255274        /* take stuff from stdin if no args are given */
    256275        char *line;
    257         char *cursor;
    258         char *token;
    259276        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);
    270278            free(line);
    271279        }
    272280    } else {
    273         // why? it breaks "dc -2 2 + p"
    274         //if (argv[0][0] == '-')
    275         //  bb_show_usage();
    276281        do {
    277282            stack_machine(*argv);
Note: See TracChangeset for help on using the changeset viewer.