From fc2238e2cb5ad322d67c120fe73ce139fd6e5a9c Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Sun, 12 Nov 2023 18:16:49 +0100 Subject: [PATCH] Added basic preprocessor to cli --- include/ulas.h | 1 + src/main.c | 6 +++++- src/ulas.c | 41 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/include/ulas.h b/include/ulas.h index e5d5ac8..fc6738d 100644 --- a/include/ulas.h +++ b/include/ulas.h @@ -78,6 +78,7 @@ struct ulas_config { char *output_path; int verbose; + int preproc_only; }; /** diff --git a/src/main.c b/src/main.c index cdc11b7..48c959f 100644 --- a/src/main.c +++ b/src/main.c @@ -9,7 +9,7 @@ #define ULAS_VER "0.0.1" // args without value -#define ULAS_OPTS "hvV" +#define ULAS_OPTS "hvVp" // args with value #define ULAS_OPTS_ARG "o:" @@ -22,6 +22,7 @@ void ulas_help(void) { ULAS_HELP("h", "display this help and exit"); ULAS_HELP("V", "display version info and exit"); ULAS_HELP("v", "verbose output"); + ULAS_HELP("p", "Stop after preprocessor"); ULAS_HELP("o=path", "Output file"); } @@ -45,6 +46,9 @@ void ulas_getopt(int argc, char **argv, struct ulas_config *cfg) { case 'o': cfg->output_path = strndup(optarg, ULAS_PATHMAX); break; + case 'p': + cfg->preproc_only = 1; + break; case '?': break; default: diff --git a/src/ulas.c b/src/ulas.c index 24a3466..6c92348 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -35,6 +35,7 @@ struct ulas_config ulas_cfg_from_env(void) { } int ulas_main(struct ulas_config cfg) { + int rc = 0; if (cfg.output_path) { ulasout = fopen(cfg.output_path, "we"); if (!ulasout) { @@ -44,15 +45,50 @@ int ulas_main(struct ulas_config cfg) { } } + if (cfg.argc > 0) { + ulasin = fopen(cfg.argv[0], "re"); + if (!ulasin) { + fprintf(ulaserr, "%s: %s\n", cfg.argv[0], strerror(errno)); + return -1; + } + } + ulas_init(cfg); + FILE *preprocdst = NULL; + + if (cfg.preproc_only) { + preprocdst = ulasout; + } else { + preprocdst = tmpfile(); + } + + if (ulas_preproc(preprocdst, ulasin) == -1) { + rc = -1; + goto cleanup; + } + + if (cfg.preproc_only) { + goto cleanup; + } + + // TODO: rest of steps here + +cleanup: + if (!cfg.preproc_only) { + fclose(preprocdst); + } + if (cfg.output_path) { fclose(ulasout); free(cfg.output_path); - return -1; } - return 0; + if (cfg.argc > 0) { + fclose(ulasin); + } + + return rc; } int ulas_isname(const char *tok, size_t n) { @@ -412,6 +448,7 @@ void ulas_trimend(char c, char *buf, size_t n) { int ulas_preprocline(struct ulas_preproc *pp, FILE *dst, FILE *src, const char *raw_line, size_t n) { /** + * Footgun warning: * We do use raw pointers to the line here... which is fine * as long as it is never used after a recursive call to the preprocessor! * never use this pointer after such a recursive call! -- 2.30.2