4 #include "xdiff-interface.h"
6 static int grep_source_load(struct grep_source
*gs
);
7 static int grep_source_is_binary(struct grep_source
*gs
);
9 static struct grep_opt grep_defaults
;
12 * Initialize the grep_defaults template with hardcoded defaults.
13 * We could let the compiler do this, but without C99 initializers
14 * the code gets unwieldy and unreadable, so...
16 void init_grep_defaults(void)
18 struct grep_opt
*opt
= &grep_defaults
;
25 memset(opt
, 0, sizeof(*opt
));
28 opt
->regflags
= REG_NEWLINE
;
30 opt
->pattern_type_option
= GREP_PATTERN_TYPE_UNSPECIFIED
;
31 opt
->extended_regexp_option
= 0;
32 strcpy(opt
->color_context
, "");
33 strcpy(opt
->color_filename
, "");
34 strcpy(opt
->color_function
, "");
35 strcpy(opt
->color_lineno
, "");
36 strcpy(opt
->color_match
, GIT_COLOR_BOLD_RED
);
37 strcpy(opt
->color_selected
, "");
38 strcpy(opt
->color_sep
, GIT_COLOR_CYAN
);
42 static int parse_pattern_type_arg(const char *opt
, const char *arg
)
44 if (!strcmp(arg
, "default"))
45 return GREP_PATTERN_TYPE_UNSPECIFIED
;
46 else if (!strcmp(arg
, "basic"))
47 return GREP_PATTERN_TYPE_BRE
;
48 else if (!strcmp(arg
, "extended"))
49 return GREP_PATTERN_TYPE_ERE
;
50 else if (!strcmp(arg
, "fixed"))
51 return GREP_PATTERN_TYPE_FIXED
;
52 else if (!strcmp(arg
, "perl"))
53 return GREP_PATTERN_TYPE_PCRE
;
54 die("bad %s argument: %s", opt
, arg
);
58 * Read the configuration file once and store it in
59 * the grep_defaults template.
61 int grep_config(const char *var
, const char *value
, void *cb
)
63 struct grep_opt
*opt
= &grep_defaults
;
66 if (userdiff_config(var
, value
) < 0)
69 if (!strcmp(var
, "grep.extendedregexp")) {
70 if (git_config_bool(var
, value
))
71 opt
->extended_regexp_option
= 1;
73 opt
->extended_regexp_option
= 0;
77 if (!strcmp(var
, "grep.patterntype")) {
78 opt
->pattern_type_option
= parse_pattern_type_arg(var
, value
);
82 if (!strcmp(var
, "grep.linenumber")) {
83 opt
->linenum
= git_config_bool(var
, value
);
87 if (!strcmp(var
, "color.grep"))
88 opt
->color
= git_config_colorbool(var
, value
);
89 else if (!strcmp(var
, "color.grep.context"))
90 color
= opt
->color_context
;
91 else if (!strcmp(var
, "color.grep.filename"))
92 color
= opt
->color_filename
;
93 else if (!strcmp(var
, "color.grep.function"))
94 color
= opt
->color_function
;
95 else if (!strcmp(var
, "color.grep.linenumber"))
96 color
= opt
->color_lineno
;
97 else if (!strcmp(var
, "color.grep.match"))
98 color
= opt
->color_match
;
99 else if (!strcmp(var
, "color.grep.selected"))
100 color
= opt
->color_selected
;
101 else if (!strcmp(var
, "color.grep.separator"))
102 color
= opt
->color_sep
;
106 return config_error_nonbool(var
);
107 color_parse(value
, var
, color
);
113 * Initialize one instance of grep_opt and copy the
114 * default values from the template we read the configuration
115 * information in an earlier call to git_config(grep_config).
117 void grep_init(struct grep_opt
*opt
, const char *prefix
)
119 struct grep_opt
*def
= &grep_defaults
;
121 memset(opt
, 0, sizeof(*opt
));
122 opt
->prefix
= prefix
;
123 opt
->prefix_length
= (prefix
&& *prefix
) ? strlen(prefix
) : 0;
124 opt
->pattern_tail
= &opt
->pattern_list
;
125 opt
->header_tail
= &opt
->header_list
;
127 opt
->color
= def
->color
;
128 opt
->extended_regexp_option
= def
->extended_regexp_option
;
129 opt
->pattern_type_option
= def
->pattern_type_option
;
130 opt
->linenum
= def
->linenum
;
131 opt
->max_depth
= def
->max_depth
;
132 opt
->pathname
= def
->pathname
;
133 opt
->regflags
= def
->regflags
;
134 opt
->relative
= def
->relative
;
136 strcpy(opt
->color_context
, def
->color_context
);
137 strcpy(opt
->color_filename
, def
->color_filename
);
138 strcpy(opt
->color_function
, def
->color_function
);
139 strcpy(opt
->color_lineno
, def
->color_lineno
);
140 strcpy(opt
->color_match
, def
->color_match
);
141 strcpy(opt
->color_selected
, def
->color_selected
);
142 strcpy(opt
->color_sep
, def
->color_sep
);
145 void grep_commit_pattern_type(enum grep_pattern_type pattern_type
, struct grep_opt
*opt
)
147 if (pattern_type
!= GREP_PATTERN_TYPE_UNSPECIFIED
)
148 grep_set_pattern_type_option(pattern_type
, opt
);
149 else if (opt
->pattern_type_option
!= GREP_PATTERN_TYPE_UNSPECIFIED
)
150 grep_set_pattern_type_option(opt
->pattern_type_option
, opt
);
151 else if (opt
->extended_regexp_option
)
152 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE
, opt
);
155 void grep_set_pattern_type_option(enum grep_pattern_type pattern_type
, struct grep_opt
*opt
)
157 switch (pattern_type
) {
158 case GREP_PATTERN_TYPE_UNSPECIFIED
:
161 case GREP_PATTERN_TYPE_BRE
:
164 opt
->regflags
&= ~REG_EXTENDED
;
167 case GREP_PATTERN_TYPE_ERE
:
170 opt
->regflags
|= REG_EXTENDED
;
173 case GREP_PATTERN_TYPE_FIXED
:
176 opt
->regflags
&= ~REG_EXTENDED
;
179 case GREP_PATTERN_TYPE_PCRE
:
182 opt
->regflags
&= ~REG_EXTENDED
;
187 static struct grep_pat
*create_grep_pat(const char *pat
, size_t patlen
,
188 const char *origin
, int no
,
189 enum grep_pat_token t
,
190 enum grep_header_field field
)
192 struct grep_pat
*p
= xcalloc(1, sizeof(*p
));
193 p
->pattern
= xmemdupz(pat
, patlen
);
194 p
->patternlen
= patlen
;
202 static void do_append_grep_pat(struct grep_pat
***tail
, struct grep_pat
*p
)
209 case GREP_PATTERN
: /* atom */
210 case GREP_PATTERN_HEAD
:
211 case GREP_PATTERN_BODY
:
213 struct grep_pat
*new_pat
;
215 char *cp
= p
->pattern
+ p
->patternlen
, *nl
= NULL
;
216 while (++len
<= p
->patternlen
) {
217 if (*(--cp
) == '\n') {
224 new_pat
= create_grep_pat(nl
+ 1, len
- 1, p
->origin
,
225 p
->no
, p
->token
, p
->field
);
226 new_pat
->next
= p
->next
;
228 *tail
= &new_pat
->next
;
231 p
->patternlen
-= len
;
239 void append_header_grep_pattern(struct grep_opt
*opt
,
240 enum grep_header_field field
, const char *pat
)
242 struct grep_pat
*p
= create_grep_pat(pat
, strlen(pat
), "header", 0,
243 GREP_PATTERN_HEAD
, field
);
244 if (field
== GREP_HEADER_REFLOG
)
245 opt
->use_reflog_filter
= 1;
246 do_append_grep_pat(&opt
->header_tail
, p
);
249 void append_grep_pattern(struct grep_opt
*opt
, const char *pat
,
250 const char *origin
, int no
, enum grep_pat_token t
)
252 append_grep_pat(opt
, pat
, strlen(pat
), origin
, no
, t
);
255 void append_grep_pat(struct grep_opt
*opt
, const char *pat
, size_t patlen
,
256 const char *origin
, int no
, enum grep_pat_token t
)
258 struct grep_pat
*p
= create_grep_pat(pat
, patlen
, origin
, no
, t
, 0);
259 do_append_grep_pat(&opt
->pattern_tail
, p
);
262 struct grep_opt
*grep_opt_dup(const struct grep_opt
*opt
)
264 struct grep_pat
*pat
;
265 struct grep_opt
*ret
= xmalloc(sizeof(struct grep_opt
));
268 ret
->pattern_list
= NULL
;
269 ret
->pattern_tail
= &ret
->pattern_list
;
271 for(pat
= opt
->pattern_list
; pat
!= NULL
; pat
= pat
->next
)
273 if(pat
->token
== GREP_PATTERN_HEAD
)
274 append_header_grep_pattern(ret
, pat
->field
,
277 append_grep_pat(ret
, pat
->pattern
, pat
->patternlen
,
278 pat
->origin
, pat
->no
, pat
->token
);
284 static NORETURN
void compile_regexp_failed(const struct grep_pat
*p
,
290 sprintf(where
, "In '%s' at %d, ", p
->origin
, p
->no
);
292 sprintf(where
, "%s, ", p
->origin
);
296 die("%s'%s': %s", where
, p
->pattern
, error
);
300 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
304 int options
= PCRE_MULTILINE
;
306 if (opt
->ignore_case
)
307 options
|= PCRE_CASELESS
;
309 p
->pcre_regexp
= pcre_compile(p
->pattern
, options
, &error
, &erroffset
,
312 compile_regexp_failed(p
, error
);
314 p
->pcre_extra_info
= pcre_study(p
->pcre_regexp
, 0, &error
);
315 if (!p
->pcre_extra_info
&& error
)
319 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
320 regmatch_t
*match
, int eflags
)
322 int ovector
[30], ret
, flags
= 0;
324 if (eflags
& REG_NOTBOL
)
325 flags
|= PCRE_NOTBOL
;
327 ret
= pcre_exec(p
->pcre_regexp
, p
->pcre_extra_info
, line
, eol
- line
,
328 0, flags
, ovector
, ARRAY_SIZE(ovector
));
329 if (ret
< 0 && ret
!= PCRE_ERROR_NOMATCH
)
330 die("pcre_exec failed with error code %d", ret
);
333 match
->rm_so
= ovector
[0];
334 match
->rm_eo
= ovector
[1];
340 static void free_pcre_regexp(struct grep_pat
*p
)
342 pcre_free(p
->pcre_regexp
);
343 pcre_free(p
->pcre_extra_info
);
345 #else /* !USE_LIBPCRE */
346 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
348 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
351 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
352 regmatch_t
*match
, int eflags
)
357 static void free_pcre_regexp(struct grep_pat
*p
)
360 #endif /* !USE_LIBPCRE */
362 static int is_fixed(const char *s
, size_t len
)
366 /* regcomp cannot accept patterns with NULs so we
367 * consider any pattern containing a NUL fixed.
369 if (memchr(s
, 0, len
))
372 for (i
= 0; i
< len
; i
++) {
373 if (is_regex_special(s
[i
]))
380 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
384 p
->word_regexp
= opt
->word_regexp
;
385 p
->ignore_case
= opt
->ignore_case
;
387 if (opt
->fixed
|| is_fixed(p
->pattern
, p
->patternlen
))
393 if (opt
->regflags
& REG_ICASE
|| p
->ignore_case
)
394 p
->kws
= kwsalloc(tolower_trans_tbl
);
396 p
->kws
= kwsalloc(NULL
);
397 kwsincr(p
->kws
, p
->pattern
, p
->patternlen
);
403 compile_pcre_regexp(p
, opt
);
407 err
= regcomp(&p
->regexp
, p
->pattern
, opt
->regflags
);
410 regerror(err
, &p
->regexp
, errbuf
, 1024);
412 compile_regexp_failed(p
, errbuf
);
416 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
417 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
426 case GREP_PATTERN
: /* atom */
427 case GREP_PATTERN_HEAD
:
428 case GREP_PATTERN_BODY
:
429 x
= xcalloc(1, sizeof (struct grep_expr
));
430 x
->node
= GREP_NODE_ATOM
;
434 case GREP_OPEN_PAREN
:
436 x
= compile_pattern_or(list
);
437 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
438 die("unmatched parenthesis");
439 *list
= (*list
)->next
;
446 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
457 die("--not not followed by pattern expression");
459 x
= xcalloc(1, sizeof (struct grep_expr
));
460 x
->node
= GREP_NODE_NOT
;
461 x
->u
.unary
= compile_pattern_not(list
);
463 die("--not followed by non pattern expression");
466 return compile_pattern_atom(list
);
470 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
473 struct grep_expr
*x
, *y
, *z
;
475 x
= compile_pattern_not(list
);
477 if (p
&& p
->token
== GREP_AND
) {
479 die("--and not followed by pattern expression");
481 y
= compile_pattern_and(list
);
483 die("--and not followed by pattern expression");
484 z
= xcalloc(1, sizeof (struct grep_expr
));
485 z
->node
= GREP_NODE_AND
;
486 z
->u
.binary
.left
= x
;
487 z
->u
.binary
.right
= y
;
493 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
496 struct grep_expr
*x
, *y
, *z
;
498 x
= compile_pattern_and(list
);
500 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
501 y
= compile_pattern_or(list
);
503 die("not a pattern expression %s", p
->pattern
);
504 z
= xcalloc(1, sizeof (struct grep_expr
));
505 z
->node
= GREP_NODE_OR
;
506 z
->u
.binary
.left
= x
;
507 z
->u
.binary
.right
= y
;
513 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
515 return compile_pattern_or(list
);
518 static void indent(int in
)
524 static void dump_grep_pat(struct grep_pat
*p
)
527 case GREP_AND
: fprintf(stderr
, "*and*"); break;
528 case GREP_OPEN_PAREN
: fprintf(stderr
, "*(*"); break;
529 case GREP_CLOSE_PAREN
: fprintf(stderr
, "*)*"); break;
530 case GREP_NOT
: fprintf(stderr
, "*not*"); break;
531 case GREP_OR
: fprintf(stderr
, "*or*"); break;
533 case GREP_PATTERN
: fprintf(stderr
, "pattern"); break;
534 case GREP_PATTERN_HEAD
: fprintf(stderr
, "pattern_head"); break;
535 case GREP_PATTERN_BODY
: fprintf(stderr
, "pattern_body"); break;
540 case GREP_PATTERN_HEAD
:
541 fprintf(stderr
, "<head %d>", p
->field
); break;
542 case GREP_PATTERN_BODY
:
543 fprintf(stderr
, "<body>"); break;
547 case GREP_PATTERN_HEAD
:
548 case GREP_PATTERN_BODY
:
550 fprintf(stderr
, "%.*s", (int)p
->patternlen
, p
->pattern
);
556 static void dump_grep_expression_1(struct grep_expr
*x
, int in
)
561 fprintf(stderr
, "true\n");
564 dump_grep_pat(x
->u
.atom
);
567 fprintf(stderr
, "(not\n");
568 dump_grep_expression_1(x
->u
.unary
, in
+1);
570 fprintf(stderr
, ")\n");
573 fprintf(stderr
, "(and\n");
574 dump_grep_expression_1(x
->u
.binary
.left
, in
+1);
575 dump_grep_expression_1(x
->u
.binary
.right
, in
+1);
577 fprintf(stderr
, ")\n");
580 fprintf(stderr
, "(or\n");
581 dump_grep_expression_1(x
->u
.binary
.left
, in
+1);
582 dump_grep_expression_1(x
->u
.binary
.right
, in
+1);
584 fprintf(stderr
, ")\n");
589 static void dump_grep_expression(struct grep_opt
*opt
)
591 struct grep_expr
*x
= opt
->pattern_expression
;
594 fprintf(stderr
, "[all-match]\n");
595 dump_grep_expression_1(x
, 0);
599 static struct grep_expr
*grep_true_expr(void)
601 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
602 z
->node
= GREP_NODE_TRUE
;
606 static struct grep_expr
*grep_or_expr(struct grep_expr
*left
, struct grep_expr
*right
)
608 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
609 z
->node
= GREP_NODE_OR
;
610 z
->u
.binary
.left
= left
;
611 z
->u
.binary
.right
= right
;
615 static struct grep_expr
*prep_header_patterns(struct grep_opt
*opt
)
618 struct grep_expr
*header_expr
;
619 struct grep_expr
*(header_group
[GREP_HEADER_FIELD_MAX
]);
620 enum grep_header_field fld
;
622 if (!opt
->header_list
)
625 for (p
= opt
->header_list
; p
; p
= p
->next
) {
626 if (p
->token
!= GREP_PATTERN_HEAD
)
627 die("bug: a non-header pattern in grep header list.");
628 if (p
->field
< 0 || GREP_HEADER_FIELD_MAX
<= p
->field
)
629 die("bug: unknown header field %d", p
->field
);
630 compile_regexp(p
, opt
);
633 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++)
634 header_group
[fld
] = NULL
;
636 for (p
= opt
->header_list
; p
; p
= p
->next
) {
638 struct grep_pat
*pp
= p
;
640 h
= compile_pattern_atom(&pp
);
641 if (!h
|| pp
!= p
->next
)
642 die("bug: malformed header expr");
643 if (!header_group
[p
->field
]) {
644 header_group
[p
->field
] = h
;
647 header_group
[p
->field
] = grep_or_expr(h
, header_group
[p
->field
]);
652 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++) {
653 if (!header_group
[fld
])
656 header_expr
= grep_true_expr();
657 header_expr
= grep_or_expr(header_group
[fld
], header_expr
);
662 static struct grep_expr
*grep_splice_or(struct grep_expr
*x
, struct grep_expr
*y
)
664 struct grep_expr
*z
= x
;
667 assert(x
->node
== GREP_NODE_OR
);
668 if (x
->u
.binary
.right
&&
669 x
->u
.binary
.right
->node
== GREP_NODE_TRUE
) {
670 x
->u
.binary
.right
= y
;
673 x
= x
->u
.binary
.right
;
678 static void compile_grep_patterns_real(struct grep_opt
*opt
)
681 struct grep_expr
*header_expr
= prep_header_patterns(opt
);
683 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
685 case GREP_PATTERN
: /* atom */
686 case GREP_PATTERN_HEAD
:
687 case GREP_PATTERN_BODY
:
688 compile_regexp(p
, opt
);
696 if (opt
->all_match
|| header_expr
)
698 else if (!opt
->extended
&& !opt
->debug
)
701 p
= opt
->pattern_list
;
703 opt
->pattern_expression
= compile_pattern_expr(&p
);
705 die("incomplete pattern expression: %s", p
->pattern
);
710 if (!opt
->pattern_expression
)
711 opt
->pattern_expression
= header_expr
;
712 else if (opt
->all_match
)
713 opt
->pattern_expression
= grep_splice_or(header_expr
,
714 opt
->pattern_expression
);
716 opt
->pattern_expression
= grep_or_expr(opt
->pattern_expression
,
721 void compile_grep_patterns(struct grep_opt
*opt
)
723 compile_grep_patterns_real(opt
);
725 dump_grep_expression(opt
);
728 static void free_pattern_expr(struct grep_expr
*x
)
735 free_pattern_expr(x
->u
.unary
);
739 free_pattern_expr(x
->u
.binary
.left
);
740 free_pattern_expr(x
->u
.binary
.right
);
746 void free_grep_patterns(struct grep_opt
*opt
)
748 struct grep_pat
*p
, *n
;
750 for (p
= opt
->pattern_list
; p
; p
= n
) {
753 case GREP_PATTERN
: /* atom */
754 case GREP_PATTERN_HEAD
:
755 case GREP_PATTERN_BODY
:
758 else if (p
->pcre_regexp
)
772 free_pattern_expr(opt
->pattern_expression
);
775 static char *end_of_line(char *cp
, unsigned long *left
)
777 unsigned long l
= *left
;
778 while (l
&& *cp
!= '\n') {
786 static int word_char(char ch
)
788 return isalnum(ch
) || ch
== '_';
791 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
794 if (want_color(opt
->color
) && color
&& color
[0]) {
795 opt
->output(opt
, color
, strlen(color
));
796 opt
->output(opt
, data
, size
);
797 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
799 opt
->output(opt
, data
, size
);
802 static void output_sep(struct grep_opt
*opt
, char sign
)
804 if (opt
->null_following_name
)
805 opt
->output(opt
, "\0", 1);
807 output_color(opt
, &sign
, 1, opt
->color_sep
);
810 static void show_name(struct grep_opt
*opt
, const char *name
)
812 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
813 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
816 static int fixmatch(struct grep_pat
*p
, char *line
, char *eol
,
819 struct kwsmatch kwsm
;
820 size_t offset
= kwsexec(p
->kws
, line
, eol
- line
, &kwsm
);
822 match
->rm_so
= match
->rm_eo
= -1;
825 match
->rm_so
= offset
;
826 match
->rm_eo
= match
->rm_so
+ kwsm
.size
[0];
831 static int regmatch(const regex_t
*preg
, char *line
, char *eol
,
832 regmatch_t
*match
, int eflags
)
836 match
->rm_eo
= eol
- line
;
837 eflags
|= REG_STARTEND
;
839 return regexec(preg
, line
, 1, match
, eflags
);
842 static int patmatch(struct grep_pat
*p
, char *line
, char *eol
,
843 regmatch_t
*match
, int eflags
)
848 hit
= !fixmatch(p
, line
, eol
, match
);
849 else if (p
->pcre_regexp
)
850 hit
= !pcrematch(p
, line
, eol
, match
, eflags
);
852 hit
= !regmatch(&p
->regexp
, line
, eol
, match
, eflags
);
857 static int strip_timestamp(char *bol
, char **eol_p
)
862 while (bol
< --eol
) {
878 { "committer ", 10 },
882 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
883 enum grep_context ctx
,
884 regmatch_t
*pmatch
, int eflags
)
888 const char *start
= bol
;
890 if ((p
->token
!= GREP_PATTERN
) &&
891 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
894 if (p
->token
== GREP_PATTERN_HEAD
) {
897 assert(p
->field
< ARRAY_SIZE(header_field
));
898 field
= header_field
[p
->field
].field
;
899 len
= header_field
[p
->field
].len
;
900 if (strncmp(bol
, field
, len
))
904 case GREP_HEADER_AUTHOR
:
905 case GREP_HEADER_COMMITTER
:
906 saved_ch
= strip_timestamp(bol
, &eol
);
914 hit
= patmatch(p
, bol
, eol
, pmatch
, eflags
);
916 if (hit
&& p
->word_regexp
) {
917 if ((pmatch
[0].rm_so
< 0) ||
918 (eol
- bol
) < pmatch
[0].rm_so
||
919 (pmatch
[0].rm_eo
< 0) ||
920 (eol
- bol
) < pmatch
[0].rm_eo
)
921 die("regexp returned nonsense");
923 /* Match beginning must be either beginning of the
924 * line, or at word boundary (i.e. the last char must
925 * not be a word char). Similarly, match end must be
926 * either end of the line, or at word boundary
927 * (i.e. the next char must not be a word char).
929 if ( ((pmatch
[0].rm_so
== 0) ||
930 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
931 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
932 !word_char(bol
[pmatch
[0].rm_eo
])) )
937 /* Words consist of at least one character. */
938 if (pmatch
->rm_so
== pmatch
->rm_eo
)
941 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
942 /* There could be more than one match on the
943 * line, and the first match might not be
944 * strict word match. But later ones could be!
945 * Forward to the next possible start, i.e. the
946 * next position following a non-word char.
948 bol
= pmatch
[0].rm_so
+ bol
+ 1;
949 while (word_char(bol
[-1]) && bol
< eol
)
951 eflags
|= REG_NOTBOL
;
956 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
959 pmatch
[0].rm_so
+= bol
- start
;
960 pmatch
[0].rm_eo
+= bol
- start
;
965 static int match_expr_eval(struct grep_expr
*x
, char *bol
, char *eol
,
966 enum grep_context ctx
, int collect_hits
)
972 die("Not a valid grep expression");
978 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
, &match
, 0);
981 h
= !match_expr_eval(x
->u
.unary
, bol
, eol
, ctx
, 0);
984 if (!match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0))
986 h
= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
990 return (match_expr_eval(x
->u
.binary
.left
,
992 match_expr_eval(x
->u
.binary
.right
,
994 h
= match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
995 x
->u
.binary
.left
->hit
|= h
;
996 h
|= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
999 die("Unexpected node type (internal error) %d", x
->node
);
1006 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
1007 enum grep_context ctx
, int collect_hits
)
1009 struct grep_expr
*x
= opt
->pattern_expression
;
1010 return match_expr_eval(x
, bol
, eol
, ctx
, collect_hits
);
1013 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
1014 enum grep_context ctx
, int collect_hits
)
1020 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
1022 /* we do not call with collect_hits without being extended */
1023 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1024 if (match_one_pattern(p
, bol
, eol
, ctx
, &match
, 0))
1030 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
1031 enum grep_context ctx
,
1032 regmatch_t
*pmatch
, int eflags
)
1036 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
1038 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
1040 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
1041 if (match
.rm_so
> pmatch
->rm_so
)
1043 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
1046 pmatch
->rm_so
= match
.rm_so
;
1047 pmatch
->rm_eo
= match
.rm_eo
;
1051 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
1052 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
1057 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
1059 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1061 case GREP_PATTERN
: /* atom */
1062 case GREP_PATTERN_HEAD
:
1063 case GREP_PATTERN_BODY
:
1064 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
1075 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
1076 const char *name
, unsigned lno
, char sign
)
1078 int rest
= eol
- bol
;
1079 char *line_color
= NULL
;
1081 if (opt
->file_break
&& opt
->last_shown
== 0) {
1082 if (opt
->show_hunk_mark
)
1083 opt
->output(opt
, "\n", 1);
1084 } else if (opt
->pre_context
|| opt
->post_context
|| opt
->funcbody
) {
1085 if (opt
->last_shown
== 0) {
1086 if (opt
->show_hunk_mark
) {
1087 output_color(opt
, "--", 2, opt
->color_sep
);
1088 opt
->output(opt
, "\n", 1);
1090 } else if (lno
> opt
->last_shown
+ 1) {
1091 output_color(opt
, "--", 2, opt
->color_sep
);
1092 opt
->output(opt
, "\n", 1);
1095 if (opt
->heading
&& opt
->last_shown
== 0) {
1096 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
1097 opt
->output(opt
, "\n", 1);
1099 opt
->last_shown
= lno
;
1101 if (!opt
->heading
&& opt
->pathname
) {
1102 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
1103 output_sep(opt
, sign
);
1107 snprintf(buf
, sizeof(buf
), "%d", lno
);
1108 output_color(opt
, buf
, strlen(buf
), opt
->color_lineno
);
1109 output_sep(opt
, sign
);
1113 enum grep_context ctx
= GREP_CONTEXT_BODY
;
1118 line_color
= opt
->color_selected
;
1119 else if (sign
== '-')
1120 line_color
= opt
->color_context
;
1121 else if (sign
== '=')
1122 line_color
= opt
->color_function
;
1124 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
1125 if (match
.rm_so
== match
.rm_eo
)
1128 output_color(opt
, bol
, match
.rm_so
, line_color
);
1129 output_color(opt
, bol
+ match
.rm_so
,
1130 match
.rm_eo
- match
.rm_so
,
1133 rest
-= match
.rm_eo
;
1134 eflags
= REG_NOTBOL
;
1138 output_color(opt
, bol
, rest
, line_color
);
1139 opt
->output(opt
, "\n", 1);
1146 * This lock protects access to the gitattributes machinery, which is
1149 pthread_mutex_t grep_attr_mutex
;
1151 static inline void grep_attr_lock(void)
1154 pthread_mutex_lock(&grep_attr_mutex
);
1157 static inline void grep_attr_unlock(void)
1160 pthread_mutex_unlock(&grep_attr_mutex
);
1164 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
1166 pthread_mutex_t grep_read_mutex
;
1169 #define grep_attr_lock()
1170 #define grep_attr_unlock()
1173 static int match_funcname(struct grep_opt
*opt
, struct grep_source
*gs
, char *bol
, char *eol
)
1175 xdemitconf_t
*xecfg
= opt
->priv
;
1176 if (xecfg
&& !xecfg
->find_func
) {
1177 grep_source_load_driver(gs
);
1178 if (gs
->driver
->funcname
.pattern
) {
1179 const struct userdiff_funcname
*pe
= &gs
->driver
->funcname
;
1180 xdiff_set_find_func(xecfg
, pe
->pattern
, pe
->cflags
);
1182 xecfg
= opt
->priv
= NULL
;
1188 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
1189 xecfg
->find_func_priv
) >= 0;
1194 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
1199 static void show_funcname_line(struct grep_opt
*opt
, struct grep_source
*gs
,
1200 char *bol
, unsigned lno
)
1202 while (bol
> gs
->buf
) {
1205 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1209 if (lno
<= opt
->last_shown
)
1212 if (match_funcname(opt
, gs
, bol
, eol
)) {
1213 show_line(opt
, bol
, eol
, gs
->name
, lno
, '=');
1219 static void show_pre_context(struct grep_opt
*opt
, struct grep_source
*gs
,
1220 char *bol
, char *end
, unsigned lno
)
1222 unsigned cur
= lno
, from
= 1, funcname_lno
= 0;
1223 int funcname_needed
= !!opt
->funcname
;
1225 if (opt
->funcbody
&& !match_funcname(opt
, gs
, bol
, end
))
1226 funcname_needed
= 2;
1228 if (opt
->pre_context
< lno
)
1229 from
= lno
- opt
->pre_context
;
1230 if (from
<= opt
->last_shown
)
1231 from
= opt
->last_shown
+ 1;
1234 while (bol
> gs
->buf
&&
1235 cur
> (funcname_needed
== 2 ? opt
->last_shown
+ 1 : from
)) {
1238 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1241 if (funcname_needed
&& match_funcname(opt
, gs
, bol
, eol
)) {
1243 funcname_needed
= 0;
1247 /* We need to look even further back to find a function signature. */
1248 if (opt
->funcname
&& funcname_needed
)
1249 show_funcname_line(opt
, gs
, bol
, cur
);
1253 char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
1255 while (*eol
!= '\n')
1257 show_line(opt
, bol
, eol
, gs
->name
, cur
, sign
);
1263 static int should_lookahead(struct grep_opt
*opt
)
1268 return 0; /* punt for too complex stuff */
1271 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1272 if (p
->token
!= GREP_PATTERN
)
1273 return 0; /* punt for "header only" and stuff */
1278 static int look_ahead(struct grep_opt
*opt
,
1279 unsigned long *left_p
,
1283 unsigned lno
= *lno_p
;
1286 char *sp
, *last_bol
;
1287 regoff_t earliest
= -1;
1289 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1293 hit
= patmatch(p
, bol
, bol
+ *left_p
, &m
, 0);
1294 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
1296 if (earliest
< 0 || m
.rm_so
< earliest
)
1301 *bol_p
= bol
+ *left_p
;
1305 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
1306 ; /* find the beginning of the line */
1309 for (sp
= bol
; sp
< last_bol
; sp
++) {
1313 *left_p
-= last_bol
- bol
;
1319 static void std_output(struct grep_opt
*opt
, const void *buf
, size_t size
)
1321 fwrite(buf
, size
, 1, stdout
);
1324 static int grep_source_1(struct grep_opt
*opt
, struct grep_source
*gs
, int collect_hits
)
1329 unsigned last_hit
= 0;
1330 int binary_match_only
= 0;
1332 int try_lookahead
= 0;
1333 int show_function
= 0;
1334 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
1338 opt
->output
= std_output
;
1340 if (opt
->pre_context
|| opt
->post_context
|| opt
->file_break
||
1342 /* Show hunk marks, except for the first file. */
1343 if (opt
->last_shown
)
1344 opt
->show_hunk_mark
= 1;
1346 * If we're using threads then we can't easily identify
1347 * the first file. Always put hunk marks in that case
1348 * and skip the very first one later in work_done().
1350 if (opt
->output
!= std_output
)
1351 opt
->show_hunk_mark
= 1;
1353 opt
->last_shown
= 0;
1355 switch (opt
->binary
) {
1356 case GREP_BINARY_DEFAULT
:
1357 if (grep_source_is_binary(gs
))
1358 binary_match_only
= 1;
1360 case GREP_BINARY_NOMATCH
:
1361 if (grep_source_is_binary(gs
))
1362 return 0; /* Assume unmatch */
1364 case GREP_BINARY_TEXT
:
1367 die("bug: unknown binary handling mode");
1370 memset(&xecfg
, 0, sizeof(xecfg
));
1373 try_lookahead
= should_lookahead(opt
);
1375 if (grep_source_load(gs
) < 0)
1385 * look_ahead() skips quickly to the line that possibly
1386 * has the next hit; don't call it if we need to do
1387 * something more than just skipping the current line
1388 * in response to an unmatch for the current line. E.g.
1389 * inside a post-context window, we will show the current
1390 * line as a context around the previous hit when it
1395 && (show_function
||
1396 lno
<= last_hit
+ opt
->post_context
))
1397 && look_ahead(opt
, &left
, &lno
, &bol
))
1399 eol
= end_of_line(bol
, &left
);
1403 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
1404 ctx
= GREP_CONTEXT_BODY
;
1406 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
1412 /* "grep -v -e foo -e bla" should list lines
1413 * that do not have either, so inversion should
1418 if (opt
->unmatch_name_only
) {
1425 if (opt
->status_only
)
1427 if (opt
->name_only
) {
1428 show_name(opt
, gs
->name
);
1433 if (binary_match_only
) {
1434 opt
->output(opt
, "Binary file ", 12);
1435 output_color(opt
, gs
->name
, strlen(gs
->name
),
1436 opt
->color_filename
);
1437 opt
->output(opt
, " matches\n", 9);
1440 /* Hit at this line. If we haven't shown the
1441 * pre-context lines, we would need to show them.
1443 if (opt
->pre_context
|| opt
->funcbody
)
1444 show_pre_context(opt
, gs
, bol
, eol
, lno
);
1445 else if (opt
->funcname
)
1446 show_funcname_line(opt
, gs
, bol
, lno
);
1447 show_line(opt
, bol
, eol
, gs
->name
, lno
, ':');
1453 if (show_function
&& match_funcname(opt
, gs
, bol
, eol
))
1455 if (show_function
||
1456 (last_hit
&& lno
<= last_hit
+ opt
->post_context
)) {
1457 /* If the last hit is within the post context,
1458 * we need to show this line.
1460 show_line(opt
, bol
, eol
, gs
->name
, lno
, '-');
1474 if (opt
->status_only
)
1476 if (opt
->unmatch_name_only
) {
1477 /* We did not see any hit, so we want to show this */
1478 show_name(opt
, gs
->name
);
1482 xdiff_clear_find_func(&xecfg
);
1486 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1487 * which feels mostly useless but sometimes useful. Maybe
1488 * make it another option? For now suppress them.
1490 if (opt
->count
&& count
) {
1492 output_color(opt
, gs
->name
, strlen(gs
->name
), opt
->color_filename
);
1493 output_sep(opt
, ':');
1494 snprintf(buf
, sizeof(buf
), "%u\n", count
);
1495 opt
->output(opt
, buf
, strlen(buf
));
1501 static void clr_hit_marker(struct grep_expr
*x
)
1503 /* All-hit markers are meaningful only at the very top level
1508 if (x
->node
!= GREP_NODE_OR
)
1510 x
->u
.binary
.left
->hit
= 0;
1511 x
= x
->u
.binary
.right
;
1515 static int chk_hit_marker(struct grep_expr
*x
)
1517 /* Top level nodes have hit markers. See if they all are hits */
1519 if (x
->node
!= GREP_NODE_OR
)
1521 if (!x
->u
.binary
.left
->hit
)
1523 x
= x
->u
.binary
.right
;
1527 int grep_source(struct grep_opt
*opt
, struct grep_source
*gs
)
1530 * we do not have to do the two-pass grep when we do not check
1531 * buffer-wide "all-match".
1533 if (!opt
->all_match
)
1534 return grep_source_1(opt
, gs
, 0);
1536 /* Otherwise the toplevel "or" terms hit a bit differently.
1537 * We first clear hit markers from them.
1539 clr_hit_marker(opt
->pattern_expression
);
1540 grep_source_1(opt
, gs
, 1);
1542 if (!chk_hit_marker(opt
->pattern_expression
))
1545 return grep_source_1(opt
, gs
, 0);
1548 int grep_buffer(struct grep_opt
*opt
, char *buf
, unsigned long size
)
1550 struct grep_source gs
;
1553 grep_source_init(&gs
, GREP_SOURCE_BUF
, NULL
, NULL
, NULL
);
1557 r
= grep_source(opt
, &gs
);
1559 grep_source_clear(&gs
);
1563 void grep_source_init(struct grep_source
*gs
, enum grep_source_type type
,
1564 const char *name
, const char *path
,
1565 const void *identifier
)
1568 gs
->name
= name
? xstrdup(name
) : NULL
;
1569 gs
->path
= path
? xstrdup(path
) : NULL
;
1575 case GREP_SOURCE_FILE
:
1576 gs
->identifier
= xstrdup(identifier
);
1578 case GREP_SOURCE_SHA1
:
1579 gs
->identifier
= xmalloc(20);
1580 memcpy(gs
->identifier
, identifier
, 20);
1582 case GREP_SOURCE_BUF
:
1583 gs
->identifier
= NULL
;
1587 void grep_source_clear(struct grep_source
*gs
)
1593 free(gs
->identifier
);
1594 gs
->identifier
= NULL
;
1595 grep_source_clear_data(gs
);
1598 void grep_source_clear_data(struct grep_source
*gs
)
1601 case GREP_SOURCE_FILE
:
1602 case GREP_SOURCE_SHA1
:
1607 case GREP_SOURCE_BUF
:
1608 /* leave user-provided buf intact */
1613 static int grep_source_load_sha1(struct grep_source
*gs
)
1615 enum object_type type
;
1618 gs
->buf
= read_sha1_file(gs
->identifier
, &type
, &gs
->size
);
1622 return error(_("'%s': unable to read %s"),
1624 sha1_to_hex(gs
->identifier
));
1628 static int grep_source_load_file(struct grep_source
*gs
)
1630 const char *filename
= gs
->identifier
;
1636 if (lstat(filename
, &st
) < 0) {
1638 if (errno
!= ENOENT
)
1639 error(_("'%s': %s"), filename
, strerror(errno
));
1642 if (!S_ISREG(st
.st_mode
))
1644 size
= xsize_t(st
.st_size
);
1645 i
= open(filename
, O_RDONLY
);
1648 data
= xmalloc(size
+ 1);
1649 if (st
.st_size
!= read_in_full(i
, data
, size
)) {
1650 error(_("'%s': short read %s"), filename
, strerror(errno
));
1663 static int grep_source_load(struct grep_source
*gs
)
1669 case GREP_SOURCE_FILE
:
1670 return grep_source_load_file(gs
);
1671 case GREP_SOURCE_SHA1
:
1672 return grep_source_load_sha1(gs
);
1673 case GREP_SOURCE_BUF
:
1674 return gs
->buf
? 0 : -1;
1676 die("BUG: invalid grep_source type");
1679 void grep_source_load_driver(struct grep_source
*gs
)
1686 gs
->driver
= userdiff_find_by_path(gs
->path
);
1688 gs
->driver
= userdiff_find_by_name("default");
1692 static int grep_source_is_binary(struct grep_source
*gs
)
1694 grep_source_load_driver(gs
);
1695 if (gs
->driver
->binary
!= -1)
1696 return gs
->driver
->binary
;
1698 if (!grep_source_load(gs
))
1699 return buffer_is_binary(gs
->buf
, gs
->size
);