4 #include "xdiff-interface.h"
6 void append_header_grep_pattern(struct grep_opt
*opt
, enum grep_header_field field
, const char *pat
)
8 struct grep_pat
*p
= xcalloc(1, sizeof(*p
));
12 p
->token
= GREP_PATTERN_HEAD
;
14 *opt
->pattern_tail
= p
;
15 opt
->pattern_tail
= &p
->next
;
19 void append_grep_pattern(struct grep_opt
*opt
, const char *pat
,
20 const char *origin
, int no
, enum grep_pat_token t
)
22 struct grep_pat
*p
= xcalloc(1, sizeof(*p
));
27 *opt
->pattern_tail
= p
;
28 opt
->pattern_tail
= &p
->next
;
32 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
36 p
->word_regexp
= opt
->word_regexp
;
37 p
->ignore_case
= opt
->ignore_case
;
41 if (opt
->regflags
& REG_ICASE
)
46 err
= regcomp(&p
->regexp
, p
->pattern
, opt
->regflags
);
51 sprintf(where
, "In '%s' at %d, ",
54 sprintf(where
, "%s, ", p
->origin
);
57 regerror(err
, &p
->regexp
, errbuf
, 1024);
59 die("%s'%s': %s", where
, p
->pattern
, errbuf
);
63 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
64 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
73 case GREP_PATTERN
: /* atom */
74 case GREP_PATTERN_HEAD
:
75 case GREP_PATTERN_BODY
:
76 x
= xcalloc(1, sizeof (struct grep_expr
));
77 x
->node
= GREP_NODE_ATOM
;
83 x
= compile_pattern_or(list
);
84 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
85 die("unmatched parenthesis");
86 *list
= (*list
)->next
;
93 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
104 die("--not not followed by pattern expression");
106 x
= xcalloc(1, sizeof (struct grep_expr
));
107 x
->node
= GREP_NODE_NOT
;
108 x
->u
.unary
= compile_pattern_not(list
);
110 die("--not followed by non pattern expression");
113 return compile_pattern_atom(list
);
117 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
120 struct grep_expr
*x
, *y
, *z
;
122 x
= compile_pattern_not(list
);
124 if (p
&& p
->token
== GREP_AND
) {
126 die("--and not followed by pattern expression");
128 y
= compile_pattern_and(list
);
130 die("--and not followed by pattern expression");
131 z
= xcalloc(1, sizeof (struct grep_expr
));
132 z
->node
= GREP_NODE_AND
;
133 z
->u
.binary
.left
= x
;
134 z
->u
.binary
.right
= y
;
140 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
143 struct grep_expr
*x
, *y
, *z
;
145 x
= compile_pattern_and(list
);
147 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
148 y
= compile_pattern_or(list
);
150 die("not a pattern expression %s", p
->pattern
);
151 z
= xcalloc(1, sizeof (struct grep_expr
));
152 z
->node
= GREP_NODE_OR
;
153 z
->u
.binary
.left
= x
;
154 z
->u
.binary
.right
= y
;
160 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
162 return compile_pattern_or(list
);
165 void compile_grep_patterns(struct grep_opt
*opt
)
172 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
174 case GREP_PATTERN
: /* atom */
175 case GREP_PATTERN_HEAD
:
176 case GREP_PATTERN_BODY
:
177 compile_regexp(p
, opt
);
188 /* Then bundle them up in an expression.
189 * A classic recursive descent parser would do.
191 p
= opt
->pattern_list
;
193 opt
->pattern_expression
= compile_pattern_expr(&p
);
195 die("incomplete pattern expression: %s", p
->pattern
);
198 static void free_pattern_expr(struct grep_expr
*x
)
204 free_pattern_expr(x
->u
.unary
);
208 free_pattern_expr(x
->u
.binary
.left
);
209 free_pattern_expr(x
->u
.binary
.right
);
215 void free_grep_patterns(struct grep_opt
*opt
)
217 struct grep_pat
*p
, *n
;
219 for (p
= opt
->pattern_list
; p
; p
= n
) {
222 case GREP_PATTERN
: /* atom */
223 case GREP_PATTERN_HEAD
:
224 case GREP_PATTERN_BODY
:
235 free_pattern_expr(opt
->pattern_expression
);
238 static char *end_of_line(char *cp
, unsigned long *left
)
240 unsigned long l
= *left
;
241 while (l
&& *cp
!= '\n') {
249 static int word_char(char ch
)
251 return isalnum(ch
) || ch
== '_';
254 static void show_name(struct grep_opt
*opt
, const char *name
)
256 printf("%s%c", name
, opt
->null_following_name
? '\0' : '\n');
260 static int fixmatch(const char *pattern
, char *line
, int ignore_case
, regmatch_t
*match
)
264 hit
= strcasestr(line
, pattern
);
266 hit
= strstr(line
, pattern
);
269 match
->rm_so
= match
->rm_eo
= -1;
273 match
->rm_so
= hit
- line
;
274 match
->rm_eo
= match
->rm_so
+ strlen(pattern
);
279 static int strip_timestamp(char *bol
, char **eol_p
)
284 while (bol
< --eol
) {
300 { "committer ", 10 },
303 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
304 enum grep_context ctx
,
305 regmatch_t
*pmatch
, int eflags
)
309 const char *start
= bol
;
311 if ((p
->token
!= GREP_PATTERN
) &&
312 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
315 if (p
->token
== GREP_PATTERN_HEAD
) {
318 assert(p
->field
< ARRAY_SIZE(header_field
));
319 field
= header_field
[p
->field
].field
;
320 len
= header_field
[p
->field
].len
;
321 if (strncmp(bol
, field
, len
))
324 saved_ch
= strip_timestamp(bol
, &eol
);
329 hit
= !fixmatch(p
->pattern
, bol
, p
->ignore_case
, pmatch
);
331 hit
= !regexec(&p
->regexp
, bol
, 1, pmatch
, eflags
);
333 if (hit
&& p
->word_regexp
) {
334 if ((pmatch
[0].rm_so
< 0) ||
335 (eol
- bol
) < pmatch
[0].rm_so
||
336 (pmatch
[0].rm_eo
< 0) ||
337 (eol
- bol
) < pmatch
[0].rm_eo
)
338 die("regexp returned nonsense");
340 /* Match beginning must be either beginning of the
341 * line, or at word boundary (i.e. the last char must
342 * not be a word char). Similarly, match end must be
343 * either end of the line, or at word boundary
344 * (i.e. the next char must not be a word char).
346 if ( ((pmatch
[0].rm_so
== 0) ||
347 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
348 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
349 !word_char(bol
[pmatch
[0].rm_eo
])) )
354 /* Words consist of at least one character. */
355 if (pmatch
->rm_so
== pmatch
->rm_eo
)
358 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
359 /* There could be more than one match on the
360 * line, and the first match might not be
361 * strict word match. But later ones could be!
362 * Forward to the next possible start, i.e. the
363 * next position following a non-word char.
365 bol
= pmatch
[0].rm_so
+ bol
+ 1;
366 while (word_char(bol
[-1]) && bol
< eol
)
368 eflags
|= REG_NOTBOL
;
373 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
376 pmatch
[0].rm_so
+= bol
- start
;
377 pmatch
[0].rm_eo
+= bol
- start
;
382 static int match_expr_eval(struct grep_expr
*x
, char *bol
, char *eol
,
383 enum grep_context ctx
, int collect_hits
)
389 die("Not a valid grep expression");
392 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
, &match
, 0);
395 h
= !match_expr_eval(x
->u
.unary
, bol
, eol
, ctx
, 0);
398 if (!match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0))
400 h
= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
404 return (match_expr_eval(x
->u
.binary
.left
,
406 match_expr_eval(x
->u
.binary
.right
,
408 h
= match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
409 x
->u
.binary
.left
->hit
|= h
;
410 h
|= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
413 die("Unexpected node type (internal error) %d", x
->node
);
420 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
421 enum grep_context ctx
, int collect_hits
)
423 struct grep_expr
*x
= opt
->pattern_expression
;
424 return match_expr_eval(x
, bol
, eol
, ctx
, collect_hits
);
427 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
428 enum grep_context ctx
, int collect_hits
)
434 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
436 /* we do not call with collect_hits without being extended */
437 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
438 if (match_one_pattern(p
, bol
, eol
, ctx
, &match
, 0))
444 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
445 enum grep_context ctx
,
446 regmatch_t
*pmatch
, int eflags
)
450 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
452 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
454 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
455 if (match
.rm_so
> pmatch
->rm_so
)
457 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
460 pmatch
->rm_so
= match
.rm_so
;
461 pmatch
->rm_eo
= match
.rm_eo
;
465 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
466 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
471 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
473 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
475 case GREP_PATTERN
: /* atom */
476 case GREP_PATTERN_HEAD
:
477 case GREP_PATTERN_BODY
:
478 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
489 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
490 const char *name
, unsigned lno
, char sign
)
492 int rest
= eol
- bol
;
494 if (opt
->pre_context
|| opt
->post_context
) {
495 if (opt
->last_shown
== 0) {
496 if (opt
->show_hunk_mark
)
497 fputs("--\n", stdout
);
499 opt
->show_hunk_mark
= 1;
500 } else if (lno
> opt
->last_shown
+ 1)
501 fputs("--\n", stdout
);
503 opt
->last_shown
= lno
;
505 if (opt
->null_following_name
)
508 printf("%s%c", name
, sign
);
510 printf("%d%c", lno
, sign
);
513 enum grep_context ctx
= GREP_CONTEXT_BODY
;
518 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
519 if (match
.rm_so
== match
.rm_eo
)
521 printf("%.*s%s%.*s%s",
522 (int)match
.rm_so
, bol
,
524 (int)(match
.rm_eo
- match
.rm_so
), bol
+ match
.rm_so
,
532 printf("%.*s\n", rest
, bol
);
535 static int match_funcname(struct grep_opt
*opt
, char *bol
, char *eol
)
537 xdemitconf_t
*xecfg
= opt
->priv
;
538 if (xecfg
&& xecfg
->find_func
) {
540 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
541 xecfg
->find_func_priv
) >= 0;
546 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
551 static void show_funcname_line(struct grep_opt
*opt
, const char *name
,
552 char *buf
, char *bol
, unsigned lno
)
557 while (bol
> buf
&& bol
[-1] != '\n')
561 if (lno
<= opt
->last_shown
)
564 if (match_funcname(opt
, bol
, eol
)) {
565 show_line(opt
, bol
, eol
, name
, lno
, '=');
571 static void show_pre_context(struct grep_opt
*opt
, const char *name
, char *buf
,
572 char *bol
, unsigned lno
)
574 unsigned cur
= lno
, from
= 1, funcname_lno
= 0;
575 int funcname_needed
= opt
->funcname
;
577 if (opt
->pre_context
< lno
)
578 from
= lno
- opt
->pre_context
;
579 if (from
<= opt
->last_shown
)
580 from
= opt
->last_shown
+ 1;
583 while (bol
> buf
&& cur
> from
) {
586 while (bol
> buf
&& bol
[-1] != '\n')
589 if (funcname_needed
&& match_funcname(opt
, bol
, eol
)) {
595 /* We need to look even further back to find a function signature. */
596 if (opt
->funcname
&& funcname_needed
)
597 show_funcname_line(opt
, name
, buf
, bol
, cur
);
601 char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
605 show_line(opt
, bol
, eol
, name
, cur
, sign
);
611 static int should_lookahead(struct grep_opt
*opt
)
616 return 0; /* punt for too complex stuff */
619 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
620 if (p
->token
!= GREP_PATTERN
)
621 return 0; /* punt for "header only" and stuff */
626 static int look_ahead(struct grep_opt
*opt
,
627 unsigned long *left_p
,
631 unsigned lno
= *lno_p
;
635 regoff_t earliest
= -1;
637 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
642 hit
= !fixmatch(p
->pattern
, bol
, p
->ignore_case
, &m
);
647 hit
= !regexec(&p
->regexp
, bol
, 1, &m
, REG_STARTEND
);
649 hit
= !regexec(&p
->regexp
, bol
, 1, &m
, 0);
652 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
654 if (earliest
< 0 || m
.rm_so
< earliest
)
659 *bol_p
= bol
+ *left_p
;
663 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
664 ; /* find the beginning of the line */
667 for (sp
= bol
; sp
< last_bol
; sp
++) {
671 *left_p
-= last_bol
- bol
;
677 static int grep_buffer_1(struct grep_opt
*opt
, const char *name
,
678 char *buf
, unsigned long size
, int collect_hits
)
681 unsigned long left
= size
;
683 unsigned last_hit
= 0;
684 int binary_match_only
= 0;
686 int try_lookahead
= 0;
687 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
692 if (buffer_is_binary(buf
, size
)) {
693 switch (opt
->binary
) {
694 case GREP_BINARY_DEFAULT
:
695 binary_match_only
= 1;
697 case GREP_BINARY_NOMATCH
:
698 return 0; /* Assume unmatch */
705 memset(&xecfg
, 0, sizeof(xecfg
));
706 if (opt
->funcname
&& !opt
->unmatch_name_only
&& !opt
->status_only
&&
707 !opt
->name_only
&& !binary_match_only
&& !collect_hits
) {
708 struct userdiff_driver
*drv
= userdiff_find_by_path(name
);
709 if (drv
&& drv
->funcname
.pattern
) {
710 const struct userdiff_funcname
*pe
= &drv
->funcname
;
711 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
715 try_lookahead
= should_lookahead(opt
);
722 * look_ahead() skips quicly to the line that possibly
723 * has the next hit; don't call it if we need to do
724 * something more than just skipping the current line
725 * in response to an unmatch for the current line. E.g.
726 * inside a post-context window, we will show the current
727 * line as a context around the previous hit when it
732 && lno
<= last_hit
+ opt
->post_context
)
733 && look_ahead(opt
, &left
, &lno
, &bol
))
735 eol
= end_of_line(bol
, &left
);
739 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
740 ctx
= GREP_CONTEXT_BODY
;
742 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
748 /* "grep -v -e foo -e bla" should list lines
749 * that do not have either, so inversion should
754 if (opt
->unmatch_name_only
) {
761 if (opt
->status_only
)
763 if (binary_match_only
) {
764 printf("Binary file %s matches\n", name
);
767 if (opt
->name_only
) {
768 show_name(opt
, name
);
771 /* Hit at this line. If we haven't shown the
772 * pre-context lines, we would need to show them.
773 * When asked to do "count", this still show
774 * the context which is nonsense, but the user
775 * deserves to get that ;-).
777 if (opt
->pre_context
)
778 show_pre_context(opt
, name
, buf
, bol
, lno
);
779 else if (opt
->funcname
)
780 show_funcname_line(opt
, name
, buf
, bol
, lno
);
782 show_line(opt
, bol
, eol
, name
, lno
, ':');
786 lno
<= last_hit
+ opt
->post_context
) {
787 /* If the last hit is within the post context,
788 * we need to show this line.
790 show_line(opt
, bol
, eol
, name
, lno
, '-');
804 if (opt
->status_only
)
806 if (opt
->unmatch_name_only
) {
807 /* We did not see any hit, so we want to show this */
808 show_name(opt
, name
);
812 xdiff_clear_find_func(&xecfg
);
816 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
817 * which feels mostly useless but sometimes useful. Maybe
818 * make it another option? For now suppress them.
820 if (opt
->count
&& count
)
821 printf("%s%c%u\n", name
,
822 opt
->null_following_name
? '\0' : ':', count
);
826 static void clr_hit_marker(struct grep_expr
*x
)
828 /* All-hit markers are meaningful only at the very top level
833 if (x
->node
!= GREP_NODE_OR
)
835 x
->u
.binary
.left
->hit
= 0;
836 x
= x
->u
.binary
.right
;
840 static int chk_hit_marker(struct grep_expr
*x
)
842 /* Top level nodes have hit markers. See if they all are hits */
844 if (x
->node
!= GREP_NODE_OR
)
846 if (!x
->u
.binary
.left
->hit
)
848 x
= x
->u
.binary
.right
;
852 int grep_buffer(struct grep_opt
*opt
, const char *name
, char *buf
, unsigned long size
)
855 * we do not have to do the two-pass grep when we do not check
856 * buffer-wide "all-match".
859 return grep_buffer_1(opt
, name
, buf
, size
, 0);
861 /* Otherwise the toplevel "or" terms hit a bit differently.
862 * We first clear hit markers from them.
864 clr_hit_marker(opt
->pattern_expression
);
865 grep_buffer_1(opt
, name
, buf
, size
, 1);
867 if (!chk_hit_marker(opt
->pattern_expression
))
870 return grep_buffer_1(opt
, name
, buf
, size
, 0);