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 isregexspecial(int c
)
33 return isspecial(c
) || c
== '$' || c
== '(' || c
== ')' || c
== '+' ||
34 c
== '.' || c
== '^' || c
== '{' || c
== '|';
37 static int is_fixed(const char *s
)
39 while (!isregexspecial(*s
))
44 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
48 if (opt
->fixed
|| is_fixed(p
->pattern
))
50 if (opt
->regflags
& REG_ICASE
)
55 err
= regcomp(&p
->regexp
, p
->pattern
, opt
->regflags
);
60 sprintf(where
, "In '%s' at %d, ",
63 sprintf(where
, "%s, ", p
->origin
);
66 regerror(err
, &p
->regexp
, errbuf
, 1024);
68 die("%s'%s': %s", where
, p
->pattern
, errbuf
);
72 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
73 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
);
93 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
94 die("unmatched parenthesis");
95 *list
= (*list
)->next
;
102 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
;
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_line(struct grep_opt
*opt
, const char *bol
, const char *eol
,
261 const char *name
, unsigned lno
, char sign
)
263 if (opt
->null_following_name
)
266 printf("%s%c", name
, sign
);
268 printf("%d%c", lno
, sign
);
269 printf("%.*s\n", (int)(eol
-bol
), bol
);
272 static void show_name(struct grep_opt
*opt
, const char *name
)
274 printf("%s%c", name
, opt
->null_following_name
? '\0' : '\n');
277 static int fixmatch(const char *pattern
, char *line
, regmatch_t
*match
)
279 char *hit
= strstr(line
, pattern
);
281 match
->rm_so
= match
->rm_eo
= -1;
285 match
->rm_so
= hit
- line
;
286 match
->rm_eo
= match
->rm_so
+ strlen(pattern
);
291 static int strip_timestamp(char *bol
, char **eol_p
)
296 while (bol
< --eol
) {
312 { "committer ", 10 },
315 static int match_one_pattern(struct grep_opt
*opt
, struct grep_pat
*p
, char *bol
, char *eol
, enum grep_context ctx
)
319 regmatch_t pmatch
[10];
321 if ((p
->token
!= GREP_PATTERN
) &&
322 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
325 if (p
->token
== GREP_PATTERN_HEAD
) {
328 assert(p
->field
< ARRAY_SIZE(header_field
));
329 field
= header_field
[p
->field
].field
;
330 len
= header_field
[p
->field
].len
;
331 if (strncmp(bol
, field
, len
))
334 saved_ch
= strip_timestamp(bol
, &eol
);
339 regex_t
*exp
= &p
->regexp
;
340 hit
= !regexec(exp
, bol
, ARRAY_SIZE(pmatch
),
344 hit
= !fixmatch(p
->pattern
, bol
, pmatch
);
347 if (hit
&& opt
->word_regexp
) {
348 if ((pmatch
[0].rm_so
< 0) ||
349 (eol
- bol
) <= pmatch
[0].rm_so
||
350 (pmatch
[0].rm_eo
< 0) ||
351 (eol
- bol
) < pmatch
[0].rm_eo
)
352 die("regexp returned nonsense");
354 /* Match beginning must be either beginning of the
355 * line, or at word boundary (i.e. the last char must
356 * not be a word char). Similarly, match end must be
357 * either end of the line, or at word boundary
358 * (i.e. the next char must not be a word char).
360 if ( ((pmatch
[0].rm_so
== 0) ||
361 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
362 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
363 !word_char(bol
[pmatch
[0].rm_eo
])) )
368 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
369 /* There could be more than one match on the
370 * line, and the first match might not be
371 * strict word match. But later ones could be!
372 * Forward to the next possible start, i.e. the
373 * next position following a non-word char.
375 bol
= pmatch
[0].rm_so
+ bol
+ 1;
376 while (word_char(bol
[-1]) && bol
< eol
)
382 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
387 static int match_expr_eval(struct grep_opt
*o
,
389 char *bol
, char *eol
,
390 enum grep_context ctx
,
397 h
= match_one_pattern(o
, x
->u
.atom
, bol
, eol
, ctx
);
400 h
= !match_expr_eval(o
, x
->u
.unary
, bol
, eol
, ctx
, 0);
404 return (match_expr_eval(o
, x
->u
.binary
.left
,
406 match_expr_eval(o
, x
->u
.binary
.right
,
408 h
= match_expr_eval(o
, x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
409 h
&= match_expr_eval(o
, x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
413 return (match_expr_eval(o
, x
->u
.binary
.left
,
415 match_expr_eval(o
, x
->u
.binary
.right
,
417 h
= match_expr_eval(o
, x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
418 x
->u
.binary
.left
->hit
|= h
;
419 h
|= match_expr_eval(o
, x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
422 die("Unexpected node type (internal error) %d", x
->node
);
429 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
430 enum grep_context ctx
, int collect_hits
)
432 struct grep_expr
*x
= opt
->pattern_expression
;
433 return match_expr_eval(opt
, x
, bol
, eol
, ctx
, collect_hits
);
436 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
437 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(opt
, p
, bol
, eol
, ctx
))
451 static int grep_buffer_1(struct grep_opt
*opt
, const char *name
,
452 char *buf
, unsigned long size
, int collect_hits
)
455 unsigned long left
= size
;
457 struct pre_context_line
{
460 } *prev
= NULL
, *pcl
;
461 unsigned last_hit
= 0;
462 unsigned last_shown
= 0;
463 int binary_match_only
= 0;
464 const char *hunk_mark
= "";
466 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
468 if (buffer_is_binary(buf
, size
)) {
469 switch (opt
->binary
) {
470 case GREP_BINARY_DEFAULT
:
471 binary_match_only
= 1;
473 case GREP_BINARY_NOMATCH
:
474 return 0; /* Assume unmatch */
481 if (opt
->pre_context
)
482 prev
= xcalloc(opt
->pre_context
, sizeof(*prev
));
483 if (opt
->pre_context
|| opt
->post_context
)
490 eol
= end_of_line(bol
, &left
);
494 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
495 ctx
= GREP_CONTEXT_BODY
;
497 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
503 /* "grep -v -e foo -e bla" should list lines
504 * that do not have either, so inversion should
509 if (opt
->unmatch_name_only
) {
516 if (opt
->status_only
)
518 if (binary_match_only
) {
519 printf("Binary file %s matches\n", name
);
522 if (opt
->name_only
) {
523 show_name(opt
, name
);
526 /* Hit at this line. If we haven't shown the
527 * pre-context lines, we would need to show them.
528 * When asked to do "count", this still show
529 * the context which is nonsense, but the user
530 * deserves to get that ;-).
532 if (opt
->pre_context
) {
534 if (opt
->pre_context
< lno
)
535 from
= lno
- opt
->pre_context
;
538 if (from
<= last_shown
)
539 from
= last_shown
+ 1;
540 if (last_shown
&& from
!= last_shown
+ 1)
541 fputs(hunk_mark
, stdout
);
543 pcl
= &prev
[lno
-from
-1];
544 show_line(opt
, pcl
->bol
, pcl
->eol
,
550 if (last_shown
&& lno
!= last_shown
+ 1)
551 fputs(hunk_mark
, stdout
);
553 show_line(opt
, bol
, eol
, name
, lno
, ':');
554 last_shown
= last_hit
= lno
;
557 lno
<= last_hit
+ opt
->post_context
) {
558 /* If the last hit is within the post context,
559 * we need to show this line.
561 if (last_shown
&& lno
!= last_shown
+ 1)
562 fputs(hunk_mark
, stdout
);
563 show_line(opt
, bol
, eol
, name
, lno
, '-');
566 if (opt
->pre_context
) {
567 memmove(prev
+1, prev
,
568 (opt
->pre_context
-1) * sizeof(*prev
));
585 if (opt
->status_only
)
587 if (opt
->unmatch_name_only
) {
588 /* We did not see any hit, so we want to show this */
589 show_name(opt
, name
);
594 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
595 * which feels mostly useless but sometimes useful. Maybe
596 * make it another option? For now suppress them.
598 if (opt
->count
&& count
)
599 printf("%s%c%u\n", name
,
600 opt
->null_following_name
? '\0' : ':', count
);
604 static void clr_hit_marker(struct grep_expr
*x
)
606 /* All-hit markers are meaningful only at the very top level
611 if (x
->node
!= GREP_NODE_OR
)
613 x
->u
.binary
.left
->hit
= 0;
614 x
= x
->u
.binary
.right
;
618 static int chk_hit_marker(struct grep_expr
*x
)
620 /* Top level nodes have hit markers. See if they all are hits */
622 if (x
->node
!= GREP_NODE_OR
)
624 if (!x
->u
.binary
.left
->hit
)
626 x
= x
->u
.binary
.right
;
630 int grep_buffer(struct grep_opt
*opt
, const char *name
, char *buf
, unsigned long size
)
633 * we do not have to do the two-pass grep when we do not check
634 * buffer-wide "all-match".
637 return grep_buffer_1(opt
, name
, buf
, size
, 0);
639 /* Otherwise the toplevel "or" terms hit a bit differently.
640 * We first clear hit markers from them.
642 clr_hit_marker(opt
->pattern_expression
);
643 grep_buffer_1(opt
, name
, buf
, size
, 1);
645 if (!chk_hit_marker(opt
->pattern_expression
))
648 return grep_buffer_1(opt
, name
, buf
, size
, 0);