Changeset 3232 in MondoRescue for branches/3.2/mindi-busybox/miscutils/dc.c


Ignore:
Timestamp:
Jan 1, 2014, 12:47:38 AM (10 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.21.1
File:
1 edited

Legend:

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

    r2725 r3232  
    1212//usage:#define dc_full_usage "\n\n"
    1313//usage:       "Tiny RPN calculator. Operations:\n"
    14 //usage:       "+, add, -, sub, *, mul, /, div, %, mod, **, exp, and, or, not, eor,\n"
     14//usage:       "+, add, -, sub, *, mul, /, div, %, mod, "IF_FEATURE_DC_LIBM("**, exp, ")"and, or, not, xor,\n"
    1515//usage:       "p - print top of the stack (without popping),\n"
    1616//usage:       "f - print entire stack,\n"
    1717//usage:       "o - pop the value and set output radix (must be 10, 16, 8 or 2).\n"
    18 //usage:       "Examples: 'dc 2 2 add' -> 4, 'dc 8 8 * 2 2 + /' -> 16"
     18//usage:       "Examples: 'dc 2 2 add p' -> 4, 'dc 8 8 mul 2 2 + / p' -> 16"
    1919//usage:
    2020//usage:#define dc_example_usage
     
    220220    {"f", print_stack_no_pop},
    221221    {"o", set_output_base},
    222     { "", NULL }
    223222};
    224223
    225224static void stack_machine(const char *argument)
    226225{
    227     char *endPointer;
     226    char *end;
    228227    double d;
    229     const struct op *o = operators;
    230 
    231     d = strtod(argument, &endPointer);
    232 
    233     if (endPointer != argument && *endPointer == '\0') {
     228    const struct op *o;
     229
     230    d = strtod(argument, &end);
     231    if (end != argument && *end == '\0') {
    234232        push(d);
    235233        return;
    236234    }
    237235
    238     while (o->function) {
     236    o = operators;
     237    do {
    239238        if (strcmp(o->name, argument) == 0) {
    240239            o->function();
     
    242241        }
    243242        o++;
    244     }
     243    } while (o != operators + ARRAY_SIZE(operators));
     244
    245245    bb_error_msg_and_die("syntax error at '%s'", argument);
    246 }
    247 
    248 /* return pointer to next token in buffer and set *buffer to one char
    249  * past the end of the above mentioned token
    250  */
    251 static char *get_token(char **buffer)
    252 {
    253     char *current = skip_whitespace(*buffer);
    254     if (*current != '\0') {
    255         *buffer = skip_non_whitespace(current);
    256         return current;
    257     }
    258     return NULL;
    259246}
    260247
     
    273260            cursor = line;
    274261            while (1) {
    275                 token = get_token(&cursor);
    276                 if (!token)
     262                token = skip_whitespace(cursor);
     263                if (*token == '\0')
    277264                    break;
    278                 *cursor++ = '\0';
     265                cursor = skip_non_whitespace(token);
     266                if (*cursor != '\0')
     267                    *cursor++ = '\0';
    279268                stack_machine(token);
    280269            }
     
    282271        }
    283272    } else {
    284         // why? it breaks "dc -2 2 * p"
     273        // why? it breaks "dc -2 2 + p"
    285274        //if (argv[0][0] == '-')
    286275        //  bb_show_usage();
Note: See TracChangeset for help on using the changeset viewer.