diff options
Diffstat (limited to 'miscutils')
-rw-r--r-- | miscutils/dc.c | 35 |
1 files changed, 34 insertions, 1 deletions
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 | ||
12 | static double stack[100]; | 12 | static double stack[100]; |
13 | static unsigned int pointer; | 13 | static unsigned int pointer; |
14 | static unsigned char base; | ||
14 | 15 | ||
15 | static void push(double a) | 16 | static 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 | ||
74 | static 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 | |||
83 | static 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 | |||
91 | static void print_stack_no_pop(void) | ||
92 | { | ||
93 | unsigned int i=pointer; | ||
94 | while (i) | ||
95 | print_base(stack[--i]); | ||
96 | } | ||
97 | |||
98 | static void print_no_pop(void) | ||
99 | { | ||
100 | print_base(stack[pointer-1]); | ||
101 | } | ||
102 | |||
73 | static void print(void) | 103 | static void print(void) |
74 | { | 104 | { |
75 | printf("%g\n", pop()); | 105 | print_base(pop()); |
76 | } | 106 | } |
77 | 107 | ||
78 | struct op { | 108 | struct 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 | ||