grep: Colorize selected, context, and function lines
[git/jnareb-git.git] / grep.c
blobb641305ff6e9a05b20d92e9c0274004625bd7f28
1 #include "cache.h"
2 #include "grep.h"
3 #include "userdiff.h"
4 #include "xdiff-interface.h"
6 void append_header_grep_pattern(struct grep_opt *opt, enum grep_header_field field, const char *pat)
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;
19 void append_grep_pattern(struct grep_opt *opt, const char *pat,
20 const char *origin, int no, enum grep_pat_token t)
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;
32 struct grep_opt *grep_opt_dup(const struct grep_opt *opt)
34 struct grep_pat *pat;
35 struct grep_opt *ret = xmalloc(sizeof(struct grep_opt));
36 *ret = *opt;
38 ret->pattern_list = NULL;
39 ret->pattern_tail = &ret->pattern_list;
41 for(pat = opt->pattern_list; pat != NULL; pat = pat->next)
43 if(pat->token == GREP_PATTERN_HEAD)
44 append_header_grep_pattern(ret, pat->field,
45 pat->pattern);
46 else
47 append_grep_pattern(ret, pat->pattern, pat->origin,
48 pat->no, pat->token);
51 return ret;
54 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
56 int err;
58 p->word_regexp = opt->word_regexp;
59 p->ignore_case = opt->ignore_case;
60 p->fixed = opt->fixed;
62 if (p->fixed)
63 return;
65 err = regcomp(&p->regexp, p->pattern, opt->regflags);
66 if (err) {
67 char errbuf[1024];
68 char where[1024];
69 if (p->no)
70 sprintf(where, "In '%s' at %d, ",
71 p->origin, p->no);
72 else if (p->origin)
73 sprintf(where, "%s, ", p->origin);
74 else
75 where[0] = 0;
76 regerror(err, &p->regexp, errbuf, 1024);
77 regfree(&p->regexp);
78 die("%s'%s': %s", where, p->pattern, errbuf);
82 static struct grep_expr *compile_pattern_or(struct grep_pat **);
83 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
85 struct grep_pat *p;
86 struct grep_expr *x;
88 p = *list;
89 if (!p)
90 return NULL;
91 switch (p->token) {
92 case GREP_PATTERN: /* atom */
93 case GREP_PATTERN_HEAD:
94 case GREP_PATTERN_BODY:
95 x = xcalloc(1, sizeof (struct grep_expr));
96 x->node = GREP_NODE_ATOM;
97 x->u.atom = p;
98 *list = p->next;
99 return x;
100 case GREP_OPEN_PAREN:
101 *list = p->next;
102 x = compile_pattern_or(list);
103 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
104 die("unmatched parenthesis");
105 *list = (*list)->next;
106 return x;
107 default:
108 return NULL;
112 static struct grep_expr *compile_pattern_not(struct grep_pat **list)
114 struct grep_pat *p;
115 struct grep_expr *x;
117 p = *list;
118 if (!p)
119 return NULL;
120 switch (p->token) {
121 case GREP_NOT:
122 if (!p->next)
123 die("--not not followed by pattern expression");
124 *list = p->next;
125 x = xcalloc(1, sizeof (struct grep_expr));
126 x->node = GREP_NODE_NOT;
127 x->u.unary = compile_pattern_not(list);
128 if (!x->u.unary)
129 die("--not followed by non pattern expression");
130 return x;
131 default:
132 return compile_pattern_atom(list);
136 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
138 struct grep_pat *p;
139 struct grep_expr *x, *y, *z;
141 x = compile_pattern_not(list);
142 p = *list;
143 if (p && p->token == GREP_AND) {
144 if (!p->next)
145 die("--and not followed by pattern expression");
146 *list = p->next;
147 y = compile_pattern_and(list);
148 if (!y)
149 die("--and not followed by pattern expression");
150 z = xcalloc(1, sizeof (struct grep_expr));
151 z->node = GREP_NODE_AND;
152 z->u.binary.left = x;
153 z->u.binary.right = y;
154 return z;
156 return x;
159 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
161 struct grep_pat *p;
162 struct grep_expr *x, *y, *z;
164 x = compile_pattern_and(list);
165 p = *list;
166 if (x && p && p->token != GREP_CLOSE_PAREN) {
167 y = compile_pattern_or(list);
168 if (!y)
169 die("not a pattern expression %s", p->pattern);
170 z = xcalloc(1, sizeof (struct grep_expr));
171 z->node = GREP_NODE_OR;
172 z->u.binary.left = x;
173 z->u.binary.right = y;
174 return z;
176 return x;
179 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
181 return compile_pattern_or(list);
184 void compile_grep_patterns(struct grep_opt *opt)
186 struct grep_pat *p;
188 if (opt->all_match)
189 opt->extended = 1;
191 for (p = opt->pattern_list; p; p = p->next) {
192 switch (p->token) {
193 case GREP_PATTERN: /* atom */
194 case GREP_PATTERN_HEAD:
195 case GREP_PATTERN_BODY:
196 compile_regexp(p, opt);
197 break;
198 default:
199 opt->extended = 1;
200 break;
204 if (!opt->extended)
205 return;
207 /* Then bundle them up in an expression.
208 * A classic recursive descent parser would do.
210 p = opt->pattern_list;
211 if (p)
212 opt->pattern_expression = compile_pattern_expr(&p);
213 if (p)
214 die("incomplete pattern expression: %s", p->pattern);
217 static void free_pattern_expr(struct grep_expr *x)
219 switch (x->node) {
220 case GREP_NODE_ATOM:
221 break;
222 case GREP_NODE_NOT:
223 free_pattern_expr(x->u.unary);
224 break;
225 case GREP_NODE_AND:
226 case GREP_NODE_OR:
227 free_pattern_expr(x->u.binary.left);
228 free_pattern_expr(x->u.binary.right);
229 break;
231 free(x);
234 void free_grep_patterns(struct grep_opt *opt)
236 struct grep_pat *p, *n;
238 for (p = opt->pattern_list; p; p = n) {
239 n = p->next;
240 switch (p->token) {
241 case GREP_PATTERN: /* atom */
242 case GREP_PATTERN_HEAD:
243 case GREP_PATTERN_BODY:
244 regfree(&p->regexp);
245 break;
246 default:
247 break;
249 free(p);
252 if (!opt->extended)
253 return;
254 free_pattern_expr(opt->pattern_expression);
257 static char *end_of_line(char *cp, unsigned long *left)
259 unsigned long l = *left;
260 while (l && *cp != '\n') {
261 l--;
262 cp++;
264 *left = l;
265 return cp;
268 static int word_char(char ch)
270 return isalnum(ch) || ch == '_';
273 static void output_color(struct grep_opt *opt, const void *data, size_t size,
274 const char *color)
276 if (opt->color && color && color[0]) {
277 opt->output(opt, color, strlen(color));
278 opt->output(opt, data, size);
279 opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
280 } else
281 opt->output(opt, data, size);
284 static void output_sep(struct grep_opt *opt, char sign)
286 if (opt->null_following_name)
287 opt->output(opt, "\0", 1);
288 else
289 output_color(opt, &sign, 1, opt->color_sep);
292 static void show_name(struct grep_opt *opt, const char *name)
294 output_color(opt, name, strlen(name), opt->color_filename);
295 opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
299 static int fixmatch(const char *pattern, char *line, int ignore_case, regmatch_t *match)
301 char *hit;
302 if (ignore_case)
303 hit = strcasestr(line, pattern);
304 else
305 hit = strstr(line, pattern);
307 if (!hit) {
308 match->rm_so = match->rm_eo = -1;
309 return REG_NOMATCH;
311 else {
312 match->rm_so = hit - line;
313 match->rm_eo = match->rm_so + strlen(pattern);
314 return 0;
318 static int strip_timestamp(char *bol, char **eol_p)
320 char *eol = *eol_p;
321 int ch;
323 while (bol < --eol) {
324 if (*eol != '>')
325 continue;
326 *eol_p = ++eol;
327 ch = *eol;
328 *eol = '\0';
329 return ch;
331 return 0;
334 static struct {
335 const char *field;
336 size_t len;
337 } header_field[] = {
338 { "author ", 7 },
339 { "committer ", 10 },
342 static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
343 enum grep_context ctx,
344 regmatch_t *pmatch, int eflags)
346 int hit = 0;
347 int saved_ch = 0;
348 const char *start = bol;
350 if ((p->token != GREP_PATTERN) &&
351 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
352 return 0;
354 if (p->token == GREP_PATTERN_HEAD) {
355 const char *field;
356 size_t len;
357 assert(p->field < ARRAY_SIZE(header_field));
358 field = header_field[p->field].field;
359 len = header_field[p->field].len;
360 if (strncmp(bol, field, len))
361 return 0;
362 bol += len;
363 saved_ch = strip_timestamp(bol, &eol);
366 again:
367 if (p->fixed)
368 hit = !fixmatch(p->pattern, bol, p->ignore_case, pmatch);
369 else
370 hit = !regexec(&p->regexp, bol, 1, pmatch, eflags);
372 if (hit && p->word_regexp) {
373 if ((pmatch[0].rm_so < 0) ||
374 (eol - bol) < pmatch[0].rm_so ||
375 (pmatch[0].rm_eo < 0) ||
376 (eol - bol) < pmatch[0].rm_eo)
377 die("regexp returned nonsense");
379 /* Match beginning must be either beginning of the
380 * line, or at word boundary (i.e. the last char must
381 * not be a word char). Similarly, match end must be
382 * either end of the line, or at word boundary
383 * (i.e. the next char must not be a word char).
385 if ( ((pmatch[0].rm_so == 0) ||
386 !word_char(bol[pmatch[0].rm_so-1])) &&
387 ((pmatch[0].rm_eo == (eol-bol)) ||
388 !word_char(bol[pmatch[0].rm_eo])) )
390 else
391 hit = 0;
393 /* Words consist of at least one character. */
394 if (pmatch->rm_so == pmatch->rm_eo)
395 hit = 0;
397 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
398 /* There could be more than one match on the
399 * line, and the first match might not be
400 * strict word match. But later ones could be!
401 * Forward to the next possible start, i.e. the
402 * next position following a non-word char.
404 bol = pmatch[0].rm_so + bol + 1;
405 while (word_char(bol[-1]) && bol < eol)
406 bol++;
407 eflags |= REG_NOTBOL;
408 if (bol < eol)
409 goto again;
412 if (p->token == GREP_PATTERN_HEAD && saved_ch)
413 *eol = saved_ch;
414 if (hit) {
415 pmatch[0].rm_so += bol - start;
416 pmatch[0].rm_eo += bol - start;
418 return hit;
421 static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
422 enum grep_context ctx, int collect_hits)
424 int h = 0;
425 regmatch_t match;
427 if (!x)
428 die("Not a valid grep expression");
429 switch (x->node) {
430 case GREP_NODE_ATOM:
431 h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
432 break;
433 case GREP_NODE_NOT:
434 h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
435 break;
436 case GREP_NODE_AND:
437 if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
438 return 0;
439 h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
440 break;
441 case GREP_NODE_OR:
442 if (!collect_hits)
443 return (match_expr_eval(x->u.binary.left,
444 bol, eol, ctx, 0) ||
445 match_expr_eval(x->u.binary.right,
446 bol, eol, ctx, 0));
447 h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
448 x->u.binary.left->hit |= h;
449 h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
450 break;
451 default:
452 die("Unexpected node type (internal error) %d", x->node);
454 if (collect_hits)
455 x->hit |= h;
456 return h;
459 static int match_expr(struct grep_opt *opt, char *bol, char *eol,
460 enum grep_context ctx, int collect_hits)
462 struct grep_expr *x = opt->pattern_expression;
463 return match_expr_eval(x, bol, eol, ctx, collect_hits);
466 static int match_line(struct grep_opt *opt, char *bol, char *eol,
467 enum grep_context ctx, int collect_hits)
469 struct grep_pat *p;
470 regmatch_t match;
472 if (opt->extended)
473 return match_expr(opt, bol, eol, ctx, collect_hits);
475 /* we do not call with collect_hits without being extended */
476 for (p = opt->pattern_list; p; p = p->next) {
477 if (match_one_pattern(p, bol, eol, ctx, &match, 0))
478 return 1;
480 return 0;
483 static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
484 enum grep_context ctx,
485 regmatch_t *pmatch, int eflags)
487 regmatch_t match;
489 if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
490 return 0;
491 if (match.rm_so < 0 || match.rm_eo < 0)
492 return 0;
493 if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
494 if (match.rm_so > pmatch->rm_so)
495 return 1;
496 if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
497 return 1;
499 pmatch->rm_so = match.rm_so;
500 pmatch->rm_eo = match.rm_eo;
501 return 1;
504 static int next_match(struct grep_opt *opt, char *bol, char *eol,
505 enum grep_context ctx, regmatch_t *pmatch, int eflags)
507 struct grep_pat *p;
508 int hit = 0;
510 pmatch->rm_so = pmatch->rm_eo = -1;
511 if (bol < eol) {
512 for (p = opt->pattern_list; p; p = p->next) {
513 switch (p->token) {
514 case GREP_PATTERN: /* atom */
515 case GREP_PATTERN_HEAD:
516 case GREP_PATTERN_BODY:
517 hit |= match_next_pattern(p, bol, eol, ctx,
518 pmatch, eflags);
519 break;
520 default:
521 break;
525 return hit;
528 static void show_line(struct grep_opt *opt, char *bol, char *eol,
529 const char *name, unsigned lno, char sign)
531 int rest = eol - bol;
532 char *line_color = NULL;
534 if (opt->pre_context || opt->post_context) {
535 if (opt->last_shown == 0) {
536 if (opt->show_hunk_mark) {
537 output_color(opt, "--", 2, opt->color_sep);
538 opt->output(opt, "\n", 1);
539 } else
540 opt->show_hunk_mark = 1;
541 } else if (lno > opt->last_shown + 1) {
542 output_color(opt, "--", 2, opt->color_sep);
543 opt->output(opt, "\n", 1);
546 opt->last_shown = lno;
548 if (opt->pathname) {
549 output_color(opt, name, strlen(name), opt->color_filename);
550 output_sep(opt, sign);
552 if (opt->linenum) {
553 char buf[32];
554 snprintf(buf, sizeof(buf), "%d", lno);
555 output_color(opt, buf, strlen(buf), opt->color_lineno);
556 output_sep(opt, sign);
558 if (opt->color) {
559 regmatch_t match;
560 enum grep_context ctx = GREP_CONTEXT_BODY;
561 int ch = *eol;
562 int eflags = 0;
564 if (sign == ':')
565 line_color = opt->color_selected;
566 else if (sign == '-')
567 line_color = opt->color_context;
568 else if (sign == '=')
569 line_color = opt->color_function;
570 *eol = '\0';
571 while (next_match(opt, bol, eol, ctx, &match, eflags)) {
572 if (match.rm_so == match.rm_eo)
573 break;
575 output_color(opt, bol, match.rm_so, line_color);
576 output_color(opt, bol + match.rm_so,
577 match.rm_eo - match.rm_so,
578 opt->color_match);
579 bol += match.rm_eo;
580 rest -= match.rm_eo;
581 eflags = REG_NOTBOL;
583 *eol = ch;
585 output_color(opt, bol, rest, line_color);
586 opt->output(opt, "\n", 1);
589 static int match_funcname(struct grep_opt *opt, char *bol, char *eol)
591 xdemitconf_t *xecfg = opt->priv;
592 if (xecfg && xecfg->find_func) {
593 char buf[1];
594 return xecfg->find_func(bol, eol - bol, buf, 1,
595 xecfg->find_func_priv) >= 0;
598 if (bol == eol)
599 return 0;
600 if (isalpha(*bol) || *bol == '_' || *bol == '$')
601 return 1;
602 return 0;
605 static void show_funcname_line(struct grep_opt *opt, const char *name,
606 char *buf, char *bol, unsigned lno)
608 while (bol > buf) {
609 char *eol = --bol;
611 while (bol > buf && bol[-1] != '\n')
612 bol--;
613 lno--;
615 if (lno <= opt->last_shown)
616 break;
618 if (match_funcname(opt, bol, eol)) {
619 show_line(opt, bol, eol, name, lno, '=');
620 break;
625 static void show_pre_context(struct grep_opt *opt, const char *name, char *buf,
626 char *bol, unsigned lno)
628 unsigned cur = lno, from = 1, funcname_lno = 0;
629 int funcname_needed = opt->funcname;
631 if (opt->pre_context < lno)
632 from = lno - opt->pre_context;
633 if (from <= opt->last_shown)
634 from = opt->last_shown + 1;
636 /* Rewind. */
637 while (bol > buf && cur > from) {
638 char *eol = --bol;
640 while (bol > buf && bol[-1] != '\n')
641 bol--;
642 cur--;
643 if (funcname_needed && match_funcname(opt, bol, eol)) {
644 funcname_lno = cur;
645 funcname_needed = 0;
649 /* We need to look even further back to find a function signature. */
650 if (opt->funcname && funcname_needed)
651 show_funcname_line(opt, name, buf, bol, cur);
653 /* Back forward. */
654 while (cur < lno) {
655 char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
657 while (*eol != '\n')
658 eol++;
659 show_line(opt, bol, eol, name, cur, sign);
660 bol = eol + 1;
661 cur++;
665 static int should_lookahead(struct grep_opt *opt)
667 struct grep_pat *p;
669 if (opt->extended)
670 return 0; /* punt for too complex stuff */
671 if (opt->invert)
672 return 0;
673 for (p = opt->pattern_list; p; p = p->next) {
674 if (p->token != GREP_PATTERN)
675 return 0; /* punt for "header only" and stuff */
677 return 1;
680 static int look_ahead(struct grep_opt *opt,
681 unsigned long *left_p,
682 unsigned *lno_p,
683 char **bol_p)
685 unsigned lno = *lno_p;
686 char *bol = *bol_p;
687 struct grep_pat *p;
688 char *sp, *last_bol;
689 regoff_t earliest = -1;
691 for (p = opt->pattern_list; p; p = p->next) {
692 int hit;
693 regmatch_t m;
695 if (p->fixed)
696 hit = !fixmatch(p->pattern, bol, p->ignore_case, &m);
697 else {
698 #ifdef REG_STARTEND
699 m.rm_so = 0;
700 m.rm_eo = *left_p;
701 hit = !regexec(&p->regexp, bol, 1, &m, REG_STARTEND);
702 #else
703 hit = !regexec(&p->regexp, bol, 1, &m, 0);
704 #endif
706 if (!hit || m.rm_so < 0 || m.rm_eo < 0)
707 continue;
708 if (earliest < 0 || m.rm_so < earliest)
709 earliest = m.rm_so;
712 if (earliest < 0) {
713 *bol_p = bol + *left_p;
714 *left_p = 0;
715 return 1;
717 for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
718 ; /* find the beginning of the line */
719 last_bol = sp;
721 for (sp = bol; sp < last_bol; sp++) {
722 if (*sp == '\n')
723 lno++;
725 *left_p -= last_bol - bol;
726 *bol_p = last_bol;
727 *lno_p = lno;
728 return 0;
731 int grep_threads_ok(const struct grep_opt *opt)
733 /* If this condition is true, then we may use the attribute
734 * machinery in grep_buffer_1. The attribute code is not
735 * thread safe, so we disable the use of threads.
737 if (opt->funcname && !opt->unmatch_name_only && !opt->status_only &&
738 !opt->name_only)
739 return 0;
741 /* If we are showing hunk marks, we should not do it for the
742 * first match. The synchronization problem we get for this
743 * constraint is not yet solved, so we disable threading in
744 * this case.
746 if (opt->pre_context || opt->post_context)
747 return 0;
749 return 1;
752 static void std_output(struct grep_opt *opt, const void *buf, size_t size)
754 fwrite(buf, size, 1, stdout);
757 static int grep_buffer_1(struct grep_opt *opt, const char *name,
758 char *buf, unsigned long size, int collect_hits)
760 char *bol = buf;
761 unsigned long left = size;
762 unsigned lno = 1;
763 unsigned last_hit = 0;
764 int binary_match_only = 0;
765 unsigned count = 0;
766 int try_lookahead = 0;
767 enum grep_context ctx = GREP_CONTEXT_HEAD;
768 xdemitconf_t xecfg;
770 opt->last_shown = 0;
772 if (!opt->output)
773 opt->output = std_output;
775 if (buffer_is_binary(buf, size)) {
776 switch (opt->binary) {
777 case GREP_BINARY_DEFAULT:
778 binary_match_only = 1;
779 break;
780 case GREP_BINARY_NOMATCH:
781 return 0; /* Assume unmatch */
782 break;
783 default:
784 break;
788 memset(&xecfg, 0, sizeof(xecfg));
789 if (opt->funcname && !opt->unmatch_name_only && !opt->status_only &&
790 !opt->name_only && !binary_match_only && !collect_hits) {
791 struct userdiff_driver *drv = userdiff_find_by_path(name);
792 if (drv && drv->funcname.pattern) {
793 const struct userdiff_funcname *pe = &drv->funcname;
794 xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
795 opt->priv = &xecfg;
798 try_lookahead = should_lookahead(opt);
800 while (left) {
801 char *eol, ch;
802 int hit;
805 * look_ahead() skips quicly to the line that possibly
806 * has the next hit; don't call it if we need to do
807 * something more than just skipping the current line
808 * in response to an unmatch for the current line. E.g.
809 * inside a post-context window, we will show the current
810 * line as a context around the previous hit when it
811 * doesn't hit.
813 if (try_lookahead
814 && !(last_hit
815 && lno <= last_hit + opt->post_context)
816 && look_ahead(opt, &left, &lno, &bol))
817 break;
818 eol = end_of_line(bol, &left);
819 ch = *eol;
820 *eol = 0;
822 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
823 ctx = GREP_CONTEXT_BODY;
825 hit = match_line(opt, bol, eol, ctx, collect_hits);
826 *eol = ch;
828 if (collect_hits)
829 goto next_line;
831 /* "grep -v -e foo -e bla" should list lines
832 * that do not have either, so inversion should
833 * be done outside.
835 if (opt->invert)
836 hit = !hit;
837 if (opt->unmatch_name_only) {
838 if (hit)
839 return 0;
840 goto next_line;
842 if (hit) {
843 count++;
844 if (opt->status_only)
845 return 1;
846 if (binary_match_only) {
847 opt->output(opt, "Binary file ", 12);
848 output_color(opt, name, strlen(name),
849 opt->color_filename);
850 opt->output(opt, " matches\n", 9);
851 return 1;
853 if (opt->name_only) {
854 show_name(opt, name);
855 return 1;
857 /* Hit at this line. If we haven't shown the
858 * pre-context lines, we would need to show them.
859 * When asked to do "count", this still show
860 * the context which is nonsense, but the user
861 * deserves to get that ;-).
863 if (opt->pre_context)
864 show_pre_context(opt, name, buf, bol, lno);
865 else if (opt->funcname)
866 show_funcname_line(opt, name, buf, bol, lno);
867 if (!opt->count)
868 show_line(opt, bol, eol, name, lno, ':');
869 last_hit = lno;
871 else if (last_hit &&
872 lno <= last_hit + opt->post_context) {
873 /* If the last hit is within the post context,
874 * we need to show this line.
876 show_line(opt, bol, eol, name, lno, '-');
879 next_line:
880 bol = eol + 1;
881 if (!left)
882 break;
883 left--;
884 lno++;
887 if (collect_hits)
888 return 0;
890 if (opt->status_only)
891 return 0;
892 if (opt->unmatch_name_only) {
893 /* We did not see any hit, so we want to show this */
894 show_name(opt, name);
895 return 1;
898 xdiff_clear_find_func(&xecfg);
899 opt->priv = NULL;
901 /* NEEDSWORK:
902 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
903 * which feels mostly useless but sometimes useful. Maybe
904 * make it another option? For now suppress them.
906 if (opt->count && count) {
907 char buf[32];
908 output_color(opt, name, strlen(name), opt->color_filename);
909 output_sep(opt, ':');
910 snprintf(buf, sizeof(buf), "%u\n", count);
911 opt->output(opt, buf, strlen(buf));
913 return !!last_hit;
916 static void clr_hit_marker(struct grep_expr *x)
918 /* All-hit markers are meaningful only at the very top level
919 * OR node.
921 while (1) {
922 x->hit = 0;
923 if (x->node != GREP_NODE_OR)
924 return;
925 x->u.binary.left->hit = 0;
926 x = x->u.binary.right;
930 static int chk_hit_marker(struct grep_expr *x)
932 /* Top level nodes have hit markers. See if they all are hits */
933 while (1) {
934 if (x->node != GREP_NODE_OR)
935 return x->hit;
936 if (!x->u.binary.left->hit)
937 return 0;
938 x = x->u.binary.right;
942 int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size)
945 * we do not have to do the two-pass grep when we do not check
946 * buffer-wide "all-match".
948 if (!opt->all_match)
949 return grep_buffer_1(opt, name, buf, size, 0);
951 /* Otherwise the toplevel "or" terms hit a bit differently.
952 * We first clear hit markers from them.
954 clr_hit_marker(opt->pattern_expression);
955 grep_buffer_1(opt, name, buf, size, 1);
957 if (!chk_hit_marker(opt->pattern_expression))
958 return 0;
960 return grep_buffer_1(opt, name, buf, size, 0);