4 #include "xdiff-interface.h"
 
   6 void append_header_grep_pattern(struct grep_opt *opt, enum grep_header_field field, const char *pat)
 
   8         struct grep_pat *p = xcalloc(1, sizeof(*p));
 
  10         p->patternlen = strlen(pat);
 
  13         p->token = GREP_PATTERN_HEAD;
 
  15         *opt->header_tail = p;
 
  16         opt->header_tail = &p->next;
 
  20 void append_grep_pattern(struct grep_opt *opt, const char *pat,
 
  21                          const char *origin, int no, enum grep_pat_token t)
 
  23         append_grep_pat(opt, pat, strlen(pat), origin, no, t);
 
  26 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen,
 
  27                      const char *origin, int no, enum grep_pat_token t)
 
  29         struct grep_pat *p = xcalloc(1, sizeof(*p));
 
  31         p->patternlen = patlen;
 
  35         *opt->pattern_tail = p;
 
  36         opt->pattern_tail = &p->next;
 
  40 struct grep_opt *grep_opt_dup(const struct grep_opt *opt)
 
  43         struct grep_opt *ret = xmalloc(sizeof(struct grep_opt));
 
  46         ret->pattern_list = NULL;
 
  47         ret->pattern_tail = &ret->pattern_list;
 
  49         for(pat = opt->pattern_list; pat != NULL; pat = pat->next)
 
  51                 if(pat->token == GREP_PATTERN_HEAD)
 
  52                         append_header_grep_pattern(ret, pat->field,
 
  55                         append_grep_pat(ret, pat->pattern, pat->patternlen,
 
  56                                         pat->origin, pat->no, pat->token);
 
  62 static NORETURN void compile_regexp_failed(const struct grep_pat *p,
 
  68                 sprintf(where, "In '%s' at %d, ", p->origin, p->no);
 
  70                 sprintf(where, "%s, ", p->origin);
 
  74         die("%s'%s': %s", where, p->pattern, error);
 
  78 static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
 
  85                 options |= PCRE_CASELESS;
 
  87         p->pcre_regexp = pcre_compile(p->pattern, options, &error, &erroffset,
 
  90                 compile_regexp_failed(p, error);
 
  92         p->pcre_extra_info = pcre_study(p->pcre_regexp, 0, &error);
 
  93         if (!p->pcre_extra_info && error)
 
  97 static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
 
  98                 regmatch_t *match, int eflags)
 
 100         int ovector[30], ret, flags = 0;
 
 102         if (eflags & REG_NOTBOL)
 
 103                 flags |= PCRE_NOTBOL;
 
 105         ret = pcre_exec(p->pcre_regexp, p->pcre_extra_info, line, eol - line,
 
 106                         0, flags, ovector, ARRAY_SIZE(ovector));
 
 107         if (ret < 0 && ret != PCRE_ERROR_NOMATCH)
 
 108                 die("pcre_exec failed with error code %d", ret);
 
 111                 match->rm_so = ovector[0];
 
 112                 match->rm_eo = ovector[1];
 
 118 static void free_pcre_regexp(struct grep_pat *p)
 
 120         pcre_free(p->pcre_regexp);
 
 121         pcre_free(p->pcre_extra_info);
 
 123 #else /* !USE_LIBPCRE */
 
 124 static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
 
 126         die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
 
 129 static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
 
 130                 regmatch_t *match, int eflags)
 
 135 static void free_pcre_regexp(struct grep_pat *p)
 
 138 #endif /* !USE_LIBPCRE */
 
 140 static int is_fixed(const char *s, size_t len)
 
 144         /* regcomp cannot accept patterns with NULs so we
 
 145          * consider any pattern containing a NUL fixed.
 
 147         if (memchr(s, 0, len))
 
 150         for (i = 0; i < len; i++) {
 
 151                 if (is_regex_special(s[i]))
 
 158 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
 
 162         p->word_regexp = opt->word_regexp;
 
 163         p->ignore_case = opt->ignore_case;
 
 165         if (opt->fixed || is_fixed(p->pattern, p->patternlen))
 
 171                 if (opt->regflags & REG_ICASE || p->ignore_case) {
 
 172                         static char trans[256];
 
 174                         for (i = 0; i < 256; i++)
 
 175                                 trans[i] = tolower(i);
 
 176                         p->kws = kwsalloc(trans);
 
 178                         p->kws = kwsalloc(NULL);
 
 180                 kwsincr(p->kws, p->pattern, p->patternlen);
 
 186                 compile_pcre_regexp(p, opt);
 
 190         err = regcomp(&p->regexp, p->pattern, opt->regflags);
 
 193                 regerror(err, &p->regexp, errbuf, 1024);
 
 195                 compile_regexp_failed(p, errbuf);
 
 199 static struct grep_expr *compile_pattern_or(struct grep_pat **);
 
 200 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
 
 209         case GREP_PATTERN: /* atom */
 
 210         case GREP_PATTERN_HEAD:
 
 211         case GREP_PATTERN_BODY:
 
 212                 x = xcalloc(1, sizeof (struct grep_expr));
 
 213                 x->node = GREP_NODE_ATOM;
 
 217         case GREP_OPEN_PAREN:
 
 219                 x = compile_pattern_or(list);
 
 220                 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
 
 221                         die("unmatched parenthesis");
 
 222                 *list = (*list)->next;
 
 229 static struct grep_expr *compile_pattern_not(struct grep_pat **list)
 
 240                         die("--not not followed by pattern expression");
 
 242                 x = xcalloc(1, sizeof (struct grep_expr));
 
 243                 x->node = GREP_NODE_NOT;
 
 244                 x->u.unary = compile_pattern_not(list);
 
 246                         die("--not followed by non pattern expression");
 
 249                 return compile_pattern_atom(list);
 
 253 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
 
 256         struct grep_expr *x, *y, *z;
 
 258         x = compile_pattern_not(list);
 
 260         if (p && p->token == GREP_AND) {
 
 262                         die("--and not followed by pattern expression");
 
 264                 y = compile_pattern_and(list);
 
 266                         die("--and not followed by pattern expression");
 
 267                 z = xcalloc(1, sizeof (struct grep_expr));
 
 268                 z->node = GREP_NODE_AND;
 
 269                 z->u.binary.left = x;
 
 270                 z->u.binary.right = y;
 
 276 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
 
 279         struct grep_expr *x, *y, *z;
 
 281         x = compile_pattern_and(list);
 
 283         if (x && p && p->token != GREP_CLOSE_PAREN) {
 
 284                 y = compile_pattern_or(list);
 
 286                         die("not a pattern expression %s", p->pattern);
 
 287                 z = xcalloc(1, sizeof (struct grep_expr));
 
 288                 z->node = GREP_NODE_OR;
 
 289                 z->u.binary.left = x;
 
 290                 z->u.binary.right = y;
 
 296 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
 
 298         return compile_pattern_or(list);
 
 301 static struct grep_expr *grep_true_expr(void)
 
 303         struct grep_expr *z = xcalloc(1, sizeof(*z));
 
 304         z->node = GREP_NODE_TRUE;
 
 308 static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
 
 310         struct grep_expr *z = xcalloc(1, sizeof(*z));
 
 311         z->node = GREP_NODE_OR;
 
 312         z->u.binary.left = left;
 
 313         z->u.binary.right = right;
 
 317 static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
 
 320         struct grep_expr *header_expr;
 
 321         struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]);
 
 322         enum grep_header_field fld;
 
 324         if (!opt->header_list)
 
 326         p = opt->header_list;
 
 327         for (p = opt->header_list; p; p = p->next) {
 
 328                 if (p->token != GREP_PATTERN_HEAD)
 
 329                         die("bug: a non-header pattern in grep header list.");
 
 330                 if (p->field < 0 || GREP_HEADER_FIELD_MAX <= p->field)
 
 331                         die("bug: unknown header field %d", p->field);
 
 332                 compile_regexp(p, opt);
 
 335         for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++)
 
 336                 header_group[fld] = NULL;
 
 338         for (p = opt->header_list; p; p = p->next) {
 
 340                 struct grep_pat *pp = p;
 
 342                 h = compile_pattern_atom(&pp);
 
 343                 if (!h || pp != p->next)
 
 344                         die("bug: malformed header expr");
 
 345                 if (!header_group[p->field]) {
 
 346                         header_group[p->field] = h;
 
 349                 header_group[p->field] = grep_or_expr(h, header_group[p->field]);
 
 354         for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) {
 
 355                 if (!header_group[fld])
 
 358                         header_expr = grep_true_expr();
 
 359                 header_expr = grep_or_expr(header_group[fld], header_expr);
 
 364 void compile_grep_patterns(struct grep_opt *opt)
 
 367         struct grep_expr *header_expr = prep_header_patterns(opt);
 
 369         for (p = opt->pattern_list; p; p = p->next) {
 
 371                 case GREP_PATTERN: /* atom */
 
 372                 case GREP_PATTERN_HEAD:
 
 373                 case GREP_PATTERN_BODY:
 
 374                         compile_regexp(p, opt);
 
 382         if (opt->all_match || header_expr)
 
 384         else if (!opt->extended)
 
 387         p = opt->pattern_list;
 
 389                 opt->pattern_expression = compile_pattern_expr(&p);
 
 391                 die("incomplete pattern expression: %s", p->pattern);
 
 396         if (!opt->pattern_expression)
 
 397                 opt->pattern_expression = header_expr;
 
 399                 opt->pattern_expression = grep_or_expr(opt->pattern_expression,
 
 404 static void free_pattern_expr(struct grep_expr *x)
 
 411                 free_pattern_expr(x->u.unary);
 
 415                 free_pattern_expr(x->u.binary.left);
 
 416                 free_pattern_expr(x->u.binary.right);
 
 422 void free_grep_patterns(struct grep_opt *opt)
 
 424         struct grep_pat *p, *n;
 
 426         for (p = opt->pattern_list; p; p = n) {
 
 429                 case GREP_PATTERN: /* atom */
 
 430                 case GREP_PATTERN_HEAD:
 
 431                 case GREP_PATTERN_BODY:
 
 434                         else if (p->pcre_regexp)
 
 447         free_pattern_expr(opt->pattern_expression);
 
 450 static char *end_of_line(char *cp, unsigned long *left)
 
 452         unsigned long l = *left;
 
 453         while (l && *cp != '\n') {
 
 461 static int word_char(char ch)
 
 463         return isalnum(ch) || ch == '_';
 
 466 static void output_color(struct grep_opt *opt, const void *data, size_t size,
 
 469         if (want_color(opt->color) && color && color[0]) {
 
 470                 opt->output(opt, color, strlen(color));
 
 471                 opt->output(opt, data, size);
 
 472                 opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
 
 474                 opt->output(opt, data, size);
 
 477 static void output_sep(struct grep_opt *opt, char sign)
 
 479         if (opt->null_following_name)
 
 480                 opt->output(opt, "\0", 1);
 
 482                 output_color(opt, &sign, 1, opt->color_sep);
 
 485 static void show_name(struct grep_opt *opt, const char *name)
 
 487         output_color(opt, name, strlen(name), opt->color_filename);
 
 488         opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
 
 491 static int fixmatch(struct grep_pat *p, char *line, char *eol,
 
 494         struct kwsmatch kwsm;
 
 495         size_t offset = kwsexec(p->kws, line, eol - line, &kwsm);
 
 497                 match->rm_so = match->rm_eo = -1;
 
 500                 match->rm_so = offset;
 
 501                 match->rm_eo = match->rm_so + kwsm.size[0];
 
 506 static int regmatch(const regex_t *preg, char *line, char *eol,
 
 507                     regmatch_t *match, int eflags)
 
 511         match->rm_eo = eol - line;
 
 512         eflags |= REG_STARTEND;
 
 514         return regexec(preg, line, 1, match, eflags);
 
 517 static int patmatch(struct grep_pat *p, char *line, char *eol,
 
 518                     regmatch_t *match, int eflags)
 
 523                 hit = !fixmatch(p, line, eol, match);
 
 524         else if (p->pcre_regexp)
 
 525                 hit = !pcrematch(p, line, eol, match, eflags);
 
 527                 hit = !regmatch(&p->regexp, line, eol, match, eflags);
 
 532 static int strip_timestamp(char *bol, char **eol_p)
 
 537         while (bol < --eol) {
 
 553         { "committer ", 10 },
 
 556 static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
 
 557                              enum grep_context ctx,
 
 558                              regmatch_t *pmatch, int eflags)
 
 562         const char *start = bol;
 
 564         if ((p->token != GREP_PATTERN) &&
 
 565             ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
 
 568         if (p->token == GREP_PATTERN_HEAD) {
 
 571                 assert(p->field < ARRAY_SIZE(header_field));
 
 572                 field = header_field[p->field].field;
 
 573                 len = header_field[p->field].len;
 
 574                 if (strncmp(bol, field, len))
 
 577                 saved_ch = strip_timestamp(bol, &eol);
 
 581         hit = patmatch(p, bol, eol, pmatch, eflags);
 
 583         if (hit && p->word_regexp) {
 
 584                 if ((pmatch[0].rm_so < 0) ||
 
 585                     (eol - bol) < pmatch[0].rm_so ||
 
 586                     (pmatch[0].rm_eo < 0) ||
 
 587                     (eol - bol) < pmatch[0].rm_eo)
 
 588                         die("regexp returned nonsense");
 
 590                 /* Match beginning must be either beginning of the
 
 591                  * line, or at word boundary (i.e. the last char must
 
 592                  * not be a word char).  Similarly, match end must be
 
 593                  * either end of the line, or at word boundary
 
 594                  * (i.e. the next char must not be a word char).
 
 596                 if ( ((pmatch[0].rm_so == 0) ||
 
 597                       !word_char(bol[pmatch[0].rm_so-1])) &&
 
 598                      ((pmatch[0].rm_eo == (eol-bol)) ||
 
 599                       !word_char(bol[pmatch[0].rm_eo])) )
 
 604                 /* Words consist of at least one character. */
 
 605                 if (pmatch->rm_so == pmatch->rm_eo)
 
 608                 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
 
 609                         /* There could be more than one match on the
 
 610                          * line, and the first match might not be
 
 611                          * strict word match.  But later ones could be!
 
 612                          * Forward to the next possible start, i.e. the
 
 613                          * next position following a non-word char.
 
 615                         bol = pmatch[0].rm_so + bol + 1;
 
 616                         while (word_char(bol[-1]) && bol < eol)
 
 618                         eflags |= REG_NOTBOL;
 
 623         if (p->token == GREP_PATTERN_HEAD && saved_ch)
 
 626                 pmatch[0].rm_so += bol - start;
 
 627                 pmatch[0].rm_eo += bol - start;
 
 632 static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
 
 633                            enum grep_context ctx, int collect_hits)
 
 639                 die("Not a valid grep expression");
 
 645                 h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
 
 648                 h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
 
 651                 if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
 
 653                 h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
 
 657                         return (match_expr_eval(x->u.binary.left,
 
 659                                 match_expr_eval(x->u.binary.right,
 
 661                 h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
 
 662                 x->u.binary.left->hit |= h;
 
 663                 h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
 
 666                 die("Unexpected node type (internal error) %d", x->node);
 
 673 static int match_expr(struct grep_opt *opt, char *bol, char *eol,
 
 674                       enum grep_context ctx, int collect_hits)
 
 676         struct grep_expr *x = opt->pattern_expression;
 
 677         return match_expr_eval(x, bol, eol, ctx, collect_hits);
 
 680 static int match_line(struct grep_opt *opt, char *bol, char *eol,
 
 681                       enum grep_context ctx, int collect_hits)
 
 687                 return match_expr(opt, bol, eol, ctx, collect_hits);
 
 689         /* we do not call with collect_hits without being extended */
 
 690         for (p = opt->pattern_list; p; p = p->next) {
 
 691                 if (match_one_pattern(p, bol, eol, ctx, &match, 0))
 
 697 static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
 
 698                               enum grep_context ctx,
 
 699                               regmatch_t *pmatch, int eflags)
 
 703         if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
 
 705         if (match.rm_so < 0 || match.rm_eo < 0)
 
 707         if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
 
 708                 if (match.rm_so > pmatch->rm_so)
 
 710                 if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
 
 713         pmatch->rm_so = match.rm_so;
 
 714         pmatch->rm_eo = match.rm_eo;
 
 718 static int next_match(struct grep_opt *opt, char *bol, char *eol,
 
 719                       enum grep_context ctx, regmatch_t *pmatch, int eflags)
 
 724         pmatch->rm_so = pmatch->rm_eo = -1;
 
 726                 for (p = opt->pattern_list; p; p = p->next) {
 
 728                         case GREP_PATTERN: /* atom */
 
 729                         case GREP_PATTERN_HEAD:
 
 730                         case GREP_PATTERN_BODY:
 
 731                                 hit |= match_next_pattern(p, bol, eol, ctx,
 
 742 static void show_line(struct grep_opt *opt, char *bol, char *eol,
 
 743                       const char *name, unsigned lno, char sign)
 
 745         int rest = eol - bol;
 
 746         char *line_color = NULL;
 
 748         if (opt->file_break && opt->last_shown == 0) {
 
 749                 if (opt->show_hunk_mark)
 
 750                         opt->output(opt, "\n", 1);
 
 751         } else if (opt->pre_context || opt->post_context || opt->funcbody) {
 
 752                 if (opt->last_shown == 0) {
 
 753                         if (opt->show_hunk_mark) {
 
 754                                 output_color(opt, "--", 2, opt->color_sep);
 
 755                                 opt->output(opt, "\n", 1);
 
 757                 } else if (lno > opt->last_shown + 1) {
 
 758                         output_color(opt, "--", 2, opt->color_sep);
 
 759                         opt->output(opt, "\n", 1);
 
 762         if (opt->heading && opt->last_shown == 0) {
 
 763                 output_color(opt, name, strlen(name), opt->color_filename);
 
 764                 opt->output(opt, "\n", 1);
 
 766         opt->last_shown = lno;
 
 768         if (!opt->heading && opt->pathname) {
 
 769                 output_color(opt, name, strlen(name), opt->color_filename);
 
 770                 output_sep(opt, sign);
 
 774                 snprintf(buf, sizeof(buf), "%d", lno);
 
 775                 output_color(opt, buf, strlen(buf), opt->color_lineno);
 
 776                 output_sep(opt, sign);
 
 780                 enum grep_context ctx = GREP_CONTEXT_BODY;
 
 785                         line_color = opt->color_selected;
 
 786                 else if (sign == '-')
 
 787                         line_color = opt->color_context;
 
 788                 else if (sign == '=')
 
 789                         line_color = opt->color_function;
 
 791                 while (next_match(opt, bol, eol, ctx, &match, eflags)) {
 
 792                         if (match.rm_so == match.rm_eo)
 
 795                         output_color(opt, bol, match.rm_so, line_color);
 
 796                         output_color(opt, bol + match.rm_so,
 
 797                                      match.rm_eo - match.rm_so,
 
 805         output_color(opt, bol, rest, line_color);
 
 806         opt->output(opt, "\n", 1);
 
 813  * This lock protects access to the gitattributes machinery, which is
 
 816 pthread_mutex_t grep_attr_mutex;
 
 818 static inline void grep_attr_lock(void)
 
 821                 pthread_mutex_lock(&grep_attr_mutex);
 
 824 static inline void grep_attr_unlock(void)
 
 827                 pthread_mutex_unlock(&grep_attr_mutex);
 
 831  * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
 
 833 pthread_mutex_t grep_read_mutex;
 
 836 #define grep_attr_lock()
 
 837 #define grep_attr_unlock()
 
 840 static int match_funcname(struct grep_opt *opt, struct grep_source *gs, char *bol, char *eol)
 
 842         xdemitconf_t *xecfg = opt->priv;
 
 843         if (xecfg && !xecfg->find_func) {
 
 844                 grep_source_load_driver(gs);
 
 845                 if (gs->driver->funcname.pattern) {
 
 846                         const struct userdiff_funcname *pe = &gs->driver->funcname;
 
 847                         xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
 
 849                         xecfg = opt->priv = NULL;
 
 855                 return xecfg->find_func(bol, eol - bol, buf, 1,
 
 856                                         xecfg->find_func_priv) >= 0;
 
 861         if (isalpha(*bol) || *bol == '_' || *bol == '$')
 
 866 static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
 
 867                                char *bol, unsigned lno)
 
 869         while (bol > gs->buf) {
 
 872                 while (bol > gs->buf && bol[-1] != '\n')
 
 876                 if (lno <= opt->last_shown)
 
 879                 if (match_funcname(opt, gs, bol, eol)) {
 
 880                         show_line(opt, bol, eol, gs->name, lno, '=');
 
 886 static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
 
 887                              char *bol, char *end, unsigned lno)
 
 889         unsigned cur = lno, from = 1, funcname_lno = 0;
 
 890         int funcname_needed = !!opt->funcname;
 
 892         if (opt->funcbody && !match_funcname(opt, gs, bol, end))
 
 895         if (opt->pre_context < lno)
 
 896                 from = lno - opt->pre_context;
 
 897         if (from <= opt->last_shown)
 
 898                 from = opt->last_shown + 1;
 
 901         while (bol > gs->buf &&
 
 902                cur > (funcname_needed == 2 ? opt->last_shown + 1 : from)) {
 
 905                 while (bol > gs->buf && bol[-1] != '\n')
 
 908                 if (funcname_needed && match_funcname(opt, gs, bol, eol)) {
 
 914         /* We need to look even further back to find a function signature. */
 
 915         if (opt->funcname && funcname_needed)
 
 916                 show_funcname_line(opt, gs, bol, cur);
 
 920                 char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
 
 924                 show_line(opt, bol, eol, gs->name, cur, sign);
 
 930 static int should_lookahead(struct grep_opt *opt)
 
 935                 return 0; /* punt for too complex stuff */
 
 938         for (p = opt->pattern_list; p; p = p->next) {
 
 939                 if (p->token != GREP_PATTERN)
 
 940                         return 0; /* punt for "header only" and stuff */
 
 945 static int look_ahead(struct grep_opt *opt,
 
 946                       unsigned long *left_p,
 
 950         unsigned lno = *lno_p;
 
 954         regoff_t earliest = -1;
 
 956         for (p = opt->pattern_list; p; p = p->next) {
 
 960                 hit = patmatch(p, bol, bol + *left_p, &m, 0);
 
 961                 if (!hit || m.rm_so < 0 || m.rm_eo < 0)
 
 963                 if (earliest < 0 || m.rm_so < earliest)
 
 968                 *bol_p = bol + *left_p;
 
 972         for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
 
 973                 ; /* find the beginning of the line */
 
 976         for (sp = bol; sp < last_bol; sp++) {
 
 980         *left_p -= last_bol - bol;
 
 986 static void std_output(struct grep_opt *opt, const void *buf, size_t size)
 
 988         fwrite(buf, size, 1, stdout);
 
 991 static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits)
 
 996         unsigned last_hit = 0;
 
 997         int binary_match_only = 0;
 
 999         int try_lookahead = 0;
 
1000         int show_function = 0;
 
1001         enum grep_context ctx = GREP_CONTEXT_HEAD;
 
1005                 opt->output = std_output;
 
1007         if (opt->pre_context || opt->post_context || opt->file_break ||
 
1009                 /* Show hunk marks, except for the first file. */
 
1010                 if (opt->last_shown)
 
1011                         opt->show_hunk_mark = 1;
 
1013                  * If we're using threads then we can't easily identify
 
1014                  * the first file.  Always put hunk marks in that case
 
1015                  * and skip the very first one later in work_done().
 
1017                 if (opt->output != std_output)
 
1018                         opt->show_hunk_mark = 1;
 
1020         opt->last_shown = 0;
 
1022         switch (opt->binary) {
 
1023         case GREP_BINARY_DEFAULT:
 
1024                 if (grep_source_is_binary(gs))
 
1025                         binary_match_only = 1;
 
1027         case GREP_BINARY_NOMATCH:
 
1028                 if (grep_source_is_binary(gs))
 
1029                         return 0; /* Assume unmatch */
 
1031         case GREP_BINARY_TEXT:
 
1034                 die("bug: unknown binary handling mode");
 
1037         memset(&xecfg, 0, sizeof(xecfg));
 
1040         try_lookahead = should_lookahead(opt);
 
1042         if (grep_source_load(gs) < 0)
 
1052                  * look_ahead() skips quickly to the line that possibly
 
1053                  * has the next hit; don't call it if we need to do
 
1054                  * something more than just skipping the current line
 
1055                  * in response to an unmatch for the current line.  E.g.
 
1056                  * inside a post-context window, we will show the current
 
1057                  * line as a context around the previous hit when it
 
1062                          && (show_function ||
 
1063                              lno <= last_hit + opt->post_context))
 
1064                     && look_ahead(opt, &left, &lno, &bol))
 
1066                 eol = end_of_line(bol, &left);
 
1070                 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
 
1071                         ctx = GREP_CONTEXT_BODY;
 
1073                 hit = match_line(opt, bol, eol, ctx, collect_hits);
 
1079                 /* "grep -v -e foo -e bla" should list lines
 
1080                  * that do not have either, so inversion should
 
1085                 if (opt->unmatch_name_only) {
 
1092                         if (opt->status_only)
 
1094                         if (opt->name_only) {
 
1095                                 show_name(opt, gs->name);
 
1100                         if (binary_match_only) {
 
1101                                 opt->output(opt, "Binary file ", 12);
 
1102                                 output_color(opt, gs->name, strlen(gs->name),
 
1103                                              opt->color_filename);
 
1104                                 opt->output(opt, " matches\n", 9);
 
1107                         /* Hit at this line.  If we haven't shown the
 
1108                          * pre-context lines, we would need to show them.
 
1110                         if (opt->pre_context || opt->funcbody)
 
1111                                 show_pre_context(opt, gs, bol, eol, lno);
 
1112                         else if (opt->funcname)
 
1113                                 show_funcname_line(opt, gs, bol, lno);
 
1114                         show_line(opt, bol, eol, gs->name, lno, ':');
 
1120                 if (show_function && match_funcname(opt, gs, bol, eol))
 
1122                 if (show_function ||
 
1123                     (last_hit && lno <= last_hit + opt->post_context)) {
 
1124                         /* If the last hit is within the post context,
 
1125                          * we need to show this line.
 
1127                         show_line(opt, bol, eol, gs->name, lno, '-');
 
1141         if (opt->status_only)
 
1143         if (opt->unmatch_name_only) {
 
1144                 /* We did not see any hit, so we want to show this */
 
1145                 show_name(opt, gs->name);
 
1149         xdiff_clear_find_func(&xecfg);
 
1153          * The real "grep -c foo *.c" gives many "bar.c:0" lines,
 
1154          * which feels mostly useless but sometimes useful.  Maybe
 
1155          * make it another option?  For now suppress them.
 
1157         if (opt->count && count) {
 
1159                 output_color(opt, gs->name, strlen(gs->name), opt->color_filename);
 
1160                 output_sep(opt, ':');
 
1161                 snprintf(buf, sizeof(buf), "%u\n", count);
 
1162                 opt->output(opt, buf, strlen(buf));
 
1168 static void clr_hit_marker(struct grep_expr *x)
 
1170         /* All-hit markers are meaningful only at the very top level
 
1175                 if (x->node != GREP_NODE_OR)
 
1177                 x->u.binary.left->hit = 0;
 
1178                 x = x->u.binary.right;
 
1182 static int chk_hit_marker(struct grep_expr *x)
 
1184         /* Top level nodes have hit markers.  See if they all are hits */
 
1186                 if (x->node != GREP_NODE_OR)
 
1188                 if (!x->u.binary.left->hit)
 
1190                 x = x->u.binary.right;
 
1194 int grep_source(struct grep_opt *opt, struct grep_source *gs)
 
1197          * we do not have to do the two-pass grep when we do not check
 
1198          * buffer-wide "all-match".
 
1200         if (!opt->all_match)
 
1201                 return grep_source_1(opt, gs, 0);
 
1203         /* Otherwise the toplevel "or" terms hit a bit differently.
 
1204          * We first clear hit markers from them.
 
1206         clr_hit_marker(opt->pattern_expression);
 
1207         grep_source_1(opt, gs, 1);
 
1209         if (!chk_hit_marker(opt->pattern_expression))
 
1212         return grep_source_1(opt, gs, 0);
 
1215 int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size)
 
1217         struct grep_source gs;
 
1220         grep_source_init(&gs, GREP_SOURCE_BUF, NULL, NULL);
 
1224         r = grep_source(opt, &gs);
 
1226         grep_source_clear(&gs);
 
1230 void grep_source_init(struct grep_source *gs, enum grep_source_type type,
 
1231                       const char *name, const void *identifier)
 
1234         gs->name = name ? xstrdup(name) : NULL;
 
1240         case GREP_SOURCE_FILE:
 
1241                 gs->identifier = xstrdup(identifier);
 
1243         case GREP_SOURCE_SHA1:
 
1244                 gs->identifier = xmalloc(20);
 
1245                 memcpy(gs->identifier, identifier, 20);
 
1247         case GREP_SOURCE_BUF:
 
1248                 gs->identifier = NULL;
 
1252 void grep_source_clear(struct grep_source *gs)
 
1256         free(gs->identifier);
 
1257         gs->identifier = NULL;
 
1258         grep_source_clear_data(gs);
 
1261 void grep_source_clear_data(struct grep_source *gs)
 
1264         case GREP_SOURCE_FILE:
 
1265         case GREP_SOURCE_SHA1:
 
1270         case GREP_SOURCE_BUF:
 
1271                 /* leave user-provided buf intact */
 
1276 static int grep_source_load_sha1(struct grep_source *gs)
 
1278         enum object_type type;
 
1281         gs->buf = read_sha1_file(gs->identifier, &type, &gs->size);
 
1285                 return error(_("'%s': unable to read %s"),
 
1287                              sha1_to_hex(gs->identifier));
 
1291 static int grep_source_load_file(struct grep_source *gs)
 
1293         const char *filename = gs->identifier;
 
1299         if (lstat(filename, &st) < 0) {
 
1301                 if (errno != ENOENT)
 
1302                         error(_("'%s': %s"), filename, strerror(errno));
 
1305         if (!S_ISREG(st.st_mode))
 
1307         size = xsize_t(st.st_size);
 
1308         i = open(filename, O_RDONLY);
 
1311         data = xmalloc(size + 1);
 
1312         if (st.st_size != read_in_full(i, data, size)) {
 
1313                 error(_("'%s': short read %s"), filename, strerror(errno));
 
1326 int grep_source_load(struct grep_source *gs)
 
1332         case GREP_SOURCE_FILE:
 
1333                 return grep_source_load_file(gs);
 
1334         case GREP_SOURCE_SHA1:
 
1335                 return grep_source_load_sha1(gs);
 
1336         case GREP_SOURCE_BUF:
 
1337                 return gs->buf ? 0 : -1;
 
1339         die("BUG: invalid grep_source type");
 
1342 void grep_source_load_driver(struct grep_source *gs)
 
1348         gs->driver = userdiff_find_by_path(gs->name);
 
1350                 gs->driver = userdiff_find_by_name("default");
 
1354 int grep_source_is_binary(struct grep_source *gs)
 
1356         grep_source_load_driver(gs);
 
1357         if (gs->driver->binary != -1)
 
1358                 return gs->driver->binary;
 
1360         if (!grep_source_load(gs))
 
1361                 return buffer_is_binary(gs->buf, gs->size);