5 void append_grep_pattern(struct grep_opt
*opt
, const char *pat
,
6 const char *origin
, int no
, enum grep_pat_token t
)
8 struct grep_pat
*p
= xcalloc(1, sizeof(*p
));
13 *opt
->pattern_tail
= p
;
14 opt
->pattern_tail
= &p
->next
;
18 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
20 int err
= regcomp(&p
->regexp
, p
->pattern
, opt
->regflags
);
25 sprintf(where
, "In '%s' at %d, ",
28 sprintf(where
, "%s, ", p
->origin
);
31 regerror(err
, &p
->regexp
, errbuf
, 1024);
33 die("%s'%s': %s", where
, p
->pattern
, errbuf
);
37 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
38 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
45 case GREP_PATTERN
: /* atom */
46 case GREP_PATTERN_HEAD
:
47 case GREP_PATTERN_BODY
:
48 x
= xcalloc(1, sizeof (struct grep_expr
));
49 x
->node
= GREP_NODE_ATOM
;
55 x
= compile_pattern_or(list
);
58 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
59 die("unmatched parenthesis");
60 *list
= (*list
)->next
;
67 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
76 die("--not not followed by pattern expression");
78 x
= xcalloc(1, sizeof (struct grep_expr
));
79 x
->node
= GREP_NODE_NOT
;
80 x
->u
.unary
= compile_pattern_not(list
);
82 die("--not followed by non pattern expression");
85 return compile_pattern_atom(list
);
89 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
92 struct grep_expr
*x
, *y
, *z
;
94 x
= compile_pattern_not(list
);
96 if (p
&& p
->token
== GREP_AND
) {
98 die("--and not followed by pattern expression");
100 y
= compile_pattern_and(list
);
102 die("--and not followed by pattern expression");
103 z
= xcalloc(1, sizeof (struct grep_expr
));
104 z
->node
= GREP_NODE_AND
;
105 z
->u
.binary
.left
= x
;
106 z
->u
.binary
.right
= y
;
112 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
115 struct grep_expr
*x
, *y
, *z
;
117 x
= compile_pattern_and(list
);
119 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
120 y
= compile_pattern_or(list
);
122 die("not a pattern expression %s", p
->pattern
);
123 z
= xcalloc(1, sizeof (struct grep_expr
));
124 z
->node
= GREP_NODE_OR
;
125 z
->u
.binary
.left
= x
;
126 z
->u
.binary
.right
= y
;
132 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
134 return compile_pattern_or(list
);
137 void compile_grep_patterns(struct grep_opt
*opt
)
144 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
146 case GREP_PATTERN
: /* atom */
147 case GREP_PATTERN_HEAD
:
148 case GREP_PATTERN_BODY
:
150 compile_regexp(p
, opt
);
161 /* Then bundle them up in an expression.
162 * A classic recursive descent parser would do.
164 p
= opt
->pattern_list
;
165 opt
->pattern_expression
= compile_pattern_expr(&p
);
167 die("incomplete pattern expression: %s", p
->pattern
);
170 static void free_pattern_expr(struct grep_expr
*x
)
176 free_pattern_expr(x
->u
.unary
);
180 free_pattern_expr(x
->u
.binary
.left
);
181 free_pattern_expr(x
->u
.binary
.right
);
187 void free_grep_patterns(struct grep_opt
*opt
)
189 struct grep_pat
*p
, *n
;
191 for (p
= opt
->pattern_list
; p
; p
= n
) {
194 case GREP_PATTERN
: /* atom */
195 case GREP_PATTERN_HEAD
:
196 case GREP_PATTERN_BODY
:
207 free_pattern_expr(opt
->pattern_expression
);
210 static char *end_of_line(char *cp
, unsigned long *left
)
212 unsigned long l
= *left
;
213 while (l
&& *cp
!= '\n') {
221 static int word_char(char ch
)
223 return isalnum(ch
) || ch
== '_';
226 static void show_line(struct grep_opt
*opt
, const char *bol
, const char *eol
,
227 const char *name
, unsigned lno
, char sign
)
230 printf("%s%c", name
, sign
);
232 printf("%d%c", lno
, sign
);
233 printf("%.*s\n", (int)(eol
-bol
), bol
);
237 * NEEDSWORK: share code with diff.c
239 #define FIRST_FEW_BYTES 8000
240 static int buffer_is_binary(const char *ptr
, unsigned long size
)
242 if (FIRST_FEW_BYTES
< size
)
243 size
= FIRST_FEW_BYTES
;
244 return !!memchr(ptr
, 0, size
);
247 static int fixmatch(const char *pattern
, char *line
, regmatch_t
*match
)
249 char *hit
= strstr(line
, pattern
);
251 match
->rm_so
= match
->rm_eo
= -1;
255 match
->rm_so
= hit
- line
;
256 match
->rm_eo
= match
->rm_so
+ strlen(pattern
);
261 static int match_one_pattern(struct grep_opt
*opt
, struct grep_pat
*p
, char *bol
, char *eol
, enum grep_context ctx
)
265 regmatch_t pmatch
[10];
267 if ((p
->token
!= GREP_PATTERN
) &&
268 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
273 regex_t
*exp
= &p
->regexp
;
274 hit
= !regexec(exp
, bol
, ARRAY_SIZE(pmatch
),
278 hit
= !fixmatch(p
->pattern
, bol
, pmatch
);
281 if (hit
&& opt
->word_regexp
) {
282 if ((pmatch
[0].rm_so
< 0) ||
283 (eol
- bol
) <= pmatch
[0].rm_so
||
284 (pmatch
[0].rm_eo
< 0) ||
285 (eol
- bol
) < pmatch
[0].rm_eo
)
286 die("regexp returned nonsense");
288 /* Match beginning must be either beginning of the
289 * line, or at word boundary (i.e. the last char must
290 * not be a word char). Similarly, match end must be
291 * either end of the line, or at word boundary
292 * (i.e. the next char must not be a word char).
294 if ( ((pmatch
[0].rm_so
== 0 && at_true_bol
) ||
295 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
296 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
297 !word_char(bol
[pmatch
[0].rm_eo
])) )
302 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
303 /* There could be more than one match on the
304 * line, and the first match might not be
305 * strict word match. But later ones could be!
307 bol
= pmatch
[0].rm_so
+ bol
+ 1;
315 static int match_expr_eval(struct grep_opt
*o
,
317 char *bol
, char *eol
,
318 enum grep_context ctx
,
325 h
= match_one_pattern(o
, x
->u
.atom
, bol
, eol
, ctx
);
328 h
= !match_expr_eval(o
, x
->u
.unary
, bol
, eol
, ctx
, 0);
332 return (match_expr_eval(o
, x
->u
.binary
.left
,
334 match_expr_eval(o
, x
->u
.binary
.right
,
336 h
= match_expr_eval(o
, x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
337 h
&= match_expr_eval(o
, x
->u
.binary
.right
, bol
, eol
, ctx
, 0);
341 return (match_expr_eval(o
, x
->u
.binary
.left
,
343 match_expr_eval(o
, x
->u
.binary
.right
,
345 h
= match_expr_eval(o
, x
->u
.binary
.left
, bol
, eol
, ctx
, 0);
346 x
->u
.binary
.left
->hit
|= h
;
347 h
|= match_expr_eval(o
, x
->u
.binary
.right
, bol
, eol
, ctx
, 1);
350 die("Unexpected node type (internal error) %d\n", x
->node
);
357 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
358 enum grep_context ctx
, int collect_hits
)
360 struct grep_expr
*x
= opt
->pattern_expression
;
361 return match_expr_eval(opt
, x
, bol
, eol
, ctx
, collect_hits
);
364 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
365 enum grep_context ctx
, int collect_hits
)
369 return match_expr(opt
, bol
, eol
, ctx
, collect_hits
);
371 /* we do not call with collect_hits without being extended */
372 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
373 if (match_one_pattern(opt
, p
, bol
, eol
, ctx
))
379 static int grep_buffer_1(struct grep_opt
*opt
, const char *name
,
380 char *buf
, unsigned long size
, int collect_hits
)
383 unsigned long left
= size
;
385 struct pre_context_line
{
388 } *prev
= NULL
, *pcl
;
389 unsigned last_hit
= 0;
390 unsigned last_shown
= 0;
391 int binary_match_only
= 0;
392 const char *hunk_mark
= "";
394 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
396 if (buffer_is_binary(buf
, size
)) {
397 switch (opt
->binary
) {
398 case GREP_BINARY_DEFAULT
:
399 binary_match_only
= 1;
401 case GREP_BINARY_NOMATCH
:
402 return 0; /* Assume unmatch */
409 if (opt
->pre_context
)
410 prev
= xcalloc(opt
->pre_context
, sizeof(*prev
));
411 if (opt
->pre_context
|| opt
->post_context
)
418 eol
= end_of_line(bol
, &left
);
422 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
423 ctx
= GREP_CONTEXT_BODY
;
425 hit
= match_line(opt
, bol
, eol
, ctx
, collect_hits
);
431 /* "grep -v -e foo -e bla" should list lines
432 * that do not have either, so inversion should
437 if (opt
->unmatch_name_only
) {
444 if (opt
->status_only
)
446 if (binary_match_only
) {
447 printf("Binary file %s matches\n", name
);
450 if (opt
->name_only
) {
451 printf("%s\n", name
);
454 /* Hit at this line. If we haven't shown the
455 * pre-context lines, we would need to show them.
456 * When asked to do "count", this still show
457 * the context which is nonsense, but the user
458 * deserves to get that ;-).
460 if (opt
->pre_context
) {
462 if (opt
->pre_context
< lno
)
463 from
= lno
- opt
->pre_context
;
466 if (from
<= last_shown
)
467 from
= last_shown
+ 1;
468 if (last_shown
&& from
!= last_shown
+ 1)
471 pcl
= &prev
[lno
-from
-1];
472 show_line(opt
, pcl
->bol
, pcl
->eol
,
478 if (last_shown
&& lno
!= last_shown
+ 1)
481 show_line(opt
, bol
, eol
, name
, lno
, ':');
482 last_shown
= last_hit
= lno
;
485 lno
<= last_hit
+ opt
->post_context
) {
486 /* If the last hit is within the post context,
487 * we need to show this line.
489 if (last_shown
&& lno
!= last_shown
+ 1)
491 show_line(opt
, bol
, eol
, name
, lno
, '-');
494 if (opt
->pre_context
) {
495 memmove(prev
+1, prev
,
496 (opt
->pre_context
-1) * sizeof(*prev
));
513 if (opt
->status_only
)
515 if (opt
->unmatch_name_only
) {
516 /* We did not see any hit, so we want to show this */
517 printf("%s\n", name
);
522 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
523 * which feels mostly useless but sometimes useful. Maybe
524 * make it another option? For now suppress them.
526 if (opt
->count
&& count
)
527 printf("%s:%u\n", name
, count
);
531 static void clr_hit_marker(struct grep_expr
*x
)
533 /* All-hit markers are meaningful only at the very top level
538 if (x
->node
!= GREP_NODE_OR
)
540 x
->u
.binary
.left
->hit
= 0;
541 x
= x
->u
.binary
.right
;
545 static int chk_hit_marker(struct grep_expr
*x
)
547 /* Top level nodes have hit markers. See if they all are hits */
549 if (x
->node
!= GREP_NODE_OR
)
551 if (!x
->u
.binary
.left
->hit
)
553 x
= x
->u
.binary
.right
;
557 int grep_buffer(struct grep_opt
*opt
, const char *name
, char *buf
, unsigned long size
)
560 * we do not have to do the two-pass grep when we do not check
561 * buffer-wide "all-match".
564 return grep_buffer_1(opt
, name
, buf
, size
, 0);
566 /* Otherwise the toplevel "or" terms hit a bit differently.
567 * We first clear hit markers from them.
569 clr_hit_marker(opt
->pattern_expression
);
570 grep_buffer_1(opt
, name
, buf
, size
, 1);
572 if (!chk_hit_marker(opt
->pattern_expression
))
575 return grep_buffer_1(opt
, name
, buf
, size
, 0);