4 #include "xdiff-interface.h"
6 static struct grep_pat
*create_grep_pat(const char *pat
, size_t patlen
,
7 const char *origin
, int no
,
9 enum grep_header_field field
)
11 struct grep_pat
*p
= xcalloc(1, sizeof(*p
));
12 p
->pattern
= xmemdupz(pat
, patlen
);
13 p
->patternlen
= patlen
;
21 static void do_append_grep_pat(struct grep_pat
***tail
, struct grep_pat
*p
)
28 case GREP_PATTERN
: /* atom */
29 case GREP_PATTERN_HEAD
:
30 case GREP_PATTERN_BODY
:
32 struct grep_pat
*new_pat
;
34 char *cp
= p
->pattern
+ p
->patternlen
, *nl
= NULL
;
35 while (++len
<= p
->patternlen
) {
36 if (*(--cp
) == '\n') {
43 new_pat
= create_grep_pat(nl
+ 1, len
- 1, p
->origin
,
44 p
->no
, p
->token
, p
->field
);
45 new_pat
->next
= p
->next
;
47 *tail
= &new_pat
->next
;
58 void append_header_grep_pattern(struct grep_opt
*opt
,
59 enum grep_header_field field
, const char *pat
)
61 struct grep_pat
*p
= create_grep_pat(pat
, strlen(pat
), "header", 0,
62 GREP_PATTERN_HEAD
, field
);
63 do_append_grep_pat(&opt
->header_tail
, p
);
66 void append_grep_pattern(struct grep_opt
*opt
, const char *pat
,
67 const char *origin
, int no
, enum grep_pat_token t
)
69 append_grep_pat(opt
, pat
, strlen(pat
), origin
, no
, t
);
72 void append_grep_pat(struct grep_opt
*opt
, const char *pat
, size_t patlen
,
73 const char *origin
, int no
, enum grep_pat_token t
)
75 struct grep_pat
*p
= create_grep_pat(pat
, patlen
, origin
, no
, t
, 0);
76 do_append_grep_pat(&opt
->pattern_tail
, p
);
79 struct grep_opt
*grep_opt_dup(const struct grep_opt
*opt
)
82 struct grep_opt
*ret
= xmalloc(sizeof(struct grep_opt
));
85 ret
->pattern_list
= NULL
;
86 ret
->pattern_tail
= &ret
->pattern_list
;
88 for(pat
= opt
->pattern_list
; pat
!= NULL
; pat
= pat
->next
)
90 if(pat
->token
== GREP_PATTERN_HEAD
)
91 append_header_grep_pattern(ret
, pat
->field
,
94 append_grep_pat(ret
, pat
->pattern
, pat
->patternlen
,
95 pat
->origin
, pat
->no
, pat
->token
);
101 static NORETURN
void compile_regexp_failed(const struct grep_pat
*p
,
107 sprintf(where
, "In '%s' at %d, ", p
->origin
, p
->no
);
109 sprintf(where
, "%s, ", p
->origin
);
113 die("%s'%s': %s", where
, p
->pattern
, error
);
117 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
121 int options
= PCRE_MULTILINE
;
123 if (opt
->ignore_case
)
124 options
|= PCRE_CASELESS
;
126 p
->pcre_regexp
= pcre_compile(p
->pattern
, options
, &error
, &erroffset
,
129 compile_regexp_failed(p
, error
);
131 p
->pcre_extra_info
= pcre_study(p
->pcre_regexp
, 0, &error
);
132 if (!p
->pcre_extra_info
&& error
)
136 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
137 regmatch_t
*match
, int eflags
)
139 int ovector
[30], ret
, flags
= 0;
141 if (eflags
& REG_NOTBOL
)
142 flags
|= PCRE_NOTBOL
;
144 ret
= pcre_exec(p
->pcre_regexp
, p
->pcre_extra_info
, line
, eol
- line
,
145 0, flags
, ovector
, ARRAY_SIZE(ovector
));
146 if (ret
< 0 && ret
!= PCRE_ERROR_NOMATCH
)
147 die("pcre_exec failed with error code %d", ret
);
150 match
->rm_so
= ovector
[0];
151 match
->rm_eo
= ovector
[1];
157 static void free_pcre_regexp(struct grep_pat
*p
)
159 pcre_free(p
->pcre_regexp
);
160 pcre_free(p
->pcre_extra_info
);
162 #else /* !USE_LIBPCRE */
163 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
165 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
168 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
169 regmatch_t
*match
, int eflags
)
174 static void free_pcre_regexp(struct grep_pat
*p
)
177 #endif /* !USE_LIBPCRE */
179 static int is_fixed(const char *s
, size_t len
)
183 /* regcomp cannot accept patterns with NULs so we
184 * consider any pattern containing a NUL fixed.
186 if (memchr(s
, 0, len
))
189 for (i
= 0; i
< len
; i
++) {
190 if (is_regex_special(s
[i
]))
197 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
201 p
->word_regexp
= opt
->word_regexp
;
202 p
->ignore_case
= opt
->ignore_case
;
204 if (opt
->fixed
|| is_fixed(p
->pattern
, p
->patternlen
))
210 if (opt
->regflags
& REG_ICASE
|| p
->ignore_case
)
211 p
->kws
= kwsalloc(tolower_trans_tbl
);
213 p
->kws
= kwsalloc(NULL
);
214 kwsincr(p
->kws
, p
->pattern
, p
->patternlen
);
220 compile_pcre_regexp(p
, opt
);
224 err
= regcomp(&p
->regexp
, p
->pattern
, opt
->regflags
);
227 regerror(err
, &p
->regexp
, errbuf
, 1024);
229 compile_regexp_failed(p
, errbuf
);
233 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
234 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
243 case GREP_PATTERN
: /* atom */
244 case GREP_PATTERN_HEAD
:
245 case GREP_PATTERN_BODY
:
246 x
= xcalloc(1, sizeof (struct grep_expr
));
247 x
->node
= GREP_NODE_ATOM
;
251 case GREP_OPEN_PAREN
:
253 x
= compile_pattern_or(list
);
254 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
255 die("unmatched parenthesis");
256 *list
= (*list
)->next
;
263 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
274 die("--not not followed by pattern expression");
276 x
= xcalloc(1, sizeof (struct grep_expr
));
277 x
->node
= GREP_NODE_NOT
;
278 x
->u
.unary
= compile_pattern_not(list
);
280 die("--not followed by non pattern expression");
283 return compile_pattern_atom(list
);
287 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
290 struct grep_expr
*x
, *y
, *z
;
292 x
= compile_pattern_not(list
);
294 if (p
&& p
->token
== GREP_AND
) {
296 die("--and not followed by pattern expression");
298 y
= compile_pattern_and(list
);
300 die("--and not followed by pattern expression");
301 z
= xcalloc(1, sizeof (struct grep_expr
));
302 z
->node
= GREP_NODE_AND
;
303 z
->u
.binary
.left
= x
;
304 z
->u
.binary
.right
= y
;
310 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
313 struct grep_expr
*x
, *y
, *z
;
315 x
= compile_pattern_and(list
);
317 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
318 y
= compile_pattern_or(list
);
320 die("not a pattern expression %s", p
->pattern
);
321 z
= xcalloc(1, sizeof (struct grep_expr
));
322 z
->node
= GREP_NODE_OR
;
323 z
->u
.binary
.left
= x
;
324 z
->u
.binary
.right
= y
;
330 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
332 return compile_pattern_or(list
);
335 static void indent(int in
)
341 static void dump_grep_pat(struct grep_pat
*p
)
344 case GREP_AND
: fprintf(stderr
, "*and*"); break;
345 case GREP_OPEN_PAREN
: fprintf(stderr
, "*(*"); break;
346 case GREP_CLOSE_PAREN
: fprintf(stderr
, "*)*"); break;
347 case GREP_NOT
: fprintf(stderr
, "*not*"); break;
348 case GREP_OR
: fprintf(stderr
, "*or*"); break;
350 case GREP_PATTERN
: fprintf(stderr
, "pattern"); break;
351 case GREP_PATTERN_HEAD
: fprintf(stderr
, "pattern_head"); break;
352 case GREP_PATTERN_BODY
: fprintf(stderr
, "pattern_body"); break;
357 case GREP_PATTERN_HEAD
:
358 fprintf(stderr
, "<head %d>", p
->field
); break;
359 case GREP_PATTERN_BODY
:
360 fprintf(stderr
, "<body>"); break;
364 case GREP_PATTERN_HEAD
:
365 case GREP_PATTERN_BODY
:
367 fprintf(stderr
, "%.*s", (int)p
->patternlen
, p
->pattern
);
373 static void dump_grep_expression_1(struct grep_expr
*x
, int in
)
378 fprintf(stderr
, "true\n");
381 dump_grep_pat(x
->u
.atom
);
384 fprintf(stderr
, "(not\n");
385 dump_grep_expression_1(x
->u
.unary
, in
+1);
387 fprintf(stderr
, ")\n");
390 fprintf(stderr
, "(and\n");
391 dump_grep_expression_1(x
->u
.binary
.left
, in
+1);
392 dump_grep_expression_1(x
->u
.binary
.right
, in
+1);
394 fprintf(stderr
, ")\n");
397 fprintf(stderr
, "(or\n");
398 dump_grep_expression_1(x
->u
.binary
.left
, in
+1);
399 dump_grep_expression_1(x
->u
.binary
.right
, in
+1);
401 fprintf(stderr
, ")\n");
406 void dump_grep_expression(struct grep_opt
*opt
)
408 struct grep_expr
*x
= opt
->pattern_expression
;
411 fprintf(stderr
, "[all-match]\n");
412 dump_grep_expression_1(x
, 0);
416 static struct grep_expr
*grep_true_expr(void)
418 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
419 z
->node
= GREP_NODE_TRUE
;
423 static struct grep_expr
*grep_or_expr(struct grep_expr
*left
, struct grep_expr
*right
)
425 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
426 z
->node
= GREP_NODE_OR
;
427 z
->u
.binary
.left
= left
;
428 z
->u
.binary
.right
= right
;
432 static struct grep_expr
*prep_header_patterns(struct grep_opt
*opt
)
435 struct grep_expr
*header_expr
;
436 struct grep_expr
*(header_group
[GREP_HEADER_FIELD_MAX
]);
437 enum grep_header_field fld
;
439 if (!opt
->header_list
)
442 for (p
= opt
->header_list
; p
; p
= p
->next
) {
443 if (p
->token
!= GREP_PATTERN_HEAD
)
444 die("bug: a non-header pattern in grep header list.");
445 if (p
->field
< 0 || GREP_HEADER_FIELD_MAX
<= p
->field
)
446 die("bug: unknown header field %d", p
->field
);
447 compile_regexp(p
, opt
);
450 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++)
451 header_group
[fld
] = NULL
;
453 for (p
= opt
->header_list
; p
; p
= p
->next
) {
455 struct grep_pat
*pp
= p
;
457 h
= compile_pattern_atom(&pp
);
458 if (!h
|| pp
!= p
->next
)
459 die("bug: malformed header expr");
460 if (!header_group
[p
->field
]) {
461 header_group
[p
->field
] = h
;
464 header_group
[p
->field
] = grep_or_expr(h
, header_group
[p
->field
]);
469 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++) {
470 if (!header_group
[fld
])
473 header_expr
= grep_true_expr();
474 header_expr
= grep_or_expr(header_group
[fld
], header_expr
);
479 static struct grep_expr
*grep_splice_or(struct grep_expr
*x
, struct grep_expr
*y
)
481 struct grep_expr
*z
= x
;
484 assert(x
->node
== GREP_NODE_OR
);
485 if (x
->u
.binary
.right
&&
486 x
->u
.binary
.right
->node
== GREP_NODE_TRUE
) {
487 x
->u
.binary
.right
= y
;
490 x
= x
->u
.binary
.right
;
495 static void compile_grep_patterns_real(struct grep_opt
*opt
)
498 struct grep_expr
*header_expr
= prep_header_patterns(opt
);
500 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
502 case GREP_PATTERN
: /* atom */
503 case GREP_PATTERN_HEAD
:
504 case GREP_PATTERN_BODY
:
505 compile_regexp(p
, opt
);
513 if (opt
->all_match
|| header_expr
)
515 else if (!opt
->extended
&& !opt
->debug
)
518 p
= opt
->pattern_list
;
520 opt
->pattern_expression
= compile_pattern_expr(&p
);
522 die("incomplete pattern expression: %s", p
->pattern
);
527 if (!opt
->pattern_expression
)
528 opt
->pattern_expression
= header_expr
;
529 else if (opt
->all_match
)
530 opt
->pattern_expression
= grep_splice_or(header_expr
,
531 opt
->pattern_expression
);
533 opt
->pattern_expression
= grep_or_expr(opt
->pattern_expression
,
538 void compile_grep_patterns(struct grep_opt
*opt
)
540 compile_grep_patterns_real(opt
);
542 dump_grep_expression(opt
);
545 static void free_pattern_expr(struct grep_expr
*x
)
552 free_pattern_expr(x
->u
.unary
);
556 free_pattern_expr(x
->u
.binary
.left
);
557 free_pattern_expr(x
->u
.binary
.right
);
563 void free_grep_patterns(struct grep_opt
*opt
)
565 struct grep_pat
*p
, *n
;
567 for (p
= opt
->pattern_list
; p
; p
= n
) {
570 case GREP_PATTERN
: /* atom */
571 case GREP_PATTERN_HEAD
:
572 case GREP_PATTERN_BODY
:
575 else if (p
->pcre_regexp
)
589 free_pattern_expr(opt
->pattern_expression
);
592 static char *end_of_line(char *cp
, unsigned long *left
)
594 unsigned long l
= *left
;
595 while (l
&& *cp
!= '\n') {
603 static int word_char(char ch
)
605 return isalnum(ch
) || ch
== '_';
608 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
611 if (want_color(opt
->color
) && color
&& color
[0]) {
612 opt
->output(opt
, color
, strlen(color
));
613 opt
->output(opt
, data
, size
);
614 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
616 opt
->output(opt
, data
, size
);
619 static void output_sep(struct grep_opt
*opt
, char sign
)
621 if (opt
->null_following_name
)
622 opt
->output(opt
, "\0", 1);
624 output_color(opt
, &sign
, 1, opt
->color_sep
);
627 static void show_name(struct grep_opt
*opt
, const char *name
)
629 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
630 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
633 static int fixmatch(struct grep_pat
*p
, char *line
, char *eol
,
636 struct kwsmatch kwsm
;
637 size_t offset
= kwsexec(p
->kws
, line
, eol
- line
, &kwsm
);
639 match
->rm_so
= match
->rm_eo
= -1;
642 match
->rm_so
= offset
;
643 match
->rm_eo
= match
->rm_so
+ kwsm
.size
[0];
648 static int regmatch(const regex_t
*preg
, char *line
, char *eol
,
649 regmatch_t
*match
, int eflags
)
653 match
->rm_eo
= eol
- line
;
654 eflags
|= REG_STARTEND
;
656 return regexec(preg
, line
, 1, match
, eflags
);
659 static int patmatch(struct grep_pat
*p
, char *line
, char *eol
,
660 regmatch_t
*match
, int eflags
)
665 hit
= !fixmatch(p
, line
, eol
, match
);
666 else if (p
->pcre_regexp
)
667 hit
= !pcrematch(p
, line
, eol
, match
, eflags
);
669 hit
= !regmatch(&p
->regexp
, line
, eol
, match
, eflags
);
674 static int strip_timestamp(char *bol
, char **eol_p
)
679 while (bol
< --eol
) {
695 { "committer ", 10 },
698 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
699 enum grep_context ctx
,
700 regmatch_t
*pmatch
, int eflags
)
704 const char *start
= bol
;
706 if ((p
->token
!= GREP_PATTERN
) &&
707 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
710 if (p
->token
== GREP_PATTERN_HEAD
) {
713 assert(p
->field
< ARRAY_SIZE(header_field
));
714 field
= header_field
[p
->field
].field
;
715 len
= header_field
[p
->field
].len
;
716 if (strncmp(bol
, field
, len
))
719 saved_ch
= strip_timestamp(bol
, &eol
);
723 hit
= patmatch(p
, bol
, eol
, pmatch
, eflags
);
725 if (hit
&& p
->word_regexp
) {
726 if ((pmatch
[0].rm_so
< 0) ||
727 (eol
- bol
) < pmatch
[0].rm_so
||
728 (pmatch
[0].rm_eo
< 0) ||
729 (eol
- bol
) < pmatch
[0].rm_eo
)
730 die("regexp returned nonsense");
732 /* Match beginning must be either beginning of the
733 * line, or at word boundary (i.e. the last char must
734 * not be a word char). Similarly, match end must be
735 * either end of the line, or at word boundary
736 * (i.e. the next char must not be a word char).
738 if ( ((pmatch
[0].rm_so
== 0) ||
739 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
740 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
741 !word_char(bol
[pmatch
[0].rm_eo
])) )
746 /* Words consist of at least one character. */
747 if (pmatch
->rm_so
== pmatch
->rm_eo
)
750 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
751 /* There could be more than one match on the
752 * line, and the first match might not be
753 * strict word match. But later ones could be!
754 * Forward to the next possible start, i.e. the
755 * next position following a non-word char.
757 bol
= pmatch
[0].rm_so
+ bol
+ 1;
758 while (word_char(bol
[-1]) && bol
< eol
)
760 eflags
|= REG_NOTBOL
;
765 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
768 pmatch
[0].rm_so
+= bol
- start
;
769 pmatch
[0].rm_eo
+= bol
- start
;
774 static int match_expr_eval(struct grep_expr
*x
, char *bol
, char *eol
,
775 enum grep_context ctx
, int collect_hits
)
781 die("Not a valid grep expression");
787 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
, &match
, 0);
790 h
= !match_expr_eval(x
->u
.unary
, bol
, eol
, ctx
, 0);
793 if (!match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0))
795 h
= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
799 return (match_expr_eval(x
->u
.binary
.left
,
801 match_expr_eval(x
->u
.binary
.right
,
803 h
= match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
804 x
->u
.binary
.left
->hit
|= h
;
805 h
|= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
808 die("Unexpected node type (internal error) %d", x
->node
);
815 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
816 enum grep_context ctx
, int collect_hits
)
818 struct grep_expr
*x
= opt
->pattern_expression
;
819 return match_expr_eval(x
, bol
, eol
, ctx
, collect_hits
);
822 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
823 enum grep_context ctx
, int collect_hits
)
829 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
831 /* we do not call with collect_hits without being extended */
832 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
833 if (match_one_pattern(p
, bol
, eol
, ctx
, &match
, 0))
839 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
840 enum grep_context ctx
,
841 regmatch_t
*pmatch
, int eflags
)
845 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
847 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
849 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
850 if (match
.rm_so
> pmatch
->rm_so
)
852 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
855 pmatch
->rm_so
= match
.rm_so
;
856 pmatch
->rm_eo
= match
.rm_eo
;
860 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
861 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
866 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
868 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
870 case GREP_PATTERN
: /* atom */
871 case GREP_PATTERN_HEAD
:
872 case GREP_PATTERN_BODY
:
873 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
884 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
885 const char *name
, unsigned lno
, char sign
)
887 int rest
= eol
- bol
;
888 char *line_color
= NULL
;
890 if (opt
->file_break
&& opt
->last_shown
== 0) {
891 if (opt
->show_hunk_mark
)
892 opt
->output(opt
, "\n", 1);
893 } else if (opt
->pre_context
|| opt
->post_context
|| opt
->funcbody
) {
894 if (opt
->last_shown
== 0) {
895 if (opt
->show_hunk_mark
) {
896 output_color(opt
, "--", 2, opt
->color_sep
);
897 opt
->output(opt
, "\n", 1);
899 } else if (lno
> opt
->last_shown
+ 1) {
900 output_color(opt
, "--", 2, opt
->color_sep
);
901 opt
->output(opt
, "\n", 1);
904 if (opt
->heading
&& opt
->last_shown
== 0) {
905 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
906 opt
->output(opt
, "\n", 1);
908 opt
->last_shown
= lno
;
910 if (!opt
->heading
&& opt
->pathname
) {
911 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
912 output_sep(opt
, sign
);
916 snprintf(buf
, sizeof(buf
), "%d", lno
);
917 output_color(opt
, buf
, strlen(buf
), opt
->color_lineno
);
918 output_sep(opt
, sign
);
922 enum grep_context ctx
= GREP_CONTEXT_BODY
;
927 line_color
= opt
->color_selected
;
928 else if (sign
== '-')
929 line_color
= opt
->color_context
;
930 else if (sign
== '=')
931 line_color
= opt
->color_function
;
933 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
934 if (match
.rm_so
== match
.rm_eo
)
937 output_color(opt
, bol
, match
.rm_so
, line_color
);
938 output_color(opt
, bol
+ match
.rm_so
,
939 match
.rm_eo
- match
.rm_so
,
947 output_color(opt
, bol
, rest
, line_color
);
948 opt
->output(opt
, "\n", 1);
955 * This lock protects access to the gitattributes machinery, which is
958 pthread_mutex_t grep_attr_mutex
;
960 static inline void grep_attr_lock(void)
963 pthread_mutex_lock(&grep_attr_mutex
);
966 static inline void grep_attr_unlock(void)
969 pthread_mutex_unlock(&grep_attr_mutex
);
973 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
975 pthread_mutex_t grep_read_mutex
;
978 #define grep_attr_lock()
979 #define grep_attr_unlock()
982 static int match_funcname(struct grep_opt
*opt
, struct grep_source
*gs
, char *bol
, char *eol
)
984 xdemitconf_t
*xecfg
= opt
->priv
;
985 if (xecfg
&& !xecfg
->find_func
) {
986 grep_source_load_driver(gs
);
987 if (gs
->driver
->funcname
.pattern
) {
988 const struct userdiff_funcname
*pe
= &gs
->driver
->funcname
;
989 xdiff_set_find_func(xecfg
, pe
->pattern
, pe
->cflags
);
991 xecfg
= opt
->priv
= NULL
;
997 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
998 xecfg
->find_func_priv
) >= 0;
1003 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
1008 static void show_funcname_line(struct grep_opt
*opt
, struct grep_source
*gs
,
1009 char *bol
, unsigned lno
)
1011 while (bol
> gs
->buf
) {
1014 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1018 if (lno
<= opt
->last_shown
)
1021 if (match_funcname(opt
, gs
, bol
, eol
)) {
1022 show_line(opt
, bol
, eol
, gs
->name
, lno
, '=');
1028 static void show_pre_context(struct grep_opt
*opt
, struct grep_source
*gs
,
1029 char *bol
, char *end
, unsigned lno
)
1031 unsigned cur
= lno
, from
= 1, funcname_lno
= 0;
1032 int funcname_needed
= !!opt
->funcname
;
1034 if (opt
->funcbody
&& !match_funcname(opt
, gs
, bol
, end
))
1035 funcname_needed
= 2;
1037 if (opt
->pre_context
< lno
)
1038 from
= lno
- opt
->pre_context
;
1039 if (from
<= opt
->last_shown
)
1040 from
= opt
->last_shown
+ 1;
1043 while (bol
> gs
->buf
&&
1044 cur
> (funcname_needed
== 2 ? opt
->last_shown
+ 1 : from
)) {
1047 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1050 if (funcname_needed
&& match_funcname(opt
, gs
, bol
, eol
)) {
1052 funcname_needed
= 0;
1056 /* We need to look even further back to find a function signature. */
1057 if (opt
->funcname
&& funcname_needed
)
1058 show_funcname_line(opt
, gs
, bol
, cur
);
1062 char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
1064 while (*eol
!= '\n')
1066 show_line(opt
, bol
, eol
, gs
->name
, cur
, sign
);
1072 static int should_lookahead(struct grep_opt
*opt
)
1077 return 0; /* punt for too complex stuff */
1080 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1081 if (p
->token
!= GREP_PATTERN
)
1082 return 0; /* punt for "header only" and stuff */
1087 static int look_ahead(struct grep_opt
*opt
,
1088 unsigned long *left_p
,
1092 unsigned lno
= *lno_p
;
1095 char *sp
, *last_bol
;
1096 regoff_t earliest
= -1;
1098 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1102 hit
= patmatch(p
, bol
, bol
+ *left_p
, &m
, 0);
1103 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
1105 if (earliest
< 0 || m
.rm_so
< earliest
)
1110 *bol_p
= bol
+ *left_p
;
1114 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
1115 ; /* find the beginning of the line */
1118 for (sp
= bol
; sp
< last_bol
; sp
++) {
1122 *left_p
-= last_bol
- bol
;
1128 static void std_output(struct grep_opt
*opt
, const void *buf
, size_t size
)
1130 fwrite(buf
, size
, 1, stdout
);
1133 static int grep_source_1(struct grep_opt
*opt
, struct grep_source
*gs
, int collect_hits
)
1138 unsigned last_hit
= 0;
1139 int binary_match_only
= 0;
1141 int try_lookahead
= 0;
1142 int show_function
= 0;
1143 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
1147 opt
->output
= std_output
;
1149 if (opt
->pre_context
|| opt
->post_context
|| opt
->file_break
||
1151 /* Show hunk marks, except for the first file. */
1152 if (opt
->last_shown
)
1153 opt
->show_hunk_mark
= 1;
1155 * If we're using threads then we can't easily identify
1156 * the first file. Always put hunk marks in that case
1157 * and skip the very first one later in work_done().
1159 if (opt
->output
!= std_output
)
1160 opt
->show_hunk_mark
= 1;
1162 opt
->last_shown
= 0;
1164 switch (opt
->binary
) {
1165 case GREP_BINARY_DEFAULT
:
1166 if (grep_source_is_binary(gs
))
1167 binary_match_only
= 1;
1169 case GREP_BINARY_NOMATCH
:
1170 if (grep_source_is_binary(gs
))
1171 return 0; /* Assume unmatch */
1173 case GREP_BINARY_TEXT
:
1176 die("bug: unknown binary handling mode");
1179 memset(&xecfg
, 0, sizeof(xecfg
));
1182 try_lookahead
= should_lookahead(opt
);
1184 if (grep_source_load(gs
) < 0)
1194 * look_ahead() skips quickly to the line that possibly
1195 * has the next hit; don't call it if we need to do
1196 * something more than just skipping the current line
1197 * in response to an unmatch for the current line. E.g.
1198 * inside a post-context window, we will show the current
1199 * line as a context around the previous hit when it
1204 && (show_function
||
1205 lno
<= last_hit
+ opt
->post_context
))
1206 && look_ahead(opt
, &left
, &lno
, &bol
))
1208 eol
= end_of_line(bol
, &left
);
1212 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
1213 ctx
= GREP_CONTEXT_BODY
;
1215 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
1221 /* "grep -v -e foo -e bla" should list lines
1222 * that do not have either, so inversion should
1227 if (opt
->unmatch_name_only
) {
1234 if (opt
->status_only
)
1236 if (opt
->name_only
) {
1237 show_name(opt
, gs
->name
);
1242 if (binary_match_only
) {
1243 opt
->output(opt
, "Binary file ", 12);
1244 output_color(opt
, gs
->name
, strlen(gs
->name
),
1245 opt
->color_filename
);
1246 opt
->output(opt
, " matches\n", 9);
1249 /* Hit at this line. If we haven't shown the
1250 * pre-context lines, we would need to show them.
1252 if (opt
->pre_context
|| opt
->funcbody
)
1253 show_pre_context(opt
, gs
, bol
, eol
, lno
);
1254 else if (opt
->funcname
)
1255 show_funcname_line(opt
, gs
, bol
, lno
);
1256 show_line(opt
, bol
, eol
, gs
->name
, lno
, ':');
1262 if (show_function
&& match_funcname(opt
, gs
, bol
, eol
))
1264 if (show_function
||
1265 (last_hit
&& lno
<= last_hit
+ opt
->post_context
)) {
1266 /* If the last hit is within the post context,
1267 * we need to show this line.
1269 show_line(opt
, bol
, eol
, gs
->name
, lno
, '-');
1283 if (opt
->status_only
)
1285 if (opt
->unmatch_name_only
) {
1286 /* We did not see any hit, so we want to show this */
1287 show_name(opt
, gs
->name
);
1291 xdiff_clear_find_func(&xecfg
);
1295 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1296 * which feels mostly useless but sometimes useful. Maybe
1297 * make it another option? For now suppress them.
1299 if (opt
->count
&& count
) {
1301 output_color(opt
, gs
->name
, strlen(gs
->name
), opt
->color_filename
);
1302 output_sep(opt
, ':');
1303 snprintf(buf
, sizeof(buf
), "%u\n", count
);
1304 opt
->output(opt
, buf
, strlen(buf
));
1310 static void clr_hit_marker(struct grep_expr
*x
)
1312 /* All-hit markers are meaningful only at the very top level
1317 if (x
->node
!= GREP_NODE_OR
)
1319 x
->u
.binary
.left
->hit
= 0;
1320 x
= x
->u
.binary
.right
;
1324 static int chk_hit_marker(struct grep_expr
*x
)
1326 /* Top level nodes have hit markers. See if they all are hits */
1328 if (x
->node
!= GREP_NODE_OR
)
1330 if (!x
->u
.binary
.left
->hit
)
1332 x
= x
->u
.binary
.right
;
1336 int grep_source(struct grep_opt
*opt
, struct grep_source
*gs
)
1339 * we do not have to do the two-pass grep when we do not check
1340 * buffer-wide "all-match".
1342 if (!opt
->all_match
)
1343 return grep_source_1(opt
, gs
, 0);
1345 /* Otherwise the toplevel "or" terms hit a bit differently.
1346 * We first clear hit markers from them.
1348 clr_hit_marker(opt
->pattern_expression
);
1349 grep_source_1(opt
, gs
, 1);
1351 if (!chk_hit_marker(opt
->pattern_expression
))
1354 return grep_source_1(opt
, gs
, 0);
1357 int grep_buffer(struct grep_opt
*opt
, char *buf
, unsigned long size
)
1359 struct grep_source gs
;
1362 grep_source_init(&gs
, GREP_SOURCE_BUF
, NULL
, NULL
);
1366 r
= grep_source(opt
, &gs
);
1368 grep_source_clear(&gs
);
1372 void grep_source_init(struct grep_source
*gs
, enum grep_source_type type
,
1373 const char *name
, const void *identifier
)
1376 gs
->name
= name
? xstrdup(name
) : NULL
;
1382 case GREP_SOURCE_FILE
:
1383 gs
->identifier
= xstrdup(identifier
);
1385 case GREP_SOURCE_SHA1
:
1386 gs
->identifier
= xmalloc(20);
1387 memcpy(gs
->identifier
, identifier
, 20);
1389 case GREP_SOURCE_BUF
:
1390 gs
->identifier
= NULL
;
1394 void grep_source_clear(struct grep_source
*gs
)
1398 free(gs
->identifier
);
1399 gs
->identifier
= NULL
;
1400 grep_source_clear_data(gs
);
1403 void grep_source_clear_data(struct grep_source
*gs
)
1406 case GREP_SOURCE_FILE
:
1407 case GREP_SOURCE_SHA1
:
1412 case GREP_SOURCE_BUF
:
1413 /* leave user-provided buf intact */
1418 static int grep_source_load_sha1(struct grep_source
*gs
)
1420 enum object_type type
;
1423 gs
->buf
= read_sha1_file(gs
->identifier
, &type
, &gs
->size
);
1427 return error(_("'%s': unable to read %s"),
1429 sha1_to_hex(gs
->identifier
));
1433 static int grep_source_load_file(struct grep_source
*gs
)
1435 const char *filename
= gs
->identifier
;
1441 if (lstat(filename
, &st
) < 0) {
1443 if (errno
!= ENOENT
)
1444 error(_("'%s': %s"), filename
, strerror(errno
));
1447 if (!S_ISREG(st
.st_mode
))
1449 size
= xsize_t(st
.st_size
);
1450 i
= open(filename
, O_RDONLY
);
1453 data
= xmalloc(size
+ 1);
1454 if (st
.st_size
!= read_in_full(i
, data
, size
)) {
1455 error(_("'%s': short read %s"), filename
, strerror(errno
));
1468 int grep_source_load(struct grep_source
*gs
)
1474 case GREP_SOURCE_FILE
:
1475 return grep_source_load_file(gs
);
1476 case GREP_SOURCE_SHA1
:
1477 return grep_source_load_sha1(gs
);
1478 case GREP_SOURCE_BUF
:
1479 return gs
->buf
? 0 : -1;
1481 die("BUG: invalid grep_source type");
1484 void grep_source_load_driver(struct grep_source
*gs
)
1490 gs
->driver
= userdiff_find_by_path(gs
->name
);
1492 gs
->driver
= userdiff_find_by_name("default");
1496 int grep_source_is_binary(struct grep_source
*gs
)
1498 grep_source_load_driver(gs
);
1499 if (gs
->driver
->binary
!= -1)
1500 return gs
->driver
->binary
;
1502 if (!grep_source_load(gs
))
1503 return buffer_is_binary(gs
->buf
, gs
->size
);