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 NORETURN void compile_regexp_failed(const struct grep_pat *p,
68 sprintf(where, "In '%s' at %d, ", p->origin, p->no);
70 sprintf(where, "%s, ", p->origin);
74 die("%s'%s': %s", where, p->pattern, error);
77 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
81 p->word_regexp = opt->word_regexp;
82 p->ignore_case = opt->ignore_case;
83 p->fixed = opt->fixed;
88 err = regcomp(&p->regexp, p->pattern, opt->regflags);
91 regerror(err, &p->regexp, errbuf, 1024);
93 compile_regexp_failed(p, errbuf);
97 static struct grep_expr *compile_pattern_or(struct grep_pat **);
98 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
107 case GREP_PATTERN: /* atom */
108 case GREP_PATTERN_HEAD:
109 case GREP_PATTERN_BODY:
110 x = xcalloc(1, sizeof (struct grep_expr));
111 x->node = GREP_NODE_ATOM;
115 case GREP_OPEN_PAREN:
117 x = compile_pattern_or(list);
118 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
119 die("unmatched parenthesis");
120 *list = (*list)->next;
127 static struct grep_expr *compile_pattern_not(struct grep_pat **list)
138 die("--not not followed by pattern expression");
140 x = xcalloc(1, sizeof (struct grep_expr));
141 x->node = GREP_NODE_NOT;
142 x->u.unary = compile_pattern_not(list);
144 die("--not followed by non pattern expression");
147 return compile_pattern_atom(list);
151 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
154 struct grep_expr *x, *y, *z;
156 x = compile_pattern_not(list);
158 if (p && p->token == GREP_AND) {
160 die("--and not followed by pattern expression");
162 y = compile_pattern_and(list);
164 die("--and not followed by pattern expression");
165 z = xcalloc(1, sizeof (struct grep_expr));
166 z->node = GREP_NODE_AND;
167 z->u.binary.left = x;
168 z->u.binary.right = y;
174 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
177 struct grep_expr *x, *y, *z;
179 x = compile_pattern_and(list);
181 if (x && p && p->token != GREP_CLOSE_PAREN) {
182 y = compile_pattern_or(list);
184 die("not a pattern expression %s", p->pattern);
185 z = xcalloc(1, sizeof (struct grep_expr));
186 z->node = GREP_NODE_OR;
187 z->u.binary.left = x;
188 z->u.binary.right = y;
194 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
196 return compile_pattern_or(list);
199 static struct grep_expr *grep_true_expr(void)
201 struct grep_expr *z = xcalloc(1, sizeof(*z));
202 z->node = GREP_NODE_TRUE;
206 static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
208 struct grep_expr *z = xcalloc(1, sizeof(*z));
209 z->node = GREP_NODE_OR;
210 z->u.binary.left = left;
211 z->u.binary.right = right;
215 static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
218 struct grep_expr *header_expr;
219 struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]);
220 enum grep_header_field fld;
222 if (!opt->header_list)
224 p = opt->header_list;
225 for (p = opt->header_list; p; p = p->next) {
226 if (p->token != GREP_PATTERN_HEAD)
227 die("bug: a non-header pattern in grep header list.");
228 if (p->field < 0 || GREP_HEADER_FIELD_MAX <= p->field)
229 die("bug: unknown header field %d", p->field);
230 compile_regexp(p, opt);
233 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++)
234 header_group[fld] = NULL;
236 for (p = opt->header_list; p; p = p->next) {
238 struct grep_pat *pp = p;
240 h = compile_pattern_atom(&pp);
241 if (!h || pp != p->next)
242 die("bug: malformed header expr");
243 if (!header_group[p->field]) {
244 header_group[p->field] = h;
247 header_group[p->field] = grep_or_expr(h, header_group[p->field]);
252 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) {
253 if (!header_group[fld])
256 header_expr = grep_true_expr();
257 header_expr = grep_or_expr(header_group[fld], header_expr);
262 void compile_grep_patterns(struct grep_opt *opt)
265 struct grep_expr *header_expr = prep_header_patterns(opt);
267 for (p = opt->pattern_list; p; p = p->next) {
269 case GREP_PATTERN: /* atom */
270 case GREP_PATTERN_HEAD:
271 case GREP_PATTERN_BODY:
272 compile_regexp(p, opt);
280 if (opt->all_match || header_expr)
282 else if (!opt->extended)
285 p = opt->pattern_list;
287 opt->pattern_expression = compile_pattern_expr(&p);
289 die("incomplete pattern expression: %s", p->pattern);
294 if (!opt->pattern_expression)
295 opt->pattern_expression = header_expr;
297 opt->pattern_expression = grep_or_expr(opt->pattern_expression,
302 static void free_pattern_expr(struct grep_expr *x)
309 free_pattern_expr(x->u.unary);
313 free_pattern_expr(x->u.binary.left);
314 free_pattern_expr(x->u.binary.right);
320 void free_grep_patterns(struct grep_opt *opt)
322 struct grep_pat *p, *n;
324 for (p = opt->pattern_list; p; p = n) {
327 case GREP_PATTERN: /* atom */
328 case GREP_PATTERN_HEAD:
329 case GREP_PATTERN_BODY:
340 free_pattern_expr(opt->pattern_expression);
343 static char *end_of_line(char *cp, unsigned long *left)
345 unsigned long l = *left;
346 while (l && *cp != '\n') {
354 static int word_char(char ch)
356 return isalnum(ch) || ch == '_';
359 static void output_color(struct grep_opt *opt, const void *data, size_t size,
362 if (opt->color && color && color[0]) {
363 opt->output(opt, color, strlen(color));
364 opt->output(opt, data, size);
365 opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
367 opt->output(opt, data, size);
370 static void output_sep(struct grep_opt *opt, char sign)
372 if (opt->null_following_name)
373 opt->output(opt, "\0", 1);
375 output_color(opt, &sign, 1, opt->color_sep);
378 static void show_name(struct grep_opt *opt, const char *name)
380 output_color(opt, name, strlen(name), opt->color_filename);
381 opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
384 static int fixmatch(struct grep_pat *p, char *line, char *eol,
389 if (p->ignore_case) {
392 hit = strcasestr(s, p->pattern);
398 hit = memmem(line, eol - line, p->pattern, p->patternlen);
401 match->rm_so = match->rm_eo = -1;
405 match->rm_so = hit - line;
406 match->rm_eo = match->rm_so + p->patternlen;
411 static int regmatch(const regex_t *preg, char *line, char *eol,
412 regmatch_t *match, int eflags)
416 match->rm_eo = eol - line;
417 eflags |= REG_STARTEND;
419 return regexec(preg, line, 1, match, eflags);
422 static int patmatch(struct grep_pat *p, char *line, char *eol,
423 regmatch_t *match, int eflags)
428 hit = !fixmatch(p, line, eol, match);
430 hit = !regmatch(&p->regexp, line, eol, match, eflags);
435 static int strip_timestamp(char *bol, char **eol_p)
440 while (bol < --eol) {
456 { "committer ", 10 },
459 static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
460 enum grep_context ctx,
461 regmatch_t *pmatch, int eflags)
465 const char *start = bol;
467 if ((p->token != GREP_PATTERN) &&
468 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
471 if (p->token == GREP_PATTERN_HEAD) {
474 assert(p->field < ARRAY_SIZE(header_field));
475 field = header_field[p->field].field;
476 len = header_field[p->field].len;
477 if (strncmp(bol, field, len))
480 saved_ch = strip_timestamp(bol, &eol);
484 hit = patmatch(p, bol, eol, pmatch, eflags);
486 if (hit && p->word_regexp) {
487 if ((pmatch[0].rm_so < 0) ||
488 (eol - bol) < pmatch[0].rm_so ||
489 (pmatch[0].rm_eo < 0) ||
490 (eol - bol) < pmatch[0].rm_eo)
491 die("regexp returned nonsense");
493 /* Match beginning must be either beginning of the
494 * line, or at word boundary (i.e. the last char must
495 * not be a word char). Similarly, match end must be
496 * either end of the line, or at word boundary
497 * (i.e. the next char must not be a word char).
499 if ( ((pmatch[0].rm_so == 0) ||
500 !word_char(bol[pmatch[0].rm_so-1])) &&
501 ((pmatch[0].rm_eo == (eol-bol)) ||
502 !word_char(bol[pmatch[0].rm_eo])) )
507 /* Words consist of at least one character. */
508 if (pmatch->rm_so == pmatch->rm_eo)
511 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
512 /* There could be more than one match on the
513 * line, and the first match might not be
514 * strict word match. But later ones could be!
515 * Forward to the next possible start, i.e. the
516 * next position following a non-word char.
518 bol = pmatch[0].rm_so + bol + 1;
519 while (word_char(bol[-1]) && bol < eol)
521 eflags |= REG_NOTBOL;
526 if (p->token == GREP_PATTERN_HEAD && saved_ch)
529 pmatch[0].rm_so += bol - start;
530 pmatch[0].rm_eo += bol - start;
535 static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
536 enum grep_context ctx, int collect_hits)
542 die("Not a valid grep expression");
548 h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
551 h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
554 if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
556 h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
560 return (match_expr_eval(x->u.binary.left,
562 match_expr_eval(x->u.binary.right,
564 h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
565 x->u.binary.left->hit |= h;
566 h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
569 die("Unexpected node type (internal error) %d", x->node);
576 static int match_expr(struct grep_opt *opt, char *bol, char *eol,
577 enum grep_context ctx, int collect_hits)
579 struct grep_expr *x = opt->pattern_expression;
580 return match_expr_eval(x, bol, eol, ctx, collect_hits);
583 static int match_line(struct grep_opt *opt, char *bol, char *eol,
584 enum grep_context ctx, int collect_hits)
590 return match_expr(opt, bol, eol, ctx, collect_hits);
592 /* we do not call with collect_hits without being extended */
593 for (p = opt->pattern_list; p; p = p->next) {
594 if (match_one_pattern(p, bol, eol, ctx, &match, 0))
600 static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
601 enum grep_context ctx,
602 regmatch_t *pmatch, int eflags)
606 if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
608 if (match.rm_so < 0 || match.rm_eo < 0)
610 if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
611 if (match.rm_so > pmatch->rm_so)
613 if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
616 pmatch->rm_so = match.rm_so;
617 pmatch->rm_eo = match.rm_eo;
621 static int next_match(struct grep_opt *opt, char *bol, char *eol,
622 enum grep_context ctx, regmatch_t *pmatch, int eflags)
627 pmatch->rm_so = pmatch->rm_eo = -1;
629 for (p = opt->pattern_list; p; p = p->next) {
631 case GREP_PATTERN: /* atom */
632 case GREP_PATTERN_HEAD:
633 case GREP_PATTERN_BODY:
634 hit |= match_next_pattern(p, bol, eol, ctx,
645 static void show_line(struct grep_opt *opt, char *bol, char *eol,
646 const char *name, unsigned lno, char sign)
648 int rest = eol - bol;
649 char *line_color = NULL;
651 if (opt->pre_context || opt->post_context) {
652 if (opt->last_shown == 0) {
653 if (opt->show_hunk_mark) {
654 output_color(opt, "--", 2, opt->color_sep);
655 opt->output(opt, "\n", 1);
657 } else if (lno > opt->last_shown + 1) {
658 output_color(opt, "--", 2, opt->color_sep);
659 opt->output(opt, "\n", 1);
662 opt->last_shown = lno;
665 output_color(opt, name, strlen(name), opt->color_filename);
666 output_sep(opt, sign);
670 snprintf(buf, sizeof(buf), "%d", lno);
671 output_color(opt, buf, strlen(buf), opt->color_lineno);
672 output_sep(opt, sign);
676 enum grep_context ctx = GREP_CONTEXT_BODY;
681 line_color = opt->color_selected;
682 else if (sign == '-')
683 line_color = opt->color_context;
684 else if (sign == '=')
685 line_color = opt->color_function;
687 while (next_match(opt, bol, eol, ctx, &match, eflags)) {
688 if (match.rm_so == match.rm_eo)
691 output_color(opt, bol, match.rm_so, line_color);
692 output_color(opt, bol + match.rm_so,
693 match.rm_eo - match.rm_so,
701 output_color(opt, bol, rest, line_color);
702 opt->output(opt, "\n", 1);
705 static int match_funcname(struct grep_opt *opt, char *bol, char *eol)
707 xdemitconf_t *xecfg = opt->priv;
708 if (xecfg && xecfg->find_func) {
710 return xecfg->find_func(bol, eol - bol, buf, 1,
711 xecfg->find_func_priv) >= 0;
716 if (isalpha(*bol) || *bol == '_' || *bol == '$')
721 static void show_funcname_line(struct grep_opt *opt, const char *name,
722 char *buf, char *bol, unsigned lno)
727 while (bol > buf && bol[-1] != '\n')
731 if (lno <= opt->last_shown)
734 if (match_funcname(opt, bol, eol)) {
735 show_line(opt, bol, eol, name, lno, '=');
741 static void show_pre_context(struct grep_opt *opt, const char *name, char *buf,
742 char *bol, unsigned lno)
744 unsigned cur = lno, from = 1, funcname_lno = 0;
745 int funcname_needed = opt->funcname;
747 if (opt->pre_context < lno)
748 from = lno - opt->pre_context;
749 if (from <= opt->last_shown)
750 from = opt->last_shown + 1;
753 while (bol > buf && cur > from) {
756 while (bol > buf && bol[-1] != '\n')
759 if (funcname_needed && match_funcname(opt, bol, eol)) {
765 /* We need to look even further back to find a function signature. */
766 if (opt->funcname && funcname_needed)
767 show_funcname_line(opt, name, buf, bol, cur);
771 char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
775 show_line(opt, bol, eol, name, cur, sign);
781 static int should_lookahead(struct grep_opt *opt)
786 return 0; /* punt for too complex stuff */
789 for (p = opt->pattern_list; p; p = p->next) {
790 if (p->token != GREP_PATTERN)
791 return 0; /* punt for "header only" and stuff */
796 static int look_ahead(struct grep_opt *opt,
797 unsigned long *left_p,
801 unsigned lno = *lno_p;
805 regoff_t earliest = -1;
807 for (p = opt->pattern_list; p; p = p->next) {
811 hit = patmatch(p, bol, bol + *left_p, &m, 0);
812 if (!hit || m.rm_so < 0 || m.rm_eo < 0)
814 if (earliest < 0 || m.rm_so < earliest)
819 *bol_p = bol + *left_p;
823 for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
824 ; /* find the beginning of the line */
827 for (sp = bol; sp < last_bol; sp++) {
831 *left_p -= last_bol - bol;
837 int grep_threads_ok(const struct grep_opt *opt)
839 /* If this condition is true, then we may use the attribute
840 * machinery in grep_buffer_1. The attribute code is not
841 * thread safe, so we disable the use of threads.
843 if (opt->funcname && !opt->unmatch_name_only && !opt->status_only &&
850 static void std_output(struct grep_opt *opt, const void *buf, size_t size)
852 fwrite(buf, size, 1, stdout);
855 static int grep_buffer_1(struct grep_opt *opt, const char *name,
856 char *buf, unsigned long size, int collect_hits)
859 unsigned long left = size;
861 unsigned last_hit = 0;
862 int binary_match_only = 0;
864 int try_lookahead = 0;
865 enum grep_context ctx = GREP_CONTEXT_HEAD;
869 opt->output = std_output;
871 if (opt->last_shown && (opt->pre_context || opt->post_context) &&
872 opt->output == std_output)
873 opt->show_hunk_mark = 1;
876 switch (opt->binary) {
877 case GREP_BINARY_DEFAULT:
878 if (buffer_is_binary(buf, size))
879 binary_match_only = 1;
881 case GREP_BINARY_NOMATCH:
882 if (buffer_is_binary(buf, size))
883 return 0; /* Assume unmatch */
885 case GREP_BINARY_TEXT:
888 die("bug: unknown binary handling mode");
891 memset(&xecfg, 0, sizeof(xecfg));
892 if (opt->funcname && !opt->unmatch_name_only && !opt->status_only &&
893 !opt->name_only && !binary_match_only && !collect_hits) {
894 struct userdiff_driver *drv = userdiff_find_by_path(name);
895 if (drv && drv->funcname.pattern) {
896 const struct userdiff_funcname *pe = &drv->funcname;
897 xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
901 try_lookahead = should_lookahead(opt);
908 * look_ahead() skips quickly to the line that possibly
909 * has the next hit; don't call it if we need to do
910 * something more than just skipping the current line
911 * in response to an unmatch for the current line. E.g.
912 * inside a post-context window, we will show the current
913 * line as a context around the previous hit when it
918 && lno <= last_hit + opt->post_context)
919 && look_ahead(opt, &left, &lno, &bol))
921 eol = end_of_line(bol, &left);
925 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
926 ctx = GREP_CONTEXT_BODY;
928 hit = match_line(opt, bol, eol, ctx, collect_hits);
934 /* "grep -v -e foo -e bla" should list lines
935 * that do not have either, so inversion should
940 if (opt->unmatch_name_only) {
947 if (opt->status_only)
949 if (opt->name_only) {
950 show_name(opt, name);
955 if (binary_match_only) {
956 opt->output(opt, "Binary file ", 12);
957 output_color(opt, name, strlen(name),
958 opt->color_filename);
959 opt->output(opt, " matches\n", 9);
962 /* Hit at this line. If we haven't shown the
963 * pre-context lines, we would need to show them.
965 if (opt->pre_context)
966 show_pre_context(opt, name, buf, bol, lno);
967 else if (opt->funcname)
968 show_funcname_line(opt, name, buf, bol, lno);
969 show_line(opt, bol, eol, name, lno, ':');
973 lno <= last_hit + opt->post_context) {
974 /* If the last hit is within the post context,
975 * we need to show this line.
977 show_line(opt, bol, eol, name, lno, '-');
991 if (opt->status_only)
993 if (opt->unmatch_name_only) {
994 /* We did not see any hit, so we want to show this */
995 show_name(opt, name);
999 xdiff_clear_find_func(&xecfg);
1003 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1004 * which feels mostly useless but sometimes useful. Maybe
1005 * make it another option? For now suppress them.
1007 if (opt->count && count) {
1009 output_color(opt, name, strlen(name), opt->color_filename);
1010 output_sep(opt, ':');
1011 snprintf(buf, sizeof(buf), "%u\n", count);
1012 opt->output(opt, buf, strlen(buf));
1018 static void clr_hit_marker(struct grep_expr *x)
1020 /* All-hit markers are meaningful only at the very top level
1025 if (x->node != GREP_NODE_OR)
1027 x->u.binary.left->hit = 0;
1028 x = x->u.binary.right;
1032 static int chk_hit_marker(struct grep_expr *x)
1034 /* Top level nodes have hit markers. See if they all are hits */
1036 if (x->node != GREP_NODE_OR)
1038 if (!x->u.binary.left->hit)
1040 x = x->u.binary.right;
1044 int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size)
1047 * we do not have to do the two-pass grep when we do not check
1048 * buffer-wide "all-match".
1050 if (!opt->all_match)
1051 return grep_buffer_1(opt, name, buf, size, 0);
1053 /* Otherwise the toplevel "or" terms hit a bit differently.
1054 * We first clear hit markers from them.
1056 clr_hit_marker(opt->pattern_expression);
1057 grep_buffer_1(opt, name, buf, size, 1);
1059 if (!chk_hit_marker(opt->pattern_expression))
1062 return grep_buffer_1(opt, name, buf, size, 0);