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 if (opt
->fixed
|| is_fixed(p
->pattern
))
44 if (opt
->regflags
& REG_ICASE
)
49 err
= regcomp(&p
->regexp
, p
->pattern
, opt
->regflags
);
54 sprintf(where
, "In '%s' at %d, ",
57 sprintf(where
, "%s, ", p
->origin
);
60 regerror(err
, &p
->regexp
, errbuf
, 1024);
62 die("%s'%s': %s", where
, p
->pattern
, errbuf
);
66 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
67 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
74 case GREP_PATTERN
: /* atom */
75 case GREP_PATTERN_HEAD
:
76 case GREP_PATTERN_BODY
:
77 x
= xcalloc(1, sizeof (struct grep_expr
));
78 x
->node
= GREP_NODE_ATOM
;
84 x
= compile_pattern_or(list
);
87 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
88 die("unmatched parenthesis");
89 *list
= (*list
)->next
;
96 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
105 die("--not not followed by pattern expression");
107 x
= xcalloc(1, sizeof (struct grep_expr
));
108 x
->node
= GREP_NODE_NOT
;
109 x
->u
.unary
= compile_pattern_not(list
);
111 die("--not followed by non pattern expression");
114 return compile_pattern_atom(list
);
118 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
121 struct grep_expr
*x
, *y
, *z
;
123 x
= compile_pattern_not(list
);
125 if (p
&& p
->token
== GREP_AND
) {
127 die("--and not followed by pattern expression");
129 y
= compile_pattern_and(list
);
131 die("--and not followed by pattern expression");
132 z
= xcalloc(1, sizeof (struct grep_expr
));
133 z
->node
= GREP_NODE_AND
;
134 z
->u
.binary
.left
= x
;
135 z
->u
.binary
.right
= y
;
141 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
144 struct grep_expr
*x
, *y
, *z
;
146 x
= compile_pattern_and(list
);
148 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
149 y
= compile_pattern_or(list
);
151 die("not a pattern expression %s", p
->pattern
);
152 z
= xcalloc(1, sizeof (struct grep_expr
));
153 z
->node
= GREP_NODE_OR
;
154 z
->u
.binary
.left
= x
;
155 z
->u
.binary
.right
= y
;
161 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
163 return compile_pattern_or(list
);
166 void compile_grep_patterns(struct grep_opt
*opt
)
173 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
175 case GREP_PATTERN
: /* atom */
176 case GREP_PATTERN_HEAD
:
177 case GREP_PATTERN_BODY
:
178 compile_regexp(p
, opt
);
189 /* Then bundle them up in an expression.
190 * A classic recursive descent parser would do.
192 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_line(struct grep_opt
*opt
, const char *bol
, const char *eol
,
255 const char *name
, unsigned lno
, char sign
)
257 if (opt
->null_following_name
)
260 printf("%s%c", name
, sign
);
262 printf("%d%c", lno
, sign
);
263 printf("%.*s\n", (int)(eol
-bol
), bol
);
266 static void show_name(struct grep_opt
*opt
, const char *name
)
268 printf("%s%c", name
, opt
->null_following_name
? '\0' : '\n');
271 static int fixmatch(const char *pattern
, char *line
, regmatch_t
*match
)
273 char *hit
= strstr(line
, pattern
);
275 match
->rm_so
= match
->rm_eo
= -1;
279 match
->rm_so
= hit
- line
;
280 match
->rm_eo
= match
->rm_so
+ strlen(pattern
);
285 static int strip_timestamp(char *bol
, char **eol_p
)
290 while (bol
< --eol
) {
306 { "committer ", 10 },
309 static int match_one_pattern(struct grep_opt
*opt
, struct grep_pat
*p
, char *bol
, char *eol
, enum grep_context ctx
)
313 regmatch_t pmatch
[10];
315 if ((p
->token
!= GREP_PATTERN
) &&
316 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
319 if (p
->token
== GREP_PATTERN_HEAD
) {
322 assert(p
->field
< ARRAY_SIZE(header_field
));
323 field
= header_field
[p
->field
].field
;
324 len
= header_field
[p
->field
].len
;
325 if (strncmp(bol
, field
, len
))
328 saved_ch
= strip_timestamp(bol
, &eol
);
333 regex_t
*exp
= &p
->regexp
;
334 hit
= !regexec(exp
, bol
, ARRAY_SIZE(pmatch
),
338 hit
= !fixmatch(p
->pattern
, bol
, pmatch
);
341 if (hit
&& opt
->word_regexp
) {
342 if ((pmatch
[0].rm_so
< 0) ||
343 (eol
- bol
) <= pmatch
[0].rm_so
||
344 (pmatch
[0].rm_eo
< 0) ||
345 (eol
- bol
) < pmatch
[0].rm_eo
)
346 die("regexp returned nonsense");
348 /* Match beginning must be either beginning of the
349 * line, or at word boundary (i.e. the last char must
350 * not be a word char). Similarly, match end must be
351 * either end of the line, or at word boundary
352 * (i.e. the next char must not be a word char).
354 if ( ((pmatch
[0].rm_so
== 0) ||
355 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
356 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
357 !word_char(bol
[pmatch
[0].rm_eo
])) )
362 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
363 /* There could be more than one match on the
364 * line, and the first match might not be
365 * strict word match. But later ones could be!
366 * Forward to the next possible start, i.e. the
367 * next position following a non-word char.
369 bol
= pmatch
[0].rm_so
+ bol
+ 1;
370 while (word_char(bol
[-1]) && bol
< eol
)
376 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
381 static int match_expr_eval(struct grep_opt
*o
,
383 char *bol
, char *eol
,
384 enum grep_context ctx
,
391 h
= match_one_pattern(o
, x
->u
.atom
, bol
, eol
, ctx
);
394 h
= !match_expr_eval(o
, x
->u
.unary
, bol
, eol
, ctx
, 0);
398 return (match_expr_eval(o
, x
->u
.binary
.left
,
400 match_expr_eval(o
, x
->u
.binary
.right
,
402 h
= match_expr_eval(o
, x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
403 h
&= match_expr_eval(o
, x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
407 return (match_expr_eval(o
, x
->u
.binary
.left
,
409 match_expr_eval(o
, x
->u
.binary
.right
,
411 h
= match_expr_eval(o
, x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
412 x
->u
.binary
.left
->hit
|= h
;
413 h
|= match_expr_eval(o
, x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
416 die("Unexpected node type (internal error) %d", x
->node
);
423 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
424 enum grep_context ctx
, int collect_hits
)
426 struct grep_expr
*x
= opt
->pattern_expression
;
427 return match_expr_eval(opt
, x
, bol
, eol
, ctx
, collect_hits
);
430 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
431 enum grep_context ctx
, int collect_hits
)
435 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
437 /* we do not call with collect_hits without being extended */
438 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
439 if (match_one_pattern(opt
, p
, bol
, eol
, ctx
))
445 static int grep_buffer_1(struct grep_opt
*opt
, const char *name
,
446 char *buf
, unsigned long size
, int collect_hits
)
449 unsigned long left
= size
;
451 struct pre_context_line
{
454 } *prev
= NULL
, *pcl
;
455 unsigned last_hit
= 0;
456 unsigned last_shown
= 0;
457 int binary_match_only
= 0;
458 const char *hunk_mark
= "";
460 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
462 if (buffer_is_binary(buf
, size
)) {
463 switch (opt
->binary
) {
464 case GREP_BINARY_DEFAULT
:
465 binary_match_only
= 1;
467 case GREP_BINARY_NOMATCH
:
468 return 0; /* Assume unmatch */
475 if (opt
->pre_context
)
476 prev
= xcalloc(opt
->pre_context
, sizeof(*prev
));
477 if (opt
->pre_context
|| opt
->post_context
)
484 eol
= end_of_line(bol
, &left
);
488 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
489 ctx
= GREP_CONTEXT_BODY
;
491 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
497 /* "grep -v -e foo -e bla" should list lines
498 * that do not have either, so inversion should
503 if (opt
->unmatch_name_only
) {
510 if (opt
->status_only
)
512 if (binary_match_only
) {
513 printf("Binary file %s matches\n", name
);
516 if (opt
->name_only
) {
517 show_name(opt
, name
);
520 /* Hit at this line. If we haven't shown the
521 * pre-context lines, we would need to show them.
522 * When asked to do "count", this still show
523 * the context which is nonsense, but the user
524 * deserves to get that ;-).
526 if (opt
->pre_context
) {
528 if (opt
->pre_context
< lno
)
529 from
= lno
- opt
->pre_context
;
532 if (from
<= last_shown
)
533 from
= last_shown
+ 1;
534 if (last_shown
&& from
!= last_shown
+ 1)
535 fputs(hunk_mark
, stdout
);
537 pcl
= &prev
[lno
-from
-1];
538 show_line(opt
, pcl
->bol
, pcl
->eol
,
544 if (last_shown
&& lno
!= last_shown
+ 1)
545 fputs(hunk_mark
, stdout
);
547 show_line(opt
, bol
, eol
, name
, lno
, ':');
548 last_shown
= last_hit
= lno
;
551 lno
<= last_hit
+ opt
->post_context
) {
552 /* If the last hit is within the post context,
553 * we need to show this line.
555 if (last_shown
&& lno
!= last_shown
+ 1)
556 fputs(hunk_mark
, stdout
);
557 show_line(opt
, bol
, eol
, name
, lno
, '-');
560 if (opt
->pre_context
) {
561 memmove(prev
+1, prev
,
562 (opt
->pre_context
-1) * sizeof(*prev
));
579 if (opt
->status_only
)
581 if (opt
->unmatch_name_only
) {
582 /* We did not see any hit, so we want to show this */
583 show_name(opt
, name
);
588 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
589 * which feels mostly useless but sometimes useful. Maybe
590 * make it another option? For now suppress them.
592 if (opt
->count
&& count
)
593 printf("%s%c%u\n", name
,
594 opt
->null_following_name
? '\0' : ':', count
);
598 static void clr_hit_marker(struct grep_expr
*x
)
600 /* All-hit markers are meaningful only at the very top level
605 if (x
->node
!= GREP_NODE_OR
)
607 x
->u
.binary
.left
->hit
= 0;
608 x
= x
->u
.binary
.right
;
612 static int chk_hit_marker(struct grep_expr
*x
)
614 /* Top level nodes have hit markers. See if they all are hits */
616 if (x
->node
!= GREP_NODE_OR
)
618 if (!x
->u
.binary
.left
->hit
)
620 x
= x
->u
.binary
.right
;
624 int grep_buffer(struct grep_opt
*opt
, const char *name
, char *buf
, unsigned long size
)
627 * we do not have to do the two-pass grep when we do not check
628 * buffer-wide "all-match".
631 return grep_buffer_1(opt
, name
, buf
, size
, 0);
633 /* Otherwise the toplevel "or" terms hit a bit differently.
634 * We first clear hit markers from them.
636 clr_hit_marker(opt
->pattern_expression
);
637 grep_buffer_1(opt
, name
, buf
, size
, 1);
639 if (!chk_hit_marker(opt
->pattern_expression
))
642 return grep_buffer_1(opt
, name
, buf
, size
, 0);