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_expr(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_expr(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 /* First compile regexps */
145 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
147 case GREP_PATTERN
: /* atom */
148 case GREP_PATTERN_HEAD
:
149 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 char *end_of_line(char *cp
, unsigned long *left
)
172 unsigned long l
= *left
;
173 while (l
&& *cp
!= '\n') {
181 static int word_char(char ch
)
183 return isalnum(ch
) || ch
== '_';
186 static void show_line(struct grep_opt
*opt
, const char *bol
, const char *eol
,
187 const char *name
, unsigned lno
, char sign
)
190 printf("%s%c", name
, sign
);
192 printf("%d%c", lno
, sign
);
193 printf("%.*s\n", (int)(eol
-bol
), bol
);
197 * NEEDSWORK: share code with diff.c
199 #define FIRST_FEW_BYTES 8000
200 static int buffer_is_binary(const char *ptr
, unsigned long size
)
202 if (FIRST_FEW_BYTES
< size
)
203 size
= FIRST_FEW_BYTES
;
204 return !!memchr(ptr
, 0, size
);
207 static int fixmatch(const char *pattern
, char *line
, regmatch_t
*match
)
209 char *hit
= strstr(line
, pattern
);
211 match
->rm_so
= match
->rm_eo
= -1;
215 match
->rm_so
= hit
- line
;
216 match
->rm_eo
= match
->rm_so
+ strlen(pattern
);
221 static int match_one_pattern(struct grep_opt
*opt
, struct grep_pat
*p
, char *bol
, char *eol
, enum grep_context ctx
)
225 regmatch_t pmatch
[10];
227 if ((p
->token
!= GREP_PATTERN
) &&
228 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
233 regex_t
*exp
= &p
->regexp
;
234 hit
= !regexec(exp
, bol
, ARRAY_SIZE(pmatch
),
238 hit
= !fixmatch(p
->pattern
, bol
, pmatch
);
241 if (hit
&& opt
->word_regexp
) {
242 if ((pmatch
[0].rm_so
< 0) ||
243 (eol
- bol
) <= pmatch
[0].rm_so
||
244 (pmatch
[0].rm_eo
< 0) ||
245 (eol
- bol
) < pmatch
[0].rm_eo
)
246 die("regexp returned nonsense");
248 /* Match beginning must be either beginning of the
249 * line, or at word boundary (i.e. the last char must
250 * not be a word char). Similarly, match end must be
251 * either end of the line, or at word boundary
252 * (i.e. the next char must not be a word char).
254 if ( ((pmatch
[0].rm_so
== 0 && at_true_bol
) ||
255 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
256 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
257 !word_char(bol
[pmatch
[0].rm_eo
])) )
262 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
263 /* There could be more than one match on the
264 * line, and the first match might not be
265 * strict word match. But later ones could be!
267 bol
= pmatch
[0].rm_so
+ bol
+ 1;
275 static int match_expr_eval(struct grep_opt
*opt
,
277 char *bol
, char *eol
,
278 enum grep_context ctx
)
282 return match_one_pattern(opt
, x
->u
.atom
, bol
, eol
, ctx
);
285 return !match_expr_eval(opt
, x
->u
.unary
, bol
, eol
, ctx
);
287 return (match_expr_eval(opt
, x
->u
.binary
.left
, bol
, eol
, ctx
) &&
288 match_expr_eval(opt
, x
->u
.binary
.right
, bol
, eol
, ctx
));
290 return (match_expr_eval(opt
, x
->u
.binary
.left
, bol
, eol
, ctx
) ||
291 match_expr_eval(opt
, x
->u
.binary
.right
, bol
, eol
, ctx
));
293 die("Unexpected node type (internal error) %d\n", x
->node
);
296 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
297 enum grep_context ctx
)
299 struct grep_expr
*x
= opt
->pattern_expression
;
300 return match_expr_eval(opt
, x
, bol
, eol
, ctx
);
303 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
304 enum grep_context ctx
)
308 return match_expr(opt
, bol
, eol
, ctx
);
309 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
310 if (match_one_pattern(opt
, p
, bol
, eol
, ctx
))
316 int grep_buffer(struct grep_opt
*opt
, const char *name
, char *buf
, unsigned long size
)
319 unsigned long left
= size
;
321 struct pre_context_line
{
324 } *prev
= NULL
, *pcl
;
325 unsigned last_hit
= 0;
326 unsigned last_shown
= 0;
327 int binary_match_only
= 0;
328 const char *hunk_mark
= "";
330 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
332 if (buffer_is_binary(buf
, size
)) {
333 switch (opt
->binary
) {
334 case GREP_BINARY_DEFAULT
:
335 binary_match_only
= 1;
337 case GREP_BINARY_NOMATCH
:
338 return 0; /* Assume unmatch */
345 if (opt
->pre_context
)
346 prev
= xcalloc(opt
->pre_context
, sizeof(*prev
));
347 if (opt
->pre_context
|| opt
->post_context
)
354 eol
= end_of_line(bol
, &left
);
358 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
359 ctx
= GREP_CONTEXT_BODY
;
361 hit
= match_line(opt
, bol
, eol
, ctx
);
364 /* "grep -v -e foo -e bla" should list lines
365 * that do not have either, so inversion should
370 if (opt
->unmatch_name_only
) {
377 if (opt
->status_only
)
379 if (binary_match_only
) {
380 printf("Binary file %s matches\n", name
);
383 if (opt
->name_only
) {
384 printf("%s\n", name
);
387 /* Hit at this line. If we haven't shown the
388 * pre-context lines, we would need to show them.
389 * When asked to do "count", this still show
390 * the context which is nonsense, but the user
391 * deserves to get that ;-).
393 if (opt
->pre_context
) {
395 if (opt
->pre_context
< lno
)
396 from
= lno
- opt
->pre_context
;
399 if (from
<= last_shown
)
400 from
= last_shown
+ 1;
401 if (last_shown
&& from
!= last_shown
+ 1)
404 pcl
= &prev
[lno
-from
-1];
405 show_line(opt
, pcl
->bol
, pcl
->eol
,
411 if (last_shown
&& lno
!= last_shown
+ 1)
414 show_line(opt
, bol
, eol
, name
, lno
, ':');
415 last_shown
= last_hit
= lno
;
418 lno
<= last_hit
+ opt
->post_context
) {
419 /* If the last hit is within the post context,
420 * we need to show this line.
422 if (last_shown
&& lno
!= last_shown
+ 1)
424 show_line(opt
, bol
, eol
, name
, lno
, '-');
427 if (opt
->pre_context
) {
428 memmove(prev
+1, prev
,
429 (opt
->pre_context
-1) * sizeof(*prev
));
442 if (opt
->status_only
)
444 if (opt
->unmatch_name_only
) {
445 /* We did not see any hit, so we want to show this */
446 printf("%s\n", name
);
451 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
452 * which feels mostly useless but sometimes useful. Maybe
453 * make it another option? For now suppress them.
455 if (opt
->count
&& count
)
456 printf("%s:%u\n", name
, count
);