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)
244 if (opt->null_following_name)
247 printf("%s%c", name, sign);
249 printf("%d%c", lno, sign);
250 printf("%.*s\n", (int)(eol-bol), bol);
253 static void show_name(struct grep_opt *opt, const char *name)
255 printf("%s%c", name, opt->null_following_name ? '\0' : '\n');
258 static int fixmatch(const char *pattern, char *line, regmatch_t *match)
260 char *hit = strstr(line, pattern);
262 match->rm_so = match->rm_eo = -1;
266 match->rm_so = hit - line;
267 match->rm_eo = match->rm_so + strlen(pattern);
272 static int strip_timestamp(char *bol, char **eol_p)
277 while (bol < --eol) {
293 { "committer ", 10 },
296 static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol, char *eol, enum grep_context ctx)
301 regmatch_t pmatch[10];
303 if ((p->token != GREP_PATTERN) &&
304 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
307 if (p->token == GREP_PATTERN_HEAD) {
310 assert(p->field < ARRAY_SIZE(header_field));
311 field = header_field[p->field].field;
312 len = header_field[p->field].len;
313 if (strncmp(bol, field, len))
316 saved_ch = strip_timestamp(bol, &eol);
321 regex_t *exp = &p->regexp;
322 hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
326 hit = !fixmatch(p->pattern, bol, pmatch);
329 if (hit && opt->word_regexp) {
330 if ((pmatch[0].rm_so < 0) ||
331 (eol - bol) <= pmatch[0].rm_so ||
332 (pmatch[0].rm_eo < 0) ||
333 (eol - bol) < pmatch[0].rm_eo)
334 die("regexp returned nonsense");
336 /* Match beginning must be either beginning of the
337 * line, or at word boundary (i.e. the last char must
338 * not be a word char). Similarly, match end must be
339 * either end of the line, or at word boundary
340 * (i.e. the next char must not be a word char).
342 if ( ((pmatch[0].rm_so == 0 && at_true_bol) ||
343 !word_char(bol[pmatch[0].rm_so-1])) &&
344 ((pmatch[0].rm_eo == (eol-bol)) ||
345 !word_char(bol[pmatch[0].rm_eo])) )
350 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
351 /* There could be more than one match on the
352 * line, and the first match might not be
353 * strict word match. But later ones could be!
355 bol = pmatch[0].rm_so + bol + 1;
360 if (p->token == GREP_PATTERN_HEAD && saved_ch)
365 static int match_expr_eval(struct grep_opt *o,
367 char *bol, char *eol,
368 enum grep_context ctx,
374 die("Not a valid grep expression");
377 h = match_one_pattern(o, x->u.atom, bol, eol, ctx);
380 h = !match_expr_eval(o, x->u.unary, bol, eol, ctx, 0);
384 return (match_expr_eval(o, x->u.binary.left,
386 match_expr_eval(o, x->u.binary.right,
388 h = match_expr_eval(o, x->u.binary.left, bol, eol, ctx, 0);
389 h &= match_expr_eval(o, x->u.binary.right, bol, eol, ctx, 0);
393 return (match_expr_eval(o, x->u.binary.left,
395 match_expr_eval(o, x->u.binary.right,
397 h = match_expr_eval(o, x->u.binary.left, bol, eol, ctx, 0);
398 x->u.binary.left->hit |= h;
399 h |= match_expr_eval(o, x->u.binary.right, bol, eol, ctx, 1);
402 die("Unexpected node type (internal error) %d\n", x->node);
409 static int match_expr(struct grep_opt *opt, char *bol, char *eol,
410 enum grep_context ctx, int collect_hits)
412 struct grep_expr *x = opt->pattern_expression;
413 return match_expr_eval(opt, x, bol, eol, ctx, collect_hits);
416 static int match_line(struct grep_opt *opt, char *bol, char *eol,
417 enum grep_context ctx, int collect_hits)
421 return match_expr(opt, bol, eol, ctx, collect_hits);
423 /* we do not call with collect_hits without being extended */
424 for (p = opt->pattern_list; p; p = p->next) {
425 if (match_one_pattern(opt, p, bol, eol, ctx))
431 static int grep_buffer_1(struct grep_opt *opt, const char *name,
432 char *buf, unsigned long size, int collect_hits)
435 unsigned long left = size;
437 struct pre_context_line {
440 } *prev = NULL, *pcl;
441 unsigned last_hit = 0;
442 unsigned last_shown = 0;
443 int binary_match_only = 0;
444 const char *hunk_mark = "";
446 enum grep_context ctx = GREP_CONTEXT_HEAD;
448 if (buffer_is_binary(buf, size)) {
449 switch (opt->binary) {
450 case GREP_BINARY_DEFAULT:
451 binary_match_only = 1;
453 case GREP_BINARY_NOMATCH:
454 return 0; /* Assume unmatch */
461 if (opt->pre_context)
462 prev = xcalloc(opt->pre_context, sizeof(*prev));
463 if (opt->pre_context || opt->post_context)
470 eol = end_of_line(bol, &left);
474 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
475 ctx = GREP_CONTEXT_BODY;
477 hit = match_line(opt, bol, eol, ctx, collect_hits);
483 /* "grep -v -e foo -e bla" should list lines
484 * that do not have either, so inversion should
489 if (opt->unmatch_name_only) {
496 if (opt->status_only)
498 if (binary_match_only) {
499 printf("Binary file %s matches\n", name);
502 if (opt->name_only) {
503 show_name(opt, name);
506 /* Hit at this line. If we haven't shown the
507 * pre-context lines, we would need to show them.
508 * When asked to do "count", this still show
509 * the context which is nonsense, but the user
510 * deserves to get that ;-).
512 if (opt->pre_context) {
514 if (opt->pre_context < lno)
515 from = lno - opt->pre_context;
518 if (from <= last_shown)
519 from = last_shown + 1;
520 if (last_shown && from != last_shown + 1)
521 fputs(hunk_mark, stdout);
523 pcl = &prev[lno-from-1];
524 show_line(opt, pcl->bol, pcl->eol,
530 if (last_shown && lno != last_shown + 1)
531 fputs(hunk_mark, stdout);
533 show_line(opt, bol, eol, name, lno, ':');
534 last_shown = last_hit = lno;
537 lno <= last_hit + opt->post_context) {
538 /* If the last hit is within the post context,
539 * we need to show this line.
541 if (last_shown && lno != last_shown + 1)
542 fputs(hunk_mark, stdout);
543 show_line(opt, bol, eol, name, lno, '-');
546 if (opt->pre_context) {
547 memmove(prev+1, prev,
548 (opt->pre_context-1) * sizeof(*prev));
565 if (opt->status_only)
567 if (opt->unmatch_name_only) {
568 /* We did not see any hit, so we want to show this */
569 show_name(opt, name);
574 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
575 * which feels mostly useless but sometimes useful. Maybe
576 * make it another option? For now suppress them.
578 if (opt->count && count)
579 printf("%s%c%u\n", name,
580 opt->null_following_name ? '\0' : ':', count);
584 static void clr_hit_marker(struct grep_expr *x)
586 /* All-hit markers are meaningful only at the very top level
591 if (x->node != GREP_NODE_OR)
593 x->u.binary.left->hit = 0;
594 x = x->u.binary.right;
598 static int chk_hit_marker(struct grep_expr *x)
600 /* Top level nodes have hit markers. See if they all are hits */
602 if (x->node != GREP_NODE_OR)
604 if (!x->u.binary.left->hit)
606 x = x->u.binary.right;
610 int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size)
613 * we do not have to do the two-pass grep when we do not check
614 * buffer-wide "all-match".
617 return grep_buffer_1(opt, name, buf, size, 0);
619 /* Otherwise the toplevel "or" terms hit a bit differently.
620 * We first clear hit markers from them.
622 clr_hit_marker(opt->pattern_expression);
623 grep_buffer_1(opt, name, buf, size, 1);
625 if (!chk_hit_marker(opt->pattern_expression))
628 return grep_buffer_1(opt, name, buf, size, 0);