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)
141 for (p = opt->pattern_list; p; p = p->next) {
143 case GREP_PATTERN: /* atom */
144 case GREP_PATTERN_HEAD:
145 case GREP_PATTERN_BODY:
147 compile_regexp(p, opt);
158 /* Then bundle them up in an expression.
159 * A classic recursive descent parser would do.
161 p = opt->pattern_list;
162 opt->pattern_expression = compile_pattern_expr(&p);
164 die("incomplete pattern expression: %s", p->pattern);
167 static void free_pattern_expr(struct grep_expr *x)
173 free_pattern_expr(x->u.unary);
177 free_pattern_expr(x->u.binary.left);
178 free_pattern_expr(x->u.binary.right);
184 void free_grep_patterns(struct grep_opt *opt)
186 struct grep_pat *p, *n;
188 for (p = opt->pattern_list; p; p = n) {
191 case GREP_PATTERN: /* atom */
192 case GREP_PATTERN_HEAD:
193 case GREP_PATTERN_BODY:
204 free_pattern_expr(opt->pattern_expression);
207 static char *end_of_line(char *cp, unsigned long *left)
209 unsigned long l = *left;
210 while (l && *cp != '\n') {
218 static int word_char(char ch)
220 return isalnum(ch) || ch == '_';
223 static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
224 const char *name, unsigned lno, char sign)
227 printf("%s%c", name, sign);
229 printf("%d%c", lno, sign);
230 printf("%.*s\n", (int)(eol-bol), bol);
234 * NEEDSWORK: share code with diff.c
236 #define FIRST_FEW_BYTES 8000
237 static int buffer_is_binary(const char *ptr, unsigned long size)
239 if (FIRST_FEW_BYTES < size)
240 size = FIRST_FEW_BYTES;
241 return !!memchr(ptr, 0, size);
244 static int fixmatch(const char *pattern, char *line, regmatch_t *match)
246 char *hit = strstr(line, pattern);
248 match->rm_so = match->rm_eo = -1;
252 match->rm_so = hit - line;
253 match->rm_eo = match->rm_so + strlen(pattern);
258 static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol, char *eol, enum grep_context ctx)
262 regmatch_t pmatch[10];
264 if ((p->token != GREP_PATTERN) &&
265 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
270 regex_t *exp = &p->regexp;
271 hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
275 hit = !fixmatch(p->pattern, bol, pmatch);
278 if (hit && opt->word_regexp) {
279 if ((pmatch[0].rm_so < 0) ||
280 (eol - bol) <= pmatch[0].rm_so ||
281 (pmatch[0].rm_eo < 0) ||
282 (eol - bol) < pmatch[0].rm_eo)
283 die("regexp returned nonsense");
285 /* Match beginning must be either beginning of the
286 * line, or at word boundary (i.e. the last char must
287 * not be a word char). Similarly, match end must be
288 * either end of the line, or at word boundary
289 * (i.e. the next char must not be a word char).
291 if ( ((pmatch[0].rm_so == 0 && at_true_bol) ||
292 !word_char(bol[pmatch[0].rm_so-1])) &&
293 ((pmatch[0].rm_eo == (eol-bol)) ||
294 !word_char(bol[pmatch[0].rm_eo])) )
299 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
300 /* There could be more than one match on the
301 * line, and the first match might not be
302 * strict word match. But later ones could be!
304 bol = pmatch[0].rm_so + bol + 1;
312 static int match_expr_eval(struct grep_opt *opt,
314 char *bol, char *eol,
315 enum grep_context ctx)
319 return match_one_pattern(opt, x->u.atom, bol, eol, ctx);
322 return !match_expr_eval(opt, x->u.unary, bol, eol, ctx);
324 return (match_expr_eval(opt, x->u.binary.left, bol, eol, ctx) &&
325 match_expr_eval(opt, x->u.binary.right, bol, eol, ctx));
327 return (match_expr_eval(opt, x->u.binary.left, bol, eol, ctx) ||
328 match_expr_eval(opt, x->u.binary.right, bol, eol, ctx));
330 die("Unexpected node type (internal error) %d\n", x->node);
333 static int match_expr(struct grep_opt *opt, char *bol, char *eol,
334 enum grep_context ctx)
336 struct grep_expr *x = opt->pattern_expression;
337 return match_expr_eval(opt, x, bol, eol, ctx);
340 static int match_line(struct grep_opt *opt, char *bol, char *eol,
341 enum grep_context ctx)
345 return match_expr(opt, bol, eol, ctx);
346 for (p = opt->pattern_list; p; p = p->next) {
347 if (match_one_pattern(opt, p, bol, eol, ctx))
353 int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size)
356 unsigned long left = size;
358 struct pre_context_line {
361 } *prev = NULL, *pcl;
362 unsigned last_hit = 0;
363 unsigned last_shown = 0;
364 int binary_match_only = 0;
365 const char *hunk_mark = "";
367 enum grep_context ctx = GREP_CONTEXT_HEAD;
369 if (buffer_is_binary(buf, size)) {
370 switch (opt->binary) {
371 case GREP_BINARY_DEFAULT:
372 binary_match_only = 1;
374 case GREP_BINARY_NOMATCH:
375 return 0; /* Assume unmatch */
382 if (opt->pre_context)
383 prev = xcalloc(opt->pre_context, sizeof(*prev));
384 if (opt->pre_context || opt->post_context)
391 eol = end_of_line(bol, &left);
395 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
396 ctx = GREP_CONTEXT_BODY;
398 hit = match_line(opt, bol, eol, ctx);
401 /* "grep -v -e foo -e bla" should list lines
402 * that do not have either, so inversion should
407 if (opt->unmatch_name_only) {
414 if (opt->status_only)
416 if (binary_match_only) {
417 printf("Binary file %s matches\n", name);
420 if (opt->name_only) {
421 printf("%s\n", name);
424 /* Hit at this line. If we haven't shown the
425 * pre-context lines, we would need to show them.
426 * When asked to do "count", this still show
427 * the context which is nonsense, but the user
428 * deserves to get that ;-).
430 if (opt->pre_context) {
432 if (opt->pre_context < lno)
433 from = lno - opt->pre_context;
436 if (from <= last_shown)
437 from = last_shown + 1;
438 if (last_shown && from != last_shown + 1)
441 pcl = &prev[lno-from-1];
442 show_line(opt, pcl->bol, pcl->eol,
448 if (last_shown && lno != last_shown + 1)
451 show_line(opt, bol, eol, name, lno, ':');
452 last_shown = last_hit = lno;
455 lno <= last_hit + opt->post_context) {
456 /* If the last hit is within the post context,
457 * we need to show this line.
459 if (last_shown && lno != last_shown + 1)
461 show_line(opt, bol, eol, name, lno, '-');
464 if (opt->pre_context) {
465 memmove(prev+1, prev,
466 (opt->pre_context-1) * sizeof(*prev));
481 if (opt->status_only)
483 if (opt->unmatch_name_only) {
484 /* We did not see any hit, so we want to show this */
485 printf("%s\n", name);
490 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
491 * which feels mostly useless but sometimes useful. Maybe
492 * make it another option? For now suppress them.
494 if (opt->count && count)
495 printf("%s:%u\n", name, count);