Skip to content

Commit

Permalink
grep: Extract compile_regexp_failed() from compile_regexp()
Browse files Browse the repository at this point in the history
This simplifies compile_regexp() a little and allows re-using error
handling code.

Signed-off-by: Michał Kiedrowicz <michal.kiedrowicz@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Michał Kiedrowicz authored and Junio C Hamano committed May 9, 2011
1 parent 8997da3 commit a30c148
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ struct grep_opt *grep_opt_dup(const struct grep_opt *opt)
return ret;
}

static NORETURN void compile_regexp_failed(const struct grep_pat *p,
const char *error)
{
char where[1024];

if (p->no)
sprintf(where, "In '%s' at %d, ", p->origin, p->no);
else if (p->origin)
sprintf(where, "%s, ", p->origin);
else
where[0] = 0;

die("%s'%s': %s", where, p->pattern, error);
}

static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
{
int err;
Expand All @@ -73,17 +88,9 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
err = regcomp(&p->regexp, p->pattern, opt->regflags);
if (err) {
char errbuf[1024];
char where[1024];
if (p->no)
sprintf(where, "In '%s' at %d, ",
p->origin, p->no);
else if (p->origin)
sprintf(where, "%s, ", p->origin);
else
where[0] = 0;
regerror(err, &p->regexp, errbuf, 1024);
regfree(&p->regexp);
die("%s'%s': %s", where, p->pattern, errbuf);
compile_regexp_failed(p, errbuf);
}
}

Expand Down

0 comments on commit a30c148

Please sign in to comment.