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 static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type
, struct grep_opt
*opt
)
168 switch (pattern_type
) {
169 case GREP_PATTERN_TYPE_UNSPECIFIED
:
172 case GREP_PATTERN_TYPE_BRE
:
175 opt
->regflags
&= ~REG_EXTENDED
;
178 case GREP_PATTERN_TYPE_ERE
:
181 opt
->regflags
|= REG_EXTENDED
;
184 case GREP_PATTERN_TYPE_FIXED
:
187 opt
->regflags
&= ~REG_EXTENDED
;
190 case GREP_PATTERN_TYPE_PCRE
:
193 opt
->regflags
&= ~REG_EXTENDED
;
198 void grep_commit_pattern_type(enum grep_pattern_type pattern_type
, struct grep_opt
*opt
)
200 if (pattern_type
!= GREP_PATTERN_TYPE_UNSPECIFIED
)
201 grep_set_pattern_type_option(pattern_type
, opt
);
202 else if (opt
->pattern_type_option
!= GREP_PATTERN_TYPE_UNSPECIFIED
)
203 grep_set_pattern_type_option(opt
->pattern_type_option
, opt
);
204 else if (opt
->extended_regexp_option
)
205 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE
, opt
);
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 if (has_non_ascii(p
->pattern
))
329 p
->pcre_tables
= pcre_maketables();
330 options
|= PCRE_CASELESS
;
332 if (is_utf8_locale() && has_non_ascii(p
->pattern
))
333 options
|= PCRE_UTF8
;
335 p
->pcre_regexp
= pcre_compile(p
->pattern
, options
, &error
, &erroffset
,
338 compile_regexp_failed(p
, error
);
340 p
->pcre_extra_info
= pcre_study(p
->pcre_regexp
, 0, &error
);
341 if (!p
->pcre_extra_info
&& error
)
345 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
346 regmatch_t
*match
, int eflags
)
348 int ovector
[30], ret
, flags
= 0;
350 if (eflags
& REG_NOTBOL
)
351 flags
|= PCRE_NOTBOL
;
353 ret
= pcre_exec(p
->pcre_regexp
, p
->pcre_extra_info
, line
, eol
- line
,
354 0, flags
, ovector
, ARRAY_SIZE(ovector
));
355 if (ret
< 0 && ret
!= PCRE_ERROR_NOMATCH
)
356 die("pcre_exec failed with error code %d", ret
);
359 match
->rm_so
= ovector
[0];
360 match
->rm_eo
= ovector
[1];
366 static void free_pcre_regexp(struct grep_pat
*p
)
368 pcre_free(p
->pcre_regexp
);
369 pcre_free(p
->pcre_extra_info
);
370 pcre_free((void *)p
->pcre_tables
);
372 #else /* !USE_LIBPCRE */
373 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
375 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
378 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
379 regmatch_t
*match
, int eflags
)
384 static void free_pcre_regexp(struct grep_pat
*p
)
387 #endif /* !USE_LIBPCRE */
389 static int is_fixed(const char *s
, size_t len
)
393 /* regcomp cannot accept patterns with NULs so we
394 * consider any pattern containing a NUL fixed.
396 if (memchr(s
, 0, len
))
399 for (i
= 0; i
< len
; i
++) {
400 if (is_regex_special(s
[i
]))
407 static void compile_fixed_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
409 struct strbuf sb
= STRBUF_INIT
;
413 basic_regex_quote_buf(&sb
, p
->pattern
);
414 regflags
= opt
->regflags
& ~REG_EXTENDED
;
415 if (opt
->ignore_case
)
416 regflags
|= REG_ICASE
;
417 err
= regcomp(&p
->regexp
, sb
.buf
, regflags
);
419 fprintf(stderr
, "fixed %s\n", sb
.buf
);
423 regerror(err
, &p
->regexp
, errbuf
, sizeof(errbuf
));
425 compile_regexp_failed(p
, errbuf
);
429 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
431 int icase
, ascii_only
;
434 p
->word_regexp
= opt
->word_regexp
;
435 p
->ignore_case
= opt
->ignore_case
;
436 icase
= opt
->regflags
& REG_ICASE
|| p
->ignore_case
;
437 ascii_only
= !has_non_ascii(p
->pattern
);
440 * Even when -F (fixed) asks us to do a non-regexp search, we
441 * may not be able to correctly case-fold when -i
442 * (ignore-case) is asked (in which case, we'll synthesize a
443 * regexp to match the pattern that matches regexp special
444 * characters literally, while ignoring case differences). On
445 * the other hand, even without -F, if the pattern does not
446 * have any regexp special characters and there is no need for
447 * case-folding search, we can internally turn it into a
448 * simple string match using kws. p->fixed tells us if we
451 if (opt
->fixed
|| is_fixed(p
->pattern
, p
->patternlen
))
452 p
->fixed
= !icase
|| ascii_only
;
457 p
->kws
= kwsalloc(icase
? tolower_trans_tbl
: NULL
);
458 kwsincr(p
->kws
, p
->pattern
, p
->patternlen
);
461 } else if (opt
->fixed
) {
463 * We come here when the pattern has the non-ascii
464 * characters we cannot case-fold, and asked to
467 compile_fixed_regexp(p
, opt
);
472 compile_pcre_regexp(p
, opt
);
476 err
= regcomp(&p
->regexp
, p
->pattern
, opt
->regflags
);
479 regerror(err
, &p
->regexp
, errbuf
, 1024);
481 compile_regexp_failed(p
, errbuf
);
485 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
486 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
495 case GREP_PATTERN
: /* atom */
496 case GREP_PATTERN_HEAD
:
497 case GREP_PATTERN_BODY
:
498 x
= xcalloc(1, sizeof (struct grep_expr
));
499 x
->node
= GREP_NODE_ATOM
;
503 case GREP_OPEN_PAREN
:
505 x
= compile_pattern_or(list
);
506 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
507 die("unmatched parenthesis");
508 *list
= (*list
)->next
;
515 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
526 die("--not not followed by pattern expression");
528 x
= xcalloc(1, sizeof (struct grep_expr
));
529 x
->node
= GREP_NODE_NOT
;
530 x
->u
.unary
= compile_pattern_not(list
);
532 die("--not followed by non pattern expression");
535 return compile_pattern_atom(list
);
539 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
542 struct grep_expr
*x
, *y
, *z
;
544 x
= compile_pattern_not(list
);
546 if (p
&& p
->token
== GREP_AND
) {
548 die("--and not followed by pattern expression");
550 y
= compile_pattern_and(list
);
552 die("--and not followed by pattern expression");
553 z
= xcalloc(1, sizeof (struct grep_expr
));
554 z
->node
= GREP_NODE_AND
;
555 z
->u
.binary
.left
= x
;
556 z
->u
.binary
.right
= y
;
562 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
565 struct grep_expr
*x
, *y
, *z
;
567 x
= compile_pattern_and(list
);
569 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
570 y
= compile_pattern_or(list
);
572 die("not a pattern expression %s", p
->pattern
);
573 z
= xcalloc(1, sizeof (struct grep_expr
));
574 z
->node
= GREP_NODE_OR
;
575 z
->u
.binary
.left
= x
;
576 z
->u
.binary
.right
= y
;
582 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
584 return compile_pattern_or(list
);
587 static void indent(int in
)
593 static void dump_grep_pat(struct grep_pat
*p
)
596 case GREP_AND
: fprintf(stderr
, "*and*"); break;
597 case GREP_OPEN_PAREN
: fprintf(stderr
, "*(*"); break;
598 case GREP_CLOSE_PAREN
: fprintf(stderr
, "*)*"); break;
599 case GREP_NOT
: fprintf(stderr
, "*not*"); break;
600 case GREP_OR
: fprintf(stderr
, "*or*"); break;
602 case GREP_PATTERN
: fprintf(stderr
, "pattern"); break;
603 case GREP_PATTERN_HEAD
: fprintf(stderr
, "pattern_head"); break;
604 case GREP_PATTERN_BODY
: fprintf(stderr
, "pattern_body"); break;
609 case GREP_PATTERN_HEAD
:
610 fprintf(stderr
, "<head %d>", p
->field
); break;
611 case GREP_PATTERN_BODY
:
612 fprintf(stderr
, "<body>"); break;
616 case GREP_PATTERN_HEAD
:
617 case GREP_PATTERN_BODY
:
619 fprintf(stderr
, "%.*s", (int)p
->patternlen
, p
->pattern
);
625 static void dump_grep_expression_1(struct grep_expr
*x
, int in
)
630 fprintf(stderr
, "true\n");
633 dump_grep_pat(x
->u
.atom
);
636 fprintf(stderr
, "(not\n");
637 dump_grep_expression_1(x
->u
.unary
, in
+1);
639 fprintf(stderr
, ")\n");
642 fprintf(stderr
, "(and\n");
643 dump_grep_expression_1(x
->u
.binary
.left
, in
+1);
644 dump_grep_expression_1(x
->u
.binary
.right
, in
+1);
646 fprintf(stderr
, ")\n");
649 fprintf(stderr
, "(or\n");
650 dump_grep_expression_1(x
->u
.binary
.left
, in
+1);
651 dump_grep_expression_1(x
->u
.binary
.right
, in
+1);
653 fprintf(stderr
, ")\n");
658 static void dump_grep_expression(struct grep_opt
*opt
)
660 struct grep_expr
*x
= opt
->pattern_expression
;
663 fprintf(stderr
, "[all-match]\n");
664 dump_grep_expression_1(x
, 0);
668 static struct grep_expr
*grep_true_expr(void)
670 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
671 z
->node
= GREP_NODE_TRUE
;
675 static struct grep_expr
*grep_or_expr(struct grep_expr
*left
, struct grep_expr
*right
)
677 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
678 z
->node
= GREP_NODE_OR
;
679 z
->u
.binary
.left
= left
;
680 z
->u
.binary
.right
= right
;
684 static struct grep_expr
*prep_header_patterns(struct grep_opt
*opt
)
687 struct grep_expr
*header_expr
;
688 struct grep_expr
*(header_group
[GREP_HEADER_FIELD_MAX
]);
689 enum grep_header_field fld
;
691 if (!opt
->header_list
)
694 for (p
= opt
->header_list
; p
; p
= p
->next
) {
695 if (p
->token
!= GREP_PATTERN_HEAD
)
696 die("BUG: a non-header pattern in grep header list.");
697 if (p
->field
< GREP_HEADER_FIELD_MIN
||
698 GREP_HEADER_FIELD_MAX
<= p
->field
)
699 die("BUG: unknown header field %d", p
->field
);
700 compile_regexp(p
, opt
);
703 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++)
704 header_group
[fld
] = NULL
;
706 for (p
= opt
->header_list
; p
; p
= p
->next
) {
708 struct grep_pat
*pp
= p
;
710 h
= compile_pattern_atom(&pp
);
711 if (!h
|| pp
!= p
->next
)
712 die("BUG: malformed header expr");
713 if (!header_group
[p
->field
]) {
714 header_group
[p
->field
] = h
;
717 header_group
[p
->field
] = grep_or_expr(h
, header_group
[p
->field
]);
722 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++) {
723 if (!header_group
[fld
])
726 header_expr
= grep_true_expr();
727 header_expr
= grep_or_expr(header_group
[fld
], header_expr
);
732 static struct grep_expr
*grep_splice_or(struct grep_expr
*x
, struct grep_expr
*y
)
734 struct grep_expr
*z
= x
;
737 assert(x
->node
== GREP_NODE_OR
);
738 if (x
->u
.binary
.right
&&
739 x
->u
.binary
.right
->node
== GREP_NODE_TRUE
) {
740 x
->u
.binary
.right
= y
;
743 x
= x
->u
.binary
.right
;
748 static void compile_grep_patterns_real(struct grep_opt
*opt
)
751 struct grep_expr
*header_expr
= prep_header_patterns(opt
);
753 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
755 case GREP_PATTERN
: /* atom */
756 case GREP_PATTERN_HEAD
:
757 case GREP_PATTERN_BODY
:
758 compile_regexp(p
, opt
);
766 if (opt
->all_match
|| header_expr
)
768 else if (!opt
->extended
&& !opt
->debug
)
771 p
= opt
->pattern_list
;
773 opt
->pattern_expression
= compile_pattern_expr(&p
);
775 die("incomplete pattern expression: %s", p
->pattern
);
780 if (!opt
->pattern_expression
)
781 opt
->pattern_expression
= header_expr
;
782 else if (opt
->all_match
)
783 opt
->pattern_expression
= grep_splice_or(header_expr
,
784 opt
->pattern_expression
);
786 opt
->pattern_expression
= grep_or_expr(opt
->pattern_expression
,
791 void compile_grep_patterns(struct grep_opt
*opt
)
793 compile_grep_patterns_real(opt
);
795 dump_grep_expression(opt
);
798 static void free_pattern_expr(struct grep_expr
*x
)
805 free_pattern_expr(x
->u
.unary
);
809 free_pattern_expr(x
->u
.binary
.left
);
810 free_pattern_expr(x
->u
.binary
.right
);
816 void free_grep_patterns(struct grep_opt
*opt
)
818 struct grep_pat
*p
, *n
;
820 for (p
= opt
->pattern_list
; p
; p
= n
) {
823 case GREP_PATTERN
: /* atom */
824 case GREP_PATTERN_HEAD
:
825 case GREP_PATTERN_BODY
:
828 else if (p
->pcre_regexp
)
842 free_pattern_expr(opt
->pattern_expression
);
845 static char *end_of_line(char *cp
, unsigned long *left
)
847 unsigned long l
= *left
;
848 while (l
&& *cp
!= '\n') {
856 static int word_char(char ch
)
858 return isalnum(ch
) || ch
== '_';
861 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
864 if (want_color(opt
->color
) && color
&& color
[0]) {
865 opt
->output(opt
, color
, strlen(color
));
866 opt
->output(opt
, data
, size
);
867 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
869 opt
->output(opt
, data
, size
);
872 static void output_sep(struct grep_opt
*opt
, char sign
)
874 if (opt
->null_following_name
)
875 opt
->output(opt
, "\0", 1);
877 output_color(opt
, &sign
, 1, opt
->color_sep
);
880 static void show_name(struct grep_opt
*opt
, const char *name
)
882 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
883 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
886 static int fixmatch(struct grep_pat
*p
, char *line
, char *eol
,
889 struct kwsmatch kwsm
;
890 size_t offset
= kwsexec(p
->kws
, line
, eol
- line
, &kwsm
);
892 match
->rm_so
= match
->rm_eo
= -1;
895 match
->rm_so
= offset
;
896 match
->rm_eo
= match
->rm_so
+ kwsm
.size
[0];
901 static int patmatch(struct grep_pat
*p
, char *line
, char *eol
,
902 regmatch_t
*match
, int eflags
)
907 hit
= !fixmatch(p
, line
, eol
, match
);
908 else if (p
->pcre_regexp
)
909 hit
= !pcrematch(p
, line
, eol
, match
, eflags
);
911 hit
= !regexec_buf(&p
->regexp
, line
, eol
- line
, 1, match
,
917 static int strip_timestamp(char *bol
, char **eol_p
)
922 while (bol
< --eol
) {
938 { "committer ", 10 },
942 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
943 enum grep_context ctx
,
944 regmatch_t
*pmatch
, int eflags
)
948 const char *start
= bol
;
950 if ((p
->token
!= GREP_PATTERN
) &&
951 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
954 if (p
->token
== GREP_PATTERN_HEAD
) {
957 assert(p
->field
< ARRAY_SIZE(header_field
));
958 field
= header_field
[p
->field
].field
;
959 len
= header_field
[p
->field
].len
;
960 if (strncmp(bol
, field
, len
))
964 case GREP_HEADER_AUTHOR
:
965 case GREP_HEADER_COMMITTER
:
966 saved_ch
= strip_timestamp(bol
, &eol
);
974 hit
= patmatch(p
, bol
, eol
, pmatch
, eflags
);
976 if (hit
&& p
->word_regexp
) {
977 if ((pmatch
[0].rm_so
< 0) ||
978 (eol
- bol
) < pmatch
[0].rm_so
||
979 (pmatch
[0].rm_eo
< 0) ||
980 (eol
- bol
) < pmatch
[0].rm_eo
)
981 die("regexp returned nonsense");
983 /* Match beginning must be either beginning of the
984 * line, or at word boundary (i.e. the last char must
985 * not be a word char). Similarly, match end must be
986 * either end of the line, or at word boundary
987 * (i.e. the next char must not be a word char).
989 if ( ((pmatch
[0].rm_so
== 0) ||
990 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
991 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
992 !word_char(bol
[pmatch
[0].rm_eo
])) )
997 /* Words consist of at least one character. */
998 if (pmatch
->rm_so
== pmatch
->rm_eo
)
1001 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
1002 /* There could be more than one match on the
1003 * line, and the first match might not be
1004 * strict word match. But later ones could be!
1005 * Forward to the next possible start, i.e. the
1006 * next position following a non-word char.
1008 bol
= pmatch
[0].rm_so
+ bol
+ 1;
1009 while (word_char(bol
[-1]) && bol
< eol
)
1011 eflags
|= REG_NOTBOL
;
1016 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
1019 pmatch
[0].rm_so
+= bol
- start
;
1020 pmatch
[0].rm_eo
+= bol
- start
;
1025 static int match_expr_eval(struct grep_expr
*x
, char *bol
, char *eol
,
1026 enum grep_context ctx
, int collect_hits
)
1032 die("Not a valid grep expression");
1034 case GREP_NODE_TRUE
:
1037 case GREP_NODE_ATOM
:
1038 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
, &match
, 0);
1041 h
= !match_expr_eval(x
->u
.unary
, bol
, eol
, ctx
, 0);
1044 if (!match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0))
1046 h
= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
1050 return (match_expr_eval(x
->u
.binary
.left
,
1051 bol
, eol
, ctx
, 0) ||
1052 match_expr_eval(x
->u
.binary
.right
,
1054 h
= match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
1055 x
->u
.binary
.left
->hit
|= h
;
1056 h
|= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
1059 die("Unexpected node type (internal error) %d", x
->node
);
1066 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
1067 enum grep_context ctx
, int collect_hits
)
1069 struct grep_expr
*x
= opt
->pattern_expression
;
1070 return match_expr_eval(x
, bol
, eol
, ctx
, collect_hits
);
1073 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
1074 enum grep_context ctx
, int collect_hits
)
1080 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
1082 /* we do not call with collect_hits without being extended */
1083 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1084 if (match_one_pattern(p
, bol
, eol
, ctx
, &match
, 0))
1090 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
1091 enum grep_context ctx
,
1092 regmatch_t
*pmatch
, int eflags
)
1096 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
1098 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
1100 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
1101 if (match
.rm_so
> pmatch
->rm_so
)
1103 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
1106 pmatch
->rm_so
= match
.rm_so
;
1107 pmatch
->rm_eo
= match
.rm_eo
;
1111 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
1112 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
1117 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
1119 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1121 case GREP_PATTERN
: /* atom */
1122 case GREP_PATTERN_HEAD
:
1123 case GREP_PATTERN_BODY
:
1124 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
1135 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
1136 const char *name
, unsigned lno
, char sign
)
1138 int rest
= eol
- bol
;
1139 const char *match_color
, *line_color
= NULL
;
1141 if (opt
->file_break
&& opt
->last_shown
== 0) {
1142 if (opt
->show_hunk_mark
)
1143 opt
->output(opt
, "\n", 1);
1144 } else if (opt
->pre_context
|| opt
->post_context
|| opt
->funcbody
) {
1145 if (opt
->last_shown
== 0) {
1146 if (opt
->show_hunk_mark
) {
1147 output_color(opt
, "--", 2, opt
->color_sep
);
1148 opt
->output(opt
, "\n", 1);
1150 } else if (lno
> opt
->last_shown
+ 1) {
1151 output_color(opt
, "--", 2, opt
->color_sep
);
1152 opt
->output(opt
, "\n", 1);
1155 if (opt
->heading
&& opt
->last_shown
== 0) {
1156 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
1157 opt
->output(opt
, "\n", 1);
1159 opt
->last_shown
= lno
;
1161 if (!opt
->heading
&& opt
->pathname
) {
1162 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
1163 output_sep(opt
, sign
);
1167 snprintf(buf
, sizeof(buf
), "%d", lno
);
1168 output_color(opt
, buf
, strlen(buf
), opt
->color_lineno
);
1169 output_sep(opt
, sign
);
1173 enum grep_context ctx
= GREP_CONTEXT_BODY
;
1178 match_color
= opt
->color_match_selected
;
1180 match_color
= opt
->color_match_context
;
1182 line_color
= opt
->color_selected
;
1183 else if (sign
== '-')
1184 line_color
= opt
->color_context
;
1185 else if (sign
== '=')
1186 line_color
= opt
->color_function
;
1188 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
1189 if (match
.rm_so
== match
.rm_eo
)
1192 output_color(opt
, bol
, match
.rm_so
, line_color
);
1193 output_color(opt
, bol
+ match
.rm_so
,
1194 match
.rm_eo
- match
.rm_so
, match_color
);
1196 rest
-= match
.rm_eo
;
1197 eflags
= REG_NOTBOL
;
1201 output_color(opt
, bol
, rest
, line_color
);
1202 opt
->output(opt
, "\n", 1);
1209 * This lock protects access to the gitattributes machinery, which is
1212 pthread_mutex_t grep_attr_mutex
;
1214 static inline void grep_attr_lock(void)
1217 pthread_mutex_lock(&grep_attr_mutex
);
1220 static inline void grep_attr_unlock(void)
1223 pthread_mutex_unlock(&grep_attr_mutex
);
1227 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
1229 pthread_mutex_t grep_read_mutex
;
1232 #define grep_attr_lock()
1233 #define grep_attr_unlock()
1236 static int match_funcname(struct grep_opt
*opt
, struct grep_source
*gs
, char *bol
, char *eol
)
1238 xdemitconf_t
*xecfg
= opt
->priv
;
1239 if (xecfg
&& !xecfg
->find_func
) {
1240 grep_source_load_driver(gs
);
1241 if (gs
->driver
->funcname
.pattern
) {
1242 const struct userdiff_funcname
*pe
= &gs
->driver
->funcname
;
1243 xdiff_set_find_func(xecfg
, pe
->pattern
, pe
->cflags
);
1245 xecfg
= opt
->priv
= NULL
;
1251 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
1252 xecfg
->find_func_priv
) >= 0;
1257 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
1262 static void show_funcname_line(struct grep_opt
*opt
, struct grep_source
*gs
,
1263 char *bol
, unsigned lno
)
1265 while (bol
> gs
->buf
) {
1268 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1272 if (lno
<= opt
->last_shown
)
1275 if (match_funcname(opt
, gs
, bol
, eol
)) {
1276 show_line(opt
, bol
, eol
, gs
->name
, lno
, '=');
1282 static void show_pre_context(struct grep_opt
*opt
, struct grep_source
*gs
,
1283 char *bol
, char *end
, unsigned lno
)
1285 unsigned cur
= lno
, from
= 1, funcname_lno
= 0;
1286 int funcname_needed
= !!opt
->funcname
;
1288 if (opt
->funcbody
&& !match_funcname(opt
, gs
, bol
, end
))
1289 funcname_needed
= 2;
1291 if (opt
->pre_context
< lno
)
1292 from
= lno
- opt
->pre_context
;
1293 if (from
<= opt
->last_shown
)
1294 from
= opt
->last_shown
+ 1;
1297 while (bol
> gs
->buf
&&
1298 cur
> (funcname_needed
== 2 ? opt
->last_shown
+ 1 : from
)) {
1301 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1304 if (funcname_needed
&& match_funcname(opt
, gs
, bol
, eol
)) {
1306 funcname_needed
= 0;
1310 /* We need to look even further back to find a function signature. */
1311 if (opt
->funcname
&& funcname_needed
)
1312 show_funcname_line(opt
, gs
, bol
, cur
);
1316 char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
1318 while (*eol
!= '\n')
1320 show_line(opt
, bol
, eol
, gs
->name
, cur
, sign
);
1326 static int should_lookahead(struct grep_opt
*opt
)
1331 return 0; /* punt for too complex stuff */
1334 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1335 if (p
->token
!= GREP_PATTERN
)
1336 return 0; /* punt for "header only" and stuff */
1341 static int look_ahead(struct grep_opt
*opt
,
1342 unsigned long *left_p
,
1346 unsigned lno
= *lno_p
;
1349 char *sp
, *last_bol
;
1350 regoff_t earliest
= -1;
1352 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1356 hit
= patmatch(p
, bol
, bol
+ *left_p
, &m
, 0);
1357 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
1359 if (earliest
< 0 || m
.rm_so
< earliest
)
1364 *bol_p
= bol
+ *left_p
;
1368 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
1369 ; /* find the beginning of the line */
1372 for (sp
= bol
; sp
< last_bol
; sp
++) {
1376 *left_p
-= last_bol
- bol
;
1382 static void std_output(struct grep_opt
*opt
, const void *buf
, size_t size
)
1384 fwrite(buf
, size
, 1, stdout
);
1387 static int fill_textconv_grep(struct userdiff_driver
*driver
,
1388 struct grep_source
*gs
)
1390 struct diff_filespec
*df
;
1394 if (!driver
|| !driver
->textconv
)
1395 return grep_source_load(gs
);
1398 * The textconv interface is intimately tied to diff_filespecs, so we
1399 * have to pretend to be one. If we could unify the grep_source
1400 * and diff_filespec structs, this mess could just go away.
1402 df
= alloc_filespec(gs
->path
);
1404 case GREP_SOURCE_SHA1
:
1405 fill_filespec(df
, gs
->identifier
, 1, 0100644);
1407 case GREP_SOURCE_FILE
:
1408 fill_filespec(df
, null_sha1
, 0, 0100644);
1411 die("BUG: attempt to textconv something without a path?");
1415 * fill_textconv is not remotely thread-safe; it may load objects
1416 * behind the scenes, and it modifies the global diff tempfile
1420 size
= fill_textconv(driver
, df
, &buf
);
1425 * The normal fill_textconv usage by the diff machinery would just keep
1426 * the textconv'd buf separate from the diff_filespec. But much of the
1427 * grep code passes around a grep_source and assumes that its "buf"
1428 * pointer is the beginning of the thing we are searching. So let's
1429 * install our textconv'd version into the grep_source, taking care not
1430 * to leak any existing buffer.
1432 grep_source_clear_data(gs
);
1439 static int is_empty_line(const char *bol
, const char *eol
)
1441 while (bol
< eol
&& isspace(*bol
))
1446 static int grep_source_1(struct grep_opt
*opt
, struct grep_source
*gs
, int collect_hits
)
1449 char *peek_bol
= NULL
;
1452 unsigned last_hit
= 0;
1453 int binary_match_only
= 0;
1455 int try_lookahead
= 0;
1456 int show_function
= 0;
1457 struct userdiff_driver
*textconv
= NULL
;
1458 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
1462 opt
->output
= std_output
;
1464 if (opt
->pre_context
|| opt
->post_context
|| opt
->file_break
||
1466 /* Show hunk marks, except for the first file. */
1467 if (opt
->last_shown
)
1468 opt
->show_hunk_mark
= 1;
1470 * If we're using threads then we can't easily identify
1471 * the first file. Always put hunk marks in that case
1472 * and skip the very first one later in work_done().
1474 if (opt
->output
!= std_output
)
1475 opt
->show_hunk_mark
= 1;
1477 opt
->last_shown
= 0;
1479 if (opt
->allow_textconv
) {
1480 grep_source_load_driver(gs
);
1482 * We might set up the shared textconv cache data here, which
1483 * is not thread-safe.
1486 textconv
= userdiff_get_textconv(gs
->driver
);
1491 * We know the result of a textconv is text, so we only have to care
1492 * about binary handling if we are not using it.
1495 switch (opt
->binary
) {
1496 case GREP_BINARY_DEFAULT
:
1497 if (grep_source_is_binary(gs
))
1498 binary_match_only
= 1;
1500 case GREP_BINARY_NOMATCH
:
1501 if (grep_source_is_binary(gs
))
1502 return 0; /* Assume unmatch */
1504 case GREP_BINARY_TEXT
:
1507 die("BUG: unknown binary handling mode");
1511 memset(&xecfg
, 0, sizeof(xecfg
));
1514 try_lookahead
= should_lookahead(opt
);
1516 if (fill_textconv_grep(textconv
, gs
) < 0)
1526 * look_ahead() skips quickly to the line that possibly
1527 * has the next hit; don't call it if we need to do
1528 * something more than just skipping the current line
1529 * in response to an unmatch for the current line. E.g.
1530 * inside a post-context window, we will show the current
1531 * line as a context around the previous hit when it
1536 && (show_function
||
1537 lno
<= last_hit
+ opt
->post_context
))
1538 && look_ahead(opt
, &left
, &lno
, &bol
))
1540 eol
= end_of_line(bol
, &left
);
1544 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
1545 ctx
= GREP_CONTEXT_BODY
;
1547 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
1553 /* "grep -v -e foo -e bla" should list lines
1554 * that do not have either, so inversion should
1559 if (opt
->unmatch_name_only
) {
1566 if (opt
->status_only
)
1568 if (opt
->name_only
) {
1569 show_name(opt
, gs
->name
);
1574 if (binary_match_only
) {
1575 opt
->output(opt
, "Binary file ", 12);
1576 output_color(opt
, gs
->name
, strlen(gs
->name
),
1577 opt
->color_filename
);
1578 opt
->output(opt
, " matches\n", 9);
1581 /* Hit at this line. If we haven't shown the
1582 * pre-context lines, we would need to show them.
1584 if (opt
->pre_context
|| opt
->funcbody
)
1585 show_pre_context(opt
, gs
, bol
, eol
, lno
);
1586 else if (opt
->funcname
)
1587 show_funcname_line(opt
, gs
, bol
, lno
);
1588 show_line(opt
, bol
, eol
, gs
->name
, lno
, ':');
1594 if (show_function
&& (!peek_bol
|| peek_bol
< bol
)) {
1595 unsigned long peek_left
= left
;
1596 char *peek_eol
= eol
;
1599 * Trailing empty lines are not interesting.
1600 * Peek past them to see if they belong to the
1601 * body of the current function.
1604 while (is_empty_line(peek_bol
, peek_eol
)) {
1605 peek_bol
= peek_eol
+ 1;
1606 peek_eol
= end_of_line(peek_bol
, &peek_left
);
1609 if (match_funcname(opt
, gs
, peek_bol
, peek_eol
))
1612 if (show_function
||
1613 (last_hit
&& lno
<= last_hit
+ opt
->post_context
)) {
1614 /* If the last hit is within the post context,
1615 * we need to show this line.
1617 show_line(opt
, bol
, eol
, gs
->name
, lno
, '-');
1631 if (opt
->status_only
)
1633 if (opt
->unmatch_name_only
) {
1634 /* We did not see any hit, so we want to show this */
1635 show_name(opt
, gs
->name
);
1639 xdiff_clear_find_func(&xecfg
);
1643 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1644 * which feels mostly useless but sometimes useful. Maybe
1645 * make it another option? For now suppress them.
1647 if (opt
->count
&& count
) {
1649 if (opt
->pathname
) {
1650 output_color(opt
, gs
->name
, strlen(gs
->name
),
1651 opt
->color_filename
);
1652 output_sep(opt
, ':');
1654 snprintf(buf
, sizeof(buf
), "%u\n", count
);
1655 opt
->output(opt
, buf
, strlen(buf
));
1661 static void clr_hit_marker(struct grep_expr
*x
)
1663 /* All-hit markers are meaningful only at the very top level
1668 if (x
->node
!= GREP_NODE_OR
)
1670 x
->u
.binary
.left
->hit
= 0;
1671 x
= x
->u
.binary
.right
;
1675 static int chk_hit_marker(struct grep_expr
*x
)
1677 /* Top level nodes have hit markers. See if they all are hits */
1679 if (x
->node
!= GREP_NODE_OR
)
1681 if (!x
->u
.binary
.left
->hit
)
1683 x
= x
->u
.binary
.right
;
1687 int grep_source(struct grep_opt
*opt
, struct grep_source
*gs
)
1690 * we do not have to do the two-pass grep when we do not check
1691 * buffer-wide "all-match".
1693 if (!opt
->all_match
)
1694 return grep_source_1(opt
, gs
, 0);
1696 /* Otherwise the toplevel "or" terms hit a bit differently.
1697 * We first clear hit markers from them.
1699 clr_hit_marker(opt
->pattern_expression
);
1700 grep_source_1(opt
, gs
, 1);
1702 if (!chk_hit_marker(opt
->pattern_expression
))
1705 return grep_source_1(opt
, gs
, 0);
1708 int grep_buffer(struct grep_opt
*opt
, char *buf
, unsigned long size
)
1710 struct grep_source gs
;
1713 grep_source_init(&gs
, GREP_SOURCE_BUF
, NULL
, NULL
, NULL
);
1717 r
= grep_source(opt
, &gs
);
1719 grep_source_clear(&gs
);
1723 void grep_source_init(struct grep_source
*gs
, enum grep_source_type type
,
1724 const char *name
, const char *path
,
1725 const void *identifier
)
1728 gs
->name
= xstrdup_or_null(name
);
1729 gs
->path
= xstrdup_or_null(path
);
1735 case GREP_SOURCE_FILE
:
1736 gs
->identifier
= xstrdup(identifier
);
1738 case GREP_SOURCE_SUBMODULE
:
1740 gs
->identifier
= NULL
;
1745 * If the identifier is non-NULL (in the submodule case) it
1746 * will be a SHA1 that needs to be copied.
1748 case GREP_SOURCE_SHA1
:
1749 gs
->identifier
= xmalloc(20);
1750 hashcpy(gs
->identifier
, identifier
);
1752 case GREP_SOURCE_BUF
:
1753 gs
->identifier
= NULL
;
1758 void grep_source_clear(struct grep_source
*gs
)
1764 free(gs
->identifier
);
1765 gs
->identifier
= NULL
;
1766 grep_source_clear_data(gs
);
1769 void grep_source_clear_data(struct grep_source
*gs
)
1772 case GREP_SOURCE_FILE
:
1773 case GREP_SOURCE_SHA1
:
1774 case GREP_SOURCE_SUBMODULE
:
1779 case GREP_SOURCE_BUF
:
1780 /* leave user-provided buf intact */
1785 static int grep_source_load_sha1(struct grep_source
*gs
)
1787 enum object_type type
;
1790 gs
->buf
= read_sha1_file(gs
->identifier
, &type
, &gs
->size
);
1794 return error(_("'%s': unable to read %s"),
1796 sha1_to_hex(gs
->identifier
));
1800 static int grep_source_load_file(struct grep_source
*gs
)
1802 const char *filename
= gs
->identifier
;
1808 if (lstat(filename
, &st
) < 0) {
1810 if (errno
!= ENOENT
)
1811 error_errno(_("failed to stat '%s'"), filename
);
1814 if (!S_ISREG(st
.st_mode
))
1816 size
= xsize_t(st
.st_size
);
1817 i
= open(filename
, O_RDONLY
);
1820 data
= xmallocz(size
);
1821 if (st
.st_size
!= read_in_full(i
, data
, size
)) {
1822 error_errno(_("'%s': short read"), filename
);
1834 static int grep_source_load(struct grep_source
*gs
)
1840 case GREP_SOURCE_FILE
:
1841 return grep_source_load_file(gs
);
1842 case GREP_SOURCE_SHA1
:
1843 return grep_source_load_sha1(gs
);
1844 case GREP_SOURCE_BUF
:
1845 return gs
->buf
? 0 : -1;
1846 case GREP_SOURCE_SUBMODULE
:
1849 die("BUG: invalid grep_source type to load");
1852 void grep_source_load_driver(struct grep_source
*gs
)
1859 gs
->driver
= userdiff_find_by_path(gs
->path
);
1861 gs
->driver
= userdiff_find_by_name("default");
1865 static int grep_source_is_binary(struct grep_source
*gs
)
1867 grep_source_load_driver(gs
);
1868 if (gs
->driver
->binary
!= -1)
1869 return gs
->driver
->binary
;
1871 if (!grep_source_load(gs
))
1872 return buffer_is_binary(gs
->buf
, gs
->size
);