4 #include "xdiff-interface.h"
6 static int grep_source_load(struct grep_source
*gs
);
7 static int grep_source_is_binary(struct grep_source
*gs
);
10 static struct grep_pat
*create_grep_pat(const char *pat
, size_t patlen
,
11 const char *origin
, int no
,
12 enum grep_pat_token t
,
13 enum grep_header_field field
)
15 struct grep_pat
*p
= xcalloc(1, sizeof(*p
));
16 p
->pattern
= xmemdupz(pat
, patlen
);
17 p
->patternlen
= patlen
;
25 static void do_append_grep_pat(struct grep_pat
***tail
, struct grep_pat
*p
)
32 case GREP_PATTERN
: /* atom */
33 case GREP_PATTERN_HEAD
:
34 case GREP_PATTERN_BODY
:
36 struct grep_pat
*new_pat
;
38 char *cp
= p
->pattern
+ p
->patternlen
, *nl
= NULL
;
39 while (++len
<= p
->patternlen
) {
40 if (*(--cp
) == '\n') {
47 new_pat
= create_grep_pat(nl
+ 1, len
- 1, p
->origin
,
48 p
->no
, p
->token
, p
->field
);
49 new_pat
->next
= p
->next
;
51 *tail
= &new_pat
->next
;
62 void append_header_grep_pattern(struct grep_opt
*opt
,
63 enum grep_header_field field
, const char *pat
)
65 struct grep_pat
*p
= create_grep_pat(pat
, strlen(pat
), "header", 0,
66 GREP_PATTERN_HEAD
, field
);
67 do_append_grep_pat(&opt
->header_tail
, p
);
70 void append_grep_pattern(struct grep_opt
*opt
, const char *pat
,
71 const char *origin
, int no
, enum grep_pat_token t
)
73 append_grep_pat(opt
, pat
, strlen(pat
), origin
, no
, t
);
76 void append_grep_pat(struct grep_opt
*opt
, const char *pat
, size_t patlen
,
77 const char *origin
, int no
, enum grep_pat_token t
)
79 struct grep_pat
*p
= create_grep_pat(pat
, patlen
, origin
, no
, t
, 0);
80 do_append_grep_pat(&opt
->pattern_tail
, p
);
83 struct grep_opt
*grep_opt_dup(const struct grep_opt
*opt
)
86 struct grep_opt
*ret
= xmalloc(sizeof(struct grep_opt
));
89 ret
->pattern_list
= NULL
;
90 ret
->pattern_tail
= &ret
->pattern_list
;
92 for(pat
= opt
->pattern_list
; pat
!= NULL
; pat
= pat
->next
)
94 if(pat
->token
== GREP_PATTERN_HEAD
)
95 append_header_grep_pattern(ret
, pat
->field
,
98 append_grep_pat(ret
, pat
->pattern
, pat
->patternlen
,
99 pat
->origin
, pat
->no
, pat
->token
);
105 static NORETURN
void compile_regexp_failed(const struct grep_pat
*p
,
111 sprintf(where
, "In '%s' at %d, ", p
->origin
, p
->no
);
113 sprintf(where
, "%s, ", p
->origin
);
117 die("%s'%s': %s", where
, p
->pattern
, error
);
121 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
125 int options
= PCRE_MULTILINE
;
127 if (opt
->ignore_case
)
128 options
|= PCRE_CASELESS
;
130 p
->pcre_regexp
= pcre_compile(p
->pattern
, options
, &error
, &erroffset
,
133 compile_regexp_failed(p
, error
);
135 p
->pcre_extra_info
= pcre_study(p
->pcre_regexp
, 0, &error
);
136 if (!p
->pcre_extra_info
&& error
)
140 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
141 regmatch_t
*match
, int eflags
)
143 int ovector
[30], ret
, flags
= 0;
145 if (eflags
& REG_NOTBOL
)
146 flags
|= PCRE_NOTBOL
;
148 ret
= pcre_exec(p
->pcre_regexp
, p
->pcre_extra_info
, line
, eol
- line
,
149 0, flags
, ovector
, ARRAY_SIZE(ovector
));
150 if (ret
< 0 && ret
!= PCRE_ERROR_NOMATCH
)
151 die("pcre_exec failed with error code %d", ret
);
154 match
->rm_so
= ovector
[0];
155 match
->rm_eo
= ovector
[1];
161 static void free_pcre_regexp(struct grep_pat
*p
)
163 pcre_free(p
->pcre_regexp
);
164 pcre_free(p
->pcre_extra_info
);
166 #else /* !USE_LIBPCRE */
167 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
169 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
172 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
173 regmatch_t
*match
, int eflags
)
178 static void free_pcre_regexp(struct grep_pat
*p
)
181 #endif /* !USE_LIBPCRE */
183 static int is_fixed(const char *s
, size_t len
)
187 /* regcomp cannot accept patterns with NULs so we
188 * consider any pattern containing a NUL fixed.
190 if (memchr(s
, 0, len
))
193 for (i
= 0; i
< len
; i
++) {
194 if (is_regex_special(s
[i
]))
201 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
205 p
->word_regexp
= opt
->word_regexp
;
206 p
->ignore_case
= opt
->ignore_case
;
208 if (opt
->fixed
|| is_fixed(p
->pattern
, p
->patternlen
))
214 if (opt
->regflags
& REG_ICASE
|| p
->ignore_case
)
215 p
->kws
= kwsalloc(tolower_trans_tbl
);
217 p
->kws
= kwsalloc(NULL
);
218 kwsincr(p
->kws
, p
->pattern
, p
->patternlen
);
224 compile_pcre_regexp(p
, opt
);
228 err
= regcomp(&p
->regexp
, p
->pattern
, opt
->regflags
);
231 regerror(err
, &p
->regexp
, errbuf
, 1024);
233 compile_regexp_failed(p
, errbuf
);
237 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
238 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
247 case GREP_PATTERN
: /* atom */
248 case GREP_PATTERN_HEAD
:
249 case GREP_PATTERN_BODY
:
250 x
= xcalloc(1, sizeof (struct grep_expr
));
251 x
->node
= GREP_NODE_ATOM
;
255 case GREP_OPEN_PAREN
:
257 x
= compile_pattern_or(list
);
258 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
259 die("unmatched parenthesis");
260 *list
= (*list
)->next
;
267 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
278 die("--not not followed by pattern expression");
280 x
= xcalloc(1, sizeof (struct grep_expr
));
281 x
->node
= GREP_NODE_NOT
;
282 x
->u
.unary
= compile_pattern_not(list
);
284 die("--not followed by non pattern expression");
287 return compile_pattern_atom(list
);
291 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
294 struct grep_expr
*x
, *y
, *z
;
296 x
= compile_pattern_not(list
);
298 if (p
&& p
->token
== GREP_AND
) {
300 die("--and not followed by pattern expression");
302 y
= compile_pattern_and(list
);
304 die("--and not followed by pattern expression");
305 z
= xcalloc(1, sizeof (struct grep_expr
));
306 z
->node
= GREP_NODE_AND
;
307 z
->u
.binary
.left
= x
;
308 z
->u
.binary
.right
= y
;
314 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
317 struct grep_expr
*x
, *y
, *z
;
319 x
= compile_pattern_and(list
);
321 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
322 y
= compile_pattern_or(list
);
324 die("not a pattern expression %s", p
->pattern
);
325 z
= xcalloc(1, sizeof (struct grep_expr
));
326 z
->node
= GREP_NODE_OR
;
327 z
->u
.binary
.left
= x
;
328 z
->u
.binary
.right
= y
;
334 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
336 return compile_pattern_or(list
);
339 static void indent(int in
)
345 static void dump_grep_pat(struct grep_pat
*p
)
348 case GREP_AND
: fprintf(stderr
, "*and*"); break;
349 case GREP_OPEN_PAREN
: fprintf(stderr
, "*(*"); break;
350 case GREP_CLOSE_PAREN
: fprintf(stderr
, "*)*"); break;
351 case GREP_NOT
: fprintf(stderr
, "*not*"); break;
352 case GREP_OR
: fprintf(stderr
, "*or*"); break;
354 case GREP_PATTERN
: fprintf(stderr
, "pattern"); break;
355 case GREP_PATTERN_HEAD
: fprintf(stderr
, "pattern_head"); break;
356 case GREP_PATTERN_BODY
: fprintf(stderr
, "pattern_body"); break;
361 case GREP_PATTERN_HEAD
:
362 fprintf(stderr
, "<head %d>", p
->field
); break;
363 case GREP_PATTERN_BODY
:
364 fprintf(stderr
, "<body>"); break;
368 case GREP_PATTERN_HEAD
:
369 case GREP_PATTERN_BODY
:
371 fprintf(stderr
, "%.*s", (int)p
->patternlen
, p
->pattern
);
377 static void dump_grep_expression_1(struct grep_expr
*x
, int in
)
382 fprintf(stderr
, "true\n");
385 dump_grep_pat(x
->u
.atom
);
388 fprintf(stderr
, "(not\n");
389 dump_grep_expression_1(x
->u
.unary
, in
+1);
391 fprintf(stderr
, ")\n");
394 fprintf(stderr
, "(and\n");
395 dump_grep_expression_1(x
->u
.binary
.left
, in
+1);
396 dump_grep_expression_1(x
->u
.binary
.right
, in
+1);
398 fprintf(stderr
, ")\n");
401 fprintf(stderr
, "(or\n");
402 dump_grep_expression_1(x
->u
.binary
.left
, in
+1);
403 dump_grep_expression_1(x
->u
.binary
.right
, in
+1);
405 fprintf(stderr
, ")\n");
410 static void dump_grep_expression(struct grep_opt
*opt
)
412 struct grep_expr
*x
= opt
->pattern_expression
;
415 fprintf(stderr
, "[all-match]\n");
416 dump_grep_expression_1(x
, 0);
420 static struct grep_expr
*grep_true_expr(void)
422 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
423 z
->node
= GREP_NODE_TRUE
;
427 static struct grep_expr
*grep_or_expr(struct grep_expr
*left
, struct grep_expr
*right
)
429 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
430 z
->node
= GREP_NODE_OR
;
431 z
->u
.binary
.left
= left
;
432 z
->u
.binary
.right
= right
;
436 static struct grep_expr
*prep_header_patterns(struct grep_opt
*opt
)
439 struct grep_expr
*header_expr
;
440 struct grep_expr
*(header_group
[GREP_HEADER_FIELD_MAX
]);
441 enum grep_header_field fld
;
443 if (!opt
->header_list
)
446 for (p
= opt
->header_list
; p
; p
= p
->next
) {
447 if (p
->token
!= GREP_PATTERN_HEAD
)
448 die("bug: a non-header pattern in grep header list.");
449 if (p
->field
< 0 || GREP_HEADER_FIELD_MAX
<= p
->field
)
450 die("bug: unknown header field %d", p
->field
);
451 compile_regexp(p
, opt
);
454 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++)
455 header_group
[fld
] = NULL
;
457 for (p
= opt
->header_list
; p
; p
= p
->next
) {
459 struct grep_pat
*pp
= p
;
461 h
= compile_pattern_atom(&pp
);
462 if (!h
|| pp
!= p
->next
)
463 die("bug: malformed header expr");
464 if (!header_group
[p
->field
]) {
465 header_group
[p
->field
] = h
;
468 header_group
[p
->field
] = grep_or_expr(h
, header_group
[p
->field
]);
473 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++) {
474 if (!header_group
[fld
])
477 header_expr
= grep_true_expr();
478 header_expr
= grep_or_expr(header_group
[fld
], header_expr
);
483 static struct grep_expr
*grep_splice_or(struct grep_expr
*x
, struct grep_expr
*y
)
485 struct grep_expr
*z
= x
;
488 assert(x
->node
== GREP_NODE_OR
);
489 if (x
->u
.binary
.right
&&
490 x
->u
.binary
.right
->node
== GREP_NODE_TRUE
) {
491 x
->u
.binary
.right
= y
;
494 x
= x
->u
.binary
.right
;
499 static void compile_grep_patterns_real(struct grep_opt
*opt
)
502 struct grep_expr
*header_expr
= prep_header_patterns(opt
);
504 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
506 case GREP_PATTERN
: /* atom */
507 case GREP_PATTERN_HEAD
:
508 case GREP_PATTERN_BODY
:
509 compile_regexp(p
, opt
);
517 if (opt
->all_match
|| header_expr
)
519 else if (!opt
->extended
&& !opt
->debug
)
522 p
= opt
->pattern_list
;
524 opt
->pattern_expression
= compile_pattern_expr(&p
);
526 die("incomplete pattern expression: %s", p
->pattern
);
531 if (!opt
->pattern_expression
)
532 opt
->pattern_expression
= header_expr
;
533 else if (opt
->all_match
)
534 opt
->pattern_expression
= grep_splice_or(header_expr
,
535 opt
->pattern_expression
);
537 opt
->pattern_expression
= grep_or_expr(opt
->pattern_expression
,
542 void compile_grep_patterns(struct grep_opt
*opt
)
544 compile_grep_patterns_real(opt
);
546 dump_grep_expression(opt
);
549 static void free_pattern_expr(struct grep_expr
*x
)
556 free_pattern_expr(x
->u
.unary
);
560 free_pattern_expr(x
->u
.binary
.left
);
561 free_pattern_expr(x
->u
.binary
.right
);
567 void free_grep_patterns(struct grep_opt
*opt
)
569 struct grep_pat
*p
, *n
;
571 for (p
= opt
->pattern_list
; p
; p
= n
) {
574 case GREP_PATTERN
: /* atom */
575 case GREP_PATTERN_HEAD
:
576 case GREP_PATTERN_BODY
:
579 else if (p
->pcre_regexp
)
593 free_pattern_expr(opt
->pattern_expression
);
596 static char *end_of_line(char *cp
, unsigned long *left
)
598 unsigned long l
= *left
;
599 while (l
&& *cp
!= '\n') {
607 static int word_char(char ch
)
609 return isalnum(ch
) || ch
== '_';
612 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
615 if (want_color(opt
->color
) && color
&& color
[0]) {
616 opt
->output(opt
, color
, strlen(color
));
617 opt
->output(opt
, data
, size
);
618 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
620 opt
->output(opt
, data
, size
);
623 static void output_sep(struct grep_opt
*opt
, char sign
)
625 if (opt
->null_following_name
)
626 opt
->output(opt
, "\0", 1);
628 output_color(opt
, &sign
, 1, opt
->color_sep
);
631 static void show_name(struct grep_opt
*opt
, const char *name
)
633 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
634 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
637 static int fixmatch(struct grep_pat
*p
, char *line
, char *eol
,
640 struct kwsmatch kwsm
;
641 size_t offset
= kwsexec(p
->kws
, line
, eol
- line
, &kwsm
);
643 match
->rm_so
= match
->rm_eo
= -1;
646 match
->rm_so
= offset
;
647 match
->rm_eo
= match
->rm_so
+ kwsm
.size
[0];
652 static int regmatch(const regex_t
*preg
, char *line
, char *eol
,
653 regmatch_t
*match
, int eflags
)
657 match
->rm_eo
= eol
- line
;
658 eflags
|= REG_STARTEND
;
660 return regexec(preg
, line
, 1, match
, eflags
);
663 static int patmatch(struct grep_pat
*p
, char *line
, char *eol
,
664 regmatch_t
*match
, int eflags
)
669 hit
= !fixmatch(p
, line
, eol
, match
);
670 else if (p
->pcre_regexp
)
671 hit
= !pcrematch(p
, line
, eol
, match
, eflags
);
673 hit
= !regmatch(&p
->regexp
, line
, eol
, match
, eflags
);
678 static int strip_timestamp(char *bol
, char **eol_p
)
683 while (bol
< --eol
) {
699 { "committer ", 10 },
702 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
703 enum grep_context ctx
,
704 regmatch_t
*pmatch
, int eflags
)
708 const char *start
= bol
;
710 if ((p
->token
!= GREP_PATTERN
) &&
711 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
714 if (p
->token
== GREP_PATTERN_HEAD
) {
717 assert(p
->field
< ARRAY_SIZE(header_field
));
718 field
= header_field
[p
->field
].field
;
719 len
= header_field
[p
->field
].len
;
720 if (strncmp(bol
, field
, len
))
724 case GREP_HEADER_AUTHOR
:
725 case GREP_HEADER_COMMITTER
:
726 saved_ch
= strip_timestamp(bol
, &eol
);
734 hit
= patmatch(p
, bol
, eol
, pmatch
, eflags
);
736 if (hit
&& p
->word_regexp
) {
737 if ((pmatch
[0].rm_so
< 0) ||
738 (eol
- bol
) < pmatch
[0].rm_so
||
739 (pmatch
[0].rm_eo
< 0) ||
740 (eol
- bol
) < pmatch
[0].rm_eo
)
741 die("regexp returned nonsense");
743 /* Match beginning must be either beginning of the
744 * line, or at word boundary (i.e. the last char must
745 * not be a word char). Similarly, match end must be
746 * either end of the line, or at word boundary
747 * (i.e. the next char must not be a word char).
749 if ( ((pmatch
[0].rm_so
== 0) ||
750 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
751 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
752 !word_char(bol
[pmatch
[0].rm_eo
])) )
757 /* Words consist of at least one character. */
758 if (pmatch
->rm_so
== pmatch
->rm_eo
)
761 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
762 /* There could be more than one match on the
763 * line, and the first match might not be
764 * strict word match. But later ones could be!
765 * Forward to the next possible start, i.e. the
766 * next position following a non-word char.
768 bol
= pmatch
[0].rm_so
+ bol
+ 1;
769 while (word_char(bol
[-1]) && bol
< eol
)
771 eflags
|= REG_NOTBOL
;
776 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
779 pmatch
[0].rm_so
+= bol
- start
;
780 pmatch
[0].rm_eo
+= bol
- start
;
785 static int match_expr_eval(struct grep_expr
*x
, char *bol
, char *eol
,
786 enum grep_context ctx
, int collect_hits
)
792 die("Not a valid grep expression");
798 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
, &match
, 0);
801 h
= !match_expr_eval(x
->u
.unary
, bol
, eol
, ctx
, 0);
804 if (!match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0))
806 h
= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
810 return (match_expr_eval(x
->u
.binary
.left
,
812 match_expr_eval(x
->u
.binary
.right
,
814 h
= match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
815 x
->u
.binary
.left
->hit
|= h
;
816 h
|= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
819 die("Unexpected node type (internal error) %d", x
->node
);
826 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
827 enum grep_context ctx
, int collect_hits
)
829 struct grep_expr
*x
= opt
->pattern_expression
;
830 return match_expr_eval(x
, bol
, eol
, ctx
, collect_hits
);
833 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
834 enum grep_context ctx
, int collect_hits
)
840 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
842 /* we do not call with collect_hits without being extended */
843 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
844 if (match_one_pattern(p
, bol
, eol
, ctx
, &match
, 0))
850 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
851 enum grep_context ctx
,
852 regmatch_t
*pmatch
, int eflags
)
856 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
858 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
860 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
861 if (match
.rm_so
> pmatch
->rm_so
)
863 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
866 pmatch
->rm_so
= match
.rm_so
;
867 pmatch
->rm_eo
= match
.rm_eo
;
871 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
872 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
877 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
879 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
881 case GREP_PATTERN
: /* atom */
882 case GREP_PATTERN_HEAD
:
883 case GREP_PATTERN_BODY
:
884 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
895 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
896 const char *name
, unsigned lno
, char sign
)
898 int rest
= eol
- bol
;
899 char *line_color
= NULL
;
901 if (opt
->file_break
&& opt
->last_shown
== 0) {
902 if (opt
->show_hunk_mark
)
903 opt
->output(opt
, "\n", 1);
904 } else if (opt
->pre_context
|| opt
->post_context
|| opt
->funcbody
) {
905 if (opt
->last_shown
== 0) {
906 if (opt
->show_hunk_mark
) {
907 output_color(opt
, "--", 2, opt
->color_sep
);
908 opt
->output(opt
, "\n", 1);
910 } else if (lno
> opt
->last_shown
+ 1) {
911 output_color(opt
, "--", 2, opt
->color_sep
);
912 opt
->output(opt
, "\n", 1);
915 if (opt
->heading
&& opt
->last_shown
== 0) {
916 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
917 opt
->output(opt
, "\n", 1);
919 opt
->last_shown
= lno
;
921 if (!opt
->heading
&& opt
->pathname
) {
922 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
923 output_sep(opt
, sign
);
927 snprintf(buf
, sizeof(buf
), "%d", lno
);
928 output_color(opt
, buf
, strlen(buf
), opt
->color_lineno
);
929 output_sep(opt
, sign
);
933 enum grep_context ctx
= GREP_CONTEXT_BODY
;
938 line_color
= opt
->color_selected
;
939 else if (sign
== '-')
940 line_color
= opt
->color_context
;
941 else if (sign
== '=')
942 line_color
= opt
->color_function
;
944 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
945 if (match
.rm_so
== match
.rm_eo
)
948 output_color(opt
, bol
, match
.rm_so
, line_color
);
949 output_color(opt
, bol
+ match
.rm_so
,
950 match
.rm_eo
- match
.rm_so
,
958 output_color(opt
, bol
, rest
, line_color
);
959 opt
->output(opt
, "\n", 1);
966 * This lock protects access to the gitattributes machinery, which is
969 pthread_mutex_t grep_attr_mutex
;
971 static inline void grep_attr_lock(void)
974 pthread_mutex_lock(&grep_attr_mutex
);
977 static inline void grep_attr_unlock(void)
980 pthread_mutex_unlock(&grep_attr_mutex
);
984 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
986 pthread_mutex_t grep_read_mutex
;
989 #define grep_attr_lock()
990 #define grep_attr_unlock()
993 static int match_funcname(struct grep_opt
*opt
, struct grep_source
*gs
, char *bol
, char *eol
)
995 xdemitconf_t
*xecfg
= opt
->priv
;
996 if (xecfg
&& !xecfg
->find_func
) {
997 grep_source_load_driver(gs
);
998 if (gs
->driver
->funcname
.pattern
) {
999 const struct userdiff_funcname
*pe
= &gs
->driver
->funcname
;
1000 xdiff_set_find_func(xecfg
, pe
->pattern
, pe
->cflags
);
1002 xecfg
= opt
->priv
= NULL
;
1008 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
1009 xecfg
->find_func_priv
) >= 0;
1014 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
1019 static void show_funcname_line(struct grep_opt
*opt
, struct grep_source
*gs
,
1020 char *bol
, unsigned lno
)
1022 while (bol
> gs
->buf
) {
1025 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1029 if (lno
<= opt
->last_shown
)
1032 if (match_funcname(opt
, gs
, bol
, eol
)) {
1033 show_line(opt
, bol
, eol
, gs
->name
, lno
, '=');
1039 static void show_pre_context(struct grep_opt
*opt
, struct grep_source
*gs
,
1040 char *bol
, char *end
, unsigned lno
)
1042 unsigned cur
= lno
, from
= 1, funcname_lno
= 0;
1043 int funcname_needed
= !!opt
->funcname
;
1045 if (opt
->funcbody
&& !match_funcname(opt
, gs
, bol
, end
))
1046 funcname_needed
= 2;
1048 if (opt
->pre_context
< lno
)
1049 from
= lno
- opt
->pre_context
;
1050 if (from
<= opt
->last_shown
)
1051 from
= opt
->last_shown
+ 1;
1054 while (bol
> gs
->buf
&&
1055 cur
> (funcname_needed
== 2 ? opt
->last_shown
+ 1 : from
)) {
1058 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1061 if (funcname_needed
&& match_funcname(opt
, gs
, bol
, eol
)) {
1063 funcname_needed
= 0;
1067 /* We need to look even further back to find a function signature. */
1068 if (opt
->funcname
&& funcname_needed
)
1069 show_funcname_line(opt
, gs
, bol
, cur
);
1073 char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
1075 while (*eol
!= '\n')
1077 show_line(opt
, bol
, eol
, gs
->name
, cur
, sign
);
1083 static int should_lookahead(struct grep_opt
*opt
)
1088 return 0; /* punt for too complex stuff */
1091 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1092 if (p
->token
!= GREP_PATTERN
)
1093 return 0; /* punt for "header only" and stuff */
1098 static int look_ahead(struct grep_opt
*opt
,
1099 unsigned long *left_p
,
1103 unsigned lno
= *lno_p
;
1106 char *sp
, *last_bol
;
1107 regoff_t earliest
= -1;
1109 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1113 hit
= patmatch(p
, bol
, bol
+ *left_p
, &m
, 0);
1114 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
1116 if (earliest
< 0 || m
.rm_so
< earliest
)
1121 *bol_p
= bol
+ *left_p
;
1125 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
1126 ; /* find the beginning of the line */
1129 for (sp
= bol
; sp
< last_bol
; sp
++) {
1133 *left_p
-= last_bol
- bol
;
1139 static void std_output(struct grep_opt
*opt
, const void *buf
, size_t size
)
1141 fwrite(buf
, size
, 1, stdout
);
1144 static int grep_source_1(struct grep_opt
*opt
, struct grep_source
*gs
, int collect_hits
)
1149 unsigned last_hit
= 0;
1150 int binary_match_only
= 0;
1152 int try_lookahead
= 0;
1153 int show_function
= 0;
1154 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
1158 opt
->output
= std_output
;
1160 if (opt
->pre_context
|| opt
->post_context
|| opt
->file_break
||
1162 /* Show hunk marks, except for the first file. */
1163 if (opt
->last_shown
)
1164 opt
->show_hunk_mark
= 1;
1166 * If we're using threads then we can't easily identify
1167 * the first file. Always put hunk marks in that case
1168 * and skip the very first one later in work_done().
1170 if (opt
->output
!= std_output
)
1171 opt
->show_hunk_mark
= 1;
1173 opt
->last_shown
= 0;
1175 switch (opt
->binary
) {
1176 case GREP_BINARY_DEFAULT
:
1177 if (grep_source_is_binary(gs
))
1178 binary_match_only
= 1;
1180 case GREP_BINARY_NOMATCH
:
1181 if (grep_source_is_binary(gs
))
1182 return 0; /* Assume unmatch */
1184 case GREP_BINARY_TEXT
:
1187 die("bug: unknown binary handling mode");
1190 memset(&xecfg
, 0, sizeof(xecfg
));
1193 try_lookahead
= should_lookahead(opt
);
1195 if (grep_source_load(gs
) < 0)
1205 * look_ahead() skips quickly to the line that possibly
1206 * has the next hit; don't call it if we need to do
1207 * something more than just skipping the current line
1208 * in response to an unmatch for the current line. E.g.
1209 * inside a post-context window, we will show the current
1210 * line as a context around the previous hit when it
1215 && (show_function
||
1216 lno
<= last_hit
+ opt
->post_context
))
1217 && look_ahead(opt
, &left
, &lno
, &bol
))
1219 eol
= end_of_line(bol
, &left
);
1223 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
1224 ctx
= GREP_CONTEXT_BODY
;
1226 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
1232 /* "grep -v -e foo -e bla" should list lines
1233 * that do not have either, so inversion should
1238 if (opt
->unmatch_name_only
) {
1245 if (opt
->status_only
)
1247 if (opt
->name_only
) {
1248 show_name(opt
, gs
->name
);
1253 if (binary_match_only
) {
1254 opt
->output(opt
, "Binary file ", 12);
1255 output_color(opt
, gs
->name
, strlen(gs
->name
),
1256 opt
->color_filename
);
1257 opt
->output(opt
, " matches\n", 9);
1260 /* Hit at this line. If we haven't shown the
1261 * pre-context lines, we would need to show them.
1263 if (opt
->pre_context
|| opt
->funcbody
)
1264 show_pre_context(opt
, gs
, bol
, eol
, lno
);
1265 else if (opt
->funcname
)
1266 show_funcname_line(opt
, gs
, bol
, lno
);
1267 show_line(opt
, bol
, eol
, gs
->name
, lno
, ':');
1273 if (show_function
&& match_funcname(opt
, gs
, bol
, eol
))
1275 if (show_function
||
1276 (last_hit
&& lno
<= last_hit
+ opt
->post_context
)) {
1277 /* If the last hit is within the post context,
1278 * we need to show this line.
1280 show_line(opt
, bol
, eol
, gs
->name
, lno
, '-');
1294 if (opt
->status_only
)
1296 if (opt
->unmatch_name_only
) {
1297 /* We did not see any hit, so we want to show this */
1298 show_name(opt
, gs
->name
);
1302 xdiff_clear_find_func(&xecfg
);
1306 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1307 * which feels mostly useless but sometimes useful. Maybe
1308 * make it another option? For now suppress them.
1310 if (opt
->count
&& count
) {
1312 output_color(opt
, gs
->name
, strlen(gs
->name
), opt
->color_filename
);
1313 output_sep(opt
, ':');
1314 snprintf(buf
, sizeof(buf
), "%u\n", count
);
1315 opt
->output(opt
, buf
, strlen(buf
));
1321 static void clr_hit_marker(struct grep_expr
*x
)
1323 /* All-hit markers are meaningful only at the very top level
1328 if (x
->node
!= GREP_NODE_OR
)
1330 x
->u
.binary
.left
->hit
= 0;
1331 x
= x
->u
.binary
.right
;
1335 static int chk_hit_marker(struct grep_expr
*x
)
1337 /* Top level nodes have hit markers. See if they all are hits */
1339 if (x
->node
!= GREP_NODE_OR
)
1341 if (!x
->u
.binary
.left
->hit
)
1343 x
= x
->u
.binary
.right
;
1347 int grep_source(struct grep_opt
*opt
, struct grep_source
*gs
)
1350 * we do not have to do the two-pass grep when we do not check
1351 * buffer-wide "all-match".
1353 if (!opt
->all_match
)
1354 return grep_source_1(opt
, gs
, 0);
1356 /* Otherwise the toplevel "or" terms hit a bit differently.
1357 * We first clear hit markers from them.
1359 clr_hit_marker(opt
->pattern_expression
);
1360 grep_source_1(opt
, gs
, 1);
1362 if (!chk_hit_marker(opt
->pattern_expression
))
1365 return grep_source_1(opt
, gs
, 0);
1368 int grep_buffer(struct grep_opt
*opt
, char *buf
, unsigned long size
)
1370 struct grep_source gs
;
1373 grep_source_init(&gs
, GREP_SOURCE_BUF
, NULL
, NULL
);
1377 r
= grep_source(opt
, &gs
);
1379 grep_source_clear(&gs
);
1383 void grep_source_init(struct grep_source
*gs
, enum grep_source_type type
,
1384 const char *name
, const void *identifier
)
1387 gs
->name
= name
? xstrdup(name
) : NULL
;
1393 case GREP_SOURCE_FILE
:
1394 gs
->identifier
= xstrdup(identifier
);
1396 case GREP_SOURCE_SHA1
:
1397 gs
->identifier
= xmalloc(20);
1398 memcpy(gs
->identifier
, identifier
, 20);
1400 case GREP_SOURCE_BUF
:
1401 gs
->identifier
= NULL
;
1405 void grep_source_clear(struct grep_source
*gs
)
1409 free(gs
->identifier
);
1410 gs
->identifier
= NULL
;
1411 grep_source_clear_data(gs
);
1414 void grep_source_clear_data(struct grep_source
*gs
)
1417 case GREP_SOURCE_FILE
:
1418 case GREP_SOURCE_SHA1
:
1423 case GREP_SOURCE_BUF
:
1424 /* leave user-provided buf intact */
1429 static int grep_source_load_sha1(struct grep_source
*gs
)
1431 enum object_type type
;
1434 gs
->buf
= read_sha1_file(gs
->identifier
, &type
, &gs
->size
);
1438 return error(_("'%s': unable to read %s"),
1440 sha1_to_hex(gs
->identifier
));
1444 static int grep_source_load_file(struct grep_source
*gs
)
1446 const char *filename
= gs
->identifier
;
1452 if (lstat(filename
, &st
) < 0) {
1454 if (errno
!= ENOENT
)
1455 error(_("'%s': %s"), filename
, strerror(errno
));
1458 if (!S_ISREG(st
.st_mode
))
1460 size
= xsize_t(st
.st_size
);
1461 i
= open(filename
, O_RDONLY
);
1464 data
= xmalloc(size
+ 1);
1465 if (st
.st_size
!= read_in_full(i
, data
, size
)) {
1466 error(_("'%s': short read %s"), filename
, strerror(errno
));
1479 static int grep_source_load(struct grep_source
*gs
)
1485 case GREP_SOURCE_FILE
:
1486 return grep_source_load_file(gs
);
1487 case GREP_SOURCE_SHA1
:
1488 return grep_source_load_sha1(gs
);
1489 case GREP_SOURCE_BUF
:
1490 return gs
->buf
? 0 : -1;
1492 die("BUG: invalid grep_source type");
1495 void grep_source_load_driver(struct grep_source
*gs
)
1501 gs
->driver
= userdiff_find_by_path(gs
->name
);
1503 gs
->driver
= userdiff_find_by_name("default");
1507 static int grep_source_is_binary(struct grep_source
*gs
)
1509 grep_source_load_driver(gs
);
1510 if (gs
->driver
->binary
!= -1)
1511 return gs
->driver
->binary
;
1513 if (!grep_source_load(gs
))
1514 return buffer_is_binary(gs
->buf
, gs
->size
);