summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-06-10 16:12:33 +0200
committerkdx <kikoodx@paranoici.org>2023-06-10 16:12:33 +0200
commit37d470da243a2165d28b8a807ce940cb27b2c321 (patch)
treea64995748228bcb0e998ffeffa4fd124bf4e07ad /src
parentc9dd409007a4c87bac3c497fd2df2e7d333b8609 (diff)
downloadgolem-37d470da243a2165d28b8a807ce940cb27b2c321.tar.gz
read source from file
Diffstat (limited to 'src')
-rw-r--r--src/main.c25
1 files changed, 23 insertions, 2 deletions
diff --git a/src/main.c b/src/main.c
index 2a15976..a256143 100644
--- a/src/main.c
+++ b/src/main.c
@@ -944,13 +944,34 @@ parse(Token *tok)
return head.next;
}
+static char *
+drain(FILE *fp)
+{
+ if (fseek(fp, 0, SEEK_END) < 0)
+ error("fseek failed");
+ const size_t size = ftell(fp);
+ if (fseek(fp, 0, SEEK_SET) < 0)
+ error("fseek failed");
+ char *data = malloc(size + 1);
+ if (data == NULL)
+ error("malloc failed");
+ if (fread(data, 1, size, fp) != size)
+ error("fread failed");
+ data[size] = '\0';
+ return data;
+}
+
int
main(int argc, char **argv)
{
if (argc != 2)
- error("usage: %s code", argv[0]);
+ error("usage: %s file", argv[0]);
- Token *tok = tokenize(argv[1]);
+ FILE *const fp = fopen(argv[1], "rb");
+ if (fp == NULL)
+ error("fopen failed");
+ Token *const tok = tokenize(drain(fp));
+ fclose(fp);
Node *node = parse(tok);