From 14c4d1583236480b9fa7f902e0eb24979549d21d Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Mon, 11 Oct 2021 11:42:13 +0200 Subject: Create run_stdin function --- src/bootstrap/main.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/bootstrap/main.c b/src/bootstrap/main.c index 2847397..b5fd821 100755 --- a/src/bootstrap/main.c +++ b/src/bootstrap/main.c @@ -50,6 +50,34 @@ run_file(char *file_name) { fclose(file); } +#define STDIN_BUF_SIZE 16 + +void +run_stdin(void) { + size_t buf_size = 0; + size_t buf_cap = STDIN_BUF_SIZE; + char *source = malloc(sizeof(char) * buf_cap); + + char c; + while ((c = getchar()) != EOF) { + if (buf_size == buf_cap) { + buf_cap *= 2; + source = realloc(source, buf_cap * sizeof(char)); + } + source[buf_size] = c; + buf_size++; + } + + StringView sv = (StringView){ + .start = source, + .n = buf_size, + }; + + process_source(&sv); + + free(source); +} + #ifndef BIN_NAME #define BIN_NAME "bdl" #endif @@ -81,7 +109,7 @@ main(int argc, char *argv[]) { // Run from stdin. if (optind == argc) { - // TODO: Run from stdin if no file is given. + run_stdin(); return EXIT_SUCCESS; } -- cgit v1.2.1