log: document use of multiple commit limiting options
[git/jnareb-git.git] / grep.c
blob925aa92f89bb767e63de70a4a17174f6bab4455e
1 #include "cache.h"
2 #include "grep.h"
3 #include "userdiff.h"
4 #include "xdiff-interface.h"
6 static struct grep_pat *create_grep_pat(const char *pat, size_t patlen,
7 const char *origin, int no,
8 enum grep_pat_token t,
9 enum grep_header_field field)
11 struct grep_pat *p = xcalloc(1, sizeof(*p));
12 p->pattern = xmemdupz(pat, patlen);
13 p->patternlen = patlen;
14 p->origin = origin;
15 p->no = no;
16 p->token = t;
17 p->field = field;
18 return p;
21 static void do_append_grep_pat(struct grep_pat ***tail, struct grep_pat *p)
23 **tail = p;
24 *tail = &p->next;
25 p->next = NULL;
27 switch (p->token) {
28 case GREP_PATTERN: /* atom */
29 case GREP_PATTERN_HEAD:
30 case GREP_PATTERN_BODY:
31 for (;;) {
32 struct grep_pat *new_pat;
33 size_t len = 0;
34 char *cp = p->pattern + p->patternlen, *nl = NULL;
35 while (++len <= p->patternlen) {
36 if (*(--cp) == '\n') {
37 nl = cp;
38 break;
41 if (!nl)
42 break;
43 new_pat = create_grep_pat(nl + 1, len - 1, p->origin,
44 p->no, p->token, p->field);
45 new_pat->next = p->next;
46 if (!p->next)
47 *tail = &new_pat->next;
48 p->next = new_pat;
49 *nl = '\0';
50 p->patternlen -= len;
52 break;
53 default:
54 break;
58 void append_header_grep_pattern(struct grep_opt *opt,
59 enum grep_header_field field, const char *pat)
61 struct grep_pat *p = create_grep_pat(pat, strlen(pat), "header", 0,
62 GREP_PATTERN_HEAD, field);
63 do_append_grep_pat(&opt->header_tail, p);
66 void append_grep_pattern(struct grep_opt *opt, const char *pat,
67 const char *origin, int no, enum grep_pat_token t)
69 append_grep_pat(opt, pat, strlen(pat), origin, no, t);
72 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen,
73 const char *origin, int no, enum grep_pat_token t)
75 struct grep_pat *p = create_grep_pat(pat, patlen, origin, no, t, 0);
76 do_append_grep_pat(&opt->pattern_tail, p);
79 struct grep_opt *grep_opt_dup(const struct grep_opt *opt)
81 struct grep_pat *pat;
82 struct grep_opt *ret = xmalloc(sizeof(struct grep_opt));
83 *ret = *opt;
85 ret->pattern_list = NULL;
86 ret->pattern_tail = &ret->pattern_list;
88 for(pat = opt->pattern_list; pat != NULL; pat = pat->next)
90 if(pat->token == GREP_PATTERN_HEAD)
91 append_header_grep_pattern(ret, pat->field,
92 pat->pattern);
93 else
94 append_grep_pat(ret, pat->pattern, pat->patternlen,
95 pat->origin, pat->no, pat->token);
98 return ret;
101 static NORETURN void compile_regexp_failed(const struct grep_pat *p,
102 const char *error)
104 char where[1024];
106 if (p->no)
107 sprintf(where, "In '%s' at %d, ", p->origin, p->no);
108 else if (p->origin)
109 sprintf(where, "%s, ", p->origin);
110 else
111 where[0] = 0;
113 die("%s'%s': %s", where, p->pattern, error);
116 #ifdef USE_LIBPCRE
117 static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
119 const char *error;
120 int erroffset;
121 int options = PCRE_MULTILINE;
123 if (opt->ignore_case)
124 options |= PCRE_CASELESS;
126 p->pcre_regexp = pcre_compile(p->pattern, options, &error, &erroffset,
127 NULL);
128 if (!p->pcre_regexp)
129 compile_regexp_failed(p, error);
131 p->pcre_extra_info = pcre_study(p->pcre_regexp, 0, &error);
132 if (!p->pcre_extra_info && error)
133 die("%s", error);
136 static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
137 regmatch_t *match, int eflags)
139 int ovector[30], ret, flags = 0;
141 if (eflags & REG_NOTBOL)
142 flags |= PCRE_NOTBOL;
144 ret = pcre_exec(p->pcre_regexp, p->pcre_extra_info, line, eol - line,
145 0, flags, ovector, ARRAY_SIZE(ovector));
146 if (ret < 0 && ret != PCRE_ERROR_NOMATCH)
147 die("pcre_exec failed with error code %d", ret);
148 if (ret > 0) {
149 ret = 0;
150 match->rm_so = ovector[0];
151 match->rm_eo = ovector[1];
154 return ret;
157 static void free_pcre_regexp(struct grep_pat *p)
159 pcre_free(p->pcre_regexp);
160 pcre_free(p->pcre_extra_info);
162 #else /* !USE_LIBPCRE */
163 static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
165 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
168 static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
169 regmatch_t *match, int eflags)
171 return 1;
174 static void free_pcre_regexp(struct grep_pat *p)
177 #endif /* !USE_LIBPCRE */
179 static int is_fixed(const char *s, size_t len)
181 size_t i;
183 /* regcomp cannot accept patterns with NULs so we
184 * consider any pattern containing a NUL fixed.
186 if (memchr(s, 0, len))
187 return 1;
189 for (i = 0; i < len; i++) {
190 if (is_regex_special(s[i]))
191 return 0;
194 return 1;
197 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
199 int err;
201 p->word_regexp = opt->word_regexp;
202 p->ignore_case = opt->ignore_case;
204 if (opt->fixed || is_fixed(p->pattern, p->patternlen))
205 p->fixed = 1;
206 else
207 p->fixed = 0;
209 if (p->fixed) {
210 if (opt->regflags & REG_ICASE || p->ignore_case)
211 p->kws = kwsalloc(tolower_trans_tbl);
212 else
213 p->kws = kwsalloc(NULL);
214 kwsincr(p->kws, p->pattern, p->patternlen);
215 kwsprep(p->kws);
216 return;
219 if (opt->pcre) {
220 compile_pcre_regexp(p, opt);
221 return;
224 err = regcomp(&p->regexp, p->pattern, opt->regflags);
225 if (err) {
226 char errbuf[1024];
227 regerror(err, &p->regexp, errbuf, 1024);
228 regfree(&p->regexp);
229 compile_regexp_failed(p, errbuf);
233 static struct grep_expr *compile_pattern_or(struct grep_pat **);
234 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
236 struct grep_pat *p;
237 struct grep_expr *x;
239 p = *list;
240 if (!p)
241 return NULL;
242 switch (p->token) {
243 case GREP_PATTERN: /* atom */
244 case GREP_PATTERN_HEAD:
245 case GREP_PATTERN_BODY:
246 x = xcalloc(1, sizeof (struct grep_expr));
247 x->node = GREP_NODE_ATOM;
248 x->u.atom = p;
249 *list = p->next;
250 return x;
251 case GREP_OPEN_PAREN:
252 *list = p->next;
253 x = compile_pattern_or(list);
254 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
255 die("unmatched parenthesis");
256 *list = (*list)->next;
257 return x;
258 default:
259 return NULL;
263 static struct grep_expr *compile_pattern_not(struct grep_pat **list)
265 struct grep_pat *p;
266 struct grep_expr *x;
268 p = *list;
269 if (!p)
270 return NULL;
271 switch (p->token) {
272 case GREP_NOT:
273 if (!p->next)
274 die("--not not followed by pattern expression");
275 *list = p->next;
276 x = xcalloc(1, sizeof (struct grep_expr));
277 x->node = GREP_NODE_NOT;
278 x->u.unary = compile_pattern_not(list);
279 if (!x->u.unary)
280 die("--not followed by non pattern expression");
281 return x;
282 default:
283 return compile_pattern_atom(list);
287 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
289 struct grep_pat *p;
290 struct grep_expr *x, *y, *z;
292 x = compile_pattern_not(list);
293 p = *list;
294 if (p && p->token == GREP_AND) {
295 if (!p->next)
296 die("--and not followed by pattern expression");
297 *list = p->next;
298 y = compile_pattern_and(list);
299 if (!y)
300 die("--and not followed by pattern expression");
301 z = xcalloc(1, sizeof (struct grep_expr));
302 z->node = GREP_NODE_AND;
303 z->u.binary.left = x;
304 z->u.binary.right = y;
305 return z;
307 return x;
310 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
312 struct grep_pat *p;
313 struct grep_expr *x, *y, *z;
315 x = compile_pattern_and(list);
316 p = *list;
317 if (x && p && p->token != GREP_CLOSE_PAREN) {
318 y = compile_pattern_or(list);
319 if (!y)
320 die("not a pattern expression %s", p->pattern);
321 z = xcalloc(1, sizeof (struct grep_expr));
322 z->node = GREP_NODE_OR;
323 z->u.binary.left = x;
324 z->u.binary.right = y;
325 return z;
327 return x;
330 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
332 return compile_pattern_or(list);
335 static void indent(int in)
337 while (in-- > 0)
338 fputc(' ', stderr);
341 static void dump_grep_pat(struct grep_pat *p)
343 switch (p->token) {
344 case GREP_AND: fprintf(stderr, "*and*"); break;
345 case GREP_OPEN_PAREN: fprintf(stderr, "*(*"); break;
346 case GREP_CLOSE_PAREN: fprintf(stderr, "*)*"); break;
347 case GREP_NOT: fprintf(stderr, "*not*"); break;
348 case GREP_OR: fprintf(stderr, "*or*"); break;
350 case GREP_PATTERN: fprintf(stderr, "pattern"); break;
351 case GREP_PATTERN_HEAD: fprintf(stderr, "pattern_head"); break;
352 case GREP_PATTERN_BODY: fprintf(stderr, "pattern_body"); break;
355 switch (p->token) {
356 default: break;
357 case GREP_PATTERN_HEAD:
358 fprintf(stderr, "<head %d>", p->field); break;
359 case GREP_PATTERN_BODY:
360 fprintf(stderr, "<body>"); break;
362 switch (p->token) {
363 default: break;
364 case GREP_PATTERN_HEAD:
365 case GREP_PATTERN_BODY:
366 case GREP_PATTERN:
367 fprintf(stderr, "%.*s", (int)p->patternlen, p->pattern);
368 break;
370 fputc('\n', stderr);
373 static void dump_grep_expression_1(struct grep_expr *x, int in)
375 indent(in);
376 switch (x->node) {
377 case GREP_NODE_TRUE:
378 fprintf(stderr, "true\n");
379 break;
380 case GREP_NODE_ATOM:
381 dump_grep_pat(x->u.atom);
382 break;
383 case GREP_NODE_NOT:
384 fprintf(stderr, "(not\n");
385 dump_grep_expression_1(x->u.unary, in+1);
386 indent(in);
387 fprintf(stderr, ")\n");
388 break;
389 case GREP_NODE_AND:
390 fprintf(stderr, "(and\n");
391 dump_grep_expression_1(x->u.binary.left, in+1);
392 dump_grep_expression_1(x->u.binary.right, in+1);
393 indent(in);
394 fprintf(stderr, ")\n");
395 break;
396 case GREP_NODE_OR:
397 fprintf(stderr, "(or\n");
398 dump_grep_expression_1(x->u.binary.left, in+1);
399 dump_grep_expression_1(x->u.binary.right, in+1);
400 indent(in);
401 fprintf(stderr, ")\n");
402 break;
406 void dump_grep_expression(struct grep_opt *opt)
408 struct grep_expr *x = opt->pattern_expression;
410 if (opt->all_match)
411 fprintf(stderr, "[all-match]\n");
412 dump_grep_expression_1(x, 0);
413 fflush(NULL);
416 static struct grep_expr *grep_true_expr(void)
418 struct grep_expr *z = xcalloc(1, sizeof(*z));
419 z->node = GREP_NODE_TRUE;
420 return z;
423 static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
425 struct grep_expr *z = xcalloc(1, sizeof(*z));
426 z->node = GREP_NODE_OR;
427 z->u.binary.left = left;
428 z->u.binary.right = right;
429 return z;
432 static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
434 struct grep_pat *p;
435 struct grep_expr *header_expr;
436 struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]);
437 enum grep_header_field fld;
439 if (!opt->header_list)
440 return NULL;
442 for (p = opt->header_list; p; p = p->next) {
443 if (p->token != GREP_PATTERN_HEAD)
444 die("bug: a non-header pattern in grep header list.");
445 if (p->field < 0 || GREP_HEADER_FIELD_MAX <= p->field)
446 die("bug: unknown header field %d", p->field);
447 compile_regexp(p, opt);
450 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++)
451 header_group[fld] = NULL;
453 for (p = opt->header_list; p; p = p->next) {
454 struct grep_expr *h;
455 struct grep_pat *pp = p;
457 h = compile_pattern_atom(&pp);
458 if (!h || pp != p->next)
459 die("bug: malformed header expr");
460 if (!header_group[p->field]) {
461 header_group[p->field] = h;
462 continue;
464 header_group[p->field] = grep_or_expr(h, header_group[p->field]);
467 header_expr = NULL;
469 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) {
470 if (!header_group[fld])
471 continue;
472 if (!header_expr)
473 header_expr = grep_true_expr();
474 header_expr = grep_or_expr(header_group[fld], header_expr);
476 return header_expr;
479 static struct grep_expr *grep_splice_or(struct grep_expr *x, struct grep_expr *y)
481 struct grep_expr *z = x;
483 while (x) {
484 assert(x->node == GREP_NODE_OR);
485 if (x->u.binary.right &&
486 x->u.binary.right->node == GREP_NODE_TRUE) {
487 x->u.binary.right = y;
488 break;
490 x = x->u.binary.right;
492 return z;
495 static void compile_grep_patterns_real(struct grep_opt *opt)
497 struct grep_pat *p;
498 struct grep_expr *header_expr = prep_header_patterns(opt);
500 for (p = opt->pattern_list; p; p = p->next) {
501 switch (p->token) {
502 case GREP_PATTERN: /* atom */
503 case GREP_PATTERN_HEAD:
504 case GREP_PATTERN_BODY:
505 compile_regexp(p, opt);
506 break;
507 default:
508 opt->extended = 1;
509 break;
513 if (opt->all_match || header_expr)
514 opt->extended = 1;
515 else if (!opt->extended && !opt->debug)
516 return;
518 p = opt->pattern_list;
519 if (p)
520 opt->pattern_expression = compile_pattern_expr(&p);
521 if (p)
522 die("incomplete pattern expression: %s", p->pattern);
524 if (!header_expr)
525 return;
527 if (!opt->pattern_expression)
528 opt->pattern_expression = header_expr;
529 else if (opt->all_match)
530 opt->pattern_expression = grep_splice_or(header_expr,
531 opt->pattern_expression);
532 else
533 opt->pattern_expression = grep_or_expr(opt->pattern_expression,
534 header_expr);
535 opt->all_match = 1;
538 void compile_grep_patterns(struct grep_opt *opt)
540 compile_grep_patterns_real(opt);
541 if (opt->debug)
542 dump_grep_expression(opt);
545 static void free_pattern_expr(struct grep_expr *x)
547 switch (x->node) {
548 case GREP_NODE_TRUE:
549 case GREP_NODE_ATOM:
550 break;
551 case GREP_NODE_NOT:
552 free_pattern_expr(x->u.unary);
553 break;
554 case GREP_NODE_AND:
555 case GREP_NODE_OR:
556 free_pattern_expr(x->u.binary.left);
557 free_pattern_expr(x->u.binary.right);
558 break;
560 free(x);
563 void free_grep_patterns(struct grep_opt *opt)
565 struct grep_pat *p, *n;
567 for (p = opt->pattern_list; p; p = n) {
568 n = p->next;
569 switch (p->token) {
570 case GREP_PATTERN: /* atom */
571 case GREP_PATTERN_HEAD:
572 case GREP_PATTERN_BODY:
573 if (p->kws)
574 kwsfree(p->kws);
575 else if (p->pcre_regexp)
576 free_pcre_regexp(p);
577 else
578 regfree(&p->regexp);
579 free(p->pattern);
580 break;
581 default:
582 break;
584 free(p);
587 if (!opt->extended)
588 return;
589 free_pattern_expr(opt->pattern_expression);
592 static char *end_of_line(char *cp, unsigned long *left)
594 unsigned long l = *left;
595 while (l && *cp != '\n') {
596 l--;
597 cp++;
599 *left = l;
600 return cp;
603 static int word_char(char ch)
605 return isalnum(ch) || ch == '_';
608 static void output_color(struct grep_opt *opt, const void *data, size_t size,
609 const char *color)
611 if (want_color(opt->color) && color && color[0]) {
612 opt->output(opt, color, strlen(color));
613 opt->output(opt, data, size);
614 opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
615 } else
616 opt->output(opt, data, size);
619 static void output_sep(struct grep_opt *opt, char sign)
621 if (opt->null_following_name)
622 opt->output(opt, "\0", 1);
623 else
624 output_color(opt, &sign, 1, opt->color_sep);
627 static void show_name(struct grep_opt *opt, const char *name)
629 output_color(opt, name, strlen(name), opt->color_filename);
630 opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
633 static int fixmatch(struct grep_pat *p, char *line, char *eol,
634 regmatch_t *match)
636 struct kwsmatch kwsm;
637 size_t offset = kwsexec(p->kws, line, eol - line, &kwsm);
638 if (offset == -1) {
639 match->rm_so = match->rm_eo = -1;
640 return REG_NOMATCH;
641 } else {
642 match->rm_so = offset;
643 match->rm_eo = match->rm_so + kwsm.size[0];
644 return 0;
648 static int regmatch(const regex_t *preg, char *line, char *eol,
649 regmatch_t *match, int eflags)
651 #ifdef REG_STARTEND
652 match->rm_so = 0;
653 match->rm_eo = eol - line;
654 eflags |= REG_STARTEND;
655 #endif
656 return regexec(preg, line, 1, match, eflags);
659 static int patmatch(struct grep_pat *p, char *line, char *eol,
660 regmatch_t *match, int eflags)
662 int hit;
664 if (p->fixed)
665 hit = !fixmatch(p, line, eol, match);
666 else if (p->pcre_regexp)
667 hit = !pcrematch(p, line, eol, match, eflags);
668 else
669 hit = !regmatch(&p->regexp, line, eol, match, eflags);
671 return hit;
674 static int strip_timestamp(char *bol, char **eol_p)
676 char *eol = *eol_p;
677 int ch;
679 while (bol < --eol) {
680 if (*eol != '>')
681 continue;
682 *eol_p = ++eol;
683 ch = *eol;
684 *eol = '\0';
685 return ch;
687 return 0;
690 static struct {
691 const char *field;
692 size_t len;
693 } header_field[] = {
694 { "author ", 7 },
695 { "committer ", 10 },
698 static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
699 enum grep_context ctx,
700 regmatch_t *pmatch, int eflags)
702 int hit = 0;
703 int saved_ch = 0;
704 const char *start = bol;
706 if ((p->token != GREP_PATTERN) &&
707 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
708 return 0;
710 if (p->token == GREP_PATTERN_HEAD) {
711 const char *field;
712 size_t len;
713 assert(p->field < ARRAY_SIZE(header_field));
714 field = header_field[p->field].field;
715 len = header_field[p->field].len;
716 if (strncmp(bol, field, len))
717 return 0;
718 bol += len;
719 saved_ch = strip_timestamp(bol, &eol);
722 again:
723 hit = patmatch(p, bol, eol, pmatch, eflags);
725 if (hit && p->word_regexp) {
726 if ((pmatch[0].rm_so < 0) ||
727 (eol - bol) < pmatch[0].rm_so ||
728 (pmatch[0].rm_eo < 0) ||
729 (eol - bol) < pmatch[0].rm_eo)
730 die("regexp returned nonsense");
732 /* Match beginning must be either beginning of the
733 * line, or at word boundary (i.e. the last char must
734 * not be a word char). Similarly, match end must be
735 * either end of the line, or at word boundary
736 * (i.e. the next char must not be a word char).
738 if ( ((pmatch[0].rm_so == 0) ||
739 !word_char(bol[pmatch[0].rm_so-1])) &&
740 ((pmatch[0].rm_eo == (eol-bol)) ||
741 !word_char(bol[pmatch[0].rm_eo])) )
743 else
744 hit = 0;
746 /* Words consist of at least one character. */
747 if (pmatch->rm_so == pmatch->rm_eo)
748 hit = 0;
750 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
751 /* There could be more than one match on the
752 * line, and the first match might not be
753 * strict word match. But later ones could be!
754 * Forward to the next possible start, i.e. the
755 * next position following a non-word char.
757 bol = pmatch[0].rm_so + bol + 1;
758 while (word_char(bol[-1]) && bol < eol)
759 bol++;
760 eflags |= REG_NOTBOL;
761 if (bol < eol)
762 goto again;
765 if (p->token == GREP_PATTERN_HEAD && saved_ch)
766 *eol = saved_ch;
767 if (hit) {
768 pmatch[0].rm_so += bol - start;
769 pmatch[0].rm_eo += bol - start;
771 return hit;
774 static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
775 enum grep_context ctx, int collect_hits)
777 int h = 0;
778 regmatch_t match;
780 if (!x)
781 die("Not a valid grep expression");
782 switch (x->node) {
783 case GREP_NODE_TRUE:
784 h = 1;
785 break;
786 case GREP_NODE_ATOM:
787 h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
788 break;
789 case GREP_NODE_NOT:
790 h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
791 break;
792 case GREP_NODE_AND:
793 if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
794 return 0;
795 h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
796 break;
797 case GREP_NODE_OR:
798 if (!collect_hits)
799 return (match_expr_eval(x->u.binary.left,
800 bol, eol, ctx, 0) ||
801 match_expr_eval(x->u.binary.right,
802 bol, eol, ctx, 0));
803 h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
804 x->u.binary.left->hit |= h;
805 h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
806 break;
807 default:
808 die("Unexpected node type (internal error) %d", x->node);
810 if (collect_hits)
811 x->hit |= h;
812 return h;
815 static int match_expr(struct grep_opt *opt, char *bol, char *eol,
816 enum grep_context ctx, int collect_hits)
818 struct grep_expr *x = opt->pattern_expression;
819 return match_expr_eval(x, bol, eol, ctx, collect_hits);
822 static int match_line(struct grep_opt *opt, char *bol, char *eol,
823 enum grep_context ctx, int collect_hits)
825 struct grep_pat *p;
826 regmatch_t match;
828 if (opt->extended)
829 return match_expr(opt, bol, eol, ctx, collect_hits);
831 /* we do not call with collect_hits without being extended */
832 for (p = opt->pattern_list; p; p = p->next) {
833 if (match_one_pattern(p, bol, eol, ctx, &match, 0))
834 return 1;
836 return 0;
839 static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
840 enum grep_context ctx,
841 regmatch_t *pmatch, int eflags)
843 regmatch_t match;
845 if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
846 return 0;
847 if (match.rm_so < 0 || match.rm_eo < 0)
848 return 0;
849 if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
850 if (match.rm_so > pmatch->rm_so)
851 return 1;
852 if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
853 return 1;
855 pmatch->rm_so = match.rm_so;
856 pmatch->rm_eo = match.rm_eo;
857 return 1;
860 static int next_match(struct grep_opt *opt, char *bol, char *eol,
861 enum grep_context ctx, regmatch_t *pmatch, int eflags)
863 struct grep_pat *p;
864 int hit = 0;
866 pmatch->rm_so = pmatch->rm_eo = -1;
867 if (bol < eol) {
868 for (p = opt->pattern_list; p; p = p->next) {
869 switch (p->token) {
870 case GREP_PATTERN: /* atom */
871 case GREP_PATTERN_HEAD:
872 case GREP_PATTERN_BODY:
873 hit |= match_next_pattern(p, bol, eol, ctx,
874 pmatch, eflags);
875 break;
876 default:
877 break;
881 return hit;
884 static void show_line(struct grep_opt *opt, char *bol, char *eol,
885 const char *name, unsigned lno, char sign)
887 int rest = eol - bol;
888 char *line_color = NULL;
890 if (opt->file_break && opt->last_shown == 0) {
891 if (opt->show_hunk_mark)
892 opt->output(opt, "\n", 1);
893 } else if (opt->pre_context || opt->post_context || opt->funcbody) {
894 if (opt->last_shown == 0) {
895 if (opt->show_hunk_mark) {
896 output_color(opt, "--", 2, opt->color_sep);
897 opt->output(opt, "\n", 1);
899 } else if (lno > opt->last_shown + 1) {
900 output_color(opt, "--", 2, opt->color_sep);
901 opt->output(opt, "\n", 1);
904 if (opt->heading && opt->last_shown == 0) {
905 output_color(opt, name, strlen(name), opt->color_filename);
906 opt->output(opt, "\n", 1);
908 opt->last_shown = lno;
910 if (!opt->heading && opt->pathname) {
911 output_color(opt, name, strlen(name), opt->color_filename);
912 output_sep(opt, sign);
914 if (opt->linenum) {
915 char buf[32];
916 snprintf(buf, sizeof(buf), "%d", lno);
917 output_color(opt, buf, strlen(buf), opt->color_lineno);
918 output_sep(opt, sign);
920 if (opt->color) {
921 regmatch_t match;
922 enum grep_context ctx = GREP_CONTEXT_BODY;
923 int ch = *eol;
924 int eflags = 0;
926 if (sign == ':')
927 line_color = opt->color_selected;
928 else if (sign == '-')
929 line_color = opt->color_context;
930 else if (sign == '=')
931 line_color = opt->color_function;
932 *eol = '\0';
933 while (next_match(opt, bol, eol, ctx, &match, eflags)) {
934 if (match.rm_so == match.rm_eo)
935 break;
937 output_color(opt, bol, match.rm_so, line_color);
938 output_color(opt, bol + match.rm_so,
939 match.rm_eo - match.rm_so,
940 opt->color_match);
941 bol += match.rm_eo;
942 rest -= match.rm_eo;
943 eflags = REG_NOTBOL;
945 *eol = ch;
947 output_color(opt, bol, rest, line_color);
948 opt->output(opt, "\n", 1);
951 #ifndef NO_PTHREADS
952 int grep_use_locks;
955 * This lock protects access to the gitattributes machinery, which is
956 * not thread-safe.
958 pthread_mutex_t grep_attr_mutex;
960 static inline void grep_attr_lock(void)
962 if (grep_use_locks)
963 pthread_mutex_lock(&grep_attr_mutex);
966 static inline void grep_attr_unlock(void)
968 if (grep_use_locks)
969 pthread_mutex_unlock(&grep_attr_mutex);
973 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
975 pthread_mutex_t grep_read_mutex;
977 #else
978 #define grep_attr_lock()
979 #define grep_attr_unlock()
980 #endif
982 static int match_funcname(struct grep_opt *opt, struct grep_source *gs, char *bol, char *eol)
984 xdemitconf_t *xecfg = opt->priv;
985 if (xecfg && !xecfg->find_func) {
986 grep_source_load_driver(gs);
987 if (gs->driver->funcname.pattern) {
988 const struct userdiff_funcname *pe = &gs->driver->funcname;
989 xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
990 } else {
991 xecfg = opt->priv = NULL;
995 if (xecfg) {
996 char buf[1];
997 return xecfg->find_func(bol, eol - bol, buf, 1,
998 xecfg->find_func_priv) >= 0;
1001 if (bol == eol)
1002 return 0;
1003 if (isalpha(*bol) || *bol == '_' || *bol == '$')
1004 return 1;
1005 return 0;
1008 static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
1009 char *bol, unsigned lno)
1011 while (bol > gs->buf) {
1012 char *eol = --bol;
1014 while (bol > gs->buf && bol[-1] != '\n')
1015 bol--;
1016 lno--;
1018 if (lno <= opt->last_shown)
1019 break;
1021 if (match_funcname(opt, gs, bol, eol)) {
1022 show_line(opt, bol, eol, gs->name, lno, '=');
1023 break;
1028 static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
1029 char *bol, char *end, unsigned lno)
1031 unsigned cur = lno, from = 1, funcname_lno = 0;
1032 int funcname_needed = !!opt->funcname;
1034 if (opt->funcbody && !match_funcname(opt, gs, bol, end))
1035 funcname_needed = 2;
1037 if (opt->pre_context < lno)
1038 from = lno - opt->pre_context;
1039 if (from <= opt->last_shown)
1040 from = opt->last_shown + 1;
1042 /* Rewind. */
1043 while (bol > gs->buf &&
1044 cur > (funcname_needed == 2 ? opt->last_shown + 1 : from)) {
1045 char *eol = --bol;
1047 while (bol > gs->buf && bol[-1] != '\n')
1048 bol--;
1049 cur--;
1050 if (funcname_needed && match_funcname(opt, gs, bol, eol)) {
1051 funcname_lno = cur;
1052 funcname_needed = 0;
1056 /* We need to look even further back to find a function signature. */
1057 if (opt->funcname && funcname_needed)
1058 show_funcname_line(opt, gs, bol, cur);
1060 /* Back forward. */
1061 while (cur < lno) {
1062 char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
1064 while (*eol != '\n')
1065 eol++;
1066 show_line(opt, bol, eol, gs->name, cur, sign);
1067 bol = eol + 1;
1068 cur++;
1072 static int should_lookahead(struct grep_opt *opt)
1074 struct grep_pat *p;
1076 if (opt->extended)
1077 return 0; /* punt for too complex stuff */
1078 if (opt->invert)
1079 return 0;
1080 for (p = opt->pattern_list; p; p = p->next) {
1081 if (p->token != GREP_PATTERN)
1082 return 0; /* punt for "header only" and stuff */
1084 return 1;
1087 static int look_ahead(struct grep_opt *opt,
1088 unsigned long *left_p,
1089 unsigned *lno_p,
1090 char **bol_p)
1092 unsigned lno = *lno_p;
1093 char *bol = *bol_p;
1094 struct grep_pat *p;
1095 char *sp, *last_bol;
1096 regoff_t earliest = -1;
1098 for (p = opt->pattern_list; p; p = p->next) {
1099 int hit;
1100 regmatch_t m;
1102 hit = patmatch(p, bol, bol + *left_p, &m, 0);
1103 if (!hit || m.rm_so < 0 || m.rm_eo < 0)
1104 continue;
1105 if (earliest < 0 || m.rm_so < earliest)
1106 earliest = m.rm_so;
1109 if (earliest < 0) {
1110 *bol_p = bol + *left_p;
1111 *left_p = 0;
1112 return 1;
1114 for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
1115 ; /* find the beginning of the line */
1116 last_bol = sp;
1118 for (sp = bol; sp < last_bol; sp++) {
1119 if (*sp == '\n')
1120 lno++;
1122 *left_p -= last_bol - bol;
1123 *bol_p = last_bol;
1124 *lno_p = lno;
1125 return 0;
1128 static void std_output(struct grep_opt *opt, const void *buf, size_t size)
1130 fwrite(buf, size, 1, stdout);
1133 static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits)
1135 char *bol;
1136 unsigned long left;
1137 unsigned lno = 1;
1138 unsigned last_hit = 0;
1139 int binary_match_only = 0;
1140 unsigned count = 0;
1141 int try_lookahead = 0;
1142 int show_function = 0;
1143 enum grep_context ctx = GREP_CONTEXT_HEAD;
1144 xdemitconf_t xecfg;
1146 if (!opt->output)
1147 opt->output = std_output;
1149 if (opt->pre_context || opt->post_context || opt->file_break ||
1150 opt->funcbody) {
1151 /* Show hunk marks, except for the first file. */
1152 if (opt->last_shown)
1153 opt->show_hunk_mark = 1;
1155 * If we're using threads then we can't easily identify
1156 * the first file. Always put hunk marks in that case
1157 * and skip the very first one later in work_done().
1159 if (opt->output != std_output)
1160 opt->show_hunk_mark = 1;
1162 opt->last_shown = 0;
1164 switch (opt->binary) {
1165 case GREP_BINARY_DEFAULT:
1166 if (grep_source_is_binary(gs))
1167 binary_match_only = 1;
1168 break;
1169 case GREP_BINARY_NOMATCH:
1170 if (grep_source_is_binary(gs))
1171 return 0; /* Assume unmatch */
1172 break;
1173 case GREP_BINARY_TEXT:
1174 break;
1175 default:
1176 die("bug: unknown binary handling mode");
1179 memset(&xecfg, 0, sizeof(xecfg));
1180 opt->priv = &xecfg;
1182 try_lookahead = should_lookahead(opt);
1184 if (grep_source_load(gs) < 0)
1185 return 0;
1187 bol = gs->buf;
1188 left = gs->size;
1189 while (left) {
1190 char *eol, ch;
1191 int hit;
1194 * look_ahead() skips quickly to the line that possibly
1195 * has the next hit; don't call it if we need to do
1196 * something more than just skipping the current line
1197 * in response to an unmatch for the current line. E.g.
1198 * inside a post-context window, we will show the current
1199 * line as a context around the previous hit when it
1200 * doesn't hit.
1202 if (try_lookahead
1203 && !(last_hit
1204 && (show_function ||
1205 lno <= last_hit + opt->post_context))
1206 && look_ahead(opt, &left, &lno, &bol))
1207 break;
1208 eol = end_of_line(bol, &left);
1209 ch = *eol;
1210 *eol = 0;
1212 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
1213 ctx = GREP_CONTEXT_BODY;
1215 hit = match_line(opt, bol, eol, ctx, collect_hits);
1216 *eol = ch;
1218 if (collect_hits)
1219 goto next_line;
1221 /* "grep -v -e foo -e bla" should list lines
1222 * that do not have either, so inversion should
1223 * be done outside.
1225 if (opt->invert)
1226 hit = !hit;
1227 if (opt->unmatch_name_only) {
1228 if (hit)
1229 return 0;
1230 goto next_line;
1232 if (hit) {
1233 count++;
1234 if (opt->status_only)
1235 return 1;
1236 if (opt->name_only) {
1237 show_name(opt, gs->name);
1238 return 1;
1240 if (opt->count)
1241 goto next_line;
1242 if (binary_match_only) {
1243 opt->output(opt, "Binary file ", 12);
1244 output_color(opt, gs->name, strlen(gs->name),
1245 opt->color_filename);
1246 opt->output(opt, " matches\n", 9);
1247 return 1;
1249 /* Hit at this line. If we haven't shown the
1250 * pre-context lines, we would need to show them.
1252 if (opt->pre_context || opt->funcbody)
1253 show_pre_context(opt, gs, bol, eol, lno);
1254 else if (opt->funcname)
1255 show_funcname_line(opt, gs, bol, lno);
1256 show_line(opt, bol, eol, gs->name, lno, ':');
1257 last_hit = lno;
1258 if (opt->funcbody)
1259 show_function = 1;
1260 goto next_line;
1262 if (show_function && match_funcname(opt, gs, bol, eol))
1263 show_function = 0;
1264 if (show_function ||
1265 (last_hit && lno <= last_hit + opt->post_context)) {
1266 /* If the last hit is within the post context,
1267 * we need to show this line.
1269 show_line(opt, bol, eol, gs->name, lno, '-');
1272 next_line:
1273 bol = eol + 1;
1274 if (!left)
1275 break;
1276 left--;
1277 lno++;
1280 if (collect_hits)
1281 return 0;
1283 if (opt->status_only)
1284 return 0;
1285 if (opt->unmatch_name_only) {
1286 /* We did not see any hit, so we want to show this */
1287 show_name(opt, gs->name);
1288 return 1;
1291 xdiff_clear_find_func(&xecfg);
1292 opt->priv = NULL;
1294 /* NEEDSWORK:
1295 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1296 * which feels mostly useless but sometimes useful. Maybe
1297 * make it another option? For now suppress them.
1299 if (opt->count && count) {
1300 char buf[32];
1301 output_color(opt, gs->name, strlen(gs->name), opt->color_filename);
1302 output_sep(opt, ':');
1303 snprintf(buf, sizeof(buf), "%u\n", count);
1304 opt->output(opt, buf, strlen(buf));
1305 return 1;
1307 return !!last_hit;
1310 static void clr_hit_marker(struct grep_expr *x)
1312 /* All-hit markers are meaningful only at the very top level
1313 * OR node.
1315 while (1) {
1316 x->hit = 0;
1317 if (x->node != GREP_NODE_OR)
1318 return;
1319 x->u.binary.left->hit = 0;
1320 x = x->u.binary.right;
1324 static int chk_hit_marker(struct grep_expr *x)
1326 /* Top level nodes have hit markers. See if they all are hits */
1327 while (1) {
1328 if (x->node != GREP_NODE_OR)
1329 return x->hit;
1330 if (!x->u.binary.left->hit)
1331 return 0;
1332 x = x->u.binary.right;
1336 int grep_source(struct grep_opt *opt, struct grep_source *gs)
1339 * we do not have to do the two-pass grep when we do not check
1340 * buffer-wide "all-match".
1342 if (!opt->all_match)
1343 return grep_source_1(opt, gs, 0);
1345 /* Otherwise the toplevel "or" terms hit a bit differently.
1346 * We first clear hit markers from them.
1348 clr_hit_marker(opt->pattern_expression);
1349 grep_source_1(opt, gs, 1);
1351 if (!chk_hit_marker(opt->pattern_expression))
1352 return 0;
1354 return grep_source_1(opt, gs, 0);
1357 int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size)
1359 struct grep_source gs;
1360 int r;
1362 grep_source_init(&gs, GREP_SOURCE_BUF, NULL, NULL);
1363 gs.buf = buf;
1364 gs.size = size;
1366 r = grep_source(opt, &gs);
1368 grep_source_clear(&gs);
1369 return r;
1372 void grep_source_init(struct grep_source *gs, enum grep_source_type type,
1373 const char *name, const void *identifier)
1375 gs->type = type;
1376 gs->name = name ? xstrdup(name) : NULL;
1377 gs->buf = NULL;
1378 gs->size = 0;
1379 gs->driver = NULL;
1381 switch (type) {
1382 case GREP_SOURCE_FILE:
1383 gs->identifier = xstrdup(identifier);
1384 break;
1385 case GREP_SOURCE_SHA1:
1386 gs->identifier = xmalloc(20);
1387 memcpy(gs->identifier, identifier, 20);
1388 break;
1389 case GREP_SOURCE_BUF:
1390 gs->identifier = NULL;
1394 void grep_source_clear(struct grep_source *gs)
1396 free(gs->name);
1397 gs->name = NULL;
1398 free(gs->identifier);
1399 gs->identifier = NULL;
1400 grep_source_clear_data(gs);
1403 void grep_source_clear_data(struct grep_source *gs)
1405 switch (gs->type) {
1406 case GREP_SOURCE_FILE:
1407 case GREP_SOURCE_SHA1:
1408 free(gs->buf);
1409 gs->buf = NULL;
1410 gs->size = 0;
1411 break;
1412 case GREP_SOURCE_BUF:
1413 /* leave user-provided buf intact */
1414 break;
1418 static int grep_source_load_sha1(struct grep_source *gs)
1420 enum object_type type;
1422 grep_read_lock();
1423 gs->buf = read_sha1_file(gs->identifier, &type, &gs->size);
1424 grep_read_unlock();
1426 if (!gs->buf)
1427 return error(_("'%s': unable to read %s"),
1428 gs->name,
1429 sha1_to_hex(gs->identifier));
1430 return 0;
1433 static int grep_source_load_file(struct grep_source *gs)
1435 const char *filename = gs->identifier;
1436 struct stat st;
1437 char *data;
1438 size_t size;
1439 int i;
1441 if (lstat(filename, &st) < 0) {
1442 err_ret:
1443 if (errno != ENOENT)
1444 error(_("'%s': %s"), filename, strerror(errno));
1445 return -1;
1447 if (!S_ISREG(st.st_mode))
1448 return -1;
1449 size = xsize_t(st.st_size);
1450 i = open(filename, O_RDONLY);
1451 if (i < 0)
1452 goto err_ret;
1453 data = xmalloc(size + 1);
1454 if (st.st_size != read_in_full(i, data, size)) {
1455 error(_("'%s': short read %s"), filename, strerror(errno));
1456 close(i);
1457 free(data);
1458 return -1;
1460 close(i);
1461 data[size] = 0;
1463 gs->buf = data;
1464 gs->size = size;
1465 return 0;
1468 int grep_source_load(struct grep_source *gs)
1470 if (gs->buf)
1471 return 0;
1473 switch (gs->type) {
1474 case GREP_SOURCE_FILE:
1475 return grep_source_load_file(gs);
1476 case GREP_SOURCE_SHA1:
1477 return grep_source_load_sha1(gs);
1478 case GREP_SOURCE_BUF:
1479 return gs->buf ? 0 : -1;
1481 die("BUG: invalid grep_source type");
1484 void grep_source_load_driver(struct grep_source *gs)
1486 if (gs->driver)
1487 return;
1489 grep_attr_lock();
1490 gs->driver = userdiff_find_by_path(gs->name);
1491 if (!gs->driver)
1492 gs->driver = userdiff_find_by_name("default");
1493 grep_attr_unlock();
1496 int grep_source_is_binary(struct grep_source *gs)
1498 grep_source_load_driver(gs);
1499 if (gs->driver->binary != -1)
1500 return gs->driver->binary;
1502 if (!grep_source_load(gs))
1503 return buffer_is_binary(gs->buf, gs->size);
1505 return 0;