@matheusbsbs / matheus-bagliano / commits / f9b9faf6f8
Fork 0

builtin-grep: allow more than one patterns.

Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed May 2, 2006 at 15:40 UTC f9b9faf6f8a408b7129e2c95234f698da2171f4b
1 file changed +51 -21
builtin-grep.c
+51 -21
@@ -75,9 +75,16 @@ static int pathspec_matches(const char **paths, const char *name)
75 return 0;
76 }
77
78 -struct grep_opt {
78 +struct grep_pat {
79 + struct grep_pat *next;
80 const char *pattern;
81 regex_t regexp;
82 +};
83 +
84 +struct grep_opt {
85 + struct grep_pat *pattern_list;
86 + struct grep_pat **pattern_tail;
87 + regex_t regexp;
88 unsigned linenum:1;
89 unsigned invert:1;
90 unsigned name_only:1;
@@ -86,6 +93,29 @@ struct grep_opt {
93 unsigned post_context;
94 };
95
96 +static void add_pattern(struct grep_opt *opt, const char *pat)
97 +{
98 + struct grep_pat *p = xcalloc(1, sizeof(*p));
99 + p->pattern = pat;
100 + *opt->pattern_tail = p;
101 + opt->pattern_tail = &p->next;
102 + p->next = NULL;
103 +}
104 +
105 +static void compile_patterns(struct grep_opt *opt)
106 +{
107 + struct grep_pat *p;
108 + for (p = opt->pattern_list; p; p = p->next) {
109 + int err = regcomp(&p->regexp, p->pattern, opt->regflags);
110 + if (err) {
111 + char errbuf[1024];
112 + regerror(err, &p->regexp, errbuf, 1024);
113 + regfree(&p->regexp);
114 + die("'%s': %s", p->pattern, errbuf);
115 + }
116 + }
117 +}
118 +
119 static char *end_of_line(char *cp, unsigned long *left)
120 {
121 unsigned long l = *left;
@@ -128,14 +158,24 @@ static int grep_buffer(struct grep_opt *opt, const char *name,
158 while (left) {
159 regmatch_t pmatch[10];
160 char *eol, ch;
131 - int hit;
161 + int hit = 0;
162 + struct grep_pat *p;
163
164 eol = end_of_line(bol, &left);
165 ch = *eol;
166 *eol = 0;
167
137 - hit = !regexec(&opt->regexp, bol, ARRAY_SIZE(pmatch),
138 - pmatch, 0);
168 + for (p = opt->pattern_list; p; p = p->next) {
169 + regex_t *exp = &p->regexp;
170 + hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
171 + pmatch, 0);
172 + if (hit)
173 + break;
174 + }
175 + /* "grep -v -e foo -e bla" should list lines
176 + * that do not have either, so inversion should
177 + * be done outside.
178 + */
179 if (opt->invert)
180 hit = !hit;
181 if (hit) {
@@ -344,7 +384,6 @@ static const char builtin_grep_usage[] =
384
385 int cmd_grep(int argc, const char **argv, char **envp)
386 {
347 - int err;
387 int hit = 0;
388 int no_more_flags = 0;
389 int seen_noncommit = 0;
@@ -355,6 +394,7 @@ int cmd_grep(int argc, const char **argv, char **envp)
394 const char **paths = NULL;
395
396 memset(&opt, 0, sizeof(opt));
397 + opt.pattern_tail = &opt.pattern_list;
398 opt.regflags = REG_NEWLINE;
399
400 /*
@@ -440,12 +480,8 @@ int cmd_grep(int argc, const char **argv, char **envp)
480 }
481 if (!strcmp("-e", arg)) {
482 if (1 < argc) {
443 - /* We probably would want to do
444 - * -e pat1 -e pat2 as well later...
445 - */
446 - if (opt.pattern)
447 - die("more than one pattern?");
448 - opt.pattern = *++argv;
483 + add_pattern(&opt, argv[1]);
484 + argv++;
485 argc--;
486 continue;
487 }
@@ -458,8 +494,8 @@ int cmd_grep(int argc, const char **argv, char **envp)
494 /* Either unrecognized option or a single pattern */
495 if (!no_more_flags && *arg == '-')
496 usage(builtin_grep_usage);
461 - if (!opt.pattern) {
462 - opt.pattern = arg;
497 + if (!opt.pattern_list) {
498 + add_pattern(&opt, arg);
499 break;
500 }
501 else {
@@ -471,15 +507,9 @@ int cmd_grep(int argc, const char **argv, char **envp)
507 break;
508 }
509 }
474 - if (!opt.pattern)
510 + if (!opt.pattern_list)
511 die("no pattern given.");
476 - err = regcomp(&opt.regexp, opt.pattern, opt.regflags);
477 - if (err) {
478 - char errbuf[1024];
479 - regerror(err, &opt.regexp, errbuf, 1024);
480 - regfree(&opt.regexp);
481 - die("'%s': %s", opt.pattern, errbuf);
482 - }
512 + compile_patterns(&opt);
513 tail = &object_list;
514 while (1 < argc) {
515 struct object *object;