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 void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
66 p
->word_regexp
= opt
->word_regexp
;
67 p
->ignore_case
= opt
->ignore_case
;
68 p
->fixed
= opt
->fixed
;
73 err
= regcomp(&p
->regexp
, p
->pattern
, opt
->regflags
);
78 sprintf(where
, "In '%s' at %d, ",
81 sprintf(where
, "%s, ", p
->origin
);
84 regerror(err
, &p
->regexp
, errbuf
, 1024);
86 die("%s'%s': %s", where
, p
->pattern
, errbuf
);
90 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
91 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
100 case GREP_PATTERN
: /* atom */
101 case GREP_PATTERN_HEAD
:
102 case GREP_PATTERN_BODY
:
103 x
= xcalloc(1, sizeof (struct grep_expr
));
104 x
->node
= GREP_NODE_ATOM
;
108 case GREP_OPEN_PAREN
:
110 x
= compile_pattern_or(list
);
111 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
112 die("unmatched parenthesis");
113 *list
= (*list
)->next
;
120 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
131 die("--not not followed by pattern expression");
133 x
= xcalloc(1, sizeof (struct grep_expr
));
134 x
->node
= GREP_NODE_NOT
;
135 x
->u
.unary
= compile_pattern_not(list
);
137 die("--not followed by non pattern expression");
140 return compile_pattern_atom(list
);
144 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
147 struct grep_expr
*x
, *y
, *z
;
149 x
= compile_pattern_not(list
);
151 if (p
&& p
->token
== GREP_AND
) {
153 die("--and not followed by pattern expression");
155 y
= compile_pattern_and(list
);
157 die("--and not followed by pattern expression");
158 z
= xcalloc(1, sizeof (struct grep_expr
));
159 z
->node
= GREP_NODE_AND
;
160 z
->u
.binary
.left
= x
;
161 z
->u
.binary
.right
= y
;
167 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
170 struct grep_expr
*x
, *y
, *z
;
172 x
= compile_pattern_and(list
);
174 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
175 y
= compile_pattern_or(list
);
177 die("not a pattern expression %s", p
->pattern
);
178 z
= xcalloc(1, sizeof (struct grep_expr
));
179 z
->node
= GREP_NODE_OR
;
180 z
->u
.binary
.left
= x
;
181 z
->u
.binary
.right
= y
;
187 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
189 return compile_pattern_or(list
);
192 static struct grep_expr
*grep_true_expr(void)
194 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
195 z
->node
= GREP_NODE_TRUE
;
199 static struct grep_expr
*grep_or_expr(struct grep_expr
*left
, struct grep_expr
*right
)
201 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
202 z
->node
= GREP_NODE_OR
;
203 z
->u
.binary
.left
= left
;
204 z
->u
.binary
.right
= right
;
208 static struct grep_expr
*prep_header_patterns(struct grep_opt
*opt
)
211 struct grep_expr
*header_expr
;
212 struct grep_expr
*(header_group
[GREP_HEADER_FIELD_MAX
]);
213 enum grep_header_field fld
;
215 if (!opt
->header_list
)
217 p
= opt
->header_list
;
218 for (p
= opt
->header_list
; p
; p
= p
->next
) {
219 if (p
->token
!= GREP_PATTERN_HEAD
)
220 die("bug: a non-header pattern in grep header list.");
221 if (p
->field
< 0 || GREP_HEADER_FIELD_MAX
<= p
->field
)
222 die("bug: unknown header field %d", p
->field
);
223 compile_regexp(p
, opt
);
226 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++)
227 header_group
[fld
] = NULL
;
229 for (p
= opt
->header_list
; p
; p
= p
->next
) {
231 struct grep_pat
*pp
= p
;
233 h
= compile_pattern_atom(&pp
);
234 if (!h
|| pp
!= p
->next
)
235 die("bug: malformed header expr");
236 if (!header_group
[p
->field
]) {
237 header_group
[p
->field
] = h
;
240 header_group
[p
->field
] = grep_or_expr(h
, header_group
[p
->field
]);
245 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++) {
246 if (!header_group
[fld
])
249 header_expr
= grep_true_expr();
250 header_expr
= grep_or_expr(header_group
[fld
], header_expr
);
255 void compile_grep_patterns(struct grep_opt
*opt
)
258 struct grep_expr
*header_expr
= prep_header_patterns(opt
);
260 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
262 case GREP_PATTERN
: /* atom */
263 case GREP_PATTERN_HEAD
:
264 case GREP_PATTERN_BODY
:
265 compile_regexp(p
, opt
);
273 if (opt
->all_match
|| header_expr
)
275 else if (!opt
->extended
)
278 p
= opt
->pattern_list
;
280 opt
->pattern_expression
= compile_pattern_expr(&p
);
282 die("incomplete pattern expression: %s", p
->pattern
);
287 if (!opt
->pattern_expression
)
288 opt
->pattern_expression
= header_expr
;
290 opt
->pattern_expression
= grep_or_expr(opt
->pattern_expression
,
295 static void free_pattern_expr(struct grep_expr
*x
)
302 free_pattern_expr(x
->u
.unary
);
306 free_pattern_expr(x
->u
.binary
.left
);
307 free_pattern_expr(x
->u
.binary
.right
);
313 void free_grep_patterns(struct grep_opt
*opt
)
315 struct grep_pat
*p
, *n
;
317 for (p
= opt
->pattern_list
; p
; p
= n
) {
320 case GREP_PATTERN
: /* atom */
321 case GREP_PATTERN_HEAD
:
322 case GREP_PATTERN_BODY
:
333 free_pattern_expr(opt
->pattern_expression
);
336 static char *end_of_line(char *cp
, unsigned long *left
)
338 unsigned long l
= *left
;
339 while (l
&& *cp
!= '\n') {
347 static int word_char(char ch
)
349 return isalnum(ch
) || ch
== '_';
352 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
355 if (opt
->color
&& color
&& color
[0]) {
356 opt
->output(opt
, color
, strlen(color
));
357 opt
->output(opt
, data
, size
);
358 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
360 opt
->output(opt
, data
, size
);
363 static void output_sep(struct grep_opt
*opt
, char sign
)
365 if (opt
->null_following_name
)
366 opt
->output(opt
, "\0", 1);
368 output_color(opt
, &sign
, 1, opt
->color_sep
);
371 static void show_name(struct grep_opt
*opt
, const char *name
)
373 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
374 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
377 static int fixmatch(struct grep_pat
*p
, char *line
, char *eol
,
382 if (p
->ignore_case
) {
385 hit
= strcasestr(s
, p
->pattern
);
391 hit
= memmem(line
, eol
- line
, p
->pattern
, p
->patternlen
);
394 match
->rm_so
= match
->rm_eo
= -1;
398 match
->rm_so
= hit
- line
;
399 match
->rm_eo
= match
->rm_so
+ p
->patternlen
;
404 static int regmatch(const regex_t
*preg
, char *line
, char *eol
,
405 regmatch_t
*match
, int eflags
)
409 match
->rm_eo
= eol
- line
;
410 eflags
|= REG_STARTEND
;
412 return regexec(preg
, line
, 1, match
, eflags
);
415 static int patmatch(struct grep_pat
*p
, char *line
, char *eol
,
416 regmatch_t
*match
, int eflags
)
421 hit
= !fixmatch(p
, line
, eol
, match
);
423 hit
= !regmatch(&p
->regexp
, line
, eol
, match
, eflags
);
428 static int strip_timestamp(char *bol
, char **eol_p
)
433 while (bol
< --eol
) {
449 { "committer ", 10 },
452 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
453 enum grep_context ctx
,
454 regmatch_t
*pmatch
, int eflags
)
458 const char *start
= bol
;
460 if ((p
->token
!= GREP_PATTERN
) &&
461 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
464 if (p
->token
== GREP_PATTERN_HEAD
) {
467 assert(p
->field
< ARRAY_SIZE(header_field
));
468 field
= header_field
[p
->field
].field
;
469 len
= header_field
[p
->field
].len
;
470 if (strncmp(bol
, field
, len
))
473 saved_ch
= strip_timestamp(bol
, &eol
);
477 hit
= patmatch(p
, bol
, eol
, pmatch
, eflags
);
479 if (hit
&& p
->word_regexp
) {
480 if ((pmatch
[0].rm_so
< 0) ||
481 (eol
- bol
) < pmatch
[0].rm_so
||
482 (pmatch
[0].rm_eo
< 0) ||
483 (eol
- bol
) < pmatch
[0].rm_eo
)
484 die("regexp returned nonsense");
486 /* Match beginning must be either beginning of the
487 * line, or at word boundary (i.e. the last char must
488 * not be a word char). Similarly, match end must be
489 * either end of the line, or at word boundary
490 * (i.e. the next char must not be a word char).
492 if ( ((pmatch
[0].rm_so
== 0) ||
493 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
494 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
495 !word_char(bol
[pmatch
[0].rm_eo
])) )
500 /* Words consist of at least one character. */
501 if (pmatch
->rm_so
== pmatch
->rm_eo
)
504 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
505 /* There could be more than one match on the
506 * line, and the first match might not be
507 * strict word match. But later ones could be!
508 * Forward to the next possible start, i.e. the
509 * next position following a non-word char.
511 bol
= pmatch
[0].rm_so
+ bol
+ 1;
512 while (word_char(bol
[-1]) && bol
< eol
)
514 eflags
|= REG_NOTBOL
;
519 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
522 pmatch
[0].rm_so
+= bol
- start
;
523 pmatch
[0].rm_eo
+= bol
- start
;
528 static int match_expr_eval(struct grep_expr
*x
, char *bol
, char *eol
,
529 enum grep_context ctx
, int collect_hits
)
535 die("Not a valid grep expression");
541 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
, &match
, 0);
544 h
= !match_expr_eval(x
->u
.unary
, bol
, eol
, ctx
, 0);
547 if (!match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0))
549 h
= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
553 return (match_expr_eval(x
->u
.binary
.left
,
555 match_expr_eval(x
->u
.binary
.right
,
557 h
= match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
558 x
->u
.binary
.left
->hit
|= h
;
559 h
|= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
562 die("Unexpected node type (internal error) %d", x
->node
);
569 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
570 enum grep_context ctx
, int collect_hits
)
572 struct grep_expr
*x
= opt
->pattern_expression
;
573 return match_expr_eval(x
, bol
, eol
, ctx
, collect_hits
);
576 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
577 enum grep_context ctx
, int collect_hits
)
583 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
585 /* we do not call with collect_hits without being extended */
586 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
587 if (match_one_pattern(p
, bol
, eol
, ctx
, &match
, 0))
593 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
594 enum grep_context ctx
,
595 regmatch_t
*pmatch
, int eflags
)
599 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
601 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
603 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
604 if (match
.rm_so
> pmatch
->rm_so
)
606 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
609 pmatch
->rm_so
= match
.rm_so
;
610 pmatch
->rm_eo
= match
.rm_eo
;
614 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
615 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
620 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
622 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
624 case GREP_PATTERN
: /* atom */
625 case GREP_PATTERN_HEAD
:
626 case GREP_PATTERN_BODY
:
627 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
638 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
639 const char *name
, unsigned lno
, char sign
)
641 int rest
= eol
- bol
;
642 char *line_color
= NULL
;
644 if (opt
->pre_context
|| opt
->post_context
) {
645 if (opt
->last_shown
== 0) {
646 if (opt
->show_hunk_mark
) {
647 output_color(opt
, "--", 2, opt
->color_sep
);
648 opt
->output(opt
, "\n", 1);
650 } else if (lno
> opt
->last_shown
+ 1) {
651 output_color(opt
, "--", 2, opt
->color_sep
);
652 opt
->output(opt
, "\n", 1);
655 opt
->last_shown
= lno
;
658 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
659 output_sep(opt
, sign
);
663 snprintf(buf
, sizeof(buf
), "%d", lno
);
664 output_color(opt
, buf
, strlen(buf
), opt
->color_lineno
);
665 output_sep(opt
, sign
);
669 enum grep_context ctx
= GREP_CONTEXT_BODY
;
674 line_color
= opt
->color_selected
;
675 else if (sign
== '-')
676 line_color
= opt
->color_context
;
677 else if (sign
== '=')
678 line_color
= opt
->color_function
;
680 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
681 if (match
.rm_so
== match
.rm_eo
)
684 output_color(opt
, bol
, match
.rm_so
, line_color
);
685 output_color(opt
, bol
+ match
.rm_so
,
686 match
.rm_eo
- match
.rm_so
,
694 output_color(opt
, bol
, rest
, line_color
);
695 opt
->output(opt
, "\n", 1);
698 static int match_funcname(struct grep_opt
*opt
, char *bol
, char *eol
)
700 xdemitconf_t
*xecfg
= opt
->priv
;
701 if (xecfg
&& xecfg
->find_func
) {
703 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
704 xecfg
->find_func_priv
) >= 0;
709 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
714 static void show_funcname_line(struct grep_opt
*opt
, const char *name
,
715 char *buf
, char *bol
, unsigned lno
)
720 while (bol
> buf
&& bol
[-1] != '\n')
724 if (lno
<= opt
->last_shown
)
727 if (match_funcname(opt
, bol
, eol
)) {
728 show_line(opt
, bol
, eol
, name
, lno
, '=');
734 static void show_pre_context(struct grep_opt
*opt
, const char *name
, char *buf
,
735 char *bol
, unsigned lno
)
737 unsigned cur
= lno
, from
= 1, funcname_lno
= 0;
738 int funcname_needed
= opt
->funcname
;
740 if (opt
->pre_context
< lno
)
741 from
= lno
- opt
->pre_context
;
742 if (from
<= opt
->last_shown
)
743 from
= opt
->last_shown
+ 1;
746 while (bol
> buf
&& cur
> from
) {
749 while (bol
> buf
&& bol
[-1] != '\n')
752 if (funcname_needed
&& match_funcname(opt
, bol
, eol
)) {
758 /* We need to look even further back to find a function signature. */
759 if (opt
->funcname
&& funcname_needed
)
760 show_funcname_line(opt
, name
, buf
, bol
, cur
);
764 char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
768 show_line(opt
, bol
, eol
, name
, cur
, sign
);
774 static int should_lookahead(struct grep_opt
*opt
)
779 return 0; /* punt for too complex stuff */
782 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
783 if (p
->token
!= GREP_PATTERN
)
784 return 0; /* punt for "header only" and stuff */
789 static int look_ahead(struct grep_opt
*opt
,
790 unsigned long *left_p
,
794 unsigned lno
= *lno_p
;
798 regoff_t earliest
= -1;
800 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
804 hit
= patmatch(p
, bol
, bol
+ *left_p
, &m
, 0);
805 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
807 if (earliest
< 0 || m
.rm_so
< earliest
)
812 *bol_p
= bol
+ *left_p
;
816 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
817 ; /* find the beginning of the line */
820 for (sp
= bol
; sp
< last_bol
; sp
++) {
824 *left_p
-= last_bol
- bol
;
830 int grep_threads_ok(const struct grep_opt
*opt
)
832 /* If this condition is true, then we may use the attribute
833 * machinery in grep_buffer_1. The attribute code is not
834 * thread safe, so we disable the use of threads.
836 if (opt
->funcname
&& !opt
->unmatch_name_only
&& !opt
->status_only
&&
843 static void std_output(struct grep_opt
*opt
, const void *buf
, size_t size
)
845 fwrite(buf
, size
, 1, stdout
);
848 static int grep_buffer_1(struct grep_opt
*opt
, const char *name
,
849 char *buf
, unsigned long size
, int collect_hits
)
852 unsigned long left
= size
;
854 unsigned last_hit
= 0;
855 int binary_match_only
= 0;
857 int try_lookahead
= 0;
858 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
862 opt
->output
= std_output
;
864 if (opt
->last_shown
&& (opt
->pre_context
|| opt
->post_context
) &&
865 opt
->output
== std_output
)
866 opt
->show_hunk_mark
= 1;
869 switch (opt
->binary
) {
870 case GREP_BINARY_DEFAULT
:
871 if (buffer_is_binary(buf
, size
))
872 binary_match_only
= 1;
874 case GREP_BINARY_NOMATCH
:
875 if (buffer_is_binary(buf
, size
))
876 return 0; /* Assume unmatch */
878 case GREP_BINARY_TEXT
:
881 die("bug: unknown binary handling mode");
884 memset(&xecfg
, 0, sizeof(xecfg
));
885 if (opt
->funcname
&& !opt
->unmatch_name_only
&& !opt
->status_only
&&
886 !opt
->name_only
&& !binary_match_only
&& !collect_hits
) {
887 struct userdiff_driver
*drv
= userdiff_find_by_path(name
);
888 if (drv
&& drv
->funcname
.pattern
) {
889 const struct userdiff_funcname
*pe
= &drv
->funcname
;
890 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
894 try_lookahead
= should_lookahead(opt
);
901 * look_ahead() skips quickly to the line that possibly
902 * has the next hit; don't call it if we need to do
903 * something more than just skipping the current line
904 * in response to an unmatch for the current line. E.g.
905 * inside a post-context window, we will show the current
906 * line as a context around the previous hit when it
911 && lno
<= last_hit
+ opt
->post_context
)
912 && look_ahead(opt
, &left
, &lno
, &bol
))
914 eol
= end_of_line(bol
, &left
);
918 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
919 ctx
= GREP_CONTEXT_BODY
;
921 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
927 /* "grep -v -e foo -e bla" should list lines
928 * that do not have either, so inversion should
933 if (opt
->unmatch_name_only
) {
940 if (opt
->status_only
)
942 if (opt
->name_only
) {
943 show_name(opt
, name
);
948 if (binary_match_only
) {
949 opt
->output(opt
, "Binary file ", 12);
950 output_color(opt
, name
, strlen(name
),
951 opt
->color_filename
);
952 opt
->output(opt
, " matches\n", 9);
955 /* Hit at this line. If we haven't shown the
956 * pre-context lines, we would need to show them.
958 if (opt
->pre_context
)
959 show_pre_context(opt
, name
, buf
, bol
, lno
);
960 else if (opt
->funcname
)
961 show_funcname_line(opt
, name
, buf
, bol
, lno
);
962 show_line(opt
, bol
, eol
, name
, lno
, ':');
966 lno
<= last_hit
+ opt
->post_context
) {
967 /* If the last hit is within the post context,
968 * we need to show this line.
970 show_line(opt
, bol
, eol
, name
, lno
, '-');
984 if (opt
->status_only
)
986 if (opt
->unmatch_name_only
) {
987 /* We did not see any hit, so we want to show this */
988 show_name(opt
, name
);
992 xdiff_clear_find_func(&xecfg
);
996 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
997 * which feels mostly useless but sometimes useful. Maybe
998 * make it another option? For now suppress them.
1000 if (opt
->count
&& count
) {
1002 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
1003 output_sep(opt
, ':');
1004 snprintf(buf
, sizeof(buf
), "%u\n", count
);
1005 opt
->output(opt
, buf
, strlen(buf
));
1011 static void clr_hit_marker(struct grep_expr
*x
)
1013 /* All-hit markers are meaningful only at the very top level
1018 if (x
->node
!= GREP_NODE_OR
)
1020 x
->u
.binary
.left
->hit
= 0;
1021 x
= x
->u
.binary
.right
;
1025 static int chk_hit_marker(struct grep_expr
*x
)
1027 /* Top level nodes have hit markers. See if they all are hits */
1029 if (x
->node
!= GREP_NODE_OR
)
1031 if (!x
->u
.binary
.left
->hit
)
1033 x
= x
->u
.binary
.right
;
1037 int grep_buffer(struct grep_opt
*opt
, const char *name
, char *buf
, unsigned long size
)
1040 * we do not have to do the two-pass grep when we do not check
1041 * buffer-wide "all-match".
1043 if (!opt
->all_match
)
1044 return grep_buffer_1(opt
, name
, buf
, size
, 0);
1046 /* Otherwise the toplevel "or" terms hit a bit differently.
1047 * We first clear hit markers from them.
1049 clr_hit_marker(opt
->pattern_expression
);
1050 grep_buffer_1(opt
, name
, buf
, size
, 1);
1052 if (!chk_hit_marker(opt
->pattern_expression
))
1055 return grep_buffer_1(opt
, name
, buf
, size
, 0);