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
;
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
)
191 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
193 case GREP_PATTERN
: /* atom */
194 case GREP_PATTERN_HEAD
:
195 case GREP_PATTERN_BODY
:
196 compile_regexp(p
, opt
);
207 /* Then bundle them up in an expression.
208 * A classic recursive descent parser would do.
210 p
= opt
->pattern_list
;
212 opt
->pattern_expression
= compile_pattern_expr(&p
);
214 die("incomplete pattern expression: %s", p
->pattern
);
217 static void free_pattern_expr(struct grep_expr
*x
)
223 free_pattern_expr(x
->u
.unary
);
227 free_pattern_expr(x
->u
.binary
.left
);
228 free_pattern_expr(x
->u
.binary
.right
);
234 void free_grep_patterns(struct grep_opt
*opt
)
236 struct grep_pat
*p
, *n
;
238 for (p
= opt
->pattern_list
; p
; p
= n
) {
241 case GREP_PATTERN
: /* atom */
242 case GREP_PATTERN_HEAD
:
243 case GREP_PATTERN_BODY
:
254 free_pattern_expr(opt
->pattern_expression
);
257 static char *end_of_line(char *cp
, unsigned long *left
)
259 unsigned long l
= *left
;
260 while (l
&& *cp
!= '\n') {
268 static int word_char(char ch
)
270 return isalnum(ch
) || ch
== '_';
273 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
276 if (opt
->color
&& color
&& color
[0]) {
277 opt
->output(opt
, color
, strlen(color
));
278 opt
->output(opt
, data
, size
);
279 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
281 opt
->output(opt
, data
, size
);
284 static void output_sep(struct grep_opt
*opt
, char sign
)
286 if (opt
->null_following_name
)
287 opt
->output(opt
, "\0", 1);
289 output_color(opt
, &sign
, 1, opt
->color_sep
);
292 static void show_name(struct grep_opt
*opt
, const char *name
)
294 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
295 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
299 static int fixmatch(const char *pattern
, char *line
, int ignore_case
, regmatch_t
*match
)
303 hit
= strcasestr(line
, pattern
);
305 hit
= strstr(line
, pattern
);
308 match
->rm_so
= match
->rm_eo
= -1;
312 match
->rm_so
= hit
- line
;
313 match
->rm_eo
= match
->rm_so
+ strlen(pattern
);
318 static int strip_timestamp(char *bol
, char **eol_p
)
323 while (bol
< --eol
) {
339 { "committer ", 10 },
342 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
343 enum grep_context ctx
,
344 regmatch_t
*pmatch
, int eflags
)
348 const char *start
= bol
;
350 if ((p
->token
!= GREP_PATTERN
) &&
351 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
354 if (p
->token
== GREP_PATTERN_HEAD
) {
357 assert(p
->field
< ARRAY_SIZE(header_field
));
358 field
= header_field
[p
->field
].field
;
359 len
= header_field
[p
->field
].len
;
360 if (strncmp(bol
, field
, len
))
363 saved_ch
= strip_timestamp(bol
, &eol
);
368 hit
= !fixmatch(p
->pattern
, bol
, p
->ignore_case
, pmatch
);
370 hit
= !regexec(&p
->regexp
, bol
, 1, pmatch
, eflags
);
372 if (hit
&& p
->word_regexp
) {
373 if ((pmatch
[0].rm_so
< 0) ||
374 (eol
- bol
) < pmatch
[0].rm_so
||
375 (pmatch
[0].rm_eo
< 0) ||
376 (eol
- bol
) < pmatch
[0].rm_eo
)
377 die("regexp returned nonsense");
379 /* Match beginning must be either beginning of the
380 * line, or at word boundary (i.e. the last char must
381 * not be a word char). Similarly, match end must be
382 * either end of the line, or at word boundary
383 * (i.e. the next char must not be a word char).
385 if ( ((pmatch
[0].rm_so
== 0) ||
386 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
387 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
388 !word_char(bol
[pmatch
[0].rm_eo
])) )
393 /* Words consist of at least one character. */
394 if (pmatch
->rm_so
== pmatch
->rm_eo
)
397 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
398 /* There could be more than one match on the
399 * line, and the first match might not be
400 * strict word match. But later ones could be!
401 * Forward to the next possible start, i.e. the
402 * next position following a non-word char.
404 bol
= pmatch
[0].rm_so
+ bol
+ 1;
405 while (word_char(bol
[-1]) && bol
< eol
)
407 eflags
|= REG_NOTBOL
;
412 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
415 pmatch
[0].rm_so
+= bol
- start
;
416 pmatch
[0].rm_eo
+= bol
- start
;
421 static int match_expr_eval(struct grep_expr
*x
, char *bol
, char *eol
,
422 enum grep_context ctx
, int collect_hits
)
428 die("Not a valid grep expression");
431 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
, &match
, 0);
434 h
= !match_expr_eval(x
->u
.unary
, bol
, eol
, ctx
, 0);
437 if (!match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0))
439 h
= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
443 return (match_expr_eval(x
->u
.binary
.left
,
445 match_expr_eval(x
->u
.binary
.right
,
447 h
= match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
448 x
->u
.binary
.left
->hit
|= h
;
449 h
|= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
452 die("Unexpected node type (internal error) %d", x
->node
);
459 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
460 enum grep_context ctx
, int collect_hits
)
462 struct grep_expr
*x
= opt
->pattern_expression
;
463 return match_expr_eval(x
, bol
, eol
, ctx
, collect_hits
);
466 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
467 enum grep_context ctx
, int collect_hits
)
473 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
475 /* we do not call with collect_hits without being extended */
476 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
477 if (match_one_pattern(p
, bol
, eol
, ctx
, &match
, 0))
483 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
484 enum grep_context ctx
,
485 regmatch_t
*pmatch
, int eflags
)
489 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
491 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
493 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
494 if (match
.rm_so
> pmatch
->rm_so
)
496 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
499 pmatch
->rm_so
= match
.rm_so
;
500 pmatch
->rm_eo
= match
.rm_eo
;
504 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
505 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
510 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
512 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
514 case GREP_PATTERN
: /* atom */
515 case GREP_PATTERN_HEAD
:
516 case GREP_PATTERN_BODY
:
517 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
528 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
529 const char *name
, unsigned lno
, char sign
)
531 int rest
= eol
- bol
;
533 if (opt
->pre_context
|| opt
->post_context
) {
534 if (opt
->last_shown
== 0) {
535 if (opt
->show_hunk_mark
) {
536 output_color(opt
, "--", 2, opt
->color_sep
);
537 opt
->output(opt
, "\n", 1);
539 opt
->show_hunk_mark
= 1;
540 } else if (lno
> opt
->last_shown
+ 1) {
541 output_color(opt
, "--", 2, opt
->color_sep
);
542 opt
->output(opt
, "\n", 1);
545 opt
->last_shown
= lno
;
548 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
549 output_sep(opt
, sign
);
553 snprintf(buf
, sizeof(buf
), "%d", lno
);
554 output_color(opt
, buf
, strlen(buf
), opt
->color_lineno
);
555 output_sep(opt
, sign
);
559 enum grep_context ctx
= GREP_CONTEXT_BODY
;
564 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
565 if (match
.rm_so
== match
.rm_eo
)
568 opt
->output(opt
, bol
, match
.rm_so
);
569 output_color(opt
, bol
+ match
.rm_so
,
570 match
.rm_eo
- match
.rm_so
,
578 opt
->output(opt
, bol
, rest
);
579 opt
->output(opt
, "\n", 1);
582 static int match_funcname(struct grep_opt
*opt
, char *bol
, char *eol
)
584 xdemitconf_t
*xecfg
= opt
->priv
;
585 if (xecfg
&& xecfg
->find_func
) {
587 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
588 xecfg
->find_func_priv
) >= 0;
593 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
598 static void show_funcname_line(struct grep_opt
*opt
, const char *name
,
599 char *buf
, char *bol
, unsigned lno
)
604 while (bol
> buf
&& bol
[-1] != '\n')
608 if (lno
<= opt
->last_shown
)
611 if (match_funcname(opt
, bol
, eol
)) {
612 show_line(opt
, bol
, eol
, name
, lno
, '=');
618 static void show_pre_context(struct grep_opt
*opt
, const char *name
, char *buf
,
619 char *bol
, unsigned lno
)
621 unsigned cur
= lno
, from
= 1, funcname_lno
= 0;
622 int funcname_needed
= opt
->funcname
;
624 if (opt
->pre_context
< lno
)
625 from
= lno
- opt
->pre_context
;
626 if (from
<= opt
->last_shown
)
627 from
= opt
->last_shown
+ 1;
630 while (bol
> buf
&& cur
> from
) {
633 while (bol
> buf
&& bol
[-1] != '\n')
636 if (funcname_needed
&& match_funcname(opt
, bol
, eol
)) {
642 /* We need to look even further back to find a function signature. */
643 if (opt
->funcname
&& funcname_needed
)
644 show_funcname_line(opt
, name
, buf
, bol
, cur
);
648 char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
652 show_line(opt
, bol
, eol
, name
, cur
, sign
);
658 static int should_lookahead(struct grep_opt
*opt
)
663 return 0; /* punt for too complex stuff */
666 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
667 if (p
->token
!= GREP_PATTERN
)
668 return 0; /* punt for "header only" and stuff */
673 static int look_ahead(struct grep_opt
*opt
,
674 unsigned long *left_p
,
678 unsigned lno
= *lno_p
;
682 regoff_t earliest
= -1;
684 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
689 hit
= !fixmatch(p
->pattern
, bol
, p
->ignore_case
, &m
);
694 hit
= !regexec(&p
->regexp
, bol
, 1, &m
, REG_STARTEND
);
696 hit
= !regexec(&p
->regexp
, bol
, 1, &m
, 0);
699 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
701 if (earliest
< 0 || m
.rm_so
< earliest
)
706 *bol_p
= bol
+ *left_p
;
710 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
711 ; /* find the beginning of the line */
714 for (sp
= bol
; sp
< last_bol
; sp
++) {
718 *left_p
-= last_bol
- bol
;
724 int grep_threads_ok(const struct grep_opt
*opt
)
726 /* If this condition is true, then we may use the attribute
727 * machinery in grep_buffer_1. The attribute code is not
728 * thread safe, so we disable the use of threads.
730 if (opt
->funcname
&& !opt
->unmatch_name_only
&& !opt
->status_only
&&
734 /* If we are showing hunk marks, we should not do it for the
735 * first match. The synchronization problem we get for this
736 * constraint is not yet solved, so we disable threading in
739 if (opt
->pre_context
|| opt
->post_context
)
745 static void std_output(struct grep_opt
*opt
, const void *buf
, size_t size
)
747 fwrite(buf
, size
, 1, stdout
);
750 static int grep_buffer_1(struct grep_opt
*opt
, const char *name
,
751 char *buf
, unsigned long size
, int collect_hits
)
754 unsigned long left
= size
;
756 unsigned last_hit
= 0;
757 int binary_match_only
= 0;
759 int try_lookahead
= 0;
760 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
766 opt
->output
= std_output
;
768 if (buffer_is_binary(buf
, size
)) {
769 switch (opt
->binary
) {
770 case GREP_BINARY_DEFAULT
:
771 binary_match_only
= 1;
773 case GREP_BINARY_NOMATCH
:
774 return 0; /* Assume unmatch */
781 memset(&xecfg
, 0, sizeof(xecfg
));
782 if (opt
->funcname
&& !opt
->unmatch_name_only
&& !opt
->status_only
&&
783 !opt
->name_only
&& !binary_match_only
&& !collect_hits
) {
784 struct userdiff_driver
*drv
= userdiff_find_by_path(name
);
785 if (drv
&& drv
->funcname
.pattern
) {
786 const struct userdiff_funcname
*pe
= &drv
->funcname
;
787 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
791 try_lookahead
= should_lookahead(opt
);
798 * look_ahead() skips quicly to the line that possibly
799 * has the next hit; don't call it if we need to do
800 * something more than just skipping the current line
801 * in response to an unmatch for the current line. E.g.
802 * inside a post-context window, we will show the current
803 * line as a context around the previous hit when it
808 && lno
<= last_hit
+ opt
->post_context
)
809 && look_ahead(opt
, &left
, &lno
, &bol
))
811 eol
= end_of_line(bol
, &left
);
815 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
816 ctx
= GREP_CONTEXT_BODY
;
818 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
824 /* "grep -v -e foo -e bla" should list lines
825 * that do not have either, so inversion should
830 if (opt
->unmatch_name_only
) {
837 if (opt
->status_only
)
839 if (binary_match_only
) {
840 opt
->output(opt
, "Binary file ", 12);
841 output_color(opt
, name
, strlen(name
),
842 opt
->color_filename
);
843 opt
->output(opt
, " matches\n", 9);
846 if (opt
->name_only
) {
847 show_name(opt
, name
);
850 /* Hit at this line. If we haven't shown the
851 * pre-context lines, we would need to show them.
852 * When asked to do "count", this still show
853 * the context which is nonsense, but the user
854 * deserves to get that ;-).
856 if (opt
->pre_context
)
857 show_pre_context(opt
, name
, buf
, bol
, lno
);
858 else if (opt
->funcname
)
859 show_funcname_line(opt
, name
, buf
, bol
, lno
);
861 show_line(opt
, bol
, eol
, name
, lno
, ':');
865 lno
<= last_hit
+ opt
->post_context
) {
866 /* If the last hit is within the post context,
867 * we need to show this line.
869 show_line(opt
, bol
, eol
, name
, lno
, '-');
883 if (opt
->status_only
)
885 if (opt
->unmatch_name_only
) {
886 /* We did not see any hit, so we want to show this */
887 show_name(opt
, name
);
891 xdiff_clear_find_func(&xecfg
);
895 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
896 * which feels mostly useless but sometimes useful. Maybe
897 * make it another option? For now suppress them.
899 if (opt
->count
&& count
) {
901 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
902 output_sep(opt
, ':');
903 snprintf(buf
, sizeof(buf
), "%u\n", count
);
904 opt
->output(opt
, buf
, strlen(buf
));
909 static void clr_hit_marker(struct grep_expr
*x
)
911 /* All-hit markers are meaningful only at the very top level
916 if (x
->node
!= GREP_NODE_OR
)
918 x
->u
.binary
.left
->hit
= 0;
919 x
= x
->u
.binary
.right
;
923 static int chk_hit_marker(struct grep_expr
*x
)
925 /* Top level nodes have hit markers. See if they all are hits */
927 if (x
->node
!= GREP_NODE_OR
)
929 if (!x
->u
.binary
.left
->hit
)
931 x
= x
->u
.binary
.right
;
935 int grep_buffer(struct grep_opt
*opt
, const char *name
, char *buf
, unsigned long size
)
938 * we do not have to do the two-pass grep when we do not check
939 * buffer-wide "all-match".
942 return grep_buffer_1(opt
, name
, buf
, size
, 0);
944 /* Otherwise the toplevel "or" terms hit a bit differently.
945 * We first clear hit markers from them.
947 clr_hit_marker(opt
->pattern_expression
);
948 grep_buffer_1(opt
, name
, buf
, size
, 1);
950 if (!chk_hit_marker(opt
->pattern_expression
))
953 return grep_buffer_1(opt
, name
, buf
, size
, 0);