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)
60 case GREP_PATTERN: /* atom */
61 case GREP_PATTERN_HEAD:
62 case GREP_PATTERN_BODY:
63 x = xcalloc(1, sizeof (struct grep_expr));
64 x->node = GREP_NODE_ATOM;
70 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)
91 die("--not not followed by pattern expression");
93 x = xcalloc(1, sizeof (struct grep_expr));
94 x->node = GREP_NODE_NOT;
95 x->u.unary = compile_pattern_not(list);
97 die("--not followed by non pattern expression");
100 return compile_pattern_atom(list);
104 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
107 struct grep_expr *x, *y, *z;
109 x = compile_pattern_not(list);
111 if (p && p->token == GREP_AND) {
113 die("--and not followed by pattern expression");
115 y = compile_pattern_and(list);
117 die("--and not followed by pattern expression");
118 z = xcalloc(1, sizeof (struct grep_expr));
119 z->node = GREP_NODE_AND;
120 z->u.binary.left = x;
121 z->u.binary.right = y;
127 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
130 struct grep_expr *x, *y, *z;
132 x = compile_pattern_and(list);
134 if (x && p && p->token != GREP_CLOSE_PAREN) {
135 y = compile_pattern_or(list);
137 die("not a pattern expression %s", p->pattern);
138 z = xcalloc(1, sizeof (struct grep_expr));
139 z->node = GREP_NODE_OR;
140 z->u.binary.left = x;
141 z->u.binary.right = y;
147 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
149 return compile_pattern_or(list);
152 void compile_grep_patterns(struct grep_opt *opt)
159 for (p = opt->pattern_list; p; p = p->next) {
161 case GREP_PATTERN: /* atom */
162 case GREP_PATTERN_HEAD:
163 case GREP_PATTERN_BODY:
165 compile_regexp(p, opt);
176 /* Then bundle them up in an expression.
177 * A classic recursive descent parser would do.
179 p = opt->pattern_list;
180 opt->pattern_expression = compile_pattern_expr(&p);
182 die("incomplete pattern expression: %s", p->pattern);
185 static void free_pattern_expr(struct grep_expr *x)
191 free_pattern_expr(x->u.unary);
195 free_pattern_expr(x->u.binary.left);
196 free_pattern_expr(x->u.binary.right);
202 void free_grep_patterns(struct grep_opt *opt)
204 struct grep_pat *p, *n;
206 for (p = opt->pattern_list; p; p = n) {
209 case GREP_PATTERN: /* atom */
210 case GREP_PATTERN_HEAD:
211 case GREP_PATTERN_BODY:
222 free_pattern_expr(opt->pattern_expression);
225 static char *end_of_line(char *cp, unsigned long *left)
227 unsigned long l = *left;
228 while (l && *cp != '\n') {
236 static int word_char(char ch)
238 return isalnum(ch) || ch == '_';
241 static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
242 const char *name, unsigned lno, char sign)
245 printf("%s%c", name, sign);
247 printf("%d%c", lno, sign);
248 printf("%.*s\n", (int)(eol-bol), bol);
251 static int fixmatch(const char *pattern, char *line, regmatch_t *match)
253 char *hit = strstr(line, pattern);
255 match->rm_so = match->rm_eo = -1;
259 match->rm_so = hit - line;
260 match->rm_eo = match->rm_so + strlen(pattern);
265 static int strip_timestamp(char *bol, char **eol_p)
270 while (bol < --eol) {
286 { "committer ", 10 },
289 static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol, char *eol, enum grep_context ctx)
294 regmatch_t pmatch[10];
296 if ((p->token != GREP_PATTERN) &&
297 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
300 if (p->token == GREP_PATTERN_HEAD) {
303 assert(p->field < ARRAY_SIZE(header_field));
304 field = header_field[p->field].field;
305 len = header_field[p->field].len;
306 if (strncmp(bol, field, len))
309 saved_ch = strip_timestamp(bol, &eol);
314 regex_t *exp = &p->regexp;
315 hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
319 hit = !fixmatch(p->pattern, bol, pmatch);
322 if (hit && opt->word_regexp) {
323 if ((pmatch[0].rm_so < 0) ||
324 (eol - bol) <= pmatch[0].rm_so ||
325 (pmatch[0].rm_eo < 0) ||
326 (eol - bol) < pmatch[0].rm_eo)
327 die("regexp returned nonsense");
329 /* Match beginning must be either beginning of the
330 * line, or at word boundary (i.e. the last char must
331 * not be a word char). Similarly, match end must be
332 * either end of the line, or at word boundary
333 * (i.e. the next char must not be a word char).
335 if ( ((pmatch[0].rm_so == 0 && at_true_bol) ||
336 !word_char(bol[pmatch[0].rm_so-1])) &&
337 ((pmatch[0].rm_eo == (eol-bol)) ||
338 !word_char(bol[pmatch[0].rm_eo])) )
343 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
344 /* There could be more than one match on the
345 * line, and the first match might not be
346 * strict word match. But later ones could be!
348 bol = pmatch[0].rm_so + bol + 1;
353 if (p->token == GREP_PATTERN_HEAD && saved_ch)
358 static int match_expr_eval(struct grep_opt *o,
360 char *bol, char *eol,
361 enum grep_context ctx,
367 die("Not a valid grep expression");
370 h = match_one_pattern(o, x->u.atom, bol, eol, ctx);
373 h = !match_expr_eval(o, x->u.unary, bol, eol, ctx, 0);
377 return (match_expr_eval(o, x->u.binary.left,
379 match_expr_eval(o, x->u.binary.right,
381 h = match_expr_eval(o, x->u.binary.left, bol, eol, ctx, 0);
382 h &= match_expr_eval(o, x->u.binary.right, bol, eol, ctx, 0);
386 return (match_expr_eval(o, x->u.binary.left,
388 match_expr_eval(o, x->u.binary.right,
390 h = match_expr_eval(o, x->u.binary.left, bol, eol, ctx, 0);
391 x->u.binary.left->hit |= h;
392 h |= match_expr_eval(o, x->u.binary.right, bol, eol, ctx, 1);
395 die("Unexpected node type (internal error) %d\n", x->node);
402 static int match_expr(struct grep_opt *opt, char *bol, char *eol,
403 enum grep_context ctx, int collect_hits)
405 struct grep_expr *x = opt->pattern_expression;
406 return match_expr_eval(opt, x, bol, eol, ctx, collect_hits);
409 static int match_line(struct grep_opt *opt, char *bol, char *eol,
410 enum grep_context ctx, int collect_hits)
414 return match_expr(opt, bol, eol, ctx, collect_hits);
416 /* we do not call with collect_hits without being extended */
417 for (p = opt->pattern_list; p; p = p->next) {
418 if (match_one_pattern(opt, p, bol, eol, ctx))
424 static int grep_buffer_1(struct grep_opt *opt, const char *name,
425 char *buf, unsigned long size, int collect_hits)
428 unsigned long left = size;
430 struct pre_context_line {
433 } *prev = NULL, *pcl;
434 unsigned last_hit = 0;
435 unsigned last_shown = 0;
436 int binary_match_only = 0;
437 const char *hunk_mark = "";
439 enum grep_context ctx = GREP_CONTEXT_HEAD;
441 if (buffer_is_binary(buf, size)) {
442 switch (opt->binary) {
443 case GREP_BINARY_DEFAULT:
444 binary_match_only = 1;
446 case GREP_BINARY_NOMATCH:
447 return 0; /* Assume unmatch */
454 if (opt->pre_context)
455 prev = xcalloc(opt->pre_context, sizeof(*prev));
456 if (opt->pre_context || opt->post_context)
463 eol = end_of_line(bol, &left);
467 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
468 ctx = GREP_CONTEXT_BODY;
470 hit = match_line(opt, bol, eol, ctx, collect_hits);
476 /* "grep -v -e foo -e bla" should list lines
477 * that do not have either, so inversion should
482 if (opt->unmatch_name_only) {
489 if (opt->status_only)
491 if (binary_match_only) {
492 printf("Binary file %s matches\n", name);
495 if (opt->name_only) {
496 printf("%s\n", name);
499 /* Hit at this line. If we haven't shown the
500 * pre-context lines, we would need to show them.
501 * When asked to do "count", this still show
502 * the context which is nonsense, but the user
503 * deserves to get that ;-).
505 if (opt->pre_context) {
507 if (opt->pre_context < lno)
508 from = lno - opt->pre_context;
511 if (from <= last_shown)
512 from = last_shown + 1;
513 if (last_shown && from != last_shown + 1)
514 fputs(hunk_mark, stdout);
516 pcl = &prev[lno-from-1];
517 show_line(opt, pcl->bol, pcl->eol,
523 if (last_shown && lno != last_shown + 1)
524 fputs(hunk_mark, stdout);
526 show_line(opt, bol, eol, name, lno, ':');
527 last_shown = last_hit = lno;
530 lno <= last_hit + opt->post_context) {
531 /* If the last hit is within the post context,
532 * we need to show this line.
534 if (last_shown && lno != last_shown + 1)
535 fputs(hunk_mark, stdout);
536 show_line(opt, bol, eol, name, lno, '-');
539 if (opt->pre_context) {
540 memmove(prev+1, prev,
541 (opt->pre_context-1) * sizeof(*prev));
558 if (opt->status_only)
560 if (opt->unmatch_name_only) {
561 /* We did not see any hit, so we want to show this */
562 printf("%s\n", name);
567 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
568 * which feels mostly useless but sometimes useful. Maybe
569 * make it another option? For now suppress them.
571 if (opt->count && count)
572 printf("%s:%u\n", name, count);
576 static void clr_hit_marker(struct grep_expr *x)
578 /* All-hit markers are meaningful only at the very top level
583 if (x->node != GREP_NODE_OR)
585 x->u.binary.left->hit = 0;
586 x = x->u.binary.right;
590 static int chk_hit_marker(struct grep_expr *x)
592 /* Top level nodes have hit markers. See if they all are hits */
594 if (x->node != GREP_NODE_OR)
596 if (!x->u.binary.left->hit)
598 x = x->u.binary.right;
602 int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size)
605 * we do not have to do the two-pass grep when we do not check
606 * buffer-wide "all-match".
609 return grep_buffer_1(opt, name, buf, size, 0);
611 /* Otherwise the toplevel "or" terms hit a bit differently.
612 * We first clear hit markers from them.
614 clr_hit_marker(opt->pattern_expression);
615 grep_buffer_1(opt, name, buf, size, 1);
617 if (!chk_hit_marker(opt->pattern_expression))
620 return grep_buffer_1(opt, name, buf, size, 0);