#include "../include/parser.h" #include #include #include #include #include #include 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); result = NULL; *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) { char *file_content; size_t filesize; TslProgram program; int result = 0; if(argc != 2) { usage(); return 1; } file_content = file_get_content(argv[1], &filesize); if(!file_content) return 2; tsl_program_init(&program); if(!tsl_parse(file_content, filesize, &program)) { result = 3; goto cleanup; } if(!tsl_program_run(&program)) { result = 4; goto cleanup; } cleanup: free(file_content); tsl_program_deinit(&program); return result; }