summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2016-10-04 13:44:57 -0400
committerJune McEnroe <june@causal.agency>2016-10-04 13:44:57 -0400
commit8982a6cd77d2e897147717cdac8d8467998f53f4 (patch)
tree26cf7d40b0abd6bf78e61b8756bb9a9ed921abbb
parentFix whitespace handling in rpn (diff)
downloadsrc-8982a6cd77d2e897147717cdac8d8467998f53f4.tar.gz
src-8982a6cd77d2e897147717cdac8d8467998f53f4.zip
Process argv in rpn
-rwxr-xr-x.bin/rpn.c37
1 files changed, 23 insertions, 14 deletions
diff --git a/.bin/rpn.c b/.bin/rpn.c
index d5be9cbf..9a88298b 100755
--- a/.bin/rpn.c
+++ b/.bin/rpn.c
@@ -97,6 +97,21 @@ static bool stack_op(char op) {
     return true;
 }
 
+static void process(const char *input) {
+    while (*input) {
+        if (stack_op(*input)) {
+            input++;
+        } else {
+            char *rest;
+            int64_t val = strtoll(input, &rest, stack.radix);
+            if (rest != input) {
+                input = rest;
+                push(val);
+            } else input++;
+        }
+    }
+}
+
 static char *prompt(EditLine *el __attribute((unused))) {
     static char p[4096];
     if (stack.len == 0) return "[] ";
@@ -113,7 +128,13 @@ static char *prompt(EditLine *el __attribute((unused))) {
     return p;
 }
 
-int main(int argc __attribute((unused)), char *argv[]) {
+int main(int argc, char *argv[]) {
+    if (argc > 1) {
+        for (int i = 1; i < argc; ++i)
+            process(argv[i]);
+        return EX_OK;
+    }
+
     EditLine *el = el_init(argv[0], stdin, stdout, stderr);
     el_set(el, EL_PROMPT, prompt);
     el_set(el, EL_SIGNAL, true);
@@ -123,19 +144,7 @@ int main(int argc __attribute((unused)), char *argv[]) {
         const char *line = el_gets(el, &count);
         if (count < 0) err(EX_IOERR, "el_gets");
         if (!line) break;
-
-        while (*line) {
-            if (stack_op(*line)) {
-                line++;
-            } else {
-                char *rest;
-                int64_t val = strtoll(line, &rest, stack.radix);
-                if (rest != line) {
-                    line = rest;
-                    push(val);
-                } else line++;
-            }
-        }
+        process(line);
     }
 
     el_end(el);