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 strcpy(opt
->color_context
, "");
35 strcpy(opt
->color_filename
, "");
36 strcpy(opt
->color_function
, "");
37 strcpy(opt
->color_lineno
, "");
38 strcpy(opt
->color_match_context
, GIT_COLOR_BOLD_RED
);
39 strcpy(opt
->color_match_selected
, GIT_COLOR_BOLD_RED
);
40 strcpy(opt
->color_selected
, "");
41 strcpy(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
, "color.grep"))
91 opt
->color
= git_config_colorbool(var
, value
);
92 else if (!strcmp(var
, "color.grep.context"))
93 color
= opt
->color_context
;
94 else if (!strcmp(var
, "color.grep.filename"))
95 color
= opt
->color_filename
;
96 else if (!strcmp(var
, "color.grep.function"))
97 color
= opt
->color_function
;
98 else if (!strcmp(var
, "color.grep.linenumber"))
99 color
= opt
->color_lineno
;
100 else if (!strcmp(var
, "color.grep.matchcontext"))
101 color
= opt
->color_match_context
;
102 else if (!strcmp(var
, "color.grep.matchselected"))
103 color
= opt
->color_match_selected
;
104 else if (!strcmp(var
, "color.grep.selected"))
105 color
= opt
->color_selected
;
106 else if (!strcmp(var
, "color.grep.separator"))
107 color
= opt
->color_sep
;
108 else if (!strcmp(var
, "color.grep.match")) {
111 return config_error_nonbool(var
);
112 color_parse(value
, var
, opt
->color_match_context
);
113 color_parse(value
, var
, opt
->color_match_selected
);
119 return config_error_nonbool(var
);
120 color_parse(value
, var
, color
);
126 * Initialize one instance of grep_opt and copy the
127 * default values from the template we read the configuration
128 * information in an earlier call to git_config(grep_config).
130 void grep_init(struct grep_opt
*opt
, const char *prefix
)
132 struct grep_opt
*def
= &grep_defaults
;
134 memset(opt
, 0, sizeof(*opt
));
135 opt
->prefix
= prefix
;
136 opt
->prefix_length
= (prefix
&& *prefix
) ? strlen(prefix
) : 0;
137 opt
->pattern_tail
= &opt
->pattern_list
;
138 opt
->header_tail
= &opt
->header_list
;
140 opt
->color
= def
->color
;
141 opt
->extended_regexp_option
= def
->extended_regexp_option
;
142 opt
->pattern_type_option
= def
->pattern_type_option
;
143 opt
->linenum
= def
->linenum
;
144 opt
->max_depth
= def
->max_depth
;
145 opt
->pathname
= def
->pathname
;
146 opt
->regflags
= def
->regflags
;
147 opt
->relative
= def
->relative
;
149 strcpy(opt
->color_context
, def
->color_context
);
150 strcpy(opt
->color_filename
, def
->color_filename
);
151 strcpy(opt
->color_function
, def
->color_function
);
152 strcpy(opt
->color_lineno
, def
->color_lineno
);
153 strcpy(opt
->color_match_context
, def
->color_match_context
);
154 strcpy(opt
->color_match_selected
, def
->color_match_selected
);
155 strcpy(opt
->color_selected
, def
->color_selected
);
156 strcpy(opt
->color_sep
, def
->color_sep
);
159 void grep_commit_pattern_type(enum grep_pattern_type pattern_type
, struct grep_opt
*opt
)
161 if (pattern_type
!= GREP_PATTERN_TYPE_UNSPECIFIED
)
162 grep_set_pattern_type_option(pattern_type
, opt
);
163 else if (opt
->pattern_type_option
!= GREP_PATTERN_TYPE_UNSPECIFIED
)
164 grep_set_pattern_type_option(opt
->pattern_type_option
, opt
);
165 else if (opt
->extended_regexp_option
)
166 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE
, opt
);
169 void grep_set_pattern_type_option(enum grep_pattern_type pattern_type
, struct grep_opt
*opt
)
171 switch (pattern_type
) {
172 case GREP_PATTERN_TYPE_UNSPECIFIED
:
175 case GREP_PATTERN_TYPE_BRE
:
178 opt
->regflags
&= ~REG_EXTENDED
;
181 case GREP_PATTERN_TYPE_ERE
:
184 opt
->regflags
|= REG_EXTENDED
;
187 case GREP_PATTERN_TYPE_FIXED
:
190 opt
->regflags
&= ~REG_EXTENDED
;
193 case GREP_PATTERN_TYPE_PCRE
:
196 opt
->regflags
&= ~REG_EXTENDED
;
201 static struct grep_pat
*create_grep_pat(const char *pat
, size_t patlen
,
202 const char *origin
, int no
,
203 enum grep_pat_token t
,
204 enum grep_header_field field
)
206 struct grep_pat
*p
= xcalloc(1, sizeof(*p
));
207 p
->pattern
= xmemdupz(pat
, patlen
);
208 p
->patternlen
= patlen
;
216 static void do_append_grep_pat(struct grep_pat
***tail
, struct grep_pat
*p
)
223 case GREP_PATTERN
: /* atom */
224 case GREP_PATTERN_HEAD
:
225 case GREP_PATTERN_BODY
:
227 struct grep_pat
*new_pat
;
229 char *cp
= p
->pattern
+ p
->patternlen
, *nl
= NULL
;
230 while (++len
<= p
->patternlen
) {
231 if (*(--cp
) == '\n') {
238 new_pat
= create_grep_pat(nl
+ 1, len
- 1, p
->origin
,
239 p
->no
, p
->token
, p
->field
);
240 new_pat
->next
= p
->next
;
242 *tail
= &new_pat
->next
;
245 p
->patternlen
-= len
;
253 void append_header_grep_pattern(struct grep_opt
*opt
,
254 enum grep_header_field field
, const char *pat
)
256 struct grep_pat
*p
= create_grep_pat(pat
, strlen(pat
), "header", 0,
257 GREP_PATTERN_HEAD
, field
);
258 if (field
== GREP_HEADER_REFLOG
)
259 opt
->use_reflog_filter
= 1;
260 do_append_grep_pat(&opt
->header_tail
, p
);
263 void append_grep_pattern(struct grep_opt
*opt
, const char *pat
,
264 const char *origin
, int no
, enum grep_pat_token t
)
266 append_grep_pat(opt
, pat
, strlen(pat
), origin
, no
, t
);
269 void append_grep_pat(struct grep_opt
*opt
, const char *pat
, size_t patlen
,
270 const char *origin
, int no
, enum grep_pat_token t
)
272 struct grep_pat
*p
= create_grep_pat(pat
, patlen
, origin
, no
, t
, 0);
273 do_append_grep_pat(&opt
->pattern_tail
, p
);
276 struct grep_opt
*grep_opt_dup(const struct grep_opt
*opt
)
278 struct grep_pat
*pat
;
279 struct grep_opt
*ret
= xmalloc(sizeof(struct grep_opt
));
282 ret
->pattern_list
= NULL
;
283 ret
->pattern_tail
= &ret
->pattern_list
;
285 for(pat
= opt
->pattern_list
; pat
!= NULL
; pat
= pat
->next
)
287 if(pat
->token
== GREP_PATTERN_HEAD
)
288 append_header_grep_pattern(ret
, pat
->field
,
291 append_grep_pat(ret
, pat
->pattern
, pat
->patternlen
,
292 pat
->origin
, pat
->no
, pat
->token
);
298 static NORETURN
void compile_regexp_failed(const struct grep_pat
*p
,
304 sprintf(where
, "In '%s' at %d, ", p
->origin
, p
->no
);
306 sprintf(where
, "%s, ", p
->origin
);
310 die("%s'%s': %s", where
, p
->pattern
, error
);
314 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
318 int options
= PCRE_MULTILINE
;
320 if (opt
->ignore_case
)
321 options
|= PCRE_CASELESS
;
323 p
->pcre_regexp
= pcre_compile(p
->pattern
, options
, &error
, &erroffset
,
326 compile_regexp_failed(p
, error
);
328 p
->pcre_extra_info
= pcre_study(p
->pcre_regexp
, 0, &error
);
329 if (!p
->pcre_extra_info
&& error
)
333 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
334 regmatch_t
*match
, int eflags
)
336 int ovector
[30], ret
, flags
= 0;
338 if (eflags
& REG_NOTBOL
)
339 flags
|= PCRE_NOTBOL
;
341 ret
= pcre_exec(p
->pcre_regexp
, p
->pcre_extra_info
, line
, eol
- line
,
342 0, flags
, ovector
, ARRAY_SIZE(ovector
));
343 if (ret
< 0 && ret
!= PCRE_ERROR_NOMATCH
)
344 die("pcre_exec failed with error code %d", ret
);
347 match
->rm_so
= ovector
[0];
348 match
->rm_eo
= ovector
[1];
354 static void free_pcre_regexp(struct grep_pat
*p
)
356 pcre_free(p
->pcre_regexp
);
357 pcre_free(p
->pcre_extra_info
);
359 #else /* !USE_LIBPCRE */
360 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
362 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
365 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
366 regmatch_t
*match
, int eflags
)
371 static void free_pcre_regexp(struct grep_pat
*p
)
374 #endif /* !USE_LIBPCRE */
376 static int is_fixed(const char *s
, size_t len
)
380 /* regcomp cannot accept patterns with NULs so we
381 * consider any pattern containing a NUL fixed.
383 if (memchr(s
, 0, len
))
386 for (i
= 0; i
< len
; i
++) {
387 if (is_regex_special(s
[i
]))
394 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
398 p
->word_regexp
= opt
->word_regexp
;
399 p
->ignore_case
= opt
->ignore_case
;
401 if (opt
->fixed
|| is_fixed(p
->pattern
, p
->patternlen
))
407 if (opt
->regflags
& REG_ICASE
|| p
->ignore_case
)
408 p
->kws
= kwsalloc(tolower_trans_tbl
);
410 p
->kws
= kwsalloc(NULL
);
411 kwsincr(p
->kws
, p
->pattern
, p
->patternlen
);
417 compile_pcre_regexp(p
, opt
);
421 err
= regcomp(&p
->regexp
, p
->pattern
, opt
->regflags
);
424 regerror(err
, &p
->regexp
, errbuf
, 1024);
426 compile_regexp_failed(p
, errbuf
);
430 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
431 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
440 case GREP_PATTERN
: /* atom */
441 case GREP_PATTERN_HEAD
:
442 case GREP_PATTERN_BODY
:
443 x
= xcalloc(1, sizeof (struct grep_expr
));
444 x
->node
= GREP_NODE_ATOM
;
448 case GREP_OPEN_PAREN
:
450 x
= compile_pattern_or(list
);
451 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
452 die("unmatched parenthesis");
453 *list
= (*list
)->next
;
460 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
471 die("--not not followed by pattern expression");
473 x
= xcalloc(1, sizeof (struct grep_expr
));
474 x
->node
= GREP_NODE_NOT
;
475 x
->u
.unary
= compile_pattern_not(list
);
477 die("--not followed by non pattern expression");
480 return compile_pattern_atom(list
);
484 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
487 struct grep_expr
*x
, *y
, *z
;
489 x
= compile_pattern_not(list
);
491 if (p
&& p
->token
== GREP_AND
) {
493 die("--and not followed by pattern expression");
495 y
= compile_pattern_and(list
);
497 die("--and not followed by pattern expression");
498 z
= xcalloc(1, sizeof (struct grep_expr
));
499 z
->node
= GREP_NODE_AND
;
500 z
->u
.binary
.left
= x
;
501 z
->u
.binary
.right
= y
;
507 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
510 struct grep_expr
*x
, *y
, *z
;
512 x
= compile_pattern_and(list
);
514 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
515 y
= compile_pattern_or(list
);
517 die("not a pattern expression %s", p
->pattern
);
518 z
= xcalloc(1, sizeof (struct grep_expr
));
519 z
->node
= GREP_NODE_OR
;
520 z
->u
.binary
.left
= x
;
521 z
->u
.binary
.right
= y
;
527 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
529 return compile_pattern_or(list
);
532 static void indent(int in
)
538 static void dump_grep_pat(struct grep_pat
*p
)
541 case GREP_AND
: fprintf(stderr
, "*and*"); break;
542 case GREP_OPEN_PAREN
: fprintf(stderr
, "*(*"); break;
543 case GREP_CLOSE_PAREN
: fprintf(stderr
, "*)*"); break;
544 case GREP_NOT
: fprintf(stderr
, "*not*"); break;
545 case GREP_OR
: fprintf(stderr
, "*or*"); break;
547 case GREP_PATTERN
: fprintf(stderr
, "pattern"); break;
548 case GREP_PATTERN_HEAD
: fprintf(stderr
, "pattern_head"); break;
549 case GREP_PATTERN_BODY
: fprintf(stderr
, "pattern_body"); break;
554 case GREP_PATTERN_HEAD
:
555 fprintf(stderr
, "<head %d>", p
->field
); break;
556 case GREP_PATTERN_BODY
:
557 fprintf(stderr
, "<body>"); break;
561 case GREP_PATTERN_HEAD
:
562 case GREP_PATTERN_BODY
:
564 fprintf(stderr
, "%.*s", (int)p
->patternlen
, p
->pattern
);
570 static void dump_grep_expression_1(struct grep_expr
*x
, int in
)
575 fprintf(stderr
, "true\n");
578 dump_grep_pat(x
->u
.atom
);
581 fprintf(stderr
, "(not\n");
582 dump_grep_expression_1(x
->u
.unary
, in
+1);
584 fprintf(stderr
, ")\n");
587 fprintf(stderr
, "(and\n");
588 dump_grep_expression_1(x
->u
.binary
.left
, in
+1);
589 dump_grep_expression_1(x
->u
.binary
.right
, in
+1);
591 fprintf(stderr
, ")\n");
594 fprintf(stderr
, "(or\n");
595 dump_grep_expression_1(x
->u
.binary
.left
, in
+1);
596 dump_grep_expression_1(x
->u
.binary
.right
, in
+1);
598 fprintf(stderr
, ")\n");
603 static void dump_grep_expression(struct grep_opt
*opt
)
605 struct grep_expr
*x
= opt
->pattern_expression
;
608 fprintf(stderr
, "[all-match]\n");
609 dump_grep_expression_1(x
, 0);
613 static struct grep_expr
*grep_true_expr(void)
615 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
616 z
->node
= GREP_NODE_TRUE
;
620 static struct grep_expr
*grep_or_expr(struct grep_expr
*left
, struct grep_expr
*right
)
622 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
623 z
->node
= GREP_NODE_OR
;
624 z
->u
.binary
.left
= left
;
625 z
->u
.binary
.right
= right
;
629 static struct grep_expr
*prep_header_patterns(struct grep_opt
*opt
)
632 struct grep_expr
*header_expr
;
633 struct grep_expr
*(header_group
[GREP_HEADER_FIELD_MAX
]);
634 enum grep_header_field fld
;
636 if (!opt
->header_list
)
639 for (p
= opt
->header_list
; p
; p
= p
->next
) {
640 if (p
->token
!= GREP_PATTERN_HEAD
)
641 die("bug: a non-header pattern in grep header list.");
642 if (p
->field
< GREP_HEADER_FIELD_MIN
||
643 GREP_HEADER_FIELD_MAX
<= p
->field
)
644 die("bug: unknown header field %d", p
->field
);
645 compile_regexp(p
, opt
);
648 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++)
649 header_group
[fld
] = NULL
;
651 for (p
= opt
->header_list
; p
; p
= p
->next
) {
653 struct grep_pat
*pp
= p
;
655 h
= compile_pattern_atom(&pp
);
656 if (!h
|| pp
!= p
->next
)
657 die("bug: malformed header expr");
658 if (!header_group
[p
->field
]) {
659 header_group
[p
->field
] = h
;
662 header_group
[p
->field
] = grep_or_expr(h
, header_group
[p
->field
]);
667 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++) {
668 if (!header_group
[fld
])
671 header_expr
= grep_true_expr();
672 header_expr
= grep_or_expr(header_group
[fld
], header_expr
);
677 static struct grep_expr
*grep_splice_or(struct grep_expr
*x
, struct grep_expr
*y
)
679 struct grep_expr
*z
= x
;
682 assert(x
->node
== GREP_NODE_OR
);
683 if (x
->u
.binary
.right
&&
684 x
->u
.binary
.right
->node
== GREP_NODE_TRUE
) {
685 x
->u
.binary
.right
= y
;
688 x
= x
->u
.binary
.right
;
693 static void compile_grep_patterns_real(struct grep_opt
*opt
)
696 struct grep_expr
*header_expr
= prep_header_patterns(opt
);
698 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
700 case GREP_PATTERN
: /* atom */
701 case GREP_PATTERN_HEAD
:
702 case GREP_PATTERN_BODY
:
703 compile_regexp(p
, opt
);
711 if (opt
->all_match
|| header_expr
)
713 else if (!opt
->extended
&& !opt
->debug
)
716 p
= opt
->pattern_list
;
718 opt
->pattern_expression
= compile_pattern_expr(&p
);
720 die("incomplete pattern expression: %s", p
->pattern
);
725 if (!opt
->pattern_expression
)
726 opt
->pattern_expression
= header_expr
;
727 else if (opt
->all_match
)
728 opt
->pattern_expression
= grep_splice_or(header_expr
,
729 opt
->pattern_expression
);
731 opt
->pattern_expression
= grep_or_expr(opt
->pattern_expression
,
736 void compile_grep_patterns(struct grep_opt
*opt
)
738 compile_grep_patterns_real(opt
);
740 dump_grep_expression(opt
);
743 static void free_pattern_expr(struct grep_expr
*x
)
750 free_pattern_expr(x
->u
.unary
);
754 free_pattern_expr(x
->u
.binary
.left
);
755 free_pattern_expr(x
->u
.binary
.right
);
761 void free_grep_patterns(struct grep_opt
*opt
)
763 struct grep_pat
*p
, *n
;
765 for (p
= opt
->pattern_list
; p
; p
= n
) {
768 case GREP_PATTERN
: /* atom */
769 case GREP_PATTERN_HEAD
:
770 case GREP_PATTERN_BODY
:
773 else if (p
->pcre_regexp
)
787 free_pattern_expr(opt
->pattern_expression
);
790 static char *end_of_line(char *cp
, unsigned long *left
)
792 unsigned long l
= *left
;
793 while (l
&& *cp
!= '\n') {
801 static int word_char(char ch
)
803 return isalnum(ch
) || ch
== '_';
806 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
809 if (want_color(opt
->color
) && color
&& color
[0]) {
810 opt
->output(opt
, color
, strlen(color
));
811 opt
->output(opt
, data
, size
);
812 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
814 opt
->output(opt
, data
, size
);
817 static void output_sep(struct grep_opt
*opt
, char sign
)
819 if (opt
->null_following_name
)
820 opt
->output(opt
, "\0", 1);
822 output_color(opt
, &sign
, 1, opt
->color_sep
);
825 static void show_name(struct grep_opt
*opt
, const char *name
)
827 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
828 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
831 static int fixmatch(struct grep_pat
*p
, char *line
, char *eol
,
834 struct kwsmatch kwsm
;
835 size_t offset
= kwsexec(p
->kws
, line
, eol
- line
, &kwsm
);
837 match
->rm_so
= match
->rm_eo
= -1;
840 match
->rm_so
= offset
;
841 match
->rm_eo
= match
->rm_so
+ kwsm
.size
[0];
846 static int regmatch(const regex_t
*preg
, char *line
, char *eol
,
847 regmatch_t
*match
, int eflags
)
851 match
->rm_eo
= eol
- line
;
852 eflags
|= REG_STARTEND
;
854 return regexec(preg
, line
, 1, match
, eflags
);
857 static int patmatch(struct grep_pat
*p
, char *line
, char *eol
,
858 regmatch_t
*match
, int eflags
)
863 hit
= !fixmatch(p
, line
, eol
, match
);
864 else if (p
->pcre_regexp
)
865 hit
= !pcrematch(p
, line
, eol
, match
, eflags
);
867 hit
= !regmatch(&p
->regexp
, line
, eol
, match
, eflags
);
872 static int strip_timestamp(char *bol
, char **eol_p
)
877 while (bol
< --eol
) {
893 { "committer ", 10 },
897 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
898 enum grep_context ctx
,
899 regmatch_t
*pmatch
, int eflags
)
903 const char *start
= bol
;
905 if ((p
->token
!= GREP_PATTERN
) &&
906 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
909 if (p
->token
== GREP_PATTERN_HEAD
) {
912 assert(p
->field
< ARRAY_SIZE(header_field
));
913 field
= header_field
[p
->field
].field
;
914 len
= header_field
[p
->field
].len
;
915 if (strncmp(bol
, field
, len
))
919 case GREP_HEADER_AUTHOR
:
920 case GREP_HEADER_COMMITTER
:
921 saved_ch
= strip_timestamp(bol
, &eol
);
929 hit
= patmatch(p
, bol
, eol
, pmatch
, eflags
);
931 if (hit
&& p
->word_regexp
) {
932 if ((pmatch
[0].rm_so
< 0) ||
933 (eol
- bol
) < pmatch
[0].rm_so
||
934 (pmatch
[0].rm_eo
< 0) ||
935 (eol
- bol
) < pmatch
[0].rm_eo
)
936 die("regexp returned nonsense");
938 /* Match beginning must be either beginning of the
939 * line, or at word boundary (i.e. the last char must
940 * not be a word char). Similarly, match end must be
941 * either end of the line, or at word boundary
942 * (i.e. the next char must not be a word char).
944 if ( ((pmatch
[0].rm_so
== 0) ||
945 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
946 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
947 !word_char(bol
[pmatch
[0].rm_eo
])) )
952 /* Words consist of at least one character. */
953 if (pmatch
->rm_so
== pmatch
->rm_eo
)
956 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
957 /* There could be more than one match on the
958 * line, and the first match might not be
959 * strict word match. But later ones could be!
960 * Forward to the next possible start, i.e. the
961 * next position following a non-word char.
963 bol
= pmatch
[0].rm_so
+ bol
+ 1;
964 while (word_char(bol
[-1]) && bol
< eol
)
966 eflags
|= REG_NOTBOL
;
971 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
974 pmatch
[0].rm_so
+= bol
- start
;
975 pmatch
[0].rm_eo
+= bol
- start
;
980 static int match_expr_eval(struct grep_expr
*x
, char *bol
, char *eol
,
981 enum grep_context ctx
, int collect_hits
)
987 die("Not a valid grep expression");
993 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
, &match
, 0);
996 h
= !match_expr_eval(x
->u
.unary
, bol
, eol
, ctx
, 0);
999 if (!match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0))
1001 h
= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
1005 return (match_expr_eval(x
->u
.binary
.left
,
1006 bol
, eol
, ctx
, 0) ||
1007 match_expr_eval(x
->u
.binary
.right
,
1009 h
= match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
1010 x
->u
.binary
.left
->hit
|= h
;
1011 h
|= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
1014 die("Unexpected node type (internal error) %d", x
->node
);
1021 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
1022 enum grep_context ctx
, int collect_hits
)
1024 struct grep_expr
*x
= opt
->pattern_expression
;
1025 return match_expr_eval(x
, bol
, eol
, ctx
, collect_hits
);
1028 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
1029 enum grep_context ctx
, int collect_hits
)
1035 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
1037 /* we do not call with collect_hits without being extended */
1038 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1039 if (match_one_pattern(p
, bol
, eol
, ctx
, &match
, 0))
1045 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
1046 enum grep_context ctx
,
1047 regmatch_t
*pmatch
, int eflags
)
1051 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
1053 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
1055 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
1056 if (match
.rm_so
> pmatch
->rm_so
)
1058 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
1061 pmatch
->rm_so
= match
.rm_so
;
1062 pmatch
->rm_eo
= match
.rm_eo
;
1066 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
1067 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
1072 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
1074 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1076 case GREP_PATTERN
: /* atom */
1077 case GREP_PATTERN_HEAD
:
1078 case GREP_PATTERN_BODY
:
1079 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
1090 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
1091 const char *name
, unsigned lno
, char sign
)
1093 int rest
= eol
- bol
;
1094 const char *match_color
, *line_color
= NULL
;
1096 if (opt
->file_break
&& opt
->last_shown
== 0) {
1097 if (opt
->show_hunk_mark
)
1098 opt
->output(opt
, "\n", 1);
1099 } else if (opt
->pre_context
|| opt
->post_context
|| opt
->funcbody
) {
1100 if (opt
->last_shown
== 0) {
1101 if (opt
->show_hunk_mark
) {
1102 output_color(opt
, "--", 2, opt
->color_sep
);
1103 opt
->output(opt
, "\n", 1);
1105 } else if (lno
> opt
->last_shown
+ 1) {
1106 output_color(opt
, "--", 2, opt
->color_sep
);
1107 opt
->output(opt
, "\n", 1);
1110 if (opt
->heading
&& opt
->last_shown
== 0) {
1111 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
1112 opt
->output(opt
, "\n", 1);
1114 opt
->last_shown
= lno
;
1116 if (!opt
->heading
&& opt
->pathname
) {
1117 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
1118 output_sep(opt
, sign
);
1122 snprintf(buf
, sizeof(buf
), "%d", lno
);
1123 output_color(opt
, buf
, strlen(buf
), opt
->color_lineno
);
1124 output_sep(opt
, sign
);
1128 enum grep_context ctx
= GREP_CONTEXT_BODY
;
1133 match_color
= opt
->color_match_selected
;
1135 match_color
= opt
->color_match_context
;
1137 line_color
= opt
->color_selected
;
1138 else if (sign
== '-')
1139 line_color
= opt
->color_context
;
1140 else if (sign
== '=')
1141 line_color
= opt
->color_function
;
1143 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
1144 if (match
.rm_so
== match
.rm_eo
)
1147 output_color(opt
, bol
, match
.rm_so
, line_color
);
1148 output_color(opt
, bol
+ match
.rm_so
,
1149 match
.rm_eo
- match
.rm_so
, match_color
);
1151 rest
-= match
.rm_eo
;
1152 eflags
= REG_NOTBOL
;
1156 output_color(opt
, bol
, rest
, line_color
);
1157 opt
->output(opt
, "\n", 1);
1164 * This lock protects access to the gitattributes machinery, which is
1167 pthread_mutex_t grep_attr_mutex
;
1169 static inline void grep_attr_lock(void)
1172 pthread_mutex_lock(&grep_attr_mutex
);
1175 static inline void grep_attr_unlock(void)
1178 pthread_mutex_unlock(&grep_attr_mutex
);
1182 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
1184 pthread_mutex_t grep_read_mutex
;
1187 #define grep_attr_lock()
1188 #define grep_attr_unlock()
1191 static int match_funcname(struct grep_opt
*opt
, struct grep_source
*gs
, char *bol
, char *eol
)
1193 xdemitconf_t
*xecfg
= opt
->priv
;
1194 if (xecfg
&& !xecfg
->find_func
) {
1195 grep_source_load_driver(gs
);
1196 if (gs
->driver
->funcname
.pattern
) {
1197 const struct userdiff_funcname
*pe
= &gs
->driver
->funcname
;
1198 xdiff_set_find_func(xecfg
, pe
->pattern
, pe
->cflags
);
1200 xecfg
= opt
->priv
= NULL
;
1206 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
1207 xecfg
->find_func_priv
) >= 0;
1212 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
1217 static void show_funcname_line(struct grep_opt
*opt
, struct grep_source
*gs
,
1218 char *bol
, unsigned lno
)
1220 while (bol
> gs
->buf
) {
1223 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1227 if (lno
<= opt
->last_shown
)
1230 if (match_funcname(opt
, gs
, bol
, eol
)) {
1231 show_line(opt
, bol
, eol
, gs
->name
, lno
, '=');
1237 static void show_pre_context(struct grep_opt
*opt
, struct grep_source
*gs
,
1238 char *bol
, char *end
, unsigned lno
)
1240 unsigned cur
= lno
, from
= 1, funcname_lno
= 0;
1241 int funcname_needed
= !!opt
->funcname
;
1243 if (opt
->funcbody
&& !match_funcname(opt
, gs
, bol
, end
))
1244 funcname_needed
= 2;
1246 if (opt
->pre_context
< lno
)
1247 from
= lno
- opt
->pre_context
;
1248 if (from
<= opt
->last_shown
)
1249 from
= opt
->last_shown
+ 1;
1252 while (bol
> gs
->buf
&&
1253 cur
> (funcname_needed
== 2 ? opt
->last_shown
+ 1 : from
)) {
1256 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1259 if (funcname_needed
&& match_funcname(opt
, gs
, bol
, eol
)) {
1261 funcname_needed
= 0;
1265 /* We need to look even further back to find a function signature. */
1266 if (opt
->funcname
&& funcname_needed
)
1267 show_funcname_line(opt
, gs
, bol
, cur
);
1271 char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
1273 while (*eol
!= '\n')
1275 show_line(opt
, bol
, eol
, gs
->name
, cur
, sign
);
1281 static int should_lookahead(struct grep_opt
*opt
)
1286 return 0; /* punt for too complex stuff */
1289 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1290 if (p
->token
!= GREP_PATTERN
)
1291 return 0; /* punt for "header only" and stuff */
1296 static int look_ahead(struct grep_opt
*opt
,
1297 unsigned long *left_p
,
1301 unsigned lno
= *lno_p
;
1304 char *sp
, *last_bol
;
1305 regoff_t earliest
= -1;
1307 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1311 hit
= patmatch(p
, bol
, bol
+ *left_p
, &m
, 0);
1312 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
1314 if (earliest
< 0 || m
.rm_so
< earliest
)
1319 *bol_p
= bol
+ *left_p
;
1323 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
1324 ; /* find the beginning of the line */
1327 for (sp
= bol
; sp
< last_bol
; sp
++) {
1331 *left_p
-= last_bol
- bol
;
1337 static void std_output(struct grep_opt
*opt
, const void *buf
, size_t size
)
1339 fwrite(buf
, size
, 1, stdout
);
1342 static int fill_textconv_grep(struct userdiff_driver
*driver
,
1343 struct grep_source
*gs
)
1345 struct diff_filespec
*df
;
1349 if (!driver
|| !driver
->textconv
)
1350 return grep_source_load(gs
);
1353 * The textconv interface is intimately tied to diff_filespecs, so we
1354 * have to pretend to be one. If we could unify the grep_source
1355 * and diff_filespec structs, this mess could just go away.
1357 df
= alloc_filespec(gs
->path
);
1359 case GREP_SOURCE_SHA1
:
1360 fill_filespec(df
, gs
->identifier
, 1, 0100644);
1362 case GREP_SOURCE_FILE
:
1363 fill_filespec(df
, null_sha1
, 0, 0100644);
1366 die("BUG: attempt to textconv something without a path?");
1370 * fill_textconv is not remotely thread-safe; it may load objects
1371 * behind the scenes, and it modifies the global diff tempfile
1375 size
= fill_textconv(driver
, df
, &buf
);
1380 * The normal fill_textconv usage by the diff machinery would just keep
1381 * the textconv'd buf separate from the diff_filespec. But much of the
1382 * grep code passes around a grep_source and assumes that its "buf"
1383 * pointer is the beginning of the thing we are searching. So let's
1384 * install our textconv'd version into the grep_source, taking care not
1385 * to leak any existing buffer.
1387 grep_source_clear_data(gs
);
1394 static int grep_source_1(struct grep_opt
*opt
, struct grep_source
*gs
, int collect_hits
)
1399 unsigned last_hit
= 0;
1400 int binary_match_only
= 0;
1402 int try_lookahead
= 0;
1403 int show_function
= 0;
1404 struct userdiff_driver
*textconv
= NULL
;
1405 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
1409 opt
->output
= std_output
;
1411 if (opt
->pre_context
|| opt
->post_context
|| opt
->file_break
||
1413 /* Show hunk marks, except for the first file. */
1414 if (opt
->last_shown
)
1415 opt
->show_hunk_mark
= 1;
1417 * If we're using threads then we can't easily identify
1418 * the first file. Always put hunk marks in that case
1419 * and skip the very first one later in work_done().
1421 if (opt
->output
!= std_output
)
1422 opt
->show_hunk_mark
= 1;
1424 opt
->last_shown
= 0;
1426 if (opt
->allow_textconv
) {
1427 grep_source_load_driver(gs
);
1429 * We might set up the shared textconv cache data here, which
1430 * is not thread-safe.
1433 textconv
= userdiff_get_textconv(gs
->driver
);
1438 * We know the result of a textconv is text, so we only have to care
1439 * about binary handling if we are not using it.
1442 switch (opt
->binary
) {
1443 case GREP_BINARY_DEFAULT
:
1444 if (grep_source_is_binary(gs
))
1445 binary_match_only
= 1;
1447 case GREP_BINARY_NOMATCH
:
1448 if (grep_source_is_binary(gs
))
1449 return 0; /* Assume unmatch */
1451 case GREP_BINARY_TEXT
:
1454 die("bug: unknown binary handling mode");
1458 memset(&xecfg
, 0, sizeof(xecfg
));
1461 try_lookahead
= should_lookahead(opt
);
1463 if (fill_textconv_grep(textconv
, gs
) < 0)
1473 * look_ahead() skips quickly to the line that possibly
1474 * has the next hit; don't call it if we need to do
1475 * something more than just skipping the current line
1476 * in response to an unmatch for the current line. E.g.
1477 * inside a post-context window, we will show the current
1478 * line as a context around the previous hit when it
1483 && (show_function
||
1484 lno
<= last_hit
+ opt
->post_context
))
1485 && look_ahead(opt
, &left
, &lno
, &bol
))
1487 eol
= end_of_line(bol
, &left
);
1491 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
1492 ctx
= GREP_CONTEXT_BODY
;
1494 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
1500 /* "grep -v -e foo -e bla" should list lines
1501 * that do not have either, so inversion should
1506 if (opt
->unmatch_name_only
) {
1513 if (opt
->status_only
)
1515 if (opt
->name_only
) {
1516 show_name(opt
, gs
->name
);
1521 if (binary_match_only
) {
1522 opt
->output(opt
, "Binary file ", 12);
1523 output_color(opt
, gs
->name
, strlen(gs
->name
),
1524 opt
->color_filename
);
1525 opt
->output(opt
, " matches\n", 9);
1528 /* Hit at this line. If we haven't shown the
1529 * pre-context lines, we would need to show them.
1531 if (opt
->pre_context
|| opt
->funcbody
)
1532 show_pre_context(opt
, gs
, bol
, eol
, lno
);
1533 else if (opt
->funcname
)
1534 show_funcname_line(opt
, gs
, bol
, lno
);
1535 show_line(opt
, bol
, eol
, gs
->name
, lno
, ':');
1541 if (show_function
&& match_funcname(opt
, gs
, bol
, eol
))
1543 if (show_function
||
1544 (last_hit
&& lno
<= last_hit
+ opt
->post_context
)) {
1545 /* If the last hit is within the post context,
1546 * we need to show this line.
1548 show_line(opt
, bol
, eol
, gs
->name
, lno
, '-');
1562 if (opt
->status_only
)
1564 if (opt
->unmatch_name_only
) {
1565 /* We did not see any hit, so we want to show this */
1566 show_name(opt
, gs
->name
);
1570 xdiff_clear_find_func(&xecfg
);
1574 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1575 * which feels mostly useless but sometimes useful. Maybe
1576 * make it another option? For now suppress them.
1578 if (opt
->count
&& count
) {
1580 output_color(opt
, gs
->name
, strlen(gs
->name
), opt
->color_filename
);
1581 output_sep(opt
, ':');
1582 snprintf(buf
, sizeof(buf
), "%u\n", count
);
1583 opt
->output(opt
, buf
, strlen(buf
));
1589 static void clr_hit_marker(struct grep_expr
*x
)
1591 /* All-hit markers are meaningful only at the very top level
1596 if (x
->node
!= GREP_NODE_OR
)
1598 x
->u
.binary
.left
->hit
= 0;
1599 x
= x
->u
.binary
.right
;
1603 static int chk_hit_marker(struct grep_expr
*x
)
1605 /* Top level nodes have hit markers. See if they all are hits */
1607 if (x
->node
!= GREP_NODE_OR
)
1609 if (!x
->u
.binary
.left
->hit
)
1611 x
= x
->u
.binary
.right
;
1615 int grep_source(struct grep_opt
*opt
, struct grep_source
*gs
)
1618 * we do not have to do the two-pass grep when we do not check
1619 * buffer-wide "all-match".
1621 if (!opt
->all_match
)
1622 return grep_source_1(opt
, gs
, 0);
1624 /* Otherwise the toplevel "or" terms hit a bit differently.
1625 * We first clear hit markers from them.
1627 clr_hit_marker(opt
->pattern_expression
);
1628 grep_source_1(opt
, gs
, 1);
1630 if (!chk_hit_marker(opt
->pattern_expression
))
1633 return grep_source_1(opt
, gs
, 0);
1636 int grep_buffer(struct grep_opt
*opt
, char *buf
, unsigned long size
)
1638 struct grep_source gs
;
1641 grep_source_init(&gs
, GREP_SOURCE_BUF
, NULL
, NULL
, NULL
);
1645 r
= grep_source(opt
, &gs
);
1647 grep_source_clear(&gs
);
1651 void grep_source_init(struct grep_source
*gs
, enum grep_source_type type
,
1652 const char *name
, const char *path
,
1653 const void *identifier
)
1656 gs
->name
= name
? xstrdup(name
) : NULL
;
1657 gs
->path
= path
? xstrdup(path
) : NULL
;
1663 case GREP_SOURCE_FILE
:
1664 gs
->identifier
= xstrdup(identifier
);
1666 case GREP_SOURCE_SHA1
:
1667 gs
->identifier
= xmalloc(20);
1668 memcpy(gs
->identifier
, identifier
, 20);
1670 case GREP_SOURCE_BUF
:
1671 gs
->identifier
= NULL
;
1675 void grep_source_clear(struct grep_source
*gs
)
1681 free(gs
->identifier
);
1682 gs
->identifier
= NULL
;
1683 grep_source_clear_data(gs
);
1686 void grep_source_clear_data(struct grep_source
*gs
)
1689 case GREP_SOURCE_FILE
:
1690 case GREP_SOURCE_SHA1
:
1695 case GREP_SOURCE_BUF
:
1696 /* leave user-provided buf intact */
1701 static int grep_source_load_sha1(struct grep_source
*gs
)
1703 enum object_type type
;
1706 gs
->buf
= read_sha1_file(gs
->identifier
, &type
, &gs
->size
);
1710 return error(_("'%s': unable to read %s"),
1712 sha1_to_hex(gs
->identifier
));
1716 static int grep_source_load_file(struct grep_source
*gs
)
1718 const char *filename
= gs
->identifier
;
1724 if (lstat(filename
, &st
) < 0) {
1726 if (errno
!= ENOENT
)
1727 error(_("'%s': %s"), filename
, strerror(errno
));
1730 if (!S_ISREG(st
.st_mode
))
1732 size
= xsize_t(st
.st_size
);
1733 i
= open(filename
, O_RDONLY
);
1736 data
= xmalloc(size
+ 1);
1737 if (st
.st_size
!= read_in_full(i
, data
, size
)) {
1738 error(_("'%s': short read %s"), filename
, strerror(errno
));
1751 static int grep_source_load(struct grep_source
*gs
)
1757 case GREP_SOURCE_FILE
:
1758 return grep_source_load_file(gs
);
1759 case GREP_SOURCE_SHA1
:
1760 return grep_source_load_sha1(gs
);
1761 case GREP_SOURCE_BUF
:
1762 return gs
->buf
? 0 : -1;
1764 die("BUG: invalid grep_source type");
1767 void grep_source_load_driver(struct grep_source
*gs
)
1774 gs
->driver
= userdiff_find_by_path(gs
->path
);
1776 gs
->driver
= userdiff_find_by_name("default");
1780 static int grep_source_is_binary(struct grep_source
*gs
)
1782 grep_source_load_driver(gs
);
1783 if (gs
->driver
->binary
!= -1)
1784 return gs
->driver
->binary
;
1786 if (!grep_source_load(gs
))
1787 return buffer_is_binary(gs
->buf
, gs
->size
);