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->header_tail = p;
15 opt->header_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;
60 p->fixed = opt->fixed;
65 err = regcomp(&p->regexp, p->pattern, opt->regflags);
70 sprintf(where, "In '%s' at %d, ",
73 sprintf(where, "%s, ", p->origin);
76 regerror(err, &p->regexp, errbuf, 1024);
78 die("%s'%s': %s", where, p->pattern, errbuf);
82 static struct grep_expr *compile_pattern_or(struct grep_pat **);
83 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
92 case GREP_PATTERN: /* atom */
93 case GREP_PATTERN_HEAD:
94 case GREP_PATTERN_BODY:
95 x = xcalloc(1, sizeof (struct grep_expr));
96 x->node = GREP_NODE_ATOM;
100 case GREP_OPEN_PAREN:
102 x = compile_pattern_or(list);
103 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
104 die("unmatched parenthesis");
105 *list = (*list)->next;
112 static struct grep_expr *compile_pattern_not(struct grep_pat **list)
123 die("--not not followed by pattern expression");
125 x = xcalloc(1, sizeof (struct grep_expr));
126 x->node = GREP_NODE_NOT;
127 x->u.unary = compile_pattern_not(list);
129 die("--not followed by non pattern expression");
132 return compile_pattern_atom(list);
136 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
139 struct grep_expr *x, *y, *z;
141 x = compile_pattern_not(list);
143 if (p && p->token == GREP_AND) {
145 die("--and not followed by pattern expression");
147 y = compile_pattern_and(list);
149 die("--and not followed by pattern expression");
150 z = xcalloc(1, sizeof (struct grep_expr));
151 z->node = GREP_NODE_AND;
152 z->u.binary.left = x;
153 z->u.binary.right = y;
159 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
162 struct grep_expr *x, *y, *z;
164 x = compile_pattern_and(list);
166 if (x && p && p->token != GREP_CLOSE_PAREN) {
167 y = compile_pattern_or(list);
169 die("not a pattern expression %s", p->pattern);
170 z = xcalloc(1, sizeof (struct grep_expr));
171 z->node = GREP_NODE_OR;
172 z->u.binary.left = x;
173 z->u.binary.right = y;
179 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
181 return compile_pattern_or(list);
184 void compile_grep_patterns(struct grep_opt *opt)
187 struct grep_expr *header_expr = NULL;
189 if (opt->header_list) {
190 p = opt->header_list;
191 header_expr = compile_pattern_expr(&p);
193 die("incomplete pattern expression: %s", p->pattern);
194 for (p = opt->header_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);
208 for (p = opt->pattern_list; p; p = p->next) {
210 case GREP_PATTERN: /* atom */
211 case GREP_PATTERN_HEAD:
212 case GREP_PATTERN_BODY:
213 compile_regexp(p, opt);
221 if (opt->all_match || header_expr)
223 else if (!opt->extended)
226 /* Then bundle them up in an expression.
227 * A classic recursive descent parser would do.
229 p = opt->pattern_list;
231 opt->pattern_expression = compile_pattern_expr(&p);
233 die("incomplete pattern expression: %s", p->pattern);
238 if (opt->pattern_expression) {
240 z = xcalloc(1, sizeof(*z));
241 z->node = GREP_NODE_OR;
242 z->u.binary.left = opt->pattern_expression;
243 z->u.binary.right = header_expr;
244 opt->pattern_expression = z;
246 opt->pattern_expression = header_expr;
251 static void free_pattern_expr(struct grep_expr *x)
257 free_pattern_expr(x->u.unary);
261 free_pattern_expr(x->u.binary.left);
262 free_pattern_expr(x->u.binary.right);
268 void free_grep_patterns(struct grep_opt *opt)
270 struct grep_pat *p, *n;
272 for (p = opt->pattern_list; p; p = n) {
275 case GREP_PATTERN: /* atom */
276 case GREP_PATTERN_HEAD:
277 case GREP_PATTERN_BODY:
288 free_pattern_expr(opt->pattern_expression);
291 static char *end_of_line(char *cp, unsigned long *left)
293 unsigned long l = *left;
294 while (l && *cp != '\n') {
302 static int word_char(char ch)
304 return isalnum(ch) || ch == '_';
307 static void show_name(struct grep_opt *opt, const char *name)
309 opt->output(opt, name, strlen(name));
310 opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
314 static int fixmatch(const char *pattern, char *line, int ignore_case, regmatch_t *match)
318 hit = strcasestr(line, pattern);
320 hit = strstr(line, pattern);
323 match->rm_so = match->rm_eo = -1;
327 match->rm_so = hit - line;
328 match->rm_eo = match->rm_so + strlen(pattern);
333 static int strip_timestamp(char *bol, char **eol_p)
338 while (bol < --eol) {
354 { "committer ", 10 },
357 static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
358 enum grep_context ctx,
359 regmatch_t *pmatch, int eflags)
363 const char *start = bol;
365 if ((p->token != GREP_PATTERN) &&
366 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
369 if (p->token == GREP_PATTERN_HEAD) {
372 assert(p->field < ARRAY_SIZE(header_field));
373 field = header_field[p->field].field;
374 len = header_field[p->field].len;
375 if (strncmp(bol, field, len))
378 saved_ch = strip_timestamp(bol, &eol);
383 hit = !fixmatch(p->pattern, bol, p->ignore_case, pmatch);
385 hit = !regexec(&p->regexp, bol, 1, pmatch, eflags);
387 if (hit && p->word_regexp) {
388 if ((pmatch[0].rm_so < 0) ||
389 (eol - bol) < pmatch[0].rm_so ||
390 (pmatch[0].rm_eo < 0) ||
391 (eol - bol) < pmatch[0].rm_eo)
392 die("regexp returned nonsense");
394 /* Match beginning must be either beginning of the
395 * line, or at word boundary (i.e. the last char must
396 * not be a word char). Similarly, match end must be
397 * either end of the line, or at word boundary
398 * (i.e. the next char must not be a word char).
400 if ( ((pmatch[0].rm_so == 0) ||
401 !word_char(bol[pmatch[0].rm_so-1])) &&
402 ((pmatch[0].rm_eo == (eol-bol)) ||
403 !word_char(bol[pmatch[0].rm_eo])) )
408 /* Words consist of at least one character. */
409 if (pmatch->rm_so == pmatch->rm_eo)
412 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
413 /* There could be more than one match on the
414 * line, and the first match might not be
415 * strict word match. But later ones could be!
416 * Forward to the next possible start, i.e. the
417 * next position following a non-word char.
419 bol = pmatch[0].rm_so + bol + 1;
420 while (word_char(bol[-1]) && bol < eol)
422 eflags |= REG_NOTBOL;
427 if (p->token == GREP_PATTERN_HEAD && saved_ch)
430 pmatch[0].rm_so += bol - start;
431 pmatch[0].rm_eo += bol - start;
436 static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
437 enum grep_context ctx, int collect_hits)
443 die("Not a valid grep expression");
446 h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
449 h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
452 if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
454 h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
458 return (match_expr_eval(x->u.binary.left,
460 match_expr_eval(x->u.binary.right,
462 h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
463 x->u.binary.left->hit |= h;
464 h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
467 die("Unexpected node type (internal error) %d", x->node);
474 static int match_expr(struct grep_opt *opt, char *bol, char *eol,
475 enum grep_context ctx, int collect_hits)
477 struct grep_expr *x = opt->pattern_expression;
478 return match_expr_eval(x, bol, eol, ctx, collect_hits);
481 static int match_line(struct grep_opt *opt, char *bol, char *eol,
482 enum grep_context ctx, int collect_hits)
488 return match_expr(opt, bol, eol, ctx, collect_hits);
490 /* we do not call with collect_hits without being extended */
491 for (p = opt->pattern_list; p; p = p->next) {
492 if (match_one_pattern(p, bol, eol, ctx, &match, 0))
498 static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
499 enum grep_context ctx,
500 regmatch_t *pmatch, int eflags)
504 if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
506 if (match.rm_so < 0 || match.rm_eo < 0)
508 if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
509 if (match.rm_so > pmatch->rm_so)
511 if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
514 pmatch->rm_so = match.rm_so;
515 pmatch->rm_eo = match.rm_eo;
519 static int next_match(struct grep_opt *opt, char *bol, char *eol,
520 enum grep_context ctx, regmatch_t *pmatch, int eflags)
525 pmatch->rm_so = pmatch->rm_eo = -1;
527 for (p = opt->pattern_list; p; p = p->next) {
529 case GREP_PATTERN: /* atom */
530 case GREP_PATTERN_HEAD:
531 case GREP_PATTERN_BODY:
532 hit |= match_next_pattern(p, bol, eol, ctx,
543 static void show_line(struct grep_opt *opt, char *bol, char *eol,
544 const char *name, unsigned lno, char sign)
546 int rest = eol - bol;
550 if (opt->pre_context || opt->post_context) {
551 if (opt->last_shown == 0) {
552 if (opt->show_hunk_mark)
553 opt->output(opt, "--\n", 3);
555 opt->show_hunk_mark = 1;
556 } else if (lno > opt->last_shown + 1)
557 opt->output(opt, "--\n", 3);
559 opt->last_shown = lno;
561 if (opt->null_following_name)
564 opt->output(opt, name, strlen(name));
565 opt->output(opt, sign_str, 1);
569 snprintf(buf, sizeof(buf), "%d", lno);
570 opt->output(opt, buf, strlen(buf));
571 opt->output(opt, sign_str, 1);
575 enum grep_context ctx = GREP_CONTEXT_BODY;
580 while (next_match(opt, bol, eol, ctx, &match, eflags)) {
581 if (match.rm_so == match.rm_eo)
584 opt->output(opt, bol, match.rm_so);
585 opt->output(opt, opt->color_match,
586 strlen(opt->color_match));
587 opt->output(opt, bol + match.rm_so,
588 (int)(match.rm_eo - match.rm_so));
589 opt->output(opt, GIT_COLOR_RESET,
590 strlen(GIT_COLOR_RESET));
597 opt->output(opt, bol, rest);
598 opt->output(opt, "\n", 1);
601 static int match_funcname(struct grep_opt *opt, char *bol, char *eol)
603 xdemitconf_t *xecfg = opt->priv;
604 if (xecfg && xecfg->find_func) {
606 return xecfg->find_func(bol, eol - bol, buf, 1,
607 xecfg->find_func_priv) >= 0;
612 if (isalpha(*bol) || *bol == '_' || *bol == '$')
617 static void show_funcname_line(struct grep_opt *opt, const char *name,
618 char *buf, char *bol, unsigned lno)
623 while (bol > buf && bol[-1] != '\n')
627 if (lno <= opt->last_shown)
630 if (match_funcname(opt, bol, eol)) {
631 show_line(opt, bol, eol, name, lno, '=');
637 static void show_pre_context(struct grep_opt *opt, const char *name, char *buf,
638 char *bol, unsigned lno)
640 unsigned cur = lno, from = 1, funcname_lno = 0;
641 int funcname_needed = opt->funcname;
643 if (opt->pre_context < lno)
644 from = lno - opt->pre_context;
645 if (from <= opt->last_shown)
646 from = opt->last_shown + 1;
649 while (bol > buf && cur > from) {
652 while (bol > buf && bol[-1] != '\n')
655 if (funcname_needed && match_funcname(opt, bol, eol)) {
661 /* We need to look even further back to find a function signature. */
662 if (opt->funcname && funcname_needed)
663 show_funcname_line(opt, name, buf, bol, cur);
667 char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
671 show_line(opt, bol, eol, name, cur, sign);
677 static int should_lookahead(struct grep_opt *opt)
682 return 0; /* punt for too complex stuff */
685 for (p = opt->pattern_list; p; p = p->next) {
686 if (p->token != GREP_PATTERN)
687 return 0; /* punt for "header only" and stuff */
692 static int look_ahead(struct grep_opt *opt,
693 unsigned long *left_p,
697 unsigned lno = *lno_p;
701 regoff_t earliest = -1;
703 for (p = opt->pattern_list; p; p = p->next) {
708 hit = !fixmatch(p->pattern, bol, p->ignore_case, &m);
713 hit = !regexec(&p->regexp, bol, 1, &m, REG_STARTEND);
715 hit = !regexec(&p->regexp, bol, 1, &m, 0);
718 if (!hit || m.rm_so < 0 || m.rm_eo < 0)
720 if (earliest < 0 || m.rm_so < earliest)
725 *bol_p = bol + *left_p;
729 for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
730 ; /* find the beginning of the line */
733 for (sp = bol; sp < last_bol; sp++) {
737 *left_p -= last_bol - bol;
743 int grep_threads_ok(const struct grep_opt *opt)
745 /* If this condition is true, then we may use the attribute
746 * machinery in grep_buffer_1. The attribute code is not
747 * thread safe, so we disable the use of threads.
749 if (opt->funcname && !opt->unmatch_name_only && !opt->status_only &&
753 /* If we are showing hunk marks, we should not do it for the
754 * first match. The synchronization problem we get for this
755 * constraint is not yet solved, so we disable threading in
758 if (opt->pre_context || opt->post_context)
764 static void std_output(struct grep_opt *opt, const void *buf, size_t size)
766 fwrite(buf, size, 1, stdout);
769 static int grep_buffer_1(struct grep_opt *opt, const char *name,
770 char *buf, unsigned long size, int collect_hits)
773 unsigned long left = size;
775 unsigned last_hit = 0;
776 int binary_match_only = 0;
778 int try_lookahead = 0;
779 enum grep_context ctx = GREP_CONTEXT_HEAD;
785 opt->output = std_output;
787 if (buffer_is_binary(buf, size)) {
788 switch (opt->binary) {
789 case GREP_BINARY_DEFAULT:
790 binary_match_only = 1;
792 case GREP_BINARY_NOMATCH:
793 return 0; /* Assume unmatch */
800 memset(&xecfg, 0, sizeof(xecfg));
801 if (opt->funcname && !opt->unmatch_name_only && !opt->status_only &&
802 !opt->name_only && !binary_match_only && !collect_hits) {
803 struct userdiff_driver *drv = userdiff_find_by_path(name);
804 if (drv && drv->funcname.pattern) {
805 const struct userdiff_funcname *pe = &drv->funcname;
806 xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
810 try_lookahead = should_lookahead(opt);
817 * look_ahead() skips quicly to the line that possibly
818 * has the next hit; don't call it if we need to do
819 * something more than just skipping the current line
820 * in response to an unmatch for the current line. E.g.
821 * inside a post-context window, we will show the current
822 * line as a context around the previous hit when it
827 && lno <= last_hit + opt->post_context)
828 && look_ahead(opt, &left, &lno, &bol))
830 eol = end_of_line(bol, &left);
834 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
835 ctx = GREP_CONTEXT_BODY;
837 hit = match_line(opt, bol, eol, ctx, collect_hits);
843 /* "grep -v -e foo -e bla" should list lines
844 * that do not have either, so inversion should
849 if (opt->unmatch_name_only) {
856 if (opt->status_only)
858 if (binary_match_only) {
859 opt->output(opt, "Binary file ", 12);
860 opt->output(opt, name, strlen(name));
861 opt->output(opt, " matches\n", 9);
864 if (opt->name_only) {
865 show_name(opt, name);
868 /* Hit at this line. If we haven't shown the
869 * pre-context lines, we would need to show them.
870 * When asked to do "count", this still show
871 * the context which is nonsense, but the user
872 * deserves to get that ;-).
874 if (opt->pre_context)
875 show_pre_context(opt, name, buf, bol, lno);
876 else if (opt->funcname)
877 show_funcname_line(opt, name, buf, bol, lno);
879 show_line(opt, bol, eol, name, lno, ':');
883 lno <= last_hit + opt->post_context) {
884 /* If the last hit is within the post context,
885 * we need to show this line.
887 show_line(opt, bol, eol, name, lno, '-');
901 if (opt->status_only)
903 if (opt->unmatch_name_only) {
904 /* We did not see any hit, so we want to show this */
905 show_name(opt, name);
909 xdiff_clear_find_func(&xecfg);
913 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
914 * which feels mostly useless but sometimes useful. Maybe
915 * make it another option? For now suppress them.
917 if (opt->count && count) {
919 opt->output(opt, name, strlen(name));
920 snprintf(buf, sizeof(buf), "%c%u\n",
921 opt->null_following_name ? '\0' : ':', count);
922 opt->output(opt, buf, strlen(buf));
927 static void clr_hit_marker(struct grep_expr *x)
929 /* All-hit markers are meaningful only at the very top level
934 if (x->node != GREP_NODE_OR)
936 x->u.binary.left->hit = 0;
937 x = x->u.binary.right;
941 static int chk_hit_marker(struct grep_expr *x)
943 /* Top level nodes have hit markers. See if they all are hits */
945 if (x->node != GREP_NODE_OR)
947 if (!x->u.binary.left->hit)
949 x = x->u.binary.right;
953 int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size)
956 * we do not have to do the two-pass grep when we do not check
957 * buffer-wide "all-match".
960 return grep_buffer_1(opt, name, buf, size, 0);
962 /* Otherwise the toplevel "or" terms hit a bit differently.
963 * We first clear hit markers from them.
965 clr_hit_marker(opt->pattern_expression);
966 grep_buffer_1(opt, name, buf, size, 1);
968 if (!chk_hit_marker(opt->pattern_expression))
971 return grep_buffer_1(opt, name, buf, size, 0);