4 #include "xdiff-interface.h"
6 static struct grep_pat
*create_grep_pat(const char *pat
, size_t patlen
,
7 const char *origin
, int no
,
9 enum grep_header_field field
)
11 struct grep_pat
*p
= xcalloc(1, sizeof(*p
));
12 p
->pattern
= xmemdupz(pat
, patlen
);
13 p
->patternlen
= patlen
;
21 static void do_append_grep_pat(struct grep_pat
***tail
, struct grep_pat
*p
)
28 case GREP_PATTERN
: /* atom */
29 case GREP_PATTERN_HEAD
:
30 case GREP_PATTERN_BODY
:
32 struct grep_pat
*new_pat
;
34 char *cp
= p
->pattern
+ p
->patternlen
, *nl
= NULL
;
35 while (++len
<= p
->patternlen
) {
36 if (*(--cp
) == '\n') {
43 new_pat
= create_grep_pat(nl
+ 1, len
- 1, p
->origin
,
44 p
->no
, p
->token
, p
->field
);
45 new_pat
->next
= p
->next
;
47 *tail
= &new_pat
->next
;
58 void append_header_grep_pattern(struct grep_opt
*opt
,
59 enum grep_header_field field
, const char *pat
)
61 struct grep_pat
*p
= create_grep_pat(pat
, strlen(pat
), "header", 0,
62 GREP_PATTERN_HEAD
, field
);
63 do_append_grep_pat(&opt
->header_tail
, p
);
66 void append_grep_pattern(struct grep_opt
*opt
, const char *pat
,
67 const char *origin
, int no
, enum grep_pat_token t
)
69 append_grep_pat(opt
, pat
, strlen(pat
), origin
, no
, t
);
72 void append_grep_pat(struct grep_opt
*opt
, const char *pat
, size_t patlen
,
73 const char *origin
, int no
, enum grep_pat_token t
)
75 struct grep_pat
*p
= create_grep_pat(pat
, patlen
, origin
, no
, t
, 0);
76 do_append_grep_pat(&opt
->pattern_tail
, p
);
79 struct grep_opt
*grep_opt_dup(const struct grep_opt
*opt
)
82 struct grep_opt
*ret
= xmalloc(sizeof(struct grep_opt
));
85 ret
->pattern_list
= NULL
;
86 ret
->pattern_tail
= &ret
->pattern_list
;
88 for(pat
= opt
->pattern_list
; pat
!= NULL
; pat
= pat
->next
)
90 if(pat
->token
== GREP_PATTERN_HEAD
)
91 append_header_grep_pattern(ret
, pat
->field
,
94 append_grep_pat(ret
, pat
->pattern
, pat
->patternlen
,
95 pat
->origin
, pat
->no
, pat
->token
);
101 static NORETURN
void compile_regexp_failed(const struct grep_pat
*p
,
107 sprintf(where
, "In '%s' at %d, ", p
->origin
, p
->no
);
109 sprintf(where
, "%s, ", p
->origin
);
113 die("%s'%s': %s", where
, p
->pattern
, error
);
117 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
121 int options
= PCRE_MULTILINE
;
123 if (opt
->ignore_case
)
124 options
|= PCRE_CASELESS
;
126 p
->pcre_regexp
= pcre_compile(p
->pattern
, options
, &error
, &erroffset
,
129 compile_regexp_failed(p
, error
);
131 p
->pcre_extra_info
= pcre_study(p
->pcre_regexp
, 0, &error
);
132 if (!p
->pcre_extra_info
&& error
)
136 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
137 regmatch_t
*match
, int eflags
)
139 int ovector
[30], ret
, flags
= 0;
141 if (eflags
& REG_NOTBOL
)
142 flags
|= PCRE_NOTBOL
;
144 ret
= pcre_exec(p
->pcre_regexp
, p
->pcre_extra_info
, line
, eol
- line
,
145 0, flags
, ovector
, ARRAY_SIZE(ovector
));
146 if (ret
< 0 && ret
!= PCRE_ERROR_NOMATCH
)
147 die("pcre_exec failed with error code %d", ret
);
150 match
->rm_so
= ovector
[0];
151 match
->rm_eo
= ovector
[1];
157 static void free_pcre_regexp(struct grep_pat
*p
)
159 pcre_free(p
->pcre_regexp
);
160 pcre_free(p
->pcre_extra_info
);
162 #else /* !USE_LIBPCRE */
163 static void compile_pcre_regexp(struct grep_pat
*p
, const struct grep_opt
*opt
)
165 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
168 static int pcrematch(struct grep_pat
*p
, const char *line
, const char *eol
,
169 regmatch_t
*match
, int eflags
)
174 static void free_pcre_regexp(struct grep_pat
*p
)
177 #endif /* !USE_LIBPCRE */
179 static int is_fixed(const char *s
, size_t len
)
183 /* regcomp cannot accept patterns with NULs so we
184 * consider any pattern containing a NUL fixed.
186 if (memchr(s
, 0, len
))
189 for (i
= 0; i
< len
; i
++) {
190 if (is_regex_special(s
[i
]))
197 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
201 p
->word_regexp
= opt
->word_regexp
;
202 p
->ignore_case
= opt
->ignore_case
;
204 if (opt
->fixed
|| is_fixed(p
->pattern
, p
->patternlen
))
210 if (opt
->regflags
& REG_ICASE
|| p
->ignore_case
)
211 p
->kws
= kwsalloc(tolower_trans_tbl
);
213 p
->kws
= kwsalloc(NULL
);
214 kwsincr(p
->kws
, p
->pattern
, p
->patternlen
);
220 compile_pcre_regexp(p
, opt
);
224 err
= regcomp(&p
->regexp
, p
->pattern
, opt
->regflags
);
227 regerror(err
, &p
->regexp
, errbuf
, 1024);
229 compile_regexp_failed(p
, errbuf
);
233 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
234 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
243 case GREP_PATTERN
: /* atom */
244 case GREP_PATTERN_HEAD
:
245 case GREP_PATTERN_BODY
:
246 x
= xcalloc(1, sizeof (struct grep_expr
));
247 x
->node
= GREP_NODE_ATOM
;
251 case GREP_OPEN_PAREN
:
253 x
= compile_pattern_or(list
);
254 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
255 die("unmatched parenthesis");
256 *list
= (*list
)->next
;
263 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
274 die("--not not followed by pattern expression");
276 x
= xcalloc(1, sizeof (struct grep_expr
));
277 x
->node
= GREP_NODE_NOT
;
278 x
->u
.unary
= compile_pattern_not(list
);
280 die("--not followed by non pattern expression");
283 return compile_pattern_atom(list
);
287 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
290 struct grep_expr
*x
, *y
, *z
;
292 x
= compile_pattern_not(list
);
294 if (p
&& p
->token
== GREP_AND
) {
296 die("--and not followed by pattern expression");
298 y
= compile_pattern_and(list
);
300 die("--and not followed by pattern expression");
301 z
= xcalloc(1, sizeof (struct grep_expr
));
302 z
->node
= GREP_NODE_AND
;
303 z
->u
.binary
.left
= x
;
304 z
->u
.binary
.right
= y
;
310 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
313 struct grep_expr
*x
, *y
, *z
;
315 x
= compile_pattern_and(list
);
317 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
318 y
= compile_pattern_or(list
);
320 die("not a pattern expression %s", p
->pattern
);
321 z
= xcalloc(1, sizeof (struct grep_expr
));
322 z
->node
= GREP_NODE_OR
;
323 z
->u
.binary
.left
= x
;
324 z
->u
.binary
.right
= y
;
330 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
332 return compile_pattern_or(list
);
335 static void indent(int in
)
341 static void dump_grep_pat(struct grep_pat
*p
)
344 case GREP_AND
: fprintf(stderr
, "*and*"); break;
345 case GREP_OPEN_PAREN
: fprintf(stderr
, "*(*"); break;
346 case GREP_CLOSE_PAREN
: fprintf(stderr
, "*)*"); break;
347 case GREP_NOT
: fprintf(stderr
, "*not*"); break;
348 case GREP_OR
: fprintf(stderr
, "*or*"); break;
350 case GREP_PATTERN
: fprintf(stderr
, "pattern"); break;
351 case GREP_PATTERN_HEAD
: fprintf(stderr
, "pattern_head"); break;
352 case GREP_PATTERN_BODY
: fprintf(stderr
, "pattern_body"); break;
357 case GREP_PATTERN_HEAD
:
358 fprintf(stderr
, "<head %d>", p
->field
); break;
359 case GREP_PATTERN_BODY
:
360 fprintf(stderr
, "<body>"); break;
364 case GREP_PATTERN_HEAD
:
365 case GREP_PATTERN_BODY
:
367 fprintf(stderr
, "%.*s", (int)p
->patternlen
, p
->pattern
);
373 static void dump_grep_expression_1(struct grep_expr
*x
, int in
)
378 fprintf(stderr
, "true\n");
381 dump_grep_pat(x
->u
.atom
);
384 fprintf(stderr
, "(not\n");
385 dump_grep_expression_1(x
->u
.unary
, in
+1);
387 fprintf(stderr
, ")\n");
390 fprintf(stderr
, "(and\n");
391 dump_grep_expression_1(x
->u
.binary
.left
, in
+1);
392 dump_grep_expression_1(x
->u
.binary
.right
, in
+1);
394 fprintf(stderr
, ")\n");
397 fprintf(stderr
, "(or\n");
398 dump_grep_expression_1(x
->u
.binary
.left
, in
+1);
399 dump_grep_expression_1(x
->u
.binary
.right
, in
+1);
401 fprintf(stderr
, ")\n");
406 void dump_grep_expression(struct grep_opt
*opt
)
408 struct grep_expr
*x
= opt
->pattern_expression
;
411 fprintf(stderr
, "[all-match]\n");
412 dump_grep_expression_1(x
, 0);
416 static struct grep_expr
*grep_true_expr(void)
418 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
419 z
->node
= GREP_NODE_TRUE
;
423 static struct grep_expr
*grep_or_expr(struct grep_expr
*left
, struct grep_expr
*right
)
425 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
426 z
->node
= GREP_NODE_OR
;
427 z
->u
.binary
.left
= left
;
428 z
->u
.binary
.right
= right
;
432 static struct grep_expr
*prep_header_patterns(struct grep_opt
*opt
)
435 struct grep_expr
*header_expr
;
436 struct grep_expr
*(header_group
[GREP_HEADER_FIELD_MAX
]);
437 enum grep_header_field fld
;
439 if (!opt
->header_list
)
442 for (p
= opt
->header_list
; p
; p
= p
->next
) {
443 if (p
->token
!= GREP_PATTERN_HEAD
)
444 die("bug: a non-header pattern in grep header list.");
445 if (p
->field
< 0 || GREP_HEADER_FIELD_MAX
<= p
->field
)
446 die("bug: unknown header field %d", p
->field
);
447 compile_regexp(p
, opt
);
450 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++)
451 header_group
[fld
] = NULL
;
453 for (p
= opt
->header_list
; p
; p
= p
->next
) {
455 struct grep_pat
*pp
= p
;
457 h
= compile_pattern_atom(&pp
);
458 if (!h
|| pp
!= p
->next
)
459 die("bug: malformed header expr");
460 if (!header_group
[p
->field
]) {
461 header_group
[p
->field
] = h
;
464 header_group
[p
->field
] = grep_or_expr(h
, header_group
[p
->field
]);
469 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++) {
470 if (!header_group
[fld
])
473 header_expr
= grep_true_expr();
474 header_expr
= grep_or_expr(header_group
[fld
], header_expr
);
479 static void compile_grep_patterns_real(struct grep_opt
*opt
)
482 struct grep_expr
*header_expr
= prep_header_patterns(opt
);
484 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
486 case GREP_PATTERN
: /* atom */
487 case GREP_PATTERN_HEAD
:
488 case GREP_PATTERN_BODY
:
489 compile_regexp(p
, opt
);
497 if (opt
->all_match
|| header_expr
)
499 else if (!opt
->extended
&& !opt
->debug
)
502 p
= opt
->pattern_list
;
504 opt
->pattern_expression
= compile_pattern_expr(&p
);
506 die("incomplete pattern expression: %s", p
->pattern
);
511 if (!opt
->pattern_expression
)
512 opt
->pattern_expression
= header_expr
;
514 opt
->pattern_expression
= grep_or_expr(opt
->pattern_expression
,
519 void compile_grep_patterns(struct grep_opt
*opt
)
521 compile_grep_patterns_real(opt
);
523 dump_grep_expression(opt
);
526 static void free_pattern_expr(struct grep_expr
*x
)
533 free_pattern_expr(x
->u
.unary
);
537 free_pattern_expr(x
->u
.binary
.left
);
538 free_pattern_expr(x
->u
.binary
.right
);
544 void free_grep_patterns(struct grep_opt
*opt
)
546 struct grep_pat
*p
, *n
;
548 for (p
= opt
->pattern_list
; p
; p
= n
) {
551 case GREP_PATTERN
: /* atom */
552 case GREP_PATTERN_HEAD
:
553 case GREP_PATTERN_BODY
:
556 else if (p
->pcre_regexp
)
570 free_pattern_expr(opt
->pattern_expression
);
573 static char *end_of_line(char *cp
, unsigned long *left
)
575 unsigned long l
= *left
;
576 while (l
&& *cp
!= '\n') {
584 static int word_char(char ch
)
586 return isalnum(ch
) || ch
== '_';
589 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
592 if (want_color(opt
->color
) && color
&& color
[0]) {
593 opt
->output(opt
, color
, strlen(color
));
594 opt
->output(opt
, data
, size
);
595 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
597 opt
->output(opt
, data
, size
);
600 static void output_sep(struct grep_opt
*opt
, char sign
)
602 if (opt
->null_following_name
)
603 opt
->output(opt
, "\0", 1);
605 output_color(opt
, &sign
, 1, opt
->color_sep
);
608 static void show_name(struct grep_opt
*opt
, const char *name
)
610 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
611 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
614 static int fixmatch(struct grep_pat
*p
, char *line
, char *eol
,
617 struct kwsmatch kwsm
;
618 size_t offset
= kwsexec(p
->kws
, line
, eol
- line
, &kwsm
);
620 match
->rm_so
= match
->rm_eo
= -1;
623 match
->rm_so
= offset
;
624 match
->rm_eo
= match
->rm_so
+ kwsm
.size
[0];
629 static int regmatch(const regex_t
*preg
, char *line
, char *eol
,
630 regmatch_t
*match
, int eflags
)
634 match
->rm_eo
= eol
- line
;
635 eflags
|= REG_STARTEND
;
637 return regexec(preg
, line
, 1, match
, eflags
);
640 static int patmatch(struct grep_pat
*p
, char *line
, char *eol
,
641 regmatch_t
*match
, int eflags
)
646 hit
= !fixmatch(p
, line
, eol
, match
);
647 else if (p
->pcre_regexp
)
648 hit
= !pcrematch(p
, line
, eol
, match
, eflags
);
650 hit
= !regmatch(&p
->regexp
, line
, eol
, match
, eflags
);
655 static int strip_timestamp(char *bol
, char **eol_p
)
660 while (bol
< --eol
) {
676 { "committer ", 10 },
679 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
680 enum grep_context ctx
,
681 regmatch_t
*pmatch
, int eflags
)
685 const char *start
= bol
;
687 if ((p
->token
!= GREP_PATTERN
) &&
688 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
691 if (p
->token
== GREP_PATTERN_HEAD
) {
694 assert(p
->field
< ARRAY_SIZE(header_field
));
695 field
= header_field
[p
->field
].field
;
696 len
= header_field
[p
->field
].len
;
697 if (strncmp(bol
, field
, len
))
700 saved_ch
= strip_timestamp(bol
, &eol
);
704 hit
= patmatch(p
, bol
, eol
, pmatch
, eflags
);
706 if (hit
&& p
->word_regexp
) {
707 if ((pmatch
[0].rm_so
< 0) ||
708 (eol
- bol
) < pmatch
[0].rm_so
||
709 (pmatch
[0].rm_eo
< 0) ||
710 (eol
- bol
) < pmatch
[0].rm_eo
)
711 die("regexp returned nonsense");
713 /* Match beginning must be either beginning of the
714 * line, or at word boundary (i.e. the last char must
715 * not be a word char). Similarly, match end must be
716 * either end of the line, or at word boundary
717 * (i.e. the next char must not be a word char).
719 if ( ((pmatch
[0].rm_so
== 0) ||
720 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
721 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
722 !word_char(bol
[pmatch
[0].rm_eo
])) )
727 /* Words consist of at least one character. */
728 if (pmatch
->rm_so
== pmatch
->rm_eo
)
731 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
732 /* There could be more than one match on the
733 * line, and the first match might not be
734 * strict word match. But later ones could be!
735 * Forward to the next possible start, i.e. the
736 * next position following a non-word char.
738 bol
= pmatch
[0].rm_so
+ bol
+ 1;
739 while (word_char(bol
[-1]) && bol
< eol
)
741 eflags
|= REG_NOTBOL
;
746 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
749 pmatch
[0].rm_so
+= bol
- start
;
750 pmatch
[0].rm_eo
+= bol
- start
;
755 static int match_expr_eval(struct grep_expr
*x
, char *bol
, char *eol
,
756 enum grep_context ctx
, int collect_hits
)
762 die("Not a valid grep expression");
768 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
, &match
, 0);
771 h
= !match_expr_eval(x
->u
.unary
, bol
, eol
, ctx
, 0);
774 if (!match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0))
776 h
= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
780 return (match_expr_eval(x
->u
.binary
.left
,
782 match_expr_eval(x
->u
.binary
.right
,
784 h
= match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
785 x
->u
.binary
.left
->hit
|= h
;
786 h
|= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
789 die("Unexpected node type (internal error) %d", x
->node
);
796 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
797 enum grep_context ctx
, int collect_hits
)
799 struct grep_expr
*x
= opt
->pattern_expression
;
800 return match_expr_eval(x
, bol
, eol
, ctx
, collect_hits
);
803 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
804 enum grep_context ctx
, int collect_hits
)
810 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
812 /* we do not call with collect_hits without being extended */
813 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
814 if (match_one_pattern(p
, bol
, eol
, ctx
, &match
, 0))
820 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
821 enum grep_context ctx
,
822 regmatch_t
*pmatch
, int eflags
)
826 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
828 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
830 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
831 if (match
.rm_so
> pmatch
->rm_so
)
833 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
836 pmatch
->rm_so
= match
.rm_so
;
837 pmatch
->rm_eo
= match
.rm_eo
;
841 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
842 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
847 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
849 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
851 case GREP_PATTERN
: /* atom */
852 case GREP_PATTERN_HEAD
:
853 case GREP_PATTERN_BODY
:
854 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
865 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
866 const char *name
, unsigned lno
, char sign
)
868 int rest
= eol
- bol
;
869 char *line_color
= NULL
;
871 if (opt
->file_break
&& opt
->last_shown
== 0) {
872 if (opt
->show_hunk_mark
)
873 opt
->output(opt
, "\n", 1);
874 } else if (opt
->pre_context
|| opt
->post_context
|| opt
->funcbody
) {
875 if (opt
->last_shown
== 0) {
876 if (opt
->show_hunk_mark
) {
877 output_color(opt
, "--", 2, opt
->color_sep
);
878 opt
->output(opt
, "\n", 1);
880 } else if (lno
> opt
->last_shown
+ 1) {
881 output_color(opt
, "--", 2, opt
->color_sep
);
882 opt
->output(opt
, "\n", 1);
885 if (opt
->heading
&& opt
->last_shown
== 0) {
886 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
887 opt
->output(opt
, "\n", 1);
889 opt
->last_shown
= lno
;
891 if (!opt
->heading
&& opt
->pathname
) {
892 output_color(opt
, name
, strlen(name
), opt
->color_filename
);
893 output_sep(opt
, sign
);
897 snprintf(buf
, sizeof(buf
), "%d", lno
);
898 output_color(opt
, buf
, strlen(buf
), opt
->color_lineno
);
899 output_sep(opt
, sign
);
903 enum grep_context ctx
= GREP_CONTEXT_BODY
;
908 line_color
= opt
->color_selected
;
909 else if (sign
== '-')
910 line_color
= opt
->color_context
;
911 else if (sign
== '=')
912 line_color
= opt
->color_function
;
914 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
915 if (match
.rm_so
== match
.rm_eo
)
918 output_color(opt
, bol
, match
.rm_so
, line_color
);
919 output_color(opt
, bol
+ match
.rm_so
,
920 match
.rm_eo
- match
.rm_so
,
928 output_color(opt
, bol
, rest
, line_color
);
929 opt
->output(opt
, "\n", 1);
936 * This lock protects access to the gitattributes machinery, which is
939 pthread_mutex_t grep_attr_mutex
;
941 static inline void grep_attr_lock(void)
944 pthread_mutex_lock(&grep_attr_mutex
);
947 static inline void grep_attr_unlock(void)
950 pthread_mutex_unlock(&grep_attr_mutex
);
954 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
956 pthread_mutex_t grep_read_mutex
;
959 #define grep_attr_lock()
960 #define grep_attr_unlock()
963 static int match_funcname(struct grep_opt
*opt
, struct grep_source
*gs
, char *bol
, char *eol
)
965 xdemitconf_t
*xecfg
= opt
->priv
;
966 if (xecfg
&& !xecfg
->find_func
) {
967 grep_source_load_driver(gs
);
968 if (gs
->driver
->funcname
.pattern
) {
969 const struct userdiff_funcname
*pe
= &gs
->driver
->funcname
;
970 xdiff_set_find_func(xecfg
, pe
->pattern
, pe
->cflags
);
972 xecfg
= opt
->priv
= NULL
;
978 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
979 xecfg
->find_func_priv
) >= 0;
984 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
989 static void show_funcname_line(struct grep_opt
*opt
, struct grep_source
*gs
,
990 char *bol
, unsigned lno
)
992 while (bol
> gs
->buf
) {
995 while (bol
> gs
->buf
&& bol
[-1] != '\n')
999 if (lno
<= opt
->last_shown
)
1002 if (match_funcname(opt
, gs
, bol
, eol
)) {
1003 show_line(opt
, bol
, eol
, gs
->name
, lno
, '=');
1009 static void show_pre_context(struct grep_opt
*opt
, struct grep_source
*gs
,
1010 char *bol
, char *end
, unsigned lno
)
1012 unsigned cur
= lno
, from
= 1, funcname_lno
= 0;
1013 int funcname_needed
= !!opt
->funcname
;
1015 if (opt
->funcbody
&& !match_funcname(opt
, gs
, bol
, end
))
1016 funcname_needed
= 2;
1018 if (opt
->pre_context
< lno
)
1019 from
= lno
- opt
->pre_context
;
1020 if (from
<= opt
->last_shown
)
1021 from
= opt
->last_shown
+ 1;
1024 while (bol
> gs
->buf
&&
1025 cur
> (funcname_needed
== 2 ? opt
->last_shown
+ 1 : from
)) {
1028 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1031 if (funcname_needed
&& match_funcname(opt
, gs
, bol
, eol
)) {
1033 funcname_needed
= 0;
1037 /* We need to look even further back to find a function signature. */
1038 if (opt
->funcname
&& funcname_needed
)
1039 show_funcname_line(opt
, gs
, bol
, cur
);
1043 char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
1045 while (*eol
!= '\n')
1047 show_line(opt
, bol
, eol
, gs
->name
, cur
, sign
);
1053 static int should_lookahead(struct grep_opt
*opt
)
1058 return 0; /* punt for too complex stuff */
1061 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1062 if (p
->token
!= GREP_PATTERN
)
1063 return 0; /* punt for "header only" and stuff */
1068 static int look_ahead(struct grep_opt
*opt
,
1069 unsigned long *left_p
,
1073 unsigned lno
= *lno_p
;
1076 char *sp
, *last_bol
;
1077 regoff_t earliest
= -1;
1079 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1083 hit
= patmatch(p
, bol
, bol
+ *left_p
, &m
, 0);
1084 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
1086 if (earliest
< 0 || m
.rm_so
< earliest
)
1091 *bol_p
= bol
+ *left_p
;
1095 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
1096 ; /* find the beginning of the line */
1099 for (sp
= bol
; sp
< last_bol
; sp
++) {
1103 *left_p
-= last_bol
- bol
;
1109 static void std_output(struct grep_opt
*opt
, const void *buf
, size_t size
)
1111 fwrite(buf
, size
, 1, stdout
);
1114 static int grep_source_1(struct grep_opt
*opt
, struct grep_source
*gs
, int collect_hits
)
1119 unsigned last_hit
= 0;
1120 int binary_match_only
= 0;
1122 int try_lookahead
= 0;
1123 int show_function
= 0;
1124 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
1128 opt
->output
= std_output
;
1130 if (opt
->pre_context
|| opt
->post_context
|| opt
->file_break
||
1132 /* Show hunk marks, except for the first file. */
1133 if (opt
->last_shown
)
1134 opt
->show_hunk_mark
= 1;
1136 * If we're using threads then we can't easily identify
1137 * the first file. Always put hunk marks in that case
1138 * and skip the very first one later in work_done().
1140 if (opt
->output
!= std_output
)
1141 opt
->show_hunk_mark
= 1;
1143 opt
->last_shown
= 0;
1145 switch (opt
->binary
) {
1146 case GREP_BINARY_DEFAULT
:
1147 if (grep_source_is_binary(gs
))
1148 binary_match_only
= 1;
1150 case GREP_BINARY_NOMATCH
:
1151 if (grep_source_is_binary(gs
))
1152 return 0; /* Assume unmatch */
1154 case GREP_BINARY_TEXT
:
1157 die("bug: unknown binary handling mode");
1160 memset(&xecfg
, 0, sizeof(xecfg
));
1163 try_lookahead
= should_lookahead(opt
);
1165 if (grep_source_load(gs
) < 0)
1175 * look_ahead() skips quickly to the line that possibly
1176 * has the next hit; don't call it if we need to do
1177 * something more than just skipping the current line
1178 * in response to an unmatch for the current line. E.g.
1179 * inside a post-context window, we will show the current
1180 * line as a context around the previous hit when it
1185 && (show_function
||
1186 lno
<= last_hit
+ opt
->post_context
))
1187 && look_ahead(opt
, &left
, &lno
, &bol
))
1189 eol
= end_of_line(bol
, &left
);
1193 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
1194 ctx
= GREP_CONTEXT_BODY
;
1196 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
1202 /* "grep -v -e foo -e bla" should list lines
1203 * that do not have either, so inversion should
1208 if (opt
->unmatch_name_only
) {
1215 if (opt
->status_only
)
1217 if (opt
->name_only
) {
1218 show_name(opt
, gs
->name
);
1223 if (binary_match_only
) {
1224 opt
->output(opt
, "Binary file ", 12);
1225 output_color(opt
, gs
->name
, strlen(gs
->name
),
1226 opt
->color_filename
);
1227 opt
->output(opt
, " matches\n", 9);
1230 /* Hit at this line. If we haven't shown the
1231 * pre-context lines, we would need to show them.
1233 if (opt
->pre_context
|| opt
->funcbody
)
1234 show_pre_context(opt
, gs
, bol
, eol
, lno
);
1235 else if (opt
->funcname
)
1236 show_funcname_line(opt
, gs
, bol
, lno
);
1237 show_line(opt
, bol
, eol
, gs
->name
, lno
, ':');
1243 if (show_function
&& match_funcname(opt
, gs
, bol
, eol
))
1245 if (show_function
||
1246 (last_hit
&& lno
<= last_hit
+ opt
->post_context
)) {
1247 /* If the last hit is within the post context,
1248 * we need to show this line.
1250 show_line(opt
, bol
, eol
, gs
->name
, lno
, '-');
1264 if (opt
->status_only
)
1266 if (opt
->unmatch_name_only
) {
1267 /* We did not see any hit, so we want to show this */
1268 show_name(opt
, gs
->name
);
1272 xdiff_clear_find_func(&xecfg
);
1276 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1277 * which feels mostly useless but sometimes useful. Maybe
1278 * make it another option? For now suppress them.
1280 if (opt
->count
&& count
) {
1282 output_color(opt
, gs
->name
, strlen(gs
->name
), opt
->color_filename
);
1283 output_sep(opt
, ':');
1284 snprintf(buf
, sizeof(buf
), "%u\n", count
);
1285 opt
->output(opt
, buf
, strlen(buf
));
1291 static void clr_hit_marker(struct grep_expr
*x
)
1293 /* All-hit markers are meaningful only at the very top level
1298 if (x
->node
!= GREP_NODE_OR
)
1300 x
->u
.binary
.left
->hit
= 0;
1301 x
= x
->u
.binary
.right
;
1305 static int chk_hit_marker(struct grep_expr
*x
)
1307 /* Top level nodes have hit markers. See if they all are hits */
1309 if (x
->node
!= GREP_NODE_OR
)
1311 if (!x
->u
.binary
.left
->hit
)
1313 x
= x
->u
.binary
.right
;
1317 int grep_source(struct grep_opt
*opt
, struct grep_source
*gs
)
1320 * we do not have to do the two-pass grep when we do not check
1321 * buffer-wide "all-match".
1323 if (!opt
->all_match
)
1324 return grep_source_1(opt
, gs
, 0);
1326 /* Otherwise the toplevel "or" terms hit a bit differently.
1327 * We first clear hit markers from them.
1329 clr_hit_marker(opt
->pattern_expression
);
1330 grep_source_1(opt
, gs
, 1);
1332 if (!chk_hit_marker(opt
->pattern_expression
))
1335 return grep_source_1(opt
, gs
, 0);
1338 int grep_buffer(struct grep_opt
*opt
, char *buf
, unsigned long size
)
1340 struct grep_source gs
;
1343 grep_source_init(&gs
, GREP_SOURCE_BUF
, NULL
, NULL
);
1347 r
= grep_source(opt
, &gs
);
1349 grep_source_clear(&gs
);
1353 void grep_source_init(struct grep_source
*gs
, enum grep_source_type type
,
1354 const char *name
, const void *identifier
)
1357 gs
->name
= name
? xstrdup(name
) : NULL
;
1363 case GREP_SOURCE_FILE
:
1364 gs
->identifier
= xstrdup(identifier
);
1366 case GREP_SOURCE_SHA1
:
1367 gs
->identifier
= xmalloc(20);
1368 memcpy(gs
->identifier
, identifier
, 20);
1370 case GREP_SOURCE_BUF
:
1371 gs
->identifier
= NULL
;
1375 void grep_source_clear(struct grep_source
*gs
)
1379 free(gs
->identifier
);
1380 gs
->identifier
= NULL
;
1381 grep_source_clear_data(gs
);
1384 void grep_source_clear_data(struct grep_source
*gs
)
1387 case GREP_SOURCE_FILE
:
1388 case GREP_SOURCE_SHA1
:
1393 case GREP_SOURCE_BUF
:
1394 /* leave user-provided buf intact */
1399 static int grep_source_load_sha1(struct grep_source
*gs
)
1401 enum object_type type
;
1404 gs
->buf
= read_sha1_file(gs
->identifier
, &type
, &gs
->size
);
1408 return error(_("'%s': unable to read %s"),
1410 sha1_to_hex(gs
->identifier
));
1414 static int grep_source_load_file(struct grep_source
*gs
)
1416 const char *filename
= gs
->identifier
;
1422 if (lstat(filename
, &st
) < 0) {
1424 if (errno
!= ENOENT
)
1425 error(_("'%s': %s"), filename
, strerror(errno
));
1428 if (!S_ISREG(st
.st_mode
))
1430 size
= xsize_t(st
.st_size
);
1431 i
= open(filename
, O_RDONLY
);
1434 data
= xmalloc(size
+ 1);
1435 if (st
.st_size
!= read_in_full(i
, data
, size
)) {
1436 error(_("'%s': short read %s"), filename
, strerror(errno
));
1449 int grep_source_load(struct grep_source
*gs
)
1455 case GREP_SOURCE_FILE
:
1456 return grep_source_load_file(gs
);
1457 case GREP_SOURCE_SHA1
:
1458 return grep_source_load_sha1(gs
);
1459 case GREP_SOURCE_BUF
:
1460 return gs
->buf
? 0 : -1;
1462 die("BUG: invalid grep_source type");
1465 void grep_source_load_driver(struct grep_source
*gs
)
1471 gs
->driver
= userdiff_find_by_path(gs
->name
);
1473 gs
->driver
= userdiff_find_by_name("default");
1477 int grep_source_is_binary(struct grep_source
*gs
)
1479 grep_source_load_driver(gs
);
1480 if (gs
->driver
->binary
!= -1)
1481 return gs
->driver
->binary
;
1483 if (!grep_source_load(gs
))
1484 return buffer_is_binary(gs
->buf
, gs
->size
);