]> git.ipfire.org Git - thirdparty/git.git/blame - grep.c
grep: rip out support for external grep
[thirdparty/git.git] / grep.c
CommitLineData
83b5d2f5 1#include "cache.h"
83b5d2f5 2#include "grep.h"
60ecac98 3#include "userdiff.h"
6bfce93e 4#include "xdiff-interface.h"
83b5d2f5 5
a4d7d2c6
JH
6void append_header_grep_pattern(struct grep_opt *opt, enum grep_header_field field, const char *pat)
7{
8 struct grep_pat *p = xcalloc(1, sizeof(*p));
9 p->pattern = pat;
10 p->origin = "header";
11 p->no = 0;
12 p->token = GREP_PATTERN_HEAD;
13 p->field = field;
14 *opt->pattern_tail = p;
15 opt->pattern_tail = &p->next;
16 p->next = NULL;
17}
18
83b5d2f5
JH
19void append_grep_pattern(struct grep_opt *opt, const char *pat,
20 const char *origin, int no, enum grep_pat_token t)
21{
22 struct grep_pat *p = xcalloc(1, sizeof(*p));
23 p->pattern = pat;
24 p->origin = origin;
25 p->no = no;
26 p->token = t;
27 *opt->pattern_tail = p;
28 opt->pattern_tail = &p->next;
29 p->next = NULL;
30}
31
c822255c
RS
32static int is_fixed(const char *s)
33{
f9b7cce6 34 while (*s && !is_regex_special(*s))
c822255c
RS
35 s++;
36 return !*s;
37}
38
83b5d2f5
JH
39static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
40{
c822255c
RS
41 int err;
42
d7eb527d 43 p->word_regexp = opt->word_regexp;
5183bf67 44 p->ignore_case = opt->ignore_case;
d7eb527d 45
c822255c
RS
46 if (opt->fixed || is_fixed(p->pattern))
47 p->fixed = 1;
48 if (opt->regflags & REG_ICASE)
49 p->fixed = 0;
50 if (p->fixed)
51 return;
52
53 err = regcomp(&p->regexp, p->pattern, opt->regflags);
83b5d2f5
JH
54 if (err) {
55 char errbuf[1024];
56 char where[1024];
57 if (p->no)
58 sprintf(where, "In '%s' at %d, ",
59 p->origin, p->no);
60 else if (p->origin)
61 sprintf(where, "%s, ", p->origin);
62 else
63 where[0] = 0;
64 regerror(err, &p->regexp, errbuf, 1024);
65 regfree(&p->regexp);
66 die("%s'%s': %s", where, p->pattern, errbuf);
67 }
68}
69
0ab7befa 70static struct grep_expr *compile_pattern_or(struct grep_pat **);
83b5d2f5
JH
71static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
72{
73 struct grep_pat *p;
74 struct grep_expr *x;
75
76 p = *list;
c922b01f
LT
77 if (!p)
78 return NULL;
83b5d2f5
JH
79 switch (p->token) {
80 case GREP_PATTERN: /* atom */
480c1ca6
JH
81 case GREP_PATTERN_HEAD:
82 case GREP_PATTERN_BODY:
83b5d2f5
JH
83 x = xcalloc(1, sizeof (struct grep_expr));
84 x->node = GREP_NODE_ATOM;
85 x->u.atom = p;
86 *list = p->next;
87 return x;
88 case GREP_OPEN_PAREN:
89 *list = p->next;
0ab7befa 90 x = compile_pattern_or(list);
83b5d2f5
JH
91 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
92 die("unmatched parenthesis");
93 *list = (*list)->next;
94 return x;
95 default:
96 return NULL;
97 }
98}
99
100static struct grep_expr *compile_pattern_not(struct grep_pat **list)
101{
102 struct grep_pat *p;
103 struct grep_expr *x;
104
105 p = *list;
c922b01f
LT
106 if (!p)
107 return NULL;
83b5d2f5
JH
108 switch (p->token) {
109 case GREP_NOT:
110 if (!p->next)
111 die("--not not followed by pattern expression");
112 *list = p->next;
113 x = xcalloc(1, sizeof (struct grep_expr));
114 x->node = GREP_NODE_NOT;
115 x->u.unary = compile_pattern_not(list);
116 if (!x->u.unary)
117 die("--not followed by non pattern expression");
118 return x;
119 default:
120 return compile_pattern_atom(list);
121 }
122}
123
124static struct grep_expr *compile_pattern_and(struct grep_pat **list)
125{
126 struct grep_pat *p;
127 struct grep_expr *x, *y, *z;
128
129 x = compile_pattern_not(list);
130 p = *list;
131 if (p && p->token == GREP_AND) {
132 if (!p->next)
133 die("--and not followed by pattern expression");
134 *list = p->next;
135 y = compile_pattern_and(list);
136 if (!y)
137 die("--and not followed by pattern expression");
138 z = xcalloc(1, sizeof (struct grep_expr));
139 z->node = GREP_NODE_AND;
140 z->u.binary.left = x;
141 z->u.binary.right = y;
142 return z;
143 }
144 return x;
145}
146
147static struct grep_expr *compile_pattern_or(struct grep_pat **list)
148{
149 struct grep_pat *p;
150 struct grep_expr *x, *y, *z;
151
152 x = compile_pattern_and(list);
153 p = *list;
154 if (x && p && p->token != GREP_CLOSE_PAREN) {
155 y = compile_pattern_or(list);
156 if (!y)
157 die("not a pattern expression %s", p->pattern);
158 z = xcalloc(1, sizeof (struct grep_expr));
159 z->node = GREP_NODE_OR;
160 z->u.binary.left = x;
161 z->u.binary.right = y;
162 return z;
163 }
164 return x;
165}
166
167static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
168{
169 return compile_pattern_or(list);
170}
171
172void compile_grep_patterns(struct grep_opt *opt)
173{
174 struct grep_pat *p;
175
0ab7befa
JH
176 if (opt->all_match)
177 opt->extended = 1;
178
83b5d2f5 179 for (p = opt->pattern_list; p; p = p->next) {
480c1ca6
JH
180 switch (p->token) {
181 case GREP_PATTERN: /* atom */
182 case GREP_PATTERN_HEAD:
183 case GREP_PATTERN_BODY:
c822255c 184 compile_regexp(p, opt);
480c1ca6
JH
185 break;
186 default:
83b5d2f5 187 opt->extended = 1;
480c1ca6
JH
188 break;
189 }
83b5d2f5
JH
190 }
191
192 if (!opt->extended)
193 return;
194
195 /* Then bundle them up in an expression.
196 * A classic recursive descent parser would do.
197 */
198 p = opt->pattern_list;
ba150a3f
MB
199 if (p)
200 opt->pattern_expression = compile_pattern_expr(&p);
83b5d2f5
JH
201 if (p)
202 die("incomplete pattern expression: %s", p->pattern);
203}
204
b48fb5b6
JH
205static void free_pattern_expr(struct grep_expr *x)
206{
207 switch (x->node) {
208 case GREP_NODE_ATOM:
209 break;
210 case GREP_NODE_NOT:
211 free_pattern_expr(x->u.unary);
212 break;
213 case GREP_NODE_AND:
214 case GREP_NODE_OR:
215 free_pattern_expr(x->u.binary.left);
216 free_pattern_expr(x->u.binary.right);
217 break;
218 }
219 free(x);
220}
221
222void free_grep_patterns(struct grep_opt *opt)
223{
224 struct grep_pat *p, *n;
225
226 for (p = opt->pattern_list; p; p = n) {
227 n = p->next;
228 switch (p->token) {
229 case GREP_PATTERN: /* atom */
230 case GREP_PATTERN_HEAD:
231 case GREP_PATTERN_BODY:
232 regfree(&p->regexp);
233 break;
234 default:
235 break;
236 }
237 free(p);
238 }
239
240 if (!opt->extended)
241 return;
242 free_pattern_expr(opt->pattern_expression);
243}
244
83b5d2f5
JH
245static char *end_of_line(char *cp, unsigned long *left)
246{
247 unsigned long l = *left;
248 while (l && *cp != '\n') {
249 l--;
250 cp++;
251 }
252 *left = l;
253 return cp;
254}
255
256static int word_char(char ch)
257{
258 return isalnum(ch) || ch == '_';
259}
260
83caecca
RZ
261static void show_name(struct grep_opt *opt, const char *name)
262{
263 printf("%s%c", name, opt->null_following_name ? '\0' : '\n');
264}
265
5183bf67
BC
266
267static int fixmatch(const char *pattern, char *line, int ignore_case, regmatch_t *match)
83b5d2f5 268{
5183bf67
BC
269 char *hit;
270 if (ignore_case)
271 hit = strcasestr(line, pattern);
272 else
273 hit = strstr(line, pattern);
274
83b5d2f5
JH
275 if (!hit) {
276 match->rm_so = match->rm_eo = -1;
277 return REG_NOMATCH;
278 }
279 else {
280 match->rm_so = hit - line;
281 match->rm_eo = match->rm_so + strlen(pattern);
282 return 0;
283 }
284}
285
a4d7d2c6
JH
286static int strip_timestamp(char *bol, char **eol_p)
287{
288 char *eol = *eol_p;
289 int ch;
290
291 while (bol < --eol) {
292 if (*eol != '>')
293 continue;
294 *eol_p = ++eol;
295 ch = *eol;
296 *eol = '\0';
297 return ch;
298 }
299 return 0;
300}
301
302static struct {
303 const char *field;
304 size_t len;
305} header_field[] = {
306 { "author ", 7 },
307 { "committer ", 10 },
308};
309
d7eb527d 310static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
79212772
RS
311 enum grep_context ctx,
312 regmatch_t *pmatch, int eflags)
83b5d2f5
JH
313{
314 int hit = 0;
a4d7d2c6 315 int saved_ch = 0;
e701fadb 316 const char *start = bol;
83b5d2f5 317
480c1ca6
JH
318 if ((p->token != GREP_PATTERN) &&
319 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
320 return 0;
321
a4d7d2c6
JH
322 if (p->token == GREP_PATTERN_HEAD) {
323 const char *field;
324 size_t len;
325 assert(p->field < ARRAY_SIZE(header_field));
326 field = header_field[p->field].field;
327 len = header_field[p->field].len;
328 if (strncmp(bol, field, len))
329 return 0;
330 bol += len;
331 saved_ch = strip_timestamp(bol, &eol);
332 }
333
83b5d2f5 334 again:
79212772 335 if (p->fixed)
5183bf67 336 hit = !fixmatch(p->pattern, bol, p->ignore_case, pmatch);
79212772
RS
337 else
338 hit = !regexec(&p->regexp, bol, 1, pmatch, eflags);
83b5d2f5 339
d7eb527d 340 if (hit && p->word_regexp) {
83b5d2f5 341 if ((pmatch[0].rm_so < 0) ||
84201eae 342 (eol - bol) < pmatch[0].rm_so ||
83b5d2f5
JH
343 (pmatch[0].rm_eo < 0) ||
344 (eol - bol) < pmatch[0].rm_eo)
345 die("regexp returned nonsense");
346
347 /* Match beginning must be either beginning of the
348 * line, or at word boundary (i.e. the last char must
349 * not be a word char). Similarly, match end must be
350 * either end of the line, or at word boundary
351 * (i.e. the next char must not be a word char).
352 */
fb62eb7f 353 if ( ((pmatch[0].rm_so == 0) ||
83b5d2f5
JH
354 !word_char(bol[pmatch[0].rm_so-1])) &&
355 ((pmatch[0].rm_eo == (eol-bol)) ||
356 !word_char(bol[pmatch[0].rm_eo])) )
357 ;
358 else
359 hit = 0;
360
84201eae
RS
361 /* Words consist of at least one character. */
362 if (pmatch->rm_so == pmatch->rm_eo)
363 hit = 0;
364
83b5d2f5
JH
365 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
366 /* There could be more than one match on the
367 * line, and the first match might not be
368 * strict word match. But later ones could be!
fb62eb7f
RS
369 * Forward to the next possible start, i.e. the
370 * next position following a non-word char.
83b5d2f5
JH
371 */
372 bol = pmatch[0].rm_so + bol + 1;
fb62eb7f
RS
373 while (word_char(bol[-1]) && bol < eol)
374 bol++;
dbb6a4ad 375 eflags |= REG_NOTBOL;
fb62eb7f
RS
376 if (bol < eol)
377 goto again;
83b5d2f5
JH
378 }
379 }
a4d7d2c6
JH
380 if (p->token == GREP_PATTERN_HEAD && saved_ch)
381 *eol = saved_ch;
e701fadb
RS
382 if (hit) {
383 pmatch[0].rm_so += bol - start;
384 pmatch[0].rm_eo += bol - start;
385 }
83b5d2f5
JH
386 return hit;
387}
388
d7eb527d
RS
389static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
390 enum grep_context ctx, int collect_hits)
83b5d2f5 391{
0ab7befa 392 int h = 0;
79212772 393 regmatch_t match;
0ab7befa 394
c922b01f
LT
395 if (!x)
396 die("Not a valid grep expression");
83b5d2f5
JH
397 switch (x->node) {
398 case GREP_NODE_ATOM:
79212772 399 h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
83b5d2f5
JH
400 break;
401 case GREP_NODE_NOT:
d7eb527d 402 h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
0ab7befa 403 break;
83b5d2f5 404 case GREP_NODE_AND:
d7eb527d 405 if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
252d560d 406 return 0;
d7eb527d 407 h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
0ab7befa 408 break;
83b5d2f5 409 case GREP_NODE_OR:
0ab7befa 410 if (!collect_hits)
d7eb527d 411 return (match_expr_eval(x->u.binary.left,
0ab7befa 412 bol, eol, ctx, 0) ||
d7eb527d 413 match_expr_eval(x->u.binary.right,
0ab7befa 414 bol, eol, ctx, 0));
d7eb527d 415 h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
0ab7befa 416 x->u.binary.left->hit |= h;
d7eb527d 417 h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
0ab7befa
JH
418 break;
419 default:
d7530708 420 die("Unexpected node type (internal error) %d", x->node);
83b5d2f5 421 }
0ab7befa
JH
422 if (collect_hits)
423 x->hit |= h;
424 return h;
83b5d2f5
JH
425}
426
480c1ca6 427static int match_expr(struct grep_opt *opt, char *bol, char *eol,
0ab7befa 428 enum grep_context ctx, int collect_hits)
83b5d2f5
JH
429{
430 struct grep_expr *x = opt->pattern_expression;
d7eb527d 431 return match_expr_eval(x, bol, eol, ctx, collect_hits);
83b5d2f5
JH
432}
433
480c1ca6 434static int match_line(struct grep_opt *opt, char *bol, char *eol,
0ab7befa 435 enum grep_context ctx, int collect_hits)
83b5d2f5
JH
436{
437 struct grep_pat *p;
79212772
RS
438 regmatch_t match;
439
83b5d2f5 440 if (opt->extended)
0ab7befa
JH
441 return match_expr(opt, bol, eol, ctx, collect_hits);
442
443 /* we do not call with collect_hits without being extended */
83b5d2f5 444 for (p = opt->pattern_list; p; p = p->next) {
79212772 445 if (match_one_pattern(p, bol, eol, ctx, &match, 0))
83b5d2f5
JH
446 return 1;
447 }
448 return 0;
449}
450
7e8f59d5
RS
451static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
452 enum grep_context ctx,
453 regmatch_t *pmatch, int eflags)
454{
455 regmatch_t match;
456
457 if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
458 return 0;
459 if (match.rm_so < 0 || match.rm_eo < 0)
460 return 0;
461 if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
462 if (match.rm_so > pmatch->rm_so)
463 return 1;
464 if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
465 return 1;
466 }
467 pmatch->rm_so = match.rm_so;
468 pmatch->rm_eo = match.rm_eo;
469 return 1;
470}
471
472static int next_match(struct grep_opt *opt, char *bol, char *eol,
473 enum grep_context ctx, regmatch_t *pmatch, int eflags)
474{
475 struct grep_pat *p;
476 int hit = 0;
477
478 pmatch->rm_so = pmatch->rm_eo = -1;
479 if (bol < eol) {
480 for (p = opt->pattern_list; p; p = p->next) {
481 switch (p->token) {
482 case GREP_PATTERN: /* atom */
483 case GREP_PATTERN_HEAD:
484 case GREP_PATTERN_BODY:
485 hit |= match_next_pattern(p, bol, eol, ctx,
486 pmatch, eflags);
487 break;
488 default:
489 break;
490 }
491 }
492 }
493 return hit;
494}
495
496static void show_line(struct grep_opt *opt, char *bol, char *eol,
497 const char *name, unsigned lno, char sign)
498{
499 int rest = eol - bol;
500
ed24e401 501 if (opt->pre_context || opt->post_context) {
046802d0
RS
502 if (opt->last_shown == 0) {
503 if (opt->show_hunk_mark)
ed24e401 504 fputs("--\n", stdout);
046802d0
RS
505 else
506 opt->show_hunk_mark = 1;
ed24e401
RS
507 } else if (lno > opt->last_shown + 1)
508 fputs("--\n", stdout);
5dd06d38
RS
509 }
510 opt->last_shown = lno;
511
7e8f59d5
RS
512 if (opt->null_following_name)
513 sign = '\0';
514 if (opt->pathname)
515 printf("%s%c", name, sign);
516 if (opt->linenum)
517 printf("%d%c", lno, sign);
518 if (opt->color) {
519 regmatch_t match;
520 enum grep_context ctx = GREP_CONTEXT_BODY;
521 int ch = *eol;
522 int eflags = 0;
523
524 *eol = '\0';
525 while (next_match(opt, bol, eol, ctx, &match, eflags)) {
1f5b9cc4
RS
526 if (match.rm_so == match.rm_eo)
527 break;
7e8f59d5 528 printf("%.*s%s%.*s%s",
747a322b 529 (int)match.rm_so, bol,
7e8f59d5 530 opt->color_match,
747a322b 531 (int)(match.rm_eo - match.rm_so), bol + match.rm_so,
7e8f59d5
RS
532 GIT_COLOR_RESET);
533 bol += match.rm_eo;
534 rest -= match.rm_eo;
535 eflags = REG_NOTBOL;
536 }
537 *eol = ch;
538 }
539 printf("%.*s\n", rest, bol);
540}
541
60ecac98 542static int match_funcname(struct grep_opt *opt, char *bol, char *eol)
2944e4e6 543{
60ecac98
RS
544 xdemitconf_t *xecfg = opt->priv;
545 if (xecfg && xecfg->find_func) {
546 char buf[1];
547 return xecfg->find_func(bol, eol - bol, buf, 1,
548 xecfg->find_func_priv) >= 0;
549 }
550
2944e4e6
RS
551 if (bol == eol)
552 return 0;
553 if (isalpha(*bol) || *bol == '_' || *bol == '$')
554 return 1;
555 return 0;
556}
557
558static void show_funcname_line(struct grep_opt *opt, const char *name,
559 char *buf, char *bol, unsigned lno)
560{
561 while (bol > buf) {
562 char *eol = --bol;
563
564 while (bol > buf && bol[-1] != '\n')
565 bol--;
566 lno--;
567
568 if (lno <= opt->last_shown)
569 break;
570
60ecac98 571 if (match_funcname(opt, bol, eol)) {
2944e4e6
RS
572 show_line(opt, bol, eol, name, lno, '=');
573 break;
574 }
575 }
576}
577
49de3216
RS
578static void show_pre_context(struct grep_opt *opt, const char *name, char *buf,
579 char *bol, unsigned lno)
580{
2944e4e6
RS
581 unsigned cur = lno, from = 1, funcname_lno = 0;
582 int funcname_needed = opt->funcname;
49de3216
RS
583
584 if (opt->pre_context < lno)
585 from = lno - opt->pre_context;
586 if (from <= opt->last_shown)
587 from = opt->last_shown + 1;
588
589 /* Rewind. */
590 while (bol > buf && cur > from) {
2944e4e6
RS
591 char *eol = --bol;
592
49de3216
RS
593 while (bol > buf && bol[-1] != '\n')
594 bol--;
595 cur--;
60ecac98 596 if (funcname_needed && match_funcname(opt, bol, eol)) {
2944e4e6
RS
597 funcname_lno = cur;
598 funcname_needed = 0;
599 }
49de3216
RS
600 }
601
2944e4e6
RS
602 /* We need to look even further back to find a function signature. */
603 if (opt->funcname && funcname_needed)
604 show_funcname_line(opt, name, buf, bol, cur);
605
49de3216
RS
606 /* Back forward. */
607 while (cur < lno) {
2944e4e6 608 char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
49de3216
RS
609
610 while (*eol != '\n')
611 eol++;
2944e4e6 612 show_line(opt, bol, eol, name, cur, sign);
49de3216
RS
613 bol = eol + 1;
614 cur++;
615 }
616}
617
a26345b6
JH
618static int should_lookahead(struct grep_opt *opt)
619{
620 struct grep_pat *p;
621
622 if (opt->extended)
623 return 0; /* punt for too complex stuff */
624 if (opt->invert)
625 return 0;
626 for (p = opt->pattern_list; p; p = p->next) {
627 if (p->token != GREP_PATTERN)
628 return 0; /* punt for "header only" and stuff */
629 }
630 return 1;
631}
632
633static int look_ahead(struct grep_opt *opt,
634 unsigned long *left_p,
635 unsigned *lno_p,
636 char **bol_p)
637{
638 unsigned lno = *lno_p;
639 char *bol = *bol_p;
640 struct grep_pat *p;
641 char *sp, *last_bol;
642 regoff_t earliest = -1;
643
644 for (p = opt->pattern_list; p; p = p->next) {
645 int hit;
646 regmatch_t m;
647
648 if (p->fixed)
e2d2e383 649 hit = !fixmatch(p->pattern, bol, p->ignore_case, &m);
a26345b6
JH
650 else
651 hit = !regexec(&p->regexp, bol, 1, &m, 0);
652 if (!hit || m.rm_so < 0 || m.rm_eo < 0)
653 continue;
654 if (earliest < 0 || m.rm_so < earliest)
655 earliest = m.rm_so;
656 }
657
658 if (earliest < 0) {
659 *bol_p = bol + *left_p;
660 *left_p = 0;
661 return 1;
662 }
663 for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
664 ; /* find the beginning of the line */
665 last_bol = sp;
666
667 for (sp = bol; sp < last_bol; sp++) {
668 if (*sp == '\n')
669 lno++;
670 }
671 *left_p -= last_bol - bol;
672 *bol_p = last_bol;
673 *lno_p = lno;
674 return 0;
675}
676
0ab7befa
JH
677static int grep_buffer_1(struct grep_opt *opt, const char *name,
678 char *buf, unsigned long size, int collect_hits)
83b5d2f5
JH
679{
680 char *bol = buf;
681 unsigned long left = size;
682 unsigned lno = 1;
83b5d2f5 683 unsigned last_hit = 0;
83b5d2f5 684 int binary_match_only = 0;
83b5d2f5 685 unsigned count = 0;
a26345b6 686 int try_lookahead = 0;
480c1ca6 687 enum grep_context ctx = GREP_CONTEXT_HEAD;
60ecac98 688 xdemitconf_t xecfg;
83b5d2f5 689
5dd06d38
RS
690 opt->last_shown = 0;
691
83b5d2f5
JH
692 if (buffer_is_binary(buf, size)) {
693 switch (opt->binary) {
694 case GREP_BINARY_DEFAULT:
695 binary_match_only = 1;
696 break;
697 case GREP_BINARY_NOMATCH:
698 return 0; /* Assume unmatch */
699 break;
700 default:
701 break;
702 }
703 }
704
60ecac98
RS
705 memset(&xecfg, 0, sizeof(xecfg));
706 if (opt->funcname && !opt->unmatch_name_only && !opt->status_only &&
707 !opt->name_only && !binary_match_only && !collect_hits) {
708 struct userdiff_driver *drv = userdiff_find_by_path(name);
709 if (drv && drv->funcname.pattern) {
710 const struct userdiff_funcname *pe = &drv->funcname;
711 xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
712 opt->priv = &xecfg;
713 }
714 }
a26345b6 715 try_lookahead = should_lookahead(opt);
60ecac98 716
83b5d2f5
JH
717 while (left) {
718 char *eol, ch;
0ab7befa 719 int hit;
83b5d2f5 720
a26345b6
JH
721 /*
722 * look_ahead() skips quicly to the line that possibly
723 * has the next hit; don't call it if we need to do
724 * something more than just skipping the current line
725 * in response to an unmatch for the current line. E.g.
726 * inside a post-context window, we will show the current
727 * line as a context around the previous hit when it
728 * doesn't hit.
729 */
730 if (try_lookahead
731 && !(last_hit
732 && lno <= last_hit + opt->post_context)
733 && look_ahead(opt, &left, &lno, &bol))
734 break;
83b5d2f5
JH
735 eol = end_of_line(bol, &left);
736 ch = *eol;
737 *eol = 0;
738
480c1ca6
JH
739 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
740 ctx = GREP_CONTEXT_BODY;
741
0ab7befa 742 hit = match_line(opt, bol, eol, ctx, collect_hits);
83b5d2f5
JH
743 *eol = ch;
744
0ab7befa
JH
745 if (collect_hits)
746 goto next_line;
747
83b5d2f5
JH
748 /* "grep -v -e foo -e bla" should list lines
749 * that do not have either, so inversion should
750 * be done outside.
751 */
752 if (opt->invert)
753 hit = !hit;
754 if (opt->unmatch_name_only) {
755 if (hit)
756 return 0;
757 goto next_line;
758 }
759 if (hit) {
760 count++;
761 if (opt->status_only)
762 return 1;
763 if (binary_match_only) {
764 printf("Binary file %s matches\n", name);
765 return 1;
766 }
767 if (opt->name_only) {
83caecca 768 show_name(opt, name);
83b5d2f5
JH
769 return 1;
770 }
771 /* Hit at this line. If we haven't shown the
772 * pre-context lines, we would need to show them.
773 * When asked to do "count", this still show
774 * the context which is nonsense, but the user
775 * deserves to get that ;-).
776 */
49de3216
RS
777 if (opt->pre_context)
778 show_pre_context(opt, name, buf, bol, lno);
2944e4e6
RS
779 else if (opt->funcname)
780 show_funcname_line(opt, name, buf, bol, lno);
83b5d2f5
JH
781 if (!opt->count)
782 show_line(opt, bol, eol, name, lno, ':');
5dd06d38 783 last_hit = lno;
83b5d2f5
JH
784 }
785 else if (last_hit &&
786 lno <= last_hit + opt->post_context) {
787 /* If the last hit is within the post context,
788 * we need to show this line.
789 */
83b5d2f5 790 show_line(opt, bol, eol, name, lno, '-');
83b5d2f5 791 }
83b5d2f5
JH
792
793 next_line:
794 bol = eol + 1;
795 if (!left)
796 break;
797 left--;
798 lno++;
799 }
800
0ab7befa
JH
801 if (collect_hits)
802 return 0;
b48fb5b6 803
83b5d2f5
JH
804 if (opt->status_only)
805 return 0;
806 if (opt->unmatch_name_only) {
807 /* We did not see any hit, so we want to show this */
83caecca 808 show_name(opt, name);
83b5d2f5
JH
809 return 1;
810 }
811
60ecac98
RS
812 xdiff_clear_find_func(&xecfg);
813 opt->priv = NULL;
814
83b5d2f5
JH
815 /* NEEDSWORK:
816 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
817 * which feels mostly useless but sometimes useful. Maybe
818 * make it another option? For now suppress them.
819 */
820 if (opt->count && count)
83caecca
RZ
821 printf("%s%c%u\n", name,
822 opt->null_following_name ? '\0' : ':', count);
83b5d2f5
JH
823 return !!last_hit;
824}
825
0ab7befa
JH
826static void clr_hit_marker(struct grep_expr *x)
827{
828 /* All-hit markers are meaningful only at the very top level
829 * OR node.
830 */
831 while (1) {
832 x->hit = 0;
833 if (x->node != GREP_NODE_OR)
834 return;
835 x->u.binary.left->hit = 0;
836 x = x->u.binary.right;
837 }
838}
839
840static int chk_hit_marker(struct grep_expr *x)
841{
842 /* Top level nodes have hit markers. See if they all are hits */
843 while (1) {
844 if (x->node != GREP_NODE_OR)
845 return x->hit;
846 if (!x->u.binary.left->hit)
847 return 0;
848 x = x->u.binary.right;
849 }
850}
851
852int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size)
853{
854 /*
855 * we do not have to do the two-pass grep when we do not check
856 * buffer-wide "all-match".
857 */
858 if (!opt->all_match)
859 return grep_buffer_1(opt, name, buf, size, 0);
860
861 /* Otherwise the toplevel "or" terms hit a bit differently.
862 * We first clear hit markers from them.
863 */
864 clr_hit_marker(opt->pattern_expression);
865 grep_buffer_1(opt, name, buf, size, 1);
866
867 if (!chk_hit_marker(opt->pattern_expression))
868 return 0;
869
870 return grep_buffer_1(opt, name, buf, size, 0);
871}