diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-03-24 14:06:51 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-03-24 14:06:51 +0000 |
commit | 5b27fbe990d868441452c474e5b14e94f8bc8335 (patch) | |
tree | c6a7e85b1d6d1c8e18089c3f7d83e392cf04448f /miscutils | |
parent | b5b45a91f0f2d3f57864b49eb3126c9eb6a2b1eb (diff) | |
download | busybox-w32-5b27fbe990d868441452c474e5b14e94f8bc8335.tar.gz busybox-w32-5b27fbe990d868441452c474e5b14e94f8bc8335.tar.bz2 busybox-w32-5b27fbe990d868441452c474e5b14e94f8bc8335.zip |
dc: use common_bufsiz1 for evaluation stack
msh: fix "underscore bug" (a_b=1111 didn't work)
dnsd: openlog(), so that applet's name is logged
Diffstat (limited to 'miscutils')
-rw-r--r-- | miscutils/dc.c | 35 |
1 files changed, 17 insertions, 18 deletions
diff --git a/miscutils/dc.c b/miscutils/dc.c index 2121f7669..8c9d77fc3 100644 --- a/miscutils/dc.c +++ b/miscutils/dc.c | |||
@@ -4,22 +4,19 @@ | |||
4 | */ | 4 | */ |
5 | 5 | ||
6 | #include "busybox.h" | 6 | #include "busybox.h" |
7 | #include <ctype.h> | ||
8 | #include <stdio.h> | ||
9 | #include <stdlib.h> | ||
10 | #include <string.h> | ||
11 | #include <unistd.h> | ||
12 | #include <math.h> | 7 | #include <math.h> |
13 | 8 | ||
14 | /* Tiny RPN calculator, because "expr" didn't give me bitwise operations. */ | 9 | /* Tiny RPN calculator, because "expr" didn't give me bitwise operations. */ |
15 | 10 | ||
16 | static double stack[100]; | 11 | enum { STACK_SIZE = sizeof(bb_common_bufsiz1) / sizeof(double) }; |
12 | |||
13 | #define stack ((double*)&bb_common_bufsiz1) | ||
17 | static unsigned int pointer; | 14 | static unsigned int pointer; |
18 | static unsigned char base; | 15 | static unsigned char base; |
19 | 16 | ||
20 | static void push(double a) | 17 | static void push(double a) |
21 | { | 18 | { |
22 | if (pointer >= (sizeof(stack) / sizeof(*stack))) | 19 | if (pointer >= STACK_SIZE) |
23 | bb_error_msg_and_die("stack overflow"); | 20 | bb_error_msg_and_die("stack overflow"); |
24 | stack[pointer++] = a; | 21 | stack[pointer++] = a; |
25 | } | 22 | } |
@@ -91,10 +88,10 @@ static void not(void) | |||
91 | 88 | ||
92 | static void set_output_base(void) | 89 | static void set_output_base(void) |
93 | { | 90 | { |
94 | base=(unsigned char)pop(); | 91 | base = (unsigned char)pop(); |
95 | if ((base != 10) && (base != 16)) { | 92 | if ((base != 10) && (base != 16)) { |
96 | fprintf(stderr, "Error: base = %d is not supported.\n", base); | 93 | bb_error_msg("error, base %d is not supported", base); |
97 | base=10; | 94 | base = 10; |
98 | } | 95 | } |
99 | } | 96 | } |
100 | 97 | ||
@@ -103,12 +100,12 @@ static void print_base(double print) | |||
103 | if (base == 16) | 100 | if (base == 16) |
104 | printf("%x\n", (unsigned int)print); | 101 | printf("%x\n", (unsigned int)print); |
105 | else | 102 | else |
106 | printf("%g\n", print); | 103 | printf("%g\n", print); |
107 | } | 104 | } |
108 | 105 | ||
109 | static void print_stack_no_pop(void) | 106 | static void print_stack_no_pop(void) |
110 | { | 107 | { |
111 | unsigned int i=pointer; | 108 | unsigned int i = pointer; |
112 | while (i) | 109 | while (i) |
113 | print_base(stack[--i]); | 110 | print_base(stack[--i]); |
114 | } | 111 | } |
@@ -119,7 +116,7 @@ static void print_no_pop(void) | |||
119 | } | 116 | } |
120 | 117 | ||
121 | struct op { | 118 | struct op { |
122 | const char *name; | 119 | const char name[4]; |
123 | void (*function) (void); | 120 | void (*function) (void); |
124 | }; | 121 | }; |
125 | 122 | ||
@@ -145,7 +142,7 @@ static const struct op operators[] = { | |||
145 | {"p", print_no_pop}, | 142 | {"p", print_no_pop}, |
146 | {"f", print_stack_no_pop}, | 143 | {"f", print_stack_no_pop}, |
147 | {"o", set_output_base}, | 144 | {"o", set_output_base}, |
148 | {0, 0} | 145 | {"", 0} |
149 | }; | 146 | }; |
150 | 147 | ||
151 | static void stack_machine(const char *argument) | 148 | static void stack_machine(const char *argument) |
@@ -164,9 +161,9 @@ static void stack_machine(const char *argument) | |||
164 | return; | 161 | return; |
165 | } | 162 | } |
166 | 163 | ||
167 | while (o->name != 0) { | 164 | while (o->name[0]) { |
168 | if (strcmp(o->name, argument) == 0) { | 165 | if (strcmp(o->name, argument) == 0) { |
169 | (*(o->function)) (); | 166 | o->function(); |
170 | return; | 167 | return; |
171 | } | 168 | } |
172 | o++; | 169 | o++; |
@@ -185,7 +182,9 @@ static char *get_token(char **buffer) | |||
185 | current = skip_whitespace(*buffer); | 182 | current = skip_whitespace(*buffer); |
186 | if (*current != 0) { | 183 | if (*current != 0) { |
187 | start = current; | 184 | start = current; |
188 | while (!isspace(*current) && *current != 0) { current++; } | 185 | while (!isspace(*current) && *current != 0) { |
186 | current++; | ||
187 | } | ||
189 | *buffer = current; | 188 | *buffer = current; |
190 | } | 189 | } |
191 | return start; | 190 | return start; |
@@ -220,7 +219,7 @@ int dc_main(int argc, char **argv) | |||
220 | free(line); | 219 | free(line); |
221 | } | 220 | } |
222 | } else { | 221 | } else { |
223 | if (*argv[1]=='-') | 222 | if (*argv[1] == '-') |
224 | bb_show_usage(); | 223 | bb_show_usage(); |
225 | while (argc >= 2) { | 224 | while (argc >= 2) { |
226 | stack_machine(argv[1]); | 225 | stack_machine(argv[1]); |