dag

Djmoch's Auto Generator
git clone git://git.danielmoch.com/dag.git
Log | Files | Refs | README | LICENSE

commit 8f7b8d94b22fd0b933e5c61ad867aecadc9147b0
parent d8d5df2fdebc1a5554b0625ac81d6594f6382f0e
Author: Daniel Moch <daniel@danielmoch.com>
Date:   Sat, 23 Jan 2021 20:39:27 -0500

Add free_dagfile

Diffstat:
Mconfig.mk | 2+-
Mdag.c | 14+++++++-------
Mdagfile.c | 43+++++++++++++++++++++++++++++++++++++++++++
Mdagfile.h | 4++++
Mparse.y | 11+++++++----
5 files changed, 62 insertions(+), 12 deletions(-)

diff --git a/config.mk b/config.mk @@ -19,5 +19,5 @@ LIBS = -ll -ly VERSION = 0.1.0dev0 CPPFLAGS := -DVERSION=\"${VERSION}\" -CFLAGS := -std=c99 -pedantic-errors -Wall -Wextra -Werror -O0 -g -c -pipe +CFLAGS := -std=c99 -pedantic-errors -Wall -Wextra -Werror -O1 -g -c -pipe LDFLAGS := ${LIBS} diff --git a/dag.c b/dag.c @@ -9,9 +9,6 @@ #include "dagfile.h" #include "string.h" -int parse_dagfile(struct dagfile *dagfile, FILE *path); -void debug_dagfile(struct dagfile *df); - static char *argv0; enum { @@ -46,7 +43,8 @@ main(int argc, char **argv) int verbose = 0; FILE *df; argv0 = basename(argv[0]); - struct dagfile dagfile = { NULL }; + struct dagfile *dagfile; + int retval; while ((ch = getopt(argc, argv, "f:hvV")) != -1) { switch (ch) { @@ -78,16 +76,18 @@ main(int argc, char **argv) fprintf(stderr, "%s: error opening %s\n", argv0, path); exit(ERR_FILE); } - if (parse_dagfile(&dagfile, df) != 0) { + if ((dagfile = parse_dagfile(df)) == NULL) { fprintf(stderr, "%s: error parsing %s\n", argv0, path); exit(ERR_PARSE); } fclose(df); if (verbose) { - debug_dagfile(&dagfile); + debug_dagfile(dagfile); } /* TODO - pledge and unveil */ - return process_dagfile(&dagfile); + retval = process_dagfile(dagfile); + + free_dagfile(dagfile); } diff --git a/dagfile.c b/dagfile.c @@ -64,6 +64,49 @@ process_dagfile(struct dagfile *df) return 0; } +void +free_dagfile(struct dagfile *df) +{ + struct source *s = df->target->sources; + while (s != NULL) { + struct source *snext = s->next; + struct extension *e = s->extensions; + while (e != NULL) { + struct extension *enext = e->next; + struct suffix *sfx = e->suffixes; + while (sfx != NULL) { + struct suffix *sfxnext = sfx->next; + struct requirement *r = sfx->requirements; + struct filter *f = sfx->filters; + while (r != NULL) { + struct requirement *rnext = r->next; + free(r->path); + free(r); + r = rnext; + } + while (f != NULL) { + struct filter *fnext = f->next; + free(f->cmd); + free(f); + f = fnext; + } + free(sfx->value); + free(sfx); + sfx = sfxnext; + } + free(e->value); + free(e); + e = enext; + } + free(s->path); + free(s); + s = snext; + } + free(df->target->path); + free(df->target); + free(df); +} + static void process_source(char *file) { diff --git a/dagfile.h b/dagfile.h @@ -42,5 +42,9 @@ struct dagfile { }; int process_dagfile(struct dagfile *df); +void free_dagfile(struct dagfile *df); +struct dagfile *parse_dagfile(FILE *path); +void debug_dagfile(struct dagfile *df); + #endif diff --git a/parse.y b/parse.y @@ -80,16 +80,19 @@ filter: FILTER STRING { push_filter($2); } %% -int -parse_dagfile(struct dagfile *df, FILE *file) +struct dagfile * +parse_dagfile(FILE *file) { - dagfile = df; + dagfile = malloc(sizeof(struct dagfile)); dagfile->target = NULL; t = NULL; e = NULL; s = NULL; yyin = file; - return yyparse(); + if (yyparse() != 0) + return NULL; + else + return dagfile; } void