aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c87
1 files changed, 67 insertions, 20 deletions
diff --git a/src/main.c b/src/main.c
index 3cc01a9..b9d64a7 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,24 +1,71 @@
#include "../include/parser.h"
#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
-int main() {
- const char *code =
-"value1 = 1\n"
-"value2 = true\n"
-"value3 = null\n"
-"value4 = \"hello world\"\n"
-"value5 = [\"hello\", \"world\", 5]\n"
-"value6 = {\"hello\": \"world\", \"value\": 23}\n"
-"value7 = fn () {}\n"
-"value8 = fn (value) {}\n"
-"value9 = {\n"
-" \"hello\": \"world\",\n"
-" \"sayHello\": fn() {\n"
-" \n"
-" }\n"
-"}\n"
-"\n"
-"str = value9[\"hello\"]\n"
-"value9[\"sayHello\"]()";
- return tsl_parse(code, strlen(code));
+static char* file_get_content(const char *filepath, size_t *filesize) {
+ struct stat file_stat;
+ int fd = open(filepath, O_RDONLY);
+ char *result = NULL;
+ *filesize = 0;
+ if(fd == -1) {
+ perror(filepath);
+ return NULL;
+ }
+
+ if(fstat(fd, &file_stat) == -1) {
+ perror(filepath);
+ goto cleanup;
+ }
+
+ if(!S_ISREG(file_stat.st_mode)) {
+ fprintf(stderr, "Error: %s is not a file\n", filepath);
+ goto cleanup;
+ }
+
+ *filesize = file_stat.st_size;
+ result = malloc(*filesize + 1);
+ if(!result) {
+ *filesize = 0;
+ fprintf(stderr, "Error: Failed to malloc %lu bytes from file %s\n", *filesize, filepath);
+ goto cleanup;
+ }
+
+ result[*filesize] = '\0';
+ if((size_t)read(fd, result, *filesize) != *filesize) {
+ free(result);
+ *filesize = 0;
+ fprintf(stderr, "Error: Failed to read all data from file %s\n", filepath);
+ goto cleanup;
+ }
+
+ cleanup:
+ close(fd);
+ return result;
+}
+
+static void usage() {
+ puts("usage: tsl [file]");
+}
+
+int main(int argc, char **argv) {
+ int result;
+ size_t filesize;
+ char *file_content;
+
+ if(argc != 2) {
+ usage();
+ return 1;
+ }
+
+ file_content = file_get_content(argv[1], &filesize);
+ if(!file_content)
+ return 1;
+ result = tsl_parse(file_content, filesize);
+ free(file_content); /* Not needed, but it make valgrind happy */
+ return result;
}