4 #include "xdiff-interface.h"
8 static int grep_source_load(struct grep_source
*gs
);
9 static int grep_source_is_binary(struct grep_source
*gs
);
11 static struct grep_opt grep_defaults
;
14 * Initialize the grep_defaults template with hardcoded defaults.
15 * We could let the compiler do this, but without C99 initializers
16 * the code gets unwieldy and unreadable, so...
18 void init_grep_defaults(void)
20 struct grep_opt
*opt
= &grep_defaults
;
27 memset(opt
, 0, sizeof(*opt
));
30 opt
->regflags
= REG_NEWLINE
;
32 opt
->pattern_type_option
= GREP_PATTERN_TYPE_UNSPECIFIED
;
33 opt
->extended_regexp_option
= 0;
34 color_set(opt
->color_context
, "");
35 color_set(opt
->color_filename
, "");
36 color_set(opt
->color_function
, "");
37 color_set(opt
->color_lineno
, "");
38 color_set(opt
->color_match_context
, GIT_COLOR_BOLD_RED
);
39 color_set(opt
->color_match_selected
, GIT_COLOR_BOLD_RED
);
40 color_set(opt
->color_selected
, "");
41 color_set(opt
->color_sep
, GIT_COLOR_CYAN
);
45 static int parse_pattern_type_arg(const char *opt
, const char *arg
)
47 if (!strcmp(arg
, "default"))
48 return GREP_PATTERN_TYPE_UNSPECIFIED
;
49 else if (!strcmp(arg
, "basic"))
50 return GREP_PATTERN_TYPE_BRE
;
51 else if (!strcmp(arg
, "extended"))
52 return GREP_PATTERN_TYPE_ERE
;
53 else if (!strcmp(arg
, "fixed"))
54 return GREP_PATTERN_TYPE_FIXED
;
55 else if (!strcmp(arg
, "perl"))
56 return GREP_PATTERN_TYPE_PCRE
;
57 die("bad %s argument: %s", opt
, arg
);
61 * Read the configuration file once and store it in
62 * the grep_defaults template.
64 int grep_config(const char *var
, const char *value
, void *cb
)
66 struct grep_opt
*opt
= &grep_defaults
;
69 if (userdiff_config(var
, value
) < 0)
72 if (!strcmp(var
, "grep.extendedregexp")) {
73 if (git_config_bool(var
, value
))
74 opt
->extended_regexp_option
= 1;
76 opt
->extended_regexp_option
= 0;
80 if (!strcmp(var
, "grep.patterntype")) {
81 opt
->pattern_type_option
= parse_pattern_type_arg(var
, value
);
85 if (!strcmp(var
, "grep.linenumber")) {
86 opt
->linenum
= git_config_bool(var
, value
);
90 if (!strcmp(var
, "grep.fullname")) {
91 opt
->relative
= !git_config_bool(var
, value
);
95 if (!strcmp(var
, "color.grep"))
96 opt
->color
= git_config_colorbool(var
, value
);
97 else if (!strcmp(var
, "color.grep.context"))
98 color
= opt
->color_context
;
99 else if (!strcmp(var
, "color.grep.filename"))
100 color
= opt
->color_filename
;
101 else if (!strcmp(var
, "color.grep.function"))
102 color
= opt
->color_function
;
103 else if (!strcmp(var
, "color.grep.linenumber"))
104 color
= opt
->color_lineno
;
105 else if (!strcmp(var
, "color.grep.matchcontext"))
106 color
= opt
->color_match_context
;
107 else if (!strcmp(var
, "color.grep.matchselected"))
108 color
= opt
->color_match_selected
;
109 else if (!strcmp(var
, "color.grep.selected"))
110 color
= opt
->color_selected
;
111 else if (!strcmp(var
, "color.grep.separator"))
112 color
= opt
->color_sep
;
113 else if (!strcmp(var
, "color.grep.match")) {
116 return config_error_nonbool(var
);
117 rc
|= color_parse(value
, opt
->color_match_context
);
118 rc
|= color_parse(value
, opt
->color_match_selected
);
124 return config_error_nonbool(var
);
125 return color_parse(value
, color
);
131 * Initialize one instance of grep_opt and copy the
132 * default values from the template we read the configuration
133 * information in an earlier call to git_config(grep_config).
135 void grep_init(struct grep_opt
*opt
, const char *prefix
)
137 struct grep_opt
*def
= &grep_defaults
;
139 memset(opt
, 0, sizeof(*opt
));
140 opt
->prefix
= prefix
;
141 opt
->prefix_length
= (prefix
&& *prefix
) ? strlen(prefix
) : 0;
142 opt
->pattern_tail
= &opt
->pattern_list
;
143 opt
->header_tail
= &opt
->header_list
;
145 opt
->color
= def
->color
;
146 opt
->extended_regexp_option
= def
->extended_regexp_option
;
147 opt
->pattern_type_option
= def
->pattern_type_option
;
148 opt
->linenum
= def
->linenum
;
149 opt
->max_depth
= def
->max_depth
;
150 opt
->pathname
= def
->pathname
;
151 opt
->regflags
= def
->regflags
;
152 opt
->relative
= def
->relative
;
154 color_set(opt
->color_context
, def
->color_context
);
155 color_set(opt
->color_filename
, def
->color_filename
);
156 color_set(opt
->color_function
, def
->color_function
);
157 color_set(opt
->color_lineno
, def
->color_lineno
);
158 color_set(opt
->color_match_context
, def
->color_match_context
);
159 color_set(opt
->color_match_selected
, def
->color_match_selected
);
160 color_set(opt
->color_selected
, def
->color_selected
);
161 color_set(opt
->color_sep
, def
->color_sep
);
164 void grep_commit_pattern_type(enum grep_pattern_type pattern_type
, struct grep_opt
*opt
)
166 if (pattern_type
!= GREP_PATTERN_TYPE_UNSPECIFIED
)
167 grep_set_pattern_type_option(pattern_type
, opt
);
168 else if (opt
->pattern_type_option
!= GREP_PATTERN_TYPE_UNSPECIFIED
)
169 grep_set_pattern_type_option(opt
->pattern_type_option
, opt
);
170 else if (opt
->extended_regexp_option
)
171 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE
, opt
);
174 void grep_set_pattern_type_option(enum grep_pattern_type pattern_type
, struct grep_opt
*opt
)
176 switch (pattern_type
) {
177 case GREP_PATTERN_TYPE_UNSPECIFIED
:
180 case GREP_PATTERN_TYPE_BRE
:
183 opt
->regflags
&= ~REG_EXTENDED
;
186 case GREP_PATTERN_TYPE_ERE
:
189 opt
->regflags
|= REG_EXTENDED
;
192 case GREP_PATTERN_TYPE_FIXED
:
195 opt
->regflags
&= ~REG_EXTENDED
;
198 case GREP_PATTERN_TYPE_PCRE
:
201 opt
->regflags
&= ~REG_EXTENDED
;
206 static struct grep_pat
*create_grep_pat(const char *pat
, size_t patlen
,
207 const char *origin
, int no
,
208 enum grep_pat_token t
,
209 enum grep_header_field field
)
211 struct grep_pat
*p
= xcalloc(1, sizeof(*p
));
212 p
->pattern
= xmemdupz(pat
, patlen
);
213 p
->patternlen
= patlen
;
221 static void do_append_grep_pat(struct grep_pat
***tail
, struct grep_pat
*p
)
228 case GREP_PATTERN
: /* atom */
229 case GREP_PATTERN_HEAD
:
230 case GREP_PATTERN_BODY
:
232 struct grep_pat
*new_pat
;
234 char *cp
= p
->pattern
+ p
->patternlen
, *nl
= NULL
;
235 while (++len
<= p
->patternlen
) {
236 if (*(--cp
) == '\n') {
243 new_pat
= create_grep_pat(nl
+ 1, len
- 1, p
->origin
,
244 p
->no
, p
->token
, p
->field
);
245 new_pat
->next
= p
->next
;
247 *tail
= &new_pat
->next
;
250 p
->patternlen
-= len
;
258 void append_header_grep_pattern(struct grep_opt
*opt
,
259 enum grep_header_field field
, const char *pat
)
261 struct grep_pat
*p
= create_grep_pat(pat
, strlen(pat
), "header", 0,
262 GREP_PATTERN_HEAD
, field
);
263 if (field
== GREP_HEADER_REFLOG
)
264 opt
->use_reflog_filter
= 1;
265 do_append_grep_pat(&opt
->header_tail
, p
);
268 void append_grep_pattern(struct grep_opt
*opt
, const char *pat
,
269 const char *origin
, int no
, enum grep_pat_token t
)
271 append_grep_pat(opt
, pat
, strlen(pat
), origin
, no
, t
);
274 void append_grep_pat(struct grep_opt
*opt
, const char *pat
, size_t patlen
,
275 const char *origin
, int no
, enum grep_pat_token t
)
277 struct grep_pat
*p
= create_grep_pat(pat
, patlen
, origin
, no
, t
, 0);
278 do_append_grep_pat(&opt
->pattern_tail
, p
);
281 struct grep_opt
*grep_opt_dup(const struct grep_opt
*opt
)
283 struct grep_pat
*pat
;
284 struct grep_opt
*ret
= xmalloc(sizeof(struct grep_opt
));
287 ret
->pattern_list
= NULL
;
288 ret
->pattern_tail
= &ret
->pattern_list
;
290 for(pat
= opt
->pattern_list
; pat
!= NULL
; pat
= pat
->next
)
292 if(pat
->token
== GREP_PATTERN_HEAD
)
293 append_header_grep_pattern(ret
, pat
->field
,
296 append_grep_pat(ret
, pat
->pattern
, pat
->patternlen
,
297 pat
->origin
, pat
->no
, pat
->token
);
303 static NORETURN
void compile_regexp_failed(const struct grep_pat
*p
,
309 xsnprintf(where
, sizeof(where
), "In '%s' at %d, ", p
->origin
, p
->no
);
311 xsnprintf(where
, sizeof(where
), "%s, ", p
->origin
);
315 die("%s'%s': %s", where
, p
->pattern
, error
);
319 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
323 int options
= PCRE_MULTILINE
;
325 if (opt
->ignore_case
)
326 options
|= PCRE_CASELESS
;
328 p
->pcre_regexp
= pcre_compile(p
->pattern
, options
, &error
, &erroffset
,
331 compile_regexp_failed(p
, error
);
333 p
->pcre_extra_info
= pcre_study(p
->pcre_regexp
, 0, &error
);
334 if (!p
->pcre_extra_info
&& error
)
338 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
339 regmatch_t
*match
, int eflags
)
341 int ovector
[30], ret
, flags
= 0;
343 if (eflags
& REG_NOTBOL
)
344 flags
|= PCRE_NOTBOL
;
346 ret
= pcre_exec(p
->pcre_regexp
, p
->pcre_extra_info
, line
, eol
- line
,
347 0, flags
, ovector
, ARRAY_SIZE(ovector
));
348 if (ret
< 0 && ret
!= PCRE_ERROR_NOMATCH
)
349 die("pcre_exec failed with error code %d", ret
);
352 match
->rm_so
= ovector
[0];
353 match
->rm_eo
= ovector
[1];
359 static void free_pcre_regexp(struct grep_pat
*p
)
361 pcre_free(p
->pcre_regexp
);
362 pcre_free(p
->pcre_extra_info
);
364 #else /* !USE_LIBPCRE */
365 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
367 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
370 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
371 regmatch_t
*match
, int eflags
)
376 static void free_pcre_regexp(struct grep_pat
*p
)
379 #endif /* !USE_LIBPCRE */
381 static int is_fixed(const char *s
, size_t len
)
385 /* regcomp cannot accept patterns with NULs so we
386 * consider any pattern containing a NUL fixed.
388 if (memchr(s
, 0, len
))
391 for (i
= 0; i
< len
; i
++) {
392 if (is_regex_special(s
[i
]))
399 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
403 p
->word_regexp
= opt
->word_regexp
;
404 p
->ignore_case
= opt
->ignore_case
;
406 if (opt
->fixed
|| is_fixed(p
->pattern
, p
->patternlen
))
412 if (opt
->regflags
& REG_ICASE
|| p
->ignore_case
)
413 p
->kws
= kwsalloc(tolower_trans_tbl
);
415 p
->kws
= kwsalloc(NULL
);
416 kwsincr(p
->kws
, p
->pattern
, p
->patternlen
);
422 compile_pcre_regexp(p
, opt
);
426 err
= regcomp(&p
->regexp
, p
->pattern
, opt
->regflags
);
429 regerror(err
, &p
->regexp
, errbuf
, 1024);
431 compile_regexp_failed(p
, errbuf
);
435 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
436 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
445 case GREP_PATTERN
: /* atom */
446 case GREP_PATTERN_HEAD
:
447 case GREP_PATTERN_BODY
:
448 x
= xcalloc(1, sizeof (struct grep_expr
));
449 x
->node
= GREP_NODE_ATOM
;
453 case GREP_OPEN_PAREN
:
455 x
= compile_pattern_or(list
);
456 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
457 die("unmatched parenthesis");
458 *list
= (*list
)->next
;
465 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
476 die("--not not followed by pattern expression");
478 x
= xcalloc(1, sizeof (struct grep_expr
));
479 x
->node
= GREP_NODE_NOT
;
480 x
->u
.unary
= compile_pattern_not(list
);
482 die("--not followed by non pattern expression");
485 return compile_pattern_atom(list
);
489 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
492 struct grep_expr
*x
, *y
, *z
;
494 x
= compile_pattern_not(list
);
496 if (p
&& p
->token
== GREP_AND
) {
498 die("--and not followed by pattern expression");
500 y
= compile_pattern_and(list
);
502 die("--and not followed by pattern expression");
503 z
= xcalloc(1, sizeof (struct grep_expr
));
504 z
->node
= GREP_NODE_AND
;
505 z
->u
.binary
.left
= x
;
506 z
->u
.binary
.right
= y
;
512 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
515 struct grep_expr
*x
, *y
, *z
;
517 x
= compile_pattern_and(list
);
519 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
520 y
= compile_pattern_or(list
);
522 die("not a pattern expression %s", p
->pattern
);
523 z
= xcalloc(1, sizeof (struct grep_expr
));
524 z
->node
= GREP_NODE_OR
;
525 z
->u
.binary
.left
= x
;
526 z
->u
.binary
.right
= y
;
532 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
534 return compile_pattern_or(list
);
537 static void indent(int in
)
543 static void dump_grep_pat(struct grep_pat
*p
)
546 case GREP_AND
: fprintf(stderr
, "*and*"); break;
547 case GREP_OPEN_PAREN
: fprintf(stderr
, "*(*"); break;
548 case GREP_CLOSE_PAREN
: fprintf(stderr
, "*)*"); break;
549 case GREP_NOT
: fprintf(stderr
, "*not*"); break;
550 case GREP_OR
: fprintf(stderr
, "*or*"); break;
552 case GREP_PATTERN
: fprintf(stderr
, "pattern"); break;
553 case GREP_PATTERN_HEAD
: fprintf(stderr
, "pattern_head"); break;
554 case GREP_PATTERN_BODY
: fprintf(stderr
, "pattern_body"); break;
559 case GREP_PATTERN_HEAD
:
560 fprintf(stderr
, "<head %d>", p
->field
); break;
561 case GREP_PATTERN_BODY
:
562 fprintf(stderr
, "<body>"); break;
566 case GREP_PATTERN_HEAD
:
567 case GREP_PATTERN_BODY
:
569 fprintf(stderr
, "%.*s", (int)p
->patternlen
, p
->pattern
);
575 static void dump_grep_expression_1(struct grep_expr
*x
, int in
)
580 fprintf(stderr
, "true\n");
583 dump_grep_pat(x
->u
.atom
);
586 fprintf(stderr
, "(not\n");
587 dump_grep_expression_1(x
->u
.unary
, in
+1);
589 fprintf(stderr
, ")\n");
592 fprintf(stderr
, "(and\n");
593 dump_grep_expression_1(x
->u
.binary
.left
, in
+1);
594 dump_grep_expression_1(x
->u
.binary
.right
, in
+1);
596 fprintf(stderr
, ")\n");
599 fprintf(stderr
, "(or\n");
600 dump_grep_expression_1(x
->u
.binary
.left
, in
+1);
601 dump_grep_expression_1(x
->u
.binary
.right
, in
+1);
603 fprintf(stderr
, ")\n");
608 static void dump_grep_expression(struct grep_opt
*opt
)
610 struct grep_expr
*x
= opt
->pattern_expression
;
613 fprintf(stderr
, "[all-match]\n");
614 dump_grep_expression_1(x
, 0);
618 static struct grep_expr
*grep_true_expr(void)
620 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
621 z
->node
= GREP_NODE_TRUE
;
625 static struct grep_expr
*grep_or_expr(struct grep_expr
*left
, struct grep_expr
*right
)
627 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
628 z
->node
= GREP_NODE_OR
;
629 z
->u
.binary
.left
= left
;
630 z
->u
.binary
.right
= right
;
634 static struct grep_expr
*prep_header_patterns(struct grep_opt
*opt
)
637 struct grep_expr
*header_expr
;
638 struct grep_expr
*(header_group
[GREP_HEADER_FIELD_MAX
]);
639 enum grep_header_field fld
;
641 if (!opt
->header_list
)
644 for (p
= opt
->header_list
; p
; p
= p
->next
) {
645 if (p
->token
!= GREP_PATTERN_HEAD
)
646 die("bug: a non-header pattern in grep header list.");
647 if (p
->field
< GREP_HEADER_FIELD_MIN
||
648 GREP_HEADER_FIELD_MAX
<= p
->field
)
649 die("bug: unknown header field %d", p
->field
);
650 compile_regexp(p
, opt
);
653 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++)
654 header_group
[fld
] = NULL
;
656 for (p
= opt
->header_list
; p
; p
= p
->next
) {
658 struct grep_pat
*pp
= p
;
660 h
= compile_pattern_atom(&pp
);
661 if (!h
|| pp
!= p
->next
)
662 die("bug: malformed header expr");
663 if (!header_group
[p
->field
]) {
664 header_group
[p
->field
] = h
;
667 header_group
[p
->field
] = grep_or_expr(h
, header_group
[p
->field
]);
672 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++) {
673 if (!header_group
[fld
])
676 header_expr
= grep_true_expr();
677 header_expr
= grep_or_expr(header_group
[fld
], header_expr
);
682 static struct grep_expr
*grep_splice_or(struct grep_expr
*x
, struct grep_expr
*y
)
684 struct grep_expr
*z
= x
;
687 assert(x
->node
== GREP_NODE_OR
);
688 if (x
->u
.binary
.right
&&
689 x
->u
.binary
.right
->node
== GREP_NODE_TRUE
) {
690 x
->u
.binary
.right
= y
;
693 x
= x
->u
.binary
.right
;
698 static void compile_grep_patterns_real(struct grep_opt
*opt
)
701 struct grep_expr
*header_expr
= prep_header_patterns(opt
);
703 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
705 case GREP_PATTERN
: /* atom */
706 case GREP_PATTERN_HEAD
:
707 case GREP_PATTERN_BODY
:
708 compile_regexp(p
, opt
);
716 if (opt
->all_match
|| header_expr
)
718 else if (!opt
->extended
&& !opt
->debug
)
721 p
= opt
->pattern_list
;
723 opt
->pattern_expression
= compile_pattern_expr(&p
);
725 die("incomplete pattern expression: %s", p
->pattern
);
730 if (!opt
->pattern_expression
)
731 opt
->pattern_expression
= header_expr
;
732 else if (opt
->all_match
)
733 opt
->pattern_expression
= grep_splice_or(header_expr
,
734 opt
->pattern_expression
);
736 opt
->pattern_expression
= grep_or_expr(opt
->pattern_expression
,
741 void compile_grep_patterns(struct grep_opt
*opt
)
743 compile_grep_patterns_real(opt
);
745 dump_grep_expression(opt
);
748 static void free_pattern_expr(struct grep_expr
*x
)
755 free_pattern_expr(x
->u
.unary
);
759 free_pattern_expr(x
->u
.binary
.left
);
760 free_pattern_expr(x
->u
.binary
.right
);
766 void free_grep_patterns(struct grep_opt
*opt
)
768 struct grep_pat
*p
, *n
;
770 for (p
= opt
->pattern_list
; p
; p
= n
) {
773 case GREP_PATTERN
: /* atom */
774 case GREP_PATTERN_HEAD
:
775 case GREP_PATTERN_BODY
:
778 else if (p
->pcre_regexp
)
792 free_pattern_expr(opt
->pattern_expression
);
795 static char *end_of_line(char *cp
, unsigned long *left
)
797 unsigned long l
= *left
;
798 while (l
&& *cp
!= '\n') {
806 static int word_char(char ch
)
808 return isalnum(ch
) || ch
== '_';
811 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
814 if (want_color(opt
->color
) && color
&& color
[0]) {
815 opt
->output(opt
, color
, strlen(color
));
816 opt
->output(opt
, data
, size
);
817 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
819 opt
->output(opt
, data
, size
);
822 static void output_sep(struct grep_opt
*opt
, char sign
)
824 if (opt
->null_following_name
)
825 opt
->output(opt
, "\0", 1);
827 output_color(opt
, &sign
, 1, opt
->color_sep
);
830 static void show_name(struct grep_opt
*opt
, const char *name
)
832 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
833 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
836 static int fixmatch(struct grep_pat
*p
, char *line
, char *eol
,
839 struct kwsmatch kwsm
;
840 size_t offset
= kwsexec(p
->kws
, line
, eol
- line
, &kwsm
);
842 match
->rm_so
= match
->rm_eo
= -1;
845 match
->rm_so
= offset
;
846 match
->rm_eo
= match
->rm_so
+ kwsm
.size
[0];
851 static int regmatch(const regex_t
*preg
, char *line
, char *eol
,
852 regmatch_t
*match
, int eflags
)
856 match
->rm_eo
= eol
- line
;
857 eflags
|= REG_STARTEND
;
859 return regexec(preg
, line
, 1, match
, eflags
);
862 static int patmatch(struct grep_pat
*p
, char *line
, char *eol
,
863 regmatch_t
*match
, int eflags
)
868 hit
= !fixmatch(p
, line
, eol
, match
);
869 else if (p
->pcre_regexp
)
870 hit
= !pcrematch(p
, line
, eol
, match
, eflags
);
872 hit
= !regmatch(&p
->regexp
, line
, eol
, match
, eflags
);
877 static int strip_timestamp(char *bol
, char **eol_p
)
882 while (bol
< --eol
) {
898 { "committer ", 10 },
902 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
903 enum grep_context ctx
,
904 regmatch_t
*pmatch
, int eflags
)
908 const char *start
= bol
;
910 if ((p
->token
!= GREP_PATTERN
) &&
911 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
914 if (p
->token
== GREP_PATTERN_HEAD
) {
917 assert(p
->field
< ARRAY_SIZE(header_field
));
918 field
= header_field
[p
->field
].field
;
919 len
= header_field
[p
->field
].len
;
920 if (strncmp(bol
, field
, len
))
924 case GREP_HEADER_AUTHOR
:
925 case GREP_HEADER_COMMITTER
:
926 saved_ch
= strip_timestamp(bol
, &eol
);
934 hit
= patmatch(p
, bol
, eol
, pmatch
, eflags
);
936 if (hit
&& p
->word_regexp
) {
937 if ((pmatch
[0].rm_so
< 0) ||
938 (eol
- bol
) < pmatch
[0].rm_so
||
939 (pmatch
[0].rm_eo
< 0) ||
940 (eol
- bol
) < pmatch
[0].rm_eo
)
941 die("regexp returned nonsense");
943 /* Match beginning must be either beginning of the
944 * line, or at word boundary (i.e. the last char must
945 * not be a word char). Similarly, match end must be
946 * either end of the line, or at word boundary
947 * (i.e. the next char must not be a word char).
949 if ( ((pmatch
[0].rm_so
== 0) ||
950 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
951 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
952 !word_char(bol
[pmatch
[0].rm_eo
])) )
957 /* Words consist of at least one character. */
958 if (pmatch
->rm_so
== pmatch
->rm_eo
)
961 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
962 /* There could be more than one match on the
963 * line, and the first match might not be
964 * strict word match. But later ones could be!
965 * Forward to the next possible start, i.e. the
966 * next position following a non-word char.
968 bol
= pmatch
[0].rm_so
+ bol
+ 1;
969 while (word_char(bol
[-1]) && bol
< eol
)
971 eflags
|= REG_NOTBOL
;
976 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
979 pmatch
[0].rm_so
+= bol
- start
;
980 pmatch
[0].rm_eo
+= bol
- start
;
985 static int match_expr_eval(struct grep_expr
*x
, char *bol
, char *eol
,
986 enum grep_context ctx
, int collect_hits
)
992 die("Not a valid grep expression");
998 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
, &match
, 0);
1001 h
= !match_expr_eval(x
->u
.unary
, bol
, eol
, ctx
, 0);
1004 if (!match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0))
1006 h
= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
1010 return (match_expr_eval(x
->u
.binary
.left
,
1011 bol
, eol
, ctx
, 0) ||
1012 match_expr_eval(x
->u
.binary
.right
,
1014 h
= match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
1015 x
->u
.binary
.left
->hit
|= h
;
1016 h
|= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
1019 die("Unexpected node type (internal error) %d", x
->node
);
1026 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
1027 enum grep_context ctx
, int collect_hits
)
1029 struct grep_expr
*x
= opt
->pattern_expression
;
1030 return match_expr_eval(x
, bol
, eol
, ctx
, collect_hits
);
1033 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
1034 enum grep_context ctx
, int collect_hits
)
1040 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
1042 /* we do not call with collect_hits without being extended */
1043 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1044 if (match_one_pattern(p
, bol
, eol
, ctx
, &match
, 0))
1050 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
1051 enum grep_context ctx
,
1052 regmatch_t
*pmatch
, int eflags
)
1056 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
1058 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
1060 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
1061 if (match
.rm_so
> pmatch
->rm_so
)
1063 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
1066 pmatch
->rm_so
= match
.rm_so
;
1067 pmatch
->rm_eo
= match
.rm_eo
;
1071 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
1072 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
1077 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
1079 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1081 case GREP_PATTERN
: /* atom */
1082 case GREP_PATTERN_HEAD
:
1083 case GREP_PATTERN_BODY
:
1084 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
1095 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
1096 const char *name
, unsigned lno
, char sign
)
1098 int rest
= eol
- bol
;
1099 const char *match_color
, *line_color
= NULL
;
1101 if (opt
->file_break
&& opt
->last_shown
== 0) {
1102 if (opt
->show_hunk_mark
)
1103 opt
->output(opt
, "\n", 1);
1104 } else if (opt
->pre_context
|| opt
->post_context
|| opt
->funcbody
) {
1105 if (opt
->last_shown
== 0) {
1106 if (opt
->show_hunk_mark
) {
1107 output_color(opt
, "--", 2, opt
->color_sep
);
1108 opt
->output(opt
, "\n", 1);
1110 } else if (lno
> opt
->last_shown
+ 1) {
1111 output_color(opt
, "--", 2, opt
->color_sep
);
1112 opt
->output(opt
, "\n", 1);
1115 if (opt
->heading
&& opt
->last_shown
== 0) {
1116 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
1117 opt
->output(opt
, "\n", 1);
1119 opt
->last_shown
= lno
;
1121 if (!opt
->heading
&& opt
->pathname
) {
1122 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
1123 output_sep(opt
, sign
);
1127 snprintf(buf
, sizeof(buf
), "%d", lno
);
1128 output_color(opt
, buf
, strlen(buf
), opt
->color_lineno
);
1129 output_sep(opt
, sign
);
1133 enum grep_context ctx
= GREP_CONTEXT_BODY
;
1138 match_color
= opt
->color_match_selected
;
1140 match_color
= opt
->color_match_context
;
1142 line_color
= opt
->color_selected
;
1143 else if (sign
== '-')
1144 line_color
= opt
->color_context
;
1145 else if (sign
== '=')
1146 line_color
= opt
->color_function
;
1148 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
1149 if (match
.rm_so
== match
.rm_eo
)
1152 output_color(opt
, bol
, match
.rm_so
, line_color
);
1153 output_color(opt
, bol
+ match
.rm_so
,
1154 match
.rm_eo
- match
.rm_so
, match_color
);
1156 rest
-= match
.rm_eo
;
1157 eflags
= REG_NOTBOL
;
1161 output_color(opt
, bol
, rest
, line_color
);
1162 opt
->output(opt
, "\n", 1);
1169 * This lock protects access to the gitattributes machinery, which is
1172 pthread_mutex_t grep_attr_mutex
;
1174 static inline void grep_attr_lock(void)
1177 pthread_mutex_lock(&grep_attr_mutex
);
1180 static inline void grep_attr_unlock(void)
1183 pthread_mutex_unlock(&grep_attr_mutex
);
1187 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
1189 pthread_mutex_t grep_read_mutex
;
1192 #define grep_attr_lock()
1193 #define grep_attr_unlock()
1196 static int match_funcname(struct grep_opt
*opt
, struct grep_source
*gs
, char *bol
, char *eol
)
1198 xdemitconf_t
*xecfg
= opt
->priv
;
1199 if (xecfg
&& !xecfg
->find_func
) {
1200 grep_source_load_driver(gs
);
1201 if (gs
->driver
->funcname
.pattern
) {
1202 const struct userdiff_funcname
*pe
= &gs
->driver
->funcname
;
1203 xdiff_set_find_func(xecfg
, pe
->pattern
, pe
->cflags
);
1205 xecfg
= opt
->priv
= NULL
;
1211 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
1212 xecfg
->find_func_priv
) >= 0;
1217 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
1222 static void show_funcname_line(struct grep_opt
*opt
, struct grep_source
*gs
,
1223 char *bol
, unsigned lno
)
1225 while (bol
> gs
->buf
) {
1228 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1232 if (lno
<= opt
->last_shown
)
1235 if (match_funcname(opt
, gs
, bol
, eol
)) {
1236 show_line(opt
, bol
, eol
, gs
->name
, lno
, '=');
1242 static void show_pre_context(struct grep_opt
*opt
, struct grep_source
*gs
,
1243 char *bol
, char *end
, unsigned lno
)
1245 unsigned cur
= lno
, from
= 1, funcname_lno
= 0;
1246 int funcname_needed
= !!opt
->funcname
;
1248 if (opt
->funcbody
&& !match_funcname(opt
, gs
, bol
, end
))
1249 funcname_needed
= 2;
1251 if (opt
->pre_context
< lno
)
1252 from
= lno
- opt
->pre_context
;
1253 if (from
<= opt
->last_shown
)
1254 from
= opt
->last_shown
+ 1;
1257 while (bol
> gs
->buf
&&
1258 cur
> (funcname_needed
== 2 ? opt
->last_shown
+ 1 : from
)) {
1261 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1264 if (funcname_needed
&& match_funcname(opt
, gs
, bol
, eol
)) {
1266 funcname_needed
= 0;
1270 /* We need to look even further back to find a function signature. */
1271 if (opt
->funcname
&& funcname_needed
)
1272 show_funcname_line(opt
, gs
, bol
, cur
);
1276 char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
1278 while (*eol
!= '\n')
1280 show_line(opt
, bol
, eol
, gs
->name
, cur
, sign
);
1286 static int should_lookahead(struct grep_opt
*opt
)
1291 return 0; /* punt for too complex stuff */
1294 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1295 if (p
->token
!= GREP_PATTERN
)
1296 return 0; /* punt for "header only" and stuff */
1301 static int look_ahead(struct grep_opt
*opt
,
1302 unsigned long *left_p
,
1306 unsigned lno
= *lno_p
;
1309 char *sp
, *last_bol
;
1310 regoff_t earliest
= -1;
1312 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1316 hit
= patmatch(p
, bol
, bol
+ *left_p
, &m
, 0);
1317 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
1319 if (earliest
< 0 || m
.rm_so
< earliest
)
1324 *bol_p
= bol
+ *left_p
;
1328 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
1329 ; /* find the beginning of the line */
1332 for (sp
= bol
; sp
< last_bol
; sp
++) {
1336 *left_p
-= last_bol
- bol
;
1342 static void std_output(struct grep_opt
*opt
, const void *buf
, size_t size
)
1344 fwrite(buf
, size
, 1, stdout
);
1347 static int fill_textconv_grep(struct userdiff_driver
*driver
,
1348 struct grep_source
*gs
)
1350 struct diff_filespec
*df
;
1354 if (!driver
|| !driver
->textconv
)
1355 return grep_source_load(gs
);
1358 * The textconv interface is intimately tied to diff_filespecs, so we
1359 * have to pretend to be one. If we could unify the grep_source
1360 * and diff_filespec structs, this mess could just go away.
1362 df
= alloc_filespec(gs
->path
);
1364 case GREP_SOURCE_SHA1
:
1365 fill_filespec(df
, gs
->identifier
, 1, 0100644);
1367 case GREP_SOURCE_FILE
:
1368 fill_filespec(df
, null_sha1
, 0, 0100644);
1371 die("BUG: attempt to textconv something without a path?");
1375 * fill_textconv is not remotely thread-safe; it may load objects
1376 * behind the scenes, and it modifies the global diff tempfile
1380 size
= fill_textconv(driver
, df
, &buf
);
1385 * The normal fill_textconv usage by the diff machinery would just keep
1386 * the textconv'd buf separate from the diff_filespec. But much of the
1387 * grep code passes around a grep_source and assumes that its "buf"
1388 * pointer is the beginning of the thing we are searching. So let's
1389 * install our textconv'd version into the grep_source, taking care not
1390 * to leak any existing buffer.
1392 grep_source_clear_data(gs
);
1399 static int is_empty_line(const char *bol
, const char *eol
)
1401 while (bol
< eol
&& isspace(*bol
))
1406 static int grep_source_1(struct grep_opt
*opt
, struct grep_source
*gs
, int collect_hits
)
1409 char *peek_bol
= NULL
;
1412 unsigned last_hit
= 0;
1413 int binary_match_only
= 0;
1415 int try_lookahead
= 0;
1416 int show_function
= 0;
1417 struct userdiff_driver
*textconv
= NULL
;
1418 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
1422 opt
->output
= std_output
;
1424 if (opt
->pre_context
|| opt
->post_context
|| opt
->file_break
||
1426 /* Show hunk marks, except for the first file. */
1427 if (opt
->last_shown
)
1428 opt
->show_hunk_mark
= 1;
1430 * If we're using threads then we can't easily identify
1431 * the first file. Always put hunk marks in that case
1432 * and skip the very first one later in work_done().
1434 if (opt
->output
!= std_output
)
1435 opt
->show_hunk_mark
= 1;
1437 opt
->last_shown
= 0;
1439 if (opt
->allow_textconv
) {
1440 grep_source_load_driver(gs
);
1442 * We might set up the shared textconv cache data here, which
1443 * is not thread-safe.
1446 textconv
= userdiff_get_textconv(gs
->driver
);
1451 * We know the result of a textconv is text, so we only have to care
1452 * about binary handling if we are not using it.
1455 switch (opt
->binary
) {
1456 case GREP_BINARY_DEFAULT
:
1457 if (grep_source_is_binary(gs
))
1458 binary_match_only
= 1;
1460 case GREP_BINARY_NOMATCH
:
1461 if (grep_source_is_binary(gs
))
1462 return 0; /* Assume unmatch */
1464 case GREP_BINARY_TEXT
:
1467 die("bug: unknown binary handling mode");
1471 memset(&xecfg
, 0, sizeof(xecfg
));
1474 try_lookahead
= should_lookahead(opt
);
1476 if (fill_textconv_grep(textconv
, gs
) < 0)
1486 * look_ahead() skips quickly to the line that possibly
1487 * has the next hit; don't call it if we need to do
1488 * something more than just skipping the current line
1489 * in response to an unmatch for the current line. E.g.
1490 * inside a post-context window, we will show the current
1491 * line as a context around the previous hit when it
1496 && (show_function
||
1497 lno
<= last_hit
+ opt
->post_context
))
1498 && look_ahead(opt
, &left
, &lno
, &bol
))
1500 eol
= end_of_line(bol
, &left
);
1504 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
1505 ctx
= GREP_CONTEXT_BODY
;
1507 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
1513 /* "grep -v -e foo -e bla" should list lines
1514 * that do not have either, so inversion should
1519 if (opt
->unmatch_name_only
) {
1526 if (opt
->status_only
)
1528 if (opt
->name_only
) {
1529 show_name(opt
, gs
->name
);
1534 if (binary_match_only
) {
1535 opt
->output(opt
, "Binary file ", 12);
1536 output_color(opt
, gs
->name
, strlen(gs
->name
),
1537 opt
->color_filename
);
1538 opt
->output(opt
, " matches\n", 9);
1541 /* Hit at this line. If we haven't shown the
1542 * pre-context lines, we would need to show them.
1544 if (opt
->pre_context
|| opt
->funcbody
)
1545 show_pre_context(opt
, gs
, bol
, eol
, lno
);
1546 else if (opt
->funcname
)
1547 show_funcname_line(opt
, gs
, bol
, lno
);
1548 show_line(opt
, bol
, eol
, gs
->name
, lno
, ':');
1554 if (show_function
&& (!peek_bol
|| peek_bol
< bol
)) {
1555 unsigned long peek_left
= left
;
1556 char *peek_eol
= eol
;
1559 * Trailing empty lines are not interesting.
1560 * Peek past them to see if they belong to the
1561 * body of the current function.
1564 while (is_empty_line(peek_bol
, peek_eol
)) {
1565 peek_bol
= peek_eol
+ 1;
1566 peek_eol
= end_of_line(peek_bol
, &peek_left
);
1569 if (match_funcname(opt
, gs
, peek_bol
, peek_eol
))
1572 if (show_function
||
1573 (last_hit
&& lno
<= last_hit
+ opt
->post_context
)) {
1574 /* If the last hit is within the post context,
1575 * we need to show this line.
1577 show_line(opt
, bol
, eol
, gs
->name
, lno
, '-');
1591 if (opt
->status_only
)
1593 if (opt
->unmatch_name_only
) {
1594 /* We did not see any hit, so we want to show this */
1595 show_name(opt
, gs
->name
);
1599 xdiff_clear_find_func(&xecfg
);
1603 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1604 * which feels mostly useless but sometimes useful. Maybe
1605 * make it another option? For now suppress them.
1607 if (opt
->count
&& count
) {
1609 if (opt
->pathname
) {
1610 output_color(opt
, gs
->name
, strlen(gs
->name
),
1611 opt
->color_filename
);
1612 output_sep(opt
, ':');
1614 snprintf(buf
, sizeof(buf
), "%u\n", count
);
1615 opt
->output(opt
, buf
, strlen(buf
));
1621 static void clr_hit_marker(struct grep_expr
*x
)
1623 /* All-hit markers are meaningful only at the very top level
1628 if (x
->node
!= GREP_NODE_OR
)
1630 x
->u
.binary
.left
->hit
= 0;
1631 x
= x
->u
.binary
.right
;
1635 static int chk_hit_marker(struct grep_expr
*x
)
1637 /* Top level nodes have hit markers. See if they all are hits */
1639 if (x
->node
!= GREP_NODE_OR
)
1641 if (!x
->u
.binary
.left
->hit
)
1643 x
= x
->u
.binary
.right
;
1647 int grep_source(struct grep_opt
*opt
, struct grep_source
*gs
)
1650 * we do not have to do the two-pass grep when we do not check
1651 * buffer-wide "all-match".
1653 if (!opt
->all_match
)
1654 return grep_source_1(opt
, gs
, 0);
1656 /* Otherwise the toplevel "or" terms hit a bit differently.
1657 * We first clear hit markers from them.
1659 clr_hit_marker(opt
->pattern_expression
);
1660 grep_source_1(opt
, gs
, 1);
1662 if (!chk_hit_marker(opt
->pattern_expression
))
1665 return grep_source_1(opt
, gs
, 0);
1668 int grep_buffer(struct grep_opt
*opt
, char *buf
, unsigned long size
)
1670 struct grep_source gs
;
1673 grep_source_init(&gs
, GREP_SOURCE_BUF
, NULL
, NULL
, NULL
);
1677 r
= grep_source(opt
, &gs
);
1679 grep_source_clear(&gs
);
1683 void grep_source_init(struct grep_source
*gs
, enum grep_source_type type
,
1684 const char *name
, const char *path
,
1685 const void *identifier
)
1688 gs
->name
= xstrdup_or_null(name
);
1689 gs
->path
= xstrdup_or_null(path
);
1695 case GREP_SOURCE_FILE
:
1696 gs
->identifier
= xstrdup(identifier
);
1698 case GREP_SOURCE_SHA1
:
1699 gs
->identifier
= xmalloc(20);
1700 hashcpy(gs
->identifier
, identifier
);
1702 case GREP_SOURCE_BUF
:
1703 gs
->identifier
= NULL
;
1707 void grep_source_clear(struct grep_source
*gs
)
1713 free(gs
->identifier
);
1714 gs
->identifier
= NULL
;
1715 grep_source_clear_data(gs
);
1718 void grep_source_clear_data(struct grep_source
*gs
)
1721 case GREP_SOURCE_FILE
:
1722 case GREP_SOURCE_SHA1
:
1727 case GREP_SOURCE_BUF
:
1728 /* leave user-provided buf intact */
1733 static int grep_source_load_sha1(struct grep_source
*gs
)
1735 enum object_type type
;
1738 gs
->buf
= read_sha1_file(gs
->identifier
, &type
, &gs
->size
);
1742 return error(_("'%s': unable to read %s"),
1744 sha1_to_hex(gs
->identifier
));
1748 static int grep_source_load_file(struct grep_source
*gs
)
1750 const char *filename
= gs
->identifier
;
1756 if (lstat(filename
, &st
) < 0) {
1758 if (errno
!= ENOENT
)
1759 error(_("'%s': %s"), filename
, strerror(errno
));
1762 if (!S_ISREG(st
.st_mode
))
1764 size
= xsize_t(st
.st_size
);
1765 i
= open(filename
, O_RDONLY
);
1768 data
= xmallocz(size
);
1769 if (st
.st_size
!= read_in_full(i
, data
, size
)) {
1770 error(_("'%s': short read %s"), filename
, strerror(errno
));
1782 static int grep_source_load(struct grep_source
*gs
)
1788 case GREP_SOURCE_FILE
:
1789 return grep_source_load_file(gs
);
1790 case GREP_SOURCE_SHA1
:
1791 return grep_source_load_sha1(gs
);
1792 case GREP_SOURCE_BUF
:
1793 return gs
->buf
? 0 : -1;
1795 die("BUG: invalid grep_source type");
1798 void grep_source_load_driver(struct grep_source
*gs
)
1805 gs
->driver
= userdiff_find_by_path(gs
->path
);
1807 gs
->driver
= userdiff_find_by_name("default");
1811 static int grep_source_is_binary(struct grep_source
*gs
)
1813 grep_source_load_driver(gs
);
1814 if (gs
->driver
->binary
!= -1)
1815 return gs
->driver
->binary
;
1817 if (!grep_source_load(gs
))
1818 return buffer_is_binary(gs
->buf
, gs
->size
);