1 #include "cache.h"
2 #include "grep.h"
3 #include "xdiff-interface.h"
5 void append_header_grep_pattern(struct grep_opt *opt, enum grep_header_field field, const char *pat)
6 {
7 struct grep_pat *p = xcalloc(1, sizeof(*p));
8 p->pattern = pat;
9 p->origin = "header";
10 p->no = 0;
11 p->token = GREP_PATTERN_HEAD;
12 p->field = field;
13 *opt->pattern_tail = p;
14 opt->pattern_tail = &p->next;
15 p->next = NULL;
16 }
18 void append_grep_pattern(struct grep_opt *opt, const char *pat,
19 const char *origin, int no, enum grep_pat_token t)
20 {
21 struct grep_pat *p = xcalloc(1, sizeof(*p));
22 p->pattern = pat;
23 p->origin = origin;
24 p->no = no;
25 p->token = t;
26 *opt->pattern_tail = p;
27 opt->pattern_tail = &p->next;
28 p->next = NULL;
29 }
31 static int is_fixed(const char *s)
32 {
33 while (*s && !is_regex_special(*s))
34 s++;
35 return !*s;
36 }
38 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
39 {
40 int err;
42 p->word_regexp = opt->word_regexp;
44 if (opt->fixed || is_fixed(p->pattern))
45 p->fixed = 1;
46 if (opt->regflags & REG_ICASE)
47 p->fixed = 0;
48 if (p->fixed)
49 return;
51 err = regcomp(&p->regexp, p->pattern, opt->regflags);
52 if (err) {
53 char errbuf[1024];
54 char where[1024];
55 if (p->no)
56 sprintf(where, "In '%s' at %d, ",
57 p->origin, p->no);
58 else if (p->origin)
59 sprintf(where, "%s, ", p->origin);
60 else
61 where[0] = 0;
62 regerror(err, &p->regexp, errbuf, 1024);
63 regfree(&p->regexp);
64 die("%s'%s': %s", where, p->pattern, errbuf);
65 }
66 }
68 static struct grep_expr *compile_pattern_or(struct grep_pat **);
69 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
70 {
71 struct grep_pat *p;
72 struct grep_expr *x;
74 p = *list;
75 if (!p)
76 return NULL;
77 switch (p->token) {
78 case GREP_PATTERN: /* atom */
79 case GREP_PATTERN_HEAD:
80 case GREP_PATTERN_BODY:
81 x = xcalloc(1, sizeof (struct grep_expr));
82 x->node = GREP_NODE_ATOM;
83 x->u.atom = p;
84 *list = p->next;
85 return x;
86 case GREP_OPEN_PAREN:
87 *list = p->next;
88 x = compile_pattern_or(list);
89 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
90 die("unmatched parenthesis");
91 *list = (*list)->next;
92 return x;
93 default:
94 return NULL;
95 }
96 }
98 static struct grep_expr *compile_pattern_not(struct grep_pat **list)
99 {
100 struct grep_pat *p;
101 struct grep_expr *x;
103 p = *list;
104 if (!p)
105 return NULL;
106 switch (p->token) {
107 case GREP_NOT:
108 if (!p->next)
109 die("--not not followed by pattern expression");
110 *list = p->next;
111 x = xcalloc(1, sizeof (struct grep_expr));
112 x->node = GREP_NODE_NOT;
113 x->u.unary = compile_pattern_not(list);
114 if (!x->u.unary)
115 die("--not followed by non pattern expression");
116 return x;
117 default:
118 return compile_pattern_atom(list);
119 }
120 }
122 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
123 {
124 struct grep_pat *p;
125 struct grep_expr *x, *y, *z;
127 x = compile_pattern_not(list);
128 p = *list;
129 if (p && p->token == GREP_AND) {
130 if (!p->next)
131 die("--and not followed by pattern expression");
132 *list = p->next;
133 y = compile_pattern_and(list);
134 if (!y)
135 die("--and not followed by pattern expression");
136 z = xcalloc(1, sizeof (struct grep_expr));
137 z->node = GREP_NODE_AND;
138 z->u.binary.left = x;
139 z->u.binary.right = y;
140 return z;
141 }
142 return x;
143 }
145 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
146 {
147 struct grep_pat *p;
148 struct grep_expr *x, *y, *z;
150 x = compile_pattern_and(list);
151 p = *list;
152 if (x && p && p->token != GREP_CLOSE_PAREN) {
153 y = compile_pattern_or(list);
154 if (!y)
155 die("not a pattern expression %s", p->pattern);
156 z = xcalloc(1, sizeof (struct grep_expr));
157 z->node = GREP_NODE_OR;
158 z->u.binary.left = x;
159 z->u.binary.right = y;
160 return z;
161 }
162 return x;
163 }
165 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
166 {
167 return compile_pattern_or(list);
168 }
170 void compile_grep_patterns(struct grep_opt *opt)
171 {
172 struct grep_pat *p;
174 if (opt->all_match)
175 opt->extended = 1;
177 for (p = opt->pattern_list; p; p = p->next) {
178 switch (p->token) {
179 case GREP_PATTERN: /* atom */
180 case GREP_PATTERN_HEAD:
181 case GREP_PATTERN_BODY:
182 compile_regexp(p, opt);
183 break;
184 default:
185 opt->extended = 1;
186 break;
187 }
188 }
190 if (!opt->extended)
191 return;
193 /* Then bundle them up in an expression.
194 * A classic recursive descent parser would do.
195 */
196 p = opt->pattern_list;
197 if (p)
198 opt->pattern_expression = compile_pattern_expr(&p);
199 if (p)
200 die("incomplete pattern expression: %s", p->pattern);
201 }
203 static void free_pattern_expr(struct grep_expr *x)
204 {
205 switch (x->node) {
206 case GREP_NODE_ATOM:
207 break;
208 case GREP_NODE_NOT:
209 free_pattern_expr(x->u.unary);
210 break;
211 case GREP_NODE_AND:
212 case GREP_NODE_OR:
213 free_pattern_expr(x->u.binary.left);
214 free_pattern_expr(x->u.binary.right);
215 break;
216 }
217 free(x);
218 }
220 void free_grep_patterns(struct grep_opt *opt)
221 {
222 struct grep_pat *p, *n;
224 for (p = opt->pattern_list; p; p = n) {
225 n = p->next;
226 switch (p->token) {
227 case GREP_PATTERN: /* atom */
228 case GREP_PATTERN_HEAD:
229 case GREP_PATTERN_BODY:
230 regfree(&p->regexp);
231 break;
232 default:
233 break;
234 }
235 free(p);
236 }
238 if (!opt->extended)
239 return;
240 free_pattern_expr(opt->pattern_expression);
241 }
243 static char *end_of_line(char *cp, unsigned long *left)
244 {
245 unsigned long l = *left;
246 while (l && *cp != '\n') {
247 l--;
248 cp++;
249 }
250 *left = l;
251 return cp;
252 }
254 static int word_char(char ch)
255 {
256 return isalnum(ch) || ch == '_';
257 }
259 static void show_name(struct grep_opt *opt, const char *name)
260 {
261 printf("%s%c", name, opt->null_following_name ? '\0' : '\n');
262 }
264 static int fixmatch(const char *pattern, char *line, regmatch_t *match)
265 {
266 char *hit = strstr(line, pattern);
267 if (!hit) {
268 match->rm_so = match->rm_eo = -1;
269 return REG_NOMATCH;
270 }
271 else {
272 match->rm_so = hit - line;
273 match->rm_eo = match->rm_so + strlen(pattern);
274 return 0;
275 }
276 }
278 static int strip_timestamp(char *bol, char **eol_p)
279 {
280 char *eol = *eol_p;
281 int ch;
283 while (bol < --eol) {
284 if (*eol != '>')
285 continue;
286 *eol_p = ++eol;
287 ch = *eol;
288 *eol = '\0';
289 return ch;
290 }
291 return 0;
292 }
294 static struct {
295 const char *field;
296 size_t len;
297 } header_field[] = {
298 { "author ", 7 },
299 { "committer ", 10 },
300 };
302 static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
303 enum grep_context ctx,
304 regmatch_t *pmatch, int eflags)
305 {
306 int hit = 0;
307 int saved_ch = 0;
308 const char *start = bol;
310 if ((p->token != GREP_PATTERN) &&
311 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
312 return 0;
314 if (p->token == GREP_PATTERN_HEAD) {
315 const char *field;
316 size_t len;
317 assert(p->field < ARRAY_SIZE(header_field));
318 field = header_field[p->field].field;
319 len = header_field[p->field].len;
320 if (strncmp(bol, field, len))
321 return 0;
322 bol += len;
323 saved_ch = strip_timestamp(bol, &eol);
324 }
326 again:
327 if (p->fixed)
328 hit = !fixmatch(p->pattern, bol, pmatch);
329 else
330 hit = !regexec(&p->regexp, bol, 1, pmatch, eflags);
332 if (hit && p->word_regexp) {
333 if ((pmatch[0].rm_so < 0) ||
334 (eol - bol) < pmatch[0].rm_so ||
335 (pmatch[0].rm_eo < 0) ||
336 (eol - bol) < pmatch[0].rm_eo)
337 die("regexp returned nonsense");
339 /* Match beginning must be either beginning of the
340 * line, or at word boundary (i.e. the last char must
341 * not be a word char). Similarly, match end must be
342 * either end of the line, or at word boundary
343 * (i.e. the next char must not be a word char).
344 */
345 if ( ((pmatch[0].rm_so == 0) ||
346 !word_char(bol[pmatch[0].rm_so-1])) &&
347 ((pmatch[0].rm_eo == (eol-bol)) ||
348 !word_char(bol[pmatch[0].rm_eo])) )
349 ;
350 else
351 hit = 0;
353 /* Words consist of at least one character. */
354 if (pmatch->rm_so == pmatch->rm_eo)
355 hit = 0;
357 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
358 /* There could be more than one match on the
359 * line, and the first match might not be
360 * strict word match. But later ones could be!
361 * Forward to the next possible start, i.e. the
362 * next position following a non-word char.
363 */
364 bol = pmatch[0].rm_so + bol + 1;
365 while (word_char(bol[-1]) && bol < eol)
366 bol++;
367 eflags |= REG_NOTBOL;
368 if (bol < eol)
369 goto again;
370 }
371 }
372 if (p->token == GREP_PATTERN_HEAD && saved_ch)
373 *eol = saved_ch;
374 if (hit) {
375 pmatch[0].rm_so += bol - start;
376 pmatch[0].rm_eo += bol - start;
377 }
378 return hit;
379 }
381 static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
382 enum grep_context ctx, int collect_hits)
383 {
384 int h = 0;
385 regmatch_t match;
387 if (!x)
388 die("Not a valid grep expression");
389 switch (x->node) {
390 case GREP_NODE_ATOM:
391 h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
392 break;
393 case GREP_NODE_NOT:
394 h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
395 break;
396 case GREP_NODE_AND:
397 if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
398 return 0;
399 h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
400 break;
401 case GREP_NODE_OR:
402 if (!collect_hits)
403 return (match_expr_eval(x->u.binary.left,
404 bol, eol, ctx, 0) ||
405 match_expr_eval(x->u.binary.right,
406 bol, eol, ctx, 0));
407 h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
408 x->u.binary.left->hit |= h;
409 h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
410 break;
411 default:
412 die("Unexpected node type (internal error) %d", x->node);
413 }
414 if (collect_hits)
415 x->hit |= h;
416 return h;
417 }
419 static int match_expr(struct grep_opt *opt, char *bol, char *eol,
420 enum grep_context ctx, int collect_hits)
421 {
422 struct grep_expr *x = opt->pattern_expression;
423 return match_expr_eval(x, bol, eol, ctx, collect_hits);
424 }
426 static int match_line(struct grep_opt *opt, char *bol, char *eol,
427 enum grep_context ctx, int collect_hits)
428 {
429 struct grep_pat *p;
430 regmatch_t match;
432 if (opt->extended)
433 return match_expr(opt, bol, eol, ctx, collect_hits);
435 /* we do not call with collect_hits without being extended */
436 for (p = opt->pattern_list; p; p = p->next) {
437 if (match_one_pattern(p, bol, eol, ctx, &match, 0))
438 return 1;
439 }
440 return 0;
441 }
443 static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
444 enum grep_context ctx,
445 regmatch_t *pmatch, int eflags)
446 {
447 regmatch_t match;
449 if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
450 return 0;
451 if (match.rm_so < 0 || match.rm_eo < 0)
452 return 0;
453 if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
454 if (match.rm_so > pmatch->rm_so)
455 return 1;
456 if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
457 return 1;
458 }
459 pmatch->rm_so = match.rm_so;
460 pmatch->rm_eo = match.rm_eo;
461 return 1;
462 }
464 static int next_match(struct grep_opt *opt, char *bol, char *eol,
465 enum grep_context ctx, regmatch_t *pmatch, int eflags)
466 {
467 struct grep_pat *p;
468 int hit = 0;
470 pmatch->rm_so = pmatch->rm_eo = -1;
471 if (bol < eol) {
472 for (p = opt->pattern_list; p; p = p->next) {
473 switch (p->token) {
474 case GREP_PATTERN: /* atom */
475 case GREP_PATTERN_HEAD:
476 case GREP_PATTERN_BODY:
477 hit |= match_next_pattern(p, bol, eol, ctx,
478 pmatch, eflags);
479 break;
480 default:
481 break;
482 }
483 }
484 }
485 return hit;
486 }
488 static void show_line(struct grep_opt *opt, char *bol, char *eol,
489 const char *name, unsigned lno, char sign)
490 {
491 int rest = eol - bol;
493 if (opt->pre_context || opt->post_context || opt->funcname) {
494 if (opt->last_shown == 0) {
495 if (opt->show_hunk_mark)
496 fputs(opt->funcname ? "==\n" : "--\n", stdout);
497 else
498 opt->show_hunk_mark = 1;
499 } else if (lno > opt->last_shown + 1) {
500 if (opt->pre_context || opt->post_context)
501 fputs((sign == '=') ? "==\n" : "--\n", stdout);
502 else if (sign == '=')
503 fputs("==\n", stdout);
504 }
505 }
506 opt->last_shown = lno;
508 if (opt->null_following_name)
509 sign = '\0';
510 if (opt->pathname)
511 printf("%s%c", name, sign);
512 if (opt->linenum)
513 printf("%d%c", lno, sign);
514 if (opt->color) {
515 regmatch_t match;
516 enum grep_context ctx = GREP_CONTEXT_BODY;
517 int ch = *eol;
518 int eflags = 0;
520 *eol = '\0';
521 while (next_match(opt, bol, eol, ctx, &match, eflags)) {
522 if (match.rm_so == match.rm_eo)
523 break;
524 printf("%.*s%s%.*s%s",
525 (int)match.rm_so, bol,
526 opt->color_match,
527 (int)(match.rm_eo - match.rm_so), bol + match.rm_so,
528 GIT_COLOR_RESET);
529 bol += match.rm_eo;
530 rest -= match.rm_eo;
531 eflags = REG_NOTBOL;
532 }
533 *eol = ch;
534 }
535 printf("%.*s\n", rest, bol);
536 }
538 static int match_funcname(char *bol, char *eol)
539 {
540 if (bol == eol)
541 return 0;
542 if (isalpha(*bol) || *bol == '_' || *bol == '$')
543 return 1;
544 return 0;
545 }
547 static void show_funcname_line(struct grep_opt *opt, const char *name,
548 char *buf, char *bol, unsigned lno)
549 {
550 while (bol > buf) {
551 char *eol = --bol;
553 while (bol > buf && bol[-1] != '\n')
554 bol--;
555 lno--;
557 if (lno <= opt->last_shown)
558 break;
560 if (match_funcname(bol, eol)) {
561 show_line(opt, bol, eol, name, lno, '=');
562 break;
563 }
564 }
565 }
567 static void show_pre_context(struct grep_opt *opt, const char *name, char *buf,
568 char *bol, unsigned lno)
569 {
570 unsigned cur = lno, from = 1, funcname_lno = 0;
571 int funcname_needed = opt->funcname;
573 if (opt->pre_context < lno)
574 from = lno - opt->pre_context;
575 if (from <= opt->last_shown)
576 from = opt->last_shown + 1;
578 /* Rewind. */
579 while (bol > buf && cur > from) {
580 char *eol = --bol;
582 while (bol > buf && bol[-1] != '\n')
583 bol--;
584 cur--;
585 if (funcname_needed && match_funcname(bol, eol)) {
586 funcname_lno = cur;
587 funcname_needed = 0;
588 }
589 }
591 /* We need to look even further back to find a function signature. */
592 if (opt->funcname && funcname_needed)
593 show_funcname_line(opt, name, buf, bol, cur);
595 /* Back forward. */
596 while (cur < lno) {
597 char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
599 while (*eol != '\n')
600 eol++;
601 show_line(opt, bol, eol, name, cur, sign);
602 bol = eol + 1;
603 cur++;
604 }
605 }
607 static int grep_buffer_1(struct grep_opt *opt, const char *name,
608 char *buf, unsigned long size, int collect_hits)
609 {
610 char *bol = buf;
611 unsigned long left = size;
612 unsigned lno = 1;
613 unsigned last_hit = 0;
614 int binary_match_only = 0;
615 unsigned count = 0;
616 enum grep_context ctx = GREP_CONTEXT_HEAD;
618 opt->last_shown = 0;
620 if (buffer_is_binary(buf, size)) {
621 switch (opt->binary) {
622 case GREP_BINARY_DEFAULT:
623 binary_match_only = 1;
624 break;
625 case GREP_BINARY_NOMATCH:
626 return 0; /* Assume unmatch */
627 break;
628 default:
629 break;
630 }
631 }
633 while (left) {
634 char *eol, ch;
635 int hit;
637 eol = end_of_line(bol, &left);
638 ch = *eol;
639 *eol = 0;
641 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
642 ctx = GREP_CONTEXT_BODY;
644 hit = match_line(opt, bol, eol, ctx, collect_hits);
645 *eol = ch;
647 if (collect_hits)
648 goto next_line;
650 /* "grep -v -e foo -e bla" should list lines
651 * that do not have either, so inversion should
652 * be done outside.
653 */
654 if (opt->invert)
655 hit = !hit;
656 if (opt->unmatch_name_only) {
657 if (hit)
658 return 0;
659 goto next_line;
660 }
661 if (hit) {
662 count++;
663 if (opt->status_only)
664 return 1;
665 if (binary_match_only) {
666 printf("Binary file %s matches\n", name);
667 return 1;
668 }
669 if (opt->name_only) {
670 show_name(opt, name);
671 return 1;
672 }
673 /* Hit at this line. If we haven't shown the
674 * pre-context lines, we would need to show them.
675 * When asked to do "count", this still show
676 * the context which is nonsense, but the user
677 * deserves to get that ;-).
678 */
679 if (opt->pre_context)
680 show_pre_context(opt, name, buf, bol, lno);
681 else if (opt->funcname)
682 show_funcname_line(opt, name, buf, bol, lno);
683 if (!opt->count)
684 show_line(opt, bol, eol, name, lno, ':');
685 last_hit = lno;
686 }
687 else if (last_hit &&
688 lno <= last_hit + opt->post_context) {
689 /* If the last hit is within the post context,
690 * we need to show this line.
691 */
692 show_line(opt, bol, eol, name, lno, '-');
693 }
695 next_line:
696 bol = eol + 1;
697 if (!left)
698 break;
699 left--;
700 lno++;
701 }
703 if (collect_hits)
704 return 0;
706 if (opt->status_only)
707 return 0;
708 if (opt->unmatch_name_only) {
709 /* We did not see any hit, so we want to show this */
710 show_name(opt, name);
711 return 1;
712 }
714 /* NEEDSWORK:
715 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
716 * which feels mostly useless but sometimes useful. Maybe
717 * make it another option? For now suppress them.
718 */
719 if (opt->count && count)
720 printf("%s%c%u\n", name,
721 opt->null_following_name ? '\0' : ':', count);
722 return !!last_hit;
723 }
725 static void clr_hit_marker(struct grep_expr *x)
726 {
727 /* All-hit markers are meaningful only at the very top level
728 * OR node.
729 */
730 while (1) {
731 x->hit = 0;
732 if (x->node != GREP_NODE_OR)
733 return;
734 x->u.binary.left->hit = 0;
735 x = x->u.binary.right;
736 }
737 }
739 static int chk_hit_marker(struct grep_expr *x)
740 {
741 /* Top level nodes have hit markers. See if they all are hits */
742 while (1) {
743 if (x->node != GREP_NODE_OR)
744 return x->hit;
745 if (!x->u.binary.left->hit)
746 return 0;
747 x = x->u.binary.right;
748 }
749 }
751 int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size)
752 {
753 /*
754 * we do not have to do the two-pass grep when we do not check
755 * buffer-wide "all-match".
756 */
757 if (!opt->all_match)
758 return grep_buffer_1(opt, name, buf, size, 0);
760 /* Otherwise the toplevel "or" terms hit a bit differently.
761 * We first clear hit markers from them.
762 */
763 clr_hit_marker(opt->pattern_expression);
764 grep_buffer_1(opt, name, buf, size, 1);
766 if (!chk_hit_marker(opt->pattern_expression))
767 return 0;
769 return grep_buffer_1(opt, name, buf, size, 0);
770 }