aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Beppu <beppu@lbox.org>2000-06-12 22:59:12 +0000
committerJohn Beppu <beppu@lbox.org>2000-06-12 22:59:12 +0000
commit5db60a7a7a7695b4b48b76de4539822f2cae3903 (patch)
tree40ac3e1e9e0bc7b99b7eab1fd7827887b2b0affc
parent0c6a970eb26edf7e77e7c173d478b94100969fc4 (diff)
downloadbusybox-w32-5db60a7a7a7695b4b48b76de4539822f2cae3903.tar.gz
busybox-w32-5db60a7a7a7695b4b48b76de4539822f2cae3903.tar.bz2
busybox-w32-5db60a7a7a7695b4b48b76de4539822f2cae3903.zip
+ works as a filter, now!
- I have a feeling I could have used strtok, but the 2 functions I added are really small, so it shouldn't be a big deal. (They were just laying around, and I wanted to use them, again).
-rw-r--r--math.c57
1 files changed, 51 insertions, 6 deletions
diff --git a/math.c b/math.c
index 18af3534a..621b5c9b0 100644
--- a/math.c
+++ b/math.c
@@ -1,5 +1,6 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2#include "internal.h" 2#include "internal.h"
3#include <ctype.h>
3#include <stdio.h> 4#include <stdio.h>
4#include <stdlib.h> 5#include <stdlib.h>
5#include <unistd.h> 6#include <unistd.h>
@@ -131,14 +132,58 @@ static void stack_machine(const char *argument)
131 exit(-1); 132 exit(-1);
132} 133}
133 134
135/* return pointer to next token in buffer and set *buffer to one char
136 * past the end of the above mentioned token
137 */
138static char *get_token(char **buffer)
139{
140 char *start = NULL;
141 char *current = *buffer;
142
143 while (isspace(*current)) { current++; }
144 if (*current != 0) {
145 start = current;
146 while (!isspace(*current) && current != 0) { current++; }
147 *buffer = current;
148 }
149 return start;
150}
151
152/* In Perl one might say, scalar m|\s*(\S+)\s*|g */
153static int number_of_tokens(char *buffer)
154{
155 int i = 0;
156 char *b = buffer;
157 while (get_token(&b)) { i++; }
158 return i;
159}
160
134int math_main(int argc, char **argv) 161int math_main(int argc, char **argv)
135{ 162{
136 if (argc <= 1 || *argv[1]=='-') 163 /* take stuff from stdin if no args are given */
137 usage(math_usage); 164 if (argc <= 1) {
138 while (argc >= 2) { 165 int i, len;
139 stack_machine(argv[1]); 166 char *line = NULL;
140 argv++; 167 char *cursor = NULL;
141 argc--; 168 char *token = NULL;
169 while ((line = cstring_lineFromFile(stdin))) {
170 cursor = line;
171 len = number_of_tokens(line);
172 for (i = 0; i < len; i++) {
173 token = get_token(&cursor);
174 *cursor++ = 0;
175 stack_machine(token);
176 }
177 free(line);
178 }
179 } else {
180 if (*argv[1]=='-')
181 usage(math_usage);
182 while (argc >= 2) {
183 stack_machine(argv[1]);
184 argv++;
185 argc--;
186 }
142 } 187 }
143 stack_machine(0); 188 stack_machine(0);
144 exit( TRUE); 189 exit( TRUE);