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));
12 p->token = GREP_PATTERN_HEAD;
14 *opt->pattern_tail = p;
15 opt->pattern_tail = &p->next;
19 void append_grep_pattern(struct grep_opt *opt, const char *pat,
20 const char *origin, int no, enum grep_pat_token t)
22 struct grep_pat *p = xcalloc(1, sizeof(*p));
27 *opt->pattern_tail = p;
28 opt->pattern_tail = &p->next;
32 struct grep_opt *grep_opt_dup(const struct grep_opt *opt)
35 struct grep_opt *ret = xmalloc(sizeof(struct grep_opt));
38 ret->pattern_list = NULL;
39 ret->pattern_tail = &ret->pattern_list;
41 for(pat = opt->pattern_list; pat != NULL; pat = pat->next)
43 if(pat->token == GREP_PATTERN_HEAD)
44 append_header_grep_pattern(ret, pat->field,
47 append_grep_pattern(ret, pat->pattern, pat->origin,
54 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
58 p->word_regexp = opt->word_regexp;
59 p->ignore_case = opt->ignore_case;
63 if (opt->regflags & REG_ICASE)
68 err = regcomp(&p->regexp, p->pattern, opt->regflags);
73 sprintf(where, "In '%s' at %d, ",
76 sprintf(where, "%s, ", p->origin);
79 regerror(err, &p->regexp, errbuf, 1024);
81 die("%s'%s': %s", where, p->pattern, errbuf);
85 static struct grep_expr *compile_pattern_or(struct grep_pat **);
86 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
95 case GREP_PATTERN: /* atom */
96 case GREP_PATTERN_HEAD:
97 case GREP_PATTERN_BODY:
98 x = xcalloc(1, sizeof (struct grep_expr));
99 x->node = GREP_NODE_ATOM;
103 case GREP_OPEN_PAREN:
105 x = compile_pattern_or(list);
106 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
107 die("unmatched parenthesis");
108 *list = (*list)->next;
115 static struct grep_expr *compile_pattern_not(struct grep_pat **list)
126 die("--not not followed by pattern expression");
128 x = xcalloc(1, sizeof (struct grep_expr));
129 x->node = GREP_NODE_NOT;
130 x->u.unary = compile_pattern_not(list);
132 die("--not followed by non pattern expression");
135 return compile_pattern_atom(list);
139 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
142 struct grep_expr *x, *y, *z;
144 x = compile_pattern_not(list);
146 if (p && p->token == GREP_AND) {
148 die("--and not followed by pattern expression");
150 y = compile_pattern_and(list);
152 die("--and not followed by pattern expression");
153 z = xcalloc(1, sizeof (struct grep_expr));
154 z->node = GREP_NODE_AND;
155 z->u.binary.left = x;
156 z->u.binary.right = y;
162 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
165 struct grep_expr *x, *y, *z;
167 x = compile_pattern_and(list);
169 if (x && p && p->token != GREP_CLOSE_PAREN) {
170 y = compile_pattern_or(list);
172 die("not a pattern expression %s", p->pattern);
173 z = xcalloc(1, sizeof (struct grep_expr));
174 z->node = GREP_NODE_OR;
175 z->u.binary.left = x;
176 z->u.binary.right = y;
182 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
184 return compile_pattern_or(list);
187 void compile_grep_patterns(struct grep_opt *opt)
194 for (p = opt->pattern_list; p; p = p->next) {
196 case GREP_PATTERN: /* atom */
197 case GREP_PATTERN_HEAD:
198 case GREP_PATTERN_BODY:
199 compile_regexp(p, opt);
210 /* Then bundle them up in an expression.
211 * A classic recursive descent parser would do.
213 p = opt->pattern_list;
215 opt->pattern_expression = compile_pattern_expr(&p);
217 die("incomplete pattern expression: %s", p->pattern);
220 static void free_pattern_expr(struct grep_expr *x)
226 free_pattern_expr(x->u.unary);
230 free_pattern_expr(x->u.binary.left);
231 free_pattern_expr(x->u.binary.right);
237 void free_grep_patterns(struct grep_opt *opt)
239 struct grep_pat *p, *n;
241 for (p = opt->pattern_list; p; p = n) {
244 case GREP_PATTERN: /* atom */
245 case GREP_PATTERN_HEAD:
246 case GREP_PATTERN_BODY:
257 free_pattern_expr(opt->pattern_expression);
260 static char *end_of_line(char *cp, unsigned long *left)
262 unsigned long l = *left;
263 while (l && *cp != '\n') {
271 static int word_char(char ch)
273 return isalnum(ch) || ch == '_';
276 static void show_name(struct grep_opt *opt, const char *name)
278 opt->output(opt, name, strlen(name));
279 opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
283 static int fixmatch(const char *pattern, char *line, int ignore_case, regmatch_t *match)
287 hit = strcasestr(line, pattern);
289 hit = strstr(line, pattern);
292 match->rm_so = match->rm_eo = -1;
296 match->rm_so = hit - line;
297 match->rm_eo = match->rm_so + strlen(pattern);
302 static int strip_timestamp(char *bol, char **eol_p)
307 while (bol < --eol) {
323 { "committer ", 10 },
326 static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
327 enum grep_context ctx,
328 regmatch_t *pmatch, int eflags)
332 const char *start = bol;
334 if ((p->token != GREP_PATTERN) &&
335 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
338 if (p->token == GREP_PATTERN_HEAD) {
341 assert(p->field < ARRAY_SIZE(header_field));
342 field = header_field[p->field].field;
343 len = header_field[p->field].len;
344 if (strncmp(bol, field, len))
347 saved_ch = strip_timestamp(bol, &eol);
352 hit = !fixmatch(p->pattern, bol, p->ignore_case, pmatch);
354 hit = !regexec(&p->regexp, bol, 1, pmatch, eflags);
356 if (hit && p->word_regexp) {
357 if ((pmatch[0].rm_so < 0) ||
358 (eol - bol) < pmatch[0].rm_so ||
359 (pmatch[0].rm_eo < 0) ||
360 (eol - bol) < pmatch[0].rm_eo)
361 die("regexp returned nonsense");
363 /* Match beginning must be either beginning of the
364 * line, or at word boundary (i.e. the last char must
365 * not be a word char). Similarly, match end must be
366 * either end of the line, or at word boundary
367 * (i.e. the next char must not be a word char).
369 if ( ((pmatch[0].rm_so == 0) ||
370 !word_char(bol[pmatch[0].rm_so-1])) &&
371 ((pmatch[0].rm_eo == (eol-bol)) ||
372 !word_char(bol[pmatch[0].rm_eo])) )
377 /* Words consist of at least one character. */
378 if (pmatch->rm_so == pmatch->rm_eo)
381 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
382 /* There could be more than one match on the
383 * line, and the first match might not be
384 * strict word match. But later ones could be!
385 * Forward to the next possible start, i.e. the
386 * next position following a non-word char.
388 bol = pmatch[0].rm_so + bol + 1;
389 while (word_char(bol[-1]) && bol < eol)
391 eflags |= REG_NOTBOL;
396 if (p->token == GREP_PATTERN_HEAD && saved_ch)
399 pmatch[0].rm_so += bol - start;
400 pmatch[0].rm_eo += bol - start;
405 static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
406 enum grep_context ctx, int collect_hits)
412 die("Not a valid grep expression");
415 h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
418 h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
421 if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
423 h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
427 return (match_expr_eval(x->u.binary.left,
429 match_expr_eval(x->u.binary.right,
431 h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
432 x->u.binary.left->hit |= h;
433 h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
436 die("Unexpected node type (internal error) %d", x->node);
443 static int match_expr(struct grep_opt *opt, char *bol, char *eol,
444 enum grep_context ctx, int collect_hits)
446 struct grep_expr *x = opt->pattern_expression;
447 return match_expr_eval(x, bol, eol, ctx, collect_hits);
450 static int match_line(struct grep_opt *opt, char *bol, char *eol,
451 enum grep_context ctx, int collect_hits)
457 return match_expr(opt, bol, eol, ctx, collect_hits);
459 /* we do not call with collect_hits without being extended */
460 for (p = opt->pattern_list; p; p = p->next) {
461 if (match_one_pattern(p, bol, eol, ctx, &match, 0))
467 static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
468 enum grep_context ctx,
469 regmatch_t *pmatch, int eflags)
473 if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
475 if (match.rm_so < 0 || match.rm_eo < 0)
477 if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
478 if (match.rm_so > pmatch->rm_so)
480 if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
483 pmatch->rm_so = match.rm_so;
484 pmatch->rm_eo = match.rm_eo;
488 static int next_match(struct grep_opt *opt, char *bol, char *eol,
489 enum grep_context ctx, regmatch_t *pmatch, int eflags)
494 pmatch->rm_so = pmatch->rm_eo = -1;
496 for (p = opt->pattern_list; p; p = p->next) {
498 case GREP_PATTERN: /* atom */
499 case GREP_PATTERN_HEAD:
500 case GREP_PATTERN_BODY:
501 hit |= match_next_pattern(p, bol, eol, ctx,
512 static void show_line(struct grep_opt *opt, char *bol, char *eol,
513 const char *name, unsigned lno, char sign)
515 int rest = eol - bol;
519 if (opt->pre_context || opt->post_context) {
520 if (opt->last_shown == 0) {
521 if (opt->show_hunk_mark)
522 opt->output(opt, "--\n", 3);
524 opt->show_hunk_mark = 1;
525 } else if (lno > opt->last_shown + 1)
526 opt->output(opt, "--\n", 3);
528 opt->last_shown = lno;
530 if (opt->null_following_name)
533 opt->output(opt, name, strlen(name));
534 opt->output(opt, sign_str, 1);
538 snprintf(buf, sizeof(buf), "%d", lno);
539 opt->output(opt, buf, strlen(buf));
540 opt->output(opt, sign_str, 1);
544 enum grep_context ctx = GREP_CONTEXT_BODY;
549 while (next_match(opt, bol, eol, ctx, &match, eflags)) {
550 if (match.rm_so == match.rm_eo)
553 opt->output(opt, bol, match.rm_so);
554 opt->output(opt, opt->color_match,
555 strlen(opt->color_match));
556 opt->output(opt, bol + match.rm_so,
557 (int)(match.rm_eo - match.rm_so));
558 opt->output(opt, GIT_COLOR_RESET,
559 strlen(GIT_COLOR_RESET));
566 opt->output(opt, bol, rest);
567 opt->output(opt, "\n", 1);
570 static int match_funcname(struct grep_opt *opt, char *bol, char *eol)
572 xdemitconf_t *xecfg = opt->priv;
573 if (xecfg && xecfg->find_func) {
575 return xecfg->find_func(bol, eol - bol, buf, 1,
576 xecfg->find_func_priv) >= 0;
581 if (isalpha(*bol) || *bol == '_' || *bol == '$')
586 static void show_funcname_line(struct grep_opt *opt, const char *name,
587 char *buf, char *bol, unsigned lno)
592 while (bol > buf && bol[-1] != '\n')
596 if (lno <= opt->last_shown)
599 if (match_funcname(opt, bol, eol)) {
600 show_line(opt, bol, eol, name, lno, '=');
606 static void show_pre_context(struct grep_opt *opt, const char *name, char *buf,
607 char *bol, unsigned lno)
609 unsigned cur = lno, from = 1, funcname_lno = 0;
610 int funcname_needed = opt->funcname;
612 if (opt->pre_context < lno)
613 from = lno - opt->pre_context;
614 if (from <= opt->last_shown)
615 from = opt->last_shown + 1;
618 while (bol > buf && cur > from) {
621 while (bol > buf && bol[-1] != '\n')
624 if (funcname_needed && match_funcname(opt, bol, eol)) {
630 /* We need to look even further back to find a function signature. */
631 if (opt->funcname && funcname_needed)
632 show_funcname_line(opt, name, buf, bol, cur);
636 char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
640 show_line(opt, bol, eol, name, cur, sign);
646 static int should_lookahead(struct grep_opt *opt)
651 return 0; /* punt for too complex stuff */
654 for (p = opt->pattern_list; p; p = p->next) {
655 if (p->token != GREP_PATTERN)
656 return 0; /* punt for "header only" and stuff */
661 static int look_ahead(struct grep_opt *opt,
662 unsigned long *left_p,
666 unsigned lno = *lno_p;
670 regoff_t earliest = -1;
672 for (p = opt->pattern_list; p; p = p->next) {
677 hit = !fixmatch(p->pattern, bol, p->ignore_case, &m);
679 hit = !regexec(&p->regexp, bol, 1, &m, 0);
680 if (!hit || m.rm_so < 0 || m.rm_eo < 0)
682 if (earliest < 0 || m.rm_so < earliest)
687 *bol_p = bol + *left_p;
691 for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
692 ; /* find the beginning of the line */
695 for (sp = bol; sp < last_bol; sp++) {
699 *left_p -= last_bol - bol;
705 int grep_threads_ok(const struct grep_opt *opt)
707 /* If this condition is true, then we may use the attribute
708 * machinery in grep_buffer_1. The attribute code is not
709 * thread safe, so we disable the use of threads.
711 if (opt->funcname && !opt->unmatch_name_only && !opt->status_only &&
715 /* If we are showing hunk marks, we should not do it for the
716 * first match. The synchronization problem we get for this
717 * constraint is not yet solved, so we disable threading in
720 if (opt->pre_context || opt->post_context)
726 static void std_output(struct grep_opt *opt, const void *buf, size_t size)
728 fwrite(buf, size, 1, stdout);
731 static int grep_buffer_1(struct grep_opt *opt, const char *name,
732 char *buf, unsigned long size, int collect_hits)
735 unsigned long left = size;
737 unsigned last_hit = 0;
738 int binary_match_only = 0;
740 int try_lookahead = 0;
741 enum grep_context ctx = GREP_CONTEXT_HEAD;
747 opt->output = std_output;
749 if (buffer_is_binary(buf, size)) {
750 switch (opt->binary) {
751 case GREP_BINARY_DEFAULT:
752 binary_match_only = 1;
754 case GREP_BINARY_NOMATCH:
755 return 0; /* Assume unmatch */
762 memset(&xecfg, 0, sizeof(xecfg));
763 if (opt->funcname && !opt->unmatch_name_only && !opt->status_only &&
764 !opt->name_only && !binary_match_only && !collect_hits) {
765 struct userdiff_driver *drv = userdiff_find_by_path(name);
766 if (drv && drv->funcname.pattern) {
767 const struct userdiff_funcname *pe = &drv->funcname;
768 xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
772 try_lookahead = should_lookahead(opt);
779 * look_ahead() skips quicly to the line that possibly
780 * has the next hit; don't call it if we need to do
781 * something more than just skipping the current line
782 * in response to an unmatch for the current line. E.g.
783 * inside a post-context window, we will show the current
784 * line as a context around the previous hit when it
789 && lno <= last_hit + opt->post_context)
790 && look_ahead(opt, &left, &lno, &bol))
792 eol = end_of_line(bol, &left);
796 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
797 ctx = GREP_CONTEXT_BODY;
799 hit = match_line(opt, bol, eol, ctx, collect_hits);
805 /* "grep -v -e foo -e bla" should list lines
806 * that do not have either, so inversion should
811 if (opt->unmatch_name_only) {
818 if (opt->status_only)
820 if (binary_match_only) {
821 opt->output(opt, "Binary file ", 12);
822 opt->output(opt, name, strlen(name));
823 opt->output(opt, " matches\n", 9);
826 if (opt->name_only) {
827 show_name(opt, name);
830 /* Hit at this line. If we haven't shown the
831 * pre-context lines, we would need to show them.
832 * When asked to do "count", this still show
833 * the context which is nonsense, but the user
834 * deserves to get that ;-).
836 if (opt->pre_context)
837 show_pre_context(opt, name, buf, bol, lno);
838 else if (opt->funcname)
839 show_funcname_line(opt, name, buf, bol, lno);
841 show_line(opt, bol, eol, name, lno, ':');
845 lno <= last_hit + opt->post_context) {
846 /* If the last hit is within the post context,
847 * we need to show this line.
849 show_line(opt, bol, eol, name, lno, '-');
863 if (opt->status_only)
865 if (opt->unmatch_name_only) {
866 /* We did not see any hit, so we want to show this */
867 show_name(opt, name);
871 xdiff_clear_find_func(&xecfg);
875 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
876 * which feels mostly useless but sometimes useful. Maybe
877 * make it another option? For now suppress them.
879 if (opt->count && count) {
881 opt->output(opt, name, strlen(name));
882 snprintf(buf, sizeof(buf), "%c%u\n",
883 opt->null_following_name ? '\0' : ':', count);
884 opt->output(opt, buf, strlen(buf));
889 static void clr_hit_marker(struct grep_expr *x)
891 /* All-hit markers are meaningful only at the very top level
896 if (x->node != GREP_NODE_OR)
898 x->u.binary.left->hit = 0;
899 x = x->u.binary.right;
903 static int chk_hit_marker(struct grep_expr *x)
905 /* Top level nodes have hit markers. See if they all are hits */
907 if (x->node != GREP_NODE_OR)
909 if (!x->u.binary.left->hit)
911 x = x->u.binary.right;
915 int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size)
918 * we do not have to do the two-pass grep when we do not check
919 * buffer-wide "all-match".
922 return grep_buffer_1(opt, name, buf, size, 0);
924 /* Otherwise the toplevel "or" terms hit a bit differently.
925 * We first clear hit markers from them.
927 clr_hit_marker(opt->pattern_expression);
928 grep_buffer_1(opt, name, buf, size, 1);
930 if (!chk_hit_marker(opt->pattern_expression))
933 return grep_buffer_1(opt, name, buf, size, 0);