4 #include "xdiff-interface.h"
6 static struct grep_pat *create_grep_pat(const char *pat, size_t patlen,
7 const char *origin, int no,
9 enum grep_header_field field)
11 struct grep_pat *p = xcalloc(1, sizeof(*p));
12 p->pattern = xmemdupz(pat, patlen);
13 p->patternlen = patlen;
21 static void do_append_grep_pat(struct grep_pat ***tail, struct grep_pat *p)
28 case GREP_PATTERN: /* atom */
29 case GREP_PATTERN_HEAD:
30 case GREP_PATTERN_BODY:
32 struct grep_pat *new_pat;
34 char *cp = p->pattern + p->patternlen, *nl = NULL;
35 while (++len <= p->patternlen) {
36 if (*(--cp) == '\n') {
43 new_pat = create_grep_pat(nl + 1, len - 1, p->origin,
44 p->no, p->token, p->field);
45 new_pat->next = p->next;
47 *tail = &new_pat->next;
58 void append_header_grep_pattern(struct grep_opt *opt,
59 enum grep_header_field field, const char *pat)
61 struct grep_pat *p = create_grep_pat(pat, strlen(pat), "header", 0,
62 GREP_PATTERN_HEAD, field);
63 do_append_grep_pat(&opt->header_tail, p);
66 void append_grep_pattern(struct grep_opt *opt, const char *pat,
67 const char *origin, int no, enum grep_pat_token t)
69 append_grep_pat(opt, pat, strlen(pat), origin, no, t);
72 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen,
73 const char *origin, int no, enum grep_pat_token t)
75 struct grep_pat *p = create_grep_pat(pat, patlen, origin, no, t, 0);
76 do_append_grep_pat(&opt->pattern_tail, p);
79 struct grep_opt *grep_opt_dup(const struct grep_opt *opt)
82 struct grep_opt *ret = xmalloc(sizeof(struct grep_opt));
85 ret->pattern_list = NULL;
86 ret->pattern_tail = &ret->pattern_list;
88 for(pat = opt->pattern_list; pat != NULL; pat = pat->next)
90 if(pat->token == GREP_PATTERN_HEAD)
91 append_header_grep_pattern(ret, pat->field,
94 append_grep_pat(ret, pat->pattern, pat->patternlen,
95 pat->origin, pat->no, pat->token);
101 static NORETURN void compile_regexp_failed(const struct grep_pat *p,
107 sprintf(where, "In '%s' at %d, ", p->origin, p->no);
109 sprintf(where, "%s, ", p->origin);
113 die("%s'%s': %s", where, p->pattern, error);
117 static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
121 int options = PCRE_MULTILINE;
123 if (opt->ignore_case)
124 options |= PCRE_CASELESS;
126 p->pcre_regexp = pcre_compile(p->pattern, options, &error, &erroffset,
129 compile_regexp_failed(p, error);
131 p->pcre_extra_info = pcre_study(p->pcre_regexp, 0, &error);
132 if (!p->pcre_extra_info && error)
136 static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
137 regmatch_t *match, int eflags)
139 int ovector[30], ret, flags = 0;
141 if (eflags & REG_NOTBOL)
142 flags |= PCRE_NOTBOL;
144 ret = pcre_exec(p->pcre_regexp, p->pcre_extra_info, line, eol - line,
145 0, flags, ovector, ARRAY_SIZE(ovector));
146 if (ret < 0 && ret != PCRE_ERROR_NOMATCH)
147 die("pcre_exec failed with error code %d", ret);
150 match->rm_so = ovector[0];
151 match->rm_eo = ovector[1];
157 static void free_pcre_regexp(struct grep_pat *p)
159 pcre_free(p->pcre_regexp);
160 pcre_free(p->pcre_extra_info);
162 #else /* !USE_LIBPCRE */
163 static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
165 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
168 static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
169 regmatch_t *match, int eflags)
174 static void free_pcre_regexp(struct grep_pat *p)
177 #endif /* !USE_LIBPCRE */
179 static int is_fixed(const char *s, size_t len)
183 /* regcomp cannot accept patterns with NULs so we
184 * consider any pattern containing a NUL fixed.
186 if (memchr(s, 0, len))
189 for (i = 0; i < len; i++) {
190 if (is_regex_special(s[i]))
197 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
201 p->word_regexp = opt->word_regexp;
202 p->ignore_case = opt->ignore_case;
204 if (opt->fixed || is_fixed(p->pattern, p->patternlen))
210 if (opt->regflags & REG_ICASE || p->ignore_case)
211 p->kws = kwsalloc(tolower_trans_tbl);
213 p->kws = kwsalloc(NULL);
214 kwsincr(p->kws, p->pattern, p->patternlen);
220 compile_pcre_regexp(p, opt);
224 err = regcomp(&p->regexp, p->pattern, opt->regflags);
227 regerror(err, &p->regexp, errbuf, 1024);
229 compile_regexp_failed(p, errbuf);
233 static struct grep_expr *compile_pattern_or(struct grep_pat **);
234 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
243 case GREP_PATTERN: /* atom */
244 case GREP_PATTERN_HEAD:
245 case GREP_PATTERN_BODY:
246 x = xcalloc(1, sizeof (struct grep_expr));
247 x->node = GREP_NODE_ATOM;
251 case GREP_OPEN_PAREN:
253 x = compile_pattern_or(list);
254 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
255 die("unmatched parenthesis");
256 *list = (*list)->next;
263 static struct grep_expr *compile_pattern_not(struct grep_pat **list)
274 die("--not not followed by pattern expression");
276 x = xcalloc(1, sizeof (struct grep_expr));
277 x->node = GREP_NODE_NOT;
278 x->u.unary = compile_pattern_not(list);
280 die("--not followed by non pattern expression");
283 return compile_pattern_atom(list);
287 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
290 struct grep_expr *x, *y, *z;
292 x = compile_pattern_not(list);
294 if (p && p->token == GREP_AND) {
296 die("--and not followed by pattern expression");
298 y = compile_pattern_and(list);
300 die("--and not followed by pattern expression");
301 z = xcalloc(1, sizeof (struct grep_expr));
302 z->node = GREP_NODE_AND;
303 z->u.binary.left = x;
304 z->u.binary.right = y;
310 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
313 struct grep_expr *x, *y, *z;
315 x = compile_pattern_and(list);
317 if (x && p && p->token != GREP_CLOSE_PAREN) {
318 y = compile_pattern_or(list);
320 die("not a pattern expression %s", p->pattern);
321 z = xcalloc(1, sizeof (struct grep_expr));
322 z->node = GREP_NODE_OR;
323 z->u.binary.left = x;
324 z->u.binary.right = y;
330 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
332 return compile_pattern_or(list);
335 static struct grep_expr *grep_true_expr(void)
337 struct grep_expr *z = xcalloc(1, sizeof(*z));
338 z->node = GREP_NODE_TRUE;
342 static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
344 struct grep_expr *z = xcalloc(1, sizeof(*z));
345 z->node = GREP_NODE_OR;
346 z->u.binary.left = left;
347 z->u.binary.right = right;
351 static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
354 struct grep_expr *header_expr;
355 struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]);
356 enum grep_header_field fld;
358 if (!opt->header_list)
361 for (p = opt->header_list; p; p = p->next) {
362 if (p->token != GREP_PATTERN_HEAD)
363 die("bug: a non-header pattern in grep header list.");
364 if (p->field < 0 || GREP_HEADER_FIELD_MAX <= p->field)
365 die("bug: unknown header field %d", p->field);
366 compile_regexp(p, opt);
369 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++)
370 header_group[fld] = NULL;
372 for (p = opt->header_list; p; p = p->next) {
374 struct grep_pat *pp = p;
376 h = compile_pattern_atom(&pp);
377 if (!h || pp != p->next)
378 die("bug: malformed header expr");
379 if (!header_group[p->field]) {
380 header_group[p->field] = h;
383 header_group[p->field] = grep_or_expr(h, header_group[p->field]);
388 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) {
389 if (!header_group[fld])
392 header_expr = grep_true_expr();
393 header_expr = grep_or_expr(header_group[fld], header_expr);
398 void compile_grep_patterns(struct grep_opt *opt)
401 struct grep_expr *header_expr = prep_header_patterns(opt);
403 for (p = opt->pattern_list; p; p = p->next) {
405 case GREP_PATTERN: /* atom */
406 case GREP_PATTERN_HEAD:
407 case GREP_PATTERN_BODY:
408 compile_regexp(p, opt);
416 if (opt->all_match || header_expr)
418 else if (!opt->extended)
421 p = opt->pattern_list;
423 opt->pattern_expression = compile_pattern_expr(&p);
425 die("incomplete pattern expression: %s", p->pattern);
430 if (!opt->pattern_expression)
431 opt->pattern_expression = header_expr;
433 opt->pattern_expression = grep_or_expr(opt->pattern_expression,
438 static void free_pattern_expr(struct grep_expr *x)
445 free_pattern_expr(x->u.unary);
449 free_pattern_expr(x->u.binary.left);
450 free_pattern_expr(x->u.binary.right);
456 void free_grep_patterns(struct grep_opt *opt)
458 struct grep_pat *p, *n;
460 for (p = opt->pattern_list; p; p = n) {
463 case GREP_PATTERN: /* atom */
464 case GREP_PATTERN_HEAD:
465 case GREP_PATTERN_BODY:
468 else if (p->pcre_regexp)
482 free_pattern_expr(opt->pattern_expression);
485 static char *end_of_line(char *cp, unsigned long *left)
487 unsigned long l = *left;
488 while (l && *cp != '\n') {
496 static int word_char(char ch)
498 return isalnum(ch) || ch == '_';
501 static void output_color(struct grep_opt *opt, const void *data, size_t size,
504 if (want_color(opt->color) && color && color[0]) {
505 opt->output(opt, color, strlen(color));
506 opt->output(opt, data, size);
507 opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
509 opt->output(opt, data, size);
512 static void output_sep(struct grep_opt *opt, char sign)
514 if (opt->null_following_name)
515 opt->output(opt, "\0", 1);
517 output_color(opt, &sign, 1, opt->color_sep);
520 static void show_name(struct grep_opt *opt, const char *name)
522 output_color(opt, name, strlen(name), opt->color_filename);
523 opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
526 static int fixmatch(struct grep_pat *p, char *line, char *eol,
529 struct kwsmatch kwsm;
530 size_t offset = kwsexec(p->kws, line, eol - line, &kwsm);
532 match->rm_so = match->rm_eo = -1;
535 match->rm_so = offset;
536 match->rm_eo = match->rm_so + kwsm.size[0];
541 static int regmatch(const regex_t *preg, char *line, char *eol,
542 regmatch_t *match, int eflags)
546 match->rm_eo = eol - line;
547 eflags |= REG_STARTEND;
549 return regexec(preg, line, 1, match, eflags);
552 static int patmatch(struct grep_pat *p, char *line, char *eol,
553 regmatch_t *match, int eflags)
558 hit = !fixmatch(p, line, eol, match);
559 else if (p->pcre_regexp)
560 hit = !pcrematch(p, line, eol, match, eflags);
562 hit = !regmatch(&p->regexp, line, eol, match, eflags);
567 static int strip_timestamp(char *bol, char **eol_p)
572 while (bol < --eol) {
588 { "committer ", 10 },
591 static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
592 enum grep_context ctx,
593 regmatch_t *pmatch, int eflags)
597 const char *start = bol;
599 if ((p->token != GREP_PATTERN) &&
600 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
603 if (p->token == GREP_PATTERN_HEAD) {
606 assert(p->field < ARRAY_SIZE(header_field));
607 field = header_field[p->field].field;
608 len = header_field[p->field].len;
609 if (strncmp(bol, field, len))
612 saved_ch = strip_timestamp(bol, &eol);
616 hit = patmatch(p, bol, eol, pmatch, eflags);
618 if (hit && p->word_regexp) {
619 if ((pmatch[0].rm_so < 0) ||
620 (eol - bol) < pmatch[0].rm_so ||
621 (pmatch[0].rm_eo < 0) ||
622 (eol - bol) < pmatch[0].rm_eo)
623 die("regexp returned nonsense");
625 /* Match beginning must be either beginning of the
626 * line, or at word boundary (i.e. the last char must
627 * not be a word char). Similarly, match end must be
628 * either end of the line, or at word boundary
629 * (i.e. the next char must not be a word char).
631 if ( ((pmatch[0].rm_so == 0) ||
632 !word_char(bol[pmatch[0].rm_so-1])) &&
633 ((pmatch[0].rm_eo == (eol-bol)) ||
634 !word_char(bol[pmatch[0].rm_eo])) )
639 /* Words consist of at least one character. */
640 if (pmatch->rm_so == pmatch->rm_eo)
643 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
644 /* There could be more than one match on the
645 * line, and the first match might not be
646 * strict word match. But later ones could be!
647 * Forward to the next possible start, i.e. the
648 * next position following a non-word char.
650 bol = pmatch[0].rm_so + bol + 1;
651 while (word_char(bol[-1]) && bol < eol)
653 eflags |= REG_NOTBOL;
658 if (p->token == GREP_PATTERN_HEAD && saved_ch)
661 pmatch[0].rm_so += bol - start;
662 pmatch[0].rm_eo += bol - start;
667 static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
668 enum grep_context ctx, int collect_hits)
674 die("Not a valid grep expression");
680 h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
683 h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
686 if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
688 h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
692 return (match_expr_eval(x->u.binary.left,
694 match_expr_eval(x->u.binary.right,
696 h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
697 x->u.binary.left->hit |= h;
698 h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
701 die("Unexpected node type (internal error) %d", x->node);
708 static int match_expr(struct grep_opt *opt, char *bol, char *eol,
709 enum grep_context ctx, int collect_hits)
711 struct grep_expr *x = opt->pattern_expression;
712 return match_expr_eval(x, bol, eol, ctx, collect_hits);
715 static int match_line(struct grep_opt *opt, char *bol, char *eol,
716 enum grep_context ctx, int collect_hits)
722 return match_expr(opt, bol, eol, ctx, collect_hits);
724 /* we do not call with collect_hits without being extended */
725 for (p = opt->pattern_list; p; p = p->next) {
726 if (match_one_pattern(p, bol, eol, ctx, &match, 0))
732 static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
733 enum grep_context ctx,
734 regmatch_t *pmatch, int eflags)
738 if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
740 if (match.rm_so < 0 || match.rm_eo < 0)
742 if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
743 if (match.rm_so > pmatch->rm_so)
745 if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
748 pmatch->rm_so = match.rm_so;
749 pmatch->rm_eo = match.rm_eo;
753 static int next_match(struct grep_opt *opt, char *bol, char *eol,
754 enum grep_context ctx, regmatch_t *pmatch, int eflags)
759 pmatch->rm_so = pmatch->rm_eo = -1;
761 for (p = opt->pattern_list; p; p = p->next) {
763 case GREP_PATTERN: /* atom */
764 case GREP_PATTERN_HEAD:
765 case GREP_PATTERN_BODY:
766 hit |= match_next_pattern(p, bol, eol, ctx,
777 static void show_line(struct grep_opt *opt, char *bol, char *eol,
778 const char *name, unsigned lno, char sign)
780 int rest = eol - bol;
781 char *line_color = NULL;
783 if (opt->file_break && opt->last_shown == 0) {
784 if (opt->show_hunk_mark)
785 opt->output(opt, "\n", 1);
786 } else if (opt->pre_context || opt->post_context || opt->funcbody) {
787 if (opt->last_shown == 0) {
788 if (opt->show_hunk_mark) {
789 output_color(opt, "--", 2, opt->color_sep);
790 opt->output(opt, "\n", 1);
792 } else if (lno > opt->last_shown + 1) {
793 output_color(opt, "--", 2, opt->color_sep);
794 opt->output(opt, "\n", 1);
797 if (opt->heading && opt->last_shown == 0) {
798 output_color(opt, name, strlen(name), opt->color_filename);
799 opt->output(opt, "\n", 1);
801 opt->last_shown = lno;
803 if (!opt->heading && opt->pathname) {
804 output_color(opt, name, strlen(name), opt->color_filename);
805 output_sep(opt, sign);
809 snprintf(buf, sizeof(buf), "%d", lno);
810 output_color(opt, buf, strlen(buf), opt->color_lineno);
811 output_sep(opt, sign);
815 enum grep_context ctx = GREP_CONTEXT_BODY;
820 line_color = opt->color_selected;
821 else if (sign == '-')
822 line_color = opt->color_context;
823 else if (sign == '=')
824 line_color = opt->color_function;
826 while (next_match(opt, bol, eol, ctx, &match, eflags)) {
827 if (match.rm_so == match.rm_eo)
830 output_color(opt, bol, match.rm_so, line_color);
831 output_color(opt, bol + match.rm_so,
832 match.rm_eo - match.rm_so,
840 output_color(opt, bol, rest, line_color);
841 opt->output(opt, "\n", 1);
848 * This lock protects access to the gitattributes machinery, which is
851 pthread_mutex_t grep_attr_mutex;
853 static inline void grep_attr_lock(void)
856 pthread_mutex_lock(&grep_attr_mutex);
859 static inline void grep_attr_unlock(void)
862 pthread_mutex_unlock(&grep_attr_mutex);
866 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
868 pthread_mutex_t grep_read_mutex;
871 #define grep_attr_lock()
872 #define grep_attr_unlock()
875 static int match_funcname(struct grep_opt *opt, struct grep_source *gs, char *bol, char *eol)
877 xdemitconf_t *xecfg = opt->priv;
878 if (xecfg && !xecfg->find_func) {
879 grep_source_load_driver(gs);
880 if (gs->driver->funcname.pattern) {
881 const struct userdiff_funcname *pe = &gs->driver->funcname;
882 xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
884 xecfg = opt->priv = NULL;
890 return xecfg->find_func(bol, eol - bol, buf, 1,
891 xecfg->find_func_priv) >= 0;
896 if (isalpha(*bol) || *bol == '_' || *bol == '$')
901 static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
902 char *bol, unsigned lno)
904 while (bol > gs->buf) {
907 while (bol > gs->buf && bol[-1] != '\n')
911 if (lno <= opt->last_shown)
914 if (match_funcname(opt, gs, bol, eol)) {
915 show_line(opt, bol, eol, gs->name, lno, '=');
921 static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
922 char *bol, char *end, unsigned lno)
924 unsigned cur = lno, from = 1, funcname_lno = 0;
925 int funcname_needed = !!opt->funcname;
927 if (opt->funcbody && !match_funcname(opt, gs, bol, end))
930 if (opt->pre_context < lno)
931 from = lno - opt->pre_context;
932 if (from <= opt->last_shown)
933 from = opt->last_shown + 1;
936 while (bol > gs->buf &&
937 cur > (funcname_needed == 2 ? opt->last_shown + 1 : from)) {
940 while (bol > gs->buf && bol[-1] != '\n')
943 if (funcname_needed && match_funcname(opt, gs, bol, eol)) {
949 /* We need to look even further back to find a function signature. */
950 if (opt->funcname && funcname_needed)
951 show_funcname_line(opt, gs, bol, cur);
955 char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
959 show_line(opt, bol, eol, gs->name, cur, sign);
965 static int should_lookahead(struct grep_opt *opt)
970 return 0; /* punt for too complex stuff */
973 for (p = opt->pattern_list; p; p = p->next) {
974 if (p->token != GREP_PATTERN)
975 return 0; /* punt for "header only" and stuff */
980 static int look_ahead(struct grep_opt *opt,
981 unsigned long *left_p,
985 unsigned lno = *lno_p;
989 regoff_t earliest = -1;
991 for (p = opt->pattern_list; p; p = p->next) {
995 hit = patmatch(p, bol, bol + *left_p, &m, 0);
996 if (!hit || m.rm_so < 0 || m.rm_eo < 0)
998 if (earliest < 0 || m.rm_so < earliest)
1003 *bol_p = bol + *left_p;
1007 for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
1008 ; /* find the beginning of the line */
1011 for (sp = bol; sp < last_bol; sp++) {
1015 *left_p -= last_bol - bol;
1021 static void std_output(struct grep_opt *opt, const void *buf, size_t size)
1023 fwrite(buf, size, 1, stdout);
1026 static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits)
1031 unsigned last_hit = 0;
1032 int binary_match_only = 0;
1034 int try_lookahead = 0;
1035 int show_function = 0;
1036 enum grep_context ctx = GREP_CONTEXT_HEAD;
1040 opt->output = std_output;
1042 if (opt->pre_context || opt->post_context || opt->file_break ||
1044 /* Show hunk marks, except for the first file. */
1045 if (opt->last_shown)
1046 opt->show_hunk_mark = 1;
1048 * If we're using threads then we can't easily identify
1049 * the first file. Always put hunk marks in that case
1050 * and skip the very first one later in work_done().
1052 if (opt->output != std_output)
1053 opt->show_hunk_mark = 1;
1055 opt->last_shown = 0;
1057 switch (opt->binary) {
1058 case GREP_BINARY_DEFAULT:
1059 if (grep_source_is_binary(gs))
1060 binary_match_only = 1;
1062 case GREP_BINARY_NOMATCH:
1063 if (grep_source_is_binary(gs))
1064 return 0; /* Assume unmatch */
1066 case GREP_BINARY_TEXT:
1069 die("bug: unknown binary handling mode");
1072 memset(&xecfg, 0, sizeof(xecfg));
1075 try_lookahead = should_lookahead(opt);
1077 if (grep_source_load(gs) < 0)
1087 * look_ahead() skips quickly to the line that possibly
1088 * has the next hit; don't call it if we need to do
1089 * something more than just skipping the current line
1090 * in response to an unmatch for the current line. E.g.
1091 * inside a post-context window, we will show the current
1092 * line as a context around the previous hit when it
1097 && (show_function ||
1098 lno <= last_hit + opt->post_context))
1099 && look_ahead(opt, &left, &lno, &bol))
1101 eol = end_of_line(bol, &left);
1105 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
1106 ctx = GREP_CONTEXT_BODY;
1108 hit = match_line(opt, bol, eol, ctx, collect_hits);
1114 /* "grep -v -e foo -e bla" should list lines
1115 * that do not have either, so inversion should
1120 if (opt->unmatch_name_only) {
1127 if (opt->status_only)
1129 if (opt->name_only) {
1130 show_name(opt, gs->name);
1135 if (binary_match_only) {
1136 opt->output(opt, "Binary file ", 12);
1137 output_color(opt, gs->name, strlen(gs->name),
1138 opt->color_filename);
1139 opt->output(opt, " matches\n", 9);
1142 /* Hit at this line. If we haven't shown the
1143 * pre-context lines, we would need to show them.
1145 if (opt->pre_context || opt->funcbody)
1146 show_pre_context(opt, gs, bol, eol, lno);
1147 else if (opt->funcname)
1148 show_funcname_line(opt, gs, bol, lno);
1149 show_line(opt, bol, eol, gs->name, lno, ':');
1155 if (show_function && match_funcname(opt, gs, bol, eol))
1157 if (show_function ||
1158 (last_hit && lno <= last_hit + opt->post_context)) {
1159 /* If the last hit is within the post context,
1160 * we need to show this line.
1162 show_line(opt, bol, eol, gs->name, lno, '-');
1176 if (opt->status_only)
1178 if (opt->unmatch_name_only) {
1179 /* We did not see any hit, so we want to show this */
1180 show_name(opt, gs->name);
1184 xdiff_clear_find_func(&xecfg);
1188 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1189 * which feels mostly useless but sometimes useful. Maybe
1190 * make it another option? For now suppress them.
1192 if (opt->count && count) {
1194 output_color(opt, gs->name, strlen(gs->name), opt->color_filename);
1195 output_sep(opt, ':');
1196 snprintf(buf, sizeof(buf), "%u\n", count);
1197 opt->output(opt, buf, strlen(buf));
1203 static void clr_hit_marker(struct grep_expr *x)
1205 /* All-hit markers are meaningful only at the very top level
1210 if (x->node != GREP_NODE_OR)
1212 x->u.binary.left->hit = 0;
1213 x = x->u.binary.right;
1217 static int chk_hit_marker(struct grep_expr *x)
1219 /* Top level nodes have hit markers. See if they all are hits */
1221 if (x->node != GREP_NODE_OR)
1223 if (!x->u.binary.left->hit)
1225 x = x->u.binary.right;
1229 int grep_source(struct grep_opt *opt, struct grep_source *gs)
1232 * we do not have to do the two-pass grep when we do not check
1233 * buffer-wide "all-match".
1235 if (!opt->all_match)
1236 return grep_source_1(opt, gs, 0);
1238 /* Otherwise the toplevel "or" terms hit a bit differently.
1239 * We first clear hit markers from them.
1241 clr_hit_marker(opt->pattern_expression);
1242 grep_source_1(opt, gs, 1);
1244 if (!chk_hit_marker(opt->pattern_expression))
1247 return grep_source_1(opt, gs, 0);
1250 int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size)
1252 struct grep_source gs;
1255 grep_source_init(&gs, GREP_SOURCE_BUF, NULL, NULL);
1259 r = grep_source(opt, &gs);
1261 grep_source_clear(&gs);
1265 void grep_source_init(struct grep_source *gs, enum grep_source_type type,
1266 const char *name, const void *identifier)
1269 gs->name = name ? xstrdup(name) : NULL;
1275 case GREP_SOURCE_FILE:
1276 gs->identifier = xstrdup(identifier);
1278 case GREP_SOURCE_SHA1:
1279 gs->identifier = xmalloc(20);
1280 memcpy(gs->identifier, identifier, 20);
1282 case GREP_SOURCE_BUF:
1283 gs->identifier = NULL;
1287 void grep_source_clear(struct grep_source *gs)
1291 free(gs->identifier);
1292 gs->identifier = NULL;
1293 grep_source_clear_data(gs);
1296 void grep_source_clear_data(struct grep_source *gs)
1299 case GREP_SOURCE_FILE:
1300 case GREP_SOURCE_SHA1:
1305 case GREP_SOURCE_BUF:
1306 /* leave user-provided buf intact */
1311 static int grep_source_load_sha1(struct grep_source *gs)
1313 enum object_type type;
1316 gs->buf = read_sha1_file(gs->identifier, &type, &gs->size);
1320 return error(_("'%s': unable to read %s"),
1322 sha1_to_hex(gs->identifier));
1326 static int grep_source_load_file(struct grep_source *gs)
1328 const char *filename = gs->identifier;
1334 if (lstat(filename, &st) < 0) {
1336 if (errno != ENOENT)
1337 error(_("'%s': %s"), filename, strerror(errno));
1340 if (!S_ISREG(st.st_mode))
1342 size = xsize_t(st.st_size);
1343 i = open(filename, O_RDONLY);
1346 data = xmalloc(size + 1);
1347 if (st.st_size != read_in_full(i, data, size)) {
1348 error(_("'%s': short read %s"), filename, strerror(errno));
1361 int grep_source_load(struct grep_source *gs)
1367 case GREP_SOURCE_FILE:
1368 return grep_source_load_file(gs);
1369 case GREP_SOURCE_SHA1:
1370 return grep_source_load_sha1(gs);
1371 case GREP_SOURCE_BUF:
1372 return gs->buf ? 0 : -1;
1374 die("BUG: invalid grep_source type");
1377 void grep_source_load_driver(struct grep_source *gs)
1383 gs->driver = userdiff_find_by_path(gs->name);
1385 gs->driver = userdiff_find_by_name("default");
1389 int grep_source_is_binary(struct grep_source *gs)
1391 grep_source_load_driver(gs);
1392 if (gs->driver->binary != -1)
1393 return gs->driver->binary;
1395 if (!grep_source_load(gs))
1396 return buffer_is_binary(gs->buf, gs->size);