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