4 #include "xdiff-interface.h"
8 static int grep_source_load(struct grep_source *gs);
9 static int grep_source_is_binary(struct grep_source *gs);
11 static struct grep_opt grep_defaults;
14 * Initialize the grep_defaults template with hardcoded defaults.
15 * We could let the compiler do this, but without C99 initializers
16 * the code gets unwieldy and unreadable, so...
18 void init_grep_defaults(void)
20 struct grep_opt *opt = &grep_defaults;
27 memset(opt, 0, sizeof(*opt));
30 opt->regflags = REG_NEWLINE;
32 opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
33 opt->extended_regexp_option = 0;
34 color_set(opt->color_context, "");
35 color_set(opt->color_filename, "");
36 color_set(opt->color_function, "");
37 color_set(opt->color_lineno, "");
38 color_set(opt->color_match_context, GIT_COLOR_BOLD_RED);
39 color_set(opt->color_match_selected, GIT_COLOR_BOLD_RED);
40 color_set(opt->color_selected, "");
41 color_set(opt->color_sep, GIT_COLOR_CYAN);
45 static int parse_pattern_type_arg(const char *opt, const char *arg)
47 if (!strcmp(arg, "default"))
48 return GREP_PATTERN_TYPE_UNSPECIFIED;
49 else if (!strcmp(arg, "basic"))
50 return GREP_PATTERN_TYPE_BRE;
51 else if (!strcmp(arg, "extended"))
52 return GREP_PATTERN_TYPE_ERE;
53 else if (!strcmp(arg, "fixed"))
54 return GREP_PATTERN_TYPE_FIXED;
55 else if (!strcmp(arg, "perl"))
56 return GREP_PATTERN_TYPE_PCRE;
57 die("bad %s argument: %s", opt, arg);
61 * Read the configuration file once and store it in
62 * the grep_defaults template.
64 int grep_config(const char *var, const char *value, void *cb)
66 struct grep_opt *opt = &grep_defaults;
69 if (userdiff_config(var, value) < 0)
72 if (!strcmp(var, "grep.extendedregexp")) {
73 if (git_config_bool(var, value))
74 opt->extended_regexp_option = 1;
76 opt->extended_regexp_option = 0;
80 if (!strcmp(var, "grep.patterntype")) {
81 opt->pattern_type_option = parse_pattern_type_arg(var, value);
85 if (!strcmp(var, "grep.linenumber")) {
86 opt->linenum = git_config_bool(var, value);
90 if (!strcmp(var, "grep.fullname")) {
91 opt->relative = !git_config_bool(var, value);
95 if (!strcmp(var, "color.grep"))
96 opt->color = git_config_colorbool(var, value);
97 else if (!strcmp(var, "color.grep.context"))
98 color = opt->color_context;
99 else if (!strcmp(var, "color.grep.filename"))
100 color = opt->color_filename;
101 else if (!strcmp(var, "color.grep.function"))
102 color = opt->color_function;
103 else if (!strcmp(var, "color.grep.linenumber"))
104 color = opt->color_lineno;
105 else if (!strcmp(var, "color.grep.matchcontext"))
106 color = opt->color_match_context;
107 else if (!strcmp(var, "color.grep.matchselected"))
108 color = opt->color_match_selected;
109 else if (!strcmp(var, "color.grep.selected"))
110 color = opt->color_selected;
111 else if (!strcmp(var, "color.grep.separator"))
112 color = opt->color_sep;
113 else if (!strcmp(var, "color.grep.match")) {
116 return config_error_nonbool(var);
117 rc |= color_parse(value, opt->color_match_context);
118 rc |= color_parse(value, opt->color_match_selected);
124 return config_error_nonbool(var);
125 return color_parse(value, color);
131 * Initialize one instance of grep_opt and copy the
132 * default values from the template we read the configuration
133 * information in an earlier call to git_config(grep_config).
135 void grep_init(struct grep_opt *opt, const char *prefix)
137 struct grep_opt *def = &grep_defaults;
139 memset(opt, 0, sizeof(*opt));
140 opt->prefix = prefix;
141 opt->prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
142 opt->pattern_tail = &opt->pattern_list;
143 opt->header_tail = &opt->header_list;
145 opt->color = def->color;
146 opt->extended_regexp_option = def->extended_regexp_option;
147 opt->pattern_type_option = def->pattern_type_option;
148 opt->linenum = def->linenum;
149 opt->max_depth = def->max_depth;
150 opt->pathname = def->pathname;
151 opt->regflags = def->regflags;
152 opt->relative = def->relative;
154 color_set(opt->color_context, def->color_context);
155 color_set(opt->color_filename, def->color_filename);
156 color_set(opt->color_function, def->color_function);
157 color_set(opt->color_lineno, def->color_lineno);
158 color_set(opt->color_match_context, def->color_match_context);
159 color_set(opt->color_match_selected, def->color_match_selected);
160 color_set(opt->color_selected, def->color_selected);
161 color_set(opt->color_sep, def->color_sep);
164 void grep_commit_pattern_type(enum grep_pattern_type pattern_type, struct grep_opt *opt)
166 if (pattern_type != GREP_PATTERN_TYPE_UNSPECIFIED)
167 grep_set_pattern_type_option(pattern_type, opt);
168 else if (opt->pattern_type_option != GREP_PATTERN_TYPE_UNSPECIFIED)
169 grep_set_pattern_type_option(opt->pattern_type_option, opt);
170 else if (opt->extended_regexp_option)
171 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, opt);
174 void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, struct grep_opt *opt)
176 switch (pattern_type) {
177 case GREP_PATTERN_TYPE_UNSPECIFIED:
180 case GREP_PATTERN_TYPE_BRE:
183 opt->regflags &= ~REG_EXTENDED;
186 case GREP_PATTERN_TYPE_ERE:
189 opt->regflags |= REG_EXTENDED;
192 case GREP_PATTERN_TYPE_FIXED:
195 opt->regflags &= ~REG_EXTENDED;
198 case GREP_PATTERN_TYPE_PCRE:
201 opt->regflags &= ~REG_EXTENDED;
206 static struct grep_pat *create_grep_pat(const char *pat, size_t patlen,
207 const char *origin, int no,
208 enum grep_pat_token t,
209 enum grep_header_field field)
211 struct grep_pat *p = xcalloc(1, sizeof(*p));
212 p->pattern = xmemdupz(pat, patlen);
213 p->patternlen = patlen;
221 static void do_append_grep_pat(struct grep_pat ***tail, struct grep_pat *p)
228 case GREP_PATTERN: /* atom */
229 case GREP_PATTERN_HEAD:
230 case GREP_PATTERN_BODY:
232 struct grep_pat *new_pat;
234 char *cp = p->pattern + p->patternlen, *nl = NULL;
235 while (++len <= p->patternlen) {
236 if (*(--cp) == '\n') {
243 new_pat = create_grep_pat(nl + 1, len - 1, p->origin,
244 p->no, p->token, p->field);
245 new_pat->next = p->next;
247 *tail = &new_pat->next;
250 p->patternlen -= len;
258 void append_header_grep_pattern(struct grep_opt *opt,
259 enum grep_header_field field, const char *pat)
261 struct grep_pat *p = create_grep_pat(pat, strlen(pat), "header", 0,
262 GREP_PATTERN_HEAD, field);
263 if (field == GREP_HEADER_REFLOG)
264 opt->use_reflog_filter = 1;
265 do_append_grep_pat(&opt->header_tail, p);
268 void append_grep_pattern(struct grep_opt *opt, const char *pat,
269 const char *origin, int no, enum grep_pat_token t)
271 append_grep_pat(opt, pat, strlen(pat), origin, no, t);
274 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen,
275 const char *origin, int no, enum grep_pat_token t)
277 struct grep_pat *p = create_grep_pat(pat, patlen, origin, no, t, 0);
278 do_append_grep_pat(&opt->pattern_tail, p);
281 struct grep_opt *grep_opt_dup(const struct grep_opt *opt)
283 struct grep_pat *pat;
284 struct grep_opt *ret = xmalloc(sizeof(struct grep_opt));
287 ret->pattern_list = NULL;
288 ret->pattern_tail = &ret->pattern_list;
290 for(pat = opt->pattern_list; pat != NULL; pat = pat->next)
292 if(pat->token == GREP_PATTERN_HEAD)
293 append_header_grep_pattern(ret, pat->field,
296 append_grep_pat(ret, pat->pattern, pat->patternlen,
297 pat->origin, pat->no, pat->token);
303 static NORETURN void compile_regexp_failed(const struct grep_pat *p,
309 xsnprintf(where, sizeof(where), "In '%s' at %d, ", p->origin, p->no);
311 xsnprintf(where, sizeof(where), "%s, ", p->origin);
315 die("%s'%s': %s", where, p->pattern, error);
319 static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
323 int options = PCRE_MULTILINE;
325 if (opt->ignore_case)
326 options |= PCRE_CASELESS;
328 p->pcre_regexp = pcre_compile(p->pattern, options, &error, &erroffset,
331 compile_regexp_failed(p, error);
333 p->pcre_extra_info = pcre_study(p->pcre_regexp, 0, &error);
334 if (!p->pcre_extra_info && error)
338 static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
339 regmatch_t *match, int eflags)
341 int ovector[30], ret, flags = 0;
343 if (eflags & REG_NOTBOL)
344 flags |= PCRE_NOTBOL;
346 ret = pcre_exec(p->pcre_regexp, p->pcre_extra_info, line, eol - line,
347 0, flags, ovector, ARRAY_SIZE(ovector));
348 if (ret < 0 && ret != PCRE_ERROR_NOMATCH)
349 die("pcre_exec failed with error code %d", ret);
352 match->rm_so = ovector[0];
353 match->rm_eo = ovector[1];
359 static void free_pcre_regexp(struct grep_pat *p)
361 pcre_free(p->pcre_regexp);
362 pcre_free(p->pcre_extra_info);
364 #else /* !USE_LIBPCRE */
365 static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
367 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
370 static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
371 regmatch_t *match, int eflags)
376 static void free_pcre_regexp(struct grep_pat *p)
379 #endif /* !USE_LIBPCRE */
381 static int is_fixed(const char *s, size_t len)
385 /* regcomp cannot accept patterns with NULs so we
386 * consider any pattern containing a NUL fixed.
388 if (memchr(s, 0, len))
391 for (i = 0; i < len; i++) {
392 if (is_regex_special(s[i]))
399 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
403 p->word_regexp = opt->word_regexp;
404 p->ignore_case = opt->ignore_case;
408 else if (is_fixed(p->pattern, p->patternlen))
414 if (opt->regflags & REG_ICASE || p->ignore_case)
415 p->kws = kwsalloc(tolower_trans_tbl);
417 p->kws = kwsalloc(NULL);
418 kwsincr(p->kws, p->pattern, p->patternlen);
424 compile_pcre_regexp(p, opt);
428 err = regcomp(&p->regexp, p->pattern, opt->regflags);
431 regerror(err, &p->regexp, errbuf, 1024);
433 compile_regexp_failed(p, errbuf);
437 static struct grep_expr *compile_pattern_or(struct grep_pat **);
438 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
447 case GREP_PATTERN: /* atom */
448 case GREP_PATTERN_HEAD:
449 case GREP_PATTERN_BODY:
450 x = xcalloc(1, sizeof (struct grep_expr));
451 x->node = GREP_NODE_ATOM;
455 case GREP_OPEN_PAREN:
457 x = compile_pattern_or(list);
458 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
459 die("unmatched parenthesis");
460 *list = (*list)->next;
467 static struct grep_expr *compile_pattern_not(struct grep_pat **list)
478 die("--not not followed by pattern expression");
480 x = xcalloc(1, sizeof (struct grep_expr));
481 x->node = GREP_NODE_NOT;
482 x->u.unary = compile_pattern_not(list);
484 die("--not followed by non pattern expression");
487 return compile_pattern_atom(list);
491 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
494 struct grep_expr *x, *y, *z;
496 x = compile_pattern_not(list);
498 if (p && p->token == GREP_AND) {
500 die("--and not followed by pattern expression");
502 y = compile_pattern_and(list);
504 die("--and not followed by pattern expression");
505 z = xcalloc(1, sizeof (struct grep_expr));
506 z->node = GREP_NODE_AND;
507 z->u.binary.left = x;
508 z->u.binary.right = y;
514 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
517 struct grep_expr *x, *y, *z;
519 x = compile_pattern_and(list);
521 if (x && p && p->token != GREP_CLOSE_PAREN) {
522 y = compile_pattern_or(list);
524 die("not a pattern expression %s", p->pattern);
525 z = xcalloc(1, sizeof (struct grep_expr));
526 z->node = GREP_NODE_OR;
527 z->u.binary.left = x;
528 z->u.binary.right = y;
534 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
536 return compile_pattern_or(list);
539 static void indent(int in)
545 static void dump_grep_pat(struct grep_pat *p)
548 case GREP_AND: fprintf(stderr, "*and*"); break;
549 case GREP_OPEN_PAREN: fprintf(stderr, "*(*"); break;
550 case GREP_CLOSE_PAREN: fprintf(stderr, "*)*"); break;
551 case GREP_NOT: fprintf(stderr, "*not*"); break;
552 case GREP_OR: fprintf(stderr, "*or*"); break;
554 case GREP_PATTERN: fprintf(stderr, "pattern"); break;
555 case GREP_PATTERN_HEAD: fprintf(stderr, "pattern_head"); break;
556 case GREP_PATTERN_BODY: fprintf(stderr, "pattern_body"); break;
561 case GREP_PATTERN_HEAD:
562 fprintf(stderr, "<head %d>", p->field); break;
563 case GREP_PATTERN_BODY:
564 fprintf(stderr, "<body>"); break;
568 case GREP_PATTERN_HEAD:
569 case GREP_PATTERN_BODY:
571 fprintf(stderr, "%.*s", (int)p->patternlen, p->pattern);
577 static void dump_grep_expression_1(struct grep_expr *x, int in)
582 fprintf(stderr, "true\n");
585 dump_grep_pat(x->u.atom);
588 fprintf(stderr, "(not\n");
589 dump_grep_expression_1(x->u.unary, in+1);
591 fprintf(stderr, ")\n");
594 fprintf(stderr, "(and\n");
595 dump_grep_expression_1(x->u.binary.left, in+1);
596 dump_grep_expression_1(x->u.binary.right, in+1);
598 fprintf(stderr, ")\n");
601 fprintf(stderr, "(or\n");
602 dump_grep_expression_1(x->u.binary.left, in+1);
603 dump_grep_expression_1(x->u.binary.right, in+1);
605 fprintf(stderr, ")\n");
610 static void dump_grep_expression(struct grep_opt *opt)
612 struct grep_expr *x = opt->pattern_expression;
615 fprintf(stderr, "[all-match]\n");
616 dump_grep_expression_1(x, 0);
620 static struct grep_expr *grep_true_expr(void)
622 struct grep_expr *z = xcalloc(1, sizeof(*z));
623 z->node = GREP_NODE_TRUE;
627 static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
629 struct grep_expr *z = xcalloc(1, sizeof(*z));
630 z->node = GREP_NODE_OR;
631 z->u.binary.left = left;
632 z->u.binary.right = right;
636 static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
639 struct grep_expr *header_expr;
640 struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]);
641 enum grep_header_field fld;
643 if (!opt->header_list)
646 for (p = opt->header_list; p; p = p->next) {
647 if (p->token != GREP_PATTERN_HEAD)
648 die("bug: a non-header pattern in grep header list.");
649 if (p->field < GREP_HEADER_FIELD_MIN ||
650 GREP_HEADER_FIELD_MAX <= p->field)
651 die("bug: unknown header field %d", p->field);
652 compile_regexp(p, opt);
655 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++)
656 header_group[fld] = NULL;
658 for (p = opt->header_list; p; p = p->next) {
660 struct grep_pat *pp = p;
662 h = compile_pattern_atom(&pp);
663 if (!h || pp != p->next)
664 die("bug: malformed header expr");
665 if (!header_group[p->field]) {
666 header_group[p->field] = h;
669 header_group[p->field] = grep_or_expr(h, header_group[p->field]);
674 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) {
675 if (!header_group[fld])
678 header_expr = grep_true_expr();
679 header_expr = grep_or_expr(header_group[fld], header_expr);
684 static struct grep_expr *grep_splice_or(struct grep_expr *x, struct grep_expr *y)
686 struct grep_expr *z = x;
689 assert(x->node == GREP_NODE_OR);
690 if (x->u.binary.right &&
691 x->u.binary.right->node == GREP_NODE_TRUE) {
692 x->u.binary.right = y;
695 x = x->u.binary.right;
700 static void compile_grep_patterns_real(struct grep_opt *opt)
703 struct grep_expr *header_expr = prep_header_patterns(opt);
705 for (p = opt->pattern_list; p; p = p->next) {
707 case GREP_PATTERN: /* atom */
708 case GREP_PATTERN_HEAD:
709 case GREP_PATTERN_BODY:
710 compile_regexp(p, opt);
718 if (opt->all_match || header_expr)
720 else if (!opt->extended && !opt->debug)
723 p = opt->pattern_list;
725 opt->pattern_expression = compile_pattern_expr(&p);
727 die("incomplete pattern expression: %s", p->pattern);
732 if (!opt->pattern_expression)
733 opt->pattern_expression = header_expr;
734 else if (opt->all_match)
735 opt->pattern_expression = grep_splice_or(header_expr,
736 opt->pattern_expression);
738 opt->pattern_expression = grep_or_expr(opt->pattern_expression,
743 void compile_grep_patterns(struct grep_opt *opt)
745 compile_grep_patterns_real(opt);
747 dump_grep_expression(opt);
750 static void free_pattern_expr(struct grep_expr *x)
757 free_pattern_expr(x->u.unary);
761 free_pattern_expr(x->u.binary.left);
762 free_pattern_expr(x->u.binary.right);
768 void free_grep_patterns(struct grep_opt *opt)
770 struct grep_pat *p, *n;
772 for (p = opt->pattern_list; p; p = n) {
775 case GREP_PATTERN: /* atom */
776 case GREP_PATTERN_HEAD:
777 case GREP_PATTERN_BODY:
780 else if (p->pcre_regexp)
794 free_pattern_expr(opt->pattern_expression);
797 static char *end_of_line(char *cp, unsigned long *left)
799 unsigned long l = *left;
800 while (l && *cp != '\n') {
808 static int word_char(char ch)
810 return isalnum(ch) || ch == '_';
813 static void output_color(struct grep_opt *opt, const void *data, size_t size,
816 if (want_color(opt->color) && color && color[0]) {
817 opt->output(opt, color, strlen(color));
818 opt->output(opt, data, size);
819 opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
821 opt->output(opt, data, size);
824 static void output_sep(struct grep_opt *opt, char sign)
826 if (opt->null_following_name)
827 opt->output(opt, "\0", 1);
829 output_color(opt, &sign, 1, opt->color_sep);
832 static void show_name(struct grep_opt *opt, const char *name)
834 output_color(opt, name, strlen(name), opt->color_filename);
835 opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
838 static int fixmatch(struct grep_pat *p, char *line, char *eol,
841 struct kwsmatch kwsm;
842 size_t offset = kwsexec(p->kws, line, eol - line, &kwsm);
844 match->rm_so = match->rm_eo = -1;
847 match->rm_so = offset;
848 match->rm_eo = match->rm_so + kwsm.size[0];
853 static int regmatch(const regex_t *preg, char *line, char *eol,
854 regmatch_t *match, int eflags)
858 match->rm_eo = eol - line;
859 eflags |= REG_STARTEND;
861 return regexec(preg, line, 1, match, eflags);
864 static int patmatch(struct grep_pat *p, char *line, char *eol,
865 regmatch_t *match, int eflags)
870 hit = !fixmatch(p, line, eol, match);
871 else if (p->pcre_regexp)
872 hit = !pcrematch(p, line, eol, match, eflags);
874 hit = !regmatch(&p->regexp, line, eol, match, eflags);
879 static int strip_timestamp(char *bol, char **eol_p)
884 while (bol < --eol) {
900 { "committer ", 10 },
904 static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
905 enum grep_context ctx,
906 regmatch_t *pmatch, int eflags)
910 const char *start = bol;
912 if ((p->token != GREP_PATTERN) &&
913 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
916 if (p->token == GREP_PATTERN_HEAD) {
919 assert(p->field < ARRAY_SIZE(header_field));
920 field = header_field[p->field].field;
921 len = header_field[p->field].len;
922 if (strncmp(bol, field, len))
926 case GREP_HEADER_AUTHOR:
927 case GREP_HEADER_COMMITTER:
928 saved_ch = strip_timestamp(bol, &eol);
936 hit = patmatch(p, bol, eol, pmatch, eflags);
938 if (hit && p->word_regexp) {
939 if ((pmatch[0].rm_so < 0) ||
940 (eol - bol) < pmatch[0].rm_so ||
941 (pmatch[0].rm_eo < 0) ||
942 (eol - bol) < pmatch[0].rm_eo)
943 die("regexp returned nonsense");
945 /* Match beginning must be either beginning of the
946 * line, or at word boundary (i.e. the last char must
947 * not be a word char). Similarly, match end must be
948 * either end of the line, or at word boundary
949 * (i.e. the next char must not be a word char).
951 if ( ((pmatch[0].rm_so == 0) ||
952 !word_char(bol[pmatch[0].rm_so-1])) &&
953 ((pmatch[0].rm_eo == (eol-bol)) ||
954 !word_char(bol[pmatch[0].rm_eo])) )
959 /* Words consist of at least one character. */
960 if (pmatch->rm_so == pmatch->rm_eo)
963 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
964 /* There could be more than one match on the
965 * line, and the first match might not be
966 * strict word match. But later ones could be!
967 * Forward to the next possible start, i.e. the
968 * next position following a non-word char.
970 bol = pmatch[0].rm_so + bol + 1;
971 while (word_char(bol[-1]) && bol < eol)
973 eflags |= REG_NOTBOL;
978 if (p->token == GREP_PATTERN_HEAD && saved_ch)
981 pmatch[0].rm_so += bol - start;
982 pmatch[0].rm_eo += bol - start;
987 static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
988 enum grep_context ctx, int collect_hits)
994 die("Not a valid grep expression");
1000 h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
1003 h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
1006 if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
1008 h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
1012 return (match_expr_eval(x->u.binary.left,
1013 bol, eol, ctx, 0) ||
1014 match_expr_eval(x->u.binary.right,
1016 h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
1017 x->u.binary.left->hit |= h;
1018 h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
1021 die("Unexpected node type (internal error) %d", x->node);
1028 static int match_expr(struct grep_opt *opt, char *bol, char *eol,
1029 enum grep_context ctx, int collect_hits)
1031 struct grep_expr *x = opt->pattern_expression;
1032 return match_expr_eval(x, bol, eol, ctx, collect_hits);
1035 static int match_line(struct grep_opt *opt, char *bol, char *eol,
1036 enum grep_context ctx, int collect_hits)
1042 return match_expr(opt, bol, eol, ctx, collect_hits);
1044 /* we do not call with collect_hits without being extended */
1045 for (p = opt->pattern_list; p; p = p->next) {
1046 if (match_one_pattern(p, bol, eol, ctx, &match, 0))
1052 static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
1053 enum grep_context ctx,
1054 regmatch_t *pmatch, int eflags)
1058 if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
1060 if (match.rm_so < 0 || match.rm_eo < 0)
1062 if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
1063 if (match.rm_so > pmatch->rm_so)
1065 if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
1068 pmatch->rm_so = match.rm_so;
1069 pmatch->rm_eo = match.rm_eo;
1073 static int next_match(struct grep_opt *opt, char *bol, char *eol,
1074 enum grep_context ctx, regmatch_t *pmatch, int eflags)
1079 pmatch->rm_so = pmatch->rm_eo = -1;
1081 for (p = opt->pattern_list; p; p = p->next) {
1083 case GREP_PATTERN: /* atom */
1084 case GREP_PATTERN_HEAD:
1085 case GREP_PATTERN_BODY:
1086 hit |= match_next_pattern(p, bol, eol, ctx,
1097 static void show_line(struct grep_opt *opt, char *bol, char *eol,
1098 const char *name, unsigned lno, char sign)
1100 int rest = eol - bol;
1101 const char *match_color, *line_color = NULL;
1103 if (opt->file_break && opt->last_shown == 0) {
1104 if (opt->show_hunk_mark)
1105 opt->output(opt, "\n", 1);
1106 } else if (opt->pre_context || opt->post_context || opt->funcbody) {
1107 if (opt->last_shown == 0) {
1108 if (opt->show_hunk_mark) {
1109 output_color(opt, "--", 2, opt->color_sep);
1110 opt->output(opt, "\n", 1);
1112 } else if (lno > opt->last_shown + 1) {
1113 output_color(opt, "--", 2, opt->color_sep);
1114 opt->output(opt, "\n", 1);
1117 if (opt->heading && opt->last_shown == 0) {
1118 output_color(opt, name, strlen(name), opt->color_filename);
1119 opt->output(opt, "\n", 1);
1121 opt->last_shown = lno;
1123 if (!opt->heading && opt->pathname) {
1124 output_color(opt, name, strlen(name), opt->color_filename);
1125 output_sep(opt, sign);
1129 snprintf(buf, sizeof(buf), "%d", lno);
1130 output_color(opt, buf, strlen(buf), opt->color_lineno);
1131 output_sep(opt, sign);
1135 enum grep_context ctx = GREP_CONTEXT_BODY;
1140 match_color = opt->color_match_selected;
1142 match_color = opt->color_match_context;
1144 line_color = opt->color_selected;
1145 else if (sign == '-')
1146 line_color = opt->color_context;
1147 else if (sign == '=')
1148 line_color = opt->color_function;
1150 while (next_match(opt, bol, eol, ctx, &match, eflags)) {
1151 if (match.rm_so == match.rm_eo)
1154 output_color(opt, bol, match.rm_so, line_color);
1155 output_color(opt, bol + match.rm_so,
1156 match.rm_eo - match.rm_so, match_color);
1158 rest -= match.rm_eo;
1159 eflags = REG_NOTBOL;
1163 output_color(opt, bol, rest, line_color);
1164 opt->output(opt, "\n", 1);
1171 * This lock protects access to the gitattributes machinery, which is
1174 pthread_mutex_t grep_attr_mutex;
1176 static inline void grep_attr_lock(void)
1179 pthread_mutex_lock(&grep_attr_mutex);
1182 static inline void grep_attr_unlock(void)
1185 pthread_mutex_unlock(&grep_attr_mutex);
1189 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
1191 pthread_mutex_t grep_read_mutex;
1194 #define grep_attr_lock()
1195 #define grep_attr_unlock()
1198 static int match_funcname(struct grep_opt *opt, struct grep_source *gs, char *bol, char *eol)
1200 xdemitconf_t *xecfg = opt->priv;
1201 if (xecfg && !xecfg->find_func) {
1202 grep_source_load_driver(gs);
1203 if (gs->driver->funcname.pattern) {
1204 const struct userdiff_funcname *pe = &gs->driver->funcname;
1205 xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
1207 xecfg = opt->priv = NULL;
1213 return xecfg->find_func(bol, eol - bol, buf, 1,
1214 xecfg->find_func_priv) >= 0;
1219 if (isalpha(*bol) || *bol == '_' || *bol == '$')
1224 static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
1225 char *bol, unsigned lno)
1227 while (bol > gs->buf) {
1230 while (bol > gs->buf && bol[-1] != '\n')
1234 if (lno <= opt->last_shown)
1237 if (match_funcname(opt, gs, bol, eol)) {
1238 show_line(opt, bol, eol, gs->name, lno, '=');
1244 static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
1245 char *bol, char *end, unsigned lno)
1247 unsigned cur = lno, from = 1, funcname_lno = 0;
1248 int funcname_needed = !!opt->funcname;
1250 if (opt->funcbody && !match_funcname(opt, gs, bol, end))
1251 funcname_needed = 2;
1253 if (opt->pre_context < lno)
1254 from = lno - opt->pre_context;
1255 if (from <= opt->last_shown)
1256 from = opt->last_shown + 1;
1259 while (bol > gs->buf &&
1260 cur > (funcname_needed == 2 ? opt->last_shown + 1 : from)) {
1263 while (bol > gs->buf && bol[-1] != '\n')
1266 if (funcname_needed && match_funcname(opt, gs, bol, eol)) {
1268 funcname_needed = 0;
1272 /* We need to look even further back to find a function signature. */
1273 if (opt->funcname && funcname_needed)
1274 show_funcname_line(opt, gs, bol, cur);
1278 char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
1280 while (*eol != '\n')
1282 show_line(opt, bol, eol, gs->name, cur, sign);
1288 static int should_lookahead(struct grep_opt *opt)
1293 return 0; /* punt for too complex stuff */
1296 for (p = opt->pattern_list; p; p = p->next) {
1297 if (p->token != GREP_PATTERN)
1298 return 0; /* punt for "header only" and stuff */
1303 static int look_ahead(struct grep_opt *opt,
1304 unsigned long *left_p,
1308 unsigned lno = *lno_p;
1311 char *sp, *last_bol;
1312 regoff_t earliest = -1;
1314 for (p = opt->pattern_list; p; p = p->next) {
1318 hit = patmatch(p, bol, bol + *left_p, &m, 0);
1319 if (!hit || m.rm_so < 0 || m.rm_eo < 0)
1321 if (earliest < 0 || m.rm_so < earliest)
1326 *bol_p = bol + *left_p;
1330 for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
1331 ; /* find the beginning of the line */
1334 for (sp = bol; sp < last_bol; sp++) {
1338 *left_p -= last_bol - bol;
1344 static void std_output(struct grep_opt *opt, const void *buf, size_t size)
1346 fwrite(buf, size, 1, stdout);
1349 static int fill_textconv_grep(struct userdiff_driver *driver,
1350 struct grep_source *gs)
1352 struct diff_filespec *df;
1356 if (!driver || !driver->textconv)
1357 return grep_source_load(gs);
1360 * The textconv interface is intimately tied to diff_filespecs, so we
1361 * have to pretend to be one. If we could unify the grep_source
1362 * and diff_filespec structs, this mess could just go away.
1364 df = alloc_filespec(gs->path);
1366 case GREP_SOURCE_SHA1:
1367 fill_filespec(df, gs->identifier, 1, 0100644);
1369 case GREP_SOURCE_FILE:
1370 fill_filespec(df, null_sha1, 0, 0100644);
1373 die("BUG: attempt to textconv something without a path?");
1377 * fill_textconv is not remotely thread-safe; it may load objects
1378 * behind the scenes, and it modifies the global diff tempfile
1382 size = fill_textconv(driver, df, &buf);
1387 * The normal fill_textconv usage by the diff machinery would just keep
1388 * the textconv'd buf separate from the diff_filespec. But much of the
1389 * grep code passes around a grep_source and assumes that its "buf"
1390 * pointer is the beginning of the thing we are searching. So let's
1391 * install our textconv'd version into the grep_source, taking care not
1392 * to leak any existing buffer.
1394 grep_source_clear_data(gs);
1401 static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits)
1406 unsigned last_hit = 0;
1407 int binary_match_only = 0;
1409 int try_lookahead = 0;
1410 int show_function = 0;
1411 struct userdiff_driver *textconv = NULL;
1412 enum grep_context ctx = GREP_CONTEXT_HEAD;
1416 opt->output = std_output;
1418 if (opt->pre_context || opt->post_context || opt->file_break ||
1420 /* Show hunk marks, except for the first file. */
1421 if (opt->last_shown)
1422 opt->show_hunk_mark = 1;
1424 * If we're using threads then we can't easily identify
1425 * the first file. Always put hunk marks in that case
1426 * and skip the very first one later in work_done().
1428 if (opt->output != std_output)
1429 opt->show_hunk_mark = 1;
1431 opt->last_shown = 0;
1433 if (opt->allow_textconv) {
1434 grep_source_load_driver(gs);
1436 * We might set up the shared textconv cache data here, which
1437 * is not thread-safe.
1440 textconv = userdiff_get_textconv(gs->driver);
1445 * We know the result of a textconv is text, so we only have to care
1446 * about binary handling if we are not using it.
1449 switch (opt->binary) {
1450 case GREP_BINARY_DEFAULT:
1451 if (grep_source_is_binary(gs))
1452 binary_match_only = 1;
1454 case GREP_BINARY_NOMATCH:
1455 if (grep_source_is_binary(gs))
1456 return 0; /* Assume unmatch */
1458 case GREP_BINARY_TEXT:
1461 die("bug: unknown binary handling mode");
1465 memset(&xecfg, 0, sizeof(xecfg));
1468 try_lookahead = should_lookahead(opt);
1470 if (fill_textconv_grep(textconv, gs) < 0)
1480 * look_ahead() skips quickly to the line that possibly
1481 * has the next hit; don't call it if we need to do
1482 * something more than just skipping the current line
1483 * in response to an unmatch for the current line. E.g.
1484 * inside a post-context window, we will show the current
1485 * line as a context around the previous hit when it
1490 && (show_function ||
1491 lno <= last_hit + opt->post_context))
1492 && look_ahead(opt, &left, &lno, &bol))
1494 eol = end_of_line(bol, &left);
1498 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
1499 ctx = GREP_CONTEXT_BODY;
1501 hit = match_line(opt, bol, eol, ctx, collect_hits);
1507 /* "grep -v -e foo -e bla" should list lines
1508 * that do not have either, so inversion should
1513 if (opt->unmatch_name_only) {
1520 if (opt->status_only)
1522 if (opt->name_only) {
1523 show_name(opt, gs->name);
1528 if (binary_match_only) {
1529 opt->output(opt, "Binary file ", 12);
1530 output_color(opt, gs->name, strlen(gs->name),
1531 opt->color_filename);
1532 opt->output(opt, " matches\n", 9);
1535 /* Hit at this line. If we haven't shown the
1536 * pre-context lines, we would need to show them.
1538 if (opt->pre_context || opt->funcbody)
1539 show_pre_context(opt, gs, bol, eol, lno);
1540 else if (opt->funcname)
1541 show_funcname_line(opt, gs, bol, lno);
1542 show_line(opt, bol, eol, gs->name, lno, ':');
1548 if (show_function && match_funcname(opt, gs, bol, eol))
1550 if (show_function ||
1551 (last_hit && lno <= last_hit + opt->post_context)) {
1552 /* If the last hit is within the post context,
1553 * we need to show this line.
1555 show_line(opt, bol, eol, gs->name, lno, '-');
1569 if (opt->status_only)
1571 if (opt->unmatch_name_only) {
1572 /* We did not see any hit, so we want to show this */
1573 show_name(opt, gs->name);
1577 xdiff_clear_find_func(&xecfg);
1581 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1582 * which feels mostly useless but sometimes useful. Maybe
1583 * make it another option? For now suppress them.
1585 if (opt->count && count) {
1587 if (opt->pathname) {
1588 output_color(opt, gs->name, strlen(gs->name),
1589 opt->color_filename);
1590 output_sep(opt, ':');
1592 snprintf(buf, sizeof(buf), "%u\n", count);
1593 opt->output(opt, buf, strlen(buf));
1599 static void clr_hit_marker(struct grep_expr *x)
1601 /* All-hit markers are meaningful only at the very top level
1606 if (x->node != GREP_NODE_OR)
1608 x->u.binary.left->hit = 0;
1609 x = x->u.binary.right;
1613 static int chk_hit_marker(struct grep_expr *x)
1615 /* Top level nodes have hit markers. See if they all are hits */
1617 if (x->node != GREP_NODE_OR)
1619 if (!x->u.binary.left->hit)
1621 x = x->u.binary.right;
1625 int grep_source(struct grep_opt *opt, struct grep_source *gs)
1628 * we do not have to do the two-pass grep when we do not check
1629 * buffer-wide "all-match".
1631 if (!opt->all_match)
1632 return grep_source_1(opt, gs, 0);
1634 /* Otherwise the toplevel "or" terms hit a bit differently.
1635 * We first clear hit markers from them.
1637 clr_hit_marker(opt->pattern_expression);
1638 grep_source_1(opt, gs, 1);
1640 if (!chk_hit_marker(opt->pattern_expression))
1643 return grep_source_1(opt, gs, 0);
1646 int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size)
1648 struct grep_source gs;
1651 grep_source_init(&gs, GREP_SOURCE_BUF, NULL, NULL, NULL);
1655 r = grep_source(opt, &gs);
1657 grep_source_clear(&gs);
1661 void grep_source_init(struct grep_source *gs, enum grep_source_type type,
1662 const char *name, const char *path,
1663 const void *identifier)
1666 gs->name = xstrdup_or_null(name);
1667 gs->path = xstrdup_or_null(path);
1673 case GREP_SOURCE_FILE:
1674 gs->identifier = xstrdup(identifier);
1676 case GREP_SOURCE_SHA1:
1677 gs->identifier = xmalloc(20);
1678 hashcpy(gs->identifier, identifier);
1680 case GREP_SOURCE_BUF:
1681 gs->identifier = NULL;
1685 void grep_source_clear(struct grep_source *gs)
1691 free(gs->identifier);
1692 gs->identifier = NULL;
1693 grep_source_clear_data(gs);
1696 void grep_source_clear_data(struct grep_source *gs)
1699 case GREP_SOURCE_FILE:
1700 case GREP_SOURCE_SHA1:
1705 case GREP_SOURCE_BUF:
1706 /* leave user-provided buf intact */
1711 static int grep_source_load_sha1(struct grep_source *gs)
1713 enum object_type type;
1716 gs->buf = read_sha1_file(gs->identifier, &type, &gs->size);
1720 return error(_("'%s': unable to read %s"),
1722 sha1_to_hex(gs->identifier));
1726 static int grep_source_load_file(struct grep_source *gs)
1728 const char *filename = gs->identifier;
1734 if (lstat(filename, &st) < 0) {
1736 if (errno != ENOENT)
1737 error(_("'%s': %s"), filename, strerror(errno));
1740 if (!S_ISREG(st.st_mode))
1742 size = xsize_t(st.st_size);
1743 i = open(filename, O_RDONLY);
1746 data = xmalloc(size + 1);
1747 if (st.st_size != read_in_full(i, data, size)) {
1748 error(_("'%s': short read %s"), filename, strerror(errno));
1761 static int grep_source_load(struct grep_source *gs)
1767 case GREP_SOURCE_FILE:
1768 return grep_source_load_file(gs);
1769 case GREP_SOURCE_SHA1:
1770 return grep_source_load_sha1(gs);
1771 case GREP_SOURCE_BUF:
1772 return gs->buf ? 0 : -1;
1774 die("BUG: invalid grep_source type");
1777 void grep_source_load_driver(struct grep_source *gs)
1784 gs->driver = userdiff_find_by_path(gs->path);
1786 gs->driver = userdiff_find_by_name("default");
1790 static int grep_source_is_binary(struct grep_source *gs)
1792 grep_source_load_driver(gs);
1793 if (gs->driver->binary != -1)
1794 return gs->driver->binary;
1796 if (!grep_source_load(gs))
1797 return buffer_is_binary(gs->buf, gs->size);