4 #include "xdiff-interface.h"
6 static struct grep_pat
*create_grep_pat(const char *pat
, size_t patlen
,
7 const char *origin
, int no
,
9 enum grep_header_field field
)
11 struct grep_pat
*p
= xcalloc(1, sizeof(*p
));
13 p
->patternlen
= patlen
;
21 void append_header_grep_pattern(struct grep_opt
*opt
,
22 enum grep_header_field field
, const char *pat
)
24 struct grep_pat
*p
= create_grep_pat(pat
, strlen(pat
), "header", 0,
25 GREP_PATTERN_HEAD
, field
);
26 *opt
->header_tail
= p
;
27 opt
->header_tail
= &p
->next
;
31 void append_grep_pattern(struct grep_opt
*opt
, const char *pat
,
32 const char *origin
, int no
, enum grep_pat_token t
)
34 append_grep_pat(opt
, pat
, strlen(pat
), origin
, no
, t
);
37 void append_grep_pat(struct grep_opt
*opt
, const char *pat
, size_t patlen
,
38 const char *origin
, int no
, enum grep_pat_token t
)
40 struct grep_pat
*p
= create_grep_pat(pat
, patlen
, origin
, no
, t
, 0);
41 *opt
->pattern_tail
= p
;
42 opt
->pattern_tail
= &p
->next
;
46 struct grep_opt
*grep_opt_dup(const struct grep_opt
*opt
)
49 struct grep_opt
*ret
= xmalloc(sizeof(struct grep_opt
));
52 ret
->pattern_list
= NULL
;
53 ret
->pattern_tail
= &ret
->pattern_list
;
55 for(pat
= opt
->pattern_list
; pat
!= NULL
; pat
= pat
->next
)
57 if(pat
->token
== GREP_PATTERN_HEAD
)
58 append_header_grep_pattern(ret
, pat
->field
,
61 append_grep_pat(ret
, pat
->pattern
, pat
->patternlen
,
62 pat
->origin
, pat
->no
, pat
->token
);
68 static NORETURN
void compile_regexp_failed(const struct grep_pat
*p
,
74 sprintf(where
, "In '%s' at %d, ", p
->origin
, p
->no
);
76 sprintf(where
, "%s, ", p
->origin
);
80 die("%s'%s': %s", where
, p
->pattern
, error
);
84 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
91 options
|= PCRE_CASELESS
;
93 p
->pcre_regexp
= pcre_compile(p
->pattern
, options
, &error
, &erroffset
,
96 compile_regexp_failed(p
, error
);
98 p
->pcre_extra_info
= pcre_study(p
->pcre_regexp
, 0, &error
);
99 if (!p
->pcre_extra_info
&& error
)
103 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
104 regmatch_t
*match
, int eflags
)
106 int ovector
[30], ret
, flags
= 0;
108 if (eflags
& REG_NOTBOL
)
109 flags
|= PCRE_NOTBOL
;
111 ret
= pcre_exec(p
->pcre_regexp
, p
->pcre_extra_info
, line
, eol
- line
,
112 0, flags
, ovector
, ARRAY_SIZE(ovector
));
113 if (ret
< 0 && ret
!= PCRE_ERROR_NOMATCH
)
114 die("pcre_exec failed with error code %d", ret
);
117 match
->rm_so
= ovector
[0];
118 match
->rm_eo
= ovector
[1];
124 static void free_pcre_regexp(struct grep_pat
*p
)
126 pcre_free(p
->pcre_regexp
);
127 pcre_free(p
->pcre_extra_info
);
129 #else /* !USE_LIBPCRE */
130 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
132 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
135 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
136 regmatch_t
*match
, int eflags
)
141 static void free_pcre_regexp(struct grep_pat
*p
)
144 #endif /* !USE_LIBPCRE */
146 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
150 p
->word_regexp
= opt
->word_regexp
;
151 p
->ignore_case
= opt
->ignore_case
;
152 p
->fixed
= opt
->fixed
;
158 compile_pcre_regexp(p
, opt
);
162 err
= regcomp(&p
->regexp
, p
->pattern
, opt
->regflags
);
165 regerror(err
, &p
->regexp
, errbuf
, 1024);
167 compile_regexp_failed(p
, errbuf
);
171 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
172 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
181 case GREP_PATTERN
: /* atom */
182 case GREP_PATTERN_HEAD
:
183 case GREP_PATTERN_BODY
:
184 x
= xcalloc(1, sizeof (struct grep_expr
));
185 x
->node
= GREP_NODE_ATOM
;
189 case GREP_OPEN_PAREN
:
191 x
= compile_pattern_or(list
);
192 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
193 die("unmatched parenthesis");
194 *list
= (*list
)->next
;
201 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
212 die("--not not followed by pattern expression");
214 x
= xcalloc(1, sizeof (struct grep_expr
));
215 x
->node
= GREP_NODE_NOT
;
216 x
->u
.unary
= compile_pattern_not(list
);
218 die("--not followed by non pattern expression");
221 return compile_pattern_atom(list
);
225 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
228 struct grep_expr
*x
, *y
, *z
;
230 x
= compile_pattern_not(list
);
232 if (p
&& p
->token
== GREP_AND
) {
234 die("--and not followed by pattern expression");
236 y
= compile_pattern_and(list
);
238 die("--and not followed by pattern expression");
239 z
= xcalloc(1, sizeof (struct grep_expr
));
240 z
->node
= GREP_NODE_AND
;
241 z
->u
.binary
.left
= x
;
242 z
->u
.binary
.right
= y
;
248 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
251 struct grep_expr
*x
, *y
, *z
;
253 x
= compile_pattern_and(list
);
255 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
256 y
= compile_pattern_or(list
);
258 die("not a pattern expression %s", p
->pattern
);
259 z
= xcalloc(1, sizeof (struct grep_expr
));
260 z
->node
= GREP_NODE_OR
;
261 z
->u
.binary
.left
= x
;
262 z
->u
.binary
.right
= y
;
268 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
270 return compile_pattern_or(list
);
273 static struct grep_expr
*grep_true_expr(void)
275 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
276 z
->node
= GREP_NODE_TRUE
;
280 static struct grep_expr
*grep_or_expr(struct grep_expr
*left
, struct grep_expr
*right
)
282 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
283 z
->node
= GREP_NODE_OR
;
284 z
->u
.binary
.left
= left
;
285 z
->u
.binary
.right
= right
;
289 static struct grep_expr
*prep_header_patterns(struct grep_opt
*opt
)
292 struct grep_expr
*header_expr
;
293 struct grep_expr
*(header_group
[GREP_HEADER_FIELD_MAX
]);
294 enum grep_header_field fld
;
296 if (!opt
->header_list
)
298 p
= opt
->header_list
;
299 for (p
= opt
->header_list
; p
; p
= p
->next
) {
300 if (p
->token
!= GREP_PATTERN_HEAD
)
301 die("bug: a non-header pattern in grep header list.");
302 if (p
->field
< 0 || GREP_HEADER_FIELD_MAX
<= p
->field
)
303 die("bug: unknown header field %d", p
->field
);
304 compile_regexp(p
, opt
);
307 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++)
308 header_group
[fld
] = NULL
;
310 for (p
= opt
->header_list
; p
; p
= p
->next
) {
312 struct grep_pat
*pp
= p
;
314 h
= compile_pattern_atom(&pp
);
315 if (!h
|| pp
!= p
->next
)
316 die("bug: malformed header expr");
317 if (!header_group
[p
->field
]) {
318 header_group
[p
->field
] = h
;
321 header_group
[p
->field
] = grep_or_expr(h
, header_group
[p
->field
]);
326 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++) {
327 if (!header_group
[fld
])
330 header_expr
= grep_true_expr();
331 header_expr
= grep_or_expr(header_group
[fld
], header_expr
);
336 void compile_grep_patterns(struct grep_opt
*opt
)
339 struct grep_expr
*header_expr
= prep_header_patterns(opt
);
341 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
343 case GREP_PATTERN
: /* atom */
344 case GREP_PATTERN_HEAD
:
345 case GREP_PATTERN_BODY
:
346 compile_regexp(p
, opt
);
354 if (opt
->all_match
|| header_expr
)
356 else if (!opt
->extended
)
359 p
= opt
->pattern_list
;
361 opt
->pattern_expression
= compile_pattern_expr(&p
);
363 die("incomplete pattern expression: %s", p
->pattern
);
368 if (!opt
->pattern_expression
)
369 opt
->pattern_expression
= header_expr
;
371 opt
->pattern_expression
= grep_or_expr(opt
->pattern_expression
,
376 static void free_pattern_expr(struct grep_expr
*x
)
383 free_pattern_expr(x
->u
.unary
);
387 free_pattern_expr(x
->u
.binary
.left
);
388 free_pattern_expr(x
->u
.binary
.right
);
394 void free_grep_patterns(struct grep_opt
*opt
)
396 struct grep_pat
*p
, *n
;
398 for (p
= opt
->pattern_list
; p
; p
= n
) {
401 case GREP_PATTERN
: /* atom */
402 case GREP_PATTERN_HEAD
:
403 case GREP_PATTERN_BODY
:
417 free_pattern_expr(opt
->pattern_expression
);
420 static char *end_of_line(char *cp
, unsigned long *left
)
422 unsigned long l
= *left
;
423 while (l
&& *cp
!= '\n') {
431 static int word_char(char ch
)
433 return isalnum(ch
) || ch
== '_';
436 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
439 if (opt
->color
&& color
&& color
[0]) {
440 opt
->output(opt
, color
, strlen(color
));
441 opt
->output(opt
, data
, size
);
442 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
444 opt
->output(opt
, data
, size
);
447 static void output_sep(struct grep_opt
*opt
, char sign
)
449 if (opt
->null_following_name
)
450 opt
->output(opt
, "\0", 1);
452 output_color(opt
, &sign
, 1, opt
->color_sep
);
455 static void show_name(struct grep_opt
*opt
, const char *name
)
457 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
458 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
461 static int fixmatch(struct grep_pat
*p
, char *line
, char *eol
,
466 if (p
->ignore_case
) {
469 hit
= strcasestr(s
, p
->pattern
);
475 hit
= memmem(line
, eol
- line
, p
->pattern
, p
->patternlen
);
478 match
->rm_so
= match
->rm_eo
= -1;
482 match
->rm_so
= hit
- line
;
483 match
->rm_eo
= match
->rm_so
+ p
->patternlen
;
488 static int regmatch(const regex_t
*preg
, char *line
, char *eol
,
489 regmatch_t
*match
, int eflags
)
493 match
->rm_eo
= eol
- line
;
494 eflags
|= REG_STARTEND
;
496 return regexec(preg
, line
, 1, match
, eflags
);
499 static int patmatch(struct grep_pat
*p
, char *line
, char *eol
,
500 regmatch_t
*match
, int eflags
)
505 hit
= !fixmatch(p
, line
, eol
, match
);
506 else if (p
->pcre_regexp
)
507 hit
= !pcrematch(p
, line
, eol
, match
, eflags
);
509 hit
= !regmatch(&p
->regexp
, line
, eol
, match
, eflags
);
514 static int strip_timestamp(char *bol
, char **eol_p
)
519 while (bol
< --eol
) {
535 { "committer ", 10 },
538 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
539 enum grep_context ctx
,
540 regmatch_t
*pmatch
, int eflags
)
544 const char *start
= bol
;
546 if ((p
->token
!= GREP_PATTERN
) &&
547 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
550 if (p
->token
== GREP_PATTERN_HEAD
) {
553 assert(p
->field
< ARRAY_SIZE(header_field
));
554 field
= header_field
[p
->field
].field
;
555 len
= header_field
[p
->field
].len
;
556 if (strncmp(bol
, field
, len
))
559 saved_ch
= strip_timestamp(bol
, &eol
);
563 hit
= patmatch(p
, bol
, eol
, pmatch
, eflags
);
565 if (hit
&& p
->word_regexp
) {
566 if ((pmatch
[0].rm_so
< 0) ||
567 (eol
- bol
) < pmatch
[0].rm_so
||
568 (pmatch
[0].rm_eo
< 0) ||
569 (eol
- bol
) < pmatch
[0].rm_eo
)
570 die("regexp returned nonsense");
572 /* Match beginning must be either beginning of the
573 * line, or at word boundary (i.e. the last char must
574 * not be a word char). Similarly, match end must be
575 * either end of the line, or at word boundary
576 * (i.e. the next char must not be a word char).
578 if ( ((pmatch
[0].rm_so
== 0) ||
579 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
580 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
581 !word_char(bol
[pmatch
[0].rm_eo
])) )
586 /* Words consist of at least one character. */
587 if (pmatch
->rm_so
== pmatch
->rm_eo
)
590 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
591 /* There could be more than one match on the
592 * line, and the first match might not be
593 * strict word match. But later ones could be!
594 * Forward to the next possible start, i.e. the
595 * next position following a non-word char.
597 bol
= pmatch
[0].rm_so
+ bol
+ 1;
598 while (word_char(bol
[-1]) && bol
< eol
)
600 eflags
|= REG_NOTBOL
;
605 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
608 pmatch
[0].rm_so
+= bol
- start
;
609 pmatch
[0].rm_eo
+= bol
- start
;
614 static int match_expr_eval(struct grep_expr
*x
, char *bol
, char *eol
,
615 enum grep_context ctx
, int collect_hits
)
621 die("Not a valid grep expression");
627 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
, &match
, 0);
630 h
= !match_expr_eval(x
->u
.unary
, bol
, eol
, ctx
, 0);
633 if (!match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0))
635 h
= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
639 return (match_expr_eval(x
->u
.binary
.left
,
641 match_expr_eval(x
->u
.binary
.right
,
643 h
= match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
644 x
->u
.binary
.left
->hit
|= h
;
645 h
|= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
648 die("Unexpected node type (internal error) %d", x
->node
);
655 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
656 enum grep_context ctx
, int collect_hits
)
658 struct grep_expr
*x
= opt
->pattern_expression
;
659 return match_expr_eval(x
, bol
, eol
, ctx
, collect_hits
);
662 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
663 enum grep_context ctx
, int collect_hits
)
669 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
671 /* we do not call with collect_hits without being extended */
672 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
673 if (match_one_pattern(p
, bol
, eol
, ctx
, &match
, 0))
679 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
680 enum grep_context ctx
,
681 regmatch_t
*pmatch
, int eflags
)
685 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
687 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
689 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
690 if (match
.rm_so
> pmatch
->rm_so
)
692 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
695 pmatch
->rm_so
= match
.rm_so
;
696 pmatch
->rm_eo
= match
.rm_eo
;
700 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
701 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
706 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
708 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
710 case GREP_PATTERN
: /* atom */
711 case GREP_PATTERN_HEAD
:
712 case GREP_PATTERN_BODY
:
713 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
724 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
725 const char *name
, unsigned lno
, char sign
)
727 int rest
= eol
- bol
;
728 char *line_color
= NULL
;
730 if (opt
->pre_context
|| opt
->post_context
) {
731 if (opt
->last_shown
== 0) {
732 if (opt
->show_hunk_mark
) {
733 output_color(opt
, "--", 2, opt
->color_sep
);
734 opt
->output(opt
, "\n", 1);
736 } else if (lno
> opt
->last_shown
+ 1) {
737 output_color(opt
, "--", 2, opt
->color_sep
);
738 opt
->output(opt
, "\n", 1);
741 opt
->last_shown
= lno
;
744 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
745 output_sep(opt
, sign
);
749 snprintf(buf
, sizeof(buf
), "%d", lno
);
750 output_color(opt
, buf
, strlen(buf
), opt
->color_lineno
);
751 output_sep(opt
, sign
);
755 enum grep_context ctx
= GREP_CONTEXT_BODY
;
760 line_color
= opt
->color_selected
;
761 else if (sign
== '-')
762 line_color
= opt
->color_context
;
763 else if (sign
== '=')
764 line_color
= opt
->color_function
;
766 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
767 if (match
.rm_so
== match
.rm_eo
)
770 output_color(opt
, bol
, match
.rm_so
, line_color
);
771 output_color(opt
, bol
+ match
.rm_so
,
772 match
.rm_eo
- match
.rm_so
,
780 output_color(opt
, bol
, rest
, line_color
);
781 opt
->output(opt
, "\n", 1);
784 static int match_funcname(struct grep_opt
*opt
, char *bol
, char *eol
)
786 xdemitconf_t
*xecfg
= opt
->priv
;
787 if (xecfg
&& xecfg
->find_func
) {
789 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
790 xecfg
->find_func_priv
) >= 0;
795 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
800 static void show_funcname_line(struct grep_opt
*opt
, const char *name
,
801 char *buf
, char *bol
, unsigned lno
)
806 while (bol
> buf
&& bol
[-1] != '\n')
810 if (lno
<= opt
->last_shown
)
813 if (match_funcname(opt
, bol
, eol
)) {
814 show_line(opt
, bol
, eol
, name
, lno
, '=');
820 static void show_pre_context(struct grep_opt
*opt
, const char *name
, char *buf
,
821 char *bol
, unsigned lno
)
823 unsigned cur
= lno
, from
= 1, funcname_lno
= 0;
824 int funcname_needed
= opt
->funcname
;
826 if (opt
->pre_context
< lno
)
827 from
= lno
- opt
->pre_context
;
828 if (from
<= opt
->last_shown
)
829 from
= opt
->last_shown
+ 1;
832 while (bol
> buf
&& cur
> from
) {
835 while (bol
> buf
&& bol
[-1] != '\n')
838 if (funcname_needed
&& match_funcname(opt
, bol
, eol
)) {
844 /* We need to look even further back to find a function signature. */
845 if (opt
->funcname
&& funcname_needed
)
846 show_funcname_line(opt
, name
, buf
, bol
, cur
);
850 char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
854 show_line(opt
, bol
, eol
, name
, cur
, sign
);
860 static int should_lookahead(struct grep_opt
*opt
)
865 return 0; /* punt for too complex stuff */
868 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
869 if (p
->token
!= GREP_PATTERN
)
870 return 0; /* punt for "header only" and stuff */
875 static int look_ahead(struct grep_opt
*opt
,
876 unsigned long *left_p
,
880 unsigned lno
= *lno_p
;
884 regoff_t earliest
= -1;
886 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
890 hit
= patmatch(p
, bol
, bol
+ *left_p
, &m
, 0);
891 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
893 if (earliest
< 0 || m
.rm_so
< earliest
)
898 *bol_p
= bol
+ *left_p
;
902 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
903 ; /* find the beginning of the line */
906 for (sp
= bol
; sp
< last_bol
; sp
++) {
910 *left_p
-= last_bol
- bol
;
916 int grep_threads_ok(const struct grep_opt
*opt
)
918 /* If this condition is true, then we may use the attribute
919 * machinery in grep_buffer_1. The attribute code is not
920 * thread safe, so we disable the use of threads.
922 if (opt
->funcname
&& !opt
->unmatch_name_only
&& !opt
->status_only
&&
929 static void std_output(struct grep_opt
*opt
, const void *buf
, size_t size
)
931 fwrite(buf
, size
, 1, stdout
);
934 static int grep_buffer_1(struct grep_opt
*opt
, const char *name
,
935 char *buf
, unsigned long size
, int collect_hits
)
938 unsigned long left
= size
;
940 unsigned last_hit
= 0;
941 int binary_match_only
= 0;
943 int try_lookahead
= 0;
944 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
948 opt
->output
= std_output
;
950 if (opt
->last_shown
&& (opt
->pre_context
|| opt
->post_context
) &&
951 opt
->output
== std_output
)
952 opt
->show_hunk_mark
= 1;
955 switch (opt
->binary
) {
956 case GREP_BINARY_DEFAULT
:
957 if (buffer_is_binary(buf
, size
))
958 binary_match_only
= 1;
960 case GREP_BINARY_NOMATCH
:
961 if (buffer_is_binary(buf
, size
))
962 return 0; /* Assume unmatch */
964 case GREP_BINARY_TEXT
:
967 die("bug: unknown binary handling mode");
970 memset(&xecfg
, 0, sizeof(xecfg
));
971 if (opt
->funcname
&& !opt
->unmatch_name_only
&& !opt
->status_only
&&
972 !opt
->name_only
&& !binary_match_only
&& !collect_hits
) {
973 struct userdiff_driver
*drv
= userdiff_find_by_path(name
);
974 if (drv
&& drv
->funcname
.pattern
) {
975 const struct userdiff_funcname
*pe
= &drv
->funcname
;
976 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
980 try_lookahead
= should_lookahead(opt
);
987 * look_ahead() skips quickly to the line that possibly
988 * has the next hit; don't call it if we need to do
989 * something more than just skipping the current line
990 * in response to an unmatch for the current line. E.g.
991 * inside a post-context window, we will show the current
992 * line as a context around the previous hit when it
997 && lno
<= last_hit
+ opt
->post_context
)
998 && look_ahead(opt
, &left
, &lno
, &bol
))
1000 eol
= end_of_line(bol
, &left
);
1004 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
1005 ctx
= GREP_CONTEXT_BODY
;
1007 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
1013 /* "grep -v -e foo -e bla" should list lines
1014 * that do not have either, so inversion should
1019 if (opt
->unmatch_name_only
) {
1026 if (opt
->status_only
)
1028 if (opt
->name_only
) {
1029 show_name(opt
, name
);
1034 if (binary_match_only
) {
1035 opt
->output(opt
, "Binary file ", 12);
1036 output_color(opt
, name
, strlen(name
),
1037 opt
->color_filename
);
1038 opt
->output(opt
, " matches\n", 9);
1041 /* Hit at this line. If we haven't shown the
1042 * pre-context lines, we would need to show them.
1044 if (opt
->pre_context
)
1045 show_pre_context(opt
, name
, buf
, bol
, lno
);
1046 else if (opt
->funcname
)
1047 show_funcname_line(opt
, name
, buf
, bol
, lno
);
1048 show_line(opt
, bol
, eol
, name
, lno
, ':');
1051 else if (last_hit
&&
1052 lno
<= last_hit
+ opt
->post_context
) {
1053 /* If the last hit is within the post context,
1054 * we need to show this line.
1056 show_line(opt
, bol
, eol
, name
, lno
, '-');
1070 if (opt
->status_only
)
1072 if (opt
->unmatch_name_only
) {
1073 /* We did not see any hit, so we want to show this */
1074 show_name(opt
, name
);
1078 xdiff_clear_find_func(&xecfg
);
1082 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1083 * which feels mostly useless but sometimes useful. Maybe
1084 * make it another option? For now suppress them.
1086 if (opt
->count
&& count
) {
1088 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
1089 output_sep(opt
, ':');
1090 snprintf(buf
, sizeof(buf
), "%u\n", count
);
1091 opt
->output(opt
, buf
, strlen(buf
));
1097 static void clr_hit_marker(struct grep_expr
*x
)
1099 /* All-hit markers are meaningful only at the very top level
1104 if (x
->node
!= GREP_NODE_OR
)
1106 x
->u
.binary
.left
->hit
= 0;
1107 x
= x
->u
.binary
.right
;
1111 static int chk_hit_marker(struct grep_expr
*x
)
1113 /* Top level nodes have hit markers. See if they all are hits */
1115 if (x
->node
!= GREP_NODE_OR
)
1117 if (!x
->u
.binary
.left
->hit
)
1119 x
= x
->u
.binary
.right
;
1123 int grep_buffer(struct grep_opt
*opt
, const char *name
, char *buf
, unsigned long size
)
1126 * we do not have to do the two-pass grep when we do not check
1127 * buffer-wide "all-match".
1129 if (!opt
->all_match
)
1130 return grep_buffer_1(opt
, name
, buf
, size
, 0);
1132 /* Otherwise the toplevel "or" terms hit a bit differently.
1133 * We first clear hit markers from them.
1135 clr_hit_marker(opt
->pattern_expression
);
1136 grep_buffer_1(opt
, name
, buf
, size
, 1);
1138 if (!chk_hit_marker(opt
->pattern_expression
))
1141 return grep_buffer_1(opt
, name
, buf
, size
, 0);