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 void compile_grep_patterns(struct grep_opt
*opt
)
195 struct grep_expr
*header_expr
= NULL
;
197 if (opt
->header_list
) {
198 p
= opt
->header_list
;
199 header_expr
= compile_pattern_expr(&p
);
201 die("incomplete pattern expression: %s", p
->pattern
);
202 for (p
= opt
->header_list
; p
; p
= p
->next
) {
204 case GREP_PATTERN
: /* atom */
205 case GREP_PATTERN_HEAD
:
206 case GREP_PATTERN_BODY
:
207 compile_regexp(p
, opt
);
216 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
218 case GREP_PATTERN
: /* atom */
219 case GREP_PATTERN_HEAD
:
220 case GREP_PATTERN_BODY
:
221 compile_regexp(p
, opt
);
229 if (opt
->all_match
|| header_expr
)
231 else if (!opt
->extended
)
234 /* Then bundle them up in an expression.
235 * A classic recursive descent parser would do.
237 p
= opt
->pattern_list
;
239 opt
->pattern_expression
= compile_pattern_expr(&p
);
241 die("incomplete pattern expression: %s", p
->pattern
);
246 if (opt
->pattern_expression
) {
248 z
= xcalloc(1, sizeof(*z
));
249 z
->node
= GREP_NODE_OR
;
250 z
->u
.binary
.left
= opt
->pattern_expression
;
251 z
->u
.binary
.right
= header_expr
;
252 opt
->pattern_expression
= z
;
254 opt
->pattern_expression
= header_expr
;
259 static void free_pattern_expr(struct grep_expr
*x
)
265 free_pattern_expr(x
->u
.unary
);
269 free_pattern_expr(x
->u
.binary
.left
);
270 free_pattern_expr(x
->u
.binary
.right
);
276 void free_grep_patterns(struct grep_opt
*opt
)
278 struct grep_pat
*p
, *n
;
280 for (p
= opt
->pattern_list
; p
; p
= n
) {
283 case GREP_PATTERN
: /* atom */
284 case GREP_PATTERN_HEAD
:
285 case GREP_PATTERN_BODY
:
296 free_pattern_expr(opt
->pattern_expression
);
299 static char *end_of_line(char *cp
, unsigned long *left
)
301 unsigned long l
= *left
;
302 while (l
&& *cp
!= '\n') {
310 static int word_char(char ch
)
312 return isalnum(ch
) || ch
== '_';
315 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
318 if (opt
->color
&& color
&& color
[0]) {
319 opt
->output(opt
, color
, strlen(color
));
320 opt
->output(opt
, data
, size
);
321 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
323 opt
->output(opt
, data
, size
);
326 static void output_sep(struct grep_opt
*opt
, char sign
)
328 if (opt
->null_following_name
)
329 opt
->output(opt
, "\0", 1);
331 output_color(opt
, &sign
, 1, opt
->color_sep
);
334 static void show_name(struct grep_opt
*opt
, const char *name
)
336 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
337 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
340 static int fixmatch(struct grep_pat
*p
, char *line
, char *eol
,
345 if (p
->ignore_case
) {
348 hit
= strcasestr(s
, p
->pattern
);
354 hit
= memmem(line
, eol
- line
, p
->pattern
, p
->patternlen
);
357 match
->rm_so
= match
->rm_eo
= -1;
361 match
->rm_so
= hit
- line
;
362 match
->rm_eo
= match
->rm_so
+ p
->patternlen
;
367 static int regmatch(const regex_t
*preg
, char *line
, char *eol
,
368 regmatch_t
*match
, int eflags
)
372 match
->rm_eo
= eol
- line
;
373 eflags
|= REG_STARTEND
;
375 return regexec(preg
, line
, 1, match
, eflags
);
378 static int strip_timestamp(char *bol
, char **eol_p
)
383 while (bol
< --eol
) {
399 { "committer ", 10 },
402 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
403 enum grep_context ctx
,
404 regmatch_t
*pmatch
, int eflags
)
408 const char *start
= bol
;
410 if ((p
->token
!= GREP_PATTERN
) &&
411 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
414 if (p
->token
== GREP_PATTERN_HEAD
) {
417 assert(p
->field
< ARRAY_SIZE(header_field
));
418 field
= header_field
[p
->field
].field
;
419 len
= header_field
[p
->field
].len
;
420 if (strncmp(bol
, field
, len
))
423 saved_ch
= strip_timestamp(bol
, &eol
);
428 hit
= !fixmatch(p
, bol
, eol
, pmatch
);
430 hit
= !regmatch(&p
->regexp
, bol
, eol
, pmatch
, eflags
);
432 if (hit
&& p
->word_regexp
) {
433 if ((pmatch
[0].rm_so
< 0) ||
434 (eol
- bol
) < pmatch
[0].rm_so
||
435 (pmatch
[0].rm_eo
< 0) ||
436 (eol
- bol
) < pmatch
[0].rm_eo
)
437 die("regexp returned nonsense");
439 /* Match beginning must be either beginning of the
440 * line, or at word boundary (i.e. the last char must
441 * not be a word char). Similarly, match end must be
442 * either end of the line, or at word boundary
443 * (i.e. the next char must not be a word char).
445 if ( ((pmatch
[0].rm_so
== 0) ||
446 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
447 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
448 !word_char(bol
[pmatch
[0].rm_eo
])) )
453 /* Words consist of at least one character. */
454 if (pmatch
->rm_so
== pmatch
->rm_eo
)
457 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
458 /* There could be more than one match on the
459 * line, and the first match might not be
460 * strict word match. But later ones could be!
461 * Forward to the next possible start, i.e. the
462 * next position following a non-word char.
464 bol
= pmatch
[0].rm_so
+ bol
+ 1;
465 while (word_char(bol
[-1]) && bol
< eol
)
467 eflags
|= REG_NOTBOL
;
472 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
475 pmatch
[0].rm_so
+= bol
- start
;
476 pmatch
[0].rm_eo
+= bol
- start
;
481 static int match_expr_eval(struct grep_expr
*x
, char *bol
, char *eol
,
482 enum grep_context ctx
, int collect_hits
)
488 die("Not a valid grep expression");
491 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
, &match
, 0);
494 h
= !match_expr_eval(x
->u
.unary
, bol
, eol
, ctx
, 0);
497 if (!match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0))
499 h
= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
503 return (match_expr_eval(x
->u
.binary
.left
,
505 match_expr_eval(x
->u
.binary
.right
,
507 h
= match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
508 x
->u
.binary
.left
->hit
|= h
;
509 h
|= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
512 die("Unexpected node type (internal error) %d", x
->node
);
519 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
520 enum grep_context ctx
, int collect_hits
)
522 struct grep_expr
*x
= opt
->pattern_expression
;
523 return match_expr_eval(x
, bol
, eol
, ctx
, collect_hits
);
526 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
527 enum grep_context ctx
, int collect_hits
)
533 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
535 /* we do not call with collect_hits without being extended */
536 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
537 if (match_one_pattern(p
, bol
, eol
, ctx
, &match
, 0))
543 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
544 enum grep_context ctx
,
545 regmatch_t
*pmatch
, int eflags
)
549 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
551 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
553 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
554 if (match
.rm_so
> pmatch
->rm_so
)
556 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
559 pmatch
->rm_so
= match
.rm_so
;
560 pmatch
->rm_eo
= match
.rm_eo
;
564 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
565 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
570 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
572 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
574 case GREP_PATTERN
: /* atom */
575 case GREP_PATTERN_HEAD
:
576 case GREP_PATTERN_BODY
:
577 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
588 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
589 const char *name
, unsigned lno
, char sign
)
591 int rest
= eol
- bol
;
592 char *line_color
= NULL
;
594 if (opt
->pre_context
|| opt
->post_context
) {
595 if (opt
->last_shown
== 0) {
596 if (opt
->show_hunk_mark
) {
597 output_color(opt
, "--", 2, opt
->color_sep
);
598 opt
->output(opt
, "\n", 1);
600 } else if (lno
> opt
->last_shown
+ 1) {
601 output_color(opt
, "--", 2, opt
->color_sep
);
602 opt
->output(opt
, "\n", 1);
605 opt
->last_shown
= lno
;
608 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
609 output_sep(opt
, sign
);
613 snprintf(buf
, sizeof(buf
), "%d", lno
);
614 output_color(opt
, buf
, strlen(buf
), opt
->color_lineno
);
615 output_sep(opt
, sign
);
619 enum grep_context ctx
= GREP_CONTEXT_BODY
;
624 line_color
= opt
->color_selected
;
625 else if (sign
== '-')
626 line_color
= opt
->color_context
;
627 else if (sign
== '=')
628 line_color
= opt
->color_function
;
630 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
631 if (match
.rm_so
== match
.rm_eo
)
634 output_color(opt
, bol
, match
.rm_so
, line_color
);
635 output_color(opt
, bol
+ match
.rm_so
,
636 match
.rm_eo
- match
.rm_so
,
644 output_color(opt
, bol
, rest
, line_color
);
645 opt
->output(opt
, "\n", 1);
648 static int match_funcname(struct grep_opt
*opt
, char *bol
, char *eol
)
650 xdemitconf_t
*xecfg
= opt
->priv
;
651 if (xecfg
&& xecfg
->find_func
) {
653 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
654 xecfg
->find_func_priv
) >= 0;
659 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
664 static void show_funcname_line(struct grep_opt
*opt
, const char *name
,
665 char *buf
, char *bol
, unsigned lno
)
670 while (bol
> buf
&& bol
[-1] != '\n')
674 if (lno
<= opt
->last_shown
)
677 if (match_funcname(opt
, bol
, eol
)) {
678 show_line(opt
, bol
, eol
, name
, lno
, '=');
684 static void show_pre_context(struct grep_opt
*opt
, const char *name
, char *buf
,
685 char *bol
, unsigned lno
)
687 unsigned cur
= lno
, from
= 1, funcname_lno
= 0;
688 int funcname_needed
= opt
->funcname
;
690 if (opt
->pre_context
< lno
)
691 from
= lno
- opt
->pre_context
;
692 if (from
<= opt
->last_shown
)
693 from
= opt
->last_shown
+ 1;
696 while (bol
> buf
&& cur
> from
) {
699 while (bol
> buf
&& bol
[-1] != '\n')
702 if (funcname_needed
&& match_funcname(opt
, bol
, eol
)) {
708 /* We need to look even further back to find a function signature. */
709 if (opt
->funcname
&& funcname_needed
)
710 show_funcname_line(opt
, name
, buf
, bol
, cur
);
714 char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
718 show_line(opt
, bol
, eol
, name
, cur
, sign
);
724 static int should_lookahead(struct grep_opt
*opt
)
729 return 0; /* punt for too complex stuff */
732 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
733 if (p
->token
!= GREP_PATTERN
)
734 return 0; /* punt for "header only" and stuff */
739 static int look_ahead(struct grep_opt
*opt
,
740 unsigned long *left_p
,
744 unsigned lno
= *lno_p
;
748 regoff_t earliest
= -1;
750 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
755 hit
= !fixmatch(p
, bol
, bol
+ *left_p
, &m
);
757 hit
= !regmatch(&p
->regexp
, bol
, bol
+ *left_p
, &m
, 0);
758 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
760 if (earliest
< 0 || m
.rm_so
< earliest
)
765 *bol_p
= bol
+ *left_p
;
769 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
770 ; /* find the beginning of the line */
773 for (sp
= bol
; sp
< last_bol
; sp
++) {
777 *left_p
-= last_bol
- bol
;
783 int grep_threads_ok(const struct grep_opt
*opt
)
785 /* If this condition is true, then we may use the attribute
786 * machinery in grep_buffer_1. The attribute code is not
787 * thread safe, so we disable the use of threads.
789 if (opt
->funcname
&& !opt
->unmatch_name_only
&& !opt
->status_only
&&
796 static void std_output(struct grep_opt
*opt
, const void *buf
, size_t size
)
798 fwrite(buf
, size
, 1, stdout
);
801 static int grep_buffer_1(struct grep_opt
*opt
, const char *name
,
802 char *buf
, unsigned long size
, int collect_hits
)
805 unsigned long left
= size
;
807 unsigned last_hit
= 0;
808 int binary_match_only
= 0;
810 int try_lookahead
= 0;
811 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
815 opt
->output
= std_output
;
817 if (opt
->last_shown
&& (opt
->pre_context
|| opt
->post_context
) &&
818 opt
->output
== std_output
)
819 opt
->show_hunk_mark
= 1;
822 switch (opt
->binary
) {
823 case GREP_BINARY_DEFAULT
:
824 if (buffer_is_binary(buf
, size
))
825 binary_match_only
= 1;
827 case GREP_BINARY_NOMATCH
:
828 if (buffer_is_binary(buf
, size
))
829 return 0; /* Assume unmatch */
831 case GREP_BINARY_TEXT
:
834 die("bug: unknown binary handling mode");
837 memset(&xecfg
, 0, sizeof(xecfg
));
838 if (opt
->funcname
&& !opt
->unmatch_name_only
&& !opt
->status_only
&&
839 !opt
->name_only
&& !binary_match_only
&& !collect_hits
) {
840 struct userdiff_driver
*drv
= userdiff_find_by_path(name
);
841 if (drv
&& drv
->funcname
.pattern
) {
842 const struct userdiff_funcname
*pe
= &drv
->funcname
;
843 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
847 try_lookahead
= should_lookahead(opt
);
854 * look_ahead() skips quicly to the line that possibly
855 * has the next hit; don't call it if we need to do
856 * something more than just skipping the current line
857 * in response to an unmatch for the current line. E.g.
858 * inside a post-context window, we will show the current
859 * line as a context around the previous hit when it
864 && lno
<= last_hit
+ opt
->post_context
)
865 && look_ahead(opt
, &left
, &lno
, &bol
))
867 eol
= end_of_line(bol
, &left
);
871 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
872 ctx
= GREP_CONTEXT_BODY
;
874 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
880 /* "grep -v -e foo -e bla" should list lines
881 * that do not have either, so inversion should
886 if (opt
->unmatch_name_only
) {
893 if (opt
->status_only
)
895 if (opt
->name_only
) {
896 show_name(opt
, name
);
901 if (binary_match_only
) {
902 opt
->output(opt
, "Binary file ", 12);
903 output_color(opt
, name
, strlen(name
),
904 opt
->color_filename
);
905 opt
->output(opt
, " matches\n", 9);
908 /* Hit at this line. If we haven't shown the
909 * pre-context lines, we would need to show them.
911 if (opt
->pre_context
)
912 show_pre_context(opt
, name
, buf
, bol
, lno
);
913 else if (opt
->funcname
)
914 show_funcname_line(opt
, name
, buf
, bol
, lno
);
915 show_line(opt
, bol
, eol
, name
, lno
, ':');
919 lno
<= last_hit
+ opt
->post_context
) {
920 /* If the last hit is within the post context,
921 * we need to show this line.
923 show_line(opt
, bol
, eol
, name
, lno
, '-');
937 if (opt
->status_only
)
939 if (opt
->unmatch_name_only
) {
940 /* We did not see any hit, so we want to show this */
941 show_name(opt
, name
);
945 xdiff_clear_find_func(&xecfg
);
949 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
950 * which feels mostly useless but sometimes useful. Maybe
951 * make it another option? For now suppress them.
953 if (opt
->count
&& count
) {
955 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
956 output_sep(opt
, ':');
957 snprintf(buf
, sizeof(buf
), "%u\n", count
);
958 opt
->output(opt
, buf
, strlen(buf
));
964 static void clr_hit_marker(struct grep_expr
*x
)
966 /* All-hit markers are meaningful only at the very top level
971 if (x
->node
!= GREP_NODE_OR
)
973 x
->u
.binary
.left
->hit
= 0;
974 x
= x
->u
.binary
.right
;
978 static int chk_hit_marker(struct grep_expr
*x
)
980 /* Top level nodes have hit markers. See if they all are hits */
982 if (x
->node
!= GREP_NODE_OR
)
984 if (!x
->u
.binary
.left
->hit
)
986 x
= x
->u
.binary
.right
;
990 int grep_buffer(struct grep_opt
*opt
, const char *name
, char *buf
, unsigned long size
)
993 * we do not have to do the two-pass grep when we do not check
994 * buffer-wide "all-match".
997 return grep_buffer_1(opt
, name
, buf
, size
, 0);
999 /* Otherwise the toplevel "or" terms hit a bit differently.
1000 * We first clear hit markers from them.
1002 clr_hit_marker(opt
->pattern_expression
);
1003 grep_buffer_1(opt
, name
, buf
, size
, 1);
1005 if (!chk_hit_marker(opt
->pattern_expression
))
1008 return grep_buffer_1(opt
, name
, buf
, size
, 0);