From: Lukas Krickl Date: Wed, 29 Nov 2023 14:31:32 +0000 (+0100) Subject: Added 2-pass code when stdin is not stin X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=b0b3f9e709af410fd2edc0f066eb3a27b4c2937f;p=ulas%2F.git Added 2-pass code when stdin is not stin --- diff --git a/include/ulas.h b/include/ulas.h index ad09376..ea50118 100644 --- a/include/ulas.h +++ b/include/ulas.h @@ -174,9 +174,8 @@ struct ulas_symbuf { */ enum ulas_pass { - ULAS_PASS_END = 0, - ULAS_PASS_FINAL = 1, - ULAS_PASS_RESOLVE = 2, + ULAS_PASS_FINAL = 0, + ULAS_PASS_RESOLVE = 1, }; struct ulas { diff --git a/src/ulas.c b/src/ulas.c index 49eae6c..3df3631 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -4,7 +4,6 @@ #include #include #include -#include FILE *ulasin = NULL; FILE *ulasout = NULL; @@ -112,25 +111,41 @@ int ulas_main(struct ulas_config cfg) { ulasin = ulas_fopen(cfg.argv[0], "re", stdin); } + // only do 2 pass if we have a file as input + // because we cannot really rewind stdout + if (!cfg.preproc_only && ulasin != stdin) { + ulas.pass = ULAS_PASS_RESOLVE; + } + FILE *preprocdst = NULL; + while (ulas.pass >= 0) { + if (ulascfg.verbose) { + fprintf(ulaserr, "[Pass %d]\n", ulas.pass); + } - if (cfg.preproc_only) { - preprocdst = ulasout; - } else { - preprocdst = tmpfile(); - } + // FIXME: it would be nice if we could do the 2 pass by clearing the + // tmpfile instead of making an entierly new one + if (cfg.preproc_only) { + preprocdst = ulasout; + } else { + preprocdst = tmpfile(); + } - if (ulas_preproc(preprocdst, ulasin) == -1) { - rc = -1; - goto cleanup; - } + if (ulas_preproc(preprocdst, ulasin) == -1) { + rc = -1; + goto cleanup; + } - if (cfg.preproc_only) { - goto cleanup; + if (ulas.pass > ULAS_PASS_FINAL) { + fclose(preprocdst); + preprocdst = NULL; + rewind(ulasin); + } + ulas.pass -= 1; } cleanup: - if (!cfg.preproc_only) { + if (!cfg.preproc_only && preprocdst) { ulas_fclose(preprocdst); }