btcs

bitcoin script parser/evaluator/compiler/decompiler
git clone git://jb55.com/btcs
Log | Files | Refs | README | LICENSE

commit 303469f1a052d3bdb3890a58529b217709e588bc
parent 5381fceb6d4a6ea80d0d8b7ca0e1bf4a0c71a690
Author: William Casarin <jb55@jb55.com>
Date:   Thu, 13 Jun 2019 00:34:42 -0700

stdin script parsing working

Diffstat:
Mmain.c | 26++++++++++++++++++++------
Mutil.c | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Mutil.h | 4++++
3 files changed, 75 insertions(+), 6 deletions(-)

diff --git a/main.c b/main.c @@ -198,15 +198,29 @@ int main(int argc, const char *argv[]) } if (is_decompile) { - int ok = read_arg_or_stdin(input, buf, sizeof(buf), &written); + // we have stdin + char *line = NULL; + ssize_t len = 0; + size_t n; + int ok; + + if (input == NULL) { + while ((len = getline(&line, &n, stdin)) != -1) { + ok = decompile(line, len-1); + + if (!ok) + fail(5, "failed to decompile"); + } + } + else { + ok = read_arg_or_stdin(input, buf, sizeof(buf), &written); - if (!ok) - fail(3, "failed to read file or stdin"); + if (!ok) + fail(4, "failed to read input arg (too big?)"); + + } - ok = decompile((const char *)buf, written); - if (!ok) - fail(4, "failed to decompile"); } else { if (!compile_options) diff --git a/util.c b/util.c @@ -1,5 +1,6 @@ #include "util.h" +#include <errno.h> static bool char_to_hex(unsigned char *val, char c) { @@ -54,6 +55,56 @@ int read_fd(FILE *fd, unsigned char *buf, size_t buflen, size_t *written) return 1; } + + +// if typedef doesn't exist (msvc, blah) +ssize_t getline(char **lineptr, size_t *n, FILE *stream) { + size_t pos; + int c; + + if (lineptr == NULL || stream == NULL || n == NULL) { + errno = EINVAL; + return -1; + } + + c = getc(stream); + if (c == EOF) { + return -1; + } + + if (*lineptr == NULL) { + *lineptr = malloc(128); + if (*lineptr == NULL) { + return -1; + } + *n = 128; + } + + pos = 0; + while(c != EOF) { + if (pos + 1 >= *n) { + size_t new_size = *n + (*n >> 2); + if (new_size < 128) { + new_size = 128; + } + char *new_ptr = realloc(*lineptr, new_size); + if (new_ptr == NULL) { + return -1; + } + *n = new_size; + *lineptr = new_ptr; + } + + ((unsigned char *)(*lineptr))[pos ++] = c; + if (c == '\n') { + break; + } + c = getc(stream); + } + + (*lineptr)[pos] = '\0'; + return pos; +} int read_arg_or_stdin(const char *arg, unsigned char *buf, size_t buflen, size_t *written) diff --git a/util.h b/util.h @@ -5,9 +5,13 @@ #include <stdbool.h> #include <stdlib.h> #include <stdio.h> +#include <stdint.h> + +typedef intptr_t ssize_t; int read_arg_or_stdin(const char *arg, unsigned char *buf, size_t buflen, size_t *written); bool hex_decode(const char *str, size_t slen, void *buf, size_t bufsize); +ssize_t getline(char **lineptr, size_t *n, FILE *stream); #endif /* UTIL_H */