3 #include "xdiff-interface.h"
5 void append_header_grep_pattern(struct grep_opt
*opt
, enum grep_header_field field
, const char *pat
)
7 struct grep_pat
*p
= xcalloc(1, sizeof(*p
));
11 p
->token
= GREP_PATTERN_HEAD
;
13 *opt
->pattern_tail
= p
;
14 opt
->pattern_tail
= &p
->next
;
18 void append_grep_pattern(struct grep_opt
*opt
, const char *pat
,
19 const char *origin
, int no
, enum grep_pat_token t
)
21 struct grep_pat
*p
= xcalloc(1, sizeof(*p
));
26 *opt
->pattern_tail
= p
;
27 opt
->pattern_tail
= &p
->next
;
31 static int is_fixed(const char *s
)
33 while (*s
&& !is_regex_special(*s
))
38 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
42 p
->word_regexp
= opt
->word_regexp
;
44 if (opt
->fixed
|| is_fixed(p
->pattern
))
46 if (opt
->regflags
& REG_ICASE
)
51 err
= regcomp(&p
->regexp
, p
->pattern
, opt
->regflags
);
56 sprintf(where
, "In '%s' at %d, ",
59 sprintf(where
, "%s, ", p
->origin
);
62 regerror(err
, &p
->regexp
, errbuf
, 1024);
64 die("%s'%s': %s", where
, p
->pattern
, errbuf
);
68 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
69 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
78 case GREP_PATTERN
: /* atom */
79 case GREP_PATTERN_HEAD
:
80 case GREP_PATTERN_BODY
:
81 x
= xcalloc(1, sizeof (struct grep_expr
));
82 x
->node
= GREP_NODE_ATOM
;
88 x
= compile_pattern_or(list
);
89 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
90 die("unmatched parenthesis");
91 *list
= (*list
)->next
;
98 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
109 die("--not not followed by pattern expression");
111 x
= xcalloc(1, sizeof (struct grep_expr
));
112 x
->node
= GREP_NODE_NOT
;
113 x
->u
.unary
= compile_pattern_not(list
);
115 die("--not followed by non pattern expression");
118 return compile_pattern_atom(list
);
122 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
125 struct grep_expr
*x
, *y
, *z
;
127 x
= compile_pattern_not(list
);
129 if (p
&& p
->token
== GREP_AND
) {
131 die("--and not followed by pattern expression");
133 y
= compile_pattern_and(list
);
135 die("--and not followed by pattern expression");
136 z
= xcalloc(1, sizeof (struct grep_expr
));
137 z
->node
= GREP_NODE_AND
;
138 z
->u
.binary
.left
= x
;
139 z
->u
.binary
.right
= y
;
145 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
148 struct grep_expr
*x
, *y
, *z
;
150 x
= compile_pattern_and(list
);
152 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
153 y
= compile_pattern_or(list
);
155 die("not a pattern expression %s", p
->pattern
);
156 z
= xcalloc(1, sizeof (struct grep_expr
));
157 z
->node
= GREP_NODE_OR
;
158 z
->u
.binary
.left
= x
;
159 z
->u
.binary
.right
= y
;
165 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
167 return compile_pattern_or(list
);
170 void compile_grep_patterns(struct grep_opt
*opt
)
177 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
179 case GREP_PATTERN
: /* atom */
180 case GREP_PATTERN_HEAD
:
181 case GREP_PATTERN_BODY
:
182 compile_regexp(p
, opt
);
193 /* Then bundle them up in an expression.
194 * A classic recursive descent parser would do.
196 p
= opt
->pattern_list
;
198 opt
->pattern_expression
= compile_pattern_expr(&p
);
200 die("incomplete pattern expression: %s", p
->pattern
);
203 static void free_pattern_expr(struct grep_expr
*x
)
209 free_pattern_expr(x
->u
.unary
);
213 free_pattern_expr(x
->u
.binary
.left
);
214 free_pattern_expr(x
->u
.binary
.right
);
220 void free_grep_patterns(struct grep_opt
*opt
)
222 struct grep_pat
*p
, *n
;
224 for (p
= opt
->pattern_list
; p
; p
= n
) {
227 case GREP_PATTERN
: /* atom */
228 case GREP_PATTERN_HEAD
:
229 case GREP_PATTERN_BODY
:
240 free_pattern_expr(opt
->pattern_expression
);
243 static char *end_of_line(char *cp
, unsigned long *left
)
245 unsigned long l
= *left
;
246 while (l
&& *cp
!= '\n') {
254 static int word_char(char ch
)
256 return isalnum(ch
) || ch
== '_';
259 static void show_name(struct grep_opt
*opt
, const char *name
)
261 printf("%s%c", name
, opt
->null_following_name
? '\0' : '\n');
264 static int fixmatch(const char *pattern
, char *line
, regmatch_t
*match
)
266 char *hit
= strstr(line
, pattern
);
268 match
->rm_so
= match
->rm_eo
= -1;
272 match
->rm_so
= hit
- line
;
273 match
->rm_eo
= match
->rm_so
+ strlen(pattern
);
278 static int strip_timestamp(char *bol
, char **eol_p
)
283 while (bol
< --eol
) {
299 { "committer ", 10 },
302 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
303 enum grep_context ctx
,
304 regmatch_t
*pmatch
, int eflags
)
308 const char *start
= bol
;
310 if ((p
->token
!= GREP_PATTERN
) &&
311 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
314 if (p
->token
== GREP_PATTERN_HEAD
) {
317 assert(p
->field
< ARRAY_SIZE(header_field
));
318 field
= header_field
[p
->field
].field
;
319 len
= header_field
[p
->field
].len
;
320 if (strncmp(bol
, field
, len
))
323 saved_ch
= strip_timestamp(bol
, &eol
);
328 hit
= !fixmatch(p
->pattern
, bol
, pmatch
);
330 hit
= !regexec(&p
->regexp
, bol
, 1, pmatch
, eflags
);
332 if (hit
&& p
->word_regexp
) {
333 if ((pmatch
[0].rm_so
< 0) ||
334 (eol
- bol
) < pmatch
[0].rm_so
||
335 (pmatch
[0].rm_eo
< 0) ||
336 (eol
- bol
) < pmatch
[0].rm_eo
)
337 die("regexp returned nonsense");
339 /* Match beginning must be either beginning of the
340 * line, or at word boundary (i.e. the last char must
341 * not be a word char). Similarly, match end must be
342 * either end of the line, or at word boundary
343 * (i.e. the next char must not be a word char).
345 if ( ((pmatch
[0].rm_so
== 0) ||
346 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
347 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
348 !word_char(bol
[pmatch
[0].rm_eo
])) )
353 /* Words consist of at least one character. */
354 if (pmatch
->rm_so
== pmatch
->rm_eo
)
357 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
358 /* There could be more than one match on the
359 * line, and the first match might not be
360 * strict word match. But later ones could be!
361 * Forward to the next possible start, i.e. the
362 * next position following a non-word char.
364 bol
= pmatch
[0].rm_so
+ bol
+ 1;
365 while (word_char(bol
[-1]) && bol
< eol
)
367 eflags
|= REG_NOTBOL
;
372 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
375 pmatch
[0].rm_so
+= bol
- start
;
376 pmatch
[0].rm_eo
+= bol
- start
;
381 static int match_expr_eval(struct grep_expr
*x
, char *bol
, char *eol
,
382 enum grep_context ctx
, int collect_hits
)
388 die("Not a valid grep expression");
391 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
, &match
, 0);
394 h
= !match_expr_eval(x
->u
.unary
, bol
, eol
, ctx
, 0);
397 if (!match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0))
399 h
= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
403 return (match_expr_eval(x
->u
.binary
.left
,
405 match_expr_eval(x
->u
.binary
.right
,
407 h
= match_expr_eval(x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
408 x
->u
.binary
.left
->hit
|= h
;
409 h
|= match_expr_eval(x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
412 die("Unexpected node type (internal error) %d", x
->node
);
419 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
420 enum grep_context ctx
, int collect_hits
)
422 struct grep_expr
*x
= opt
->pattern_expression
;
423 return match_expr_eval(x
, bol
, eol
, ctx
, collect_hits
);
426 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
427 enum grep_context ctx
, int collect_hits
)
433 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
435 /* we do not call with collect_hits without being extended */
436 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
437 if (match_one_pattern(p
, bol
, eol
, ctx
, &match
, 0))
443 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
444 enum grep_context ctx
,
445 regmatch_t
*pmatch
, int eflags
)
449 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
451 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
453 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
454 if (match
.rm_so
> pmatch
->rm_so
)
456 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
459 pmatch
->rm_so
= match
.rm_so
;
460 pmatch
->rm_eo
= match
.rm_eo
;
464 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
465 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
470 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
472 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
474 case GREP_PATTERN
: /* atom */
475 case GREP_PATTERN_HEAD
:
476 case GREP_PATTERN_BODY
:
477 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
488 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
489 const char *name
, unsigned lno
, char sign
)
491 int rest
= eol
- bol
;
493 if (opt
->null_following_name
)
496 printf("%s%c", name
, sign
);
498 printf("%d%c", lno
, sign
);
501 enum grep_context ctx
= GREP_CONTEXT_BODY
;
506 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
507 if (match
.rm_so
== match
.rm_eo
)
509 printf("%.*s%s%.*s%s",
510 (int)match
.rm_so
, bol
,
512 (int)(match
.rm_eo
- match
.rm_so
), bol
+ match
.rm_so
,
520 printf("%.*s\n", rest
, bol
);
523 static int grep_buffer_1(struct grep_opt
*opt
, const char *name
,
524 char *buf
, unsigned long size
, int collect_hits
)
527 unsigned long left
= size
;
529 struct pre_context_line
{
532 } *prev
= NULL
, *pcl
;
533 unsigned last_hit
= 0;
534 unsigned last_shown
= 0;
535 int binary_match_only
= 0;
536 const char *hunk_mark
= "";
538 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
540 if (buffer_is_binary(buf
, size
)) {
541 switch (opt
->binary
) {
542 case GREP_BINARY_DEFAULT
:
543 binary_match_only
= 1;
545 case GREP_BINARY_NOMATCH
:
546 return 0; /* Assume unmatch */
553 if (opt
->pre_context
)
554 prev
= xcalloc(opt
->pre_context
, sizeof(*prev
));
555 if (opt
->pre_context
|| opt
->post_context
)
562 eol
= end_of_line(bol
, &left
);
566 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
567 ctx
= GREP_CONTEXT_BODY
;
569 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
575 /* "grep -v -e foo -e bla" should list lines
576 * that do not have either, so inversion should
581 if (opt
->unmatch_name_only
) {
588 if (opt
->status_only
)
590 if (binary_match_only
) {
591 printf("Binary file %s matches\n", name
);
594 if (opt
->name_only
) {
595 show_name(opt
, name
);
598 /* Hit at this line. If we haven't shown the
599 * pre-context lines, we would need to show them.
600 * When asked to do "count", this still show
601 * the context which is nonsense, but the user
602 * deserves to get that ;-).
604 if (opt
->pre_context
) {
606 if (opt
->pre_context
< lno
)
607 from
= lno
- opt
->pre_context
;
610 if (from
<= last_shown
)
611 from
= last_shown
+ 1;
612 if (last_shown
&& from
!= last_shown
+ 1)
613 fputs(hunk_mark
, stdout
);
615 pcl
= &prev
[lno
-from
-1];
616 show_line(opt
, pcl
->bol
, pcl
->eol
,
622 if (last_shown
&& lno
!= last_shown
+ 1)
623 fputs(hunk_mark
, stdout
);
625 show_line(opt
, bol
, eol
, name
, lno
, ':');
626 last_shown
= last_hit
= lno
;
629 lno
<= last_hit
+ opt
->post_context
) {
630 /* If the last hit is within the post context,
631 * we need to show this line.
633 if (last_shown
&& lno
!= last_shown
+ 1)
634 fputs(hunk_mark
, stdout
);
635 show_line(opt
, bol
, eol
, name
, lno
, '-');
638 if (opt
->pre_context
) {
639 memmove(prev
+1, prev
,
640 (opt
->pre_context
-1) * sizeof(*prev
));
657 if (opt
->status_only
)
659 if (opt
->unmatch_name_only
) {
660 /* We did not see any hit, so we want to show this */
661 show_name(opt
, name
);
666 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
667 * which feels mostly useless but sometimes useful. Maybe
668 * make it another option? For now suppress them.
670 if (opt
->count
&& count
)
671 printf("%s%c%u\n", name
,
672 opt
->null_following_name
? '\0' : ':', count
);
676 static void clr_hit_marker(struct grep_expr
*x
)
678 /* All-hit markers are meaningful only at the very top level
683 if (x
->node
!= GREP_NODE_OR
)
685 x
->u
.binary
.left
->hit
= 0;
686 x
= x
->u
.binary
.right
;
690 static int chk_hit_marker(struct grep_expr
*x
)
692 /* Top level nodes have hit markers. See if they all are hits */
694 if (x
->node
!= GREP_NODE_OR
)
696 if (!x
->u
.binary
.left
->hit
)
698 x
= x
->u
.binary
.right
;
702 int grep_buffer(struct grep_opt
*opt
, const char *name
, char *buf
, unsigned long size
)
705 * we do not have to do the two-pass grep when we do not check
706 * buffer-wide "all-match".
709 return grep_buffer_1(opt
, name
, buf
, size
, 0);
711 /* Otherwise the toplevel "or" terms hit a bit differently.
712 * We first clear hit markers from them.
714 clr_hit_marker(opt
->pattern_expression
);
715 grep_buffer_1(opt
, name
, buf
, size
, 1);
717 if (!chk_hit_marker(opt
->pattern_expression
))
720 return grep_buffer_1(opt
, name
, buf
, size
, 0);