diff options
author | Jo-Philipp Wich <jo@mein.io> | 2020-09-10 18:37:24 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2020-09-10 18:37:24 +0200 |
commit | 9ea4368ff1e4842286e0e7a6aec58a2c2c799d5b (patch) | |
tree | 47d31fa6d686dd50220edfa505fcc21ce6c860fe /main.c | |
parent | 39164c57d893b02f9a253c661abc2459bcd57e5c (diff) |
treewide: implement default lstrip_blocks and trim_blocks behaviour
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 34 |
1 files changed, 27 insertions, 7 deletions
@@ -38,11 +38,13 @@ print_usage(char *app) { printf( "== Usage ==\n\n" - " # %s [-d] {-i <file> | -s \"utpl script...\"}\n" + " # %s [-d] [-l] [-r] {-i <file> | -s \"utpl script...\"}\n" " -h, --help Print this help\n" " -i file Specify an utpl script to parse\n" " -s \"utpl script...\" Specify an utpl code fragment to parse\n" - " -d Instead of executing the script, dump the resulting AST as dot\n", + " -d Instead of executing the script, dump the resulting AST as dot\n" + " -l Do not strip leading block whitespace\n" + " -r Do not trim trailing block newlines\n", app); } @@ -136,9 +138,8 @@ static void dump(struct ut_state *s, uint32_t off, int level) { #endif /* NDEBUG */ static enum ut_error_type -parse(const char *source, bool dumponly) +parse(struct ut_state *state, const char *source, bool dumponly) { - struct ut_state *state = calloc(1, sizeof(*state)); enum ut_error_type err; char *msg; @@ -173,8 +174,9 @@ parse(const char *source, bool dumponly) int main(int argc, char **argv) { - size_t rlen, tlen = 0; + struct ut_state *state; bool dumponly = false; + size_t rlen, tlen = 0; char buf[1024], *tmp; char *source = NULL; FILE *input = NULL; @@ -186,7 +188,17 @@ main(int argc, char **argv) goto out; } - while ((opt = getopt(argc, argv, "dhi:s:")) != -1) + state = calloc(1, sizeof(*state)); + + if (!state) { + rv = UT_ERROR_OUT_OF_MEMORY; + goto out; + } + + state->lstrip_blocks = 1; + state->trim_blocks = 1; + + while ((opt = getopt(argc, argv, "dhlri:s:")) != -1) { switch (opt) { case 'h': @@ -208,6 +220,14 @@ main(int argc, char **argv) dumponly = true; break; + case 'l': + state->lstrip_blocks = 0; + break; + + case 'r': + state->trim_blocks = 0; + break; + case 's': source = optarg; break; @@ -240,7 +260,7 @@ main(int argc, char **argv) } } - rv = source ? parse(source, dumponly) : 0; + rv = source ? parse(state, source, dumponly) : 0; out: if (input && input != stdin) |