From da1010f92a2c845c251d3c208a4582b2ceb799e4 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Sun, 3 Jun 2012 10:10:47 -0300 Subject: [PATCH] b2/: add regular expression conversion debugging (option -R) --- b2/boom.c | 15 +++++++++++++++ b2/lang.y | 2 +- b2/subst.c | 7 +++++-- b2/subst.h | 2 +- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/b2/boom.c b/b2/boom.c index 68c5239..52d0ad9 100644 --- a/b2/boom.c +++ b/b2/boom.c @@ -75,6 +75,15 @@ static void do_substitutions(void) } +static void parse_re(const char *re) +{ + char *res; + + subst_match(NULL, re, &res); + printf("%s\n", res); +} + + static void dump_hierarchy(struct action act) { fields_dump(stderr, act.fields); @@ -124,6 +133,7 @@ static void usage(const char *name) " -N name for the next file, override the name in diagnostics\n" " -q var=value ... run substitutions with the specified inputs\n" " -Q var=value ... run substitutions and then do parametric search\n" +" -R regex parse and print regular expression\n" , name); exit(1); } @@ -178,6 +188,11 @@ int main(int argc, char **argv) process = add_var; query = 1; select_parts = 1; + } else if (!strcmp(argv[i], "-R")) { + i++; + if (!argv[i]) + usage(*argv); + parse_re(argv[i]); } else usage(*argv); } diff --git a/b2/lang.y b/b2/lang.y index 0ae909a..92f7fec 100644 --- a/b2/lang.y +++ b/b2/lang.y @@ -619,7 +619,7 @@ block: if ($4) { if ($2 != rel_eq) yyerror("only = allow for matching"); - $$ = subst_match($1, $3); + $$ = subst_match($1, $3, NULL); $$->u.match.block = $4; } else { $$ = subst_assign($1, $2, $3); diff --git a/b2/subst.c b/b2/subst.c index a6f10db..f4e063c 100644 --- a/b2/subst.c +++ b/b2/subst.c @@ -113,7 +113,7 @@ static char *prepare_re(const char *re, char *units) } -struct subst *subst_match(const char *src, const char *re) +struct subst *subst_match(const char *src, const char *re, char **res) { char error[1000]; struct subst *sub; @@ -124,7 +124,10 @@ struct subst *subst_match(const char *src, const char *re) sub->u.match.src = src; tmp = prepare_re(re, sub->u.match.units); err = regcomp(&sub->u.match.re, tmp, REG_EXTENDED); - free(tmp); + if (res) + *res = tmp; + else + free(tmp); if (err) { regerror(err, &sub->u.match.re, error, sizeof(error)); yyerrorf("%s", error); diff --git a/b2/subst.h b/b2/subst.h index 0406df3..bede5c6 100644 --- a/b2/subst.h +++ b/b2/subst.h @@ -76,7 +76,7 @@ struct subst { extern const char *fn; -struct subst *subst_match(const char *src, const char *re); +struct subst *subst_match(const char *src, const char *re, char **res); struct subst *subst_assign(const char *dst, enum relop op, const char *pat); struct subst *subst_end(void); struct subst *subst_ignore(void);