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
;
44 p
->ignore_case
= opt
->ignore_case
;
46 if (opt
->fixed
|| is_fixed(p
->pattern
))
48 if (opt
->regflags
& REG_ICASE
)
53 err
= regcomp(&p
->regexp
, p
->pattern
, opt
->regflags
);
58 sprintf(where
, "In '%s' at %d, ",
61 sprintf(where
, "%s, ", p
->origin
);
64 regerror(err
, &p
->regexp
, errbuf
, 1024);
66 die("%s'%s': %s", where
, p
->pattern
, errbuf
);
70 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
71 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
80 case GREP_PATTERN
: /* atom */
81 case GREP_PATTERN_HEAD
:
82 case GREP_PATTERN_BODY
:
83 x
= xcalloc(1, sizeof (struct grep_expr
));
84 x
->node
= GREP_NODE_ATOM
;
90 x
= compile_pattern_or(list
);
91 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
92 die("unmatched parenthesis");
93 *list
= (*list
)->next
;
100 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
111 die("--not not followed by pattern expression");
113 x
= xcalloc(1, sizeof (struct grep_expr
));
114 x
->node
= GREP_NODE_NOT
;
115 x
->u
.unary
= compile_pattern_not(list
);
117 die("--not followed by non pattern expression");
120 return compile_pattern_atom(list
);
124 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
127 struct grep_expr
*x
, *y
, *z
;
129 x
= compile_pattern_not(list
);
131 if (p
&& p
->token
== GREP_AND
) {
133 die("--and not followed by pattern expression");
135 y
= compile_pattern_and(list
);
137 die("--and not followed by pattern expression");
138 z
= xcalloc(1, sizeof (struct grep_expr
));
139 z
->node
= GREP_NODE_AND
;
140 z
->u
.binary
.left
= x
;
141 z
->u
.binary
.right
= y
;
147 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
150 struct grep_expr
*x
, *y
, *z
;
152 x
= compile_pattern_and(list
);
154 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
155 y
= compile_pattern_or(list
);
157 die("not a pattern expression %s", p
->pattern
);
158 z
= xcalloc(1, sizeof (struct grep_expr
));
159 z
->node
= GREP_NODE_OR
;
160 z
->u
.binary
.left
= x
;
161 z
->u
.binary
.right
= y
;
167 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
169 return compile_pattern_or(list
);
172 void compile_grep_patterns(struct grep_opt
*opt
)
179 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
181 case GREP_PATTERN
: /* atom */
182 case GREP_PATTERN_HEAD
:
183 case GREP_PATTERN_BODY
:
184 compile_regexp(p
, opt
);
195 /* Then bundle them up in an expression.
196 * A classic recursive descent parser would do.
198 p
= opt
->pattern_list
;
200 opt
->pattern_expression
= compile_pattern_expr(&p
);
202 die("incomplete pattern expression: %s", p
->pattern
);
205 static void free_pattern_expr(struct grep_expr
*x
)
211 free_pattern_expr(x
->u
.unary
);
215 free_pattern_expr(x
->u
.binary
.left
);
216 free_pattern_expr(x
->u
.binary
.right
);
222 void free_grep_patterns(struct grep_opt
*opt
)
224 struct grep_pat
*p
, *n
;
226 for (p
= opt
->pattern_list
; p
; p
= n
) {
229 case GREP_PATTERN
: /* atom */
230 case GREP_PATTERN_HEAD
:
231 case GREP_PATTERN_BODY
:
242 free_pattern_expr(opt
->pattern_expression
);
245 static char *end_of_line(char *cp
, unsigned long *left
)
247 unsigned long l
= *left
;
248 while (l
&& *cp
!= '\n') {
256 static int word_char(char ch
)
258 return isalnum(ch
) || ch
== '_';
261 static void show_name(struct grep_opt
*opt
, const char *name
)
263 printf("%s%c", name
, opt
->null_following_name
? '\0' : '\n');
267 static int fixmatch(const char *pattern
, char *line
, int ignore_case
, regmatch_t
*match
)
271 hit
= strcasestr(line
, pattern
);
273 hit
= strstr(line
, pattern
);
276 match
->rm_so
= match
->rm_eo
= -1;
280 match
->rm_so
= hit
- line
;
281 match
->rm_eo
= match
->rm_so
+ strlen(pattern
);
286 static int strip_timestamp(char *bol
, char **eol_p
)
291 while (bol
< --eol
) {
307 { "committer ", 10 },
310 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
311 enum grep_context ctx
,
312 regmatch_t
*pmatch
, int eflags
)
316 const char *start
= bol
;
318 if ((p
->token
!= GREP_PATTERN
) &&
319 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
322 if (p
->token
== GREP_PATTERN_HEAD
) {
325 assert(p
->field
< ARRAY_SIZE(header_field
));
326 field
= header_field
[p
->field
].field
;
327 len
= header_field
[p
->field
].len
;
328 if (strncmp(bol
, field
, len
))
331 saved_ch
= strip_timestamp(bol
, &eol
);
336 hit
= !fixmatch(p
->pattern
, bol
, p
->ignore_case
, pmatch
);
338 hit
= !regexec(&p
->regexp
, bol
, 1, pmatch
, eflags
);
340 if (hit
&& p
->word_regexp
) {
341 if ((pmatch
[0].rm_so
< 0) ||
342 (eol
- bol
) < pmatch
[0].rm_so
||
343 (pmatch
[0].rm_eo
< 0) ||
344 (eol
- bol
) < pmatch
[0].rm_eo
)
345 die("regexp returned nonsense");
347 /* Match beginning must be either beginning of the
348 * line, or at word boundary (i.e. the last char must
349 * not be a word char). Similarly, match end must be
350 * either end of the line, or at word boundary
351 * (i.e. the next char must not be a word char).
353 if ( ((pmatch
[0].rm_so
== 0) ||
354 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
355 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
356 !word_char(bol
[pmatch
[0].rm_eo
])) )
361 /* Words consist of at least one character. */
362 if (pmatch
->rm_so
== pmatch
->rm_eo
)
365 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
366 /* There could be more than one match on the
367 * line, and the first match might not be
368 * strict word match. But later ones could be!
369 * Forward to the next possible start, i.e. the
370 * next position following a non-word char.
372 bol
= pmatch
[0].rm_so
+ bol
+ 1;
373 while (word_char(bol
[-1]) && bol
< eol
)
375 eflags
|= REG_NOTBOL
;
380 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
383 pmatch
[0].rm_so
+= bol
- start
;
384 pmatch
[0].rm_eo
+= bol
- start
;
389 static int match_expr_eval(struct grep_expr
*x
, char *bol
, char *eol
,
390 enum grep_context ctx
, int collect_hits
)
396 die("Not a valid grep expression");
399 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
, &match
, 0);
402 h
= !match_expr_eval(x
->u
.unary
, bol
, eol
, ctx
, 0);
405 if (!match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0))
407 h
= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
411 return (match_expr_eval(x
->u
.binary
.left
,
413 match_expr_eval(x
->u
.binary
.right
,
415 h
= match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
416 x
->u
.binary
.left
->hit
|= h
;
417 h
|= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
420 die("Unexpected node type (internal error) %d", x
->node
);
427 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
428 enum grep_context ctx
, int collect_hits
)
430 struct grep_expr
*x
= opt
->pattern_expression
;
431 return match_expr_eval(x
, bol
, eol
, ctx
, collect_hits
);
434 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
435 enum grep_context ctx
, int collect_hits
)
441 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
443 /* we do not call with collect_hits without being extended */
444 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
445 if (match_one_pattern(p
, bol
, eol
, ctx
, &match
, 0))
451 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
452 enum grep_context ctx
,
453 regmatch_t
*pmatch
, int eflags
)
457 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
459 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
461 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
462 if (match
.rm_so
> pmatch
->rm_so
)
464 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
467 pmatch
->rm_so
= match
.rm_so
;
468 pmatch
->rm_eo
= match
.rm_eo
;
472 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
473 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
478 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
480 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
482 case GREP_PATTERN
: /* atom */
483 case GREP_PATTERN_HEAD
:
484 case GREP_PATTERN_BODY
:
485 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
496 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
497 const char *name
, unsigned lno
, char sign
)
499 int rest
= eol
- bol
;
501 if (opt
->pre_context
|| opt
->post_context
) {
502 if (opt
->last_shown
== 0) {
503 if (opt
->show_hunk_mark
)
504 fputs("--\n", stdout
);
506 opt
->show_hunk_mark
= 1;
507 } else if (lno
> opt
->last_shown
+ 1)
508 fputs("--\n", stdout
);
510 opt
->last_shown
= lno
;
512 if (opt
->null_following_name
)
515 printf("%s%c", name
, sign
);
517 printf("%d%c", lno
, sign
);
520 enum grep_context ctx
= GREP_CONTEXT_BODY
;
525 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
526 if (match
.rm_so
== match
.rm_eo
)
528 printf("%.*s%s%.*s%s",
529 (int)match
.rm_so
, bol
,
531 (int)(match
.rm_eo
- match
.rm_so
), bol
+ match
.rm_so
,
539 printf("%.*s\n", rest
, bol
);
542 static int match_funcname(struct grep_opt
*opt
, char *bol
, char *eol
)
544 xdemitconf_t
*xecfg
= opt
->priv
;
545 if (xecfg
&& xecfg
->find_func
) {
547 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
548 xecfg
->find_func_priv
) >= 0;
553 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
558 static void show_funcname_line(struct grep_opt
*opt
, const char *name
,
559 char *buf
, char *bol
, unsigned lno
)
564 while (bol
> buf
&& bol
[-1] != '\n')
568 if (lno
<= opt
->last_shown
)
571 if (match_funcname(opt
, bol
, eol
)) {
572 show_line(opt
, bol
, eol
, name
, lno
, '=');
578 static void show_pre_context(struct grep_opt
*opt
, const char *name
, char *buf
,
579 char *bol
, unsigned lno
)
581 unsigned cur
= lno
, from
= 1, funcname_lno
= 0;
582 int funcname_needed
= opt
->funcname
;
584 if (opt
->pre_context
< lno
)
585 from
= lno
- opt
->pre_context
;
586 if (from
<= opt
->last_shown
)
587 from
= opt
->last_shown
+ 1;
590 while (bol
> buf
&& cur
> from
) {
593 while (bol
> buf
&& bol
[-1] != '\n')
596 if (funcname_needed
&& match_funcname(opt
, bol
, eol
)) {
602 /* We need to look even further back to find a function signature. */
603 if (opt
->funcname
&& funcname_needed
)
604 show_funcname_line(opt
, name
, buf
, bol
, cur
);
608 char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
612 show_line(opt
, bol
, eol
, name
, cur
, sign
);
618 static int grep_buffer_1(struct grep_opt
*opt
, const char *name
,
619 char *buf
, unsigned long size
, int collect_hits
)
622 unsigned long left
= size
;
624 unsigned last_hit
= 0;
625 int binary_match_only
= 0;
627 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
632 if (buffer_is_binary(buf
, size
)) {
633 switch (opt
->binary
) {
634 case GREP_BINARY_DEFAULT
:
635 binary_match_only
= 1;
637 case GREP_BINARY_NOMATCH
:
638 return 0; /* Assume unmatch */
645 memset(&xecfg
, 0, sizeof(xecfg
));
646 if (opt
->funcname
&& !opt
->unmatch_name_only
&& !opt
->status_only
&&
647 !opt
->name_only
&& !binary_match_only
&& !collect_hits
) {
648 struct userdiff_driver
*drv
= userdiff_find_by_path(name
);
649 if (drv
&& drv
->funcname
.pattern
) {
650 const struct userdiff_funcname
*pe
= &drv
->funcname
;
651 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
660 eol
= end_of_line(bol
, &left
);
664 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
665 ctx
= GREP_CONTEXT_BODY
;
667 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
673 /* "grep -v -e foo -e bla" should list lines
674 * that do not have either, so inversion should
679 if (opt
->unmatch_name_only
) {
686 if (opt
->status_only
)
688 if (binary_match_only
) {
689 printf("Binary file %s matches\n", name
);
692 if (opt
->name_only
) {
693 show_name(opt
, name
);
696 /* Hit at this line. If we haven't shown the
697 * pre-context lines, we would need to show them.
698 * When asked to do "count", this still show
699 * the context which is nonsense, but the user
700 * deserves to get that ;-).
702 if (opt
->pre_context
)
703 show_pre_context(opt
, name
, buf
, bol
, lno
);
704 else if (opt
->funcname
)
705 show_funcname_line(opt
, name
, buf
, bol
, lno
);
707 show_line(opt
, bol
, eol
, name
, lno
, ':');
711 lno
<= last_hit
+ opt
->post_context
) {
712 /* If the last hit is within the post context,
713 * we need to show this line.
715 show_line(opt
, bol
, eol
, name
, lno
, '-');
729 if (opt
->status_only
)
731 if (opt
->unmatch_name_only
) {
732 /* We did not see any hit, so we want to show this */
733 show_name(opt
, name
);
737 xdiff_clear_find_func(&xecfg
);
741 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
742 * which feels mostly useless but sometimes useful. Maybe
743 * make it another option? For now suppress them.
745 if (opt
->count
&& count
)
746 printf("%s%c%u\n", name
,
747 opt
->null_following_name
? '\0' : ':', count
);
751 static void clr_hit_marker(struct grep_expr
*x
)
753 /* All-hit markers are meaningful only at the very top level
758 if (x
->node
!= GREP_NODE_OR
)
760 x
->u
.binary
.left
->hit
= 0;
761 x
= x
->u
.binary
.right
;
765 static int chk_hit_marker(struct grep_expr
*x
)
767 /* Top level nodes have hit markers. See if they all are hits */
769 if (x
->node
!= GREP_NODE_OR
)
771 if (!x
->u
.binary
.left
->hit
)
773 x
= x
->u
.binary
.right
;
777 int grep_buffer(struct grep_opt
*opt
, const char *name
, char *buf
, unsigned long size
)
780 * we do not have to do the two-pass grep when we do not check
781 * buffer-wide "all-match".
784 return grep_buffer_1(opt
, name
, buf
, size
, 0);
786 /* Otherwise the toplevel "or" terms hit a bit differently.
787 * We first clear hit markers from them.
789 clr_hit_marker(opt
->pattern_expression
);
790 grep_buffer_1(opt
, name
, buf
, size
, 1);
792 if (!chk_hit_marker(opt
->pattern_expression
))
795 return grep_buffer_1(opt
, name
, buf
, size
, 0);