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 int is_fixed(const char *s
)
34 while (*s
&& !is_regex_special(*s
))
39 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
43 p
->word_regexp
= opt
->word_regexp
;
45 if (opt
->fixed
|| is_fixed(p
->pattern
))
47 if (opt
->regflags
& REG_ICASE
)
52 err
= regcomp(&p
->regexp
, p
->pattern
, opt
->regflags
);
57 sprintf(where
, "In '%s' at %d, ",
60 sprintf(where
, "%s, ", p
->origin
);
63 regerror(err
, &p
->regexp
, errbuf
, 1024);
65 die("%s'%s': %s", where
, p
->pattern
, errbuf
);
69 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
70 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
79 case GREP_PATTERN
: /* atom */
80 case GREP_PATTERN_HEAD
:
81 case GREP_PATTERN_BODY
:
82 x
= xcalloc(1, sizeof (struct grep_expr
));
83 x
->node
= GREP_NODE_ATOM
;
89 x
= compile_pattern_or(list
);
90 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
91 die("unmatched parenthesis");
92 *list
= (*list
)->next
;
99 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
110 die("--not not followed by pattern expression");
112 x
= xcalloc(1, sizeof (struct grep_expr
));
113 x
->node
= GREP_NODE_NOT
;
114 x
->u
.unary
= compile_pattern_not(list
);
116 die("--not followed by non pattern expression");
119 return compile_pattern_atom(list
);
123 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
126 struct grep_expr
*x
, *y
, *z
;
128 x
= compile_pattern_not(list
);
130 if (p
&& p
->token
== GREP_AND
) {
132 die("--and not followed by pattern expression");
134 y
= compile_pattern_and(list
);
136 die("--and not followed by pattern expression");
137 z
= xcalloc(1, sizeof (struct grep_expr
));
138 z
->node
= GREP_NODE_AND
;
139 z
->u
.binary
.left
= x
;
140 z
->u
.binary
.right
= y
;
146 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
149 struct grep_expr
*x
, *y
, *z
;
151 x
= compile_pattern_and(list
);
153 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
154 y
= compile_pattern_or(list
);
156 die("not a pattern expression %s", p
->pattern
);
157 z
= xcalloc(1, sizeof (struct grep_expr
));
158 z
->node
= GREP_NODE_OR
;
159 z
->u
.binary
.left
= x
;
160 z
->u
.binary
.right
= y
;
166 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
168 return compile_pattern_or(list
);
171 void compile_grep_patterns(struct grep_opt
*opt
)
178 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
180 case GREP_PATTERN
: /* atom */
181 case GREP_PATTERN_HEAD
:
182 case GREP_PATTERN_BODY
:
183 compile_regexp(p
, opt
);
194 /* Then bundle them up in an expression.
195 * A classic recursive descent parser would do.
197 p
= opt
->pattern_list
;
199 opt
->pattern_expression
= compile_pattern_expr(&p
);
201 die("incomplete pattern expression: %s", p
->pattern
);
204 static void free_pattern_expr(struct grep_expr
*x
)
210 free_pattern_expr(x
->u
.unary
);
214 free_pattern_expr(x
->u
.binary
.left
);
215 free_pattern_expr(x
->u
.binary
.right
);
221 void free_grep_patterns(struct grep_opt
*opt
)
223 struct grep_pat
*p
, *n
;
225 for (p
= opt
->pattern_list
; p
; p
= n
) {
228 case GREP_PATTERN
: /* atom */
229 case GREP_PATTERN_HEAD
:
230 case GREP_PATTERN_BODY
:
241 free_pattern_expr(opt
->pattern_expression
);
244 static char *end_of_line(char *cp
, unsigned long *left
)
246 unsigned long l
= *left
;
247 while (l
&& *cp
!= '\n') {
255 static int word_char(char ch
)
257 return isalnum(ch
) || ch
== '_';
260 static void show_name(struct grep_opt
*opt
, const char *name
)
262 printf("%s%c", name
, opt
->null_following_name
? '\0' : '\n');
265 static int fixmatch(const char *pattern
, char *line
, regmatch_t
*match
)
267 char *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
, 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
|| opt
->funcname
) {
495 if (opt
->last_shown
== 0) {
496 if (opt
->show_hunk_mark
)
497 fputs(opt
->funcname
? "==\n" : "--\n", stdout
);
499 opt
->show_hunk_mark
= 1;
500 } else if (lno
> opt
->last_shown
+ 1) {
501 if (opt
->pre_context
|| opt
->post_context
)
502 fputs((sign
== '=') ? "==\n" : "--\n", stdout
);
503 else if (sign
== '=')
504 fputs("==\n", stdout
);
507 opt
->last_shown
= lno
;
509 if (opt
->null_following_name
)
512 printf("%s%c", name
, sign
);
514 printf("%d%c", lno
, sign
);
517 enum grep_context ctx
= GREP_CONTEXT_BODY
;
522 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
523 if (match
.rm_so
== match
.rm_eo
)
525 printf("%.*s%s%.*s%s",
526 (int)match
.rm_so
, bol
,
528 (int)(match
.rm_eo
- match
.rm_so
), bol
+ match
.rm_so
,
536 printf("%.*s\n", rest
, bol
);
539 static int match_funcname(struct grep_opt
*opt
, char *bol
, char *eol
)
541 xdemitconf_t
*xecfg
= opt
->priv
;
542 if (xecfg
&& xecfg
->find_func
) {
544 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
545 xecfg
->find_func_priv
) >= 0;
550 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
555 static void show_funcname_line(struct grep_opt
*opt
, const char *name
,
556 char *buf
, char *bol
, unsigned lno
)
561 while (bol
> buf
&& bol
[-1] != '\n')
565 if (lno
<= opt
->last_shown
)
568 if (match_funcname(opt
, bol
, eol
)) {
569 show_line(opt
, bol
, eol
, name
, lno
, '=');
575 static void show_pre_context(struct grep_opt
*opt
, const char *name
, char *buf
,
576 char *bol
, unsigned lno
)
578 unsigned cur
= lno
, from
= 1, funcname_lno
= 0;
579 int funcname_needed
= opt
->funcname
;
581 if (opt
->pre_context
< lno
)
582 from
= lno
- opt
->pre_context
;
583 if (from
<= opt
->last_shown
)
584 from
= opt
->last_shown
+ 1;
587 while (bol
> buf
&& cur
> from
) {
590 while (bol
> buf
&& bol
[-1] != '\n')
593 if (funcname_needed
&& match_funcname(opt
, bol
, eol
)) {
599 /* We need to look even further back to find a function signature. */
600 if (opt
->funcname
&& funcname_needed
)
601 show_funcname_line(opt
, name
, buf
, bol
, cur
);
605 char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
609 show_line(opt
, bol
, eol
, name
, cur
, sign
);
615 static int grep_buffer_1(struct grep_opt
*opt
, const char *name
,
616 char *buf
, unsigned long size
, int collect_hits
)
619 unsigned long left
= size
;
621 unsigned last_hit
= 0;
622 int binary_match_only
= 0;
624 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
629 if (buffer_is_binary(buf
, size
)) {
630 switch (opt
->binary
) {
631 case GREP_BINARY_DEFAULT
:
632 binary_match_only
= 1;
634 case GREP_BINARY_NOMATCH
:
635 return 0; /* Assume unmatch */
642 memset(&xecfg
, 0, sizeof(xecfg
));
643 if (opt
->funcname
&& !opt
->unmatch_name_only
&& !opt
->status_only
&&
644 !opt
->name_only
&& !binary_match_only
&& !collect_hits
) {
645 struct userdiff_driver
*drv
= userdiff_find_by_path(name
);
646 if (drv
&& drv
->funcname
.pattern
) {
647 const struct userdiff_funcname
*pe
= &drv
->funcname
;
648 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
657 eol
= end_of_line(bol
, &left
);
661 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
662 ctx
= GREP_CONTEXT_BODY
;
664 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
670 /* "grep -v -e foo -e bla" should list lines
671 * that do not have either, so inversion should
676 if (opt
->unmatch_name_only
) {
683 if (opt
->status_only
)
685 if (binary_match_only
) {
686 printf("Binary file %s matches\n", name
);
689 if (opt
->name_only
) {
690 show_name(opt
, name
);
693 /* Hit at this line. If we haven't shown the
694 * pre-context lines, we would need to show them.
695 * When asked to do "count", this still show
696 * the context which is nonsense, but the user
697 * deserves to get that ;-).
699 if (opt
->pre_context
)
700 show_pre_context(opt
, name
, buf
, bol
, lno
);
701 else if (opt
->funcname
)
702 show_funcname_line(opt
, name
, buf
, bol
, lno
);
704 show_line(opt
, bol
, eol
, name
, lno
, ':');
708 lno
<= last_hit
+ opt
->post_context
) {
709 /* If the last hit is within the post context,
710 * we need to show this line.
712 show_line(opt
, bol
, eol
, name
, lno
, '-');
726 if (opt
->status_only
)
728 if (opt
->unmatch_name_only
) {
729 /* We did not see any hit, so we want to show this */
730 show_name(opt
, name
);
734 xdiff_clear_find_func(&xecfg
);
738 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
739 * which feels mostly useless but sometimes useful. Maybe
740 * make it another option? For now suppress them.
742 if (opt
->count
&& count
)
743 printf("%s%c%u\n", name
,
744 opt
->null_following_name
? '\0' : ':', count
);
748 static void clr_hit_marker(struct grep_expr
*x
)
750 /* All-hit markers are meaningful only at the very top level
755 if (x
->node
!= GREP_NODE_OR
)
757 x
->u
.binary
.left
->hit
= 0;
758 x
= x
->u
.binary
.right
;
762 static int chk_hit_marker(struct grep_expr
*x
)
764 /* Top level nodes have hit markers. See if they all are hits */
766 if (x
->node
!= GREP_NODE_OR
)
768 if (!x
->u
.binary
.left
->hit
)
770 x
= x
->u
.binary
.right
;
774 int grep_buffer(struct grep_opt
*opt
, const char *name
, char *buf
, unsigned long size
)
777 * we do not have to do the two-pass grep when we do not check
778 * buffer-wide "all-match".
781 return grep_buffer_1(opt
, name
, buf
, size
, 0);
783 /* Otherwise the toplevel "or" terms hit a bit differently.
784 * We first clear hit markers from them.
786 clr_hit_marker(opt
->pattern_expression
);
787 grep_buffer_1(opt
, name
, buf
, size
, 1);
789 if (!chk_hit_marker(opt
->pattern_expression
))
792 return grep_buffer_1(opt
, name
, buf
, size
, 0);