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));
10 p->patternlen = strlen(pat);
13 p->token = GREP_PATTERN_HEAD;
15 *opt->header_tail = p;
16 opt->header_tail = &p->next;
20 void append_grep_pattern(struct grep_opt *opt, const char *pat,
21 const char *origin, int no, enum grep_pat_token t)
23 append_grep_pat(opt, pat, strlen(pat), origin, no, t);
26 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen,
27 const char *origin, int no, enum grep_pat_token t)
29 struct grep_pat *p = xcalloc(1, sizeof(*p));
31 p->patternlen = patlen;
35 *opt->pattern_tail = p;
36 opt->pattern_tail = &p->next;
40 struct grep_opt *grep_opt_dup(const struct grep_opt *opt)
43 struct grep_opt *ret = xmalloc(sizeof(struct grep_opt));
46 ret->pattern_list = NULL;
47 ret->pattern_tail = &ret->pattern_list;
49 for(pat = opt->pattern_list; pat != NULL; pat = pat->next)
51 if(pat->token == GREP_PATTERN_HEAD)
52 append_header_grep_pattern(ret, pat->field,
55 append_grep_pat(ret, pat->pattern, pat->patternlen,
56 pat->origin, pat->no, pat->token);
62 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
66 p->word_regexp = opt->word_regexp;
67 p->ignore_case = opt->ignore_case;
68 p->fixed = opt->fixed;
73 err = regcomp(&p->regexp, p->pattern, opt->regflags);
78 sprintf(where, "In '%s' at %d, ",
81 sprintf(where, "%s, ", p->origin);
84 regerror(err, &p->regexp, errbuf, 1024);
86 die("%s'%s': %s", where, p->pattern, errbuf);
90 static struct grep_expr *compile_pattern_or(struct grep_pat **);
91 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
100 case GREP_PATTERN: /* atom */
101 case GREP_PATTERN_HEAD:
102 case GREP_PATTERN_BODY:
103 x = xcalloc(1, sizeof (struct grep_expr));
104 x->node = GREP_NODE_ATOM;
108 case GREP_OPEN_PAREN:
110 x = compile_pattern_or(list);
111 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
112 die("unmatched parenthesis");
113 *list = (*list)->next;
120 static struct grep_expr *compile_pattern_not(struct grep_pat **list)
131 die("--not not followed by pattern expression");
133 x = xcalloc(1, sizeof (struct grep_expr));
134 x->node = GREP_NODE_NOT;
135 x->u.unary = compile_pattern_not(list);
137 die("--not followed by non pattern expression");
140 return compile_pattern_atom(list);
144 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
147 struct grep_expr *x, *y, *z;
149 x = compile_pattern_not(list);
151 if (p && p->token == GREP_AND) {
153 die("--and not followed by pattern expression");
155 y = compile_pattern_and(list);
157 die("--and not followed by pattern expression");
158 z = xcalloc(1, sizeof (struct grep_expr));
159 z->node = GREP_NODE_AND;
160 z->u.binary.left = x;
161 z->u.binary.right = y;
167 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
170 struct grep_expr *x, *y, *z;
172 x = compile_pattern_and(list);
174 if (x && p && p->token != GREP_CLOSE_PAREN) {
175 y = compile_pattern_or(list);
177 die("not a pattern expression %s", p->pattern);
178 z = xcalloc(1, sizeof (struct grep_expr));
179 z->node = GREP_NODE_OR;
180 z->u.binary.left = x;
181 z->u.binary.right = y;
187 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
189 return compile_pattern_or(list);
192 static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
195 struct grep_expr *header_expr;
197 if (!opt->header_list)
199 p = opt->header_list;
200 header_expr = compile_pattern_expr(&p);
202 die("incomplete pattern expression: %s", p->pattern);
203 for (p = opt->header_list; p; p = p->next) {
204 if (p->token != GREP_PATTERN_HEAD)
205 die("bug: a non-header pattern in grep header list.");
206 if (p->field < 0 || GREP_HEADER_FIELD_MAX <= p->field)
207 die("bug: unknown header field %d", p->field);
208 compile_regexp(p, opt);
213 void compile_grep_patterns(struct grep_opt *opt)
216 struct grep_expr *header_expr = prep_header_patterns(opt);
218 for (p = opt->pattern_list; p; p = p->next) {
220 case GREP_PATTERN: /* atom */
221 case GREP_PATTERN_HEAD:
222 case GREP_PATTERN_BODY:
223 compile_regexp(p, opt);
231 if (opt->all_match || header_expr)
233 else if (!opt->extended)
236 p = opt->pattern_list;
238 opt->pattern_expression = compile_pattern_expr(&p);
240 die("incomplete pattern expression: %s", p->pattern);
245 if (opt->pattern_expression) {
247 z = xcalloc(1, sizeof(*z));
248 z->node = GREP_NODE_OR;
249 z->u.binary.left = opt->pattern_expression;
250 z->u.binary.right = header_expr;
251 opt->pattern_expression = z;
253 opt->pattern_expression = header_expr;
258 static void free_pattern_expr(struct grep_expr *x)
264 free_pattern_expr(x->u.unary);
268 free_pattern_expr(x->u.binary.left);
269 free_pattern_expr(x->u.binary.right);
275 void free_grep_patterns(struct grep_opt *opt)
277 struct grep_pat *p, *n;
279 for (p = opt->pattern_list; p; p = n) {
282 case GREP_PATTERN: /* atom */
283 case GREP_PATTERN_HEAD:
284 case GREP_PATTERN_BODY:
295 free_pattern_expr(opt->pattern_expression);
298 static char *end_of_line(char *cp, unsigned long *left)
300 unsigned long l = *left;
301 while (l && *cp != '\n') {
309 static int word_char(char ch)
311 return isalnum(ch) || ch == '_';
314 static void output_color(struct grep_opt *opt, const void *data, size_t size,
317 if (opt->color && color && color[0]) {
318 opt->output(opt, color, strlen(color));
319 opt->output(opt, data, size);
320 opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
322 opt->output(opt, data, size);
325 static void output_sep(struct grep_opt *opt, char sign)
327 if (opt->null_following_name)
328 opt->output(opt, "\0", 1);
330 output_color(opt, &sign, 1, opt->color_sep);
333 static void show_name(struct grep_opt *opt, const char *name)
335 output_color(opt, name, strlen(name), opt->color_filename);
336 opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
339 static int fixmatch(struct grep_pat *p, char *line, char *eol,
344 if (p->ignore_case) {
347 hit = strcasestr(s, p->pattern);
353 hit = memmem(line, eol - line, p->pattern, p->patternlen);
356 match->rm_so = match->rm_eo = -1;
360 match->rm_so = hit - line;
361 match->rm_eo = match->rm_so + p->patternlen;
366 static int regmatch(const regex_t *preg, char *line, char *eol,
367 regmatch_t *match, int eflags)
371 match->rm_eo = eol - line;
372 eflags |= REG_STARTEND;
374 return regexec(preg, line, 1, match, eflags);
377 static int strip_timestamp(char *bol, char **eol_p)
382 while (bol < --eol) {
398 { "committer ", 10 },
401 static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
402 enum grep_context ctx,
403 regmatch_t *pmatch, int eflags)
407 const char *start = bol;
409 if ((p->token != GREP_PATTERN) &&
410 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
413 if (p->token == GREP_PATTERN_HEAD) {
416 assert(p->field < ARRAY_SIZE(header_field));
417 field = header_field[p->field].field;
418 len = header_field[p->field].len;
419 if (strncmp(bol, field, len))
422 saved_ch = strip_timestamp(bol, &eol);
427 hit = !fixmatch(p, bol, eol, pmatch);
429 hit = !regmatch(&p->regexp, bol, eol, pmatch, eflags);
431 if (hit && p->word_regexp) {
432 if ((pmatch[0].rm_so < 0) ||
433 (eol - bol) < pmatch[0].rm_so ||
434 (pmatch[0].rm_eo < 0) ||
435 (eol - bol) < pmatch[0].rm_eo)
436 die("regexp returned nonsense");
438 /* Match beginning must be either beginning of the
439 * line, or at word boundary (i.e. the last char must
440 * not be a word char). Similarly, match end must be
441 * either end of the line, or at word boundary
442 * (i.e. the next char must not be a word char).
444 if ( ((pmatch[0].rm_so == 0) ||
445 !word_char(bol[pmatch[0].rm_so-1])) &&
446 ((pmatch[0].rm_eo == (eol-bol)) ||
447 !word_char(bol[pmatch[0].rm_eo])) )
452 /* Words consist of at least one character. */
453 if (pmatch->rm_so == pmatch->rm_eo)
456 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
457 /* There could be more than one match on the
458 * line, and the first match might not be
459 * strict word match. But later ones could be!
460 * Forward to the next possible start, i.e. the
461 * next position following a non-word char.
463 bol = pmatch[0].rm_so + bol + 1;
464 while (word_char(bol[-1]) && bol < eol)
466 eflags |= REG_NOTBOL;
471 if (p->token == GREP_PATTERN_HEAD && saved_ch)
474 pmatch[0].rm_so += bol - start;
475 pmatch[0].rm_eo += bol - start;
480 static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
481 enum grep_context ctx, int collect_hits)
487 die("Not a valid grep expression");
490 h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
493 h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
496 if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
498 h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
502 return (match_expr_eval(x->u.binary.left,
504 match_expr_eval(x->u.binary.right,
506 h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
507 x->u.binary.left->hit |= h;
508 h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
511 die("Unexpected node type (internal error) %d", x->node);
518 static int match_expr(struct grep_opt *opt, char *bol, char *eol,
519 enum grep_context ctx, int collect_hits)
521 struct grep_expr *x = opt->pattern_expression;
522 return match_expr_eval(x, bol, eol, ctx, collect_hits);
525 static int match_line(struct grep_opt *opt, char *bol, char *eol,
526 enum grep_context ctx, int collect_hits)
532 return match_expr(opt, bol, eol, ctx, collect_hits);
534 /* we do not call with collect_hits without being extended */
535 for (p = opt->pattern_list; p; p = p->next) {
536 if (match_one_pattern(p, bol, eol, ctx, &match, 0))
542 static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
543 enum grep_context ctx,
544 regmatch_t *pmatch, int eflags)
548 if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
550 if (match.rm_so < 0 || match.rm_eo < 0)
552 if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
553 if (match.rm_so > pmatch->rm_so)
555 if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
558 pmatch->rm_so = match.rm_so;
559 pmatch->rm_eo = match.rm_eo;
563 static int next_match(struct grep_opt *opt, char *bol, char *eol,
564 enum grep_context ctx, regmatch_t *pmatch, int eflags)
569 pmatch->rm_so = pmatch->rm_eo = -1;
571 for (p = opt->pattern_list; p; p = p->next) {
573 case GREP_PATTERN: /* atom */
574 case GREP_PATTERN_HEAD:
575 case GREP_PATTERN_BODY:
576 hit |= match_next_pattern(p, bol, eol, ctx,
587 static void show_line(struct grep_opt *opt, char *bol, char *eol,
588 const char *name, unsigned lno, char sign)
590 int rest = eol - bol;
591 char *line_color = NULL;
593 if (opt->pre_context || opt->post_context) {
594 if (opt->last_shown == 0) {
595 if (opt->show_hunk_mark) {
596 output_color(opt, "--", 2, opt->color_sep);
597 opt->output(opt, "\n", 1);
599 } else if (lno > opt->last_shown + 1) {
600 output_color(opt, "--", 2, opt->color_sep);
601 opt->output(opt, "\n", 1);
604 opt->last_shown = lno;
607 output_color(opt, name, strlen(name), opt->color_filename);
608 output_sep(opt, sign);
612 snprintf(buf, sizeof(buf), "%d", lno);
613 output_color(opt, buf, strlen(buf), opt->color_lineno);
614 output_sep(opt, sign);
618 enum grep_context ctx = GREP_CONTEXT_BODY;
623 line_color = opt->color_selected;
624 else if (sign == '-')
625 line_color = opt->color_context;
626 else if (sign == '=')
627 line_color = opt->color_function;
629 while (next_match(opt, bol, eol, ctx, &match, eflags)) {
630 if (match.rm_so == match.rm_eo)
633 output_color(opt, bol, match.rm_so, line_color);
634 output_color(opt, bol + match.rm_so,
635 match.rm_eo - match.rm_so,
643 output_color(opt, bol, rest, line_color);
644 opt->output(opt, "\n", 1);
647 static int match_funcname(struct grep_opt *opt, char *bol, char *eol)
649 xdemitconf_t *xecfg = opt->priv;
650 if (xecfg && xecfg->find_func) {
652 return xecfg->find_func(bol, eol - bol, buf, 1,
653 xecfg->find_func_priv) >= 0;
658 if (isalpha(*bol) || *bol == '_' || *bol == '$')
663 static void show_funcname_line(struct grep_opt *opt, const char *name,
664 char *buf, char *bol, unsigned lno)
669 while (bol > buf && bol[-1] != '\n')
673 if (lno <= opt->last_shown)
676 if (match_funcname(opt, bol, eol)) {
677 show_line(opt, bol, eol, name, lno, '=');
683 static void show_pre_context(struct grep_opt *opt, const char *name, char *buf,
684 char *bol, unsigned lno)
686 unsigned cur = lno, from = 1, funcname_lno = 0;
687 int funcname_needed = opt->funcname;
689 if (opt->pre_context < lno)
690 from = lno - opt->pre_context;
691 if (from <= opt->last_shown)
692 from = opt->last_shown + 1;
695 while (bol > buf && cur > from) {
698 while (bol > buf && bol[-1] != '\n')
701 if (funcname_needed && match_funcname(opt, bol, eol)) {
707 /* We need to look even further back to find a function signature. */
708 if (opt->funcname && funcname_needed)
709 show_funcname_line(opt, name, buf, bol, cur);
713 char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
717 show_line(opt, bol, eol, name, cur, sign);
723 static int should_lookahead(struct grep_opt *opt)
728 return 0; /* punt for too complex stuff */
731 for (p = opt->pattern_list; p; p = p->next) {
732 if (p->token != GREP_PATTERN)
733 return 0; /* punt for "header only" and stuff */
738 static int look_ahead(struct grep_opt *opt,
739 unsigned long *left_p,
743 unsigned lno = *lno_p;
747 regoff_t earliest = -1;
749 for (p = opt->pattern_list; p; p = p->next) {
754 hit = !fixmatch(p, bol, bol + *left_p, &m);
756 hit = !regmatch(&p->regexp, bol, bol + *left_p, &m, 0);
757 if (!hit || m.rm_so < 0 || m.rm_eo < 0)
759 if (earliest < 0 || m.rm_so < earliest)
764 *bol_p = bol + *left_p;
768 for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
769 ; /* find the beginning of the line */
772 for (sp = bol; sp < last_bol; sp++) {
776 *left_p -= last_bol - bol;
782 int grep_threads_ok(const struct grep_opt *opt)
784 /* If this condition is true, then we may use the attribute
785 * machinery in grep_buffer_1. The attribute code is not
786 * thread safe, so we disable the use of threads.
788 if (opt->funcname && !opt->unmatch_name_only && !opt->status_only &&
795 static void std_output(struct grep_opt *opt, const void *buf, size_t size)
797 fwrite(buf, size, 1, stdout);
800 static int grep_buffer_1(struct grep_opt *opt, const char *name,
801 char *buf, unsigned long size, int collect_hits)
804 unsigned long left = size;
806 unsigned last_hit = 0;
807 int binary_match_only = 0;
809 int try_lookahead = 0;
810 enum grep_context ctx = GREP_CONTEXT_HEAD;
814 opt->output = std_output;
816 if (opt->last_shown && (opt->pre_context || opt->post_context) &&
817 opt->output == std_output)
818 opt->show_hunk_mark = 1;
821 switch (opt->binary) {
822 case GREP_BINARY_DEFAULT:
823 if (buffer_is_binary(buf, size))
824 binary_match_only = 1;
826 case GREP_BINARY_NOMATCH:
827 if (buffer_is_binary(buf, size))
828 return 0; /* Assume unmatch */
830 case GREP_BINARY_TEXT:
833 die("bug: unknown binary handling mode");
836 memset(&xecfg, 0, sizeof(xecfg));
837 if (opt->funcname && !opt->unmatch_name_only && !opt->status_only &&
838 !opt->name_only && !binary_match_only && !collect_hits) {
839 struct userdiff_driver *drv = userdiff_find_by_path(name);
840 if (drv && drv->funcname.pattern) {
841 const struct userdiff_funcname *pe = &drv->funcname;
842 xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
846 try_lookahead = should_lookahead(opt);
853 * look_ahead() skips quicly to the line that possibly
854 * has the next hit; don't call it if we need to do
855 * something more than just skipping the current line
856 * in response to an unmatch for the current line. E.g.
857 * inside a post-context window, we will show the current
858 * line as a context around the previous hit when it
863 && lno <= last_hit + opt->post_context)
864 && look_ahead(opt, &left, &lno, &bol))
866 eol = end_of_line(bol, &left);
870 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
871 ctx = GREP_CONTEXT_BODY;
873 hit = match_line(opt, bol, eol, ctx, collect_hits);
879 /* "grep -v -e foo -e bla" should list lines
880 * that do not have either, so inversion should
885 if (opt->unmatch_name_only) {
892 if (opt->status_only)
894 if (opt->name_only) {
895 show_name(opt, name);
900 if (binary_match_only) {
901 opt->output(opt, "Binary file ", 12);
902 output_color(opt, name, strlen(name),
903 opt->color_filename);
904 opt->output(opt, " matches\n", 9);
907 /* Hit at this line. If we haven't shown the
908 * pre-context lines, we would need to show them.
910 if (opt->pre_context)
911 show_pre_context(opt, name, buf, bol, lno);
912 else if (opt->funcname)
913 show_funcname_line(opt, name, buf, bol, lno);
914 show_line(opt, bol, eol, name, lno, ':');
918 lno <= last_hit + opt->post_context) {
919 /* If the last hit is within the post context,
920 * we need to show this line.
922 show_line(opt, bol, eol, name, lno, '-');
936 if (opt->status_only)
938 if (opt->unmatch_name_only) {
939 /* We did not see any hit, so we want to show this */
940 show_name(opt, name);
944 xdiff_clear_find_func(&xecfg);
948 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
949 * which feels mostly useless but sometimes useful. Maybe
950 * make it another option? For now suppress them.
952 if (opt->count && count) {
954 output_color(opt, name, strlen(name), opt->color_filename);
955 output_sep(opt, ':');
956 snprintf(buf, sizeof(buf), "%u\n", count);
957 opt->output(opt, buf, strlen(buf));
963 static void clr_hit_marker(struct grep_expr *x)
965 /* All-hit markers are meaningful only at the very top level
970 if (x->node != GREP_NODE_OR)
972 x->u.binary.left->hit = 0;
973 x = x->u.binary.right;
977 static int chk_hit_marker(struct grep_expr *x)
979 /* Top level nodes have hit markers. See if they all are hits */
981 if (x->node != GREP_NODE_OR)
983 if (!x->u.binary.left->hit)
985 x = x->u.binary.right;
989 int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size)
992 * we do not have to do the two-pass grep when we do not check
993 * buffer-wide "all-match".
996 return grep_buffer_1(opt, name, buf, size, 0);
998 /* Otherwise the toplevel "or" terms hit a bit differently.
999 * We first clear hit markers from them.
1001 clr_hit_marker(opt->pattern_expression);
1002 grep_buffer_1(opt, name, buf, size, 1);
1004 if (!chk_hit_marker(opt->pattern_expression))
1007 return grep_buffer_1(opt, name, buf, size, 0);