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
)
82 int options
= PCRE_MULTILINE
;
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 int is_fixed(const char *s
, size_t len
)
144 /* regcomp cannot accept patterns with NULs so we
145 * consider any pattern containing a NUL fixed.
147 if (memchr(s
, 0, len
))
150 for (i
= 0; i
< len
; i
++) {
151 if (is_regex_special(s
[i
]))
158 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
162 p
->word_regexp
= opt
->word_regexp
;
163 p
->ignore_case
= opt
->ignore_case
;
165 if (opt
->fixed
|| is_fixed(p
->pattern
, p
->patternlen
))
171 if (opt
->regflags
& REG_ICASE
|| p
->ignore_case
)
172 p
->kws
= kwsalloc(tolower_trans_tbl
);
174 p
->kws
= kwsalloc(NULL
);
175 kwsincr(p
->kws
, p
->pattern
, p
->patternlen
);
181 compile_pcre_regexp(p
, opt
);
185 err
= regcomp(&p
->regexp
, p
->pattern
, opt
->regflags
);
188 regerror(err
, &p
->regexp
, errbuf
, 1024);
190 compile_regexp_failed(p
, errbuf
);
194 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
195 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
204 case GREP_PATTERN
: /* atom */
205 case GREP_PATTERN_HEAD
:
206 case GREP_PATTERN_BODY
:
207 x
= xcalloc(1, sizeof (struct grep_expr
));
208 x
->node
= GREP_NODE_ATOM
;
212 case GREP_OPEN_PAREN
:
214 x
= compile_pattern_or(list
);
215 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
216 die("unmatched parenthesis");
217 *list
= (*list
)->next
;
224 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
235 die("--not not followed by pattern expression");
237 x
= xcalloc(1, sizeof (struct grep_expr
));
238 x
->node
= GREP_NODE_NOT
;
239 x
->u
.unary
= compile_pattern_not(list
);
241 die("--not followed by non pattern expression");
244 return compile_pattern_atom(list
);
248 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
251 struct grep_expr
*x
, *y
, *z
;
253 x
= compile_pattern_not(list
);
255 if (p
&& p
->token
== GREP_AND
) {
257 die("--and not followed by pattern expression");
259 y
= compile_pattern_and(list
);
261 die("--and not followed by pattern expression");
262 z
= xcalloc(1, sizeof (struct grep_expr
));
263 z
->node
= GREP_NODE_AND
;
264 z
->u
.binary
.left
= x
;
265 z
->u
.binary
.right
= y
;
271 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
274 struct grep_expr
*x
, *y
, *z
;
276 x
= compile_pattern_and(list
);
278 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
279 y
= compile_pattern_or(list
);
281 die("not a pattern expression %s", p
->pattern
);
282 z
= xcalloc(1, sizeof (struct grep_expr
));
283 z
->node
= GREP_NODE_OR
;
284 z
->u
.binary
.left
= x
;
285 z
->u
.binary
.right
= y
;
291 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
293 return compile_pattern_or(list
);
296 static struct grep_expr
*grep_true_expr(void)
298 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
299 z
->node
= GREP_NODE_TRUE
;
303 static struct grep_expr
*grep_or_expr(struct grep_expr
*left
, struct grep_expr
*right
)
305 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
306 z
->node
= GREP_NODE_OR
;
307 z
->u
.binary
.left
= left
;
308 z
->u
.binary
.right
= right
;
312 static struct grep_expr
*prep_header_patterns(struct grep_opt
*opt
)
315 struct grep_expr
*header_expr
;
316 struct grep_expr
*(header_group
[GREP_HEADER_FIELD_MAX
]);
317 enum grep_header_field fld
;
319 if (!opt
->header_list
)
321 p
= opt
->header_list
;
322 for (p
= opt
->header_list
; p
; p
= p
->next
) {
323 if (p
->token
!= GREP_PATTERN_HEAD
)
324 die("bug: a non-header pattern in grep header list.");
325 if (p
->field
< 0 || GREP_HEADER_FIELD_MAX
<= p
->field
)
326 die("bug: unknown header field %d", p
->field
);
327 compile_regexp(p
, opt
);
330 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++)
331 header_group
[fld
] = NULL
;
333 for (p
= opt
->header_list
; p
; p
= p
->next
) {
335 struct grep_pat
*pp
= p
;
337 h
= compile_pattern_atom(&pp
);
338 if (!h
|| pp
!= p
->next
)
339 die("bug: malformed header expr");
340 if (!header_group
[p
->field
]) {
341 header_group
[p
->field
] = h
;
344 header_group
[p
->field
] = grep_or_expr(h
, header_group
[p
->field
]);
349 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++) {
350 if (!header_group
[fld
])
353 header_expr
= grep_true_expr();
354 header_expr
= grep_or_expr(header_group
[fld
], header_expr
);
359 void compile_grep_patterns(struct grep_opt
*opt
)
362 struct grep_expr
*header_expr
= prep_header_patterns(opt
);
364 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
366 case GREP_PATTERN
: /* atom */
367 case GREP_PATTERN_HEAD
:
368 case GREP_PATTERN_BODY
:
369 compile_regexp(p
, opt
);
377 if (opt
->all_match
|| header_expr
)
379 else if (!opt
->extended
)
382 p
= opt
->pattern_list
;
384 opt
->pattern_expression
= compile_pattern_expr(&p
);
386 die("incomplete pattern expression: %s", p
->pattern
);
391 if (!opt
->pattern_expression
)
392 opt
->pattern_expression
= header_expr
;
394 opt
->pattern_expression
= grep_or_expr(opt
->pattern_expression
,
399 static void free_pattern_expr(struct grep_expr
*x
)
406 free_pattern_expr(x
->u
.unary
);
410 free_pattern_expr(x
->u
.binary
.left
);
411 free_pattern_expr(x
->u
.binary
.right
);
417 void free_grep_patterns(struct grep_opt
*opt
)
419 struct grep_pat
*p
, *n
;
421 for (p
= opt
->pattern_list
; p
; p
= n
) {
424 case GREP_PATTERN
: /* atom */
425 case GREP_PATTERN_HEAD
:
426 case GREP_PATTERN_BODY
:
429 else if (p
->pcre_regexp
)
442 free_pattern_expr(opt
->pattern_expression
);
445 static char *end_of_line(char *cp
, unsigned long *left
)
447 unsigned long l
= *left
;
448 while (l
&& *cp
!= '\n') {
456 static int word_char(char ch
)
458 return isalnum(ch
) || ch
== '_';
461 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
464 if (want_color(opt
->color
) && color
&& color
[0]) {
465 opt
->output(opt
, color
, strlen(color
));
466 opt
->output(opt
, data
, size
);
467 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
469 opt
->output(opt
, data
, size
);
472 static void output_sep(struct grep_opt
*opt
, char sign
)
474 if (opt
->null_following_name
)
475 opt
->output(opt
, "\0", 1);
477 output_color(opt
, &sign
, 1, opt
->color_sep
);
480 static void show_name(struct grep_opt
*opt
, const char *name
)
482 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
483 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
486 static int fixmatch(struct grep_pat
*p
, char *line
, char *eol
,
489 struct kwsmatch kwsm
;
490 size_t offset
= kwsexec(p
->kws
, line
, eol
- line
, &kwsm
);
492 match
->rm_so
= match
->rm_eo
= -1;
495 match
->rm_so
= offset
;
496 match
->rm_eo
= match
->rm_so
+ kwsm
.size
[0];
501 static int regmatch(const regex_t
*preg
, char *line
, char *eol
,
502 regmatch_t
*match
, int eflags
)
506 match
->rm_eo
= eol
- line
;
507 eflags
|= REG_STARTEND
;
509 return regexec(preg
, line
, 1, match
, eflags
);
512 static int patmatch(struct grep_pat
*p
, char *line
, char *eol
,
513 regmatch_t
*match
, int eflags
)
518 hit
= !fixmatch(p
, line
, eol
, match
);
519 else if (p
->pcre_regexp
)
520 hit
= !pcrematch(p
, line
, eol
, match
, eflags
);
522 hit
= !regmatch(&p
->regexp
, line
, eol
, match
, eflags
);
527 static int strip_timestamp(char *bol
, char **eol_p
)
532 while (bol
< --eol
) {
548 { "committer ", 10 },
551 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
552 enum grep_context ctx
,
553 regmatch_t
*pmatch
, int eflags
)
557 const char *start
= bol
;
559 if ((p
->token
!= GREP_PATTERN
) &&
560 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
563 if (p
->token
== GREP_PATTERN_HEAD
) {
566 assert(p
->field
< ARRAY_SIZE(header_field
));
567 field
= header_field
[p
->field
].field
;
568 len
= header_field
[p
->field
].len
;
569 if (strncmp(bol
, field
, len
))
572 saved_ch
= strip_timestamp(bol
, &eol
);
576 hit
= patmatch(p
, bol
, eol
, pmatch
, eflags
);
578 if (hit
&& p
->word_regexp
) {
579 if ((pmatch
[0].rm_so
< 0) ||
580 (eol
- bol
) < pmatch
[0].rm_so
||
581 (pmatch
[0].rm_eo
< 0) ||
582 (eol
- bol
) < pmatch
[0].rm_eo
)
583 die("regexp returned nonsense");
585 /* Match beginning must be either beginning of the
586 * line, or at word boundary (i.e. the last char must
587 * not be a word char). Similarly, match end must be
588 * either end of the line, or at word boundary
589 * (i.e. the next char must not be a word char).
591 if ( ((pmatch
[0].rm_so
== 0) ||
592 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
593 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
594 !word_char(bol
[pmatch
[0].rm_eo
])) )
599 /* Words consist of at least one character. */
600 if (pmatch
->rm_so
== pmatch
->rm_eo
)
603 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
604 /* There could be more than one match on the
605 * line, and the first match might not be
606 * strict word match. But later ones could be!
607 * Forward to the next possible start, i.e. the
608 * next position following a non-word char.
610 bol
= pmatch
[0].rm_so
+ bol
+ 1;
611 while (word_char(bol
[-1]) && bol
< eol
)
613 eflags
|= REG_NOTBOL
;
618 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
621 pmatch
[0].rm_so
+= bol
- start
;
622 pmatch
[0].rm_eo
+= bol
- start
;
627 static int match_expr_eval(struct grep_expr
*x
, char *bol
, char *eol
,
628 enum grep_context ctx
, int collect_hits
)
634 die("Not a valid grep expression");
640 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
, &match
, 0);
643 h
= !match_expr_eval(x
->u
.unary
, bol
, eol
, ctx
, 0);
646 if (!match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0))
648 h
= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
652 return (match_expr_eval(x
->u
.binary
.left
,
654 match_expr_eval(x
->u
.binary
.right
,
656 h
= match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
657 x
->u
.binary
.left
->hit
|= h
;
658 h
|= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
661 die("Unexpected node type (internal error) %d", x
->node
);
668 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
669 enum grep_context ctx
, int collect_hits
)
671 struct grep_expr
*x
= opt
->pattern_expression
;
672 return match_expr_eval(x
, bol
, eol
, ctx
, collect_hits
);
675 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
676 enum grep_context ctx
, int collect_hits
)
682 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
684 /* we do not call with collect_hits without being extended */
685 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
686 if (match_one_pattern(p
, bol
, eol
, ctx
, &match
, 0))
692 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
693 enum grep_context ctx
,
694 regmatch_t
*pmatch
, int eflags
)
698 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
700 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
702 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
703 if (match
.rm_so
> pmatch
->rm_so
)
705 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
708 pmatch
->rm_so
= match
.rm_so
;
709 pmatch
->rm_eo
= match
.rm_eo
;
713 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
714 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
719 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
721 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
723 case GREP_PATTERN
: /* atom */
724 case GREP_PATTERN_HEAD
:
725 case GREP_PATTERN_BODY
:
726 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
737 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
738 const char *name
, unsigned lno
, char sign
)
740 int rest
= eol
- bol
;
741 char *line_color
= NULL
;
743 if (opt
->file_break
&& opt
->last_shown
== 0) {
744 if (opt
->show_hunk_mark
)
745 opt
->output(opt
, "\n", 1);
746 } else if (opt
->pre_context
|| opt
->post_context
|| opt
->funcbody
) {
747 if (opt
->last_shown
== 0) {
748 if (opt
->show_hunk_mark
) {
749 output_color(opt
, "--", 2, opt
->color_sep
);
750 opt
->output(opt
, "\n", 1);
752 } else if (lno
> opt
->last_shown
+ 1) {
753 output_color(opt
, "--", 2, opt
->color_sep
);
754 opt
->output(opt
, "\n", 1);
757 if (opt
->heading
&& opt
->last_shown
== 0) {
758 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
759 opt
->output(opt
, "\n", 1);
761 opt
->last_shown
= lno
;
763 if (!opt
->heading
&& opt
->pathname
) {
764 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
765 output_sep(opt
, sign
);
769 snprintf(buf
, sizeof(buf
), "%d", lno
);
770 output_color(opt
, buf
, strlen(buf
), opt
->color_lineno
);
771 output_sep(opt
, sign
);
775 enum grep_context ctx
= GREP_CONTEXT_BODY
;
780 line_color
= opt
->color_selected
;
781 else if (sign
== '-')
782 line_color
= opt
->color_context
;
783 else if (sign
== '=')
784 line_color
= opt
->color_function
;
786 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
787 if (match
.rm_so
== match
.rm_eo
)
790 output_color(opt
, bol
, match
.rm_so
, line_color
);
791 output_color(opt
, bol
+ match
.rm_so
,
792 match
.rm_eo
- match
.rm_so
,
800 output_color(opt
, bol
, rest
, line_color
);
801 opt
->output(opt
, "\n", 1);
808 * This lock protects access to the gitattributes machinery, which is
811 pthread_mutex_t grep_attr_mutex
;
813 static inline void grep_attr_lock(void)
816 pthread_mutex_lock(&grep_attr_mutex
);
819 static inline void grep_attr_unlock(void)
822 pthread_mutex_unlock(&grep_attr_mutex
);
826 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
828 pthread_mutex_t grep_read_mutex
;
831 #define grep_attr_lock()
832 #define grep_attr_unlock()
835 static int match_funcname(struct grep_opt
*opt
, struct grep_source
*gs
, char *bol
, char *eol
)
837 xdemitconf_t
*xecfg
= opt
->priv
;
838 if (xecfg
&& !xecfg
->find_func
) {
839 grep_source_load_driver(gs
);
840 if (gs
->driver
->funcname
.pattern
) {
841 const struct userdiff_funcname
*pe
= &gs
->driver
->funcname
;
842 xdiff_set_find_func(xecfg
, pe
->pattern
, pe
->cflags
);
844 xecfg
= opt
->priv
= NULL
;
850 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
851 xecfg
->find_func_priv
) >= 0;
856 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
861 static void show_funcname_line(struct grep_opt
*opt
, struct grep_source
*gs
,
862 char *bol
, unsigned lno
)
864 while (bol
> gs
->buf
) {
867 while (bol
> gs
->buf
&& bol
[-1] != '\n')
871 if (lno
<= opt
->last_shown
)
874 if (match_funcname(opt
, gs
, bol
, eol
)) {
875 show_line(opt
, bol
, eol
, gs
->name
, lno
, '=');
881 static void show_pre_context(struct grep_opt
*opt
, struct grep_source
*gs
,
882 char *bol
, char *end
, unsigned lno
)
884 unsigned cur
= lno
, from
= 1, funcname_lno
= 0;
885 int funcname_needed
= !!opt
->funcname
;
887 if (opt
->funcbody
&& !match_funcname(opt
, gs
, bol
, end
))
890 if (opt
->pre_context
< lno
)
891 from
= lno
- opt
->pre_context
;
892 if (from
<= opt
->last_shown
)
893 from
= opt
->last_shown
+ 1;
896 while (bol
> gs
->buf
&&
897 cur
> (funcname_needed
== 2 ? opt
->last_shown
+ 1 : from
)) {
900 while (bol
> gs
->buf
&& bol
[-1] != '\n')
903 if (funcname_needed
&& match_funcname(opt
, gs
, bol
, eol
)) {
909 /* We need to look even further back to find a function signature. */
910 if (opt
->funcname
&& funcname_needed
)
911 show_funcname_line(opt
, gs
, bol
, cur
);
915 char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
919 show_line(opt
, bol
, eol
, gs
->name
, cur
, sign
);
925 static int should_lookahead(struct grep_opt
*opt
)
930 return 0; /* punt for too complex stuff */
933 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
934 if (p
->token
!= GREP_PATTERN
)
935 return 0; /* punt for "header only" and stuff */
940 static int look_ahead(struct grep_opt
*opt
,
941 unsigned long *left_p
,
945 unsigned lno
= *lno_p
;
949 regoff_t earliest
= -1;
951 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
955 hit
= patmatch(p
, bol
, bol
+ *left_p
, &m
, 0);
956 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
958 if (earliest
< 0 || m
.rm_so
< earliest
)
963 *bol_p
= bol
+ *left_p
;
967 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
968 ; /* find the beginning of the line */
971 for (sp
= bol
; sp
< last_bol
; sp
++) {
975 *left_p
-= last_bol
- bol
;
981 static void std_output(struct grep_opt
*opt
, const void *buf
, size_t size
)
983 fwrite(buf
, size
, 1, stdout
);
986 static int grep_source_1(struct grep_opt
*opt
, struct grep_source
*gs
, int collect_hits
)
991 unsigned last_hit
= 0;
992 int binary_match_only
= 0;
994 int try_lookahead
= 0;
995 int show_function
= 0;
996 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
1000 opt
->output
= std_output
;
1002 if (opt
->pre_context
|| opt
->post_context
|| opt
->file_break
||
1004 /* Show hunk marks, except for the first file. */
1005 if (opt
->last_shown
)
1006 opt
->show_hunk_mark
= 1;
1008 * If we're using threads then we can't easily identify
1009 * the first file. Always put hunk marks in that case
1010 * and skip the very first one later in work_done().
1012 if (opt
->output
!= std_output
)
1013 opt
->show_hunk_mark
= 1;
1015 opt
->last_shown
= 0;
1017 switch (opt
->binary
) {
1018 case GREP_BINARY_DEFAULT
:
1019 if (grep_source_is_binary(gs
))
1020 binary_match_only
= 1;
1022 case GREP_BINARY_NOMATCH
:
1023 if (grep_source_is_binary(gs
))
1024 return 0; /* Assume unmatch */
1026 case GREP_BINARY_TEXT
:
1029 die("bug: unknown binary handling mode");
1032 memset(&xecfg
, 0, sizeof(xecfg
));
1035 try_lookahead
= should_lookahead(opt
);
1037 if (grep_source_load(gs
) < 0)
1047 * look_ahead() skips quickly to the line that possibly
1048 * has the next hit; don't call it if we need to do
1049 * something more than just skipping the current line
1050 * in response to an unmatch for the current line. E.g.
1051 * inside a post-context window, we will show the current
1052 * line as a context around the previous hit when it
1057 && (show_function
||
1058 lno
<= last_hit
+ opt
->post_context
))
1059 && look_ahead(opt
, &left
, &lno
, &bol
))
1061 eol
= end_of_line(bol
, &left
);
1065 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
1066 ctx
= GREP_CONTEXT_BODY
;
1068 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
1074 /* "grep -v -e foo -e bla" should list lines
1075 * that do not have either, so inversion should
1080 if (opt
->unmatch_name_only
) {
1087 if (opt
->status_only
)
1089 if (opt
->name_only
) {
1090 show_name(opt
, gs
->name
);
1095 if (binary_match_only
) {
1096 opt
->output(opt
, "Binary file ", 12);
1097 output_color(opt
, gs
->name
, strlen(gs
->name
),
1098 opt
->color_filename
);
1099 opt
->output(opt
, " matches\n", 9);
1102 /* Hit at this line. If we haven't shown the
1103 * pre-context lines, we would need to show them.
1105 if (opt
->pre_context
|| opt
->funcbody
)
1106 show_pre_context(opt
, gs
, bol
, eol
, lno
);
1107 else if (opt
->funcname
)
1108 show_funcname_line(opt
, gs
, bol
, lno
);
1109 show_line(opt
, bol
, eol
, gs
->name
, lno
, ':');
1115 if (show_function
&& match_funcname(opt
, gs
, bol
, eol
))
1117 if (show_function
||
1118 (last_hit
&& lno
<= last_hit
+ opt
->post_context
)) {
1119 /* If the last hit is within the post context,
1120 * we need to show this line.
1122 show_line(opt
, bol
, eol
, gs
->name
, lno
, '-');
1136 if (opt
->status_only
)
1138 if (opt
->unmatch_name_only
) {
1139 /* We did not see any hit, so we want to show this */
1140 show_name(opt
, gs
->name
);
1144 xdiff_clear_find_func(&xecfg
);
1148 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1149 * which feels mostly useless but sometimes useful. Maybe
1150 * make it another option? For now suppress them.
1152 if (opt
->count
&& count
) {
1154 output_color(opt
, gs
->name
, strlen(gs
->name
), opt
->color_filename
);
1155 output_sep(opt
, ':');
1156 snprintf(buf
, sizeof(buf
), "%u\n", count
);
1157 opt
->output(opt
, buf
, strlen(buf
));
1163 static void clr_hit_marker(struct grep_expr
*x
)
1165 /* All-hit markers are meaningful only at the very top level
1170 if (x
->node
!= GREP_NODE_OR
)
1172 x
->u
.binary
.left
->hit
= 0;
1173 x
= x
->u
.binary
.right
;
1177 static int chk_hit_marker(struct grep_expr
*x
)
1179 /* Top level nodes have hit markers. See if they all are hits */
1181 if (x
->node
!= GREP_NODE_OR
)
1183 if (!x
->u
.binary
.left
->hit
)
1185 x
= x
->u
.binary
.right
;
1189 int grep_source(struct grep_opt
*opt
, struct grep_source
*gs
)
1192 * we do not have to do the two-pass grep when we do not check
1193 * buffer-wide "all-match".
1195 if (!opt
->all_match
)
1196 return grep_source_1(opt
, gs
, 0);
1198 /* Otherwise the toplevel "or" terms hit a bit differently.
1199 * We first clear hit markers from them.
1201 clr_hit_marker(opt
->pattern_expression
);
1202 grep_source_1(opt
, gs
, 1);
1204 if (!chk_hit_marker(opt
->pattern_expression
))
1207 return grep_source_1(opt
, gs
, 0);
1210 int grep_buffer(struct grep_opt
*opt
, char *buf
, unsigned long size
)
1212 struct grep_source gs
;
1215 grep_source_init(&gs
, GREP_SOURCE_BUF
, NULL
, NULL
);
1219 r
= grep_source(opt
, &gs
);
1221 grep_source_clear(&gs
);
1225 void grep_source_init(struct grep_source
*gs
, enum grep_source_type type
,
1226 const char *name
, const void *identifier
)
1229 gs
->name
= name
? xstrdup(name
) : NULL
;
1235 case GREP_SOURCE_FILE
:
1236 gs
->identifier
= xstrdup(identifier
);
1238 case GREP_SOURCE_SHA1
:
1239 gs
->identifier
= xmalloc(20);
1240 memcpy(gs
->identifier
, identifier
, 20);
1242 case GREP_SOURCE_BUF
:
1243 gs
->identifier
= NULL
;
1247 void grep_source_clear(struct grep_source
*gs
)
1251 free(gs
->identifier
);
1252 gs
->identifier
= NULL
;
1253 grep_source_clear_data(gs
);
1256 void grep_source_clear_data(struct grep_source
*gs
)
1259 case GREP_SOURCE_FILE
:
1260 case GREP_SOURCE_SHA1
:
1265 case GREP_SOURCE_BUF
:
1266 /* leave user-provided buf intact */
1271 static int grep_source_load_sha1(struct grep_source
*gs
)
1273 enum object_type type
;
1276 gs
->buf
= read_sha1_file(gs
->identifier
, &type
, &gs
->size
);
1280 return error(_("'%s': unable to read %s"),
1282 sha1_to_hex(gs
->identifier
));
1286 static int grep_source_load_file(struct grep_source
*gs
)
1288 const char *filename
= gs
->identifier
;
1294 if (lstat(filename
, &st
) < 0) {
1296 if (errno
!= ENOENT
)
1297 error(_("'%s': %s"), filename
, strerror(errno
));
1300 if (!S_ISREG(st
.st_mode
))
1302 size
= xsize_t(st
.st_size
);
1303 i
= open(filename
, O_RDONLY
);
1306 data
= xmalloc(size
+ 1);
1307 if (st
.st_size
!= read_in_full(i
, data
, size
)) {
1308 error(_("'%s': short read %s"), filename
, strerror(errno
));
1321 int grep_source_load(struct grep_source
*gs
)
1327 case GREP_SOURCE_FILE
:
1328 return grep_source_load_file(gs
);
1329 case GREP_SOURCE_SHA1
:
1330 return grep_source_load_sha1(gs
);
1331 case GREP_SOURCE_BUF
:
1332 return gs
->buf
? 0 : -1;
1334 die("BUG: invalid grep_source type");
1337 void grep_source_load_driver(struct grep_source
*gs
)
1343 gs
->driver
= userdiff_find_by_path(gs
->name
);
1345 gs
->driver
= userdiff_find_by_name("default");
1349 int grep_source_is_binary(struct grep_source
*gs
)
1351 grep_source_load_driver(gs
);
1352 if (gs
->driver
->binary
!= -1)
1353 return gs
->driver
->binary
;
1355 if (!grep_source_load(gs
))
1356 return buffer_is_binary(gs
->buf
, gs
->size
);