1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-28 12:46:10 +02:00

builtin-grep: allow more than one patterns.

Signed-off-by: Junio C Hamano <junkio@cox.net>
This commit is contained in:
Junio C Hamano 2006-05-02 15:40:49 -07:00
parent f462ebb48b
commit f9b9faf6f8

View File

@ -75,9 +75,16 @@ static int pathspec_matches(const char **paths, const char *name)
return 0; return 0;
} }
struct grep_opt { struct grep_pat {
struct grep_pat *next;
const char *pattern; const char *pattern;
regex_t regexp; regex_t regexp;
};
struct grep_opt {
struct grep_pat *pattern_list;
struct grep_pat **pattern_tail;
regex_t regexp;
unsigned linenum:1; unsigned linenum:1;
unsigned invert:1; unsigned invert:1;
unsigned name_only:1; unsigned name_only:1;
@ -86,6 +93,29 @@ struct grep_opt {
unsigned post_context; unsigned post_context;
}; };
static void add_pattern(struct grep_opt *opt, const char *pat)
{
struct grep_pat *p = xcalloc(1, sizeof(*p));
p->pattern = pat;
*opt->pattern_tail = p;
opt->pattern_tail = &p->next;
p->next = NULL;
}
static void compile_patterns(struct grep_opt *opt)
{
struct grep_pat *p;
for (p = opt->pattern_list; p; p = p->next) {
int err = regcomp(&p->regexp, p->pattern, opt->regflags);
if (err) {
char errbuf[1024];
regerror(err, &p->regexp, errbuf, 1024);
regfree(&p->regexp);
die("'%s': %s", p->pattern, errbuf);
}
}
}
static char *end_of_line(char *cp, unsigned long *left) static char *end_of_line(char *cp, unsigned long *left)
{ {
unsigned long l = *left; unsigned long l = *left;
@ -128,14 +158,24 @@ static int grep_buffer(struct grep_opt *opt, const char *name,
while (left) { while (left) {
regmatch_t pmatch[10]; regmatch_t pmatch[10];
char *eol, ch; char *eol, ch;
int hit; int hit = 0;
struct grep_pat *p;
eol = end_of_line(bol, &left); eol = end_of_line(bol, &left);
ch = *eol; ch = *eol;
*eol = 0; *eol = 0;
hit = !regexec(&opt->regexp, bol, ARRAY_SIZE(pmatch), for (p = opt->pattern_list; p; p = p->next) {
pmatch, 0); regex_t *exp = &p->regexp;
hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
pmatch, 0);
if (hit)
break;
}
/* "grep -v -e foo -e bla" should list lines
* that do not have either, so inversion should
* be done outside.
*/
if (opt->invert) if (opt->invert)
hit = !hit; hit = !hit;
if (hit) { if (hit) {
@ -344,7 +384,6 @@ static const char builtin_grep_usage[] =
int cmd_grep(int argc, const char **argv, char **envp) int cmd_grep(int argc, const char **argv, char **envp)
{ {
int err;
int hit = 0; int hit = 0;
int no_more_flags = 0; int no_more_flags = 0;
int seen_noncommit = 0; int seen_noncommit = 0;
@ -355,6 +394,7 @@ int cmd_grep(int argc, const char **argv, char **envp)
const char **paths = NULL; const char **paths = NULL;
memset(&opt, 0, sizeof(opt)); memset(&opt, 0, sizeof(opt));
opt.pattern_tail = &opt.pattern_list;
opt.regflags = REG_NEWLINE; opt.regflags = REG_NEWLINE;
/* /*
@ -440,12 +480,8 @@ int cmd_grep(int argc, const char **argv, char **envp)
} }
if (!strcmp("-e", arg)) { if (!strcmp("-e", arg)) {
if (1 < argc) { if (1 < argc) {
/* We probably would want to do add_pattern(&opt, argv[1]);
* -e pat1 -e pat2 as well later... argv++;
*/
if (opt.pattern)
die("more than one pattern?");
opt.pattern = *++argv;
argc--; argc--;
continue; continue;
} }
@ -458,8 +494,8 @@ int cmd_grep(int argc, const char **argv, char **envp)
/* Either unrecognized option or a single pattern */ /* Either unrecognized option or a single pattern */
if (!no_more_flags && *arg == '-') if (!no_more_flags && *arg == '-')
usage(builtin_grep_usage); usage(builtin_grep_usage);
if (!opt.pattern) { if (!opt.pattern_list) {
opt.pattern = arg; add_pattern(&opt, arg);
break; break;
} }
else { else {
@ -471,15 +507,9 @@ int cmd_grep(int argc, const char **argv, char **envp)
break; break;
} }
} }
if (!opt.pattern) if (!opt.pattern_list)
die("no pattern given."); die("no pattern given.");
err = regcomp(&opt.regexp, opt.pattern, opt.regflags); compile_patterns(&opt);
if (err) {
char errbuf[1024];
regerror(err, &opt.regexp, errbuf, 1024);
regfree(&opt.regexp);
die("'%s': %s", opt.pattern, errbuf);
}
tail = &object_list; tail = &object_list;
while (1 < argc) { while (1 < argc) {
struct object *object; struct object *object;