aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-11 11:42:13 +0200
committerBad Diode <bd@badd10de.dev>2021-10-11 11:42:13 +0200
commit14c4d1583236480b9fa7f902e0eb24979549d21d (patch)
treeb468188d1bc4e8c62d0789abf45f9a675b4f72f2
parent6a833ddc268191bfaea94a1cd896684f1b834ef7 (diff)
downloadbdl-14c4d1583236480b9fa7f902e0eb24979549d21d.tar.gz
bdl-14c4d1583236480b9fa7f902e0eb24979549d21d.zip
Create run_stdin function
-rwxr-xr-xsrc/bootstrap/main.c30
1 files changed, 29 insertions, 1 deletions
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) {
50 fclose(file); 50 fclose(file);
51} 51}
52 52
53#define STDIN_BUF_SIZE 16
54
55void
56run_stdin(void) {
57 size_t buf_size = 0;
58 size_t buf_cap = STDIN_BUF_SIZE;
59 char *source = malloc(sizeof(char) * buf_cap);
60
61 char c;
62 while ((c = getchar()) != EOF) {
63 if (buf_size == buf_cap) {
64 buf_cap *= 2;
65 source = realloc(source, buf_cap * sizeof(char));
66 }
67 source[buf_size] = c;
68 buf_size++;
69 }
70
71 StringView sv = (StringView){
72 .start = source,
73 .n = buf_size,
74 };
75
76 process_source(&sv);
77
78 free(source);
79}
80
53#ifndef BIN_NAME 81#ifndef BIN_NAME
54#define BIN_NAME "bdl" 82#define BIN_NAME "bdl"
55#endif 83#endif
@@ -81,7 +109,7 @@ main(int argc, char *argv[]) {
81 109
82 // Run from stdin. 110 // Run from stdin.
83 if (optind == argc) { 111 if (optind == argc) {
84 // TODO: Run from stdin if no file is given. 112 run_stdin();
85 return EXIT_SUCCESS; 113 return EXIT_SUCCESS;
86 } 114 }
87 115