From 168e2ff38ea1f5358092314a4e879fe30a5461b9 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Sun, 5 Nov 2023 08:03:08 +0100 Subject: [PATCH] WIP: basic io --- include/ulas.h | 12 +++++++++++- src/main.c | 16 ++++++++++------ src/ulas.c | 25 +++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/include/ulas.h b/include/ulas.h index f916a91..57b106d 100644 --- a/include/ulas.h +++ b/include/ulas.h @@ -6,6 +6,8 @@ #include #include +#define ULAS_PATHMAX 4096 + #define MAX(x, y) (((x) > (y)) ? (x) : (y)) #define MIN(x, y) (((x) < (y)) ? (x) : (y)) @@ -20,7 +22,10 @@ // configurable tokens #define ULAS_TOK_COMMENT ';' -#define ULAS_TOK_DIRECTIVE_BEGIN '#' +// start of as directives such as .org +#define ULAS_TOK_ASDIR_BEGIN '.' +// start of preprocessor directives such as #define or #include +#define ULAS_TOK_PREPROC_BEGIN '#' // format macros #define ULAS_FMT(f, fmt) \ @@ -33,9 +38,12 @@ extern FILE *ulasout; extern FILE *ulaserr; struct ulas_config { + // argv represents file names char **argv; int argc; + char *output_path; + bool verbose; }; @@ -46,4 +54,6 @@ void ulas_init(struct ulas_config cfg); int ulas_main(struct ulas_config cfg); +char *ulas_strndup(const char *src, size_t n); + #endif diff --git a/src/main.c b/src/main.c index d591da6..90bd1b2 100644 --- a/src/main.c +++ b/src/main.c @@ -12,16 +12,17 @@ #define ULAS_OPTS "hvV" // args with value -#define ULAS_OPTS_ARG "" +#define ULAS_OPTS_ARG "o:" -#define ULAS_HELP(a, desc) printf("\t%s\t%s\n", (a), desc); +#define ULAS_HELP(a, desc) printf("\t-%s\t%s\n", (a), desc); void ulas_help(void) { printf("%s\n", ULAS_NAME); - printf("Usage %s [-%s]\n\n", ULAS_NAME, ULAS_OPTS); - ULAS_HELP("-h", "display this help and exit"); - ULAS_HELP("-V", "display version info and exit"); - ULAS_HELP("-v", "verbose output"); + printf("Usage %s [-%s] [-o=path] [input]\n\n", ULAS_NAME, ULAS_OPTS); + ULAS_HELP("h", "display this help and exit"); + ULAS_HELP("V", "display version info and exit"); + ULAS_HELP("v", "verbose output"); + ULAS_HELP("o=path", "Output file"); } void ulas_version(void) { printf("%s version %s\n", ULAS_NAME, ULAS_VER); } @@ -41,6 +42,9 @@ void ulas_getopt(int argc, char **argv, struct ulas_config *cfg) { case 'v': cfg->verbose = true; break; + case 'o': + cfg->output_path = ulas_strndup(optarg, ULAS_PATHMAX); + break; case '?': break; default: diff --git a/src/ulas.c b/src/ulas.c index 1d88cd9..ee6c299 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -1,4 +1,6 @@ #include "ulas.h" +#include +#include FILE *ulasin = NULL; FILE *ulasout = NULL; @@ -25,7 +27,30 @@ struct ulas_config ulas_cfg_from_env(void) { return cfg; } +char *ulas_strndup(const char *src, size_t n) { + size_t len = MIN(strlen(src), n); + char *dst = malloc(len); + strncpy(dst, src, len); + return dst; +} + int ulas_main(struct ulas_config cfg) { + if (cfg.output_path) { + ulasout = fopen(cfg.output_path, "we"); + if (!ulasout) { + fprintf(ulaserr, "%s: %s\n", cfg.output_path, strerror(errno)); + free(cfg.output_path); + return -1; + } + } + ulas_init(cfg); + + if (cfg.output_path) { + fclose(ulasout); + free(cfg.output_path); + return -1; + } + return 0; } -- 2.30.2