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 output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
310 if (opt
->color
&& color
&& color
[0]) {
311 opt
->output(opt
, color
, strlen(color
));
312 opt
->output(opt
, data
, size
);
313 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
315 opt
->output(opt
, data
, size
);
318 static void output_sep(struct grep_opt
*opt
, char sign
)
320 if (opt
->null_following_name
)
321 opt
->output(opt
, "\0", 1);
323 output_color(opt
, &sign
, 1, opt
->color_sep
);
326 static void show_name(struct grep_opt
*opt
, const char *name
)
328 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
329 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
332 static int fixmatch(const char *pattern
, char *line
, char *eol
,
333 int ignore_case
, regmatch_t
*match
)
340 hit
= strcasestr(s
, pattern
);
346 hit
= memmem(line
, eol
- line
, pattern
, strlen(pattern
));
349 match
->rm_so
= match
->rm_eo
= -1;
353 match
->rm_so
= hit
- line
;
354 match
->rm_eo
= match
->rm_so
+ strlen(pattern
);
359 static int regmatch(const regex_t
*preg
, char *line
, char *eol
,
360 regmatch_t
*match
, int eflags
)
364 match
->rm_eo
= eol
- line
;
365 eflags
|= REG_STARTEND
;
367 return regexec(preg
, line
, 1, match
, eflags
);
370 static int strip_timestamp(char *bol
, char **eol_p
)
375 while (bol
< --eol
) {
391 { "committer ", 10 },
394 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
395 enum grep_context ctx
,
396 regmatch_t
*pmatch
, int eflags
)
400 const char *start
= bol
;
402 if ((p
->token
!= GREP_PATTERN
) &&
403 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
406 if (p
->token
== GREP_PATTERN_HEAD
) {
409 assert(p
->field
< ARRAY_SIZE(header_field
));
410 field
= header_field
[p
->field
].field
;
411 len
= header_field
[p
->field
].len
;
412 if (strncmp(bol
, field
, len
))
415 saved_ch
= strip_timestamp(bol
, &eol
);
420 hit
= !fixmatch(p
->pattern
, bol
, eol
, p
->ignore_case
, pmatch
);
422 hit
= !regmatch(&p
->regexp
, bol
, eol
, pmatch
, eflags
);
424 if (hit
&& p
->word_regexp
) {
425 if ((pmatch
[0].rm_so
< 0) ||
426 (eol
- bol
) < pmatch
[0].rm_so
||
427 (pmatch
[0].rm_eo
< 0) ||
428 (eol
- bol
) < pmatch
[0].rm_eo
)
429 die("regexp returned nonsense");
431 /* Match beginning must be either beginning of the
432 * line, or at word boundary (i.e. the last char must
433 * not be a word char). Similarly, match end must be
434 * either end of the line, or at word boundary
435 * (i.e. the next char must not be a word char).
437 if ( ((pmatch
[0].rm_so
== 0) ||
438 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
439 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
440 !word_char(bol
[pmatch
[0].rm_eo
])) )
445 /* Words consist of at least one character. */
446 if (pmatch
->rm_so
== pmatch
->rm_eo
)
449 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
450 /* There could be more than one match on the
451 * line, and the first match might not be
452 * strict word match. But later ones could be!
453 * Forward to the next possible start, i.e. the
454 * next position following a non-word char.
456 bol
= pmatch
[0].rm_so
+ bol
+ 1;
457 while (word_char(bol
[-1]) && bol
< eol
)
459 eflags
|= REG_NOTBOL
;
464 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
467 pmatch
[0].rm_so
+= bol
- start
;
468 pmatch
[0].rm_eo
+= bol
- start
;
473 static int match_expr_eval(struct grep_expr
*x
, char *bol
, char *eol
,
474 enum grep_context ctx
, int collect_hits
)
480 die("Not a valid grep expression");
483 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
, &match
, 0);
486 h
= !match_expr_eval(x
->u
.unary
, bol
, eol
, ctx
, 0);
489 if (!match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0))
491 h
= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
495 return (match_expr_eval(x
->u
.binary
.left
,
497 match_expr_eval(x
->u
.binary
.right
,
499 h
= match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
500 x
->u
.binary
.left
->hit
|= h
;
501 h
|= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
504 die("Unexpected node type (internal error) %d", x
->node
);
511 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
512 enum grep_context ctx
, int collect_hits
)
514 struct grep_expr
*x
= opt
->pattern_expression
;
515 return match_expr_eval(x
, bol
, eol
, ctx
, collect_hits
);
518 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
519 enum grep_context ctx
, int collect_hits
)
525 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
527 /* we do not call with collect_hits without being extended */
528 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
529 if (match_one_pattern(p
, bol
, eol
, ctx
, &match
, 0))
535 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
536 enum grep_context ctx
,
537 regmatch_t
*pmatch
, int eflags
)
541 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
543 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
545 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
546 if (match
.rm_so
> pmatch
->rm_so
)
548 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
551 pmatch
->rm_so
= match
.rm_so
;
552 pmatch
->rm_eo
= match
.rm_eo
;
556 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
557 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
562 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
564 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
566 case GREP_PATTERN
: /* atom */
567 case GREP_PATTERN_HEAD
:
568 case GREP_PATTERN_BODY
:
569 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
580 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
581 const char *name
, unsigned lno
, char sign
)
583 int rest
= eol
- bol
;
584 char *line_color
= NULL
;
586 if (opt
->pre_context
|| opt
->post_context
) {
587 if (opt
->last_shown
== 0) {
588 if (opt
->show_hunk_mark
) {
589 output_color(opt
, "--", 2, opt
->color_sep
);
590 opt
->output(opt
, "\n", 1);
592 } else if (lno
> opt
->last_shown
+ 1) {
593 output_color(opt
, "--", 2, opt
->color_sep
);
594 opt
->output(opt
, "\n", 1);
597 opt
->last_shown
= lno
;
600 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
601 output_sep(opt
, sign
);
605 snprintf(buf
, sizeof(buf
), "%d", lno
);
606 output_color(opt
, buf
, strlen(buf
), opt
->color_lineno
);
607 output_sep(opt
, sign
);
611 enum grep_context ctx
= GREP_CONTEXT_BODY
;
616 line_color
= opt
->color_selected
;
617 else if (sign
== '-')
618 line_color
= opt
->color_context
;
619 else if (sign
== '=')
620 line_color
= opt
->color_function
;
622 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
623 if (match
.rm_so
== match
.rm_eo
)
626 output_color(opt
, bol
, match
.rm_so
, line_color
);
627 output_color(opt
, bol
+ match
.rm_so
,
628 match
.rm_eo
- match
.rm_so
,
636 output_color(opt
, bol
, rest
, line_color
);
637 opt
->output(opt
, "\n", 1);
640 static int match_funcname(struct grep_opt
*opt
, char *bol
, char *eol
)
642 xdemitconf_t
*xecfg
= opt
->priv
;
643 if (xecfg
&& xecfg
->find_func
) {
645 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
646 xecfg
->find_func_priv
) >= 0;
651 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
656 static void show_funcname_line(struct grep_opt
*opt
, const char *name
,
657 char *buf
, char *bol
, unsigned lno
)
662 while (bol
> buf
&& bol
[-1] != '\n')
666 if (lno
<= opt
->last_shown
)
669 if (match_funcname(opt
, bol
, eol
)) {
670 show_line(opt
, bol
, eol
, name
, lno
, '=');
676 static void show_pre_context(struct grep_opt
*opt
, const char *name
, char *buf
,
677 char *bol
, unsigned lno
)
679 unsigned cur
= lno
, from
= 1, funcname_lno
= 0;
680 int funcname_needed
= opt
->funcname
;
682 if (opt
->pre_context
< lno
)
683 from
= lno
- opt
->pre_context
;
684 if (from
<= opt
->last_shown
)
685 from
= opt
->last_shown
+ 1;
688 while (bol
> buf
&& cur
> from
) {
691 while (bol
> buf
&& bol
[-1] != '\n')
694 if (funcname_needed
&& match_funcname(opt
, bol
, eol
)) {
700 /* We need to look even further back to find a function signature. */
701 if (opt
->funcname
&& funcname_needed
)
702 show_funcname_line(opt
, name
, buf
, bol
, cur
);
706 char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
710 show_line(opt
, bol
, eol
, name
, cur
, sign
);
716 static int should_lookahead(struct grep_opt
*opt
)
721 return 0; /* punt for too complex stuff */
724 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
725 if (p
->token
!= GREP_PATTERN
)
726 return 0; /* punt for "header only" and stuff */
731 static int look_ahead(struct grep_opt
*opt
,
732 unsigned long *left_p
,
736 unsigned lno
= *lno_p
;
740 regoff_t earliest
= -1;
742 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
747 hit
= !fixmatch(p
->pattern
, bol
, bol
+ *left_p
,
750 hit
= !regmatch(&p
->regexp
, bol
, bol
+ *left_p
, &m
, 0);
751 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
753 if (earliest
< 0 || m
.rm_so
< earliest
)
758 *bol_p
= bol
+ *left_p
;
762 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
763 ; /* find the beginning of the line */
766 for (sp
= bol
; sp
< last_bol
; sp
++) {
770 *left_p
-= last_bol
- bol
;
776 int grep_threads_ok(const struct grep_opt
*opt
)
778 /* If this condition is true, then we may use the attribute
779 * machinery in grep_buffer_1. The attribute code is not
780 * thread safe, so we disable the use of threads.
782 if (opt
->funcname
&& !opt
->unmatch_name_only
&& !opt
->status_only
&&
789 static void std_output(struct grep_opt
*opt
, const void *buf
, size_t size
)
791 fwrite(buf
, size
, 1, stdout
);
794 static int grep_buffer_1(struct grep_opt
*opt
, const char *name
,
795 char *buf
, unsigned long size
, int collect_hits
)
798 unsigned long left
= size
;
800 unsigned last_hit
= 0;
801 int binary_match_only
= 0;
803 int try_lookahead
= 0;
804 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
808 opt
->output
= std_output
;
810 if (opt
->last_shown
&& (opt
->pre_context
|| opt
->post_context
) &&
811 opt
->output
== std_output
)
812 opt
->show_hunk_mark
= 1;
815 switch (opt
->binary
) {
816 case GREP_BINARY_DEFAULT
:
817 if (buffer_is_binary(buf
, size
))
818 binary_match_only
= 1;
820 case GREP_BINARY_NOMATCH
:
821 if (buffer_is_binary(buf
, size
))
822 return 0; /* Assume unmatch */
824 case GREP_BINARY_TEXT
:
827 die("bug: unknown binary handling mode");
830 memset(&xecfg
, 0, sizeof(xecfg
));
831 if (opt
->funcname
&& !opt
->unmatch_name_only
&& !opt
->status_only
&&
832 !opt
->name_only
&& !binary_match_only
&& !collect_hits
) {
833 struct userdiff_driver
*drv
= userdiff_find_by_path(name
);
834 if (drv
&& drv
->funcname
.pattern
) {
835 const struct userdiff_funcname
*pe
= &drv
->funcname
;
836 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
840 try_lookahead
= should_lookahead(opt
);
847 * look_ahead() skips quicly to the line that possibly
848 * has the next hit; don't call it if we need to do
849 * something more than just skipping the current line
850 * in response to an unmatch for the current line. E.g.
851 * inside a post-context window, we will show the current
852 * line as a context around the previous hit when it
857 && lno
<= last_hit
+ opt
->post_context
)
858 && look_ahead(opt
, &left
, &lno
, &bol
))
860 eol
= end_of_line(bol
, &left
);
864 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
865 ctx
= GREP_CONTEXT_BODY
;
867 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
873 /* "grep -v -e foo -e bla" should list lines
874 * that do not have either, so inversion should
879 if (opt
->unmatch_name_only
) {
886 if (opt
->status_only
)
888 if (opt
->name_only
) {
889 show_name(opt
, name
);
894 if (binary_match_only
) {
895 opt
->output(opt
, "Binary file ", 12);
896 output_color(opt
, name
, strlen(name
),
897 opt
->color_filename
);
898 opt
->output(opt
, " matches\n", 9);
901 /* Hit at this line. If we haven't shown the
902 * pre-context lines, we would need to show them.
904 if (opt
->pre_context
)
905 show_pre_context(opt
, name
, buf
, bol
, lno
);
906 else if (opt
->funcname
)
907 show_funcname_line(opt
, name
, buf
, bol
, lno
);
908 show_line(opt
, bol
, eol
, name
, lno
, ':');
912 lno
<= last_hit
+ opt
->post_context
) {
913 /* If the last hit is within the post context,
914 * we need to show this line.
916 show_line(opt
, bol
, eol
, name
, lno
, '-');
930 if (opt
->status_only
)
932 if (opt
->unmatch_name_only
) {
933 /* We did not see any hit, so we want to show this */
934 show_name(opt
, name
);
938 xdiff_clear_find_func(&xecfg
);
942 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
943 * which feels mostly useless but sometimes useful. Maybe
944 * make it another option? For now suppress them.
946 if (opt
->count
&& count
) {
948 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
949 output_sep(opt
, ':');
950 snprintf(buf
, sizeof(buf
), "%u\n", count
);
951 opt
->output(opt
, buf
, strlen(buf
));
957 static void clr_hit_marker(struct grep_expr
*x
)
959 /* All-hit markers are meaningful only at the very top level
964 if (x
->node
!= GREP_NODE_OR
)
966 x
->u
.binary
.left
->hit
= 0;
967 x
= x
->u
.binary
.right
;
971 static int chk_hit_marker(struct grep_expr
*x
)
973 /* Top level nodes have hit markers. See if they all are hits */
975 if (x
->node
!= GREP_NODE_OR
)
977 if (!x
->u
.binary
.left
->hit
)
979 x
= x
->u
.binary
.right
;
983 int grep_buffer(struct grep_opt
*opt
, const char *name
, char *buf
, unsigned long size
)
986 * we do not have to do the two-pass grep when we do not check
987 * buffer-wide "all-match".
990 return grep_buffer_1(opt
, name
, buf
, size
, 0);
992 /* Otherwise the toplevel "or" terms hit a bit differently.
993 * We first clear hit markers from them.
995 clr_hit_marker(opt
->pattern_expression
);
996 grep_buffer_1(opt
, name
, buf
, size
, 1);
998 if (!chk_hit_marker(opt
->pattern_expression
))
1001 return grep_buffer_1(opt
, name
, buf
, size
, 0);