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 static void do_append_grep_pat(struct grep_pat
***tail
, struct grep_pat
*p
)
28 void append_header_grep_pattern(struct grep_opt
*opt
,
29 enum grep_header_field field
, const char *pat
)
31 struct grep_pat
*p
= create_grep_pat(pat
, strlen(pat
), "header", 0,
32 GREP_PATTERN_HEAD
, field
);
33 do_append_grep_pat(&opt
->header_tail
, p
);
36 void append_grep_pattern(struct grep_opt
*opt
, const char *pat
,
37 const char *origin
, int no
, enum grep_pat_token t
)
39 append_grep_pat(opt
, pat
, strlen(pat
), origin
, no
, t
);
42 void append_grep_pat(struct grep_opt
*opt
, const char *pat
, size_t patlen
,
43 const char *origin
, int no
, enum grep_pat_token t
)
45 struct grep_pat
*p
= create_grep_pat(pat
, patlen
, origin
, no
, t
, 0);
46 do_append_grep_pat(&opt
->pattern_tail
, p
);
49 struct grep_opt
*grep_opt_dup(const struct grep_opt
*opt
)
52 struct grep_opt
*ret
= xmalloc(sizeof(struct grep_opt
));
55 ret
->pattern_list
= NULL
;
56 ret
->pattern_tail
= &ret
->pattern_list
;
58 for(pat
= opt
->pattern_list
; pat
!= NULL
; pat
= pat
->next
)
60 if(pat
->token
== GREP_PATTERN_HEAD
)
61 append_header_grep_pattern(ret
, pat
->field
,
64 append_grep_pat(ret
, pat
->pattern
, pat
->patternlen
,
65 pat
->origin
, pat
->no
, pat
->token
);
71 static NORETURN
void compile_regexp_failed(const struct grep_pat
*p
,
77 sprintf(where
, "In '%s' at %d, ", p
->origin
, p
->no
);
79 sprintf(where
, "%s, ", p
->origin
);
83 die("%s'%s': %s", where
, p
->pattern
, error
);
87 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
94 options
|= PCRE_CASELESS
;
96 p
->pcre_regexp
= pcre_compile(p
->pattern
, options
, &error
, &erroffset
,
99 compile_regexp_failed(p
, error
);
101 p
->pcre_extra_info
= pcre_study(p
->pcre_regexp
, 0, &error
);
102 if (!p
->pcre_extra_info
&& error
)
106 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
107 regmatch_t
*match
, int eflags
)
109 int ovector
[30], ret
, flags
= 0;
111 if (eflags
& REG_NOTBOL
)
112 flags
|= PCRE_NOTBOL
;
114 ret
= pcre_exec(p
->pcre_regexp
, p
->pcre_extra_info
, line
, eol
- line
,
115 0, flags
, ovector
, ARRAY_SIZE(ovector
));
116 if (ret
< 0 && ret
!= PCRE_ERROR_NOMATCH
)
117 die("pcre_exec failed with error code %d", ret
);
120 match
->rm_so
= ovector
[0];
121 match
->rm_eo
= ovector
[1];
127 static void free_pcre_regexp(struct grep_pat
*p
)
129 pcre_free(p
->pcre_regexp
);
130 pcre_free(p
->pcre_extra_info
);
132 #else /* !USE_LIBPCRE */
133 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
135 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
138 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
139 regmatch_t
*match
, int eflags
)
144 static void free_pcre_regexp(struct grep_pat
*p
)
147 #endif /* !USE_LIBPCRE */
149 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
153 p
->word_regexp
= opt
->word_regexp
;
154 p
->ignore_case
= opt
->ignore_case
;
155 p
->fixed
= opt
->fixed
;
161 compile_pcre_regexp(p
, opt
);
165 err
= regcomp(&p
->regexp
, p
->pattern
, opt
->regflags
);
168 regerror(err
, &p
->regexp
, errbuf
, 1024);
170 compile_regexp_failed(p
, errbuf
);
174 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
175 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
184 case GREP_PATTERN
: /* atom */
185 case GREP_PATTERN_HEAD
:
186 case GREP_PATTERN_BODY
:
187 x
= xcalloc(1, sizeof (struct grep_expr
));
188 x
->node
= GREP_NODE_ATOM
;
192 case GREP_OPEN_PAREN
:
194 x
= compile_pattern_or(list
);
195 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
196 die("unmatched parenthesis");
197 *list
= (*list
)->next
;
204 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
215 die("--not not followed by pattern expression");
217 x
= xcalloc(1, sizeof (struct grep_expr
));
218 x
->node
= GREP_NODE_NOT
;
219 x
->u
.unary
= compile_pattern_not(list
);
221 die("--not followed by non pattern expression");
224 return compile_pattern_atom(list
);
228 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
231 struct grep_expr
*x
, *y
, *z
;
233 x
= compile_pattern_not(list
);
235 if (p
&& p
->token
== GREP_AND
) {
237 die("--and not followed by pattern expression");
239 y
= compile_pattern_and(list
);
241 die("--and not followed by pattern expression");
242 z
= xcalloc(1, sizeof (struct grep_expr
));
243 z
->node
= GREP_NODE_AND
;
244 z
->u
.binary
.left
= x
;
245 z
->u
.binary
.right
= y
;
251 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
254 struct grep_expr
*x
, *y
, *z
;
256 x
= compile_pattern_and(list
);
258 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
259 y
= compile_pattern_or(list
);
261 die("not a pattern expression %s", p
->pattern
);
262 z
= xcalloc(1, sizeof (struct grep_expr
));
263 z
->node
= GREP_NODE_OR
;
264 z
->u
.binary
.left
= x
;
265 z
->u
.binary
.right
= y
;
271 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
273 return compile_pattern_or(list
);
276 static struct grep_expr
*grep_true_expr(void)
278 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
279 z
->node
= GREP_NODE_TRUE
;
283 static struct grep_expr
*grep_or_expr(struct grep_expr
*left
, struct grep_expr
*right
)
285 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
286 z
->node
= GREP_NODE_OR
;
287 z
->u
.binary
.left
= left
;
288 z
->u
.binary
.right
= right
;
292 static struct grep_expr
*prep_header_patterns(struct grep_opt
*opt
)
295 struct grep_expr
*header_expr
;
296 struct grep_expr
*(header_group
[GREP_HEADER_FIELD_MAX
]);
297 enum grep_header_field fld
;
299 if (!opt
->header_list
)
301 p
= opt
->header_list
;
302 for (p
= opt
->header_list
; p
; p
= p
->next
) {
303 if (p
->token
!= GREP_PATTERN_HEAD
)
304 die("bug: a non-header pattern in grep header list.");
305 if (p
->field
< 0 || GREP_HEADER_FIELD_MAX
<= p
->field
)
306 die("bug: unknown header field %d", p
->field
);
307 compile_regexp(p
, opt
);
310 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++)
311 header_group
[fld
] = NULL
;
313 for (p
= opt
->header_list
; p
; p
= p
->next
) {
315 struct grep_pat
*pp
= p
;
317 h
= compile_pattern_atom(&pp
);
318 if (!h
|| pp
!= p
->next
)
319 die("bug: malformed header expr");
320 if (!header_group
[p
->field
]) {
321 header_group
[p
->field
] = h
;
324 header_group
[p
->field
] = grep_or_expr(h
, header_group
[p
->field
]);
329 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++) {
330 if (!header_group
[fld
])
333 header_expr
= grep_true_expr();
334 header_expr
= grep_or_expr(header_group
[fld
], header_expr
);
339 void compile_grep_patterns(struct grep_opt
*opt
)
342 struct grep_expr
*header_expr
= prep_header_patterns(opt
);
344 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
346 case GREP_PATTERN
: /* atom */
347 case GREP_PATTERN_HEAD
:
348 case GREP_PATTERN_BODY
:
349 compile_regexp(p
, opt
);
357 if (opt
->all_match
|| header_expr
)
359 else if (!opt
->extended
)
362 p
= opt
->pattern_list
;
364 opt
->pattern_expression
= compile_pattern_expr(&p
);
366 die("incomplete pattern expression: %s", p
->pattern
);
371 if (!opt
->pattern_expression
)
372 opt
->pattern_expression
= header_expr
;
374 opt
->pattern_expression
= grep_or_expr(opt
->pattern_expression
,
379 static void free_pattern_expr(struct grep_expr
*x
)
386 free_pattern_expr(x
->u
.unary
);
390 free_pattern_expr(x
->u
.binary
.left
);
391 free_pattern_expr(x
->u
.binary
.right
);
397 void free_grep_patterns(struct grep_opt
*opt
)
399 struct grep_pat
*p
, *n
;
401 for (p
= opt
->pattern_list
; p
; p
= n
) {
404 case GREP_PATTERN
: /* atom */
405 case GREP_PATTERN_HEAD
:
406 case GREP_PATTERN_BODY
:
420 free_pattern_expr(opt
->pattern_expression
);
423 static char *end_of_line(char *cp
, unsigned long *left
)
425 unsigned long l
= *left
;
426 while (l
&& *cp
!= '\n') {
434 static int word_char(char ch
)
436 return isalnum(ch
) || ch
== '_';
439 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
442 if (opt
->color
&& color
&& color
[0]) {
443 opt
->output(opt
, color
, strlen(color
));
444 opt
->output(opt
, data
, size
);
445 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
447 opt
->output(opt
, data
, size
);
450 static void output_sep(struct grep_opt
*opt
, char sign
)
452 if (opt
->null_following_name
)
453 opt
->output(opt
, "\0", 1);
455 output_color(opt
, &sign
, 1, opt
->color_sep
);
458 static void show_name(struct grep_opt
*opt
, const char *name
)
460 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
461 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
464 static int fixmatch(struct grep_pat
*p
, char *line
, char *eol
,
469 if (p
->ignore_case
) {
472 hit
= strcasestr(s
, p
->pattern
);
478 hit
= memmem(line
, eol
- line
, p
->pattern
, p
->patternlen
);
481 match
->rm_so
= match
->rm_eo
= -1;
485 match
->rm_so
= hit
- line
;
486 match
->rm_eo
= match
->rm_so
+ p
->patternlen
;
491 static int regmatch(const regex_t
*preg
, char *line
, char *eol
,
492 regmatch_t
*match
, int eflags
)
496 match
->rm_eo
= eol
- line
;
497 eflags
|= REG_STARTEND
;
499 return regexec(preg
, line
, 1, match
, eflags
);
502 static int patmatch(struct grep_pat
*p
, char *line
, char *eol
,
503 regmatch_t
*match
, int eflags
)
508 hit
= !fixmatch(p
, line
, eol
, match
);
509 else if (p
->pcre_regexp
)
510 hit
= !pcrematch(p
, line
, eol
, match
, eflags
);
512 hit
= !regmatch(&p
->regexp
, line
, eol
, match
, eflags
);
517 static int strip_timestamp(char *bol
, char **eol_p
)
522 while (bol
< --eol
) {
538 { "committer ", 10 },
541 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
542 enum grep_context ctx
,
543 regmatch_t
*pmatch
, int eflags
)
547 const char *start
= bol
;
549 if ((p
->token
!= GREP_PATTERN
) &&
550 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
553 if (p
->token
== GREP_PATTERN_HEAD
) {
556 assert(p
->field
< ARRAY_SIZE(header_field
));
557 field
= header_field
[p
->field
].field
;
558 len
= header_field
[p
->field
].len
;
559 if (strncmp(bol
, field
, len
))
562 saved_ch
= strip_timestamp(bol
, &eol
);
566 hit
= patmatch(p
, bol
, eol
, pmatch
, eflags
);
568 if (hit
&& p
->word_regexp
) {
569 if ((pmatch
[0].rm_so
< 0) ||
570 (eol
- bol
) < pmatch
[0].rm_so
||
571 (pmatch
[0].rm_eo
< 0) ||
572 (eol
- bol
) < pmatch
[0].rm_eo
)
573 die("regexp returned nonsense");
575 /* Match beginning must be either beginning of the
576 * line, or at word boundary (i.e. the last char must
577 * not be a word char). Similarly, match end must be
578 * either end of the line, or at word boundary
579 * (i.e. the next char must not be a word char).
581 if ( ((pmatch
[0].rm_so
== 0) ||
582 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
583 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
584 !word_char(bol
[pmatch
[0].rm_eo
])) )
589 /* Words consist of at least one character. */
590 if (pmatch
->rm_so
== pmatch
->rm_eo
)
593 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
594 /* There could be more than one match on the
595 * line, and the first match might not be
596 * strict word match. But later ones could be!
597 * Forward to the next possible start, i.e. the
598 * next position following a non-word char.
600 bol
= pmatch
[0].rm_so
+ bol
+ 1;
601 while (word_char(bol
[-1]) && bol
< eol
)
603 eflags
|= REG_NOTBOL
;
608 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
611 pmatch
[0].rm_so
+= bol
- start
;
612 pmatch
[0].rm_eo
+= bol
- start
;
617 static int match_expr_eval(struct grep_expr
*x
, char *bol
, char *eol
,
618 enum grep_context ctx
, int collect_hits
)
624 die("Not a valid grep expression");
630 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
, &match
, 0);
633 h
= !match_expr_eval(x
->u
.unary
, bol
, eol
, ctx
, 0);
636 if (!match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0))
638 h
= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
642 return (match_expr_eval(x
->u
.binary
.left
,
644 match_expr_eval(x
->u
.binary
.right
,
646 h
= match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
647 x
->u
.binary
.left
->hit
|= h
;
648 h
|= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
651 die("Unexpected node type (internal error) %d", x
->node
);
658 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
659 enum grep_context ctx
, int collect_hits
)
661 struct grep_expr
*x
= opt
->pattern_expression
;
662 return match_expr_eval(x
, bol
, eol
, ctx
, collect_hits
);
665 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
666 enum grep_context ctx
, int collect_hits
)
672 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
674 /* we do not call with collect_hits without being extended */
675 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
676 if (match_one_pattern(p
, bol
, eol
, ctx
, &match
, 0))
682 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
683 enum grep_context ctx
,
684 regmatch_t
*pmatch
, int eflags
)
688 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
690 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
692 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
693 if (match
.rm_so
> pmatch
->rm_so
)
695 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
698 pmatch
->rm_so
= match
.rm_so
;
699 pmatch
->rm_eo
= match
.rm_eo
;
703 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
704 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
709 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
711 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
713 case GREP_PATTERN
: /* atom */
714 case GREP_PATTERN_HEAD
:
715 case GREP_PATTERN_BODY
:
716 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
727 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
728 const char *name
, unsigned lno
, char sign
)
730 int rest
= eol
- bol
;
731 char *line_color
= NULL
;
733 if (opt
->pre_context
|| opt
->post_context
) {
734 if (opt
->last_shown
== 0) {
735 if (opt
->show_hunk_mark
) {
736 output_color(opt
, "--", 2, opt
->color_sep
);
737 opt
->output(opt
, "\n", 1);
739 } else if (lno
> opt
->last_shown
+ 1) {
740 output_color(opt
, "--", 2, opt
->color_sep
);
741 opt
->output(opt
, "\n", 1);
744 opt
->last_shown
= lno
;
747 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
748 output_sep(opt
, sign
);
752 snprintf(buf
, sizeof(buf
), "%d", lno
);
753 output_color(opt
, buf
, strlen(buf
), opt
->color_lineno
);
754 output_sep(opt
, sign
);
758 enum grep_context ctx
= GREP_CONTEXT_BODY
;
763 line_color
= opt
->color_selected
;
764 else if (sign
== '-')
765 line_color
= opt
->color_context
;
766 else if (sign
== '=')
767 line_color
= opt
->color_function
;
769 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
770 if (match
.rm_so
== match
.rm_eo
)
773 output_color(opt
, bol
, match
.rm_so
, line_color
);
774 output_color(opt
, bol
+ match
.rm_so
,
775 match
.rm_eo
- match
.rm_so
,
783 output_color(opt
, bol
, rest
, line_color
);
784 opt
->output(opt
, "\n", 1);
787 static int match_funcname(struct grep_opt
*opt
, char *bol
, char *eol
)
789 xdemitconf_t
*xecfg
= opt
->priv
;
790 if (xecfg
&& xecfg
->find_func
) {
792 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
793 xecfg
->find_func_priv
) >= 0;
798 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
803 static void show_funcname_line(struct grep_opt
*opt
, const char *name
,
804 char *buf
, char *bol
, unsigned lno
)
809 while (bol
> buf
&& bol
[-1] != '\n')
813 if (lno
<= opt
->last_shown
)
816 if (match_funcname(opt
, bol
, eol
)) {
817 show_line(opt
, bol
, eol
, name
, lno
, '=');
823 static void show_pre_context(struct grep_opt
*opt
, const char *name
, char *buf
,
824 char *bol
, unsigned lno
)
826 unsigned cur
= lno
, from
= 1, funcname_lno
= 0;
827 int funcname_needed
= opt
->funcname
;
829 if (opt
->pre_context
< lno
)
830 from
= lno
- opt
->pre_context
;
831 if (from
<= opt
->last_shown
)
832 from
= opt
->last_shown
+ 1;
835 while (bol
> buf
&& cur
> from
) {
838 while (bol
> buf
&& bol
[-1] != '\n')
841 if (funcname_needed
&& match_funcname(opt
, bol
, eol
)) {
847 /* We need to look even further back to find a function signature. */
848 if (opt
->funcname
&& funcname_needed
)
849 show_funcname_line(opt
, name
, buf
, bol
, cur
);
853 char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
857 show_line(opt
, bol
, eol
, name
, cur
, sign
);
863 static int should_lookahead(struct grep_opt
*opt
)
868 return 0; /* punt for too complex stuff */
871 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
872 if (p
->token
!= GREP_PATTERN
)
873 return 0; /* punt for "header only" and stuff */
878 static int look_ahead(struct grep_opt
*opt
,
879 unsigned long *left_p
,
883 unsigned lno
= *lno_p
;
887 regoff_t earliest
= -1;
889 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
893 hit
= patmatch(p
, bol
, bol
+ *left_p
, &m
, 0);
894 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
896 if (earliest
< 0 || m
.rm_so
< earliest
)
901 *bol_p
= bol
+ *left_p
;
905 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
906 ; /* find the beginning of the line */
909 for (sp
= bol
; sp
< last_bol
; sp
++) {
913 *left_p
-= last_bol
- bol
;
919 int grep_threads_ok(const struct grep_opt
*opt
)
921 /* If this condition is true, then we may use the attribute
922 * machinery in grep_buffer_1. The attribute code is not
923 * thread safe, so we disable the use of threads.
925 if (opt
->funcname
&& !opt
->unmatch_name_only
&& !opt
->status_only
&&
932 static void std_output(struct grep_opt
*opt
, const void *buf
, size_t size
)
934 fwrite(buf
, size
, 1, stdout
);
937 static int grep_buffer_1(struct grep_opt
*opt
, const char *name
,
938 char *buf
, unsigned long size
, int collect_hits
)
941 unsigned long left
= size
;
943 unsigned last_hit
= 0;
944 int binary_match_only
= 0;
946 int try_lookahead
= 0;
947 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
951 opt
->output
= std_output
;
953 if (opt
->last_shown
&& (opt
->pre_context
|| opt
->post_context
) &&
954 opt
->output
== std_output
)
955 opt
->show_hunk_mark
= 1;
958 switch (opt
->binary
) {
959 case GREP_BINARY_DEFAULT
:
960 if (buffer_is_binary(buf
, size
))
961 binary_match_only
= 1;
963 case GREP_BINARY_NOMATCH
:
964 if (buffer_is_binary(buf
, size
))
965 return 0; /* Assume unmatch */
967 case GREP_BINARY_TEXT
:
970 die("bug: unknown binary handling mode");
973 memset(&xecfg
, 0, sizeof(xecfg
));
974 if (opt
->funcname
&& !opt
->unmatch_name_only
&& !opt
->status_only
&&
975 !opt
->name_only
&& !binary_match_only
&& !collect_hits
) {
976 struct userdiff_driver
*drv
= userdiff_find_by_path(name
);
977 if (drv
&& drv
->funcname
.pattern
) {
978 const struct userdiff_funcname
*pe
= &drv
->funcname
;
979 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
983 try_lookahead
= should_lookahead(opt
);
990 * look_ahead() skips quickly to the line that possibly
991 * has the next hit; don't call it if we need to do
992 * something more than just skipping the current line
993 * in response to an unmatch for the current line. E.g.
994 * inside a post-context window, we will show the current
995 * line as a context around the previous hit when it
1000 && lno
<= last_hit
+ opt
->post_context
)
1001 && look_ahead(opt
, &left
, &lno
, &bol
))
1003 eol
= end_of_line(bol
, &left
);
1007 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
1008 ctx
= GREP_CONTEXT_BODY
;
1010 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
1016 /* "grep -v -e foo -e bla" should list lines
1017 * that do not have either, so inversion should
1022 if (opt
->unmatch_name_only
) {
1029 if (opt
->status_only
)
1031 if (opt
->name_only
) {
1032 show_name(opt
, name
);
1037 if (binary_match_only
) {
1038 opt
->output(opt
, "Binary file ", 12);
1039 output_color(opt
, name
, strlen(name
),
1040 opt
->color_filename
);
1041 opt
->output(opt
, " matches\n", 9);
1044 /* Hit at this line. If we haven't shown the
1045 * pre-context lines, we would need to show them.
1047 if (opt
->pre_context
)
1048 show_pre_context(opt
, name
, buf
, bol
, lno
);
1049 else if (opt
->funcname
)
1050 show_funcname_line(opt
, name
, buf
, bol
, lno
);
1051 show_line(opt
, bol
, eol
, name
, lno
, ':');
1054 else if (last_hit
&&
1055 lno
<= last_hit
+ opt
->post_context
) {
1056 /* If the last hit is within the post context,
1057 * we need to show this line.
1059 show_line(opt
, bol
, eol
, name
, lno
, '-');
1073 if (opt
->status_only
)
1075 if (opt
->unmatch_name_only
) {
1076 /* We did not see any hit, so we want to show this */
1077 show_name(opt
, name
);
1081 xdiff_clear_find_func(&xecfg
);
1085 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1086 * which feels mostly useless but sometimes useful. Maybe
1087 * make it another option? For now suppress them.
1089 if (opt
->count
&& count
) {
1091 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
1092 output_sep(opt
, ':');
1093 snprintf(buf
, sizeof(buf
), "%u\n", count
);
1094 opt
->output(opt
, buf
, strlen(buf
));
1100 static void clr_hit_marker(struct grep_expr
*x
)
1102 /* All-hit markers are meaningful only at the very top level
1107 if (x
->node
!= GREP_NODE_OR
)
1109 x
->u
.binary
.left
->hit
= 0;
1110 x
= x
->u
.binary
.right
;
1114 static int chk_hit_marker(struct grep_expr
*x
)
1116 /* Top level nodes have hit markers. See if they all are hits */
1118 if (x
->node
!= GREP_NODE_OR
)
1120 if (!x
->u
.binary
.left
->hit
)
1122 x
= x
->u
.binary
.right
;
1126 int grep_buffer(struct grep_opt
*opt
, const char *name
, char *buf
, unsigned long size
)
1129 * we do not have to do the two-pass grep when we do not check
1130 * buffer-wide "all-match".
1132 if (!opt
->all_match
)
1133 return grep_buffer_1(opt
, name
, buf
, size
, 0);
1135 /* Otherwise the toplevel "or" terms hit a bit differently.
1136 * We first clear hit markers from them.
1138 clr_hit_marker(opt
->pattern_expression
);
1139 grep_buffer_1(opt
, name
, buf
, size
, 1);
1141 if (!chk_hit_marker(opt
->pattern_expression
))
1144 return grep_buffer_1(opt
, name
, buf
, size
, 0);