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 NORETURN
void compile_regexp_failed(const struct grep_pat
*p
,
68 sprintf(where
, "In '%s' at %d, ", p
->origin
, p
->no
);
70 sprintf(where
, "%s, ", p
->origin
);
74 die("%s'%s': %s", where
, p
->pattern
, error
);
78 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
85 options
|= PCRE_CASELESS
;
87 p
->pcre_regexp
= pcre_compile(p
->pattern
, options
, &error
, &erroffset
,
90 compile_regexp_failed(p
, error
);
92 p
->pcre_extra_info
= pcre_study(p
->pcre_regexp
, 0, &error
);
93 if (!p
->pcre_extra_info
&& error
)
97 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
98 regmatch_t
*match
, int eflags
)
100 int ovector
[30], ret
, flags
= 0;
102 if (eflags
& REG_NOTBOL
)
103 flags
|= PCRE_NOTBOL
;
105 ret
= pcre_exec(p
->pcre_regexp
, p
->pcre_extra_info
, line
, eol
- line
,
106 0, flags
, ovector
, ARRAY_SIZE(ovector
));
107 if (ret
< 0 && ret
!= PCRE_ERROR_NOMATCH
)
108 die("pcre_exec failed with error code %d", ret
);
111 match
->rm_so
= ovector
[0];
112 match
->rm_eo
= ovector
[1];
118 static void free_pcre_regexp(struct grep_pat
*p
)
120 pcre_free(p
->pcre_regexp
);
121 pcre_free(p
->pcre_extra_info
);
123 #else /* !USE_LIBPCRE */
124 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
126 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
129 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
130 regmatch_t
*match
, int eflags
)
135 static void free_pcre_regexp(struct grep_pat
*p
)
138 #endif /* !USE_LIBPCRE */
140 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
144 p
->word_regexp
= opt
->word_regexp
;
145 p
->ignore_case
= opt
->ignore_case
;
146 p
->fixed
= opt
->fixed
;
152 compile_pcre_regexp(p
, opt
);
156 err
= regcomp(&p
->regexp
, p
->pattern
, opt
->regflags
);
159 regerror(err
, &p
->regexp
, errbuf
, 1024);
161 compile_regexp_failed(p
, errbuf
);
165 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
166 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
175 case GREP_PATTERN
: /* atom */
176 case GREP_PATTERN_HEAD
:
177 case GREP_PATTERN_BODY
:
178 x
= xcalloc(1, sizeof (struct grep_expr
));
179 x
->node
= GREP_NODE_ATOM
;
183 case GREP_OPEN_PAREN
:
185 x
= compile_pattern_or(list
);
186 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
187 die("unmatched parenthesis");
188 *list
= (*list
)->next
;
195 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
206 die("--not not followed by pattern expression");
208 x
= xcalloc(1, sizeof (struct grep_expr
));
209 x
->node
= GREP_NODE_NOT
;
210 x
->u
.unary
= compile_pattern_not(list
);
212 die("--not followed by non pattern expression");
215 return compile_pattern_atom(list
);
219 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
222 struct grep_expr
*x
, *y
, *z
;
224 x
= compile_pattern_not(list
);
226 if (p
&& p
->token
== GREP_AND
) {
228 die("--and not followed by pattern expression");
230 y
= compile_pattern_and(list
);
232 die("--and not followed by pattern expression");
233 z
= xcalloc(1, sizeof (struct grep_expr
));
234 z
->node
= GREP_NODE_AND
;
235 z
->u
.binary
.left
= x
;
236 z
->u
.binary
.right
= y
;
242 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
245 struct grep_expr
*x
, *y
, *z
;
247 x
= compile_pattern_and(list
);
249 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
250 y
= compile_pattern_or(list
);
252 die("not a pattern expression %s", p
->pattern
);
253 z
= xcalloc(1, sizeof (struct grep_expr
));
254 z
->node
= GREP_NODE_OR
;
255 z
->u
.binary
.left
= x
;
256 z
->u
.binary
.right
= y
;
262 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
264 return compile_pattern_or(list
);
267 static struct grep_expr
*grep_true_expr(void)
269 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
270 z
->node
= GREP_NODE_TRUE
;
274 static struct grep_expr
*grep_or_expr(struct grep_expr
*left
, struct grep_expr
*right
)
276 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
277 z
->node
= GREP_NODE_OR
;
278 z
->u
.binary
.left
= left
;
279 z
->u
.binary
.right
= right
;
283 static struct grep_expr
*prep_header_patterns(struct grep_opt
*opt
)
286 struct grep_expr
*header_expr
;
287 struct grep_expr
*(header_group
[GREP_HEADER_FIELD_MAX
]);
288 enum grep_header_field fld
;
290 if (!opt
->header_list
)
292 p
= opt
->header_list
;
293 for (p
= opt
->header_list
; p
; p
= p
->next
) {
294 if (p
->token
!= GREP_PATTERN_HEAD
)
295 die("bug: a non-header pattern in grep header list.");
296 if (p
->field
< 0 || GREP_HEADER_FIELD_MAX
<= p
->field
)
297 die("bug: unknown header field %d", p
->field
);
298 compile_regexp(p
, opt
);
301 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++)
302 header_group
[fld
] = NULL
;
304 for (p
= opt
->header_list
; p
; p
= p
->next
) {
306 struct grep_pat
*pp
= p
;
308 h
= compile_pattern_atom(&pp
);
309 if (!h
|| pp
!= p
->next
)
310 die("bug: malformed header expr");
311 if (!header_group
[p
->field
]) {
312 header_group
[p
->field
] = h
;
315 header_group
[p
->field
] = grep_or_expr(h
, header_group
[p
->field
]);
320 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++) {
321 if (!header_group
[fld
])
324 header_expr
= grep_true_expr();
325 header_expr
= grep_or_expr(header_group
[fld
], header_expr
);
330 void compile_grep_patterns(struct grep_opt
*opt
)
333 struct grep_expr
*header_expr
= prep_header_patterns(opt
);
335 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
337 case GREP_PATTERN
: /* atom */
338 case GREP_PATTERN_HEAD
:
339 case GREP_PATTERN_BODY
:
340 compile_regexp(p
, opt
);
348 if (opt
->all_match
|| header_expr
)
350 else if (!opt
->extended
)
353 p
= opt
->pattern_list
;
355 opt
->pattern_expression
= compile_pattern_expr(&p
);
357 die("incomplete pattern expression: %s", p
->pattern
);
362 if (!opt
->pattern_expression
)
363 opt
->pattern_expression
= header_expr
;
365 opt
->pattern_expression
= grep_or_expr(opt
->pattern_expression
,
370 static void free_pattern_expr(struct grep_expr
*x
)
377 free_pattern_expr(x
->u
.unary
);
381 free_pattern_expr(x
->u
.binary
.left
);
382 free_pattern_expr(x
->u
.binary
.right
);
388 void free_grep_patterns(struct grep_opt
*opt
)
390 struct grep_pat
*p
, *n
;
392 for (p
= opt
->pattern_list
; p
; p
= n
) {
395 case GREP_PATTERN
: /* atom */
396 case GREP_PATTERN_HEAD
:
397 case GREP_PATTERN_BODY
:
411 free_pattern_expr(opt
->pattern_expression
);
414 static char *end_of_line(char *cp
, unsigned long *left
)
416 unsigned long l
= *left
;
417 while (l
&& *cp
!= '\n') {
425 static int word_char(char ch
)
427 return isalnum(ch
) || ch
== '_';
430 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
433 if (want_color(opt
->color
) && color
&& color
[0]) {
434 opt
->output(opt
, color
, strlen(color
));
435 opt
->output(opt
, data
, size
);
436 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
438 opt
->output(opt
, data
, size
);
441 static void output_sep(struct grep_opt
*opt
, char sign
)
443 if (opt
->null_following_name
)
444 opt
->output(opt
, "\0", 1);
446 output_color(opt
, &sign
, 1, opt
->color_sep
);
449 static void show_name(struct grep_opt
*opt
, const char *name
)
451 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
452 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
455 static int fixmatch(struct grep_pat
*p
, char *line
, char *eol
,
460 if (p
->ignore_case
) {
463 hit
= strcasestr(s
, p
->pattern
);
469 hit
= memmem(line
, eol
- line
, p
->pattern
, p
->patternlen
);
472 match
->rm_so
= match
->rm_eo
= -1;
476 match
->rm_so
= hit
- line
;
477 match
->rm_eo
= match
->rm_so
+ p
->patternlen
;
482 static int regmatch(const regex_t
*preg
, char *line
, char *eol
,
483 regmatch_t
*match
, int eflags
)
487 match
->rm_eo
= eol
- line
;
488 eflags
|= REG_STARTEND
;
490 return regexec(preg
, line
, 1, match
, eflags
);
493 static int patmatch(struct grep_pat
*p
, char *line
, char *eol
,
494 regmatch_t
*match
, int eflags
)
499 hit
= !fixmatch(p
, line
, eol
, match
);
500 else if (p
->pcre_regexp
)
501 hit
= !pcrematch(p
, line
, eol
, match
, eflags
);
503 hit
= !regmatch(&p
->regexp
, line
, eol
, match
, eflags
);
508 static int strip_timestamp(char *bol
, char **eol_p
)
513 while (bol
< --eol
) {
529 { "committer ", 10 },
532 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
533 enum grep_context ctx
,
534 regmatch_t
*pmatch
, int eflags
)
538 const char *start
= bol
;
540 if ((p
->token
!= GREP_PATTERN
) &&
541 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
544 if (p
->token
== GREP_PATTERN_HEAD
) {
547 assert(p
->field
< ARRAY_SIZE(header_field
));
548 field
= header_field
[p
->field
].field
;
549 len
= header_field
[p
->field
].len
;
550 if (strncmp(bol
, field
, len
))
553 saved_ch
= strip_timestamp(bol
, &eol
);
557 hit
= patmatch(p
, bol
, eol
, pmatch
, eflags
);
559 if (hit
&& p
->word_regexp
) {
560 if ((pmatch
[0].rm_so
< 0) ||
561 (eol
- bol
) < pmatch
[0].rm_so
||
562 (pmatch
[0].rm_eo
< 0) ||
563 (eol
- bol
) < pmatch
[0].rm_eo
)
564 die("regexp returned nonsense");
566 /* Match beginning must be either beginning of the
567 * line, or at word boundary (i.e. the last char must
568 * not be a word char). Similarly, match end must be
569 * either end of the line, or at word boundary
570 * (i.e. the next char must not be a word char).
572 if ( ((pmatch
[0].rm_so
== 0) ||
573 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
574 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
575 !word_char(bol
[pmatch
[0].rm_eo
])) )
580 /* Words consist of at least one character. */
581 if (pmatch
->rm_so
== pmatch
->rm_eo
)
584 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
585 /* There could be more than one match on the
586 * line, and the first match might not be
587 * strict word match. But later ones could be!
588 * Forward to the next possible start, i.e. the
589 * next position following a non-word char.
591 bol
= pmatch
[0].rm_so
+ bol
+ 1;
592 while (word_char(bol
[-1]) && bol
< eol
)
594 eflags
|= REG_NOTBOL
;
599 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
602 pmatch
[0].rm_so
+= bol
- start
;
603 pmatch
[0].rm_eo
+= bol
- start
;
608 static int match_expr_eval(struct grep_expr
*x
, char *bol
, char *eol
,
609 enum grep_context ctx
, int collect_hits
)
615 die("Not a valid grep expression");
621 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
, &match
, 0);
624 h
= !match_expr_eval(x
->u
.unary
, bol
, eol
, ctx
, 0);
627 if (!match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0))
629 h
= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
633 return (match_expr_eval(x
->u
.binary
.left
,
635 match_expr_eval(x
->u
.binary
.right
,
637 h
= match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
638 x
->u
.binary
.left
->hit
|= h
;
639 h
|= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
642 die("Unexpected node type (internal error) %d", x
->node
);
649 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
650 enum grep_context ctx
, int collect_hits
)
652 struct grep_expr
*x
= opt
->pattern_expression
;
653 return match_expr_eval(x
, bol
, eol
, ctx
, collect_hits
);
656 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
657 enum grep_context ctx
, int collect_hits
)
663 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
665 /* we do not call with collect_hits without being extended */
666 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
667 if (match_one_pattern(p
, bol
, eol
, ctx
, &match
, 0))
673 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
674 enum grep_context ctx
,
675 regmatch_t
*pmatch
, int eflags
)
679 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
681 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
683 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
684 if (match
.rm_so
> pmatch
->rm_so
)
686 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
689 pmatch
->rm_so
= match
.rm_so
;
690 pmatch
->rm_eo
= match
.rm_eo
;
694 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
695 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
700 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
702 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
704 case GREP_PATTERN
: /* atom */
705 case GREP_PATTERN_HEAD
:
706 case GREP_PATTERN_BODY
:
707 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
718 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
719 const char *name
, unsigned lno
, char sign
)
721 int rest
= eol
- bol
;
722 char *line_color
= NULL
;
724 if (opt
->file_break
&& opt
->last_shown
== 0) {
725 if (opt
->show_hunk_mark
)
726 opt
->output(opt
, "\n", 1);
727 } else if (opt
->pre_context
|| opt
->post_context
|| opt
->funcbody
) {
728 if (opt
->last_shown
== 0) {
729 if (opt
->show_hunk_mark
) {
730 output_color(opt
, "--", 2, opt
->color_sep
);
731 opt
->output(opt
, "\n", 1);
733 } else if (lno
> opt
->last_shown
+ 1) {
734 output_color(opt
, "--", 2, opt
->color_sep
);
735 opt
->output(opt
, "\n", 1);
738 if (opt
->heading
&& opt
->last_shown
== 0) {
739 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
740 opt
->output(opt
, "\n", 1);
742 opt
->last_shown
= lno
;
744 if (!opt
->heading
&& opt
->pathname
) {
745 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
746 output_sep(opt
, sign
);
750 snprintf(buf
, sizeof(buf
), "%d", lno
);
751 output_color(opt
, buf
, strlen(buf
), opt
->color_lineno
);
752 output_sep(opt
, sign
);
756 enum grep_context ctx
= GREP_CONTEXT_BODY
;
761 line_color
= opt
->color_selected
;
762 else if (sign
== '-')
763 line_color
= opt
->color_context
;
764 else if (sign
== '=')
765 line_color
= opt
->color_function
;
767 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
768 if (match
.rm_so
== match
.rm_eo
)
771 output_color(opt
, bol
, match
.rm_so
, line_color
);
772 output_color(opt
, bol
+ match
.rm_so
,
773 match
.rm_eo
- match
.rm_so
,
781 output_color(opt
, bol
, rest
, line_color
);
782 opt
->output(opt
, "\n", 1);
785 static int match_funcname(struct grep_opt
*opt
, char *bol
, char *eol
)
787 xdemitconf_t
*xecfg
= opt
->priv
;
788 if (xecfg
&& xecfg
->find_func
) {
790 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
791 xecfg
->find_func_priv
) >= 0;
796 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
801 static void show_funcname_line(struct grep_opt
*opt
, const char *name
,
802 char *buf
, char *bol
, unsigned lno
)
807 while (bol
> buf
&& bol
[-1] != '\n')
811 if (lno
<= opt
->last_shown
)
814 if (match_funcname(opt
, bol
, eol
)) {
815 show_line(opt
, bol
, eol
, name
, lno
, '=');
821 static void show_pre_context(struct grep_opt
*opt
, const char *name
, char *buf
,
822 char *bol
, char *end
, unsigned lno
)
824 unsigned cur
= lno
, from
= 1, funcname_lno
= 0;
825 int funcname_needed
= !!opt
->funcname
;
827 if (opt
->funcbody
&& !match_funcname(opt
, bol
, end
))
830 if (opt
->pre_context
< lno
)
831 from
= lno
- opt
->pre_context
;
832 if (from
<= opt
->last_shown
)
833 from
= opt
->last_shown
+ 1;
837 cur
> (funcname_needed
== 2 ? opt
->last_shown
+ 1 : from
)) {
840 while (bol
> buf
&& bol
[-1] != '\n')
843 if (funcname_needed
&& match_funcname(opt
, bol
, eol
)) {
849 /* We need to look even further back to find a function signature. */
850 if (opt
->funcname
&& funcname_needed
)
851 show_funcname_line(opt
, name
, buf
, bol
, cur
);
855 char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
859 show_line(opt
, bol
, eol
, name
, cur
, sign
);
865 static int should_lookahead(struct grep_opt
*opt
)
870 return 0; /* punt for too complex stuff */
873 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
874 if (p
->token
!= GREP_PATTERN
)
875 return 0; /* punt for "header only" and stuff */
880 static int look_ahead(struct grep_opt
*opt
,
881 unsigned long *left_p
,
885 unsigned lno
= *lno_p
;
889 regoff_t earliest
= -1;
891 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
895 hit
= patmatch(p
, bol
, bol
+ *left_p
, &m
, 0);
896 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
898 if (earliest
< 0 || m
.rm_so
< earliest
)
903 *bol_p
= bol
+ *left_p
;
907 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
908 ; /* find the beginning of the line */
911 for (sp
= bol
; sp
< last_bol
; sp
++) {
915 *left_p
-= last_bol
- bol
;
921 int grep_threads_ok(const struct grep_opt
*opt
)
923 /* If this condition is true, then we may use the attribute
924 * machinery in grep_buffer_1. The attribute code is not
925 * thread safe, so we disable the use of threads.
927 if (opt
->funcname
&& !opt
->unmatch_name_only
&& !opt
->status_only
&&
934 static void std_output(struct grep_opt
*opt
, const void *buf
, size_t size
)
936 fwrite(buf
, size
, 1, stdout
);
939 static int grep_buffer_1(struct grep_opt
*opt
, const char *name
,
940 char *buf
, unsigned long size
, int collect_hits
)
943 unsigned long left
= size
;
945 unsigned last_hit
= 0;
946 int binary_match_only
= 0;
948 int try_lookahead
= 0;
949 int show_function
= 0;
950 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
954 opt
->output
= std_output
;
956 if (opt
->pre_context
|| opt
->post_context
|| opt
->file_break
||
958 /* Show hunk marks, except for the first file. */
960 opt
->show_hunk_mark
= 1;
962 * If we're using threads then we can't easily identify
963 * the first file. Always put hunk marks in that case
964 * and skip the very first one later in work_done().
966 if (opt
->output
!= std_output
)
967 opt
->show_hunk_mark
= 1;
971 switch (opt
->binary
) {
972 case GREP_BINARY_DEFAULT
:
973 if (buffer_is_binary(buf
, size
))
974 binary_match_only
= 1;
976 case GREP_BINARY_NOMATCH
:
977 if (buffer_is_binary(buf
, size
))
978 return 0; /* Assume unmatch */
980 case GREP_BINARY_TEXT
:
983 die("bug: unknown binary handling mode");
986 memset(&xecfg
, 0, sizeof(xecfg
));
987 if (opt
->funcname
&& !opt
->unmatch_name_only
&& !opt
->status_only
&&
988 !opt
->name_only
&& !binary_match_only
&& !collect_hits
) {
989 struct userdiff_driver
*drv
= userdiff_find_by_path(name
);
990 if (drv
&& drv
->funcname
.pattern
) {
991 const struct userdiff_funcname
*pe
= &drv
->funcname
;
992 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
996 try_lookahead
= should_lookahead(opt
);
1003 * look_ahead() skips quickly to the line that possibly
1004 * has the next hit; don't call it if we need to do
1005 * something more than just skipping the current line
1006 * in response to an unmatch for the current line. E.g.
1007 * inside a post-context window, we will show the current
1008 * line as a context around the previous hit when it
1013 && (show_function
||
1014 lno
<= last_hit
+ opt
->post_context
))
1015 && look_ahead(opt
, &left
, &lno
, &bol
))
1017 eol
= end_of_line(bol
, &left
);
1021 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
1022 ctx
= GREP_CONTEXT_BODY
;
1024 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
1030 /* "grep -v -e foo -e bla" should list lines
1031 * that do not have either, so inversion should
1036 if (opt
->unmatch_name_only
) {
1043 if (opt
->status_only
)
1045 if (opt
->name_only
) {
1046 show_name(opt
, name
);
1051 if (binary_match_only
) {
1052 opt
->output(opt
, "Binary file ", 12);
1053 output_color(opt
, name
, strlen(name
),
1054 opt
->color_filename
);
1055 opt
->output(opt
, " matches\n", 9);
1058 /* Hit at this line. If we haven't shown the
1059 * pre-context lines, we would need to show them.
1061 if (opt
->pre_context
|| opt
->funcbody
)
1062 show_pre_context(opt
, name
, buf
, bol
, eol
, lno
);
1063 else if (opt
->funcname
)
1064 show_funcname_line(opt
, name
, buf
, bol
, lno
);
1065 show_line(opt
, bol
, eol
, name
, lno
, ':');
1071 if (show_function
&& match_funcname(opt
, bol
, eol
))
1073 if (show_function
||
1074 (last_hit
&& lno
<= last_hit
+ opt
->post_context
)) {
1075 /* If the last hit is within the post context,
1076 * we need to show this line.
1078 show_line(opt
, bol
, eol
, name
, lno
, '-');
1092 if (opt
->status_only
)
1094 if (opt
->unmatch_name_only
) {
1095 /* We did not see any hit, so we want to show this */
1096 show_name(opt
, name
);
1100 xdiff_clear_find_func(&xecfg
);
1104 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1105 * which feels mostly useless but sometimes useful. Maybe
1106 * make it another option? For now suppress them.
1108 if (opt
->count
&& count
) {
1110 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
1111 output_sep(opt
, ':');
1112 snprintf(buf
, sizeof(buf
), "%u\n", count
);
1113 opt
->output(opt
, buf
, strlen(buf
));
1119 static void clr_hit_marker(struct grep_expr
*x
)
1121 /* All-hit markers are meaningful only at the very top level
1126 if (x
->node
!= GREP_NODE_OR
)
1128 x
->u
.binary
.left
->hit
= 0;
1129 x
= x
->u
.binary
.right
;
1133 static int chk_hit_marker(struct grep_expr
*x
)
1135 /* Top level nodes have hit markers. See if they all are hits */
1137 if (x
->node
!= GREP_NODE_OR
)
1139 if (!x
->u
.binary
.left
->hit
)
1141 x
= x
->u
.binary
.right
;
1145 int grep_buffer(struct grep_opt
*opt
, const char *name
, char *buf
, unsigned long size
)
1148 * we do not have to do the two-pass grep when we do not check
1149 * buffer-wide "all-match".
1151 if (!opt
->all_match
)
1152 return grep_buffer_1(opt
, name
, buf
, size
, 0);
1154 /* Otherwise the toplevel "or" terms hit a bit differently.
1155 * We first clear hit markers from them.
1157 clr_hit_marker(opt
->pattern_expression
);
1158 grep_buffer_1(opt
, name
, buf
, size
, 1);
1160 if (!chk_hit_marker(opt
->pattern_expression
))
1163 return grep_buffer_1(opt
, name
, buf
, size
, 0);