4 #include "xdiff-interface.h"
10 static int grep_source_load(struct grep_source
*gs
);
11 static int grep_source_is_binary(struct grep_source
*gs
);
13 static struct grep_opt grep_defaults
;
16 * Initialize the grep_defaults template with hardcoded defaults.
17 * We could let the compiler do this, but without C99 initializers
18 * the code gets unwieldy and unreadable, so...
20 void init_grep_defaults(void)
22 struct grep_opt
*opt
= &grep_defaults
;
29 memset(opt
, 0, sizeof(*opt
));
32 opt
->regflags
= REG_NEWLINE
;
34 opt
->pattern_type_option
= GREP_PATTERN_TYPE_UNSPECIFIED
;
35 opt
->extended_regexp_option
= 0;
36 color_set(opt
->color_context
, "");
37 color_set(opt
->color_filename
, "");
38 color_set(opt
->color_function
, "");
39 color_set(opt
->color_lineno
, "");
40 color_set(opt
->color_match_context
, GIT_COLOR_BOLD_RED
);
41 color_set(opt
->color_match_selected
, GIT_COLOR_BOLD_RED
);
42 color_set(opt
->color_selected
, "");
43 color_set(opt
->color_sep
, GIT_COLOR_CYAN
);
47 static int parse_pattern_type_arg(const char *opt
, const char *arg
)
49 if (!strcmp(arg
, "default"))
50 return GREP_PATTERN_TYPE_UNSPECIFIED
;
51 else if (!strcmp(arg
, "basic"))
52 return GREP_PATTERN_TYPE_BRE
;
53 else if (!strcmp(arg
, "extended"))
54 return GREP_PATTERN_TYPE_ERE
;
55 else if (!strcmp(arg
, "fixed"))
56 return GREP_PATTERN_TYPE_FIXED
;
57 else if (!strcmp(arg
, "perl"))
58 return GREP_PATTERN_TYPE_PCRE
;
59 die("bad %s argument: %s", opt
, arg
);
63 * Read the configuration file once and store it in
64 * the grep_defaults template.
66 int grep_config(const char *var
, const char *value
, void *cb
)
68 struct grep_opt
*opt
= &grep_defaults
;
71 if (userdiff_config(var
, value
) < 0)
74 if (!strcmp(var
, "grep.extendedregexp")) {
75 if (git_config_bool(var
, value
))
76 opt
->extended_regexp_option
= 1;
78 opt
->extended_regexp_option
= 0;
82 if (!strcmp(var
, "grep.patterntype")) {
83 opt
->pattern_type_option
= parse_pattern_type_arg(var
, value
);
87 if (!strcmp(var
, "grep.linenumber")) {
88 opt
->linenum
= git_config_bool(var
, value
);
92 if (!strcmp(var
, "grep.fullname")) {
93 opt
->relative
= !git_config_bool(var
, value
);
97 if (!strcmp(var
, "color.grep"))
98 opt
->color
= git_config_colorbool(var
, value
);
99 else if (!strcmp(var
, "color.grep.context"))
100 color
= opt
->color_context
;
101 else if (!strcmp(var
, "color.grep.filename"))
102 color
= opt
->color_filename
;
103 else if (!strcmp(var
, "color.grep.function"))
104 color
= opt
->color_function
;
105 else if (!strcmp(var
, "color.grep.linenumber"))
106 color
= opt
->color_lineno
;
107 else if (!strcmp(var
, "color.grep.matchcontext"))
108 color
= opt
->color_match_context
;
109 else if (!strcmp(var
, "color.grep.matchselected"))
110 color
= opt
->color_match_selected
;
111 else if (!strcmp(var
, "color.grep.selected"))
112 color
= opt
->color_selected
;
113 else if (!strcmp(var
, "color.grep.separator"))
114 color
= opt
->color_sep
;
115 else if (!strcmp(var
, "color.grep.match")) {
118 return config_error_nonbool(var
);
119 rc
|= color_parse(value
, opt
->color_match_context
);
120 rc
|= color_parse(value
, opt
->color_match_selected
);
126 return config_error_nonbool(var
);
127 return color_parse(value
, color
);
133 * Initialize one instance of grep_opt and copy the
134 * default values from the template we read the configuration
135 * information in an earlier call to git_config(grep_config).
137 void grep_init(struct grep_opt
*opt
, const char *prefix
)
139 struct grep_opt
*def
= &grep_defaults
;
141 memset(opt
, 0, sizeof(*opt
));
142 opt
->prefix
= prefix
;
143 opt
->prefix_length
= (prefix
&& *prefix
) ? strlen(prefix
) : 0;
144 opt
->pattern_tail
= &opt
->pattern_list
;
145 opt
->header_tail
= &opt
->header_list
;
147 opt
->color
= def
->color
;
148 opt
->extended_regexp_option
= def
->extended_regexp_option
;
149 opt
->pattern_type_option
= def
->pattern_type_option
;
150 opt
->linenum
= def
->linenum
;
151 opt
->max_depth
= def
->max_depth
;
152 opt
->pathname
= def
->pathname
;
153 opt
->regflags
= def
->regflags
;
154 opt
->relative
= def
->relative
;
156 color_set(opt
->color_context
, def
->color_context
);
157 color_set(opt
->color_filename
, def
->color_filename
);
158 color_set(opt
->color_function
, def
->color_function
);
159 color_set(opt
->color_lineno
, def
->color_lineno
);
160 color_set(opt
->color_match_context
, def
->color_match_context
);
161 color_set(opt
->color_match_selected
, def
->color_match_selected
);
162 color_set(opt
->color_selected
, def
->color_selected
);
163 color_set(opt
->color_sep
, def
->color_sep
);
166 void grep_commit_pattern_type(enum grep_pattern_type pattern_type
, struct grep_opt
*opt
)
168 if (pattern_type
!= GREP_PATTERN_TYPE_UNSPECIFIED
)
169 grep_set_pattern_type_option(pattern_type
, opt
);
170 else if (opt
->pattern_type_option
!= GREP_PATTERN_TYPE_UNSPECIFIED
)
171 grep_set_pattern_type_option(opt
->pattern_type_option
, opt
);
172 else if (opt
->extended_regexp_option
)
173 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE
, opt
);
176 void grep_set_pattern_type_option(enum grep_pattern_type pattern_type
, struct grep_opt
*opt
)
178 switch (pattern_type
) {
179 case GREP_PATTERN_TYPE_UNSPECIFIED
:
182 case GREP_PATTERN_TYPE_BRE
:
185 opt
->regflags
&= ~REG_EXTENDED
;
188 case GREP_PATTERN_TYPE_ERE
:
191 opt
->regflags
|= REG_EXTENDED
;
194 case GREP_PATTERN_TYPE_FIXED
:
197 opt
->regflags
&= ~REG_EXTENDED
;
200 case GREP_PATTERN_TYPE_PCRE
:
203 opt
->regflags
&= ~REG_EXTENDED
;
208 static struct grep_pat
*create_grep_pat(const char *pat
, size_t patlen
,
209 const char *origin
, int no
,
210 enum grep_pat_token t
,
211 enum grep_header_field field
)
213 struct grep_pat
*p
= xcalloc(1, sizeof(*p
));
214 p
->pattern
= xmemdupz(pat
, patlen
);
215 p
->patternlen
= patlen
;
223 static void do_append_grep_pat(struct grep_pat
***tail
, struct grep_pat
*p
)
230 case GREP_PATTERN
: /* atom */
231 case GREP_PATTERN_HEAD
:
232 case GREP_PATTERN_BODY
:
234 struct grep_pat
*new_pat
;
236 char *cp
= p
->pattern
+ p
->patternlen
, *nl
= NULL
;
237 while (++len
<= p
->patternlen
) {
238 if (*(--cp
) == '\n') {
245 new_pat
= create_grep_pat(nl
+ 1, len
- 1, p
->origin
,
246 p
->no
, p
->token
, p
->field
);
247 new_pat
->next
= p
->next
;
249 *tail
= &new_pat
->next
;
252 p
->patternlen
-= len
;
260 void append_header_grep_pattern(struct grep_opt
*opt
,
261 enum grep_header_field field
, const char *pat
)
263 struct grep_pat
*p
= create_grep_pat(pat
, strlen(pat
), "header", 0,
264 GREP_PATTERN_HEAD
, field
);
265 if (field
== GREP_HEADER_REFLOG
)
266 opt
->use_reflog_filter
= 1;
267 do_append_grep_pat(&opt
->header_tail
, p
);
270 void append_grep_pattern(struct grep_opt
*opt
, const char *pat
,
271 const char *origin
, int no
, enum grep_pat_token t
)
273 append_grep_pat(opt
, pat
, strlen(pat
), origin
, no
, t
);
276 void append_grep_pat(struct grep_opt
*opt
, const char *pat
, size_t patlen
,
277 const char *origin
, int no
, enum grep_pat_token t
)
279 struct grep_pat
*p
= create_grep_pat(pat
, patlen
, origin
, no
, t
, 0);
280 do_append_grep_pat(&opt
->pattern_tail
, p
);
283 struct grep_opt
*grep_opt_dup(const struct grep_opt
*opt
)
285 struct grep_pat
*pat
;
286 struct grep_opt
*ret
= xmalloc(sizeof(struct grep_opt
));
289 ret
->pattern_list
= NULL
;
290 ret
->pattern_tail
= &ret
->pattern_list
;
292 for(pat
= opt
->pattern_list
; pat
!= NULL
; pat
= pat
->next
)
294 if(pat
->token
== GREP_PATTERN_HEAD
)
295 append_header_grep_pattern(ret
, pat
->field
,
298 append_grep_pat(ret
, pat
->pattern
, pat
->patternlen
,
299 pat
->origin
, pat
->no
, pat
->token
);
305 static NORETURN
void compile_regexp_failed(const struct grep_pat
*p
,
311 xsnprintf(where
, sizeof(where
), "In '%s' at %d, ", p
->origin
, p
->no
);
313 xsnprintf(where
, sizeof(where
), "%s, ", p
->origin
);
317 die("%s'%s': %s", where
, p
->pattern
, error
);
321 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
325 int options
= PCRE_MULTILINE
;
327 if (opt
->ignore_case
)
328 options
|= PCRE_CASELESS
;
330 p
->pcre_regexp
= pcre_compile(p
->pattern
, options
, &error
, &erroffset
,
333 compile_regexp_failed(p
, error
);
335 p
->pcre_extra_info
= pcre_study(p
->pcre_regexp
, 0, &error
);
336 if (!p
->pcre_extra_info
&& error
)
340 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
341 regmatch_t
*match
, int eflags
)
343 int ovector
[30], ret
, flags
= 0;
345 if (eflags
& REG_NOTBOL
)
346 flags
|= PCRE_NOTBOL
;
348 ret
= pcre_exec(p
->pcre_regexp
, p
->pcre_extra_info
, line
, eol
- line
,
349 0, flags
, ovector
, ARRAY_SIZE(ovector
));
350 if (ret
< 0 && ret
!= PCRE_ERROR_NOMATCH
)
351 die("pcre_exec failed with error code %d", ret
);
354 match
->rm_so
= ovector
[0];
355 match
->rm_eo
= ovector
[1];
361 static void free_pcre_regexp(struct grep_pat
*p
)
363 pcre_free(p
->pcre_regexp
);
364 pcre_free(p
->pcre_extra_info
);
366 #else /* !USE_LIBPCRE */
367 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
369 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
372 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
373 regmatch_t
*match
, int eflags
)
378 static void free_pcre_regexp(struct grep_pat
*p
)
381 #endif /* !USE_LIBPCRE */
383 static int is_fixed(const char *s
, size_t len
)
387 /* regcomp cannot accept patterns with NULs so we
388 * consider any pattern containing a NUL fixed.
390 if (memchr(s
, 0, len
))
393 for (i
= 0; i
< len
; i
++) {
394 if (is_regex_special(s
[i
]))
401 static void compile_fixed_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
403 struct strbuf sb
= STRBUF_INIT
;
407 basic_regex_quote_buf(&sb
, p
->pattern
);
408 regflags
= opt
->regflags
& ~REG_EXTENDED
;
409 if (opt
->ignore_case
)
410 regflags
|= REG_ICASE
;
411 err
= regcomp(&p
->regexp
, sb
.buf
, regflags
);
413 fprintf(stderr
, "fixed %s\n", sb
.buf
);
417 regerror(err
, &p
->regexp
, errbuf
, sizeof(errbuf
));
419 compile_regexp_failed(p
, errbuf
);
423 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
425 int icase
, ascii_only
;
428 p
->word_regexp
= opt
->word_regexp
;
429 p
->ignore_case
= opt
->ignore_case
;
430 icase
= opt
->regflags
& REG_ICASE
|| p
->ignore_case
;
431 ascii_only
= !has_non_ascii(p
->pattern
);
434 * Even when -F (fixed) asks us to do a non-regexp search, we
435 * may not be able to correctly case-fold when -i
436 * (ignore-case) is asked (in which case, we'll synthesize a
437 * regexp to match the pattern that matches regexp special
438 * characters literally, while ignoring case differences). On
439 * the other hand, even without -F, if the pattern does not
440 * have any regexp special characters and there is no need for
441 * case-folding search, we can internally turn it into a
442 * simple string match using kws. p->fixed tells us if we
445 if (opt
->fixed
|| is_fixed(p
->pattern
, p
->patternlen
))
446 p
->fixed
= !icase
|| ascii_only
;
451 if (opt
->regflags
& REG_ICASE
|| p
->ignore_case
)
452 p
->kws
= kwsalloc(tolower_trans_tbl
);
454 p
->kws
= kwsalloc(NULL
);
455 kwsincr(p
->kws
, p
->pattern
, p
->patternlen
);
458 } else if (opt
->fixed
) {
460 * We come here when the pattern has the non-ascii
461 * characters we cannot case-fold, and asked to
464 compile_fixed_regexp(p
, opt
);
469 compile_pcre_regexp(p
, opt
);
473 err
= regcomp(&p
->regexp
, p
->pattern
, opt
->regflags
);
476 regerror(err
, &p
->regexp
, errbuf
, 1024);
478 compile_regexp_failed(p
, errbuf
);
482 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
483 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
492 case GREP_PATTERN
: /* atom */
493 case GREP_PATTERN_HEAD
:
494 case GREP_PATTERN_BODY
:
495 x
= xcalloc(1, sizeof (struct grep_expr
));
496 x
->node
= GREP_NODE_ATOM
;
500 case GREP_OPEN_PAREN
:
502 x
= compile_pattern_or(list
);
503 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
504 die("unmatched parenthesis");
505 *list
= (*list
)->next
;
512 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
523 die("--not not followed by pattern expression");
525 x
= xcalloc(1, sizeof (struct grep_expr
));
526 x
->node
= GREP_NODE_NOT
;
527 x
->u
.unary
= compile_pattern_not(list
);
529 die("--not followed by non pattern expression");
532 return compile_pattern_atom(list
);
536 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
539 struct grep_expr
*x
, *y
, *z
;
541 x
= compile_pattern_not(list
);
543 if (p
&& p
->token
== GREP_AND
) {
545 die("--and not followed by pattern expression");
547 y
= compile_pattern_and(list
);
549 die("--and not followed by pattern expression");
550 z
= xcalloc(1, sizeof (struct grep_expr
));
551 z
->node
= GREP_NODE_AND
;
552 z
->u
.binary
.left
= x
;
553 z
->u
.binary
.right
= y
;
559 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
562 struct grep_expr
*x
, *y
, *z
;
564 x
= compile_pattern_and(list
);
566 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
567 y
= compile_pattern_or(list
);
569 die("not a pattern expression %s", p
->pattern
);
570 z
= xcalloc(1, sizeof (struct grep_expr
));
571 z
->node
= GREP_NODE_OR
;
572 z
->u
.binary
.left
= x
;
573 z
->u
.binary
.right
= y
;
579 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
581 return compile_pattern_or(list
);
584 static void indent(int in
)
590 static void dump_grep_pat(struct grep_pat
*p
)
593 case GREP_AND
: fprintf(stderr
, "*and*"); break;
594 case GREP_OPEN_PAREN
: fprintf(stderr
, "*(*"); break;
595 case GREP_CLOSE_PAREN
: fprintf(stderr
, "*)*"); break;
596 case GREP_NOT
: fprintf(stderr
, "*not*"); break;
597 case GREP_OR
: fprintf(stderr
, "*or*"); break;
599 case GREP_PATTERN
: fprintf(stderr
, "pattern"); break;
600 case GREP_PATTERN_HEAD
: fprintf(stderr
, "pattern_head"); break;
601 case GREP_PATTERN_BODY
: fprintf(stderr
, "pattern_body"); break;
606 case GREP_PATTERN_HEAD
:
607 fprintf(stderr
, "<head %d>", p
->field
); break;
608 case GREP_PATTERN_BODY
:
609 fprintf(stderr
, "<body>"); break;
613 case GREP_PATTERN_HEAD
:
614 case GREP_PATTERN_BODY
:
616 fprintf(stderr
, "%.*s", (int)p
->patternlen
, p
->pattern
);
622 static void dump_grep_expression_1(struct grep_expr
*x
, int in
)
627 fprintf(stderr
, "true\n");
630 dump_grep_pat(x
->u
.atom
);
633 fprintf(stderr
, "(not\n");
634 dump_grep_expression_1(x
->u
.unary
, in
+1);
636 fprintf(stderr
, ")\n");
639 fprintf(stderr
, "(and\n");
640 dump_grep_expression_1(x
->u
.binary
.left
, in
+1);
641 dump_grep_expression_1(x
->u
.binary
.right
, in
+1);
643 fprintf(stderr
, ")\n");
646 fprintf(stderr
, "(or\n");
647 dump_grep_expression_1(x
->u
.binary
.left
, in
+1);
648 dump_grep_expression_1(x
->u
.binary
.right
, in
+1);
650 fprintf(stderr
, ")\n");
655 static void dump_grep_expression(struct grep_opt
*opt
)
657 struct grep_expr
*x
= opt
->pattern_expression
;
660 fprintf(stderr
, "[all-match]\n");
661 dump_grep_expression_1(x
, 0);
665 static struct grep_expr
*grep_true_expr(void)
667 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
668 z
->node
= GREP_NODE_TRUE
;
672 static struct grep_expr
*grep_or_expr(struct grep_expr
*left
, struct grep_expr
*right
)
674 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
675 z
->node
= GREP_NODE_OR
;
676 z
->u
.binary
.left
= left
;
677 z
->u
.binary
.right
= right
;
681 static struct grep_expr
*prep_header_patterns(struct grep_opt
*opt
)
684 struct grep_expr
*header_expr
;
685 struct grep_expr
*(header_group
[GREP_HEADER_FIELD_MAX
]);
686 enum grep_header_field fld
;
688 if (!opt
->header_list
)
691 for (p
= opt
->header_list
; p
; p
= p
->next
) {
692 if (p
->token
!= GREP_PATTERN_HEAD
)
693 die("bug: a non-header pattern in grep header list.");
694 if (p
->field
< GREP_HEADER_FIELD_MIN
||
695 GREP_HEADER_FIELD_MAX
<= p
->field
)
696 die("bug: unknown header field %d", p
->field
);
697 compile_regexp(p
, opt
);
700 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++)
701 header_group
[fld
] = NULL
;
703 for (p
= opt
->header_list
; p
; p
= p
->next
) {
705 struct grep_pat
*pp
= p
;
707 h
= compile_pattern_atom(&pp
);
708 if (!h
|| pp
!= p
->next
)
709 die("bug: malformed header expr");
710 if (!header_group
[p
->field
]) {
711 header_group
[p
->field
] = h
;
714 header_group
[p
->field
] = grep_or_expr(h
, header_group
[p
->field
]);
719 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++) {
720 if (!header_group
[fld
])
723 header_expr
= grep_true_expr();
724 header_expr
= grep_or_expr(header_group
[fld
], header_expr
);
729 static struct grep_expr
*grep_splice_or(struct grep_expr
*x
, struct grep_expr
*y
)
731 struct grep_expr
*z
= x
;
734 assert(x
->node
== GREP_NODE_OR
);
735 if (x
->u
.binary
.right
&&
736 x
->u
.binary
.right
->node
== GREP_NODE_TRUE
) {
737 x
->u
.binary
.right
= y
;
740 x
= x
->u
.binary
.right
;
745 static void compile_grep_patterns_real(struct grep_opt
*opt
)
748 struct grep_expr
*header_expr
= prep_header_patterns(opt
);
750 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
752 case GREP_PATTERN
: /* atom */
753 case GREP_PATTERN_HEAD
:
754 case GREP_PATTERN_BODY
:
755 compile_regexp(p
, opt
);
763 if (opt
->all_match
|| header_expr
)
765 else if (!opt
->extended
&& !opt
->debug
)
768 p
= opt
->pattern_list
;
770 opt
->pattern_expression
= compile_pattern_expr(&p
);
772 die("incomplete pattern expression: %s", p
->pattern
);
777 if (!opt
->pattern_expression
)
778 opt
->pattern_expression
= header_expr
;
779 else if (opt
->all_match
)
780 opt
->pattern_expression
= grep_splice_or(header_expr
,
781 opt
->pattern_expression
);
783 opt
->pattern_expression
= grep_or_expr(opt
->pattern_expression
,
788 void compile_grep_patterns(struct grep_opt
*opt
)
790 compile_grep_patterns_real(opt
);
792 dump_grep_expression(opt
);
795 static void free_pattern_expr(struct grep_expr
*x
)
802 free_pattern_expr(x
->u
.unary
);
806 free_pattern_expr(x
->u
.binary
.left
);
807 free_pattern_expr(x
->u
.binary
.right
);
813 void free_grep_patterns(struct grep_opt
*opt
)
815 struct grep_pat
*p
, *n
;
817 for (p
= opt
->pattern_list
; p
; p
= n
) {
820 case GREP_PATTERN
: /* atom */
821 case GREP_PATTERN_HEAD
:
822 case GREP_PATTERN_BODY
:
825 else if (p
->pcre_regexp
)
839 free_pattern_expr(opt
->pattern_expression
);
842 static char *end_of_line(char *cp
, unsigned long *left
)
844 unsigned long l
= *left
;
845 while (l
&& *cp
!= '\n') {
853 static int word_char(char ch
)
855 return isalnum(ch
) || ch
== '_';
858 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
861 if (want_color(opt
->color
) && color
&& color
[0]) {
862 opt
->output(opt
, color
, strlen(color
));
863 opt
->output(opt
, data
, size
);
864 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
866 opt
->output(opt
, data
, size
);
869 static void output_sep(struct grep_opt
*opt
, char sign
)
871 if (opt
->null_following_name
)
872 opt
->output(opt
, "\0", 1);
874 output_color(opt
, &sign
, 1, opt
->color_sep
);
877 static void show_name(struct grep_opt
*opt
, const char *name
)
879 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
880 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
883 static int fixmatch(struct grep_pat
*p
, char *line
, char *eol
,
886 struct kwsmatch kwsm
;
887 size_t offset
= kwsexec(p
->kws
, line
, eol
- line
, &kwsm
);
889 match
->rm_so
= match
->rm_eo
= -1;
892 match
->rm_so
= offset
;
893 match
->rm_eo
= match
->rm_so
+ kwsm
.size
[0];
898 static int regmatch(const regex_t
*preg
, char *line
, char *eol
,
899 regmatch_t
*match
, int eflags
)
903 match
->rm_eo
= eol
- line
;
904 eflags
|= REG_STARTEND
;
906 return regexec(preg
, line
, 1, match
, eflags
);
909 static int patmatch(struct grep_pat
*p
, char *line
, char *eol
,
910 regmatch_t
*match
, int eflags
)
915 hit
= !fixmatch(p
, line
, eol
, match
);
916 else if (p
->pcre_regexp
)
917 hit
= !pcrematch(p
, line
, eol
, match
, eflags
);
919 hit
= !regmatch(&p
->regexp
, line
, eol
, match
, eflags
);
924 static int strip_timestamp(char *bol
, char **eol_p
)
929 while (bol
< --eol
) {
945 { "committer ", 10 },
949 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
950 enum grep_context ctx
,
951 regmatch_t
*pmatch
, int eflags
)
955 const char *start
= bol
;
957 if ((p
->token
!= GREP_PATTERN
) &&
958 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
961 if (p
->token
== GREP_PATTERN_HEAD
) {
964 assert(p
->field
< ARRAY_SIZE(header_field
));
965 field
= header_field
[p
->field
].field
;
966 len
= header_field
[p
->field
].len
;
967 if (strncmp(bol
, field
, len
))
971 case GREP_HEADER_AUTHOR
:
972 case GREP_HEADER_COMMITTER
:
973 saved_ch
= strip_timestamp(bol
, &eol
);
981 hit
= patmatch(p
, bol
, eol
, pmatch
, eflags
);
983 if (hit
&& p
->word_regexp
) {
984 if ((pmatch
[0].rm_so
< 0) ||
985 (eol
- bol
) < pmatch
[0].rm_so
||
986 (pmatch
[0].rm_eo
< 0) ||
987 (eol
- bol
) < pmatch
[0].rm_eo
)
988 die("regexp returned nonsense");
990 /* Match beginning must be either beginning of the
991 * line, or at word boundary (i.e. the last char must
992 * not be a word char). Similarly, match end must be
993 * either end of the line, or at word boundary
994 * (i.e. the next char must not be a word char).
996 if ( ((pmatch
[0].rm_so
== 0) ||
997 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
998 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
999 !word_char(bol
[pmatch
[0].rm_eo
])) )
1004 /* Words consist of at least one character. */
1005 if (pmatch
->rm_so
== pmatch
->rm_eo
)
1008 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
1009 /* There could be more than one match on the
1010 * line, and the first match might not be
1011 * strict word match. But later ones could be!
1012 * Forward to the next possible start, i.e. the
1013 * next position following a non-word char.
1015 bol
= pmatch
[0].rm_so
+ bol
+ 1;
1016 while (word_char(bol
[-1]) && bol
< eol
)
1018 eflags
|= REG_NOTBOL
;
1023 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
1026 pmatch
[0].rm_so
+= bol
- start
;
1027 pmatch
[0].rm_eo
+= bol
- start
;
1032 static int match_expr_eval(struct grep_expr
*x
, char *bol
, char *eol
,
1033 enum grep_context ctx
, int collect_hits
)
1039 die("Not a valid grep expression");
1041 case GREP_NODE_TRUE
:
1044 case GREP_NODE_ATOM
:
1045 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
, &match
, 0);
1048 h
= !match_expr_eval(x
->u
.unary
, bol
, eol
, ctx
, 0);
1051 if (!match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0))
1053 h
= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
1057 return (match_expr_eval(x
->u
.binary
.left
,
1058 bol
, eol
, ctx
, 0) ||
1059 match_expr_eval(x
->u
.binary
.right
,
1061 h
= match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
1062 x
->u
.binary
.left
->hit
|= h
;
1063 h
|= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
1066 die("Unexpected node type (internal error) %d", x
->node
);
1073 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
1074 enum grep_context ctx
, int collect_hits
)
1076 struct grep_expr
*x
= opt
->pattern_expression
;
1077 return match_expr_eval(x
, bol
, eol
, ctx
, collect_hits
);
1080 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
1081 enum grep_context ctx
, int collect_hits
)
1087 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
1089 /* we do not call with collect_hits without being extended */
1090 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1091 if (match_one_pattern(p
, bol
, eol
, ctx
, &match
, 0))
1097 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
1098 enum grep_context ctx
,
1099 regmatch_t
*pmatch
, int eflags
)
1103 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
1105 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
1107 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
1108 if (match
.rm_so
> pmatch
->rm_so
)
1110 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
1113 pmatch
->rm_so
= match
.rm_so
;
1114 pmatch
->rm_eo
= match
.rm_eo
;
1118 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
1119 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
1124 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
1126 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1128 case GREP_PATTERN
: /* atom */
1129 case GREP_PATTERN_HEAD
:
1130 case GREP_PATTERN_BODY
:
1131 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
1142 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
1143 const char *name
, unsigned lno
, char sign
)
1145 int rest
= eol
- bol
;
1146 const char *match_color
, *line_color
= NULL
;
1148 if (opt
->file_break
&& opt
->last_shown
== 0) {
1149 if (opt
->show_hunk_mark
)
1150 opt
->output(opt
, "\n", 1);
1151 } else if (opt
->pre_context
|| opt
->post_context
|| opt
->funcbody
) {
1152 if (opt
->last_shown
== 0) {
1153 if (opt
->show_hunk_mark
) {
1154 output_color(opt
, "--", 2, opt
->color_sep
);
1155 opt
->output(opt
, "\n", 1);
1157 } else if (lno
> opt
->last_shown
+ 1) {
1158 output_color(opt
, "--", 2, opt
->color_sep
);
1159 opt
->output(opt
, "\n", 1);
1162 if (opt
->heading
&& opt
->last_shown
== 0) {
1163 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
1164 opt
->output(opt
, "\n", 1);
1166 opt
->last_shown
= lno
;
1168 if (!opt
->heading
&& opt
->pathname
) {
1169 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
1170 output_sep(opt
, sign
);
1174 snprintf(buf
, sizeof(buf
), "%d", lno
);
1175 output_color(opt
, buf
, strlen(buf
), opt
->color_lineno
);
1176 output_sep(opt
, sign
);
1180 enum grep_context ctx
= GREP_CONTEXT_BODY
;
1185 match_color
= opt
->color_match_selected
;
1187 match_color
= opt
->color_match_context
;
1189 line_color
= opt
->color_selected
;
1190 else if (sign
== '-')
1191 line_color
= opt
->color_context
;
1192 else if (sign
== '=')
1193 line_color
= opt
->color_function
;
1195 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
1196 if (match
.rm_so
== match
.rm_eo
)
1199 output_color(opt
, bol
, match
.rm_so
, line_color
);
1200 output_color(opt
, bol
+ match
.rm_so
,
1201 match
.rm_eo
- match
.rm_so
, match_color
);
1203 rest
-= match
.rm_eo
;
1204 eflags
= REG_NOTBOL
;
1208 output_color(opt
, bol
, rest
, line_color
);
1209 opt
->output(opt
, "\n", 1);
1216 * This lock protects access to the gitattributes machinery, which is
1219 pthread_mutex_t grep_attr_mutex
;
1221 static inline void grep_attr_lock(void)
1224 pthread_mutex_lock(&grep_attr_mutex
);
1227 static inline void grep_attr_unlock(void)
1230 pthread_mutex_unlock(&grep_attr_mutex
);
1234 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
1236 pthread_mutex_t grep_read_mutex
;
1239 #define grep_attr_lock()
1240 #define grep_attr_unlock()
1243 static int match_funcname(struct grep_opt
*opt
, struct grep_source
*gs
, char *bol
, char *eol
)
1245 xdemitconf_t
*xecfg
= opt
->priv
;
1246 if (xecfg
&& !xecfg
->find_func
) {
1247 grep_source_load_driver(gs
);
1248 if (gs
->driver
->funcname
.pattern
) {
1249 const struct userdiff_funcname
*pe
= &gs
->driver
->funcname
;
1250 xdiff_set_find_func(xecfg
, pe
->pattern
, pe
->cflags
);
1252 xecfg
= opt
->priv
= NULL
;
1258 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
1259 xecfg
->find_func_priv
) >= 0;
1264 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
1269 static void show_funcname_line(struct grep_opt
*opt
, struct grep_source
*gs
,
1270 char *bol
, unsigned lno
)
1272 while (bol
> gs
->buf
) {
1275 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1279 if (lno
<= opt
->last_shown
)
1282 if (match_funcname(opt
, gs
, bol
, eol
)) {
1283 show_line(opt
, bol
, eol
, gs
->name
, lno
, '=');
1289 static void show_pre_context(struct grep_opt
*opt
, struct grep_source
*gs
,
1290 char *bol
, char *end
, unsigned lno
)
1292 unsigned cur
= lno
, from
= 1, funcname_lno
= 0;
1293 int funcname_needed
= !!opt
->funcname
;
1295 if (opt
->funcbody
&& !match_funcname(opt
, gs
, bol
, end
))
1296 funcname_needed
= 2;
1298 if (opt
->pre_context
< lno
)
1299 from
= lno
- opt
->pre_context
;
1300 if (from
<= opt
->last_shown
)
1301 from
= opt
->last_shown
+ 1;
1304 while (bol
> gs
->buf
&&
1305 cur
> (funcname_needed
== 2 ? opt
->last_shown
+ 1 : from
)) {
1308 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1311 if (funcname_needed
&& match_funcname(opt
, gs
, bol
, eol
)) {
1313 funcname_needed
= 0;
1317 /* We need to look even further back to find a function signature. */
1318 if (opt
->funcname
&& funcname_needed
)
1319 show_funcname_line(opt
, gs
, bol
, cur
);
1323 char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
1325 while (*eol
!= '\n')
1327 show_line(opt
, bol
, eol
, gs
->name
, cur
, sign
);
1333 static int should_lookahead(struct grep_opt
*opt
)
1338 return 0; /* punt for too complex stuff */
1341 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1342 if (p
->token
!= GREP_PATTERN
)
1343 return 0; /* punt for "header only" and stuff */
1348 static int look_ahead(struct grep_opt
*opt
,
1349 unsigned long *left_p
,
1353 unsigned lno
= *lno_p
;
1356 char *sp
, *last_bol
;
1357 regoff_t earliest
= -1;
1359 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1363 hit
= patmatch(p
, bol
, bol
+ *left_p
, &m
, 0);
1364 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
1366 if (earliest
< 0 || m
.rm_so
< earliest
)
1371 *bol_p
= bol
+ *left_p
;
1375 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
1376 ; /* find the beginning of the line */
1379 for (sp
= bol
; sp
< last_bol
; sp
++) {
1383 *left_p
-= last_bol
- bol
;
1389 static void std_output(struct grep_opt
*opt
, const void *buf
, size_t size
)
1391 fwrite(buf
, size
, 1, stdout
);
1394 static int fill_textconv_grep(struct userdiff_driver
*driver
,
1395 struct grep_source
*gs
)
1397 struct diff_filespec
*df
;
1401 if (!driver
|| !driver
->textconv
)
1402 return grep_source_load(gs
);
1405 * The textconv interface is intimately tied to diff_filespecs, so we
1406 * have to pretend to be one. If we could unify the grep_source
1407 * and diff_filespec structs, this mess could just go away.
1409 df
= alloc_filespec(gs
->path
);
1411 case GREP_SOURCE_SHA1
:
1412 fill_filespec(df
, gs
->identifier
, 1, 0100644);
1414 case GREP_SOURCE_FILE
:
1415 fill_filespec(df
, null_sha1
, 0, 0100644);
1418 die("BUG: attempt to textconv something without a path?");
1422 * fill_textconv is not remotely thread-safe; it may load objects
1423 * behind the scenes, and it modifies the global diff tempfile
1427 size
= fill_textconv(driver
, df
, &buf
);
1432 * The normal fill_textconv usage by the diff machinery would just keep
1433 * the textconv'd buf separate from the diff_filespec. But much of the
1434 * grep code passes around a grep_source and assumes that its "buf"
1435 * pointer is the beginning of the thing we are searching. So let's
1436 * install our textconv'd version into the grep_source, taking care not
1437 * to leak any existing buffer.
1439 grep_source_clear_data(gs
);
1446 static int grep_source_1(struct grep_opt
*opt
, struct grep_source
*gs
, int collect_hits
)
1451 unsigned last_hit
= 0;
1452 int binary_match_only
= 0;
1454 int try_lookahead
= 0;
1455 int show_function
= 0;
1456 struct userdiff_driver
*textconv
= NULL
;
1457 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
1461 opt
->output
= std_output
;
1463 if (opt
->pre_context
|| opt
->post_context
|| opt
->file_break
||
1465 /* Show hunk marks, except for the first file. */
1466 if (opt
->last_shown
)
1467 opt
->show_hunk_mark
= 1;
1469 * If we're using threads then we can't easily identify
1470 * the first file. Always put hunk marks in that case
1471 * and skip the very first one later in work_done().
1473 if (opt
->output
!= std_output
)
1474 opt
->show_hunk_mark
= 1;
1476 opt
->last_shown
= 0;
1478 if (opt
->allow_textconv
) {
1479 grep_source_load_driver(gs
);
1481 * We might set up the shared textconv cache data here, which
1482 * is not thread-safe.
1485 textconv
= userdiff_get_textconv(gs
->driver
);
1490 * We know the result of a textconv is text, so we only have to care
1491 * about binary handling if we are not using it.
1494 switch (opt
->binary
) {
1495 case GREP_BINARY_DEFAULT
:
1496 if (grep_source_is_binary(gs
))
1497 binary_match_only
= 1;
1499 case GREP_BINARY_NOMATCH
:
1500 if (grep_source_is_binary(gs
))
1501 return 0; /* Assume unmatch */
1503 case GREP_BINARY_TEXT
:
1506 die("bug: unknown binary handling mode");
1510 memset(&xecfg
, 0, sizeof(xecfg
));
1513 try_lookahead
= should_lookahead(opt
);
1515 if (fill_textconv_grep(textconv
, gs
) < 0)
1525 * look_ahead() skips quickly to the line that possibly
1526 * has the next hit; don't call it if we need to do
1527 * something more than just skipping the current line
1528 * in response to an unmatch for the current line. E.g.
1529 * inside a post-context window, we will show the current
1530 * line as a context around the previous hit when it
1535 && (show_function
||
1536 lno
<= last_hit
+ opt
->post_context
))
1537 && look_ahead(opt
, &left
, &lno
, &bol
))
1539 eol
= end_of_line(bol
, &left
);
1543 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
1544 ctx
= GREP_CONTEXT_BODY
;
1546 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
1552 /* "grep -v -e foo -e bla" should list lines
1553 * that do not have either, so inversion should
1558 if (opt
->unmatch_name_only
) {
1565 if (opt
->status_only
)
1567 if (opt
->name_only
) {
1568 show_name(opt
, gs
->name
);
1573 if (binary_match_only
) {
1574 opt
->output(opt
, "Binary file ", 12);
1575 output_color(opt
, gs
->name
, strlen(gs
->name
),
1576 opt
->color_filename
);
1577 opt
->output(opt
, " matches\n", 9);
1580 /* Hit at this line. If we haven't shown the
1581 * pre-context lines, we would need to show them.
1583 if (opt
->pre_context
|| opt
->funcbody
)
1584 show_pre_context(opt
, gs
, bol
, eol
, lno
);
1585 else if (opt
->funcname
)
1586 show_funcname_line(opt
, gs
, bol
, lno
);
1587 show_line(opt
, bol
, eol
, gs
->name
, lno
, ':');
1593 if (show_function
&& match_funcname(opt
, gs
, bol
, eol
))
1595 if (show_function
||
1596 (last_hit
&& lno
<= last_hit
+ opt
->post_context
)) {
1597 /* If the last hit is within the post context,
1598 * we need to show this line.
1600 show_line(opt
, bol
, eol
, gs
->name
, lno
, '-');
1614 if (opt
->status_only
)
1616 if (opt
->unmatch_name_only
) {
1617 /* We did not see any hit, so we want to show this */
1618 show_name(opt
, gs
->name
);
1622 xdiff_clear_find_func(&xecfg
);
1626 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1627 * which feels mostly useless but sometimes useful. Maybe
1628 * make it another option? For now suppress them.
1630 if (opt
->count
&& count
) {
1632 if (opt
->pathname
) {
1633 output_color(opt
, gs
->name
, strlen(gs
->name
),
1634 opt
->color_filename
);
1635 output_sep(opt
, ':');
1637 snprintf(buf
, sizeof(buf
), "%u\n", count
);
1638 opt
->output(opt
, buf
, strlen(buf
));
1644 static void clr_hit_marker(struct grep_expr
*x
)
1646 /* All-hit markers are meaningful only at the very top level
1651 if (x
->node
!= GREP_NODE_OR
)
1653 x
->u
.binary
.left
->hit
= 0;
1654 x
= x
->u
.binary
.right
;
1658 static int chk_hit_marker(struct grep_expr
*x
)
1660 /* Top level nodes have hit markers. See if they all are hits */
1662 if (x
->node
!= GREP_NODE_OR
)
1664 if (!x
->u
.binary
.left
->hit
)
1666 x
= x
->u
.binary
.right
;
1670 int grep_source(struct grep_opt
*opt
, struct grep_source
*gs
)
1673 * we do not have to do the two-pass grep when we do not check
1674 * buffer-wide "all-match".
1676 if (!opt
->all_match
)
1677 return grep_source_1(opt
, gs
, 0);
1679 /* Otherwise the toplevel "or" terms hit a bit differently.
1680 * We first clear hit markers from them.
1682 clr_hit_marker(opt
->pattern_expression
);
1683 grep_source_1(opt
, gs
, 1);
1685 if (!chk_hit_marker(opt
->pattern_expression
))
1688 return grep_source_1(opt
, gs
, 0);
1691 int grep_buffer(struct grep_opt
*opt
, char *buf
, unsigned long size
)
1693 struct grep_source gs
;
1696 grep_source_init(&gs
, GREP_SOURCE_BUF
, NULL
, NULL
, NULL
);
1700 r
= grep_source(opt
, &gs
);
1702 grep_source_clear(&gs
);
1706 void grep_source_init(struct grep_source
*gs
, enum grep_source_type type
,
1707 const char *name
, const char *path
,
1708 const void *identifier
)
1711 gs
->name
= xstrdup_or_null(name
);
1712 gs
->path
= xstrdup_or_null(path
);
1718 case GREP_SOURCE_FILE
:
1719 gs
->identifier
= xstrdup(identifier
);
1721 case GREP_SOURCE_SHA1
:
1722 gs
->identifier
= xmalloc(20);
1723 hashcpy(gs
->identifier
, identifier
);
1725 case GREP_SOURCE_BUF
:
1726 gs
->identifier
= NULL
;
1730 void grep_source_clear(struct grep_source
*gs
)
1736 free(gs
->identifier
);
1737 gs
->identifier
= NULL
;
1738 grep_source_clear_data(gs
);
1741 void grep_source_clear_data(struct grep_source
*gs
)
1744 case GREP_SOURCE_FILE
:
1745 case GREP_SOURCE_SHA1
:
1750 case GREP_SOURCE_BUF
:
1751 /* leave user-provided buf intact */
1756 static int grep_source_load_sha1(struct grep_source
*gs
)
1758 enum object_type type
;
1761 gs
->buf
= read_sha1_file(gs
->identifier
, &type
, &gs
->size
);
1765 return error(_("'%s': unable to read %s"),
1767 sha1_to_hex(gs
->identifier
));
1771 static int grep_source_load_file(struct grep_source
*gs
)
1773 const char *filename
= gs
->identifier
;
1779 if (lstat(filename
, &st
) < 0) {
1781 if (errno
!= ENOENT
)
1782 error(_("'%s': %s"), filename
, strerror(errno
));
1785 if (!S_ISREG(st
.st_mode
))
1787 size
= xsize_t(st
.st_size
);
1788 i
= open(filename
, O_RDONLY
);
1791 data
= xmalloc(size
+ 1);
1792 if (st
.st_size
!= read_in_full(i
, data
, size
)) {
1793 error(_("'%s': short read %s"), filename
, strerror(errno
));
1806 static int grep_source_load(struct grep_source
*gs
)
1812 case GREP_SOURCE_FILE
:
1813 return grep_source_load_file(gs
);
1814 case GREP_SOURCE_SHA1
:
1815 return grep_source_load_sha1(gs
);
1816 case GREP_SOURCE_BUF
:
1817 return gs
->buf
? 0 : -1;
1819 die("BUG: invalid grep_source type");
1822 void grep_source_load_driver(struct grep_source
*gs
)
1829 gs
->driver
= userdiff_find_by_path(gs
->path
);
1831 gs
->driver
= userdiff_find_by_name("default");
1835 static int grep_source_is_binary(struct grep_source
*gs
)
1837 grep_source_load_driver(gs
);
1838 if (gs
->driver
->binary
!= -1)
1839 return gs
->driver
->binary
;
1841 if (!grep_source_load(gs
))
1842 return buffer_is_binary(gs
->buf
, gs
->size
);