/* vi: set sw=4 ts=4: */
-#include "internal.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <unistd.h>
#include <math.h>
+#include "busybox.h"
/* Tiny RPN calculator, because "expr" didn't give me bitwise operations. */
-static const char dc_usage[] = "dc expression ...\n"
-#ifndef BB_FEATURE_TRIVIAL_HELP
- "\nThis is a Tiny RPN calculator that understands the\n"
- "following operations: +, -, /, *, and, or, not, eor.\n"
- "i.e. 'dc 2 2 add' -> 4, and 'dc 8 8 \\* 2 2 + /' -> 16\n"
-#endif
- ;
-
static double stack[100];
static unsigned int pointer;
static void push(double a)
{
- if (pointer >= (sizeof(stack) / sizeof(*stack))) {
- fprintf(stderr, "dc: stack overflow\n");
- exit(-1);
- } else
- stack[pointer++] = a;
+ if (pointer >= (sizeof(stack) / sizeof(*stack)))
+ error_msg_and_die("stack overflow");
+ stack[pointer++] = a;
}
static double pop()
{
- if (pointer == 0) {
- fprintf(stderr, "dc: stack underflow\n");
- exit(-1);
- }
+ if (pointer == 0)
+ error_msg_and_die("stack underflow");
return stack[--pointer];
}
}
o++;
}
- fprintf(stderr, "dc: %s: syntax error.\n", argument);
- exit(-1);
+ error_msg_and_die("%s: syntax error.", argument);
}
/* return pointer to next token in buffer and set *buffer to one char
char *line = NULL;
char *cursor = NULL;
char *token = NULL;
- while ((line = cstring_lineFromFile(stdin))) {
+ while ((line = get_line_from_file(stdin))) {
cursor = line;
len = number_of_tokens(line);
for (i = 0; i < len; i++) {
}
} else {
if (*argv[1]=='-')
- usage(dc_usage);
+ show_usage();
while (argc >= 2) {
stack_machine(argv[1]);
argv++;
}
}
stack_machine(0);
- return( TRUE);
+ return EXIT_SUCCESS;
}