aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2002-12-12 10:31:53 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2002-12-12 10:31:53 +0000
commit6d07432b2ff21f0d8537ba5fae3c402be9cb247a (patch)
tree21111cbc0beb96ef6bbb1dfdf51c52bcdd0bed52
parentb8dff0c2a2d2d53e6f0888568305dc17e990cddc (diff)
downloadbusybox-w32-6d07432b2ff21f0d8537ba5fae3c402be9cb247a.tar.gz
busybox-w32-6d07432b2ff21f0d8537ba5fae3c402be9cb247a.tar.bz2
busybox-w32-6d07432b2ff21f0d8537ba5fae3c402be9cb247a.zip
Support the o, f and p options, patch by Magnus M�rtensson
-rw-r--r--include/usage.h7
-rw-r--r--miscutils/dc.c35
2 files changed, 40 insertions, 2 deletions
diff --git a/include/usage.h b/include/usage.h
index 5b357a218..2ebf9ad43 100644
--- a/include/usage.h
+++ b/include/usage.h
@@ -245,7 +245,12 @@
245#define dc_full_usage \ 245#define dc_full_usage \
246 "This is a Tiny RPN calculator that understands the\n" \ 246 "This is a Tiny RPN calculator that understands the\n" \
247 "following operations: +, -, /, *, and, or, not, eor.\n" \ 247 "following operations: +, -, /, *, and, or, not, eor.\n" \
248 "i.e., 'dc 2 2 add' -> 4, and 'dc 8 8 \\* 2 2 + /' -> 16" 248 "i.e., 'dc 2 2 add' -> 4, and 'dc 8 8 \\* 2 2 + /' -> 16" \
249 "p - Prints the value on the top of the stack, without altering the stack.\n" \
250 "f - Prints the entire contents of the stack without altering anything.\n" \
251 "o - Pops the value off the top of the stack and uses it to set the output radix.\n" \
252 " Only 10 and 16 are supported.\n"
253
249#define dc_example_usage \ 254#define dc_example_usage \
250 "$ dc 2 2 +\n" \ 255 "$ dc 2 2 +\n" \
251 "4\n" \ 256 "4\n" \
diff --git a/miscutils/dc.c b/miscutils/dc.c
index f9020b360..c7b43ea0a 100644
--- a/miscutils/dc.c
+++ b/miscutils/dc.c
@@ -11,6 +11,7 @@
11 11
12static double stack[100]; 12static double stack[100];
13static unsigned int pointer; 13static unsigned int pointer;
14static unsigned char base;
14 15
15static void push(double a) 16static void push(double a)
16{ 17{
@@ -70,9 +71,38 @@ static void not(void)
70 push(~(unsigned int) pop()); 71 push(~(unsigned int) pop());
71} 72}
72 73
74static void set_output_base(void)
75{
76 base=(unsigned char)pop();
77 if ((base != 10) && (base != 16)) {
78 fprintf(stderr, "Error: base = %d is not supported.\n", base);
79 base=10;
80 }
81}
82
83static void print_base(double print)
84{
85 if (base == 16)
86 printf("%x\n", (unsigned int)print);
87 else
88 printf("%g\n", print);
89}
90
91static void print_stack_no_pop(void)
92{
93 unsigned int i=pointer;
94 while (i)
95 print_base(stack[--i]);
96}
97
98static void print_no_pop(void)
99{
100 print_base(stack[pointer-1]);
101}
102
73static void print(void) 103static void print(void)
74{ 104{
75 printf("%g\n", pop()); 105 print_base(pop());
76} 106}
77 107
78struct op { 108struct op {
@@ -93,6 +123,9 @@ static const struct op operators[] = {
93 {"or", or}, 123 {"or", or},
94 {"not", not}, 124 {"not", not},
95 {"eor", eor}, 125 {"eor", eor},
126 {"p", print_no_pop},
127 {"f", print_stack_no_pop},
128 {"o", set_output_base},
96 {0, 0} 129 {0, 0}
97}; 130};
98 131