commit a5e45a2b7e84f6f3076f3a021fd2adbb137c44d9
parent 22aa3b95e597972d18bd7200dc39f982f7e5ab2a
Author: William Casarin <jb55@jb55.com>
Date: Sat, 16 Dec 2017 21:14:27 -0800
progress
Diffstat:
8 files changed, 418 insertions(+), 12 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -2,3 +2,4 @@
/parser.tab.c
/parser.tab.h
/bcalc
+*.o
diff --git a/Makefile b/Makefile
@@ -1,8 +1,12 @@
BIN=bcalc
-GEN=parser.tab.c parser.tab.h lex.yy.c
+DEPS=$(wildcard deps/*/*.c) $(GEN)
PREFIX ?= /usr/local
-SRCS=num.c
+
+DEPS = $(wildcard deps/*/*.c)
+OBJS = $(DEPS:.c=.o) parser.tab.o lex.yy.o
+GEN=parser.tab.c parser.tab.h lex.yy.c $(OBJS)
+
all: $(BIN)
@@ -16,8 +20,8 @@ install: $(BIN)
mkdir -p $(PREFIX)/bin
cp $(BIN) $(PREFIX)/bin
-$(BIN): $(GEN)
- $(CC) -o $@ $(SRCS) parser.tab.c lex.yy.c
+$(BIN): $(OBJS)
+ $(CC) -o $@ num.c $(OBJS)
clean:
rm -f $(GEN)
diff --git a/README.md b/README.md
@@ -0,0 +1,17 @@
+
+# bcalc
+
+ A simple calculator that understands Bitcoin Units
+
+## Usage
+
+ $ bcalc <<<'1 msat + 2 sats + 3 bits'
+
+
+## Install
+
+ $ make install
+
+## Units
+
+
diff --git a/deps/commander/commander.c b/deps/commander/commander.c
@@ -0,0 +1,269 @@
+
+//
+// commander.c
+//
+// Copyright (c) 2012 TJ Holowaychuk <tj@vision-media.ca>
+//
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include "commander.h"
+
+/*
+ * Output error and exit.
+ */
+
+static void
+error(char *msg) {
+ fprintf(stderr, "%s\n", msg);
+ exit(1);
+}
+
+/*
+ * Output command version.
+ */
+
+static void
+command_version(command_t *self) {
+ printf("%s\n", self->version);
+ command_free(self);
+ exit(0);
+}
+
+/*
+ * Output command help.
+ */
+
+void
+command_help(command_t *self) {
+ printf("\n");
+ printf(" Usage: %s %s\n", self->name, self->usage);
+ printf("\n");
+ printf(" Options:\n");
+ printf("\n");
+
+ int i;
+ for (i = 0; i < self->option_count; ++i) {
+ command_option_t *option = &self->options[i];
+ printf(" %s, %-25s %s\n"
+ , option->small
+ , option->large_with_arg
+ , option->description);
+ }
+
+ printf("\n");
+ command_free(self);
+ exit(0);
+}
+
+/*
+ * Initialize with program `name` and `version`.
+ */
+
+void
+command_init(command_t *self, const char *name, const char *version) {
+ self->arg = NULL;
+ self->name = name;
+ self->version = version;
+ self->option_count = self->argc = 0;
+ self->usage = "[options]";
+ self->nargv = NULL;
+ command_option(self, "-V", "--version", "output program version", command_version);
+ command_option(self, "-h", "--help", "output help information", command_help);
+}
+
+/*
+ * Free up commander after use.
+ */
+
+void
+command_free(command_t *self) {
+ int i;
+
+ for (i = 0; i < self->option_count; ++i) {
+ command_option_t *option = &self->options[i];
+ free(option->argname);
+ free(option->large);
+ }
+
+ if (self->nargv) {
+ for (i = 0; self->nargv[i]; ++i) {
+ free(self->nargv[i]);
+ }
+ free(self->nargv);
+ }
+}
+
+/*
+ * Parse argname from `str`. For example
+ * Take "--required <arg>" and populate `flag`
+ * with "--required" and `arg` with "<arg>".
+ */
+
+static void
+parse_argname(const char *str, char *flag, char *arg) {
+ int buffer = 0;
+ size_t flagpos = 0;
+ size_t argpos = 0;
+ size_t len = strlen(str);
+ size_t i;
+
+ for (i = 0; i < len; ++i) {
+ if (buffer || '[' == str[i] || '<' == str[i]) {
+ buffer = 1;
+ arg[argpos++] = str[i];
+ } else {
+ if (' ' == str[i]) continue;
+ flag[flagpos++] = str[i];
+ }
+ }
+
+ arg[argpos] = '\0';
+ flag[flagpos] = '\0';
+}
+
+/*
+ * Normalize the argument vector by exploding
+ * multiple options (if any). For example
+ * "foo -abc --scm git" -> "foo -a -b -c --scm git"
+ */
+
+static char **
+normalize_args(int *argc, char **argv) {
+ int size = 0;
+ int alloc = *argc + 1;
+ char **nargv = malloc(alloc * sizeof(char *));
+ int i;
+
+ for (i = 0; argv[i]; ++i) {
+ const char *arg = argv[i];
+ size_t len = strlen(arg);
+
+ // short flag
+ if (len > 2 && '-' == arg[0] && !strchr(arg + 1, '-')) {
+ alloc += len - 2;
+ nargv = realloc(nargv, alloc * sizeof(char *));
+ for (size_t j = 1; j < len; ++j) {
+ nargv[size] = malloc(3);
+ sprintf(nargv[size], "-%c", arg[j]);
+ size++;
+ }
+ continue;
+ }
+
+ // regular arg
+ nargv[size] = malloc(len + 1);
+ strcpy(nargv[size], arg);
+ size++;
+ }
+
+ nargv[size] = NULL;
+ *argc = size;
+ return nargv;
+}
+
+/*
+ * Define an option.
+ */
+
+void
+command_option(command_t *self, const char *small, const char *large, const char *desc, command_callback_t cb) {
+ if (self->option_count == COMMANDER_MAX_OPTIONS) {
+ command_free(self);
+ error("Maximum option definitions exceeded");
+ }
+ int n = self->option_count++;
+ command_option_t *option = &self->options[n];
+ option->cb = cb;
+ option->small = small;
+ option->description = desc;
+ option->required_arg = option->optional_arg = 0;
+ option->large_with_arg = large;
+ option->argname = malloc(strlen(large) + 1);
+ assert(option->argname);
+ option->large = malloc(strlen(large) + 1);
+ assert(option->large);
+ parse_argname(large, option->large, option->argname);
+ if ('[' == option->argname[0]) option->optional_arg = 1;
+ if ('<' == option->argname[0]) option->required_arg = 1;
+}
+
+/*
+ * Parse `argv` (internal).
+ * Input arguments should be normalized first
+ * see `normalize_args`.
+ */
+
+static void
+command_parse_args(command_t *self, int argc, char **argv) {
+ int literal = 0;
+ int i, j;
+
+ for (i = 1; i < argc; ++i) {
+ const char *arg = argv[i];
+ for (j = 0; j < self->option_count; ++j) {
+ command_option_t *option = &self->options[j];
+
+ // match flag
+ if (!strcmp(arg, option->small) || !strcmp(arg, option->large)) {
+ self->arg = NULL;
+
+ // required
+ if (option->required_arg) {
+ arg = argv[++i];
+ if (!arg || '-' == arg[0]) {
+ fprintf(stderr, "%s %s argument required\n", option->large, option->argname);
+ command_free(self);
+ exit(1);
+ }
+ self->arg = arg;
+ }
+
+ // optional
+ if (option->optional_arg) {
+ if (argv[i + 1] && '-' != argv[i + 1][0]) {
+ self->arg = argv[++i];
+ }
+ }
+
+ // invoke callback
+ option->cb(self);
+ goto match;
+ }
+ }
+
+ // --
+ if ('-' == arg[0] && '-' == arg[1] && 0 == arg[2]) {
+ literal = 1;
+ goto match;
+ }
+
+ // unrecognized
+ if ('-' == arg[0] && !literal) {
+ fprintf(stderr, "unrecognized flag %s\n", arg);
+ command_free(self);
+ exit(1);
+ }
+
+ int n = self->argc++;
+ if (n == COMMANDER_MAX_ARGS) {
+ command_free(self);
+ error("Maximum number of arguments exceeded");
+ }
+ self->argv[n] = (char *) arg;
+ match:;
+ }
+}
+
+/*
+ * Parse `argv` (public).
+ */
+
+void
+command_parse(command_t *self, int argc, char **argv) {
+ self->nargv = normalize_args(&argc, argv);
+ command_parse_args(self, argc, self->nargv);
+ self->argv[self->argc] = NULL;
+}
diff --git a/deps/commander/commander.h b/deps/commander/commander.h
@@ -0,0 +1,88 @@
+
+//
+// commander.h
+//
+// Copyright (c) 2012 TJ Holowaychuk <tj@vision-media.ca>
+//
+
+#ifndef COMMANDER_H
+#define COMMANDER_H
+
+/*
+ * Max options that can be defined.
+ */
+
+#ifndef COMMANDER_MAX_OPTIONS
+#define COMMANDER_MAX_OPTIONS 32
+#endif
+
+/*
+ * Max arguments that can be passed.
+ */
+
+#ifndef COMMANDER_MAX_ARGS
+#define COMMANDER_MAX_ARGS 32
+#endif
+
+/*
+ * Command struct.
+ */
+
+struct command;
+
+/*
+ * Option callback.
+ */
+
+typedef void (* command_callback_t)(struct command *self);
+
+/*
+ * Command option.
+ */
+
+typedef struct {
+ int optional_arg;
+ int required_arg;
+ char *argname;
+ char *large;
+ const char *small;
+ const char *large_with_arg;
+ const char *description;
+ command_callback_t cb;
+} command_option_t;
+
+/*
+ * Command.
+ */
+
+typedef struct command {
+ void *data;
+ const char *usage;
+ const char *arg;
+ const char *name;
+ const char *version;
+ int option_count;
+ command_option_t options[COMMANDER_MAX_OPTIONS];
+ int argc;
+ char *argv[COMMANDER_MAX_ARGS];
+ char **nargv;
+} command_t;
+
+// prototypes
+
+void
+command_init(command_t *self, const char *name, const char *version);
+
+void
+command_free(command_t *self);
+
+void
+command_help(command_t *self);
+
+void
+command_option(command_t *self, const char *small, const char *large, const char *desc, command_callback_t cb);
+
+void
+command_parse(command_t *self, int argc, char **argv);
+
+#endif /* COMMANDER_H */
diff --git a/deps/commander/package.json b/deps/commander/package.json
@@ -0,0 +1,9 @@
+{
+ "name": "commander",
+ "version": "1.3.2",
+ "repo": "clibs/commander",
+ "description": "Command-line argument parser",
+ "keywords": ["cli", "command", "parser", "argv", "args", "options"],
+ "license": "MIT",
+ "src": ["src/commander.h", "src/commander.c"]
+}
diff --git a/num.c b/num.c
@@ -85,12 +85,13 @@ num_div(struct num *dst, struct num *a, struct num *b) {
void
num_assign(struct num *dst, struct num *a) {
- dst->type = TYPE_INT;
- dst->unit = UNIT_MSATOSHI;
- dst->intval = num_to_msat(a);
+ struct num num;
+ num.type = TYPE_INT;
+ num.unit = UNIT_MSATOSHI;
+ num.intval = num_to_msat(a);
+ *dst = num;
}
-
void
num_print(struct num *num) {
if (num->type == TYPE_FLOAT)
diff --git a/parser.y b/parser.y
@@ -44,10 +44,27 @@ line: T_NEWLINE
| expr T_NEWLINE { num_print(&$1); printf("\n"); }
;
-expr: T_INT { num_init(&$$); }
- | T_FLOAT { num_init(&$$); $$.floatval = $1; }
- | T_INT T_UNIT { num_init(&$$); $$.intval = $1; $$.unit = $2; }
- | T_FLOAT T_UNIT { num_init(&$$); $$.floatval = $1; $$.unit = $2; }
+expr:
+ | T_INT T_UNIT { num_init(&$$);
+ $$.intval = $1;
+ $$.type = TYPE_INT;
+ $$.unit = $2;
+ num_assign(&$$, &$$);
+ }
+ | T_FLOAT T_UNIT { num_init(&$$);
+ $$.floatval = $1;
+ $$.type = TYPE_FLOAT;
+ $$.unit = $2;
+ num_assign(&$$, &$$);
+ }
+ /* | T_INT { num_init(&$$); */
+ /* $$.intval = $1; */
+ /* $$.type = TYPE_INT; */
+ /* } */
+ /* | T_FLOAT { num_init(&$$); */
+ /* $$.floatval = $1; */
+ /* $$.type = TYPE_FLOAT; */
+ /* } */
| expr T_PLUS expr { num_add(&$$, &$1, &$3); }
| expr T_MINUS expr { num_sub(&$$, &$1, &$3); }
| expr T_MULTIPLY expr { num_mul(&$$, &$1, &$3); }