4 #include "xdiff-interface.h"
6 static int grep_source_load(struct grep_source *gs);
7 static int grep_source_is_binary(struct grep_source *gs);
10 static struct grep_pat *create_grep_pat(const char *pat, size_t patlen,
11 const char *origin, int no,
12 enum grep_pat_token t,
13 enum grep_header_field field)
15 struct grep_pat *p = xcalloc(1, sizeof(*p));
16 p->pattern = xmemdupz(pat, patlen);
17 p->patternlen = patlen;
25 static void do_append_grep_pat(struct grep_pat ***tail, struct grep_pat *p)
32 case GREP_PATTERN: /* atom */
33 case GREP_PATTERN_HEAD:
34 case GREP_PATTERN_BODY:
36 struct grep_pat *new_pat;
38 char *cp = p->pattern + p->patternlen, *nl = NULL;
39 while (++len <= p->patternlen) {
40 if (*(--cp) == '\n') {
47 new_pat = create_grep_pat(nl + 1, len - 1, p->origin,
48 p->no, p->token, p->field);
49 new_pat->next = p->next;
51 *tail = &new_pat->next;
62 void append_header_grep_pattern(struct grep_opt *opt,
63 enum grep_header_field field, const char *pat)
65 struct grep_pat *p = create_grep_pat(pat, strlen(pat), "header", 0,
66 GREP_PATTERN_HEAD, field);
67 if (field == GREP_HEADER_REFLOG)
68 opt->use_reflog_filter = 1;
69 do_append_grep_pat(&opt->header_tail, p);
72 void append_grep_pattern(struct grep_opt *opt, const char *pat,
73 const char *origin, int no, enum grep_pat_token t)
75 append_grep_pat(opt, pat, strlen(pat), origin, no, t);
78 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen,
79 const char *origin, int no, enum grep_pat_token t)
81 struct grep_pat *p = create_grep_pat(pat, patlen, origin, no, t, 0);
82 do_append_grep_pat(&opt->pattern_tail, p);
85 struct grep_opt *grep_opt_dup(const struct grep_opt *opt)
88 struct grep_opt *ret = xmalloc(sizeof(struct grep_opt));
91 ret->pattern_list = NULL;
92 ret->pattern_tail = &ret->pattern_list;
94 for(pat = opt->pattern_list; pat != NULL; pat = pat->next)
96 if(pat->token == GREP_PATTERN_HEAD)
97 append_header_grep_pattern(ret, pat->field,
100 append_grep_pat(ret, pat->pattern, pat->patternlen,
101 pat->origin, pat->no, pat->token);
107 static NORETURN void compile_regexp_failed(const struct grep_pat *p,
113 sprintf(where, "In '%s' at %d, ", p->origin, p->no);
115 sprintf(where, "%s, ", p->origin);
119 die("%s'%s': %s", where, p->pattern, error);
123 static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
127 int options = PCRE_MULTILINE;
129 if (opt->ignore_case)
130 options |= PCRE_CASELESS;
132 p->pcre_regexp = pcre_compile(p->pattern, options, &error, &erroffset,
135 compile_regexp_failed(p, error);
137 p->pcre_extra_info = pcre_study(p->pcre_regexp, 0, &error);
138 if (!p->pcre_extra_info && error)
142 static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
143 regmatch_t *match, int eflags)
145 int ovector[30], ret, flags = 0;
147 if (eflags & REG_NOTBOL)
148 flags |= PCRE_NOTBOL;
150 ret = pcre_exec(p->pcre_regexp, p->pcre_extra_info, line, eol - line,
151 0, flags, ovector, ARRAY_SIZE(ovector));
152 if (ret < 0 && ret != PCRE_ERROR_NOMATCH)
153 die("pcre_exec failed with error code %d", ret);
156 match->rm_so = ovector[0];
157 match->rm_eo = ovector[1];
163 static void free_pcre_regexp(struct grep_pat *p)
165 pcre_free(p->pcre_regexp);
166 pcre_free(p->pcre_extra_info);
168 #else /* !USE_LIBPCRE */
169 static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
171 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
174 static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
175 regmatch_t *match, int eflags)
180 static void free_pcre_regexp(struct grep_pat *p)
183 #endif /* !USE_LIBPCRE */
185 static int is_fixed(const char *s, size_t len)
189 /* regcomp cannot accept patterns with NULs so we
190 * consider any pattern containing a NUL fixed.
192 if (memchr(s, 0, len))
195 for (i = 0; i < len; i++) {
196 if (is_regex_special(s[i]))
203 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
207 p->word_regexp = opt->word_regexp;
208 p->ignore_case = opt->ignore_case;
210 if (opt->fixed || is_fixed(p->pattern, p->patternlen))
216 if (opt->regflags & REG_ICASE || p->ignore_case)
217 p->kws = kwsalloc(tolower_trans_tbl);
219 p->kws = kwsalloc(NULL);
220 kwsincr(p->kws, p->pattern, p->patternlen);
226 compile_pcre_regexp(p, opt);
230 err = regcomp(&p->regexp, p->pattern, opt->regflags);
233 regerror(err, &p->regexp, errbuf, 1024);
235 compile_regexp_failed(p, errbuf);
239 static struct grep_expr *compile_pattern_or(struct grep_pat **);
240 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
249 case GREP_PATTERN: /* atom */
250 case GREP_PATTERN_HEAD:
251 case GREP_PATTERN_BODY:
252 x = xcalloc(1, sizeof (struct grep_expr));
253 x->node = GREP_NODE_ATOM;
257 case GREP_OPEN_PAREN:
259 x = compile_pattern_or(list);
260 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
261 die("unmatched parenthesis");
262 *list = (*list)->next;
269 static struct grep_expr *compile_pattern_not(struct grep_pat **list)
280 die("--not not followed by pattern expression");
282 x = xcalloc(1, sizeof (struct grep_expr));
283 x->node = GREP_NODE_NOT;
284 x->u.unary = compile_pattern_not(list);
286 die("--not followed by non pattern expression");
289 return compile_pattern_atom(list);
293 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
296 struct grep_expr *x, *y, *z;
298 x = compile_pattern_not(list);
300 if (p && p->token == GREP_AND) {
302 die("--and not followed by pattern expression");
304 y = compile_pattern_and(list);
306 die("--and not followed by pattern expression");
307 z = xcalloc(1, sizeof (struct grep_expr));
308 z->node = GREP_NODE_AND;
309 z->u.binary.left = x;
310 z->u.binary.right = y;
316 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
319 struct grep_expr *x, *y, *z;
321 x = compile_pattern_and(list);
323 if (x && p && p->token != GREP_CLOSE_PAREN) {
324 y = compile_pattern_or(list);
326 die("not a pattern expression %s", p->pattern);
327 z = xcalloc(1, sizeof (struct grep_expr));
328 z->node = GREP_NODE_OR;
329 z->u.binary.left = x;
330 z->u.binary.right = y;
336 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
338 return compile_pattern_or(list);
341 static void indent(int in)
347 static void dump_grep_pat(struct grep_pat *p)
350 case GREP_AND: fprintf(stderr, "*and*"); break;
351 case GREP_OPEN_PAREN: fprintf(stderr, "*(*"); break;
352 case GREP_CLOSE_PAREN: fprintf(stderr, "*)*"); break;
353 case GREP_NOT: fprintf(stderr, "*not*"); break;
354 case GREP_OR: fprintf(stderr, "*or*"); break;
356 case GREP_PATTERN: fprintf(stderr, "pattern"); break;
357 case GREP_PATTERN_HEAD: fprintf(stderr, "pattern_head"); break;
358 case GREP_PATTERN_BODY: fprintf(stderr, "pattern_body"); break;
363 case GREP_PATTERN_HEAD:
364 fprintf(stderr, "<head %d>", p->field); break;
365 case GREP_PATTERN_BODY:
366 fprintf(stderr, "<body>"); break;
370 case GREP_PATTERN_HEAD:
371 case GREP_PATTERN_BODY:
373 fprintf(stderr, "%.*s", (int)p->patternlen, p->pattern);
379 static void dump_grep_expression_1(struct grep_expr *x, int in)
384 fprintf(stderr, "true\n");
387 dump_grep_pat(x->u.atom);
390 fprintf(stderr, "(not\n");
391 dump_grep_expression_1(x->u.unary, in+1);
393 fprintf(stderr, ")\n");
396 fprintf(stderr, "(and\n");
397 dump_grep_expression_1(x->u.binary.left, in+1);
398 dump_grep_expression_1(x->u.binary.right, in+1);
400 fprintf(stderr, ")\n");
403 fprintf(stderr, "(or\n");
404 dump_grep_expression_1(x->u.binary.left, in+1);
405 dump_grep_expression_1(x->u.binary.right, in+1);
407 fprintf(stderr, ")\n");
412 static void dump_grep_expression(struct grep_opt *opt)
414 struct grep_expr *x = opt->pattern_expression;
417 fprintf(stderr, "[all-match]\n");
418 dump_grep_expression_1(x, 0);
422 static struct grep_expr *grep_true_expr(void)
424 struct grep_expr *z = xcalloc(1, sizeof(*z));
425 z->node = GREP_NODE_TRUE;
429 static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
431 struct grep_expr *z = xcalloc(1, sizeof(*z));
432 z->node = GREP_NODE_OR;
433 z->u.binary.left = left;
434 z->u.binary.right = right;
438 static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
441 struct grep_expr *header_expr;
442 struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]);
443 enum grep_header_field fld;
445 if (!opt->header_list)
448 for (p = opt->header_list; p; p = p->next) {
449 if (p->token != GREP_PATTERN_HEAD)
450 die("bug: a non-header pattern in grep header list.");
451 if (p->field < 0 || GREP_HEADER_FIELD_MAX <= p->field)
452 die("bug: unknown header field %d", p->field);
453 compile_regexp(p, opt);
456 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++)
457 header_group[fld] = NULL;
459 for (p = opt->header_list; p; p = p->next) {
461 struct grep_pat *pp = p;
463 h = compile_pattern_atom(&pp);
464 if (!h || pp != p->next)
465 die("bug: malformed header expr");
466 if (!header_group[p->field]) {
467 header_group[p->field] = h;
470 header_group[p->field] = grep_or_expr(h, header_group[p->field]);
475 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) {
476 if (!header_group[fld])
479 header_expr = grep_true_expr();
480 header_expr = grep_or_expr(header_group[fld], header_expr);
485 static struct grep_expr *grep_splice_or(struct grep_expr *x, struct grep_expr *y)
487 struct grep_expr *z = x;
490 assert(x->node == GREP_NODE_OR);
491 if (x->u.binary.right &&
492 x->u.binary.right->node == GREP_NODE_TRUE) {
493 x->u.binary.right = y;
496 x = x->u.binary.right;
501 static void compile_grep_patterns_real(struct grep_opt *opt)
504 struct grep_expr *header_expr = prep_header_patterns(opt);
506 for (p = opt->pattern_list; p; p = p->next) {
508 case GREP_PATTERN: /* atom */
509 case GREP_PATTERN_HEAD:
510 case GREP_PATTERN_BODY:
511 compile_regexp(p, opt);
519 if (opt->all_match || header_expr)
521 else if (!opt->extended && !opt->debug)
524 p = opt->pattern_list;
526 opt->pattern_expression = compile_pattern_expr(&p);
528 die("incomplete pattern expression: %s", p->pattern);
533 if (!opt->pattern_expression)
534 opt->pattern_expression = header_expr;
535 else if (opt->all_match)
536 opt->pattern_expression = grep_splice_or(header_expr,
537 opt->pattern_expression);
539 opt->pattern_expression = grep_or_expr(opt->pattern_expression,
544 void compile_grep_patterns(struct grep_opt *opt)
546 compile_grep_patterns_real(opt);
548 dump_grep_expression(opt);
551 static void free_pattern_expr(struct grep_expr *x)
558 free_pattern_expr(x->u.unary);
562 free_pattern_expr(x->u.binary.left);
563 free_pattern_expr(x->u.binary.right);
569 void free_grep_patterns(struct grep_opt *opt)
571 struct grep_pat *p, *n;
573 for (p = opt->pattern_list; p; p = n) {
576 case GREP_PATTERN: /* atom */
577 case GREP_PATTERN_HEAD:
578 case GREP_PATTERN_BODY:
581 else if (p->pcre_regexp)
595 free_pattern_expr(opt->pattern_expression);
598 static char *end_of_line(char *cp, unsigned long *left)
600 unsigned long l = *left;
601 while (l && *cp != '\n') {
609 static int word_char(char ch)
611 return isalnum(ch) || ch == '_';
614 static void output_color(struct grep_opt *opt, const void *data, size_t size,
617 if (want_color(opt->color) && color && color[0]) {
618 opt->output(opt, color, strlen(color));
619 opt->output(opt, data, size);
620 opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
622 opt->output(opt, data, size);
625 static void output_sep(struct grep_opt *opt, char sign)
627 if (opt->null_following_name)
628 opt->output(opt, "\0", 1);
630 output_color(opt, &sign, 1, opt->color_sep);
633 static void show_name(struct grep_opt *opt, const char *name)
635 output_color(opt, name, strlen(name), opt->color_filename);
636 opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
639 static int fixmatch(struct grep_pat *p, char *line, char *eol,
642 struct kwsmatch kwsm;
643 size_t offset = kwsexec(p->kws, line, eol - line, &kwsm);
645 match->rm_so = match->rm_eo = -1;
648 match->rm_so = offset;
649 match->rm_eo = match->rm_so + kwsm.size[0];
654 static int regmatch(const regex_t *preg, char *line, char *eol,
655 regmatch_t *match, int eflags)
659 match->rm_eo = eol - line;
660 eflags |= REG_STARTEND;
662 return regexec(preg, line, 1, match, eflags);
665 static int patmatch(struct grep_pat *p, char *line, char *eol,
666 regmatch_t *match, int eflags)
671 hit = !fixmatch(p, line, eol, match);
672 else if (p->pcre_regexp)
673 hit = !pcrematch(p, line, eol, match, eflags);
675 hit = !regmatch(&p->regexp, line, eol, match, eflags);
680 static int strip_timestamp(char *bol, char **eol_p)
685 while (bol < --eol) {
701 { "committer ", 10 },
705 static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
706 enum grep_context ctx,
707 regmatch_t *pmatch, int eflags)
711 const char *start = bol;
713 if ((p->token != GREP_PATTERN) &&
714 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
717 if (p->token == GREP_PATTERN_HEAD) {
720 assert(p->field < ARRAY_SIZE(header_field));
721 field = header_field[p->field].field;
722 len = header_field[p->field].len;
723 if (strncmp(bol, field, len))
727 case GREP_HEADER_AUTHOR:
728 case GREP_HEADER_COMMITTER:
729 saved_ch = strip_timestamp(bol, &eol);
737 hit = patmatch(p, bol, eol, pmatch, eflags);
739 if (hit && p->word_regexp) {
740 if ((pmatch[0].rm_so < 0) ||
741 (eol - bol) < pmatch[0].rm_so ||
742 (pmatch[0].rm_eo < 0) ||
743 (eol - bol) < pmatch[0].rm_eo)
744 die("regexp returned nonsense");
746 /* Match beginning must be either beginning of the
747 * line, or at word boundary (i.e. the last char must
748 * not be a word char). Similarly, match end must be
749 * either end of the line, or at word boundary
750 * (i.e. the next char must not be a word char).
752 if ( ((pmatch[0].rm_so == 0) ||
753 !word_char(bol[pmatch[0].rm_so-1])) &&
754 ((pmatch[0].rm_eo == (eol-bol)) ||
755 !word_char(bol[pmatch[0].rm_eo])) )
760 /* Words consist of at least one character. */
761 if (pmatch->rm_so == pmatch->rm_eo)
764 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
765 /* There could be more than one match on the
766 * line, and the first match might not be
767 * strict word match. But later ones could be!
768 * Forward to the next possible start, i.e. the
769 * next position following a non-word char.
771 bol = pmatch[0].rm_so + bol + 1;
772 while (word_char(bol[-1]) && bol < eol)
774 eflags |= REG_NOTBOL;
779 if (p->token == GREP_PATTERN_HEAD && saved_ch)
782 pmatch[0].rm_so += bol - start;
783 pmatch[0].rm_eo += bol - start;
788 static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
789 enum grep_context ctx, int collect_hits)
795 die("Not a valid grep expression");
801 h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
804 h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
807 if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
809 h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
813 return (match_expr_eval(x->u.binary.left,
815 match_expr_eval(x->u.binary.right,
817 h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
818 x->u.binary.left->hit |= h;
819 h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
822 die("Unexpected node type (internal error) %d", x->node);
829 static int match_expr(struct grep_opt *opt, char *bol, char *eol,
830 enum grep_context ctx, int collect_hits)
832 struct grep_expr *x = opt->pattern_expression;
833 return match_expr_eval(x, bol, eol, ctx, collect_hits);
836 static int match_line(struct grep_opt *opt, char *bol, char *eol,
837 enum grep_context ctx, int collect_hits)
843 return match_expr(opt, bol, eol, ctx, collect_hits);
845 /* we do not call with collect_hits without being extended */
846 for (p = opt->pattern_list; p; p = p->next) {
847 if (match_one_pattern(p, bol, eol, ctx, &match, 0))
853 static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
854 enum grep_context ctx,
855 regmatch_t *pmatch, int eflags)
859 if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
861 if (match.rm_so < 0 || match.rm_eo < 0)
863 if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
864 if (match.rm_so > pmatch->rm_so)
866 if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
869 pmatch->rm_so = match.rm_so;
870 pmatch->rm_eo = match.rm_eo;
874 static int next_match(struct grep_opt *opt, char *bol, char *eol,
875 enum grep_context ctx, regmatch_t *pmatch, int eflags)
880 pmatch->rm_so = pmatch->rm_eo = -1;
882 for (p = opt->pattern_list; p; p = p->next) {
884 case GREP_PATTERN: /* atom */
885 case GREP_PATTERN_HEAD:
886 case GREP_PATTERN_BODY:
887 hit |= match_next_pattern(p, bol, eol, ctx,
898 static void show_line(struct grep_opt *opt, char *bol, char *eol,
899 const char *name, unsigned lno, char sign)
901 int rest = eol - bol;
902 char *line_color = NULL;
904 if (opt->file_break && opt->last_shown == 0) {
905 if (opt->show_hunk_mark)
906 opt->output(opt, "\n", 1);
907 } else if (opt->pre_context || opt->post_context || opt->funcbody) {
908 if (opt->last_shown == 0) {
909 if (opt->show_hunk_mark) {
910 output_color(opt, "--", 2, opt->color_sep);
911 opt->output(opt, "\n", 1);
913 } else if (lno > opt->last_shown + 1) {
914 output_color(opt, "--", 2, opt->color_sep);
915 opt->output(opt, "\n", 1);
918 if (opt->heading && opt->last_shown == 0) {
919 output_color(opt, name, strlen(name), opt->color_filename);
920 opt->output(opt, "\n", 1);
922 opt->last_shown = lno;
924 if (!opt->heading && opt->pathname) {
925 output_color(opt, name, strlen(name), opt->color_filename);
926 output_sep(opt, sign);
930 snprintf(buf, sizeof(buf), "%d", lno);
931 output_color(opt, buf, strlen(buf), opt->color_lineno);
932 output_sep(opt, sign);
936 enum grep_context ctx = GREP_CONTEXT_BODY;
941 line_color = opt->color_selected;
942 else if (sign == '-')
943 line_color = opt->color_context;
944 else if (sign == '=')
945 line_color = opt->color_function;
947 while (next_match(opt, bol, eol, ctx, &match, eflags)) {
948 if (match.rm_so == match.rm_eo)
951 output_color(opt, bol, match.rm_so, line_color);
952 output_color(opt, bol + match.rm_so,
953 match.rm_eo - match.rm_so,
961 output_color(opt, bol, rest, line_color);
962 opt->output(opt, "\n", 1);
969 * This lock protects access to the gitattributes machinery, which is
972 pthread_mutex_t grep_attr_mutex;
974 static inline void grep_attr_lock(void)
977 pthread_mutex_lock(&grep_attr_mutex);
980 static inline void grep_attr_unlock(void)
983 pthread_mutex_unlock(&grep_attr_mutex);
987 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
989 pthread_mutex_t grep_read_mutex;
992 #define grep_attr_lock()
993 #define grep_attr_unlock()
996 static int match_funcname(struct grep_opt *opt, struct grep_source *gs, char *bol, char *eol)
998 xdemitconf_t *xecfg = opt->priv;
999 if (xecfg && !xecfg->find_func) {
1000 grep_source_load_driver(gs);
1001 if (gs->driver->funcname.pattern) {
1002 const struct userdiff_funcname *pe = &gs->driver->funcname;
1003 xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
1005 xecfg = opt->priv = NULL;
1011 return xecfg->find_func(bol, eol - bol, buf, 1,
1012 xecfg->find_func_priv) >= 0;
1017 if (isalpha(*bol) || *bol == '_' || *bol == '$')
1022 static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
1023 char *bol, unsigned lno)
1025 while (bol > gs->buf) {
1028 while (bol > gs->buf && bol[-1] != '\n')
1032 if (lno <= opt->last_shown)
1035 if (match_funcname(opt, gs, bol, eol)) {
1036 show_line(opt, bol, eol, gs->name, lno, '=');
1042 static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
1043 char *bol, char *end, unsigned lno)
1045 unsigned cur = lno, from = 1, funcname_lno = 0;
1046 int funcname_needed = !!opt->funcname;
1048 if (opt->funcbody && !match_funcname(opt, gs, bol, end))
1049 funcname_needed = 2;
1051 if (opt->pre_context < lno)
1052 from = lno - opt->pre_context;
1053 if (from <= opt->last_shown)
1054 from = opt->last_shown + 1;
1057 while (bol > gs->buf &&
1058 cur > (funcname_needed == 2 ? opt->last_shown + 1 : from)) {
1061 while (bol > gs->buf && bol[-1] != '\n')
1064 if (funcname_needed && match_funcname(opt, gs, bol, eol)) {
1066 funcname_needed = 0;
1070 /* We need to look even further back to find a function signature. */
1071 if (opt->funcname && funcname_needed)
1072 show_funcname_line(opt, gs, bol, cur);
1076 char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
1078 while (*eol != '\n')
1080 show_line(opt, bol, eol, gs->name, cur, sign);
1086 static int should_lookahead(struct grep_opt *opt)
1091 return 0; /* punt for too complex stuff */
1094 for (p = opt->pattern_list; p; p = p->next) {
1095 if (p->token != GREP_PATTERN)
1096 return 0; /* punt for "header only" and stuff */
1101 static int look_ahead(struct grep_opt *opt,
1102 unsigned long *left_p,
1106 unsigned lno = *lno_p;
1109 char *sp, *last_bol;
1110 regoff_t earliest = -1;
1112 for (p = opt->pattern_list; p; p = p->next) {
1116 hit = patmatch(p, bol, bol + *left_p, &m, 0);
1117 if (!hit || m.rm_so < 0 || m.rm_eo < 0)
1119 if (earliest < 0 || m.rm_so < earliest)
1124 *bol_p = bol + *left_p;
1128 for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
1129 ; /* find the beginning of the line */
1132 for (sp = bol; sp < last_bol; sp++) {
1136 *left_p -= last_bol - bol;
1142 static void std_output(struct grep_opt *opt, const void *buf, size_t size)
1144 fwrite(buf, size, 1, stdout);
1147 static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits)
1152 unsigned last_hit = 0;
1153 int binary_match_only = 0;
1155 int try_lookahead = 0;
1156 int show_function = 0;
1157 enum grep_context ctx = GREP_CONTEXT_HEAD;
1161 opt->output = std_output;
1163 if (opt->pre_context || opt->post_context || opt->file_break ||
1165 /* Show hunk marks, except for the first file. */
1166 if (opt->last_shown)
1167 opt->show_hunk_mark = 1;
1169 * If we're using threads then we can't easily identify
1170 * the first file. Always put hunk marks in that case
1171 * and skip the very first one later in work_done().
1173 if (opt->output != std_output)
1174 opt->show_hunk_mark = 1;
1176 opt->last_shown = 0;
1178 switch (opt->binary) {
1179 case GREP_BINARY_DEFAULT:
1180 if (grep_source_is_binary(gs))
1181 binary_match_only = 1;
1183 case GREP_BINARY_NOMATCH:
1184 if (grep_source_is_binary(gs))
1185 return 0; /* Assume unmatch */
1187 case GREP_BINARY_TEXT:
1190 die("bug: unknown binary handling mode");
1193 memset(&xecfg, 0, sizeof(xecfg));
1196 try_lookahead = should_lookahead(opt);
1198 if (grep_source_load(gs) < 0)
1208 * look_ahead() skips quickly to the line that possibly
1209 * has the next hit; don't call it if we need to do
1210 * something more than just skipping the current line
1211 * in response to an unmatch for the current line. E.g.
1212 * inside a post-context window, we will show the current
1213 * line as a context around the previous hit when it
1218 && (show_function ||
1219 lno <= last_hit + opt->post_context))
1220 && look_ahead(opt, &left, &lno, &bol))
1222 eol = end_of_line(bol, &left);
1226 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
1227 ctx = GREP_CONTEXT_BODY;
1229 hit = match_line(opt, bol, eol, ctx, collect_hits);
1235 /* "grep -v -e foo -e bla" should list lines
1236 * that do not have either, so inversion should
1241 if (opt->unmatch_name_only) {
1248 if (opt->status_only)
1250 if (opt->name_only) {
1251 show_name(opt, gs->name);
1256 if (binary_match_only) {
1257 opt->output(opt, "Binary file ", 12);
1258 output_color(opt, gs->name, strlen(gs->name),
1259 opt->color_filename);
1260 opt->output(opt, " matches\n", 9);
1263 /* Hit at this line. If we haven't shown the
1264 * pre-context lines, we would need to show them.
1266 if (opt->pre_context || opt->funcbody)
1267 show_pre_context(opt, gs, bol, eol, lno);
1268 else if (opt->funcname)
1269 show_funcname_line(opt, gs, bol, lno);
1270 show_line(opt, bol, eol, gs->name, lno, ':');
1276 if (show_function && match_funcname(opt, gs, bol, eol))
1278 if (show_function ||
1279 (last_hit && lno <= last_hit + opt->post_context)) {
1280 /* If the last hit is within the post context,
1281 * we need to show this line.
1283 show_line(opt, bol, eol, gs->name, lno, '-');
1297 if (opt->status_only)
1299 if (opt->unmatch_name_only) {
1300 /* We did not see any hit, so we want to show this */
1301 show_name(opt, gs->name);
1305 xdiff_clear_find_func(&xecfg);
1309 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1310 * which feels mostly useless but sometimes useful. Maybe
1311 * make it another option? For now suppress them.
1313 if (opt->count && count) {
1315 output_color(opt, gs->name, strlen(gs->name), opt->color_filename);
1316 output_sep(opt, ':');
1317 snprintf(buf, sizeof(buf), "%u\n", count);
1318 opt->output(opt, buf, strlen(buf));
1324 static void clr_hit_marker(struct grep_expr *x)
1326 /* All-hit markers are meaningful only at the very top level
1331 if (x->node != GREP_NODE_OR)
1333 x->u.binary.left->hit = 0;
1334 x = x->u.binary.right;
1338 static int chk_hit_marker(struct grep_expr *x)
1340 /* Top level nodes have hit markers. See if they all are hits */
1342 if (x->node != GREP_NODE_OR)
1344 if (!x->u.binary.left->hit)
1346 x = x->u.binary.right;
1350 int grep_source(struct grep_opt *opt, struct grep_source *gs)
1353 * we do not have to do the two-pass grep when we do not check
1354 * buffer-wide "all-match".
1356 if (!opt->all_match)
1357 return grep_source_1(opt, gs, 0);
1359 /* Otherwise the toplevel "or" terms hit a bit differently.
1360 * We first clear hit markers from them.
1362 clr_hit_marker(opt->pattern_expression);
1363 grep_source_1(opt, gs, 1);
1365 if (!chk_hit_marker(opt->pattern_expression))
1368 return grep_source_1(opt, gs, 0);
1371 int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size)
1373 struct grep_source gs;
1376 grep_source_init(&gs, GREP_SOURCE_BUF, NULL, NULL);
1380 r = grep_source(opt, &gs);
1382 grep_source_clear(&gs);
1386 void grep_source_init(struct grep_source *gs, enum grep_source_type type,
1387 const char *name, const void *identifier)
1390 gs->name = name ? xstrdup(name) : NULL;
1396 case GREP_SOURCE_FILE:
1397 gs->identifier = xstrdup(identifier);
1399 case GREP_SOURCE_SHA1:
1400 gs->identifier = xmalloc(20);
1401 memcpy(gs->identifier, identifier, 20);
1403 case GREP_SOURCE_BUF:
1404 gs->identifier = NULL;
1408 void grep_source_clear(struct grep_source *gs)
1412 free(gs->identifier);
1413 gs->identifier = NULL;
1414 grep_source_clear_data(gs);
1417 void grep_source_clear_data(struct grep_source *gs)
1420 case GREP_SOURCE_FILE:
1421 case GREP_SOURCE_SHA1:
1426 case GREP_SOURCE_BUF:
1427 /* leave user-provided buf intact */
1432 static int grep_source_load_sha1(struct grep_source *gs)
1434 enum object_type type;
1437 gs->buf = read_sha1_file(gs->identifier, &type, &gs->size);
1441 return error(_("'%s': unable to read %s"),
1443 sha1_to_hex(gs->identifier));
1447 static int grep_source_load_file(struct grep_source *gs)
1449 const char *filename = gs->identifier;
1455 if (lstat(filename, &st) < 0) {
1457 if (errno != ENOENT)
1458 error(_("'%s': %s"), filename, strerror(errno));
1461 if (!S_ISREG(st.st_mode))
1463 size = xsize_t(st.st_size);
1464 i = open(filename, O_RDONLY);
1467 data = xmalloc(size + 1);
1468 if (st.st_size != read_in_full(i, data, size)) {
1469 error(_("'%s': short read %s"), filename, strerror(errno));
1482 static int grep_source_load(struct grep_source *gs)
1488 case GREP_SOURCE_FILE:
1489 return grep_source_load_file(gs);
1490 case GREP_SOURCE_SHA1:
1491 return grep_source_load_sha1(gs);
1492 case GREP_SOURCE_BUF:
1493 return gs->buf ? 0 : -1;
1495 die("BUG: invalid grep_source type");
1498 void grep_source_load_driver(struct grep_source *gs)
1504 gs->driver = userdiff_find_by_path(gs->name);
1506 gs->driver = userdiff_find_by_name("default");
1510 static int grep_source_is_binary(struct grep_source *gs)
1512 grep_source_load_driver(gs);
1513 if (gs->driver->binary != -1)
1514 return gs->driver->binary;
1516 if (!grep_source_load(gs))
1517 return buffer_is_binary(gs->buf, gs->size);