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
, GIT_COLOR_BOLD_RED
);
39 strcpy(opt
->color_selected
, "");
40 strcpy(opt
->color_sep
, GIT_COLOR_CYAN
);
44 static int parse_pattern_type_arg(const char *opt
, const char *arg
)
46 if (!strcmp(arg
, "default"))
47 return GREP_PATTERN_TYPE_UNSPECIFIED
;
48 else if (!strcmp(arg
, "basic"))
49 return GREP_PATTERN_TYPE_BRE
;
50 else if (!strcmp(arg
, "extended"))
51 return GREP_PATTERN_TYPE_ERE
;
52 else if (!strcmp(arg
, "fixed"))
53 return GREP_PATTERN_TYPE_FIXED
;
54 else if (!strcmp(arg
, "perl"))
55 return GREP_PATTERN_TYPE_PCRE
;
56 die("bad %s argument: %s", opt
, arg
);
60 * Read the configuration file once and store it in
61 * the grep_defaults template.
63 int grep_config(const char *var
, const char *value
, void *cb
)
65 struct grep_opt
*opt
= &grep_defaults
;
68 if (userdiff_config(var
, value
) < 0)
71 if (!strcmp(var
, "grep.extendedregexp")) {
72 if (git_config_bool(var
, value
))
73 opt
->extended_regexp_option
= 1;
75 opt
->extended_regexp_option
= 0;
79 if (!strcmp(var
, "grep.patterntype")) {
80 opt
->pattern_type_option
= parse_pattern_type_arg(var
, value
);
84 if (!strcmp(var
, "grep.linenumber")) {
85 opt
->linenum
= git_config_bool(var
, value
);
89 if (!strcmp(var
, "grep.fullname")) {
90 opt
->relative
= !git_config_bool(var
, value
);
94 if (!strcmp(var
, "color.grep"))
95 opt
->color
= git_config_colorbool(var
, value
);
96 else if (!strcmp(var
, "color.grep.context"))
97 color
= opt
->color_context
;
98 else if (!strcmp(var
, "color.grep.filename"))
99 color
= opt
->color_filename
;
100 else if (!strcmp(var
, "color.grep.function"))
101 color
= opt
->color_function
;
102 else if (!strcmp(var
, "color.grep.linenumber"))
103 color
= opt
->color_lineno
;
104 else if (!strcmp(var
, "color.grep.match"))
105 color
= opt
->color_match
;
106 else if (!strcmp(var
, "color.grep.selected"))
107 color
= opt
->color_selected
;
108 else if (!strcmp(var
, "color.grep.separator"))
109 color
= opt
->color_sep
;
113 return config_error_nonbool(var
);
114 color_parse(value
, var
, color
);
120 * Initialize one instance of grep_opt and copy the
121 * default values from the template we read the configuration
122 * information in an earlier call to git_config(grep_config).
124 void grep_init(struct grep_opt
*opt
, const char *prefix
)
126 struct grep_opt
*def
= &grep_defaults
;
128 memset(opt
, 0, sizeof(*opt
));
129 opt
->prefix
= prefix
;
130 opt
->prefix_length
= (prefix
&& *prefix
) ? strlen(prefix
) : 0;
131 opt
->pattern_tail
= &opt
->pattern_list
;
132 opt
->header_tail
= &opt
->header_list
;
134 opt
->color
= def
->color
;
135 opt
->extended_regexp_option
= def
->extended_regexp_option
;
136 opt
->pattern_type_option
= def
->pattern_type_option
;
137 opt
->linenum
= def
->linenum
;
138 opt
->max_depth
= def
->max_depth
;
139 opt
->pathname
= def
->pathname
;
140 opt
->regflags
= def
->regflags
;
141 opt
->relative
= def
->relative
;
143 strcpy(opt
->color_context
, def
->color_context
);
144 strcpy(opt
->color_filename
, def
->color_filename
);
145 strcpy(opt
->color_function
, def
->color_function
);
146 strcpy(opt
->color_lineno
, def
->color_lineno
);
147 strcpy(opt
->color_match
, def
->color_match
);
148 strcpy(opt
->color_selected
, def
->color_selected
);
149 strcpy(opt
->color_sep
, def
->color_sep
);
152 void grep_commit_pattern_type(enum grep_pattern_type pattern_type
, struct grep_opt
*opt
)
154 if (pattern_type
!= GREP_PATTERN_TYPE_UNSPECIFIED
)
155 grep_set_pattern_type_option(pattern_type
, opt
);
156 else if (opt
->pattern_type_option
!= GREP_PATTERN_TYPE_UNSPECIFIED
)
157 grep_set_pattern_type_option(opt
->pattern_type_option
, opt
);
158 else if (opt
->extended_regexp_option
)
159 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE
, opt
);
162 void grep_set_pattern_type_option(enum grep_pattern_type pattern_type
, struct grep_opt
*opt
)
164 switch (pattern_type
) {
165 case GREP_PATTERN_TYPE_UNSPECIFIED
:
168 case GREP_PATTERN_TYPE_BRE
:
171 opt
->regflags
&= ~REG_EXTENDED
;
174 case GREP_PATTERN_TYPE_ERE
:
177 opt
->regflags
|= REG_EXTENDED
;
180 case GREP_PATTERN_TYPE_FIXED
:
183 opt
->regflags
&= ~REG_EXTENDED
;
186 case GREP_PATTERN_TYPE_PCRE
:
189 opt
->regflags
&= ~REG_EXTENDED
;
194 static struct grep_pat
*create_grep_pat(const char *pat
, size_t patlen
,
195 const char *origin
, int no
,
196 enum grep_pat_token t
,
197 enum grep_header_field field
)
199 struct grep_pat
*p
= xcalloc(1, sizeof(*p
));
200 p
->pattern
= xmemdupz(pat
, patlen
);
201 p
->patternlen
= patlen
;
209 static void do_append_grep_pat(struct grep_pat
***tail
, struct grep_pat
*p
)
216 case GREP_PATTERN
: /* atom */
217 case GREP_PATTERN_HEAD
:
218 case GREP_PATTERN_BODY
:
220 struct grep_pat
*new_pat
;
222 char *cp
= p
->pattern
+ p
->patternlen
, *nl
= NULL
;
223 while (++len
<= p
->patternlen
) {
224 if (*(--cp
) == '\n') {
231 new_pat
= create_grep_pat(nl
+ 1, len
- 1, p
->origin
,
232 p
->no
, p
->token
, p
->field
);
233 new_pat
->next
= p
->next
;
235 *tail
= &new_pat
->next
;
238 p
->patternlen
-= len
;
246 void append_header_grep_pattern(struct grep_opt
*opt
,
247 enum grep_header_field field
, const char *pat
)
249 struct grep_pat
*p
= create_grep_pat(pat
, strlen(pat
), "header", 0,
250 GREP_PATTERN_HEAD
, field
);
251 if (field
== GREP_HEADER_REFLOG
)
252 opt
->use_reflog_filter
= 1;
253 do_append_grep_pat(&opt
->header_tail
, p
);
256 void append_grep_pattern(struct grep_opt
*opt
, const char *pat
,
257 const char *origin
, int no
, enum grep_pat_token t
)
259 append_grep_pat(opt
, pat
, strlen(pat
), origin
, no
, t
);
262 void append_grep_pat(struct grep_opt
*opt
, const char *pat
, size_t patlen
,
263 const char *origin
, int no
, enum grep_pat_token t
)
265 struct grep_pat
*p
= create_grep_pat(pat
, patlen
, origin
, no
, t
, 0);
266 do_append_grep_pat(&opt
->pattern_tail
, p
);
269 struct grep_opt
*grep_opt_dup(const struct grep_opt
*opt
)
271 struct grep_pat
*pat
;
272 struct grep_opt
*ret
= xmalloc(sizeof(struct grep_opt
));
275 ret
->pattern_list
= NULL
;
276 ret
->pattern_tail
= &ret
->pattern_list
;
278 for(pat
= opt
->pattern_list
; pat
!= NULL
; pat
= pat
->next
)
280 if(pat
->token
== GREP_PATTERN_HEAD
)
281 append_header_grep_pattern(ret
, pat
->field
,
284 append_grep_pat(ret
, pat
->pattern
, pat
->patternlen
,
285 pat
->origin
, pat
->no
, pat
->token
);
291 static NORETURN
void compile_regexp_failed(const struct grep_pat
*p
,
297 sprintf(where
, "In '%s' at %d, ", p
->origin
, p
->no
);
299 sprintf(where
, "%s, ", p
->origin
);
303 die("%s'%s': %s", where
, p
->pattern
, error
);
307 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
311 int options
= PCRE_MULTILINE
;
313 if (opt
->ignore_case
)
314 options
|= PCRE_CASELESS
;
316 p
->pcre_regexp
= pcre_compile(p
->pattern
, options
, &error
, &erroffset
,
319 compile_regexp_failed(p
, error
);
321 p
->pcre_extra_info
= pcre_study(p
->pcre_regexp
, 0, &error
);
322 if (!p
->pcre_extra_info
&& error
)
326 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
327 regmatch_t
*match
, int eflags
)
329 int ovector
[30], ret
, flags
= 0;
331 if (eflags
& REG_NOTBOL
)
332 flags
|= PCRE_NOTBOL
;
334 ret
= pcre_exec(p
->pcre_regexp
, p
->pcre_extra_info
, line
, eol
- line
,
335 0, flags
, ovector
, ARRAY_SIZE(ovector
));
336 if (ret
< 0 && ret
!= PCRE_ERROR_NOMATCH
)
337 die("pcre_exec failed with error code %d", ret
);
340 match
->rm_so
= ovector
[0];
341 match
->rm_eo
= ovector
[1];
347 static void free_pcre_regexp(struct grep_pat
*p
)
349 pcre_free(p
->pcre_regexp
);
350 pcre_free(p
->pcre_extra_info
);
352 #else /* !USE_LIBPCRE */
353 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
355 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
358 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
359 regmatch_t
*match
, int eflags
)
364 static void free_pcre_regexp(struct grep_pat
*p
)
367 #endif /* !USE_LIBPCRE */
369 static int is_fixed(const char *s
, size_t len
)
373 /* regcomp cannot accept patterns with NULs so we
374 * consider any pattern containing a NUL fixed.
376 if (memchr(s
, 0, len
))
379 for (i
= 0; i
< len
; i
++) {
380 if (is_regex_special(s
[i
]))
387 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
391 p
->word_regexp
= opt
->word_regexp
;
392 p
->ignore_case
= opt
->ignore_case
;
394 if (opt
->fixed
|| is_fixed(p
->pattern
, p
->patternlen
))
400 if (opt
->regflags
& REG_ICASE
|| p
->ignore_case
)
401 p
->kws
= kwsalloc(tolower_trans_tbl
);
403 p
->kws
= kwsalloc(NULL
);
404 kwsincr(p
->kws
, p
->pattern
, p
->patternlen
);
410 compile_pcre_regexp(p
, opt
);
414 err
= regcomp(&p
->regexp
, p
->pattern
, opt
->regflags
);
417 regerror(err
, &p
->regexp
, errbuf
, 1024);
419 compile_regexp_failed(p
, errbuf
);
423 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
424 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
433 case GREP_PATTERN
: /* atom */
434 case GREP_PATTERN_HEAD
:
435 case GREP_PATTERN_BODY
:
436 x
= xcalloc(1, sizeof (struct grep_expr
));
437 x
->node
= GREP_NODE_ATOM
;
441 case GREP_OPEN_PAREN
:
443 x
= compile_pattern_or(list
);
444 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
445 die("unmatched parenthesis");
446 *list
= (*list
)->next
;
453 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
464 die("--not not followed by pattern expression");
466 x
= xcalloc(1, sizeof (struct grep_expr
));
467 x
->node
= GREP_NODE_NOT
;
468 x
->u
.unary
= compile_pattern_not(list
);
470 die("--not followed by non pattern expression");
473 return compile_pattern_atom(list
);
477 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
480 struct grep_expr
*x
, *y
, *z
;
482 x
= compile_pattern_not(list
);
484 if (p
&& p
->token
== GREP_AND
) {
486 die("--and not followed by pattern expression");
488 y
= compile_pattern_and(list
);
490 die("--and not followed by pattern expression");
491 z
= xcalloc(1, sizeof (struct grep_expr
));
492 z
->node
= GREP_NODE_AND
;
493 z
->u
.binary
.left
= x
;
494 z
->u
.binary
.right
= y
;
500 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
503 struct grep_expr
*x
, *y
, *z
;
505 x
= compile_pattern_and(list
);
507 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
508 y
= compile_pattern_or(list
);
510 die("not a pattern expression %s", p
->pattern
);
511 z
= xcalloc(1, sizeof (struct grep_expr
));
512 z
->node
= GREP_NODE_OR
;
513 z
->u
.binary
.left
= x
;
514 z
->u
.binary
.right
= y
;
520 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
522 return compile_pattern_or(list
);
525 static void indent(int in
)
531 static void dump_grep_pat(struct grep_pat
*p
)
534 case GREP_AND
: fprintf(stderr
, "*and*"); break;
535 case GREP_OPEN_PAREN
: fprintf(stderr
, "*(*"); break;
536 case GREP_CLOSE_PAREN
: fprintf(stderr
, "*)*"); break;
537 case GREP_NOT
: fprintf(stderr
, "*not*"); break;
538 case GREP_OR
: fprintf(stderr
, "*or*"); break;
540 case GREP_PATTERN
: fprintf(stderr
, "pattern"); break;
541 case GREP_PATTERN_HEAD
: fprintf(stderr
, "pattern_head"); break;
542 case GREP_PATTERN_BODY
: fprintf(stderr
, "pattern_body"); break;
547 case GREP_PATTERN_HEAD
:
548 fprintf(stderr
, "<head %d>", p
->field
); break;
549 case GREP_PATTERN_BODY
:
550 fprintf(stderr
, "<body>"); break;
554 case GREP_PATTERN_HEAD
:
555 case GREP_PATTERN_BODY
:
557 fprintf(stderr
, "%.*s", (int)p
->patternlen
, p
->pattern
);
563 static void dump_grep_expression_1(struct grep_expr
*x
, int in
)
568 fprintf(stderr
, "true\n");
571 dump_grep_pat(x
->u
.atom
);
574 fprintf(stderr
, "(not\n");
575 dump_grep_expression_1(x
->u
.unary
, in
+1);
577 fprintf(stderr
, ")\n");
580 fprintf(stderr
, "(and\n");
581 dump_grep_expression_1(x
->u
.binary
.left
, in
+1);
582 dump_grep_expression_1(x
->u
.binary
.right
, in
+1);
584 fprintf(stderr
, ")\n");
587 fprintf(stderr
, "(or\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");
596 static void dump_grep_expression(struct grep_opt
*opt
)
598 struct grep_expr
*x
= opt
->pattern_expression
;
601 fprintf(stderr
, "[all-match]\n");
602 dump_grep_expression_1(x
, 0);
606 static struct grep_expr
*grep_true_expr(void)
608 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
609 z
->node
= GREP_NODE_TRUE
;
613 static struct grep_expr
*grep_or_expr(struct grep_expr
*left
, struct grep_expr
*right
)
615 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
616 z
->node
= GREP_NODE_OR
;
617 z
->u
.binary
.left
= left
;
618 z
->u
.binary
.right
= right
;
622 static struct grep_expr
*prep_header_patterns(struct grep_opt
*opt
)
625 struct grep_expr
*header_expr
;
626 struct grep_expr
*(header_group
[GREP_HEADER_FIELD_MAX
]);
627 enum grep_header_field fld
;
629 if (!opt
->header_list
)
632 for (p
= opt
->header_list
; p
; p
= p
->next
) {
633 if (p
->token
!= GREP_PATTERN_HEAD
)
634 die("bug: a non-header pattern in grep header list.");
635 if (p
->field
< GREP_HEADER_FIELD_MIN
||
636 GREP_HEADER_FIELD_MAX
<= p
->field
)
637 die("bug: unknown header field %d", p
->field
);
638 compile_regexp(p
, opt
);
641 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++)
642 header_group
[fld
] = NULL
;
644 for (p
= opt
->header_list
; p
; p
= p
->next
) {
646 struct grep_pat
*pp
= p
;
648 h
= compile_pattern_atom(&pp
);
649 if (!h
|| pp
!= p
->next
)
650 die("bug: malformed header expr");
651 if (!header_group
[p
->field
]) {
652 header_group
[p
->field
] = h
;
655 header_group
[p
->field
] = grep_or_expr(h
, header_group
[p
->field
]);
660 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++) {
661 if (!header_group
[fld
])
664 header_expr
= grep_true_expr();
665 header_expr
= grep_or_expr(header_group
[fld
], header_expr
);
670 static struct grep_expr
*grep_splice_or(struct grep_expr
*x
, struct grep_expr
*y
)
672 struct grep_expr
*z
= x
;
675 assert(x
->node
== GREP_NODE_OR
);
676 if (x
->u
.binary
.right
&&
677 x
->u
.binary
.right
->node
== GREP_NODE_TRUE
) {
678 x
->u
.binary
.right
= y
;
681 x
= x
->u
.binary
.right
;
686 static void compile_grep_patterns_real(struct grep_opt
*opt
)
689 struct grep_expr
*header_expr
= prep_header_patterns(opt
);
691 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
693 case GREP_PATTERN
: /* atom */
694 case GREP_PATTERN_HEAD
:
695 case GREP_PATTERN_BODY
:
696 compile_regexp(p
, opt
);
704 if (opt
->all_match
|| header_expr
)
706 else if (!opt
->extended
&& !opt
->debug
)
709 p
= opt
->pattern_list
;
711 opt
->pattern_expression
= compile_pattern_expr(&p
);
713 die("incomplete pattern expression: %s", p
->pattern
);
718 if (!opt
->pattern_expression
)
719 opt
->pattern_expression
= header_expr
;
720 else if (opt
->all_match
)
721 opt
->pattern_expression
= grep_splice_or(header_expr
,
722 opt
->pattern_expression
);
724 opt
->pattern_expression
= grep_or_expr(opt
->pattern_expression
,
729 void compile_grep_patterns(struct grep_opt
*opt
)
731 compile_grep_patterns_real(opt
);
733 dump_grep_expression(opt
);
736 static void free_pattern_expr(struct grep_expr
*x
)
743 free_pattern_expr(x
->u
.unary
);
747 free_pattern_expr(x
->u
.binary
.left
);
748 free_pattern_expr(x
->u
.binary
.right
);
754 void free_grep_patterns(struct grep_opt
*opt
)
756 struct grep_pat
*p
, *n
;
758 for (p
= opt
->pattern_list
; p
; p
= n
) {
761 case GREP_PATTERN
: /* atom */
762 case GREP_PATTERN_HEAD
:
763 case GREP_PATTERN_BODY
:
766 else if (p
->pcre_regexp
)
780 free_pattern_expr(opt
->pattern_expression
);
783 static char *end_of_line(char *cp
, unsigned long *left
)
785 unsigned long l
= *left
;
786 while (l
&& *cp
!= '\n') {
794 static int word_char(char ch
)
796 return isalnum(ch
) || ch
== '_';
799 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
802 if (want_color(opt
->color
) && color
&& color
[0]) {
803 opt
->output(opt
, color
, strlen(color
));
804 opt
->output(opt
, data
, size
);
805 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
807 opt
->output(opt
, data
, size
);
810 static void output_sep(struct grep_opt
*opt
, char sign
)
812 if (opt
->null_following_name
)
813 opt
->output(opt
, "\0", 1);
815 output_color(opt
, &sign
, 1, opt
->color_sep
);
818 static void show_name(struct grep_opt
*opt
, const char *name
)
820 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
821 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
824 static int fixmatch(struct grep_pat
*p
, char *line
, char *eol
,
827 struct kwsmatch kwsm
;
828 size_t offset
= kwsexec(p
->kws
, line
, eol
- line
, &kwsm
);
830 match
->rm_so
= match
->rm_eo
= -1;
833 match
->rm_so
= offset
;
834 match
->rm_eo
= match
->rm_so
+ kwsm
.size
[0];
839 static int regmatch(const regex_t
*preg
, char *line
, char *eol
,
840 regmatch_t
*match
, int eflags
)
844 match
->rm_eo
= eol
- line
;
845 eflags
|= REG_STARTEND
;
847 return regexec(preg
, line
, 1, match
, eflags
);
850 static int patmatch(struct grep_pat
*p
, char *line
, char *eol
,
851 regmatch_t
*match
, int eflags
)
856 hit
= !fixmatch(p
, line
, eol
, match
);
857 else if (p
->pcre_regexp
)
858 hit
= !pcrematch(p
, line
, eol
, match
, eflags
);
860 hit
= !regmatch(&p
->regexp
, line
, eol
, match
, eflags
);
865 static int strip_timestamp(char *bol
, char **eol_p
)
870 while (bol
< --eol
) {
886 { "committer ", 10 },
890 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
891 enum grep_context ctx
,
892 regmatch_t
*pmatch
, int eflags
)
896 const char *start
= bol
;
898 if ((p
->token
!= GREP_PATTERN
) &&
899 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
902 if (p
->token
== GREP_PATTERN_HEAD
) {
905 assert(p
->field
< ARRAY_SIZE(header_field
));
906 field
= header_field
[p
->field
].field
;
907 len
= header_field
[p
->field
].len
;
908 if (strncmp(bol
, field
, len
))
912 case GREP_HEADER_AUTHOR
:
913 case GREP_HEADER_COMMITTER
:
914 saved_ch
= strip_timestamp(bol
, &eol
);
922 hit
= patmatch(p
, bol
, eol
, pmatch
, eflags
);
924 if (hit
&& p
->word_regexp
) {
925 if ((pmatch
[0].rm_so
< 0) ||
926 (eol
- bol
) < pmatch
[0].rm_so
||
927 (pmatch
[0].rm_eo
< 0) ||
928 (eol
- bol
) < pmatch
[0].rm_eo
)
929 die("regexp returned nonsense");
931 /* Match beginning must be either beginning of the
932 * line, or at word boundary (i.e. the last char must
933 * not be a word char). Similarly, match end must be
934 * either end of the line, or at word boundary
935 * (i.e. the next char must not be a word char).
937 if ( ((pmatch
[0].rm_so
== 0) ||
938 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
939 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
940 !word_char(bol
[pmatch
[0].rm_eo
])) )
945 /* Words consist of at least one character. */
946 if (pmatch
->rm_so
== pmatch
->rm_eo
)
949 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
950 /* There could be more than one match on the
951 * line, and the first match might not be
952 * strict word match. But later ones could be!
953 * Forward to the next possible start, i.e. the
954 * next position following a non-word char.
956 bol
= pmatch
[0].rm_so
+ bol
+ 1;
957 while (word_char(bol
[-1]) && bol
< eol
)
959 eflags
|= REG_NOTBOL
;
964 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
967 pmatch
[0].rm_so
+= bol
- start
;
968 pmatch
[0].rm_eo
+= bol
- start
;
973 static int match_expr_eval(struct grep_expr
*x
, char *bol
, char *eol
,
974 enum grep_context ctx
, int collect_hits
)
980 die("Not a valid grep expression");
986 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
, &match
, 0);
989 h
= !match_expr_eval(x
->u
.unary
, bol
, eol
, ctx
, 0);
992 if (!match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0))
994 h
= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
998 return (match_expr_eval(x
->u
.binary
.left
,
1000 match_expr_eval(x
->u
.binary
.right
,
1002 h
= match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
1003 x
->u
.binary
.left
->hit
|= h
;
1004 h
|= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
1007 die("Unexpected node type (internal error) %d", x
->node
);
1014 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
1015 enum grep_context ctx
, int collect_hits
)
1017 struct grep_expr
*x
= opt
->pattern_expression
;
1018 return match_expr_eval(x
, bol
, eol
, ctx
, collect_hits
);
1021 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
1022 enum grep_context ctx
, int collect_hits
)
1028 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
1030 /* we do not call with collect_hits without being extended */
1031 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1032 if (match_one_pattern(p
, bol
, eol
, ctx
, &match
, 0))
1038 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
1039 enum grep_context ctx
,
1040 regmatch_t
*pmatch
, int eflags
)
1044 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
1046 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
1048 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
1049 if (match
.rm_so
> pmatch
->rm_so
)
1051 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
1054 pmatch
->rm_so
= match
.rm_so
;
1055 pmatch
->rm_eo
= match
.rm_eo
;
1059 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
1060 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
1065 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
1067 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1069 case GREP_PATTERN
: /* atom */
1070 case GREP_PATTERN_HEAD
:
1071 case GREP_PATTERN_BODY
:
1072 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
1083 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
1084 const char *name
, unsigned lno
, char sign
)
1086 int rest
= eol
- bol
;
1087 char *line_color
= NULL
;
1089 if (opt
->file_break
&& opt
->last_shown
== 0) {
1090 if (opt
->show_hunk_mark
)
1091 opt
->output(opt
, "\n", 1);
1092 } else if (opt
->pre_context
|| opt
->post_context
|| opt
->funcbody
) {
1093 if (opt
->last_shown
== 0) {
1094 if (opt
->show_hunk_mark
) {
1095 output_color(opt
, "--", 2, opt
->color_sep
);
1096 opt
->output(opt
, "\n", 1);
1098 } else if (lno
> opt
->last_shown
+ 1) {
1099 output_color(opt
, "--", 2, opt
->color_sep
);
1100 opt
->output(opt
, "\n", 1);
1103 if (opt
->heading
&& opt
->last_shown
== 0) {
1104 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
1105 opt
->output(opt
, "\n", 1);
1107 opt
->last_shown
= lno
;
1109 if (!opt
->heading
&& opt
->pathname
) {
1110 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
1111 output_sep(opt
, sign
);
1115 snprintf(buf
, sizeof(buf
), "%d", lno
);
1116 output_color(opt
, buf
, strlen(buf
), opt
->color_lineno
);
1117 output_sep(opt
, sign
);
1121 enum grep_context ctx
= GREP_CONTEXT_BODY
;
1126 line_color
= opt
->color_selected
;
1127 else if (sign
== '-')
1128 line_color
= opt
->color_context
;
1129 else if (sign
== '=')
1130 line_color
= opt
->color_function
;
1132 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
1133 if (match
.rm_so
== match
.rm_eo
)
1136 output_color(opt
, bol
, match
.rm_so
, line_color
);
1137 output_color(opt
, bol
+ match
.rm_so
,
1138 match
.rm_eo
- match
.rm_so
,
1141 rest
-= match
.rm_eo
;
1142 eflags
= REG_NOTBOL
;
1146 output_color(opt
, bol
, rest
, line_color
);
1147 opt
->output(opt
, "\n", 1);
1154 * This lock protects access to the gitattributes machinery, which is
1157 pthread_mutex_t grep_attr_mutex
;
1159 static inline void grep_attr_lock(void)
1162 pthread_mutex_lock(&grep_attr_mutex
);
1165 static inline void grep_attr_unlock(void)
1168 pthread_mutex_unlock(&grep_attr_mutex
);
1172 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
1174 pthread_mutex_t grep_read_mutex
;
1177 #define grep_attr_lock()
1178 #define grep_attr_unlock()
1181 static int match_funcname(struct grep_opt
*opt
, struct grep_source
*gs
, char *bol
, char *eol
)
1183 xdemitconf_t
*xecfg
= opt
->priv
;
1184 if (xecfg
&& !xecfg
->find_func
) {
1185 grep_source_load_driver(gs
);
1186 if (gs
->driver
->funcname
.pattern
) {
1187 const struct userdiff_funcname
*pe
= &gs
->driver
->funcname
;
1188 xdiff_set_find_func(xecfg
, pe
->pattern
, pe
->cflags
);
1190 xecfg
= opt
->priv
= NULL
;
1196 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
1197 xecfg
->find_func_priv
) >= 0;
1202 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
1207 static void show_funcname_line(struct grep_opt
*opt
, struct grep_source
*gs
,
1208 char *bol
, unsigned lno
)
1210 while (bol
> gs
->buf
) {
1213 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1217 if (lno
<= opt
->last_shown
)
1220 if (match_funcname(opt
, gs
, bol
, eol
)) {
1221 show_line(opt
, bol
, eol
, gs
->name
, lno
, '=');
1227 static void show_pre_context(struct grep_opt
*opt
, struct grep_source
*gs
,
1228 char *bol
, char *end
, unsigned lno
)
1230 unsigned cur
= lno
, from
= 1, funcname_lno
= 0;
1231 int funcname_needed
= !!opt
->funcname
;
1233 if (opt
->funcbody
&& !match_funcname(opt
, gs
, bol
, end
))
1234 funcname_needed
= 2;
1236 if (opt
->pre_context
< lno
)
1237 from
= lno
- opt
->pre_context
;
1238 if (from
<= opt
->last_shown
)
1239 from
= opt
->last_shown
+ 1;
1242 while (bol
> gs
->buf
&&
1243 cur
> (funcname_needed
== 2 ? opt
->last_shown
+ 1 : from
)) {
1246 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1249 if (funcname_needed
&& match_funcname(opt
, gs
, bol
, eol
)) {
1251 funcname_needed
= 0;
1255 /* We need to look even further back to find a function signature. */
1256 if (opt
->funcname
&& funcname_needed
)
1257 show_funcname_line(opt
, gs
, bol
, cur
);
1261 char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
1263 while (*eol
!= '\n')
1265 show_line(opt
, bol
, eol
, gs
->name
, cur
, sign
);
1271 static int should_lookahead(struct grep_opt
*opt
)
1276 return 0; /* punt for too complex stuff */
1279 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1280 if (p
->token
!= GREP_PATTERN
)
1281 return 0; /* punt for "header only" and stuff */
1286 static int look_ahead(struct grep_opt
*opt
,
1287 unsigned long *left_p
,
1291 unsigned lno
= *lno_p
;
1294 char *sp
, *last_bol
;
1295 regoff_t earliest
= -1;
1297 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1301 hit
= patmatch(p
, bol
, bol
+ *left_p
, &m
, 0);
1302 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
1304 if (earliest
< 0 || m
.rm_so
< earliest
)
1309 *bol_p
= bol
+ *left_p
;
1313 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
1314 ; /* find the beginning of the line */
1317 for (sp
= bol
; sp
< last_bol
; sp
++) {
1321 *left_p
-= last_bol
- bol
;
1327 static void std_output(struct grep_opt
*opt
, const void *buf
, size_t size
)
1329 fwrite(buf
, size
, 1, stdout
);
1332 static int fill_textconv_grep(struct userdiff_driver
*driver
,
1333 struct grep_source
*gs
)
1335 struct diff_filespec
*df
;
1339 if (!driver
|| !driver
->textconv
)
1340 return grep_source_load(gs
);
1343 * The textconv interface is intimately tied to diff_filespecs, so we
1344 * have to pretend to be one. If we could unify the grep_source
1345 * and diff_filespec structs, this mess could just go away.
1347 df
= alloc_filespec(gs
->path
);
1349 case GREP_SOURCE_SHA1
:
1350 fill_filespec(df
, gs
->identifier
, 1, 0100644);
1352 case GREP_SOURCE_FILE
:
1353 fill_filespec(df
, null_sha1
, 0, 0100644);
1356 die("BUG: attempt to textconv something without a path?");
1360 * fill_textconv is not remotely thread-safe; it may load objects
1361 * behind the scenes, and it modifies the global diff tempfile
1365 size
= fill_textconv(driver
, df
, &buf
);
1370 * The normal fill_textconv usage by the diff machinery would just keep
1371 * the textconv'd buf separate from the diff_filespec. But much of the
1372 * grep code passes around a grep_source and assumes that its "buf"
1373 * pointer is the beginning of the thing we are searching. So let's
1374 * install our textconv'd version into the grep_source, taking care not
1375 * to leak any existing buffer.
1377 grep_source_clear_data(gs
);
1384 static int grep_source_1(struct grep_opt
*opt
, struct grep_source
*gs
, int collect_hits
)
1389 unsigned last_hit
= 0;
1390 int binary_match_only
= 0;
1392 int try_lookahead
= 0;
1393 int show_function
= 0;
1394 struct userdiff_driver
*textconv
= NULL
;
1395 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
1399 opt
->output
= std_output
;
1401 if (opt
->pre_context
|| opt
->post_context
|| opt
->file_break
||
1403 /* Show hunk marks, except for the first file. */
1404 if (opt
->last_shown
)
1405 opt
->show_hunk_mark
= 1;
1407 * If we're using threads then we can't easily identify
1408 * the first file. Always put hunk marks in that case
1409 * and skip the very first one later in work_done().
1411 if (opt
->output
!= std_output
)
1412 opt
->show_hunk_mark
= 1;
1414 opt
->last_shown
= 0;
1416 if (opt
->allow_textconv
) {
1417 grep_source_load_driver(gs
);
1419 * We might set up the shared textconv cache data here, which
1420 * is not thread-safe.
1423 textconv
= userdiff_get_textconv(gs
->driver
);
1428 * We know the result of a textconv is text, so we only have to care
1429 * about binary handling if we are not using it.
1432 switch (opt
->binary
) {
1433 case GREP_BINARY_DEFAULT
:
1434 if (grep_source_is_binary(gs
))
1435 binary_match_only
= 1;
1437 case GREP_BINARY_NOMATCH
:
1438 if (grep_source_is_binary(gs
))
1439 return 0; /* Assume unmatch */
1441 case GREP_BINARY_TEXT
:
1444 die("bug: unknown binary handling mode");
1448 memset(&xecfg
, 0, sizeof(xecfg
));
1451 try_lookahead
= should_lookahead(opt
);
1453 if (fill_textconv_grep(textconv
, gs
) < 0)
1463 * look_ahead() skips quickly to the line that possibly
1464 * has the next hit; don't call it if we need to do
1465 * something more than just skipping the current line
1466 * in response to an unmatch for the current line. E.g.
1467 * inside a post-context window, we will show the current
1468 * line as a context around the previous hit when it
1473 && (show_function
||
1474 lno
<= last_hit
+ opt
->post_context
))
1475 && look_ahead(opt
, &left
, &lno
, &bol
))
1477 eol
= end_of_line(bol
, &left
);
1481 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
1482 ctx
= GREP_CONTEXT_BODY
;
1484 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
1490 /* "grep -v -e foo -e bla" should list lines
1491 * that do not have either, so inversion should
1496 if (opt
->unmatch_name_only
) {
1503 if (opt
->status_only
)
1505 if (opt
->name_only
) {
1506 show_name(opt
, gs
->name
);
1511 if (binary_match_only
) {
1512 opt
->output(opt
, "Binary file ", 12);
1513 output_color(opt
, gs
->name
, strlen(gs
->name
),
1514 opt
->color_filename
);
1515 opt
->output(opt
, " matches\n", 9);
1518 /* Hit at this line. If we haven't shown the
1519 * pre-context lines, we would need to show them.
1521 if (opt
->pre_context
|| opt
->funcbody
)
1522 show_pre_context(opt
, gs
, bol
, eol
, lno
);
1523 else if (opt
->funcname
)
1524 show_funcname_line(opt
, gs
, bol
, lno
);
1525 show_line(opt
, bol
, eol
, gs
->name
, lno
, ':');
1531 if (show_function
&& match_funcname(opt
, gs
, bol
, eol
))
1533 if (show_function
||
1534 (last_hit
&& lno
<= last_hit
+ opt
->post_context
)) {
1535 /* If the last hit is within the post context,
1536 * we need to show this line.
1538 show_line(opt
, bol
, eol
, gs
->name
, lno
, '-');
1552 if (opt
->status_only
)
1554 if (opt
->unmatch_name_only
) {
1555 /* We did not see any hit, so we want to show this */
1556 show_name(opt
, gs
->name
);
1560 xdiff_clear_find_func(&xecfg
);
1564 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1565 * which feels mostly useless but sometimes useful. Maybe
1566 * make it another option? For now suppress them.
1568 if (opt
->count
&& count
) {
1570 if (opt
->pathname
) {
1571 output_color(opt
, gs
->name
, strlen(gs
->name
),
1572 opt
->color_filename
);
1573 output_sep(opt
, ':');
1575 snprintf(buf
, sizeof(buf
), "%u\n", count
);
1576 opt
->output(opt
, buf
, strlen(buf
));
1582 static void clr_hit_marker(struct grep_expr
*x
)
1584 /* All-hit markers are meaningful only at the very top level
1589 if (x
->node
!= GREP_NODE_OR
)
1591 x
->u
.binary
.left
->hit
= 0;
1592 x
= x
->u
.binary
.right
;
1596 static int chk_hit_marker(struct grep_expr
*x
)
1598 /* Top level nodes have hit markers. See if they all are hits */
1600 if (x
->node
!= GREP_NODE_OR
)
1602 if (!x
->u
.binary
.left
->hit
)
1604 x
= x
->u
.binary
.right
;
1608 int grep_source(struct grep_opt
*opt
, struct grep_source
*gs
)
1611 * we do not have to do the two-pass grep when we do not check
1612 * buffer-wide "all-match".
1614 if (!opt
->all_match
)
1615 return grep_source_1(opt
, gs
, 0);
1617 /* Otherwise the toplevel "or" terms hit a bit differently.
1618 * We first clear hit markers from them.
1620 clr_hit_marker(opt
->pattern_expression
);
1621 grep_source_1(opt
, gs
, 1);
1623 if (!chk_hit_marker(opt
->pattern_expression
))
1626 return grep_source_1(opt
, gs
, 0);
1629 int grep_buffer(struct grep_opt
*opt
, char *buf
, unsigned long size
)
1631 struct grep_source gs
;
1634 grep_source_init(&gs
, GREP_SOURCE_BUF
, NULL
, NULL
, NULL
);
1638 r
= grep_source(opt
, &gs
);
1640 grep_source_clear(&gs
);
1644 void grep_source_init(struct grep_source
*gs
, enum grep_source_type type
,
1645 const char *name
, const char *path
,
1646 const void *identifier
)
1649 gs
->name
= name
? xstrdup(name
) : NULL
;
1650 gs
->path
= path
? xstrdup(path
) : NULL
;
1656 case GREP_SOURCE_FILE
:
1657 gs
->identifier
= xstrdup(identifier
);
1659 case GREP_SOURCE_SHA1
:
1660 gs
->identifier
= xmalloc(20);
1661 hashcpy(gs
->identifier
, identifier
);
1663 case GREP_SOURCE_BUF
:
1664 gs
->identifier
= NULL
;
1668 void grep_source_clear(struct grep_source
*gs
)
1674 free(gs
->identifier
);
1675 gs
->identifier
= NULL
;
1676 grep_source_clear_data(gs
);
1679 void grep_source_clear_data(struct grep_source
*gs
)
1682 case GREP_SOURCE_FILE
:
1683 case GREP_SOURCE_SHA1
:
1688 case GREP_SOURCE_BUF
:
1689 /* leave user-provided buf intact */
1694 static int grep_source_load_sha1(struct grep_source
*gs
)
1696 enum object_type type
;
1699 gs
->buf
= read_sha1_file(gs
->identifier
, &type
, &gs
->size
);
1703 return error(_("'%s': unable to read %s"),
1705 sha1_to_hex(gs
->identifier
));
1709 static int grep_source_load_file(struct grep_source
*gs
)
1711 const char *filename
= gs
->identifier
;
1717 if (lstat(filename
, &st
) < 0) {
1719 if (errno
!= ENOENT
)
1720 error(_("'%s': %s"), filename
, strerror(errno
));
1723 if (!S_ISREG(st
.st_mode
))
1725 size
= xsize_t(st
.st_size
);
1726 i
= open(filename
, O_RDONLY
);
1729 data
= xmalloc(size
+ 1);
1730 if (st
.st_size
!= read_in_full(i
, data
, size
)) {
1731 error(_("'%s': short read %s"), filename
, strerror(errno
));
1744 static int grep_source_load(struct grep_source
*gs
)
1750 case GREP_SOURCE_FILE
:
1751 return grep_source_load_file(gs
);
1752 case GREP_SOURCE_SHA1
:
1753 return grep_source_load_sha1(gs
);
1754 case GREP_SOURCE_BUF
:
1755 return gs
->buf
? 0 : -1;
1757 die("BUG: invalid grep_source type");
1760 void grep_source_load_driver(struct grep_source
*gs
)
1767 gs
->driver
= userdiff_find_by_path(gs
->path
);
1769 gs
->driver
= userdiff_find_by_name("default");
1773 static int grep_source_is_binary(struct grep_source
*gs
)
1775 grep_source_load_driver(gs
);
1776 if (gs
->driver
->binary
!= -1)
1777 return gs
->driver
->binary
;
1779 if (!grep_source_load(gs
))
1780 return buffer_is_binary(gs
->buf
, gs
->size
);