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 void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
33 int err
= regcomp(&p
->regexp
, p
->pattern
, opt
->regflags
);
38 sprintf(where
, "In '%s' at %d, ",
41 sprintf(where
, "%s, ", p
->origin
);
44 regerror(err
, &p
->regexp
, errbuf
, 1024);
46 die("%s'%s': %s", where
, p
->pattern
, errbuf
);
50 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
51 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
58 case GREP_PATTERN
: /* atom */
59 case GREP_PATTERN_HEAD
:
60 case GREP_PATTERN_BODY
:
61 x
= xcalloc(1, sizeof (struct grep_expr
));
62 x
->node
= GREP_NODE_ATOM
;
68 x
= compile_pattern_or(list
);
71 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
72 die("unmatched parenthesis");
73 *list
= (*list
)->next
;
80 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
89 die("--not not followed by pattern expression");
91 x
= xcalloc(1, sizeof (struct grep_expr
));
92 x
->node
= GREP_NODE_NOT
;
93 x
->u
.unary
= compile_pattern_not(list
);
95 die("--not followed by non pattern expression");
98 return compile_pattern_atom(list
);
102 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
105 struct grep_expr
*x
, *y
, *z
;
107 x
= compile_pattern_not(list
);
109 if (p
&& p
->token
== GREP_AND
) {
111 die("--and not followed by pattern expression");
113 y
= compile_pattern_and(list
);
115 die("--and not followed by pattern expression");
116 z
= xcalloc(1, sizeof (struct grep_expr
));
117 z
->node
= GREP_NODE_AND
;
118 z
->u
.binary
.left
= x
;
119 z
->u
.binary
.right
= y
;
125 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
128 struct grep_expr
*x
, *y
, *z
;
130 x
= compile_pattern_and(list
);
132 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
133 y
= compile_pattern_or(list
);
135 die("not a pattern expression %s", p
->pattern
);
136 z
= xcalloc(1, sizeof (struct grep_expr
));
137 z
->node
= GREP_NODE_OR
;
138 z
->u
.binary
.left
= x
;
139 z
->u
.binary
.right
= y
;
145 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
147 return compile_pattern_or(list
);
150 void compile_grep_patterns(struct grep_opt
*opt
)
157 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
159 case GREP_PATTERN
: /* atom */
160 case GREP_PATTERN_HEAD
:
161 case GREP_PATTERN_BODY
:
163 compile_regexp(p
, opt
);
174 /* Then bundle them up in an expression.
175 * A classic recursive descent parser would do.
177 p
= opt
->pattern_list
;
178 opt
->pattern_expression
= compile_pattern_expr(&p
);
180 die("incomplete pattern expression: %s", p
->pattern
);
183 static void free_pattern_expr(struct grep_expr
*x
)
189 free_pattern_expr(x
->u
.unary
);
193 free_pattern_expr(x
->u
.binary
.left
);
194 free_pattern_expr(x
->u
.binary
.right
);
200 void free_grep_patterns(struct grep_opt
*opt
)
202 struct grep_pat
*p
, *n
;
204 for (p
= opt
->pattern_list
; p
; p
= n
) {
207 case GREP_PATTERN
: /* atom */
208 case GREP_PATTERN_HEAD
:
209 case GREP_PATTERN_BODY
:
220 free_pattern_expr(opt
->pattern_expression
);
223 static char *end_of_line(char *cp
, unsigned long *left
)
225 unsigned long l
= *left
;
226 while (l
&& *cp
!= '\n') {
234 static int word_char(char ch
)
236 return isalnum(ch
) || ch
== '_';
239 static void show_line(struct grep_opt
*opt
, const char *bol
, const char *eol
,
240 const char *name
, unsigned lno
, char sign
)
243 printf("%s%c", name
, sign
);
245 printf("%d%c", lno
, sign
);
246 printf("%.*s\n", (int)(eol
-bol
), bol
);
249 static int fixmatch(const char *pattern
, char *line
, regmatch_t
*match
)
251 char *hit
= strstr(line
, pattern
);
253 match
->rm_so
= match
->rm_eo
= -1;
257 match
->rm_so
= hit
- line
;
258 match
->rm_eo
= match
->rm_so
+ strlen(pattern
);
263 static int strip_timestamp(char *bol
, char **eol_p
)
268 while (bol
< --eol
) {
284 { "committer ", 10 },
287 static int match_one_pattern(struct grep_opt
*opt
, struct grep_pat
*p
, char *bol
, char *eol
, enum grep_context ctx
)
292 regmatch_t pmatch
[10];
294 if ((p
->token
!= GREP_PATTERN
) &&
295 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
298 if (p
->token
== GREP_PATTERN_HEAD
) {
301 assert(p
->field
< ARRAY_SIZE(header_field
));
302 field
= header_field
[p
->field
].field
;
303 len
= header_field
[p
->field
].len
;
304 if (strncmp(bol
, field
, len
))
307 saved_ch
= strip_timestamp(bol
, &eol
);
312 regex_t
*exp
= &p
->regexp
;
313 hit
= !regexec(exp
, bol
, ARRAY_SIZE(pmatch
),
317 hit
= !fixmatch(p
->pattern
, bol
, pmatch
);
320 if (hit
&& opt
->word_regexp
) {
321 if ((pmatch
[0].rm_so
< 0) ||
322 (eol
- bol
) <= pmatch
[0].rm_so
||
323 (pmatch
[0].rm_eo
< 0) ||
324 (eol
- bol
) < pmatch
[0].rm_eo
)
325 die("regexp returned nonsense");
327 /* Match beginning must be either beginning of the
328 * line, or at word boundary (i.e. the last char must
329 * not be a word char). Similarly, match end must be
330 * either end of the line, or at word boundary
331 * (i.e. the next char must not be a word char).
333 if ( ((pmatch
[0].rm_so
== 0 && at_true_bol
) ||
334 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
335 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
336 !word_char(bol
[pmatch
[0].rm_eo
])) )
341 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
342 /* There could be more than one match on the
343 * line, and the first match might not be
344 * strict word match. But later ones could be!
346 bol
= pmatch
[0].rm_so
+ bol
+ 1;
351 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
356 static int match_expr_eval(struct grep_opt
*o
,
358 char *bol
, char *eol
,
359 enum grep_context ctx
,
366 h
= match_one_pattern(o
, x
->u
.atom
, bol
, eol
, ctx
);
369 h
= !match_expr_eval(o
, x
->u
.unary
, bol
, eol
, ctx
, 0);
373 return (match_expr_eval(o
, x
->u
.binary
.left
,
375 match_expr_eval(o
, x
->u
.binary
.right
,
377 h
= match_expr_eval(o
, x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
378 h
&= match_expr_eval(o
, x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
382 return (match_expr_eval(o
, x
->u
.binary
.left
,
384 match_expr_eval(o
, x
->u
.binary
.right
,
386 h
= match_expr_eval(o
, x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
387 x
->u
.binary
.left
->hit
|= h
;
388 h
|= match_expr_eval(o
, x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
391 die("Unexpected node type (internal error) %d\n", x
->node
);
398 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
399 enum grep_context ctx
, int collect_hits
)
401 struct grep_expr
*x
= opt
->pattern_expression
;
402 return match_expr_eval(opt
, x
, bol
, eol
, ctx
, collect_hits
);
405 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
406 enum grep_context ctx
, int collect_hits
)
410 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
412 /* we do not call with collect_hits without being extended */
413 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
414 if (match_one_pattern(opt
, p
, bol
, eol
, ctx
))
420 static int grep_buffer_1(struct grep_opt
*opt
, const char *name
,
421 char *buf
, unsigned long size
, int collect_hits
)
424 unsigned long left
= size
;
426 struct pre_context_line
{
429 } *prev
= NULL
, *pcl
;
430 unsigned last_hit
= 0;
431 unsigned last_shown
= 0;
432 int binary_match_only
= 0;
433 const char *hunk_mark
= "";
435 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
437 if (buffer_is_binary(buf
, size
)) {
438 switch (opt
->binary
) {
439 case GREP_BINARY_DEFAULT
:
440 binary_match_only
= 1;
442 case GREP_BINARY_NOMATCH
:
443 return 0; /* Assume unmatch */
450 if (opt
->pre_context
)
451 prev
= xcalloc(opt
->pre_context
, sizeof(*prev
));
452 if (opt
->pre_context
|| opt
->post_context
)
459 eol
= end_of_line(bol
, &left
);
463 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
464 ctx
= GREP_CONTEXT_BODY
;
466 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
472 /* "grep -v -e foo -e bla" should list lines
473 * that do not have either, so inversion should
478 if (opt
->unmatch_name_only
) {
485 if (opt
->status_only
)
487 if (binary_match_only
) {
488 printf("Binary file %s matches\n", name
);
491 if (opt
->name_only
) {
492 printf("%s\n", name
);
495 /* Hit at this line. If we haven't shown the
496 * pre-context lines, we would need to show them.
497 * When asked to do "count", this still show
498 * the context which is nonsense, but the user
499 * deserves to get that ;-).
501 if (opt
->pre_context
) {
503 if (opt
->pre_context
< lno
)
504 from
= lno
- opt
->pre_context
;
507 if (from
<= last_shown
)
508 from
= last_shown
+ 1;
509 if (last_shown
&& from
!= last_shown
+ 1)
512 pcl
= &prev
[lno
-from
-1];
513 show_line(opt
, pcl
->bol
, pcl
->eol
,
519 if (last_shown
&& lno
!= last_shown
+ 1)
522 show_line(opt
, bol
, eol
, name
, lno
, ':');
523 last_shown
= last_hit
= lno
;
526 lno
<= last_hit
+ opt
->post_context
) {
527 /* If the last hit is within the post context,
528 * we need to show this line.
530 if (last_shown
&& lno
!= last_shown
+ 1)
532 show_line(opt
, bol
, eol
, name
, lno
, '-');
535 if (opt
->pre_context
) {
536 memmove(prev
+1, prev
,
537 (opt
->pre_context
-1) * sizeof(*prev
));
554 if (opt
->status_only
)
556 if (opt
->unmatch_name_only
) {
557 /* We did not see any hit, so we want to show this */
558 printf("%s\n", name
);
563 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
564 * which feels mostly useless but sometimes useful. Maybe
565 * make it another option? For now suppress them.
567 if (opt
->count
&& count
)
568 printf("%s:%u\n", name
, count
);
572 static void clr_hit_marker(struct grep_expr
*x
)
574 /* All-hit markers are meaningful only at the very top level
579 if (x
->node
!= GREP_NODE_OR
)
581 x
->u
.binary
.left
->hit
= 0;
582 x
= x
->u
.binary
.right
;
586 static int chk_hit_marker(struct grep_expr
*x
)
588 /* Top level nodes have hit markers. See if they all are hits */
590 if (x
->node
!= GREP_NODE_OR
)
592 if (!x
->u
.binary
.left
->hit
)
594 x
= x
->u
.binary
.right
;
598 int grep_buffer(struct grep_opt
*opt
, const char *name
, char *buf
, unsigned long size
)
601 * we do not have to do the two-pass grep when we do not check
602 * buffer-wide "all-match".
605 return grep_buffer_1(opt
, name
, buf
, size
, 0);
607 /* Otherwise the toplevel "or" terms hit a bit differently.
608 * We first clear hit markers from them.
610 clr_hit_marker(opt
->pattern_expression
);
611 grep_buffer_1(opt
, name
, buf
, size
, 1);
613 if (!chk_hit_marker(opt
->pattern_expression
))
616 return grep_buffer_1(opt
, name
, buf
, size
, 0);