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
)
76 case GREP_PATTERN
: /* atom */
77 case GREP_PATTERN_HEAD
:
78 case GREP_PATTERN_BODY
:
79 x
= xcalloc(1, sizeof (struct grep_expr
));
80 x
->node
= GREP_NODE_ATOM
;
86 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
)
107 die("--not not followed by pattern expression");
109 x
= xcalloc(1, sizeof (struct grep_expr
));
110 x
->node
= GREP_NODE_NOT
;
111 x
->u
.unary
= compile_pattern_not(list
);
113 die("--not followed by non pattern expression");
116 return compile_pattern_atom(list
);
120 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
123 struct grep_expr
*x
, *y
, *z
;
125 x
= compile_pattern_not(list
);
127 if (p
&& p
->token
== GREP_AND
) {
129 die("--and not followed by pattern expression");
131 y
= compile_pattern_and(list
);
133 die("--and not followed by pattern expression");
134 z
= xcalloc(1, sizeof (struct grep_expr
));
135 z
->node
= GREP_NODE_AND
;
136 z
->u
.binary
.left
= x
;
137 z
->u
.binary
.right
= y
;
143 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
146 struct grep_expr
*x
, *y
, *z
;
148 x
= compile_pattern_and(list
);
150 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
151 y
= compile_pattern_or(list
);
153 die("not a pattern expression %s", p
->pattern
);
154 z
= xcalloc(1, sizeof (struct grep_expr
));
155 z
->node
= GREP_NODE_OR
;
156 z
->u
.binary
.left
= x
;
157 z
->u
.binary
.right
= y
;
163 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
165 return compile_pattern_or(list
);
168 void compile_grep_patterns(struct grep_opt
*opt
)
175 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
177 case GREP_PATTERN
: /* atom */
178 case GREP_PATTERN_HEAD
:
179 case GREP_PATTERN_BODY
:
180 compile_regexp(p
, opt
);
191 /* Then bundle them up in an expression.
192 * A classic recursive descent parser would do.
194 p
= opt
->pattern_list
;
195 opt
->pattern_expression
= compile_pattern_expr(&p
);
197 die("incomplete pattern expression: %s", p
->pattern
);
200 static void free_pattern_expr(struct grep_expr
*x
)
206 free_pattern_expr(x
->u
.unary
);
210 free_pattern_expr(x
->u
.binary
.left
);
211 free_pattern_expr(x
->u
.binary
.right
);
217 void free_grep_patterns(struct grep_opt
*opt
)
219 struct grep_pat
*p
, *n
;
221 for (p
= opt
->pattern_list
; p
; p
= n
) {
224 case GREP_PATTERN
: /* atom */
225 case GREP_PATTERN_HEAD
:
226 case GREP_PATTERN_BODY
:
237 free_pattern_expr(opt
->pattern_expression
);
240 static char *end_of_line(char *cp
, unsigned long *left
)
242 unsigned long l
= *left
;
243 while (l
&& *cp
!= '\n') {
251 static int word_char(char ch
)
253 return isalnum(ch
) || ch
== '_';
256 static void show_line(struct grep_opt
*opt
, const char *bol
, const char *eol
,
257 const char *name
, unsigned lno
, char sign
)
259 if (opt
->null_following_name
)
262 printf("%s%c", name
, sign
);
264 printf("%d%c", lno
, sign
);
265 printf("%.*s\n", (int)(eol
-bol
), bol
);
268 static void show_name(struct grep_opt
*opt
, const char *name
)
270 printf("%s%c", name
, opt
->null_following_name
? '\0' : '\n');
273 static int fixmatch(const char *pattern
, char *line
, regmatch_t
*match
)
275 char *hit
= strstr(line
, pattern
);
277 match
->rm_so
= match
->rm_eo
= -1;
281 match
->rm_so
= hit
- line
;
282 match
->rm_eo
= match
->rm_so
+ strlen(pattern
);
287 static int strip_timestamp(char *bol
, char **eol_p
)
292 while (bol
< --eol
) {
308 { "committer ", 10 },
311 static int match_one_pattern(struct grep_opt
*opt
, struct grep_pat
*p
, char *bol
, char *eol
, enum grep_context ctx
)
315 regmatch_t pmatch
[10];
317 if ((p
->token
!= GREP_PATTERN
) &&
318 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
321 if (p
->token
== GREP_PATTERN_HEAD
) {
324 assert(p
->field
< ARRAY_SIZE(header_field
));
325 field
= header_field
[p
->field
].field
;
326 len
= header_field
[p
->field
].len
;
327 if (strncmp(bol
, field
, len
))
330 saved_ch
= strip_timestamp(bol
, &eol
);
335 regex_t
*exp
= &p
->regexp
;
336 hit
= !regexec(exp
, bol
, ARRAY_SIZE(pmatch
),
340 hit
= !fixmatch(p
->pattern
, bol
, pmatch
);
343 if (hit
&& opt
->word_regexp
) {
344 if ((pmatch
[0].rm_so
< 0) ||
345 (eol
- bol
) <= pmatch
[0].rm_so
||
346 (pmatch
[0].rm_eo
< 0) ||
347 (eol
- bol
) < pmatch
[0].rm_eo
)
348 die("regexp returned nonsense");
350 /* Match beginning must be either beginning of the
351 * line, or at word boundary (i.e. the last char must
352 * not be a word char). Similarly, match end must be
353 * either end of the line, or at word boundary
354 * (i.e. the next char must not be a word char).
356 if ( ((pmatch
[0].rm_so
== 0) ||
357 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
358 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
359 !word_char(bol
[pmatch
[0].rm_eo
])) )
364 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
365 /* There could be more than one match on the
366 * line, and the first match might not be
367 * strict word match. But later ones could be!
368 * Forward to the next possible start, i.e. the
369 * next position following a non-word char.
371 bol
= pmatch
[0].rm_so
+ bol
+ 1;
372 while (word_char(bol
[-1]) && bol
< eol
)
378 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
383 static int match_expr_eval(struct grep_opt
*o
,
385 char *bol
, char *eol
,
386 enum grep_context ctx
,
392 die("Not a valid grep expression");
395 h
= match_one_pattern(o
, x
->u
.atom
, bol
, eol
, ctx
);
398 h
= !match_expr_eval(o
, x
->u
.unary
, bol
, eol
, ctx
, 0);
402 return (match_expr_eval(o
, x
->u
.binary
.left
,
404 match_expr_eval(o
, x
->u
.binary
.right
,
406 h
= match_expr_eval(o
, x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
407 h
&= match_expr_eval(o
, x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
411 return (match_expr_eval(o
, x
->u
.binary
.left
,
413 match_expr_eval(o
, x
->u
.binary
.right
,
415 h
= match_expr_eval(o
, x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
416 x
->u
.binary
.left
->hit
|= h
;
417 h
|= match_expr_eval(o
, 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(opt
, 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
)
439 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
441 /* we do not call with collect_hits without being extended */
442 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
443 if (match_one_pattern(opt
, p
, bol
, eol
, ctx
))
449 static int grep_buffer_1(struct grep_opt
*opt
, const char *name
,
450 char *buf
, unsigned long size
, int collect_hits
)
453 unsigned long left
= size
;
455 struct pre_context_line
{
458 } *prev
= NULL
, *pcl
;
459 unsigned last_hit
= 0;
460 unsigned last_shown
= 0;
461 int binary_match_only
= 0;
462 const char *hunk_mark
= "";
464 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
466 if (buffer_is_binary(buf
, size
)) {
467 switch (opt
->binary
) {
468 case GREP_BINARY_DEFAULT
:
469 binary_match_only
= 1;
471 case GREP_BINARY_NOMATCH
:
472 return 0; /* Assume unmatch */
479 if (opt
->pre_context
)
480 prev
= xcalloc(opt
->pre_context
, sizeof(*prev
));
481 if (opt
->pre_context
|| opt
->post_context
)
488 eol
= end_of_line(bol
, &left
);
492 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
493 ctx
= GREP_CONTEXT_BODY
;
495 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
501 /* "grep -v -e foo -e bla" should list lines
502 * that do not have either, so inversion should
507 if (opt
->unmatch_name_only
) {
514 if (opt
->status_only
)
516 if (binary_match_only
) {
517 printf("Binary file %s matches\n", name
);
520 if (opt
->name_only
) {
521 show_name(opt
, name
);
524 /* Hit at this line. If we haven't shown the
525 * pre-context lines, we would need to show them.
526 * When asked to do "count", this still show
527 * the context which is nonsense, but the user
528 * deserves to get that ;-).
530 if (opt
->pre_context
) {
532 if (opt
->pre_context
< lno
)
533 from
= lno
- opt
->pre_context
;
536 if (from
<= last_shown
)
537 from
= last_shown
+ 1;
538 if (last_shown
&& from
!= last_shown
+ 1)
539 fputs(hunk_mark
, stdout
);
541 pcl
= &prev
[lno
-from
-1];
542 show_line(opt
, pcl
->bol
, pcl
->eol
,
548 if (last_shown
&& lno
!= last_shown
+ 1)
549 fputs(hunk_mark
, stdout
);
551 show_line(opt
, bol
, eol
, name
, lno
, ':');
552 last_shown
= last_hit
= lno
;
555 lno
<= last_hit
+ opt
->post_context
) {
556 /* If the last hit is within the post context,
557 * we need to show this line.
559 if (last_shown
&& lno
!= last_shown
+ 1)
560 fputs(hunk_mark
, stdout
);
561 show_line(opt
, bol
, eol
, name
, lno
, '-');
564 if (opt
->pre_context
) {
565 memmove(prev
+1, prev
,
566 (opt
->pre_context
-1) * sizeof(*prev
));
583 if (opt
->status_only
)
585 if (opt
->unmatch_name_only
) {
586 /* We did not see any hit, so we want to show this */
587 show_name(opt
, name
);
592 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
593 * which feels mostly useless but sometimes useful. Maybe
594 * make it another option? For now suppress them.
596 if (opt
->count
&& count
)
597 printf("%s%c%u\n", name
,
598 opt
->null_following_name
? '\0' : ':', count
);
602 static void clr_hit_marker(struct grep_expr
*x
)
604 /* All-hit markers are meaningful only at the very top level
609 if (x
->node
!= GREP_NODE_OR
)
611 x
->u
.binary
.left
->hit
= 0;
612 x
= x
->u
.binary
.right
;
616 static int chk_hit_marker(struct grep_expr
*x
)
618 /* Top level nodes have hit markers. See if they all are hits */
620 if (x
->node
!= GREP_NODE_OR
)
622 if (!x
->u
.binary
.left
->hit
)
624 x
= x
->u
.binary
.right
;
628 int grep_buffer(struct grep_opt
*opt
, const char *name
, char *buf
, unsigned long size
)
631 * we do not have to do the two-pass grep when we do not check
632 * buffer-wide "all-match".
635 return grep_buffer_1(opt
, name
, buf
, size
, 0);
637 /* Otherwise the toplevel "or" terms hit a bit differently.
638 * We first clear hit markers from them.
640 clr_hit_marker(opt
->pattern_expression
);
641 grep_buffer_1(opt
, name
, buf
, size
, 1);
643 if (!chk_hit_marker(opt
->pattern_expression
))
646 return grep_buffer_1(opt
, name
, buf
, size
, 0);