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
;
20 memset(opt
, 0, sizeof(*opt
));
23 opt
->regflags
= REG_NEWLINE
;
25 opt
->pattern_type_option
= GREP_PATTERN_TYPE_UNSPECIFIED
;
26 opt
->extended_regexp_option
= 0;
27 strcpy(opt
->color_context
, "");
28 strcpy(opt
->color_filename
, "");
29 strcpy(opt
->color_function
, "");
30 strcpy(opt
->color_lineno
, "");
31 strcpy(opt
->color_match
, GIT_COLOR_BOLD_RED
);
32 strcpy(opt
->color_selected
, "");
33 strcpy(opt
->color_sep
, GIT_COLOR_CYAN
);
37 static int parse_pattern_type_arg(const char *opt
, const char *arg
)
39 if (!strcmp(arg
, "default"))
40 return GREP_PATTERN_TYPE_UNSPECIFIED
;
41 else if (!strcmp(arg
, "basic"))
42 return GREP_PATTERN_TYPE_BRE
;
43 else if (!strcmp(arg
, "extended"))
44 return GREP_PATTERN_TYPE_ERE
;
45 else if (!strcmp(arg
, "fixed"))
46 return GREP_PATTERN_TYPE_FIXED
;
47 else if (!strcmp(arg
, "perl"))
48 return GREP_PATTERN_TYPE_PCRE
;
49 die("bad %s argument: %s", opt
, arg
);
53 * Read the configuration file once and store it in
54 * the grep_defaults template.
56 int grep_config(const char *var
, const char *value
, void *cb
)
58 struct grep_opt
*opt
= &grep_defaults
;
61 if (userdiff_config(var
, value
) < 0)
64 if (!strcmp(var
, "grep.extendedregexp")) {
65 if (git_config_bool(var
, value
))
66 opt
->extended_regexp_option
= 1;
68 opt
->extended_regexp_option
= 0;
72 if (!strcmp(var
, "grep.patterntype")) {
73 opt
->pattern_type_option
= parse_pattern_type_arg(var
, value
);
77 if (!strcmp(var
, "grep.linenumber")) {
78 opt
->linenum
= git_config_bool(var
, value
);
82 if (!strcmp(var
, "color.grep"))
83 opt
->color
= git_config_colorbool(var
, value
);
84 else if (!strcmp(var
, "color.grep.context"))
85 color
= opt
->color_context
;
86 else if (!strcmp(var
, "color.grep.filename"))
87 color
= opt
->color_filename
;
88 else if (!strcmp(var
, "color.grep.function"))
89 color
= opt
->color_function
;
90 else if (!strcmp(var
, "color.grep.linenumber"))
91 color
= opt
->color_lineno
;
92 else if (!strcmp(var
, "color.grep.match"))
93 color
= opt
->color_match
;
94 else if (!strcmp(var
, "color.grep.selected"))
95 color
= opt
->color_selected
;
96 else if (!strcmp(var
, "color.grep.separator"))
97 color
= opt
->color_sep
;
101 return config_error_nonbool(var
);
102 color_parse(value
, var
, color
);
108 * Initialize one instance of grep_opt and copy the
109 * default values from the template we read the configuration
110 * information in an earlier call to git_config(grep_config).
112 void grep_init(struct grep_opt
*opt
, const char *prefix
)
114 struct grep_opt
*def
= &grep_defaults
;
116 memset(opt
, 0, sizeof(*opt
));
117 opt
->prefix
= prefix
;
118 opt
->prefix_length
= (prefix
&& *prefix
) ? strlen(prefix
) : 0;
119 opt
->pattern_tail
= &opt
->pattern_list
;
120 opt
->header_tail
= &opt
->header_list
;
122 opt
->color
= def
->color
;
123 opt
->extended_regexp_option
= def
->extended_regexp_option
;
124 opt
->pattern_type_option
= def
->pattern_type_option
;
125 opt
->linenum
= def
->linenum
;
126 opt
->max_depth
= def
->max_depth
;
127 opt
->pathname
= def
->pathname
;
128 opt
->regflags
= def
->regflags
;
129 opt
->relative
= def
->relative
;
131 strcpy(opt
->color_context
, def
->color_context
);
132 strcpy(opt
->color_filename
, def
->color_filename
);
133 strcpy(opt
->color_function
, def
->color_function
);
134 strcpy(opt
->color_lineno
, def
->color_lineno
);
135 strcpy(opt
->color_match
, def
->color_match
);
136 strcpy(opt
->color_selected
, def
->color_selected
);
137 strcpy(opt
->color_sep
, def
->color_sep
);
140 void grep_commit_pattern_type(enum grep_pattern_type pattern_type
, struct grep_opt
*opt
)
142 if (pattern_type
!= GREP_PATTERN_TYPE_UNSPECIFIED
)
143 grep_set_pattern_type_option(pattern_type
, opt
);
144 else if (opt
->pattern_type_option
!= GREP_PATTERN_TYPE_UNSPECIFIED
)
145 grep_set_pattern_type_option(opt
->pattern_type_option
, opt
);
146 else if (opt
->extended_regexp_option
)
147 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE
, opt
);
150 void grep_set_pattern_type_option(enum grep_pattern_type pattern_type
, struct grep_opt
*opt
)
152 switch (pattern_type
) {
153 case GREP_PATTERN_TYPE_UNSPECIFIED
:
156 case GREP_PATTERN_TYPE_BRE
:
159 opt
->regflags
&= ~REG_EXTENDED
;
162 case GREP_PATTERN_TYPE_ERE
:
165 opt
->regflags
|= REG_EXTENDED
;
168 case GREP_PATTERN_TYPE_FIXED
:
171 opt
->regflags
&= ~REG_EXTENDED
;
174 case GREP_PATTERN_TYPE_PCRE
:
177 opt
->regflags
&= ~REG_EXTENDED
;
182 static struct grep_pat
*create_grep_pat(const char *pat
, size_t patlen
,
183 const char *origin
, int no
,
184 enum grep_pat_token t
,
185 enum grep_header_field field
)
187 struct grep_pat
*p
= xcalloc(1, sizeof(*p
));
188 p
->pattern
= xmemdupz(pat
, patlen
);
189 p
->patternlen
= patlen
;
197 static void do_append_grep_pat(struct grep_pat
***tail
, struct grep_pat
*p
)
204 case GREP_PATTERN
: /* atom */
205 case GREP_PATTERN_HEAD
:
206 case GREP_PATTERN_BODY
:
208 struct grep_pat
*new_pat
;
210 char *cp
= p
->pattern
+ p
->patternlen
, *nl
= NULL
;
211 while (++len
<= p
->patternlen
) {
212 if (*(--cp
) == '\n') {
219 new_pat
= create_grep_pat(nl
+ 1, len
- 1, p
->origin
,
220 p
->no
, p
->token
, p
->field
);
221 new_pat
->next
= p
->next
;
223 *tail
= &new_pat
->next
;
226 p
->patternlen
-= len
;
234 void append_header_grep_pattern(struct grep_opt
*opt
,
235 enum grep_header_field field
, const char *pat
)
237 struct grep_pat
*p
= create_grep_pat(pat
, strlen(pat
), "header", 0,
238 GREP_PATTERN_HEAD
, field
);
239 if (field
== GREP_HEADER_REFLOG
)
240 opt
->use_reflog_filter
= 1;
241 do_append_grep_pat(&opt
->header_tail
, p
);
244 void append_grep_pattern(struct grep_opt
*opt
, const char *pat
,
245 const char *origin
, int no
, enum grep_pat_token t
)
247 append_grep_pat(opt
, pat
, strlen(pat
), origin
, no
, t
);
250 void append_grep_pat(struct grep_opt
*opt
, const char *pat
, size_t patlen
,
251 const char *origin
, int no
, enum grep_pat_token t
)
253 struct grep_pat
*p
= create_grep_pat(pat
, patlen
, origin
, no
, t
, 0);
254 do_append_grep_pat(&opt
->pattern_tail
, p
);
257 struct grep_opt
*grep_opt_dup(const struct grep_opt
*opt
)
259 struct grep_pat
*pat
;
260 struct grep_opt
*ret
= xmalloc(sizeof(struct grep_opt
));
263 ret
->pattern_list
= NULL
;
264 ret
->pattern_tail
= &ret
->pattern_list
;
266 for(pat
= opt
->pattern_list
; pat
!= NULL
; pat
= pat
->next
)
268 if(pat
->token
== GREP_PATTERN_HEAD
)
269 append_header_grep_pattern(ret
, pat
->field
,
272 append_grep_pat(ret
, pat
->pattern
, pat
->patternlen
,
273 pat
->origin
, pat
->no
, pat
->token
);
279 static NORETURN
void compile_regexp_failed(const struct grep_pat
*p
,
285 sprintf(where
, "In '%s' at %d, ", p
->origin
, p
->no
);
287 sprintf(where
, "%s, ", p
->origin
);
291 die("%s'%s': %s", where
, p
->pattern
, error
);
295 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
299 int options
= PCRE_MULTILINE
;
301 if (opt
->ignore_case
)
302 options
|= PCRE_CASELESS
;
304 p
->pcre_regexp
= pcre_compile(p
->pattern
, options
, &error
, &erroffset
,
307 compile_regexp_failed(p
, error
);
309 p
->pcre_extra_info
= pcre_study(p
->pcre_regexp
, 0, &error
);
310 if (!p
->pcre_extra_info
&& error
)
314 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
315 regmatch_t
*match
, int eflags
)
317 int ovector
[30], ret
, flags
= 0;
319 if (eflags
& REG_NOTBOL
)
320 flags
|= PCRE_NOTBOL
;
322 ret
= pcre_exec(p
->pcre_regexp
, p
->pcre_extra_info
, line
, eol
- line
,
323 0, flags
, ovector
, ARRAY_SIZE(ovector
));
324 if (ret
< 0 && ret
!= PCRE_ERROR_NOMATCH
)
325 die("pcre_exec failed with error code %d", ret
);
328 match
->rm_so
= ovector
[0];
329 match
->rm_eo
= ovector
[1];
335 static void free_pcre_regexp(struct grep_pat
*p
)
337 pcre_free(p
->pcre_regexp
);
338 pcre_free(p
->pcre_extra_info
);
340 #else /* !USE_LIBPCRE */
341 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
343 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
346 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
347 regmatch_t
*match
, int eflags
)
352 static void free_pcre_regexp(struct grep_pat
*p
)
355 #endif /* !USE_LIBPCRE */
357 static int is_fixed(const char *s
, size_t len
)
361 /* regcomp cannot accept patterns with NULs so we
362 * consider any pattern containing a NUL fixed.
364 if (memchr(s
, 0, len
))
367 for (i
= 0; i
< len
; i
++) {
368 if (is_regex_special(s
[i
]))
375 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
379 p
->word_regexp
= opt
->word_regexp
;
380 p
->ignore_case
= opt
->ignore_case
;
382 if (opt
->fixed
|| is_fixed(p
->pattern
, p
->patternlen
))
388 if (opt
->regflags
& REG_ICASE
|| p
->ignore_case
)
389 p
->kws
= kwsalloc(tolower_trans_tbl
);
391 p
->kws
= kwsalloc(NULL
);
392 kwsincr(p
->kws
, p
->pattern
, p
->patternlen
);
398 compile_pcre_regexp(p
, opt
);
402 err
= regcomp(&p
->regexp
, p
->pattern
, opt
->regflags
);
405 regerror(err
, &p
->regexp
, errbuf
, 1024);
407 compile_regexp_failed(p
, errbuf
);
411 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
412 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
421 case GREP_PATTERN
: /* atom */
422 case GREP_PATTERN_HEAD
:
423 case GREP_PATTERN_BODY
:
424 x
= xcalloc(1, sizeof (struct grep_expr
));
425 x
->node
= GREP_NODE_ATOM
;
429 case GREP_OPEN_PAREN
:
431 x
= compile_pattern_or(list
);
432 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
433 die("unmatched parenthesis");
434 *list
= (*list
)->next
;
441 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
452 die("--not not followed by pattern expression");
454 x
= xcalloc(1, sizeof (struct grep_expr
));
455 x
->node
= GREP_NODE_NOT
;
456 x
->u
.unary
= compile_pattern_not(list
);
458 die("--not followed by non pattern expression");
461 return compile_pattern_atom(list
);
465 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
468 struct grep_expr
*x
, *y
, *z
;
470 x
= compile_pattern_not(list
);
472 if (p
&& p
->token
== GREP_AND
) {
474 die("--and not followed by pattern expression");
476 y
= compile_pattern_and(list
);
478 die("--and not followed by pattern expression");
479 z
= xcalloc(1, sizeof (struct grep_expr
));
480 z
->node
= GREP_NODE_AND
;
481 z
->u
.binary
.left
= x
;
482 z
->u
.binary
.right
= y
;
488 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
491 struct grep_expr
*x
, *y
, *z
;
493 x
= compile_pattern_and(list
);
495 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
496 y
= compile_pattern_or(list
);
498 die("not a pattern expression %s", p
->pattern
);
499 z
= xcalloc(1, sizeof (struct grep_expr
));
500 z
->node
= GREP_NODE_OR
;
501 z
->u
.binary
.left
= x
;
502 z
->u
.binary
.right
= y
;
508 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
510 return compile_pattern_or(list
);
513 static void indent(int in
)
519 static void dump_grep_pat(struct grep_pat
*p
)
522 case GREP_AND
: fprintf(stderr
, "*and*"); break;
523 case GREP_OPEN_PAREN
: fprintf(stderr
, "*(*"); break;
524 case GREP_CLOSE_PAREN
: fprintf(stderr
, "*)*"); break;
525 case GREP_NOT
: fprintf(stderr
, "*not*"); break;
526 case GREP_OR
: fprintf(stderr
, "*or*"); break;
528 case GREP_PATTERN
: fprintf(stderr
, "pattern"); break;
529 case GREP_PATTERN_HEAD
: fprintf(stderr
, "pattern_head"); break;
530 case GREP_PATTERN_BODY
: fprintf(stderr
, "pattern_body"); break;
535 case GREP_PATTERN_HEAD
:
536 fprintf(stderr
, "<head %d>", p
->field
); break;
537 case GREP_PATTERN_BODY
:
538 fprintf(stderr
, "<body>"); break;
542 case GREP_PATTERN_HEAD
:
543 case GREP_PATTERN_BODY
:
545 fprintf(stderr
, "%.*s", (int)p
->patternlen
, p
->pattern
);
551 static void dump_grep_expression_1(struct grep_expr
*x
, int in
)
556 fprintf(stderr
, "true\n");
559 dump_grep_pat(x
->u
.atom
);
562 fprintf(stderr
, "(not\n");
563 dump_grep_expression_1(x
->u
.unary
, in
+1);
565 fprintf(stderr
, ")\n");
568 fprintf(stderr
, "(and\n");
569 dump_grep_expression_1(x
->u
.binary
.left
, in
+1);
570 dump_grep_expression_1(x
->u
.binary
.right
, in
+1);
572 fprintf(stderr
, ")\n");
575 fprintf(stderr
, "(or\n");
576 dump_grep_expression_1(x
->u
.binary
.left
, in
+1);
577 dump_grep_expression_1(x
->u
.binary
.right
, in
+1);
579 fprintf(stderr
, ")\n");
584 static void dump_grep_expression(struct grep_opt
*opt
)
586 struct grep_expr
*x
= opt
->pattern_expression
;
589 fprintf(stderr
, "[all-match]\n");
590 dump_grep_expression_1(x
, 0);
594 static struct grep_expr
*grep_true_expr(void)
596 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
597 z
->node
= GREP_NODE_TRUE
;
601 static struct grep_expr
*grep_or_expr(struct grep_expr
*left
, struct grep_expr
*right
)
603 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
604 z
->node
= GREP_NODE_OR
;
605 z
->u
.binary
.left
= left
;
606 z
->u
.binary
.right
= right
;
610 static struct grep_expr
*prep_header_patterns(struct grep_opt
*opt
)
613 struct grep_expr
*header_expr
;
614 struct grep_expr
*(header_group
[GREP_HEADER_FIELD_MAX
]);
615 enum grep_header_field fld
;
617 if (!opt
->header_list
)
620 for (p
= opt
->header_list
; p
; p
= p
->next
) {
621 if (p
->token
!= GREP_PATTERN_HEAD
)
622 die("bug: a non-header pattern in grep header list.");
623 if (p
->field
< 0 || GREP_HEADER_FIELD_MAX
<= p
->field
)
624 die("bug: unknown header field %d", p
->field
);
625 compile_regexp(p
, opt
);
628 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++)
629 header_group
[fld
] = NULL
;
631 for (p
= opt
->header_list
; p
; p
= p
->next
) {
633 struct grep_pat
*pp
= p
;
635 h
= compile_pattern_atom(&pp
);
636 if (!h
|| pp
!= p
->next
)
637 die("bug: malformed header expr");
638 if (!header_group
[p
->field
]) {
639 header_group
[p
->field
] = h
;
642 header_group
[p
->field
] = grep_or_expr(h
, header_group
[p
->field
]);
647 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++) {
648 if (!header_group
[fld
])
651 header_expr
= grep_true_expr();
652 header_expr
= grep_or_expr(header_group
[fld
], header_expr
);
657 static struct grep_expr
*grep_splice_or(struct grep_expr
*x
, struct grep_expr
*y
)
659 struct grep_expr
*z
= x
;
662 assert(x
->node
== GREP_NODE_OR
);
663 if (x
->u
.binary
.right
&&
664 x
->u
.binary
.right
->node
== GREP_NODE_TRUE
) {
665 x
->u
.binary
.right
= y
;
668 x
= x
->u
.binary
.right
;
673 static void compile_grep_patterns_real(struct grep_opt
*opt
)
676 struct grep_expr
*header_expr
= prep_header_patterns(opt
);
678 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
680 case GREP_PATTERN
: /* atom */
681 case GREP_PATTERN_HEAD
:
682 case GREP_PATTERN_BODY
:
683 compile_regexp(p
, opt
);
691 if (opt
->all_match
|| header_expr
)
693 else if (!opt
->extended
&& !opt
->debug
)
696 p
= opt
->pattern_list
;
698 opt
->pattern_expression
= compile_pattern_expr(&p
);
700 die("incomplete pattern expression: %s", p
->pattern
);
705 if (!opt
->pattern_expression
)
706 opt
->pattern_expression
= header_expr
;
707 else if (opt
->all_match
)
708 opt
->pattern_expression
= grep_splice_or(header_expr
,
709 opt
->pattern_expression
);
711 opt
->pattern_expression
= grep_or_expr(opt
->pattern_expression
,
716 void compile_grep_patterns(struct grep_opt
*opt
)
718 compile_grep_patterns_real(opt
);
720 dump_grep_expression(opt
);
723 static void free_pattern_expr(struct grep_expr
*x
)
730 free_pattern_expr(x
->u
.unary
);
734 free_pattern_expr(x
->u
.binary
.left
);
735 free_pattern_expr(x
->u
.binary
.right
);
741 void free_grep_patterns(struct grep_opt
*opt
)
743 struct grep_pat
*p
, *n
;
745 for (p
= opt
->pattern_list
; p
; p
= n
) {
748 case GREP_PATTERN
: /* atom */
749 case GREP_PATTERN_HEAD
:
750 case GREP_PATTERN_BODY
:
753 else if (p
->pcre_regexp
)
767 free_pattern_expr(opt
->pattern_expression
);
770 static char *end_of_line(char *cp
, unsigned long *left
)
772 unsigned long l
= *left
;
773 while (l
&& *cp
!= '\n') {
781 static int word_char(char ch
)
783 return isalnum(ch
) || ch
== '_';
786 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
789 if (want_color(opt
->color
) && color
&& color
[0]) {
790 opt
->output(opt
, color
, strlen(color
));
791 opt
->output(opt
, data
, size
);
792 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
794 opt
->output(opt
, data
, size
);
797 static void output_sep(struct grep_opt
*opt
, char sign
)
799 if (opt
->null_following_name
)
800 opt
->output(opt
, "\0", 1);
802 output_color(opt
, &sign
, 1, opt
->color_sep
);
805 static void show_name(struct grep_opt
*opt
, const char *name
)
807 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
808 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
811 static int fixmatch(struct grep_pat
*p
, char *line
, char *eol
,
814 struct kwsmatch kwsm
;
815 size_t offset
= kwsexec(p
->kws
, line
, eol
- line
, &kwsm
);
817 match
->rm_so
= match
->rm_eo
= -1;
820 match
->rm_so
= offset
;
821 match
->rm_eo
= match
->rm_so
+ kwsm
.size
[0];
826 static int regmatch(const regex_t
*preg
, char *line
, char *eol
,
827 regmatch_t
*match
, int eflags
)
831 match
->rm_eo
= eol
- line
;
832 eflags
|= REG_STARTEND
;
834 return regexec(preg
, line
, 1, match
, eflags
);
837 static int patmatch(struct grep_pat
*p
, char *line
, char *eol
,
838 regmatch_t
*match
, int eflags
)
843 hit
= !fixmatch(p
, line
, eol
, match
);
844 else if (p
->pcre_regexp
)
845 hit
= !pcrematch(p
, line
, eol
, match
, eflags
);
847 hit
= !regmatch(&p
->regexp
, line
, eol
, match
, eflags
);
852 static int strip_timestamp(char *bol
, char **eol_p
)
857 while (bol
< --eol
) {
873 { "committer ", 10 },
877 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
878 enum grep_context ctx
,
879 regmatch_t
*pmatch
, int eflags
)
883 const char *start
= bol
;
885 if ((p
->token
!= GREP_PATTERN
) &&
886 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
889 if (p
->token
== GREP_PATTERN_HEAD
) {
892 assert(p
->field
< ARRAY_SIZE(header_field
));
893 field
= header_field
[p
->field
].field
;
894 len
= header_field
[p
->field
].len
;
895 if (strncmp(bol
, field
, len
))
899 case GREP_HEADER_AUTHOR
:
900 case GREP_HEADER_COMMITTER
:
901 saved_ch
= strip_timestamp(bol
, &eol
);
909 hit
= patmatch(p
, bol
, eol
, pmatch
, eflags
);
911 if (hit
&& p
->word_regexp
) {
912 if ((pmatch
[0].rm_so
< 0) ||
913 (eol
- bol
) < pmatch
[0].rm_so
||
914 (pmatch
[0].rm_eo
< 0) ||
915 (eol
- bol
) < pmatch
[0].rm_eo
)
916 die("regexp returned nonsense");
918 /* Match beginning must be either beginning of the
919 * line, or at word boundary (i.e. the last char must
920 * not be a word char). Similarly, match end must be
921 * either end of the line, or at word boundary
922 * (i.e. the next char must not be a word char).
924 if ( ((pmatch
[0].rm_so
== 0) ||
925 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
926 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
927 !word_char(bol
[pmatch
[0].rm_eo
])) )
932 /* Words consist of at least one character. */
933 if (pmatch
->rm_so
== pmatch
->rm_eo
)
936 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
937 /* There could be more than one match on the
938 * line, and the first match might not be
939 * strict word match. But later ones could be!
940 * Forward to the next possible start, i.e. the
941 * next position following a non-word char.
943 bol
= pmatch
[0].rm_so
+ bol
+ 1;
944 while (word_char(bol
[-1]) && bol
< eol
)
946 eflags
|= REG_NOTBOL
;
951 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
954 pmatch
[0].rm_so
+= bol
- start
;
955 pmatch
[0].rm_eo
+= bol
- start
;
960 static int match_expr_eval(struct grep_expr
*x
, char *bol
, char *eol
,
961 enum grep_context ctx
, int collect_hits
)
967 die("Not a valid grep expression");
973 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
, &match
, 0);
976 h
= !match_expr_eval(x
->u
.unary
, bol
, eol
, ctx
, 0);
979 if (!match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0))
981 h
= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
985 return (match_expr_eval(x
->u
.binary
.left
,
987 match_expr_eval(x
->u
.binary
.right
,
989 h
= match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
990 x
->u
.binary
.left
->hit
|= h
;
991 h
|= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
994 die("Unexpected node type (internal error) %d", x
->node
);
1001 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
1002 enum grep_context ctx
, int collect_hits
)
1004 struct grep_expr
*x
= opt
->pattern_expression
;
1005 return match_expr_eval(x
, bol
, eol
, ctx
, collect_hits
);
1008 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
1009 enum grep_context ctx
, int collect_hits
)
1015 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
1017 /* we do not call with collect_hits without being extended */
1018 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1019 if (match_one_pattern(p
, bol
, eol
, ctx
, &match
, 0))
1025 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
1026 enum grep_context ctx
,
1027 regmatch_t
*pmatch
, int eflags
)
1031 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
1033 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
1035 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
1036 if (match
.rm_so
> pmatch
->rm_so
)
1038 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
1041 pmatch
->rm_so
= match
.rm_so
;
1042 pmatch
->rm_eo
= match
.rm_eo
;
1046 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
1047 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
1052 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
1054 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1056 case GREP_PATTERN
: /* atom */
1057 case GREP_PATTERN_HEAD
:
1058 case GREP_PATTERN_BODY
:
1059 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
1070 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
1071 const char *name
, unsigned lno
, char sign
)
1073 int rest
= eol
- bol
;
1074 char *line_color
= NULL
;
1076 if (opt
->file_break
&& opt
->last_shown
== 0) {
1077 if (opt
->show_hunk_mark
)
1078 opt
->output(opt
, "\n", 1);
1079 } else if (opt
->pre_context
|| opt
->post_context
|| opt
->funcbody
) {
1080 if (opt
->last_shown
== 0) {
1081 if (opt
->show_hunk_mark
) {
1082 output_color(opt
, "--", 2, opt
->color_sep
);
1083 opt
->output(opt
, "\n", 1);
1085 } else if (lno
> opt
->last_shown
+ 1) {
1086 output_color(opt
, "--", 2, opt
->color_sep
);
1087 opt
->output(opt
, "\n", 1);
1090 if (opt
->heading
&& opt
->last_shown
== 0) {
1091 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
1092 opt
->output(opt
, "\n", 1);
1094 opt
->last_shown
= lno
;
1096 if (!opt
->heading
&& opt
->pathname
) {
1097 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
1098 output_sep(opt
, sign
);
1102 snprintf(buf
, sizeof(buf
), "%d", lno
);
1103 output_color(opt
, buf
, strlen(buf
), opt
->color_lineno
);
1104 output_sep(opt
, sign
);
1108 enum grep_context ctx
= GREP_CONTEXT_BODY
;
1113 line_color
= opt
->color_selected
;
1114 else if (sign
== '-')
1115 line_color
= opt
->color_context
;
1116 else if (sign
== '=')
1117 line_color
= opt
->color_function
;
1119 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
1120 if (match
.rm_so
== match
.rm_eo
)
1123 output_color(opt
, bol
, match
.rm_so
, line_color
);
1124 output_color(opt
, bol
+ match
.rm_so
,
1125 match
.rm_eo
- match
.rm_so
,
1128 rest
-= match
.rm_eo
;
1129 eflags
= REG_NOTBOL
;
1133 output_color(opt
, bol
, rest
, line_color
);
1134 opt
->output(opt
, "\n", 1);
1141 * This lock protects access to the gitattributes machinery, which is
1144 pthread_mutex_t grep_attr_mutex
;
1146 static inline void grep_attr_lock(void)
1149 pthread_mutex_lock(&grep_attr_mutex
);
1152 static inline void grep_attr_unlock(void)
1155 pthread_mutex_unlock(&grep_attr_mutex
);
1159 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
1161 pthread_mutex_t grep_read_mutex
;
1164 #define grep_attr_lock()
1165 #define grep_attr_unlock()
1168 static int match_funcname(struct grep_opt
*opt
, struct grep_source
*gs
, char *bol
, char *eol
)
1170 xdemitconf_t
*xecfg
= opt
->priv
;
1171 if (xecfg
&& !xecfg
->find_func
) {
1172 grep_source_load_driver(gs
);
1173 if (gs
->driver
->funcname
.pattern
) {
1174 const struct userdiff_funcname
*pe
= &gs
->driver
->funcname
;
1175 xdiff_set_find_func(xecfg
, pe
->pattern
, pe
->cflags
);
1177 xecfg
= opt
->priv
= NULL
;
1183 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
1184 xecfg
->find_func_priv
) >= 0;
1189 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
1194 static void show_funcname_line(struct grep_opt
*opt
, struct grep_source
*gs
,
1195 char *bol
, unsigned lno
)
1197 while (bol
> gs
->buf
) {
1200 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1204 if (lno
<= opt
->last_shown
)
1207 if (match_funcname(opt
, gs
, bol
, eol
)) {
1208 show_line(opt
, bol
, eol
, gs
->name
, lno
, '=');
1214 static void show_pre_context(struct grep_opt
*opt
, struct grep_source
*gs
,
1215 char *bol
, char *end
, unsigned lno
)
1217 unsigned cur
= lno
, from
= 1, funcname_lno
= 0;
1218 int funcname_needed
= !!opt
->funcname
;
1220 if (opt
->funcbody
&& !match_funcname(opt
, gs
, bol
, end
))
1221 funcname_needed
= 2;
1223 if (opt
->pre_context
< lno
)
1224 from
= lno
- opt
->pre_context
;
1225 if (from
<= opt
->last_shown
)
1226 from
= opt
->last_shown
+ 1;
1229 while (bol
> gs
->buf
&&
1230 cur
> (funcname_needed
== 2 ? opt
->last_shown
+ 1 : from
)) {
1233 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1236 if (funcname_needed
&& match_funcname(opt
, gs
, bol
, eol
)) {
1238 funcname_needed
= 0;
1242 /* We need to look even further back to find a function signature. */
1243 if (opt
->funcname
&& funcname_needed
)
1244 show_funcname_line(opt
, gs
, bol
, cur
);
1248 char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
1250 while (*eol
!= '\n')
1252 show_line(opt
, bol
, eol
, gs
->name
, cur
, sign
);
1258 static int should_lookahead(struct grep_opt
*opt
)
1263 return 0; /* punt for too complex stuff */
1266 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1267 if (p
->token
!= GREP_PATTERN
)
1268 return 0; /* punt for "header only" and stuff */
1273 static int look_ahead(struct grep_opt
*opt
,
1274 unsigned long *left_p
,
1278 unsigned lno
= *lno_p
;
1281 char *sp
, *last_bol
;
1282 regoff_t earliest
= -1;
1284 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1288 hit
= patmatch(p
, bol
, bol
+ *left_p
, &m
, 0);
1289 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
1291 if (earliest
< 0 || m
.rm_so
< earliest
)
1296 *bol_p
= bol
+ *left_p
;
1300 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
1301 ; /* find the beginning of the line */
1304 for (sp
= bol
; sp
< last_bol
; sp
++) {
1308 *left_p
-= last_bol
- bol
;
1314 static void std_output(struct grep_opt
*opt
, const void *buf
, size_t size
)
1316 fwrite(buf
, size
, 1, stdout
);
1319 static int grep_source_1(struct grep_opt
*opt
, struct grep_source
*gs
, int collect_hits
)
1324 unsigned last_hit
= 0;
1325 int binary_match_only
= 0;
1327 int try_lookahead
= 0;
1328 int show_function
= 0;
1329 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
1333 opt
->output
= std_output
;
1335 if (opt
->pre_context
|| opt
->post_context
|| opt
->file_break
||
1337 /* Show hunk marks, except for the first file. */
1338 if (opt
->last_shown
)
1339 opt
->show_hunk_mark
= 1;
1341 * If we're using threads then we can't easily identify
1342 * the first file. Always put hunk marks in that case
1343 * and skip the very first one later in work_done().
1345 if (opt
->output
!= std_output
)
1346 opt
->show_hunk_mark
= 1;
1348 opt
->last_shown
= 0;
1350 switch (opt
->binary
) {
1351 case GREP_BINARY_DEFAULT
:
1352 if (grep_source_is_binary(gs
))
1353 binary_match_only
= 1;
1355 case GREP_BINARY_NOMATCH
:
1356 if (grep_source_is_binary(gs
))
1357 return 0; /* Assume unmatch */
1359 case GREP_BINARY_TEXT
:
1362 die("bug: unknown binary handling mode");
1365 memset(&xecfg
, 0, sizeof(xecfg
));
1368 try_lookahead
= should_lookahead(opt
);
1370 if (grep_source_load(gs
) < 0)
1380 * look_ahead() skips quickly to the line that possibly
1381 * has the next hit; don't call it if we need to do
1382 * something more than just skipping the current line
1383 * in response to an unmatch for the current line. E.g.
1384 * inside a post-context window, we will show the current
1385 * line as a context around the previous hit when it
1390 && (show_function
||
1391 lno
<= last_hit
+ opt
->post_context
))
1392 && look_ahead(opt
, &left
, &lno
, &bol
))
1394 eol
= end_of_line(bol
, &left
);
1398 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
1399 ctx
= GREP_CONTEXT_BODY
;
1401 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
1407 /* "grep -v -e foo -e bla" should list lines
1408 * that do not have either, so inversion should
1413 if (opt
->unmatch_name_only
) {
1420 if (opt
->status_only
)
1422 if (opt
->name_only
) {
1423 show_name(opt
, gs
->name
);
1428 if (binary_match_only
) {
1429 opt
->output(opt
, "Binary file ", 12);
1430 output_color(opt
, gs
->name
, strlen(gs
->name
),
1431 opt
->color_filename
);
1432 opt
->output(opt
, " matches\n", 9);
1435 /* Hit at this line. If we haven't shown the
1436 * pre-context lines, we would need to show them.
1438 if (opt
->pre_context
|| opt
->funcbody
)
1439 show_pre_context(opt
, gs
, bol
, eol
, lno
);
1440 else if (opt
->funcname
)
1441 show_funcname_line(opt
, gs
, bol
, lno
);
1442 show_line(opt
, bol
, eol
, gs
->name
, lno
, ':');
1448 if (show_function
&& match_funcname(opt
, gs
, bol
, eol
))
1450 if (show_function
||
1451 (last_hit
&& lno
<= last_hit
+ opt
->post_context
)) {
1452 /* If the last hit is within the post context,
1453 * we need to show this line.
1455 show_line(opt
, bol
, eol
, gs
->name
, lno
, '-');
1469 if (opt
->status_only
)
1471 if (opt
->unmatch_name_only
) {
1472 /* We did not see any hit, so we want to show this */
1473 show_name(opt
, gs
->name
);
1477 xdiff_clear_find_func(&xecfg
);
1481 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1482 * which feels mostly useless but sometimes useful. Maybe
1483 * make it another option? For now suppress them.
1485 if (opt
->count
&& count
) {
1487 output_color(opt
, gs
->name
, strlen(gs
->name
), opt
->color_filename
);
1488 output_sep(opt
, ':');
1489 snprintf(buf
, sizeof(buf
), "%u\n", count
);
1490 opt
->output(opt
, buf
, strlen(buf
));
1496 static void clr_hit_marker(struct grep_expr
*x
)
1498 /* All-hit markers are meaningful only at the very top level
1503 if (x
->node
!= GREP_NODE_OR
)
1505 x
->u
.binary
.left
->hit
= 0;
1506 x
= x
->u
.binary
.right
;
1510 static int chk_hit_marker(struct grep_expr
*x
)
1512 /* Top level nodes have hit markers. See if they all are hits */
1514 if (x
->node
!= GREP_NODE_OR
)
1516 if (!x
->u
.binary
.left
->hit
)
1518 x
= x
->u
.binary
.right
;
1522 int grep_source(struct grep_opt
*opt
, struct grep_source
*gs
)
1525 * we do not have to do the two-pass grep when we do not check
1526 * buffer-wide "all-match".
1528 if (!opt
->all_match
)
1529 return grep_source_1(opt
, gs
, 0);
1531 /* Otherwise the toplevel "or" terms hit a bit differently.
1532 * We first clear hit markers from them.
1534 clr_hit_marker(opt
->pattern_expression
);
1535 grep_source_1(opt
, gs
, 1);
1537 if (!chk_hit_marker(opt
->pattern_expression
))
1540 return grep_source_1(opt
, gs
, 0);
1543 int grep_buffer(struct grep_opt
*opt
, char *buf
, unsigned long size
)
1545 struct grep_source gs
;
1548 grep_source_init(&gs
, GREP_SOURCE_BUF
, NULL
, NULL
);
1552 r
= grep_source(opt
, &gs
);
1554 grep_source_clear(&gs
);
1558 void grep_source_init(struct grep_source
*gs
, enum grep_source_type type
,
1559 const char *name
, const void *identifier
)
1562 gs
->name
= name
? xstrdup(name
) : NULL
;
1568 case GREP_SOURCE_FILE
:
1569 gs
->identifier
= xstrdup(identifier
);
1571 case GREP_SOURCE_SHA1
:
1572 gs
->identifier
= xmalloc(20);
1573 memcpy(gs
->identifier
, identifier
, 20);
1575 case GREP_SOURCE_BUF
:
1576 gs
->identifier
= NULL
;
1580 void grep_source_clear(struct grep_source
*gs
)
1584 free(gs
->identifier
);
1585 gs
->identifier
= NULL
;
1586 grep_source_clear_data(gs
);
1589 void grep_source_clear_data(struct grep_source
*gs
)
1592 case GREP_SOURCE_FILE
:
1593 case GREP_SOURCE_SHA1
:
1598 case GREP_SOURCE_BUF
:
1599 /* leave user-provided buf intact */
1604 static int grep_source_load_sha1(struct grep_source
*gs
)
1606 enum object_type type
;
1609 gs
->buf
= read_sha1_file(gs
->identifier
, &type
, &gs
->size
);
1613 return error(_("'%s': unable to read %s"),
1615 sha1_to_hex(gs
->identifier
));
1619 static int grep_source_load_file(struct grep_source
*gs
)
1621 const char *filename
= gs
->identifier
;
1627 if (lstat(filename
, &st
) < 0) {
1629 if (errno
!= ENOENT
)
1630 error(_("'%s': %s"), filename
, strerror(errno
));
1633 if (!S_ISREG(st
.st_mode
))
1635 size
= xsize_t(st
.st_size
);
1636 i
= open(filename
, O_RDONLY
);
1639 data
= xmalloc(size
+ 1);
1640 if (st
.st_size
!= read_in_full(i
, data
, size
)) {
1641 error(_("'%s': short read %s"), filename
, strerror(errno
));
1654 static int grep_source_load(struct grep_source
*gs
)
1660 case GREP_SOURCE_FILE
:
1661 return grep_source_load_file(gs
);
1662 case GREP_SOURCE_SHA1
:
1663 return grep_source_load_sha1(gs
);
1664 case GREP_SOURCE_BUF
:
1665 return gs
->buf
? 0 : -1;
1667 die("BUG: invalid grep_source type");
1670 void grep_source_load_driver(struct grep_source
*gs
)
1676 gs
->driver
= userdiff_find_by_path(gs
->name
);
1678 gs
->driver
= userdiff_find_by_name("default");
1682 static int grep_source_is_binary(struct grep_source
*gs
)
1684 grep_source_load_driver(gs
);
1685 if (gs
->driver
->binary
!= -1)
1686 return gs
->driver
->binary
;
1688 if (!grep_source_load(gs
))
1689 return buffer_is_binary(gs
->buf
, gs
->size
);