Patch from David Meggy to make the swap default to the new version if no
[oweals/busybox.git] / miscutils / dc.c
index c7b43ea0ac1efbbc665a91d84326f70cca1dc551..f574ae4a0f6c16c8105dc9311b7d37fbf8c204d6 100644 (file)
@@ -16,14 +16,14 @@ static unsigned char base;
 static void push(double a)
 {
        if (pointer >= (sizeof(stack) / sizeof(*stack)))
-               error_msg_and_die("stack overflow");
+               bb_error_msg_and_die("stack overflow");
        stack[pointer++] = a;
 }
 
 static double pop(void)
 {
        if (pointer == 0)
-               error_msg_and_die("stack underflow");
+               bb_error_msg_and_die("stack underflow");
        return stack[--pointer];
 }
 
@@ -44,6 +44,13 @@ static void mul(void)
        push(pop() * pop());
 }
 
+static void power(void)
+{
+       double topower = pop();
+
+       push(pow(pop(), topower));
+}
+
 static void divide(void)
 {
        double divisor = pop();
@@ -51,6 +58,13 @@ static void divide(void)
        push(pop() / divisor);
 }
 
+static void mod(void)
+{
+       unsigned int d = pop();
+
+       push((unsigned int) pop() % d);
+}
+
 static void and(void)
 {
        push((unsigned int) pop() & (unsigned int) pop());
@@ -119,10 +133,16 @@ static const struct op operators[] = {
        {"mul", mul},
        {"/",   divide},
        {"div", divide},
+       {"**",  power},
+       {"exp", power},
+       {"pow", power},
+       {"%",   mod},
+       {"mod", mod},
        {"and", and},
        {"or",  or},
        {"not", not},
        {"eor", eor},
+       {"xor", eor},
        {"p", print_no_pop},
        {"f", print_stack_no_pop},
        {"o", set_output_base},
@@ -154,7 +174,7 @@ static void stack_machine(const char *argument)
                }
                o++;
        }
-       error_msg_and_die("%s: syntax error.", argument);
+       bb_error_msg_and_die("%s: syntax error.", argument);
 }
 
 /* return pointer to next token in buffer and set *buffer to one char
@@ -168,7 +188,7 @@ static char *get_token(char **buffer)
        while (isspace(*current)) { current++; }
        if (*current != 0) {
                start = current;
-               while (!isspace(*current) && current != 0) { current++; }
+               while (!isspace(*current) && *current != 0) { current++; }
                *buffer = current;
        }
        return start;
@@ -191,7 +211,7 @@ int dc_main(int argc, char **argv)
                char *line   = NULL;
                char *cursor = NULL;
                char *token  = NULL;
-               while ((line = get_line_from_file(stdin))) {
+               while ((line = bb_get_chomped_line_from_file(stdin))) {
                        cursor = line;
                        len = number_of_tokens(line);
                        for (i = 0; i < len; i++) {
@@ -203,7 +223,7 @@ int dc_main(int argc, char **argv)
                }
        } else {
                if (*argv[1]=='-')
-                       show_usage();
+                       bb_show_usage();
                while (argc >= 2) {
                        stack_machine(argv[1]);
                        argv++;