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 show_name(struct grep_opt
*opt
, const char *name
)
275 opt
->output(opt
, name
, strlen(name
));
276 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
280 static int fixmatch(const char *pattern
, char *line
, int ignore_case
, regmatch_t
*match
)
284 hit
= strcasestr(line
, pattern
);
286 hit
= strstr(line
, pattern
);
289 match
->rm_so
= match
->rm_eo
= -1;
293 match
->rm_so
= hit
- line
;
294 match
->rm_eo
= match
->rm_so
+ strlen(pattern
);
299 static int strip_timestamp(char *bol
, char **eol_p
)
304 while (bol
< --eol
) {
320 { "committer ", 10 },
323 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
324 enum grep_context ctx
,
325 regmatch_t
*pmatch
, int eflags
)
329 const char *start
= bol
;
331 if ((p
->token
!= GREP_PATTERN
) &&
332 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
335 if (p
->token
== GREP_PATTERN_HEAD
) {
338 assert(p
->field
< ARRAY_SIZE(header_field
));
339 field
= header_field
[p
->field
].field
;
340 len
= header_field
[p
->field
].len
;
341 if (strncmp(bol
, field
, len
))
344 saved_ch
= strip_timestamp(bol
, &eol
);
349 hit
= !fixmatch(p
->pattern
, bol
, p
->ignore_case
, pmatch
);
351 hit
= !regexec(&p
->regexp
, bol
, 1, pmatch
, eflags
);
353 if (hit
&& p
->word_regexp
) {
354 if ((pmatch
[0].rm_so
< 0) ||
355 (eol
- bol
) < pmatch
[0].rm_so
||
356 (pmatch
[0].rm_eo
< 0) ||
357 (eol
- bol
) < pmatch
[0].rm_eo
)
358 die("regexp returned nonsense");
360 /* Match beginning must be either beginning of the
361 * line, or at word boundary (i.e. the last char must
362 * not be a word char). Similarly, match end must be
363 * either end of the line, or at word boundary
364 * (i.e. the next char must not be a word char).
366 if ( ((pmatch
[0].rm_so
== 0) ||
367 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
368 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
369 !word_char(bol
[pmatch
[0].rm_eo
])) )
374 /* Words consist of at least one character. */
375 if (pmatch
->rm_so
== pmatch
->rm_eo
)
378 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
379 /* There could be more than one match on the
380 * line, and the first match might not be
381 * strict word match. But later ones could be!
382 * Forward to the next possible start, i.e. the
383 * next position following a non-word char.
385 bol
= pmatch
[0].rm_so
+ bol
+ 1;
386 while (word_char(bol
[-1]) && bol
< eol
)
388 eflags
|= REG_NOTBOL
;
393 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
396 pmatch
[0].rm_so
+= bol
- start
;
397 pmatch
[0].rm_eo
+= bol
- start
;
402 static int match_expr_eval(struct grep_expr
*x
, char *bol
, char *eol
,
403 enum grep_context ctx
, int collect_hits
)
409 die("Not a valid grep expression");
412 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
, &match
, 0);
415 h
= !match_expr_eval(x
->u
.unary
, bol
, eol
, ctx
, 0);
418 if (!match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0))
420 h
= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
424 return (match_expr_eval(x
->u
.binary
.left
,
426 match_expr_eval(x
->u
.binary
.right
,
428 h
= match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
429 x
->u
.binary
.left
->hit
|= h
;
430 h
|= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
433 die("Unexpected node type (internal error) %d", x
->node
);
440 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
441 enum grep_context ctx
, int collect_hits
)
443 struct grep_expr
*x
= opt
->pattern_expression
;
444 return match_expr_eval(x
, bol
, eol
, ctx
, collect_hits
);
447 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
448 enum grep_context ctx
, int collect_hits
)
454 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
456 /* we do not call with collect_hits without being extended */
457 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
458 if (match_one_pattern(p
, bol
, eol
, ctx
, &match
, 0))
464 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
465 enum grep_context ctx
,
466 regmatch_t
*pmatch
, int eflags
)
470 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
472 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
474 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
475 if (match
.rm_so
> pmatch
->rm_so
)
477 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
480 pmatch
->rm_so
= match
.rm_so
;
481 pmatch
->rm_eo
= match
.rm_eo
;
485 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
486 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
491 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
493 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
495 case GREP_PATTERN
: /* atom */
496 case GREP_PATTERN_HEAD
:
497 case GREP_PATTERN_BODY
:
498 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
509 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
510 const char *name
, unsigned lno
, char sign
)
512 int rest
= eol
- bol
;
516 if (opt
->pre_context
|| opt
->post_context
) {
517 if (opt
->last_shown
== 0) {
518 if (opt
->show_hunk_mark
)
519 opt
->output(opt
, "--\n", 3);
521 opt
->show_hunk_mark
= 1;
522 } else if (lno
> opt
->last_shown
+ 1)
523 opt
->output(opt
, "--\n", 3);
525 opt
->last_shown
= lno
;
527 if (opt
->null_following_name
)
530 opt
->output(opt
, name
, strlen(name
));
531 opt
->output(opt
, sign_str
, 1);
535 snprintf(buf
, sizeof(buf
), "%d", lno
);
536 opt
->output(opt
, buf
, strlen(buf
));
537 opt
->output(opt
, sign_str
, 1);
541 enum grep_context ctx
= GREP_CONTEXT_BODY
;
546 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
547 if (match
.rm_so
== match
.rm_eo
)
550 opt
->output(opt
, bol
, match
.rm_so
);
551 opt
->output(opt
, opt
->color_match
,
552 strlen(opt
->color_match
));
553 opt
->output(opt
, bol
+ match
.rm_so
,
554 (int)(match
.rm_eo
- match
.rm_so
));
555 opt
->output(opt
, GIT_COLOR_RESET
,
556 strlen(GIT_COLOR_RESET
));
563 opt
->output(opt
, bol
, rest
);
564 opt
->output(opt
, "\n", 1);
567 static int match_funcname(struct grep_opt
*opt
, char *bol
, char *eol
)
569 xdemitconf_t
*xecfg
= opt
->priv
;
570 if (xecfg
&& xecfg
->find_func
) {
572 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
573 xecfg
->find_func_priv
) >= 0;
578 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
583 static void show_funcname_line(struct grep_opt
*opt
, const char *name
,
584 char *buf
, char *bol
, unsigned lno
)
589 while (bol
> buf
&& bol
[-1] != '\n')
593 if (lno
<= opt
->last_shown
)
596 if (match_funcname(opt
, bol
, eol
)) {
597 show_line(opt
, bol
, eol
, name
, lno
, '=');
603 static void show_pre_context(struct grep_opt
*opt
, const char *name
, char *buf
,
604 char *bol
, unsigned lno
)
606 unsigned cur
= lno
, from
= 1, funcname_lno
= 0;
607 int funcname_needed
= opt
->funcname
;
609 if (opt
->pre_context
< lno
)
610 from
= lno
- opt
->pre_context
;
611 if (from
<= opt
->last_shown
)
612 from
= opt
->last_shown
+ 1;
615 while (bol
> buf
&& cur
> from
) {
618 while (bol
> buf
&& bol
[-1] != '\n')
621 if (funcname_needed
&& match_funcname(opt
, bol
, eol
)) {
627 /* We need to look even further back to find a function signature. */
628 if (opt
->funcname
&& funcname_needed
)
629 show_funcname_line(opt
, name
, buf
, bol
, cur
);
633 char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
637 show_line(opt
, bol
, eol
, name
, cur
, sign
);
643 static int should_lookahead(struct grep_opt
*opt
)
648 return 0; /* punt for too complex stuff */
651 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
652 if (p
->token
!= GREP_PATTERN
)
653 return 0; /* punt for "header only" and stuff */
658 static int look_ahead(struct grep_opt
*opt
,
659 unsigned long *left_p
,
663 unsigned lno
= *lno_p
;
667 regoff_t earliest
= -1;
669 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
674 hit
= !fixmatch(p
->pattern
, bol
, p
->ignore_case
, &m
);
679 hit
= !regexec(&p
->regexp
, bol
, 1, &m
, REG_STARTEND
);
681 hit
= !regexec(&p
->regexp
, bol
, 1, &m
, 0);
684 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
686 if (earliest
< 0 || m
.rm_so
< earliest
)
691 *bol_p
= bol
+ *left_p
;
695 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
696 ; /* find the beginning of the line */
699 for (sp
= bol
; sp
< last_bol
; sp
++) {
703 *left_p
-= last_bol
- bol
;
709 int grep_threads_ok(const struct grep_opt
*opt
)
711 /* If this condition is true, then we may use the attribute
712 * machinery in grep_buffer_1. The attribute code is not
713 * thread safe, so we disable the use of threads.
715 if (opt
->funcname
&& !opt
->unmatch_name_only
&& !opt
->status_only
&&
719 /* If we are showing hunk marks, we should not do it for the
720 * first match. The synchronization problem we get for this
721 * constraint is not yet solved, so we disable threading in
724 if (opt
->pre_context
|| opt
->post_context
)
730 static void std_output(struct grep_opt
*opt
, const void *buf
, size_t size
)
732 fwrite(buf
, size
, 1, stdout
);
735 static int grep_buffer_1(struct grep_opt
*opt
, const char *name
,
736 char *buf
, unsigned long size
, int collect_hits
)
739 unsigned long left
= size
;
741 unsigned last_hit
= 0;
742 int binary_match_only
= 0;
744 int try_lookahead
= 0;
745 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
751 opt
->output
= std_output
;
753 if (buffer_is_binary(buf
, size
)) {
754 switch (opt
->binary
) {
755 case GREP_BINARY_DEFAULT
:
756 binary_match_only
= 1;
758 case GREP_BINARY_NOMATCH
:
759 return 0; /* Assume unmatch */
766 memset(&xecfg
, 0, sizeof(xecfg
));
767 if (opt
->funcname
&& !opt
->unmatch_name_only
&& !opt
->status_only
&&
768 !opt
->name_only
&& !binary_match_only
&& !collect_hits
) {
769 struct userdiff_driver
*drv
= userdiff_find_by_path(name
);
770 if (drv
&& drv
->funcname
.pattern
) {
771 const struct userdiff_funcname
*pe
= &drv
->funcname
;
772 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
776 try_lookahead
= should_lookahead(opt
);
783 * look_ahead() skips quicly to the line that possibly
784 * has the next hit; don't call it if we need to do
785 * something more than just skipping the current line
786 * in response to an unmatch for the current line. E.g.
787 * inside a post-context window, we will show the current
788 * line as a context around the previous hit when it
793 && lno
<= last_hit
+ opt
->post_context
)
794 && look_ahead(opt
, &left
, &lno
, &bol
))
796 eol
= end_of_line(bol
, &left
);
800 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
801 ctx
= GREP_CONTEXT_BODY
;
803 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
809 /* "grep -v -e foo -e bla" should list lines
810 * that do not have either, so inversion should
815 if (opt
->unmatch_name_only
) {
822 if (opt
->status_only
)
824 if (binary_match_only
) {
825 opt
->output(opt
, "Binary file ", 12);
826 opt
->output(opt
, name
, strlen(name
));
827 opt
->output(opt
, " matches\n", 9);
830 if (opt
->name_only
) {
831 show_name(opt
, name
);
834 /* Hit at this line. If we haven't shown the
835 * pre-context lines, we would need to show them.
836 * When asked to do "count", this still show
837 * the context which is nonsense, but the user
838 * deserves to get that ;-).
840 if (opt
->pre_context
)
841 show_pre_context(opt
, name
, buf
, bol
, lno
);
842 else if (opt
->funcname
)
843 show_funcname_line(opt
, name
, buf
, bol
, lno
);
845 show_line(opt
, bol
, eol
, name
, lno
, ':');
849 lno
<= last_hit
+ opt
->post_context
) {
850 /* If the last hit is within the post context,
851 * we need to show this line.
853 show_line(opt
, bol
, eol
, name
, lno
, '-');
867 if (opt
->status_only
)
869 if (opt
->unmatch_name_only
) {
870 /* We did not see any hit, so we want to show this */
871 show_name(opt
, name
);
875 xdiff_clear_find_func(&xecfg
);
879 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
880 * which feels mostly useless but sometimes useful. Maybe
881 * make it another option? For now suppress them.
883 if (opt
->count
&& count
) {
885 opt
->output(opt
, name
, strlen(name
));
886 snprintf(buf
, sizeof(buf
), "%c%u\n",
887 opt
->null_following_name
? '\0' : ':', count
);
888 opt
->output(opt
, buf
, strlen(buf
));
893 static void clr_hit_marker(struct grep_expr
*x
)
895 /* All-hit markers are meaningful only at the very top level
900 if (x
->node
!= GREP_NODE_OR
)
902 x
->u
.binary
.left
->hit
= 0;
903 x
= x
->u
.binary
.right
;
907 static int chk_hit_marker(struct grep_expr
*x
)
909 /* Top level nodes have hit markers. See if they all are hits */
911 if (x
->node
!= GREP_NODE_OR
)
913 if (!x
->u
.binary
.left
->hit
)
915 x
= x
->u
.binary
.right
;
919 int grep_buffer(struct grep_opt
*opt
, const char *name
, char *buf
, unsigned long size
)
922 * we do not have to do the two-pass grep when we do not check
923 * buffer-wide "all-match".
926 return grep_buffer_1(opt
, name
, buf
, size
, 0);
928 /* Otherwise the toplevel "or" terms hit a bit differently.
929 * We first clear hit markers from them.
931 clr_hit_marker(opt
->pattern_expression
);
932 grep_buffer_1(opt
, name
, buf
, size
, 1);
934 if (!chk_hit_marker(opt
->pattern_expression
))
937 return grep_buffer_1(opt
, name
, buf
, size
, 0);