From d46ee327756a2d649c709738275af1263b0822ae Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Thu, 29 Jan 2026 04:12:33 +0000 Subject: [PATCH] main: split off reading and usage function to its own unit --- Makefile | 2 +- include/arl/cli.h | 31 ++++++++++++++++++ src/cli.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 63 ++--------------------------------- 4 files changed, 118 insertions(+), 61 deletions(-) create mode 100644 include/arl/cli.h create mode 100644 src/cli.c diff --git a/Makefile b/Makefile index 1980693..f5e6864 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ DIST=build OUT=$(DIST)/arl.out MODULES=. lib lexer -UNITS=main lib/vec lib/sv lexer/token lexer/lexer +UNITS=main cli lib/vec lib/sv lexer/token lexer/lexer OBJECTS:=$(patsubst %,$(DIST)/%.o, $(UNITS)) LDFLAGS= diff --git a/include/arl/cli.h b/include/arl/cli.h new file mode 100644 index 0000000..8194dc5 --- /dev/null +++ b/include/arl/cli.h @@ -0,0 +1,31 @@ +/* cli.h: CLI helpers + * Created: 2026-01-29 + * Author: Aryadev Chavali + * License: See end of file + * Commentary: + */ + +#ifndef CLI_H +#define CLI_H + +#include + +#include + +int read_file(const char *filename, sv_t *ret); +int read_pipe(FILE *pipe, sv_t *ret); +void usage(FILE *fp); + +#endif + +/* Copyright (C) 2026 Aryadev Chavali + + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the MIT License for details. + + * You may distribute and modify this code under the terms of the MIT License, + * which you should have received a copy of along with this program. If not, + * please go to . + + */ diff --git a/src/cli.c b/src/cli.c new file mode 100644 index 0000000..97a6eac --- /dev/null +++ b/src/cli.c @@ -0,0 +1,83 @@ +/* cli.c: + * Created: 2026-01-29 + * Author: Aryadev Chavali + * License: See end of file + * Commentary: See /include/arl/cli.h + */ + +#include +#include + +#include +#include + +int read_file(const char *filename, sv_t *ret) +{ + // NOTE: Stupidly simple. Presumes the file is NOT three pipes in a trench + // coat. + FILE *fp = fopen(filename, "rb"); + if (!fp) + return 1; + + fseek(fp, 0, SEEK_END); + ret->size = ftell(fp); + fseek(fp, 0, SEEK_SET); + ret->data = calloc(1, ret->size + 1); + fread(ret->data, ret->size, 1, fp); + fclose(fp); + + ret->data[ret->size] = '\0'; + return 0; +} + +int read_pipe(FILE *pipe, sv_t *ret) +{ + // NOTE: We can't read an entire pipe at once like we did for read_file. So + // let's read in buffered chunks, with a vector to keep them contiguous. + vec_t contents = {0}; + char buffer[1024]; + while (!feof(pipe)) + { + size_t bytes_read = fread(buffer, 1, sizeof(buffer), pipe); + vec_append(&contents, buffer, bytes_read); + } + + ret->size = contents.size; + // Get that null terminator in, but only after we've recorded the actual size + // of what's been read. + vec_append_byte(&contents, '\0'); + + if (contents.not_inlined) + { + // Take the heap pointer from us. + ret->data = vec_data(&contents); + } + else + { + // vec_data(&contents) is stack allocated; can't carry that out of this + // function! + ret->data = calloc(1, contents.size); + memcpy(ret->data, vec_data(&contents), contents.size); + } + return 0; +} + +void usage(FILE *fp) +{ + fprintf(fp, "Usage: arl [FILE]\n" + "Compiles [FILE] as ARL source code.\n" + " [FILE]: File to compile.\n" + "If FILE is \"--\", then read from stdin.\n"); +} + +/* Copyright (C) 2026 Aryadev Chavali + + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the MIT License for details. + + * You may distribute and modify this code under the terms of the MIT License, + * which you should have received a copy of along with this program. If not, + * please go to . + + */ diff --git a/src/main.c b/src/main.c index 977eca8..3f16b6e 100644 --- a/src/main.c +++ b/src/main.c @@ -18,64 +18,7 @@ #include #include -int read_file(const char *filename, sv_t *ret) -{ - // NOTE: Stupidly simple. Presumes the file is NOT three pipes in a trench - // coat. - FILE *fp = fopen(filename, "rb"); - if (!fp) - return 1; - - fseek(fp, 0, SEEK_END); - ret->size = ftell(fp); - fseek(fp, 0, SEEK_SET); - ret->data = calloc(1, ret->size + 1); - fread(ret->data, ret->size, 1, fp); - fclose(fp); - - ret->data[ret->size] = '\0'; - return 0; -} - -int read_pipe(FILE *pipe, sv_t *ret) -{ - // NOTE: We can't read an entire pipe at once like we did for read_file. So - // let's read in buffered chunks, with a vector to keep them contiguous. - vec_t contents = {0}; - char buffer[1024]; - while (!feof(pipe)) - { - size_t bytes_read = fread(buffer, 1, sizeof(buffer), pipe); - vec_append(&contents, buffer, bytes_read); - } - - ret->size = contents.size; - // Get that null terminator in, but only after we've recorded the actual size - // of what's been read. - vec_append_byte(&contents, '\0'); - - if (contents.not_inlined) - { - // Take the heap pointer from us. - ret->data = vec_data(&contents); - } - else - { - // vec_data(&contents) is stack allocated; can't carry that out of this - // function! - ret->data = calloc(1, contents.size); - memcpy(ret->data, vec_data(&contents), contents.size); - } - return 0; -} - -void usage(FILE *fp) -{ - fprintf(fp, "Usage: arl [FILE]\n" - "Compiles [FILE] as ARL source code.\n" - " [FILE]: File to compile.\n" - "If FILE is \"--\", then read from stdin.\n"); -} +#include int main(int argc, char *argv[]) { @@ -127,11 +70,11 @@ int main(int argc, char *argv[]) goto end; } - LOG("Lexed %lu tokens\n", tokens.vec.size / sizeof(token_t)); #if VERBOSE_LOGS + LOG("Lexed %lu tokens ", tokens.vec.size / sizeof(token_t)); token_stream_print(stdout, &tokens); -#endif printf("\n"); +#endif end: if (contents.data)