5 #include "xdiff-interface.h"
11 static int grep_source_load(struct grep_source
*gs
);
12 static int grep_source_is_binary(struct grep_source
*gs
);
14 static struct grep_opt grep_defaults
;
16 static void std_output(struct grep_opt
*opt
, const void *buf
, size_t size
)
18 fwrite(buf
, size
, 1, stdout
);
21 static void color_set(char *dst
, const char *color_bytes
)
23 xsnprintf(dst
, COLOR_MAXLEN
, "%s", color_bytes
);
27 * Initialize the grep_defaults template with hardcoded defaults.
28 * We could let the compiler do this, but without C99 initializers
29 * the code gets unwieldy and unreadable, so...
31 void init_grep_defaults(void)
33 struct grep_opt
*opt
= &grep_defaults
;
40 memset(opt
, 0, sizeof(*opt
));
44 opt
->pattern_type_option
= GREP_PATTERN_TYPE_UNSPECIFIED
;
45 color_set(opt
->color_context
, "");
46 color_set(opt
->color_filename
, "");
47 color_set(opt
->color_function
, "");
48 color_set(opt
->color_lineno
, "");
49 color_set(opt
->color_match_context
, GIT_COLOR_BOLD_RED
);
50 color_set(opt
->color_match_selected
, GIT_COLOR_BOLD_RED
);
51 color_set(opt
->color_selected
, "");
52 color_set(opt
->color_sep
, GIT_COLOR_CYAN
);
54 opt
->output
= std_output
;
57 static int parse_pattern_type_arg(const char *opt
, const char *arg
)
59 if (!strcmp(arg
, "default"))
60 return GREP_PATTERN_TYPE_UNSPECIFIED
;
61 else if (!strcmp(arg
, "basic"))
62 return GREP_PATTERN_TYPE_BRE
;
63 else if (!strcmp(arg
, "extended"))
64 return GREP_PATTERN_TYPE_ERE
;
65 else if (!strcmp(arg
, "fixed"))
66 return GREP_PATTERN_TYPE_FIXED
;
67 else if (!strcmp(arg
, "perl"))
68 return GREP_PATTERN_TYPE_PCRE
;
69 die("bad %s argument: %s", opt
, arg
);
73 * Read the configuration file once and store it in
74 * the grep_defaults template.
76 int grep_config(const char *var
, const char *value
, void *cb
)
78 struct grep_opt
*opt
= &grep_defaults
;
81 if (userdiff_config(var
, value
) < 0)
84 if (!strcmp(var
, "grep.extendedregexp")) {
85 opt
->extended_regexp_option
= git_config_bool(var
, value
);
89 if (!strcmp(var
, "grep.patterntype")) {
90 opt
->pattern_type_option
= parse_pattern_type_arg(var
, value
);
94 if (!strcmp(var
, "grep.linenumber")) {
95 opt
->linenum
= git_config_bool(var
, value
);
99 if (!strcmp(var
, "grep.fullname")) {
100 opt
->relative
= !git_config_bool(var
, value
);
104 if (!strcmp(var
, "color.grep"))
105 opt
->color
= git_config_colorbool(var
, value
);
106 else if (!strcmp(var
, "color.grep.context"))
107 color
= opt
->color_context
;
108 else if (!strcmp(var
, "color.grep.filename"))
109 color
= opt
->color_filename
;
110 else if (!strcmp(var
, "color.grep.function"))
111 color
= opt
->color_function
;
112 else if (!strcmp(var
, "color.grep.linenumber"))
113 color
= opt
->color_lineno
;
114 else if (!strcmp(var
, "color.grep.matchcontext"))
115 color
= opt
->color_match_context
;
116 else if (!strcmp(var
, "color.grep.matchselected"))
117 color
= opt
->color_match_selected
;
118 else if (!strcmp(var
, "color.grep.selected"))
119 color
= opt
->color_selected
;
120 else if (!strcmp(var
, "color.grep.separator"))
121 color
= opt
->color_sep
;
122 else if (!strcmp(var
, "color.grep.match")) {
125 return config_error_nonbool(var
);
126 rc
|= color_parse(value
, opt
->color_match_context
);
127 rc
|= color_parse(value
, opt
->color_match_selected
);
133 return config_error_nonbool(var
);
134 return color_parse(value
, color
);
140 * Initialize one instance of grep_opt and copy the
141 * default values from the template we read the configuration
142 * information in an earlier call to git_config(grep_config).
144 void grep_init(struct grep_opt
*opt
, const char *prefix
)
146 struct grep_opt
*def
= &grep_defaults
;
148 memset(opt
, 0, sizeof(*opt
));
149 opt
->prefix
= prefix
;
150 opt
->prefix_length
= (prefix
&& *prefix
) ? strlen(prefix
) : 0;
151 opt
->pattern_tail
= &opt
->pattern_list
;
152 opt
->header_tail
= &opt
->header_list
;
154 opt
->color
= def
->color
;
155 opt
->extended_regexp_option
= def
->extended_regexp_option
;
156 opt
->pattern_type_option
= def
->pattern_type_option
;
157 opt
->linenum
= def
->linenum
;
158 opt
->max_depth
= def
->max_depth
;
159 opt
->pathname
= def
->pathname
;
160 opt
->relative
= def
->relative
;
161 opt
->output
= def
->output
;
163 color_set(opt
->color_context
, def
->color_context
);
164 color_set(opt
->color_filename
, def
->color_filename
);
165 color_set(opt
->color_function
, def
->color_function
);
166 color_set(opt
->color_lineno
, def
->color_lineno
);
167 color_set(opt
->color_match_context
, def
->color_match_context
);
168 color_set(opt
->color_match_selected
, def
->color_match_selected
);
169 color_set(opt
->color_selected
, def
->color_selected
);
170 color_set(opt
->color_sep
, def
->color_sep
);
173 static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type
, struct grep_opt
*opt
)
176 * When committing to the pattern type by setting the relevant
177 * fields in grep_opt it's generally not necessary to zero out
178 * the fields we're not choosing, since they won't have been
179 * set by anything. The extended_regexp_option field is the
180 * only exception to this.
182 * This is because in the process of parsing grep.patternType
183 * & grep.extendedRegexp we set opt->pattern_type_option and
184 * opt->extended_regexp_option, respectively. We then
185 * internally use opt->extended_regexp_option to see if we're
186 * compiling an ERE. It must be unset if that's not actually
189 if (pattern_type
!= GREP_PATTERN_TYPE_ERE
&&
190 opt
->extended_regexp_option
)
191 opt
->extended_regexp_option
= 0;
193 switch (pattern_type
) {
194 case GREP_PATTERN_TYPE_UNSPECIFIED
:
197 case GREP_PATTERN_TYPE_BRE
:
200 case GREP_PATTERN_TYPE_ERE
:
201 opt
->extended_regexp_option
= 1;
204 case GREP_PATTERN_TYPE_FIXED
:
208 case GREP_PATTERN_TYPE_PCRE
:
213 * It's important that pcre1 always be assigned to
214 * even when there's no USE_LIBPCRE* defined. We still
215 * call the PCRE stub function, it just dies with
216 * "cannot use Perl-compatible regexes[...]".
224 void grep_commit_pattern_type(enum grep_pattern_type pattern_type
, struct grep_opt
*opt
)
226 if (pattern_type
!= GREP_PATTERN_TYPE_UNSPECIFIED
)
227 grep_set_pattern_type_option(pattern_type
, opt
);
228 else if (opt
->pattern_type_option
!= GREP_PATTERN_TYPE_UNSPECIFIED
)
229 grep_set_pattern_type_option(opt
->pattern_type_option
, opt
);
230 else if (opt
->extended_regexp_option
)
232 * This branch *must* happen after setting from the
233 * opt->pattern_type_option above, we don't want
234 * grep.extendedRegexp to override grep.patternType!
236 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE
, opt
);
239 static struct grep_pat
*create_grep_pat(const char *pat
, size_t patlen
,
240 const char *origin
, int no
,
241 enum grep_pat_token t
,
242 enum grep_header_field field
)
244 struct grep_pat
*p
= xcalloc(1, sizeof(*p
));
245 p
->pattern
= xmemdupz(pat
, patlen
);
246 p
->patternlen
= patlen
;
254 static void do_append_grep_pat(struct grep_pat
***tail
, struct grep_pat
*p
)
261 case GREP_PATTERN
: /* atom */
262 case GREP_PATTERN_HEAD
:
263 case GREP_PATTERN_BODY
:
265 struct grep_pat
*new_pat
;
267 char *cp
= p
->pattern
+ p
->patternlen
, *nl
= NULL
;
268 while (++len
<= p
->patternlen
) {
269 if (*(--cp
) == '\n') {
276 new_pat
= create_grep_pat(nl
+ 1, len
- 1, p
->origin
,
277 p
->no
, p
->token
, p
->field
);
278 new_pat
->next
= p
->next
;
280 *tail
= &new_pat
->next
;
283 p
->patternlen
-= len
;
291 void append_header_grep_pattern(struct grep_opt
*opt
,
292 enum grep_header_field field
, const char *pat
)
294 struct grep_pat
*p
= create_grep_pat(pat
, strlen(pat
), "header", 0,
295 GREP_PATTERN_HEAD
, field
);
296 if (field
== GREP_HEADER_REFLOG
)
297 opt
->use_reflog_filter
= 1;
298 do_append_grep_pat(&opt
->header_tail
, p
);
301 void append_grep_pattern(struct grep_opt
*opt
, const char *pat
,
302 const char *origin
, int no
, enum grep_pat_token t
)
304 append_grep_pat(opt
, pat
, strlen(pat
), origin
, no
, t
);
307 void append_grep_pat(struct grep_opt
*opt
, const char *pat
, size_t patlen
,
308 const char *origin
, int no
, enum grep_pat_token t
)
310 struct grep_pat
*p
= create_grep_pat(pat
, patlen
, origin
, no
, t
, 0);
311 do_append_grep_pat(&opt
->pattern_tail
, p
);
314 struct grep_opt
*grep_opt_dup(const struct grep_opt
*opt
)
316 struct grep_pat
*pat
;
317 struct grep_opt
*ret
= xmalloc(sizeof(struct grep_opt
));
320 ret
->pattern_list
= NULL
;
321 ret
->pattern_tail
= &ret
->pattern_list
;
323 for(pat
= opt
->pattern_list
; pat
!= NULL
; pat
= pat
->next
)
325 if(pat
->token
== GREP_PATTERN_HEAD
)
326 append_header_grep_pattern(ret
, pat
->field
,
329 append_grep_pat(ret
, pat
->pattern
, pat
->patternlen
,
330 pat
->origin
, pat
->no
, pat
->token
);
336 static NORETURN
void compile_regexp_failed(const struct grep_pat
*p
,
342 xsnprintf(where
, sizeof(where
), "In '%s' at %d, ", p
->origin
, p
->no
);
344 xsnprintf(where
, sizeof(where
), "%s, ", p
->origin
);
348 die("%s'%s': %s", where
, p
->pattern
, error
);
351 static int is_fixed(const char *s
, size_t len
)
355 for (i
= 0; i
< len
; i
++) {
356 if (is_regex_special(s
[i
]))
363 static int has_null(const char *s
, size_t len
)
366 * regcomp cannot accept patterns with NULs so when using it
367 * we consider any pattern containing a NUL fixed.
369 if (memchr(s
, 0, len
))
376 static void compile_pcre1_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
380 int options
= PCRE_MULTILINE
;
382 if (opt
->ignore_case
) {
383 if (has_non_ascii(p
->pattern
))
384 p
->pcre1_tables
= pcre_maketables();
385 options
|= PCRE_CASELESS
;
387 if (is_utf8_locale() && has_non_ascii(p
->pattern
))
388 options
|= PCRE_UTF8
;
390 p
->pcre1_regexp
= pcre_compile(p
->pattern
, options
, &error
, &erroffset
,
392 if (!p
->pcre1_regexp
)
393 compile_regexp_failed(p
, error
);
395 p
->pcre1_extra_info
= pcre_study(p
->pcre1_regexp
, GIT_PCRE_STUDY_JIT_COMPILE
, &error
);
396 if (!p
->pcre1_extra_info
&& error
)
399 #ifdef GIT_PCRE1_USE_JIT
400 pcre_config(PCRE_CONFIG_JIT
, &p
->pcre1_jit_on
);
401 if (p
->pcre1_jit_on
== 1) {
402 p
->pcre1_jit_stack
= pcre_jit_stack_alloc(1, 1024 * 1024);
403 if (!p
->pcre1_jit_stack
)
404 die("Couldn't allocate PCRE JIT stack");
405 pcre_assign_jit_stack(p
->pcre1_extra_info
, NULL
, p
->pcre1_jit_stack
);
406 } else if (p
->pcre1_jit_on
!= 0) {
407 BUG("The pcre1_jit_on variable should be 0 or 1, not %d",
413 static int pcre1match(struct grep_pat
*p
, const char *line
, const char *eol
,
414 regmatch_t
*match
, int eflags
)
416 int ovector
[30], ret
, flags
= 0;
418 if (eflags
& REG_NOTBOL
)
419 flags
|= PCRE_NOTBOL
;
421 #ifdef GIT_PCRE1_USE_JIT
422 if (p
->pcre1_jit_on
) {
423 ret
= pcre_jit_exec(p
->pcre1_regexp
, p
->pcre1_extra_info
, line
,
424 eol
- line
, 0, flags
, ovector
,
425 ARRAY_SIZE(ovector
), p
->pcre1_jit_stack
);
429 ret
= pcre_exec(p
->pcre1_regexp
, p
->pcre1_extra_info
, line
,
430 eol
- line
, 0, flags
, ovector
,
431 ARRAY_SIZE(ovector
));
434 if (ret
< 0 && ret
!= PCRE_ERROR_NOMATCH
)
435 die("pcre_exec failed with error code %d", ret
);
438 match
->rm_so
= ovector
[0];
439 match
->rm_eo
= ovector
[1];
445 static void free_pcre1_regexp(struct grep_pat
*p
)
447 pcre_free(p
->pcre1_regexp
);
448 #ifdef GIT_PCRE1_USE_JIT
449 if (p
->pcre1_jit_on
) {
450 pcre_free_study(p
->pcre1_extra_info
);
451 pcre_jit_stack_free(p
->pcre1_jit_stack
);
455 pcre_free(p
->pcre1_extra_info
);
457 pcre_free((void *)p
->pcre1_tables
);
459 #else /* !USE_LIBPCRE1 */
460 static void compile_pcre1_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
462 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
465 static int pcre1match(struct grep_pat
*p
, const char *line
, const char *eol
,
466 regmatch_t
*match
, int eflags
)
471 static void free_pcre1_regexp(struct grep_pat
*p
)
474 #endif /* !USE_LIBPCRE1 */
477 static void compile_pcre2_pattern(struct grep_pat
*p
, const struct grep_opt
*opt
)
480 PCRE2_UCHAR errbuf
[256];
481 PCRE2_SIZE erroffset
;
482 int options
= PCRE2_MULTILINE
;
483 const uint8_t *character_tables
= NULL
;
490 p
->pcre2_compile_context
= NULL
;
492 if (opt
->ignore_case
) {
493 if (has_non_ascii(p
->pattern
)) {
494 character_tables
= pcre2_maketables(NULL
);
495 p
->pcre2_compile_context
= pcre2_compile_context_create(NULL
);
496 pcre2_set_character_tables(p
->pcre2_compile_context
, character_tables
);
498 options
|= PCRE2_CASELESS
;
500 if (is_utf8_locale() && has_non_ascii(p
->pattern
))
501 options
|= PCRE2_UTF
;
503 p
->pcre2_pattern
= pcre2_compile((PCRE2_SPTR
)p
->pattern
,
504 p
->patternlen
, options
, &error
, &erroffset
,
505 p
->pcre2_compile_context
);
507 if (p
->pcre2_pattern
) {
508 p
->pcre2_match_data
= pcre2_match_data_create_from_pattern(p
->pcre2_pattern
, NULL
);
509 if (!p
->pcre2_match_data
)
510 die("Couldn't allocate PCRE2 match data");
512 pcre2_get_error_message(error
, errbuf
, sizeof(errbuf
));
513 compile_regexp_failed(p
, (const char *)&errbuf
);
516 pcre2_config(PCRE2_CONFIG_JIT
, &p
->pcre2_jit_on
);
517 if (p
->pcre2_jit_on
== 1) {
518 jitret
= pcre2_jit_compile(p
->pcre2_pattern
, PCRE2_JIT_COMPLETE
);
520 die("Couldn't JIT the PCRE2 pattern '%s', got '%d'\n", p
->pattern
, jitret
);
523 * The pcre2_config(PCRE2_CONFIG_JIT, ...) call just
524 * tells us whether the library itself supports JIT,
525 * but to see whether we're going to be actually using
526 * JIT we need to extract PCRE2_INFO_JITSIZE from the
527 * pattern *after* we do pcre2_jit_compile() above.
529 * This is because if the pattern contains the
530 * (*NO_JIT) verb (see pcre2syntax(3))
531 * pcre2_jit_compile() will exit early with 0. If we
532 * then proceed to call pcre2_jit_match() further down
533 * the line instead of pcre2_match() we'll either
534 * segfault (pre PCRE 10.31) or run into a fatal error
537 patinforet
= pcre2_pattern_info(p
->pcre2_pattern
, PCRE2_INFO_JITSIZE
, &jitsizearg
);
539 BUG("pcre2_pattern_info() failed: %d", patinforet
);
540 if (jitsizearg
== 0) {
545 p
->pcre2_jit_stack
= pcre2_jit_stack_create(1, 1024 * 1024, NULL
);
546 if (!p
->pcre2_jit_stack
)
547 die("Couldn't allocate PCRE2 JIT stack");
548 p
->pcre2_match_context
= pcre2_match_context_create(NULL
);
549 if (!p
->pcre2_match_context
)
550 die("Couldn't allocate PCRE2 match context");
551 pcre2_jit_stack_assign(p
->pcre2_match_context
, NULL
, p
->pcre2_jit_stack
);
552 } else if (p
->pcre2_jit_on
!= 0) {
553 BUG("The pcre2_jit_on variable should be 0 or 1, not %d",
558 static int pcre2match(struct grep_pat
*p
, const char *line
, const char *eol
,
559 regmatch_t
*match
, int eflags
)
563 PCRE2_UCHAR errbuf
[256];
565 if (eflags
& REG_NOTBOL
)
566 flags
|= PCRE2_NOTBOL
;
569 ret
= pcre2_jit_match(p
->pcre2_pattern
, (unsigned char *)line
,
570 eol
- line
, 0, flags
, p
->pcre2_match_data
,
573 ret
= pcre2_match(p
->pcre2_pattern
, (unsigned char *)line
,
574 eol
- line
, 0, flags
, p
->pcre2_match_data
,
577 if (ret
< 0 && ret
!= PCRE2_ERROR_NOMATCH
) {
578 pcre2_get_error_message(ret
, errbuf
, sizeof(errbuf
));
579 die("%s failed with error code %d: %s",
580 (p
->pcre2_jit_on
? "pcre2_jit_match" : "pcre2_match"), ret
,
584 ovector
= pcre2_get_ovector_pointer(p
->pcre2_match_data
);
586 match
->rm_so
= (int)ovector
[0];
587 match
->rm_eo
= (int)ovector
[1];
593 static void free_pcre2_pattern(struct grep_pat
*p
)
595 pcre2_compile_context_free(p
->pcre2_compile_context
);
596 pcre2_code_free(p
->pcre2_pattern
);
597 pcre2_match_data_free(p
->pcre2_match_data
);
598 pcre2_jit_stack_free(p
->pcre2_jit_stack
);
599 pcre2_match_context_free(p
->pcre2_match_context
);
601 #else /* !USE_LIBPCRE2 */
602 static void compile_pcre2_pattern(struct grep_pat
*p
, const struct grep_opt
*opt
)
605 * Unreachable until USE_LIBPCRE2 becomes synonymous with
606 * USE_LIBPCRE. See the sibling comment in
607 * grep_set_pattern_type_option().
609 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
612 static int pcre2match(struct grep_pat
*p
, const char *line
, const char *eol
,
613 regmatch_t
*match
, int eflags
)
618 static void free_pcre2_pattern(struct grep_pat
*p
)
621 #endif /* !USE_LIBPCRE2 */
623 static void compile_fixed_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
625 struct strbuf sb
= STRBUF_INIT
;
629 basic_regex_quote_buf(&sb
, p
->pattern
);
630 if (opt
->ignore_case
)
631 regflags
|= REG_ICASE
;
632 err
= regcomp(&p
->regexp
, sb
.buf
, regflags
);
634 fprintf(stderr
, "fixed %s\n", sb
.buf
);
638 regerror(err
, &p
->regexp
, errbuf
, sizeof(errbuf
));
639 compile_regexp_failed(p
, errbuf
);
643 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
647 int regflags
= REG_NEWLINE
;
649 p
->word_regexp
= opt
->word_regexp
;
650 p
->ignore_case
= opt
->ignore_case
;
651 ascii_only
= !has_non_ascii(p
->pattern
);
654 * Even when -F (fixed) asks us to do a non-regexp search, we
655 * may not be able to correctly case-fold when -i
656 * (ignore-case) is asked (in which case, we'll synthesize a
657 * regexp to match the pattern that matches regexp special
658 * characters literally, while ignoring case differences). On
659 * the other hand, even without -F, if the pattern does not
660 * have any regexp special characters and there is no need for
661 * case-folding search, we can internally turn it into a
662 * simple string match using kws. p->fixed tells us if we
666 has_null(p
->pattern
, p
->patternlen
) ||
667 is_fixed(p
->pattern
, p
->patternlen
))
668 p
->fixed
= !p
->ignore_case
|| ascii_only
;
671 p
->kws
= kwsalloc(p
->ignore_case
? tolower_trans_tbl
: NULL
);
672 kwsincr(p
->kws
, p
->pattern
, p
->patternlen
);
675 } else if (opt
->fixed
) {
677 * We come here when the pattern has the non-ascii
678 * characters we cannot case-fold, and asked to
681 compile_fixed_regexp(p
, opt
);
686 compile_pcre2_pattern(p
, opt
);
691 compile_pcre1_regexp(p
, opt
);
696 regflags
|= REG_ICASE
;
697 if (opt
->extended_regexp_option
)
698 regflags
|= REG_EXTENDED
;
699 err
= regcomp(&p
->regexp
, p
->pattern
, regflags
);
702 regerror(err
, &p
->regexp
, errbuf
, 1024);
703 compile_regexp_failed(p
, errbuf
);
707 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
708 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
717 case GREP_PATTERN
: /* atom */
718 case GREP_PATTERN_HEAD
:
719 case GREP_PATTERN_BODY
:
720 x
= xcalloc(1, sizeof (struct grep_expr
));
721 x
->node
= GREP_NODE_ATOM
;
725 case GREP_OPEN_PAREN
:
727 x
= compile_pattern_or(list
);
728 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
729 die("unmatched parenthesis");
730 *list
= (*list
)->next
;
737 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
748 die("--not not followed by pattern expression");
750 x
= xcalloc(1, sizeof (struct grep_expr
));
751 x
->node
= GREP_NODE_NOT
;
752 x
->u
.unary
= compile_pattern_not(list
);
754 die("--not followed by non pattern expression");
757 return compile_pattern_atom(list
);
761 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
764 struct grep_expr
*x
, *y
, *z
;
766 x
= compile_pattern_not(list
);
768 if (p
&& p
->token
== GREP_AND
) {
770 die("--and not followed by pattern expression");
772 y
= compile_pattern_and(list
);
774 die("--and not followed by pattern expression");
775 z
= xcalloc(1, sizeof (struct grep_expr
));
776 z
->node
= GREP_NODE_AND
;
777 z
->u
.binary
.left
= x
;
778 z
->u
.binary
.right
= y
;
784 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
787 struct grep_expr
*x
, *y
, *z
;
789 x
= compile_pattern_and(list
);
791 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
792 y
= compile_pattern_or(list
);
794 die("not a pattern expression %s", p
->pattern
);
795 z
= xcalloc(1, sizeof (struct grep_expr
));
796 z
->node
= GREP_NODE_OR
;
797 z
->u
.binary
.left
= x
;
798 z
->u
.binary
.right
= y
;
804 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
806 return compile_pattern_or(list
);
809 static void indent(int in
)
815 static void dump_grep_pat(struct grep_pat
*p
)
818 case GREP_AND
: fprintf(stderr
, "*and*"); break;
819 case GREP_OPEN_PAREN
: fprintf(stderr
, "*(*"); break;
820 case GREP_CLOSE_PAREN
: fprintf(stderr
, "*)*"); break;
821 case GREP_NOT
: fprintf(stderr
, "*not*"); break;
822 case GREP_OR
: fprintf(stderr
, "*or*"); break;
824 case GREP_PATTERN
: fprintf(stderr
, "pattern"); break;
825 case GREP_PATTERN_HEAD
: fprintf(stderr
, "pattern_head"); break;
826 case GREP_PATTERN_BODY
: fprintf(stderr
, "pattern_body"); break;
831 case GREP_PATTERN_HEAD
:
832 fprintf(stderr
, "<head %d>", p
->field
); break;
833 case GREP_PATTERN_BODY
:
834 fprintf(stderr
, "<body>"); break;
838 case GREP_PATTERN_HEAD
:
839 case GREP_PATTERN_BODY
:
841 fprintf(stderr
, "%.*s", (int)p
->patternlen
, p
->pattern
);
847 static void dump_grep_expression_1(struct grep_expr
*x
, int in
)
852 fprintf(stderr
, "true\n");
855 dump_grep_pat(x
->u
.atom
);
858 fprintf(stderr
, "(not\n");
859 dump_grep_expression_1(x
->u
.unary
, in
+1);
861 fprintf(stderr
, ")\n");
864 fprintf(stderr
, "(and\n");
865 dump_grep_expression_1(x
->u
.binary
.left
, in
+1);
866 dump_grep_expression_1(x
->u
.binary
.right
, in
+1);
868 fprintf(stderr
, ")\n");
871 fprintf(stderr
, "(or\n");
872 dump_grep_expression_1(x
->u
.binary
.left
, in
+1);
873 dump_grep_expression_1(x
->u
.binary
.right
, in
+1);
875 fprintf(stderr
, ")\n");
880 static void dump_grep_expression(struct grep_opt
*opt
)
882 struct grep_expr
*x
= opt
->pattern_expression
;
885 fprintf(stderr
, "[all-match]\n");
886 dump_grep_expression_1(x
, 0);
890 static struct grep_expr
*grep_true_expr(void)
892 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
893 z
->node
= GREP_NODE_TRUE
;
897 static struct grep_expr
*grep_or_expr(struct grep_expr
*left
, struct grep_expr
*right
)
899 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
900 z
->node
= GREP_NODE_OR
;
901 z
->u
.binary
.left
= left
;
902 z
->u
.binary
.right
= right
;
906 static struct grep_expr
*prep_header_patterns(struct grep_opt
*opt
)
909 struct grep_expr
*header_expr
;
910 struct grep_expr
*(header_group
[GREP_HEADER_FIELD_MAX
]);
911 enum grep_header_field fld
;
913 if (!opt
->header_list
)
916 for (p
= opt
->header_list
; p
; p
= p
->next
) {
917 if (p
->token
!= GREP_PATTERN_HEAD
)
918 BUG("a non-header pattern in grep header list.");
919 if (p
->field
< GREP_HEADER_FIELD_MIN
||
920 GREP_HEADER_FIELD_MAX
<= p
->field
)
921 BUG("unknown header field %d", p
->field
);
922 compile_regexp(p
, opt
);
925 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++)
926 header_group
[fld
] = NULL
;
928 for (p
= opt
->header_list
; p
; p
= p
->next
) {
930 struct grep_pat
*pp
= p
;
932 h
= compile_pattern_atom(&pp
);
933 if (!h
|| pp
!= p
->next
)
934 BUG("malformed header expr");
935 if (!header_group
[p
->field
]) {
936 header_group
[p
->field
] = h
;
939 header_group
[p
->field
] = grep_or_expr(h
, header_group
[p
->field
]);
944 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++) {
945 if (!header_group
[fld
])
948 header_expr
= grep_true_expr();
949 header_expr
= grep_or_expr(header_group
[fld
], header_expr
);
954 static struct grep_expr
*grep_splice_or(struct grep_expr
*x
, struct grep_expr
*y
)
956 struct grep_expr
*z
= x
;
959 assert(x
->node
== GREP_NODE_OR
);
960 if (x
->u
.binary
.right
&&
961 x
->u
.binary
.right
->node
== GREP_NODE_TRUE
) {
962 x
->u
.binary
.right
= y
;
965 x
= x
->u
.binary
.right
;
970 static void compile_grep_patterns_real(struct grep_opt
*opt
)
973 struct grep_expr
*header_expr
= prep_header_patterns(opt
);
975 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
977 case GREP_PATTERN
: /* atom */
978 case GREP_PATTERN_HEAD
:
979 case GREP_PATTERN_BODY
:
980 compile_regexp(p
, opt
);
988 if (opt
->all_match
|| header_expr
)
990 else if (!opt
->extended
&& !opt
->debug
)
993 p
= opt
->pattern_list
;
995 opt
->pattern_expression
= compile_pattern_expr(&p
);
997 die("incomplete pattern expression: %s", p
->pattern
);
1002 if (!opt
->pattern_expression
)
1003 opt
->pattern_expression
= header_expr
;
1004 else if (opt
->all_match
)
1005 opt
->pattern_expression
= grep_splice_or(header_expr
,
1006 opt
->pattern_expression
);
1008 opt
->pattern_expression
= grep_or_expr(opt
->pattern_expression
,
1013 void compile_grep_patterns(struct grep_opt
*opt
)
1015 compile_grep_patterns_real(opt
);
1017 dump_grep_expression(opt
);
1020 static void free_pattern_expr(struct grep_expr
*x
)
1023 case GREP_NODE_TRUE
:
1024 case GREP_NODE_ATOM
:
1027 free_pattern_expr(x
->u
.unary
);
1031 free_pattern_expr(x
->u
.binary
.left
);
1032 free_pattern_expr(x
->u
.binary
.right
);
1038 void free_grep_patterns(struct grep_opt
*opt
)
1040 struct grep_pat
*p
, *n
;
1042 for (p
= opt
->pattern_list
; p
; p
= n
) {
1045 case GREP_PATTERN
: /* atom */
1046 case GREP_PATTERN_HEAD
:
1047 case GREP_PATTERN_BODY
:
1050 else if (p
->pcre1_regexp
)
1051 free_pcre1_regexp(p
);
1052 else if (p
->pcre2_pattern
)
1053 free_pcre2_pattern(p
);
1055 regfree(&p
->regexp
);
1066 free_pattern_expr(opt
->pattern_expression
);
1069 static char *end_of_line(char *cp
, unsigned long *left
)
1071 unsigned long l
= *left
;
1072 while (l
&& *cp
!= '\n') {
1080 static int word_char(char ch
)
1082 return isalnum(ch
) || ch
== '_';
1085 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
1088 if (want_color(opt
->color
) && color
&& color
[0]) {
1089 opt
->output(opt
, color
, strlen(color
));
1090 opt
->output(opt
, data
, size
);
1091 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
1093 opt
->output(opt
, data
, size
);
1096 static void output_sep(struct grep_opt
*opt
, char sign
)
1098 if (opt
->null_following_name
)
1099 opt
->output(opt
, "\0", 1);
1101 output_color(opt
, &sign
, 1, opt
->color_sep
);
1104 static void show_name(struct grep_opt
*opt
, const char *name
)
1106 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
1107 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
1110 static int fixmatch(struct grep_pat
*p
, char *line
, char *eol
,
1113 struct kwsmatch kwsm
;
1114 size_t offset
= kwsexec(p
->kws
, line
, eol
- line
, &kwsm
);
1116 match
->rm_so
= match
->rm_eo
= -1;
1119 match
->rm_so
= offset
;
1120 match
->rm_eo
= match
->rm_so
+ kwsm
.size
[0];
1125 static int patmatch(struct grep_pat
*p
, char *line
, char *eol
,
1126 regmatch_t
*match
, int eflags
)
1131 hit
= !fixmatch(p
, line
, eol
, match
);
1132 else if (p
->pcre1_regexp
)
1133 hit
= !pcre1match(p
, line
, eol
, match
, eflags
);
1134 else if (p
->pcre2_pattern
)
1135 hit
= !pcre2match(p
, line
, eol
, match
, eflags
);
1137 hit
= !regexec_buf(&p
->regexp
, line
, eol
- line
, 1, match
,
1143 static int strip_timestamp(char *bol
, char **eol_p
)
1148 while (bol
< --eol
) {
1162 } header_field
[] = {
1164 { "committer ", 10 },
1168 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
1169 enum grep_context ctx
,
1170 regmatch_t
*pmatch
, int eflags
)
1174 const char *start
= bol
;
1176 if ((p
->token
!= GREP_PATTERN
) &&
1177 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
1180 if (p
->token
== GREP_PATTERN_HEAD
) {
1183 assert(p
->field
< ARRAY_SIZE(header_field
));
1184 field
= header_field
[p
->field
].field
;
1185 len
= header_field
[p
->field
].len
;
1186 if (strncmp(bol
, field
, len
))
1190 case GREP_HEADER_AUTHOR
:
1191 case GREP_HEADER_COMMITTER
:
1192 saved_ch
= strip_timestamp(bol
, &eol
);
1200 hit
= patmatch(p
, bol
, eol
, pmatch
, eflags
);
1202 if (hit
&& p
->word_regexp
) {
1203 if ((pmatch
[0].rm_so
< 0) ||
1204 (eol
- bol
) < pmatch
[0].rm_so
||
1205 (pmatch
[0].rm_eo
< 0) ||
1206 (eol
- bol
) < pmatch
[0].rm_eo
)
1207 die("regexp returned nonsense");
1209 /* Match beginning must be either beginning of the
1210 * line, or at word boundary (i.e. the last char must
1211 * not be a word char). Similarly, match end must be
1212 * either end of the line, or at word boundary
1213 * (i.e. the next char must not be a word char).
1215 if ( ((pmatch
[0].rm_so
== 0) ||
1216 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
1217 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
1218 !word_char(bol
[pmatch
[0].rm_eo
])) )
1223 /* Words consist of at least one character. */
1224 if (pmatch
->rm_so
== pmatch
->rm_eo
)
1227 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
1228 /* There could be more than one match on the
1229 * line, and the first match might not be
1230 * strict word match. But later ones could be!
1231 * Forward to the next possible start, i.e. the
1232 * next position following a non-word char.
1234 bol
= pmatch
[0].rm_so
+ bol
+ 1;
1235 while (word_char(bol
[-1]) && bol
< eol
)
1237 eflags
|= REG_NOTBOL
;
1242 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
1245 pmatch
[0].rm_so
+= bol
- start
;
1246 pmatch
[0].rm_eo
+= bol
- start
;
1251 static int match_expr_eval(struct grep_expr
*x
, char *bol
, char *eol
,
1252 enum grep_context ctx
, int collect_hits
)
1258 die("Not a valid grep expression");
1260 case GREP_NODE_TRUE
:
1263 case GREP_NODE_ATOM
:
1264 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
, &match
, 0);
1267 h
= !match_expr_eval(x
->u
.unary
, bol
, eol
, ctx
, 0);
1270 if (!match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0))
1272 h
= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
1276 return (match_expr_eval(x
->u
.binary
.left
,
1277 bol
, eol
, ctx
, 0) ||
1278 match_expr_eval(x
->u
.binary
.right
,
1280 h
= match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
1281 x
->u
.binary
.left
->hit
|= h
;
1282 h
|= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
1285 die("Unexpected node type (internal error) %d", x
->node
);
1292 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
1293 enum grep_context ctx
, int collect_hits
)
1295 struct grep_expr
*x
= opt
->pattern_expression
;
1296 return match_expr_eval(x
, bol
, eol
, ctx
, collect_hits
);
1299 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
1300 enum grep_context ctx
, int collect_hits
)
1306 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
1308 /* we do not call with collect_hits without being extended */
1309 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1310 if (match_one_pattern(p
, bol
, eol
, ctx
, &match
, 0))
1316 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
1317 enum grep_context ctx
,
1318 regmatch_t
*pmatch
, int eflags
)
1322 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
1324 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
1326 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
1327 if (match
.rm_so
> pmatch
->rm_so
)
1329 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
1332 pmatch
->rm_so
= match
.rm_so
;
1333 pmatch
->rm_eo
= match
.rm_eo
;
1337 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
1338 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
1343 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
1345 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1347 case GREP_PATTERN
: /* atom */
1348 case GREP_PATTERN_HEAD
:
1349 case GREP_PATTERN_BODY
:
1350 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
1361 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
1362 const char *name
, unsigned lno
, char sign
)
1364 int rest
= eol
- bol
;
1365 const char *match_color
, *line_color
= NULL
;
1367 if (opt
->file_break
&& opt
->last_shown
== 0) {
1368 if (opt
->show_hunk_mark
)
1369 opt
->output(opt
, "\n", 1);
1370 } else if (opt
->pre_context
|| opt
->post_context
|| opt
->funcbody
) {
1371 if (opt
->last_shown
== 0) {
1372 if (opt
->show_hunk_mark
) {
1373 output_color(opt
, "--", 2, opt
->color_sep
);
1374 opt
->output(opt
, "\n", 1);
1376 } else if (lno
> opt
->last_shown
+ 1) {
1377 output_color(opt
, "--", 2, opt
->color_sep
);
1378 opt
->output(opt
, "\n", 1);
1381 if (opt
->heading
&& opt
->last_shown
== 0) {
1382 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
1383 opt
->output(opt
, "\n", 1);
1385 opt
->last_shown
= lno
;
1387 if (!opt
->heading
&& opt
->pathname
) {
1388 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
1389 output_sep(opt
, sign
);
1393 xsnprintf(buf
, sizeof(buf
), "%d", lno
);
1394 output_color(opt
, buf
, strlen(buf
), opt
->color_lineno
);
1395 output_sep(opt
, sign
);
1399 enum grep_context ctx
= GREP_CONTEXT_BODY
;
1404 match_color
= opt
->color_match_selected
;
1406 match_color
= opt
->color_match_context
;
1408 line_color
= opt
->color_selected
;
1409 else if (sign
== '-')
1410 line_color
= opt
->color_context
;
1411 else if (sign
== '=')
1412 line_color
= opt
->color_function
;
1414 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
1415 if (match
.rm_so
== match
.rm_eo
)
1418 output_color(opt
, bol
, match
.rm_so
, line_color
);
1419 output_color(opt
, bol
+ match
.rm_so
,
1420 match
.rm_eo
- match
.rm_so
, match_color
);
1422 rest
-= match
.rm_eo
;
1423 eflags
= REG_NOTBOL
;
1427 output_color(opt
, bol
, rest
, line_color
);
1428 opt
->output(opt
, "\n", 1);
1435 * This lock protects access to the gitattributes machinery, which is
1438 pthread_mutex_t grep_attr_mutex
;
1440 static inline void grep_attr_lock(void)
1443 pthread_mutex_lock(&grep_attr_mutex
);
1446 static inline void grep_attr_unlock(void)
1449 pthread_mutex_unlock(&grep_attr_mutex
);
1453 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
1455 pthread_mutex_t grep_read_mutex
;
1458 #define grep_attr_lock()
1459 #define grep_attr_unlock()
1462 static int match_funcname(struct grep_opt
*opt
, struct grep_source
*gs
, char *bol
, char *eol
)
1464 xdemitconf_t
*xecfg
= opt
->priv
;
1465 if (xecfg
&& !xecfg
->find_func
) {
1466 grep_source_load_driver(gs
);
1467 if (gs
->driver
->funcname
.pattern
) {
1468 const struct userdiff_funcname
*pe
= &gs
->driver
->funcname
;
1469 xdiff_set_find_func(xecfg
, pe
->pattern
, pe
->cflags
);
1471 xecfg
= opt
->priv
= NULL
;
1477 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
1478 xecfg
->find_func_priv
) >= 0;
1483 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
1488 static void show_funcname_line(struct grep_opt
*opt
, struct grep_source
*gs
,
1489 char *bol
, unsigned lno
)
1491 while (bol
> gs
->buf
) {
1494 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1498 if (lno
<= opt
->last_shown
)
1501 if (match_funcname(opt
, gs
, bol
, eol
)) {
1502 show_line(opt
, bol
, eol
, gs
->name
, lno
, '=');
1508 static int is_empty_line(const char *bol
, const char *eol
);
1510 static void show_pre_context(struct grep_opt
*opt
, struct grep_source
*gs
,
1511 char *bol
, char *end
, unsigned lno
)
1513 unsigned cur
= lno
, from
= 1, funcname_lno
= 0, orig_from
;
1514 int funcname_needed
= !!opt
->funcname
, comment_needed
= 0;
1516 if (opt
->pre_context
< lno
)
1517 from
= lno
- opt
->pre_context
;
1518 if (from
<= opt
->last_shown
)
1519 from
= opt
->last_shown
+ 1;
1521 if (opt
->funcbody
) {
1522 if (match_funcname(opt
, gs
, bol
, end
))
1525 funcname_needed
= 1;
1526 from
= opt
->last_shown
+ 1;
1530 while (bol
> gs
->buf
&& cur
> from
) {
1531 char *next_bol
= bol
;
1534 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1537 if (comment_needed
&& (is_empty_line(bol
, eol
) ||
1538 match_funcname(opt
, gs
, bol
, eol
))) {
1547 if (funcname_needed
&& match_funcname(opt
, gs
, bol
, eol
)) {
1549 funcname_needed
= 0;
1557 /* We need to look even further back to find a function signature. */
1558 if (opt
->funcname
&& funcname_needed
)
1559 show_funcname_line(opt
, gs
, bol
, cur
);
1563 char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
1565 while (*eol
!= '\n')
1567 show_line(opt
, bol
, eol
, gs
->name
, cur
, sign
);
1573 static int should_lookahead(struct grep_opt
*opt
)
1578 return 0; /* punt for too complex stuff */
1581 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1582 if (p
->token
!= GREP_PATTERN
)
1583 return 0; /* punt for "header only" and stuff */
1588 static int look_ahead(struct grep_opt
*opt
,
1589 unsigned long *left_p
,
1593 unsigned lno
= *lno_p
;
1596 char *sp
, *last_bol
;
1597 regoff_t earliest
= -1;
1599 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1603 hit
= patmatch(p
, bol
, bol
+ *left_p
, &m
, 0);
1604 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
1606 if (earliest
< 0 || m
.rm_so
< earliest
)
1611 *bol_p
= bol
+ *left_p
;
1615 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
1616 ; /* find the beginning of the line */
1619 for (sp
= bol
; sp
< last_bol
; sp
++) {
1623 *left_p
-= last_bol
- bol
;
1629 static int fill_textconv_grep(struct userdiff_driver
*driver
,
1630 struct grep_source
*gs
)
1632 struct diff_filespec
*df
;
1636 if (!driver
|| !driver
->textconv
)
1637 return grep_source_load(gs
);
1640 * The textconv interface is intimately tied to diff_filespecs, so we
1641 * have to pretend to be one. If we could unify the grep_source
1642 * and diff_filespec structs, this mess could just go away.
1644 df
= alloc_filespec(gs
->path
);
1646 case GREP_SOURCE_OID
:
1647 fill_filespec(df
, gs
->identifier
, 1, 0100644);
1649 case GREP_SOURCE_FILE
:
1650 fill_filespec(df
, &null_oid
, 0, 0100644);
1653 BUG("attempt to textconv something without a path?");
1657 * fill_textconv is not remotely thread-safe; it may load objects
1658 * behind the scenes, and it modifies the global diff tempfile
1662 size
= fill_textconv(driver
, df
, &buf
);
1667 * The normal fill_textconv usage by the diff machinery would just keep
1668 * the textconv'd buf separate from the diff_filespec. But much of the
1669 * grep code passes around a grep_source and assumes that its "buf"
1670 * pointer is the beginning of the thing we are searching. So let's
1671 * install our textconv'd version into the grep_source, taking care not
1672 * to leak any existing buffer.
1674 grep_source_clear_data(gs
);
1681 static int is_empty_line(const char *bol
, const char *eol
)
1683 while (bol
< eol
&& isspace(*bol
))
1688 static int grep_source_1(struct grep_opt
*opt
, struct grep_source
*gs
, int collect_hits
)
1691 char *peek_bol
= NULL
;
1694 unsigned last_hit
= 0;
1695 int binary_match_only
= 0;
1697 int try_lookahead
= 0;
1698 int show_function
= 0;
1699 struct userdiff_driver
*textconv
= NULL
;
1700 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
1704 opt
->output
= std_output
;
1706 if (opt
->pre_context
|| opt
->post_context
|| opt
->file_break
||
1708 /* Show hunk marks, except for the first file. */
1709 if (opt
->last_shown
)
1710 opt
->show_hunk_mark
= 1;
1712 * If we're using threads then we can't easily identify
1713 * the first file. Always put hunk marks in that case
1714 * and skip the very first one later in work_done().
1716 if (opt
->output
!= std_output
)
1717 opt
->show_hunk_mark
= 1;
1719 opt
->last_shown
= 0;
1721 if (opt
->allow_textconv
) {
1722 grep_source_load_driver(gs
);
1724 * We might set up the shared textconv cache data here, which
1725 * is not thread-safe.
1728 textconv
= userdiff_get_textconv(gs
->driver
);
1733 * We know the result of a textconv is text, so we only have to care
1734 * about binary handling if we are not using it.
1737 switch (opt
->binary
) {
1738 case GREP_BINARY_DEFAULT
:
1739 if (grep_source_is_binary(gs
))
1740 binary_match_only
= 1;
1742 case GREP_BINARY_NOMATCH
:
1743 if (grep_source_is_binary(gs
))
1744 return 0; /* Assume unmatch */
1746 case GREP_BINARY_TEXT
:
1749 BUG("unknown binary handling mode");
1753 memset(&xecfg
, 0, sizeof(xecfg
));
1756 try_lookahead
= should_lookahead(opt
);
1758 if (fill_textconv_grep(textconv
, gs
) < 0)
1768 * look_ahead() skips quickly to the line that possibly
1769 * has the next hit; don't call it if we need to do
1770 * something more than just skipping the current line
1771 * in response to an unmatch for the current line. E.g.
1772 * inside a post-context window, we will show the current
1773 * line as a context around the previous hit when it
1778 && (show_function
||
1779 lno
<= last_hit
+ opt
->post_context
))
1780 && look_ahead(opt
, &left
, &lno
, &bol
))
1782 eol
= end_of_line(bol
, &left
);
1786 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
1787 ctx
= GREP_CONTEXT_BODY
;
1789 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
1795 /* "grep -v -e foo -e bla" should list lines
1796 * that do not have either, so inversion should
1801 if (opt
->unmatch_name_only
) {
1808 if (opt
->status_only
)
1810 if (opt
->name_only
) {
1811 show_name(opt
, gs
->name
);
1816 if (binary_match_only
) {
1817 opt
->output(opt
, "Binary file ", 12);
1818 output_color(opt
, gs
->name
, strlen(gs
->name
),
1819 opt
->color_filename
);
1820 opt
->output(opt
, " matches\n", 9);
1823 /* Hit at this line. If we haven't shown the
1824 * pre-context lines, we would need to show them.
1826 if (opt
->pre_context
|| opt
->funcbody
)
1827 show_pre_context(opt
, gs
, bol
, eol
, lno
);
1828 else if (opt
->funcname
)
1829 show_funcname_line(opt
, gs
, bol
, lno
);
1830 show_line(opt
, bol
, eol
, gs
->name
, lno
, ':');
1836 if (show_function
&& (!peek_bol
|| peek_bol
< bol
)) {
1837 unsigned long peek_left
= left
;
1838 char *peek_eol
= eol
;
1841 * Trailing empty lines are not interesting.
1842 * Peek past them to see if they belong to the
1843 * body of the current function.
1846 while (is_empty_line(peek_bol
, peek_eol
)) {
1847 peek_bol
= peek_eol
+ 1;
1848 peek_eol
= end_of_line(peek_bol
, &peek_left
);
1851 if (match_funcname(opt
, gs
, peek_bol
, peek_eol
))
1854 if (show_function
||
1855 (last_hit
&& lno
<= last_hit
+ opt
->post_context
)) {
1856 /* If the last hit is within the post context,
1857 * we need to show this line.
1859 show_line(opt
, bol
, eol
, gs
->name
, lno
, '-');
1873 if (opt
->status_only
)
1874 return opt
->unmatch_name_only
;
1875 if (opt
->unmatch_name_only
) {
1876 /* We did not see any hit, so we want to show this */
1877 show_name(opt
, gs
->name
);
1881 xdiff_clear_find_func(&xecfg
);
1885 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1886 * which feels mostly useless but sometimes useful. Maybe
1887 * make it another option? For now suppress them.
1889 if (opt
->count
&& count
) {
1891 if (opt
->pathname
) {
1892 output_color(opt
, gs
->name
, strlen(gs
->name
),
1893 opt
->color_filename
);
1894 output_sep(opt
, ':');
1896 xsnprintf(buf
, sizeof(buf
), "%u\n", count
);
1897 opt
->output(opt
, buf
, strlen(buf
));
1903 static void clr_hit_marker(struct grep_expr
*x
)
1905 /* All-hit markers are meaningful only at the very top level
1910 if (x
->node
!= GREP_NODE_OR
)
1912 x
->u
.binary
.left
->hit
= 0;
1913 x
= x
->u
.binary
.right
;
1917 static int chk_hit_marker(struct grep_expr
*x
)
1919 /* Top level nodes have hit markers. See if they all are hits */
1921 if (x
->node
!= GREP_NODE_OR
)
1923 if (!x
->u
.binary
.left
->hit
)
1925 x
= x
->u
.binary
.right
;
1929 int grep_source(struct grep_opt
*opt
, struct grep_source
*gs
)
1932 * we do not have to do the two-pass grep when we do not check
1933 * buffer-wide "all-match".
1935 if (!opt
->all_match
)
1936 return grep_source_1(opt
, gs
, 0);
1938 /* Otherwise the toplevel "or" terms hit a bit differently.
1939 * We first clear hit markers from them.
1941 clr_hit_marker(opt
->pattern_expression
);
1942 grep_source_1(opt
, gs
, 1);
1944 if (!chk_hit_marker(opt
->pattern_expression
))
1947 return grep_source_1(opt
, gs
, 0);
1950 int grep_buffer(struct grep_opt
*opt
, char *buf
, unsigned long size
)
1952 struct grep_source gs
;
1955 grep_source_init(&gs
, GREP_SOURCE_BUF
, NULL
, NULL
, NULL
);
1959 r
= grep_source(opt
, &gs
);
1961 grep_source_clear(&gs
);
1965 void grep_source_init(struct grep_source
*gs
, enum grep_source_type type
,
1966 const char *name
, const char *path
,
1967 const void *identifier
)
1970 gs
->name
= xstrdup_or_null(name
);
1971 gs
->path
= xstrdup_or_null(path
);
1977 case GREP_SOURCE_FILE
:
1978 gs
->identifier
= xstrdup(identifier
);
1980 case GREP_SOURCE_OID
:
1981 gs
->identifier
= oiddup(identifier
);
1983 case GREP_SOURCE_BUF
:
1984 gs
->identifier
= NULL
;
1989 void grep_source_clear(struct grep_source
*gs
)
1991 FREE_AND_NULL(gs
->name
);
1992 FREE_AND_NULL(gs
->path
);
1993 FREE_AND_NULL(gs
->identifier
);
1994 grep_source_clear_data(gs
);
1997 void grep_source_clear_data(struct grep_source
*gs
)
2000 case GREP_SOURCE_FILE
:
2001 case GREP_SOURCE_OID
:
2002 FREE_AND_NULL(gs
->buf
);
2005 case GREP_SOURCE_BUF
:
2006 /* leave user-provided buf intact */
2011 static int grep_source_load_oid(struct grep_source
*gs
)
2013 enum object_type type
;
2016 gs
->buf
= read_object_file(gs
->identifier
, &type
, &gs
->size
);
2020 return error(_("'%s': unable to read %s"),
2022 oid_to_hex(gs
->identifier
));
2026 static int grep_source_load_file(struct grep_source
*gs
)
2028 const char *filename
= gs
->identifier
;
2034 if (lstat(filename
, &st
) < 0) {
2036 if (errno
!= ENOENT
)
2037 error_errno(_("failed to stat '%s'"), filename
);
2040 if (!S_ISREG(st
.st_mode
))
2042 size
= xsize_t(st
.st_size
);
2043 i
= open(filename
, O_RDONLY
);
2046 data
= xmallocz(size
);
2047 if (st
.st_size
!= read_in_full(i
, data
, size
)) {
2048 error_errno(_("'%s': short read"), filename
);
2060 static int grep_source_load(struct grep_source
*gs
)
2066 case GREP_SOURCE_FILE
:
2067 return grep_source_load_file(gs
);
2068 case GREP_SOURCE_OID
:
2069 return grep_source_load_oid(gs
);
2070 case GREP_SOURCE_BUF
:
2071 return gs
->buf
? 0 : -1;
2073 BUG("invalid grep_source type to load");
2076 void grep_source_load_driver(struct grep_source
*gs
)
2083 gs
->driver
= userdiff_find_by_path(gs
->path
);
2085 gs
->driver
= userdiff_find_by_name("default");
2089 static int grep_source_is_binary(struct grep_source
*gs
)
2091 grep_source_load_driver(gs
);
2092 if (gs
->driver
->binary
!= -1)
2093 return gs
->driver
->binary
;
2095 if (!grep_source_load(gs
))
2096 return buffer_is_binary(gs
->buf
, gs
->size
);