grep.con commit grep: move pattern-type bits support to top-level grep.[ch] (c5c31d3)
   1#include "cache.h"
   2#include "grep.h"
   3#include "userdiff.h"
   4#include "xdiff-interface.h"
   5
   6static int grep_source_load(struct grep_source *gs);
   7static int grep_source_is_binary(struct grep_source *gs);
   8
   9static struct grep_opt grep_defaults;
  10
  11/*
  12 * Initialize the grep_defaults template with hardcoded defaults.
  13 * We could let the compiler do this, but without C99 initializers
  14 * the code gets unwieldy and unreadable, so...
  15 */
  16void init_grep_defaults(void)
  17{
  18        struct grep_opt *opt = &grep_defaults;
  19
  20        memset(opt, 0, sizeof(*opt));
  21        opt->relative = 1;
  22        opt->pathname = 1;
  23        opt->regflags = REG_NEWLINE;
  24        opt->max_depth = -1;
  25        opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
  26        opt->extended_regexp_option = 0;
  27        strcpy(opt->color_context, "");
  28        strcpy(opt->color_filename, "");
  29        strcpy(opt->color_function, "");
  30        strcpy(opt->color_lineno, "");
  31        strcpy(opt->color_match, GIT_COLOR_BOLD_RED);
  32        strcpy(opt->color_selected, "");
  33        strcpy(opt->color_sep, GIT_COLOR_CYAN);
  34        opt->color = -1;
  35}
  36
  37static int parse_pattern_type_arg(const char *opt, const char *arg)
  38{
  39        if (!strcmp(arg, "default"))
  40                return GREP_PATTERN_TYPE_UNSPECIFIED;
  41        else if (!strcmp(arg, "basic"))
  42                return GREP_PATTERN_TYPE_BRE;
  43        else if (!strcmp(arg, "extended"))
  44                return GREP_PATTERN_TYPE_ERE;
  45        else if (!strcmp(arg, "fixed"))
  46                return GREP_PATTERN_TYPE_FIXED;
  47        else if (!strcmp(arg, "perl"))
  48                return GREP_PATTERN_TYPE_PCRE;
  49        die("bad %s argument: %s", opt, arg);
  50}
  51
  52/*
  53 * Read the configuration file once and store it in
  54 * the grep_defaults template.
  55 */
  56int grep_config(const char *var, const char *value, void *cb)
  57{
  58        struct grep_opt *opt = &grep_defaults;
  59        char *color = NULL;
  60
  61        if (userdiff_config(var, value) < 0)
  62                return -1;
  63
  64        if (!strcmp(var, "grep.extendedregexp")) {
  65                if (git_config_bool(var, value))
  66                        opt->extended_regexp_option = 1;
  67                else
  68                        opt->extended_regexp_option = 0;
  69                return 0;
  70        }
  71
  72        if (!strcmp(var, "grep.patterntype")) {
  73                opt->pattern_type_option = parse_pattern_type_arg(var, value);
  74                return 0;
  75        }
  76
  77        if (!strcmp(var, "grep.linenumber")) {
  78                opt->linenum = git_config_bool(var, value);
  79                return 0;
  80        }
  81
  82        if (!strcmp(var, "color.grep"))
  83                opt->color = git_config_colorbool(var, value);
  84        else if (!strcmp(var, "color.grep.context"))
  85                color = opt->color_context;
  86        else if (!strcmp(var, "color.grep.filename"))
  87                color = opt->color_filename;
  88        else if (!strcmp(var, "color.grep.function"))
  89                color = opt->color_function;
  90        else if (!strcmp(var, "color.grep.linenumber"))
  91                color = opt->color_lineno;
  92        else if (!strcmp(var, "color.grep.match"))
  93                color = opt->color_match;
  94        else if (!strcmp(var, "color.grep.selected"))
  95                color = opt->color_selected;
  96        else if (!strcmp(var, "color.grep.separator"))
  97                color = opt->color_sep;
  98
  99        if (color) {
 100                if (!value)
 101                        return config_error_nonbool(var);
 102                color_parse(value, var, color);
 103        }
 104        return 0;
 105}
 106
 107/*
 108 * Initialize one instance of grep_opt and copy the
 109 * default values from the template we read the configuration
 110 * information in an earlier call to git_config(grep_config).
 111 */
 112void grep_init(struct grep_opt *opt, const char *prefix)
 113{
 114        struct grep_opt *def = &grep_defaults;
 115
 116        memset(opt, 0, sizeof(*opt));
 117        opt->prefix = prefix;
 118        opt->prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
 119        opt->pattern_tail = &opt->pattern_list;
 120        opt->header_tail = &opt->header_list;
 121
 122        opt->color = def->color;
 123        opt->extended_regexp_option = def->extended_regexp_option;
 124        opt->pattern_type_option = def->pattern_type_option;
 125        opt->linenum = def->linenum;
 126        opt->max_depth = def->max_depth;
 127        opt->pathname = def->pathname;
 128        opt->regflags = def->regflags;
 129        opt->relative = def->relative;
 130
 131        strcpy(opt->color_context, def->color_context);
 132        strcpy(opt->color_filename, def->color_filename);
 133        strcpy(opt->color_function, def->color_function);
 134        strcpy(opt->color_lineno, def->color_lineno);
 135        strcpy(opt->color_match, def->color_match);
 136        strcpy(opt->color_selected, def->color_selected);
 137        strcpy(opt->color_sep, def->color_sep);
 138}
 139
 140void grep_commit_pattern_type(enum grep_pattern_type pattern_type, struct grep_opt *opt)
 141{
 142        if (pattern_type != GREP_PATTERN_TYPE_UNSPECIFIED)
 143                grep_set_pattern_type_option(pattern_type, opt);
 144        else if (opt->pattern_type_option != GREP_PATTERN_TYPE_UNSPECIFIED)
 145                grep_set_pattern_type_option(opt->pattern_type_option, opt);
 146        else if (opt->extended_regexp_option)
 147                grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, opt);
 148}
 149
 150void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, struct grep_opt *opt)
 151{
 152        switch (pattern_type) {
 153        case GREP_PATTERN_TYPE_UNSPECIFIED:
 154                /* fall through */
 155
 156        case GREP_PATTERN_TYPE_BRE:
 157                opt->fixed = 0;
 158                opt->pcre = 0;
 159                opt->regflags &= ~REG_EXTENDED;
 160                break;
 161
 162        case GREP_PATTERN_TYPE_ERE:
 163                opt->fixed = 0;
 164                opt->pcre = 0;
 165                opt->regflags |= REG_EXTENDED;
 166                break;
 167
 168        case GREP_PATTERN_TYPE_FIXED:
 169                opt->fixed = 1;
 170                opt->pcre = 0;
 171                opt->regflags &= ~REG_EXTENDED;
 172                break;
 173
 174        case GREP_PATTERN_TYPE_PCRE:
 175                opt->fixed = 0;
 176                opt->pcre = 1;
 177                opt->regflags &= ~REG_EXTENDED;
 178                break;
 179        }
 180}
 181
 182static struct grep_pat *create_grep_pat(const char *pat, size_t patlen,
 183                                        const char *origin, int no,
 184                                        enum grep_pat_token t,
 185                                        enum grep_header_field field)
 186{
 187        struct grep_pat *p = xcalloc(1, sizeof(*p));
 188        p->pattern = xmemdupz(pat, patlen);
 189        p->patternlen = patlen;
 190        p->origin = origin;
 191        p->no = no;
 192        p->token = t;
 193        p->field = field;
 194        return p;
 195}
 196
 197static void do_append_grep_pat(struct grep_pat ***tail, struct grep_pat *p)
 198{
 199        **tail = p;
 200        *tail = &p->next;
 201        p->next = NULL;
 202
 203        switch (p->token) {
 204        case GREP_PATTERN: /* atom */
 205        case GREP_PATTERN_HEAD:
 206        case GREP_PATTERN_BODY:
 207                for (;;) {
 208                        struct grep_pat *new_pat;
 209                        size_t len = 0;
 210                        char *cp = p->pattern + p->patternlen, *nl = NULL;
 211                        while (++len <= p->patternlen) {
 212                                if (*(--cp) == '\n') {
 213                                        nl = cp;
 214                                        break;
 215                                }
 216                        }
 217                        if (!nl)
 218                                break;
 219                        new_pat = create_grep_pat(nl + 1, len - 1, p->origin,
 220                                                  p->no, p->token, p->field);
 221                        new_pat->next = p->next;
 222                        if (!p->next)
 223                                *tail = &new_pat->next;
 224                        p->next = new_pat;
 225                        *nl = '\0';
 226                        p->patternlen -= len;
 227                }
 228                break;
 229        default:
 230                break;
 231        }
 232}
 233
 234void append_header_grep_pattern(struct grep_opt *opt,
 235                                enum grep_header_field field, const char *pat)
 236{
 237        struct grep_pat *p = create_grep_pat(pat, strlen(pat), "header", 0,
 238                                             GREP_PATTERN_HEAD, field);
 239        if (field == GREP_HEADER_REFLOG)
 240                opt->use_reflog_filter = 1;
 241        do_append_grep_pat(&opt->header_tail, p);
 242}
 243
 244void append_grep_pattern(struct grep_opt *opt, const char *pat,
 245                         const char *origin, int no, enum grep_pat_token t)
 246{
 247        append_grep_pat(opt, pat, strlen(pat), origin, no, t);
 248}
 249
 250void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen,
 251                     const char *origin, int no, enum grep_pat_token t)
 252{
 253        struct grep_pat *p = create_grep_pat(pat, patlen, origin, no, t, 0);
 254        do_append_grep_pat(&opt->pattern_tail, p);
 255}
 256
 257struct grep_opt *grep_opt_dup(const struct grep_opt *opt)
 258{
 259        struct grep_pat *pat;
 260        struct grep_opt *ret = xmalloc(sizeof(struct grep_opt));
 261        *ret = *opt;
 262
 263        ret->pattern_list = NULL;
 264        ret->pattern_tail = &ret->pattern_list;
 265
 266        for(pat = opt->pattern_list; pat != NULL; pat = pat->next)
 267        {
 268                if(pat->token == GREP_PATTERN_HEAD)
 269                        append_header_grep_pattern(ret, pat->field,
 270                                                   pat->pattern);
 271                else
 272                        append_grep_pat(ret, pat->pattern, pat->patternlen,
 273                                        pat->origin, pat->no, pat->token);
 274        }
 275
 276        return ret;
 277}
 278
 279static NORETURN void compile_regexp_failed(const struct grep_pat *p,
 280                const char *error)
 281{
 282        char where[1024];
 283
 284        if (p->no)
 285                sprintf(where, "In '%s' at %d, ", p->origin, p->no);
 286        else if (p->origin)
 287                sprintf(where, "%s, ", p->origin);
 288        else
 289                where[0] = 0;
 290
 291        die("%s'%s': %s", where, p->pattern, error);
 292}
 293
 294#ifdef USE_LIBPCRE
 295static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
 296{
 297        const char *error;
 298        int erroffset;
 299        int options = PCRE_MULTILINE;
 300
 301        if (opt->ignore_case)
 302                options |= PCRE_CASELESS;
 303
 304        p->pcre_regexp = pcre_compile(p->pattern, options, &error, &erroffset,
 305                        NULL);
 306        if (!p->pcre_regexp)
 307                compile_regexp_failed(p, error);
 308
 309        p->pcre_extra_info = pcre_study(p->pcre_regexp, 0, &error);
 310        if (!p->pcre_extra_info && error)
 311                die("%s", error);
 312}
 313
 314static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
 315                regmatch_t *match, int eflags)
 316{
 317        int ovector[30], ret, flags = 0;
 318
 319        if (eflags & REG_NOTBOL)
 320                flags |= PCRE_NOTBOL;
 321
 322        ret = pcre_exec(p->pcre_regexp, p->pcre_extra_info, line, eol - line,
 323                        0, flags, ovector, ARRAY_SIZE(ovector));
 324        if (ret < 0 && ret != PCRE_ERROR_NOMATCH)
 325                die("pcre_exec failed with error code %d", ret);
 326        if (ret > 0) {
 327                ret = 0;
 328                match->rm_so = ovector[0];
 329                match->rm_eo = ovector[1];
 330        }
 331
 332        return ret;
 333}
 334
 335static void free_pcre_regexp(struct grep_pat *p)
 336{
 337        pcre_free(p->pcre_regexp);
 338        pcre_free(p->pcre_extra_info);
 339}
 340#else /* !USE_LIBPCRE */
 341static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
 342{
 343        die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
 344}
 345
 346static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
 347                regmatch_t *match, int eflags)
 348{
 349        return 1;
 350}
 351
 352static void free_pcre_regexp(struct grep_pat *p)
 353{
 354}
 355#endif /* !USE_LIBPCRE */
 356
 357static int is_fixed(const char *s, size_t len)
 358{
 359        size_t i;
 360
 361        /* regcomp cannot accept patterns with NULs so we
 362         * consider any pattern containing a NUL fixed.
 363         */
 364        if (memchr(s, 0, len))
 365                return 1;
 366
 367        for (i = 0; i < len; i++) {
 368                if (is_regex_special(s[i]))
 369                        return 0;
 370        }
 371
 372        return 1;
 373}
 374
 375static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
 376{
 377        int err;
 378
 379        p->word_regexp = opt->word_regexp;
 380        p->ignore_case = opt->ignore_case;
 381
 382        if (opt->fixed || is_fixed(p->pattern, p->patternlen))
 383                p->fixed = 1;
 384        else
 385                p->fixed = 0;
 386
 387        if (p->fixed) {
 388                if (opt->regflags & REG_ICASE || p->ignore_case)
 389                        p->kws = kwsalloc(tolower_trans_tbl);
 390                else
 391                        p->kws = kwsalloc(NULL);
 392                kwsincr(p->kws, p->pattern, p->patternlen);
 393                kwsprep(p->kws);
 394                return;
 395        }
 396
 397        if (opt->pcre) {
 398                compile_pcre_regexp(p, opt);
 399                return;
 400        }
 401
 402        err = regcomp(&p->regexp, p->pattern, opt->regflags);
 403        if (err) {
 404                char errbuf[1024];
 405                regerror(err, &p->regexp, errbuf, 1024);
 406                regfree(&p->regexp);
 407                compile_regexp_failed(p, errbuf);
 408        }
 409}
 410
 411static struct grep_expr *compile_pattern_or(struct grep_pat **);
 412static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
 413{
 414        struct grep_pat *p;
 415        struct grep_expr *x;
 416
 417        p = *list;
 418        if (!p)
 419                return NULL;
 420        switch (p->token) {
 421        case GREP_PATTERN: /* atom */
 422        case GREP_PATTERN_HEAD:
 423        case GREP_PATTERN_BODY:
 424                x = xcalloc(1, sizeof (struct grep_expr));
 425                x->node = GREP_NODE_ATOM;
 426                x->u.atom = p;
 427                *list = p->next;
 428                return x;
 429        case GREP_OPEN_PAREN:
 430                *list = p->next;
 431                x = compile_pattern_or(list);
 432                if (!*list || (*list)->token != GREP_CLOSE_PAREN)
 433                        die("unmatched parenthesis");
 434                *list = (*list)->next;
 435                return x;
 436        default:
 437                return NULL;
 438        }
 439}
 440
 441static struct grep_expr *compile_pattern_not(struct grep_pat **list)
 442{
 443        struct grep_pat *p;
 444        struct grep_expr *x;
 445
 446        p = *list;
 447        if (!p)
 448                return NULL;
 449        switch (p->token) {
 450        case GREP_NOT:
 451                if (!p->next)
 452                        die("--not not followed by pattern expression");
 453                *list = p->next;
 454                x = xcalloc(1, sizeof (struct grep_expr));
 455                x->node = GREP_NODE_NOT;
 456                x->u.unary = compile_pattern_not(list);
 457                if (!x->u.unary)
 458                        die("--not followed by non pattern expression");
 459                return x;
 460        default:
 461                return compile_pattern_atom(list);
 462        }
 463}
 464
 465static struct grep_expr *compile_pattern_and(struct grep_pat **list)
 466{
 467        struct grep_pat *p;
 468        struct grep_expr *x, *y, *z;
 469
 470        x = compile_pattern_not(list);
 471        p = *list;
 472        if (p && p->token == GREP_AND) {
 473                if (!p->next)
 474                        die("--and not followed by pattern expression");
 475                *list = p->next;
 476                y = compile_pattern_and(list);
 477                if (!y)
 478                        die("--and not followed by pattern expression");
 479                z = xcalloc(1, sizeof (struct grep_expr));
 480                z->node = GREP_NODE_AND;
 481                z->u.binary.left = x;
 482                z->u.binary.right = y;
 483                return z;
 484        }
 485        return x;
 486}
 487
 488static struct grep_expr *compile_pattern_or(struct grep_pat **list)
 489{
 490        struct grep_pat *p;
 491        struct grep_expr *x, *y, *z;
 492
 493        x = compile_pattern_and(list);
 494        p = *list;
 495        if (x && p && p->token != GREP_CLOSE_PAREN) {
 496                y = compile_pattern_or(list);
 497                if (!y)
 498                        die("not a pattern expression %s", p->pattern);
 499                z = xcalloc(1, sizeof (struct grep_expr));
 500                z->node = GREP_NODE_OR;
 501                z->u.binary.left = x;
 502                z->u.binary.right = y;
 503                return z;
 504        }
 505        return x;
 506}
 507
 508static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
 509{
 510        return compile_pattern_or(list);
 511}
 512
 513static void indent(int in)
 514{
 515        while (in-- > 0)
 516                fputc(' ', stderr);
 517}
 518
 519static void dump_grep_pat(struct grep_pat *p)
 520{
 521        switch (p->token) {
 522        case GREP_AND: fprintf(stderr, "*and*"); break;
 523        case GREP_OPEN_PAREN: fprintf(stderr, "*(*"); break;
 524        case GREP_CLOSE_PAREN: fprintf(stderr, "*)*"); break;
 525        case GREP_NOT: fprintf(stderr, "*not*"); break;
 526        case GREP_OR: fprintf(stderr, "*or*"); break;
 527
 528        case GREP_PATTERN: fprintf(stderr, "pattern"); break;
 529        case GREP_PATTERN_HEAD: fprintf(stderr, "pattern_head"); break;
 530        case GREP_PATTERN_BODY: fprintf(stderr, "pattern_body"); break;
 531        }
 532
 533        switch (p->token) {
 534        default: break;
 535        case GREP_PATTERN_HEAD:
 536                fprintf(stderr, "<head %d>", p->field); break;
 537        case GREP_PATTERN_BODY:
 538                fprintf(stderr, "<body>"); break;
 539        }
 540        switch (p->token) {
 541        default: break;
 542        case GREP_PATTERN_HEAD:
 543        case GREP_PATTERN_BODY:
 544        case GREP_PATTERN:
 545                fprintf(stderr, "%.*s", (int)p->patternlen, p->pattern);
 546                break;
 547        }
 548        fputc('\n', stderr);
 549}
 550
 551static void dump_grep_expression_1(struct grep_expr *x, int in)
 552{
 553        indent(in);
 554        switch (x->node) {
 555        case GREP_NODE_TRUE:
 556                fprintf(stderr, "true\n");
 557                break;
 558        case GREP_NODE_ATOM:
 559                dump_grep_pat(x->u.atom);
 560                break;
 561        case GREP_NODE_NOT:
 562                fprintf(stderr, "(not\n");
 563                dump_grep_expression_1(x->u.unary, in+1);
 564                indent(in);
 565                fprintf(stderr, ")\n");
 566                break;
 567        case GREP_NODE_AND:
 568                fprintf(stderr, "(and\n");
 569                dump_grep_expression_1(x->u.binary.left, in+1);
 570                dump_grep_expression_1(x->u.binary.right, in+1);
 571                indent(in);
 572                fprintf(stderr, ")\n");
 573                break;
 574        case GREP_NODE_OR:
 575                fprintf(stderr, "(or\n");
 576                dump_grep_expression_1(x->u.binary.left, in+1);
 577                dump_grep_expression_1(x->u.binary.right, in+1);
 578                indent(in);
 579                fprintf(stderr, ")\n");
 580                break;
 581        }
 582}
 583
 584static void dump_grep_expression(struct grep_opt *opt)
 585{
 586        struct grep_expr *x = opt->pattern_expression;
 587
 588        if (opt->all_match)
 589                fprintf(stderr, "[all-match]\n");
 590        dump_grep_expression_1(x, 0);
 591        fflush(NULL);
 592}
 593
 594static struct grep_expr *grep_true_expr(void)
 595{
 596        struct grep_expr *z = xcalloc(1, sizeof(*z));
 597        z->node = GREP_NODE_TRUE;
 598        return z;
 599}
 600
 601static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
 602{
 603        struct grep_expr *z = xcalloc(1, sizeof(*z));
 604        z->node = GREP_NODE_OR;
 605        z->u.binary.left = left;
 606        z->u.binary.right = right;
 607        return z;
 608}
 609
 610static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
 611{
 612        struct grep_pat *p;
 613        struct grep_expr *header_expr;
 614        struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]);
 615        enum grep_header_field fld;
 616
 617        if (!opt->header_list)
 618                return NULL;
 619
 620        for (p = opt->header_list; p; p = p->next) {
 621                if (p->token != GREP_PATTERN_HEAD)
 622                        die("bug: a non-header pattern in grep header list.");
 623                if (p->field < 0 || GREP_HEADER_FIELD_MAX <= p->field)
 624                        die("bug: unknown header field %d", p->field);
 625                compile_regexp(p, opt);
 626        }
 627
 628        for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++)
 629                header_group[fld] = NULL;
 630
 631        for (p = opt->header_list; p; p = p->next) {
 632                struct grep_expr *h;
 633                struct grep_pat *pp = p;
 634
 635                h = compile_pattern_atom(&pp);
 636                if (!h || pp != p->next)
 637                        die("bug: malformed header expr");
 638                if (!header_group[p->field]) {
 639                        header_group[p->field] = h;
 640                        continue;
 641                }
 642                header_group[p->field] = grep_or_expr(h, header_group[p->field]);
 643        }
 644
 645        header_expr = NULL;
 646
 647        for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) {
 648                if (!header_group[fld])
 649                        continue;
 650                if (!header_expr)
 651                        header_expr = grep_true_expr();
 652                header_expr = grep_or_expr(header_group[fld], header_expr);
 653        }
 654        return header_expr;
 655}
 656
 657static struct grep_expr *grep_splice_or(struct grep_expr *x, struct grep_expr *y)
 658{
 659        struct grep_expr *z = x;
 660
 661        while (x) {
 662                assert(x->node == GREP_NODE_OR);
 663                if (x->u.binary.right &&
 664                    x->u.binary.right->node == GREP_NODE_TRUE) {
 665                        x->u.binary.right = y;
 666                        break;
 667                }
 668                x = x->u.binary.right;
 669        }
 670        return z;
 671}
 672
 673static void compile_grep_patterns_real(struct grep_opt *opt)
 674{
 675        struct grep_pat *p;
 676        struct grep_expr *header_expr = prep_header_patterns(opt);
 677
 678        for (p = opt->pattern_list; p; p = p->next) {
 679                switch (p->token) {
 680                case GREP_PATTERN: /* atom */
 681                case GREP_PATTERN_HEAD:
 682                case GREP_PATTERN_BODY:
 683                        compile_regexp(p, opt);
 684                        break;
 685                default:
 686                        opt->extended = 1;
 687                        break;
 688                }
 689        }
 690
 691        if (opt->all_match || header_expr)
 692                opt->extended = 1;
 693        else if (!opt->extended && !opt->debug)
 694                return;
 695
 696        p = opt->pattern_list;
 697        if (p)
 698                opt->pattern_expression = compile_pattern_expr(&p);
 699        if (p)
 700                die("incomplete pattern expression: %s", p->pattern);
 701
 702        if (!header_expr)
 703                return;
 704
 705        if (!opt->pattern_expression)
 706                opt->pattern_expression = header_expr;
 707        else if (opt->all_match)
 708                opt->pattern_expression = grep_splice_or(header_expr,
 709                                                         opt->pattern_expression);
 710        else
 711                opt->pattern_expression = grep_or_expr(opt->pattern_expression,
 712                                                       header_expr);
 713        opt->all_match = 1;
 714}
 715
 716void compile_grep_patterns(struct grep_opt *opt)
 717{
 718        compile_grep_patterns_real(opt);
 719        if (opt->debug)
 720                dump_grep_expression(opt);
 721}
 722
 723static void free_pattern_expr(struct grep_expr *x)
 724{
 725        switch (x->node) {
 726        case GREP_NODE_TRUE:
 727        case GREP_NODE_ATOM:
 728                break;
 729        case GREP_NODE_NOT:
 730                free_pattern_expr(x->u.unary);
 731                break;
 732        case GREP_NODE_AND:
 733        case GREP_NODE_OR:
 734                free_pattern_expr(x->u.binary.left);
 735                free_pattern_expr(x->u.binary.right);
 736                break;
 737        }
 738        free(x);
 739}
 740
 741void free_grep_patterns(struct grep_opt *opt)
 742{
 743        struct grep_pat *p, *n;
 744
 745        for (p = opt->pattern_list; p; p = n) {
 746                n = p->next;
 747                switch (p->token) {
 748                case GREP_PATTERN: /* atom */
 749                case GREP_PATTERN_HEAD:
 750                case GREP_PATTERN_BODY:
 751                        if (p->kws)
 752                                kwsfree(p->kws);
 753                        else if (p->pcre_regexp)
 754                                free_pcre_regexp(p);
 755                        else
 756                                regfree(&p->regexp);
 757                        free(p->pattern);
 758                        break;
 759                default:
 760                        break;
 761                }
 762                free(p);
 763        }
 764
 765        if (!opt->extended)
 766                return;
 767        free_pattern_expr(opt->pattern_expression);
 768}
 769
 770static char *end_of_line(char *cp, unsigned long *left)
 771{
 772        unsigned long l = *left;
 773        while (l && *cp != '\n') {
 774                l--;
 775                cp++;
 776        }
 777        *left = l;
 778        return cp;
 779}
 780
 781static int word_char(char ch)
 782{
 783        return isalnum(ch) || ch == '_';
 784}
 785
 786static void output_color(struct grep_opt *opt, const void *data, size_t size,
 787                         const char *color)
 788{
 789        if (want_color(opt->color) && color && color[0]) {
 790                opt->output(opt, color, strlen(color));
 791                opt->output(opt, data, size);
 792                opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
 793        } else
 794                opt->output(opt, data, size);
 795}
 796
 797static void output_sep(struct grep_opt *opt, char sign)
 798{
 799        if (opt->null_following_name)
 800                opt->output(opt, "\0", 1);
 801        else
 802                output_color(opt, &sign, 1, opt->color_sep);
 803}
 804
 805static void show_name(struct grep_opt *opt, const char *name)
 806{
 807        output_color(opt, name, strlen(name), opt->color_filename);
 808        opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
 809}
 810
 811static int fixmatch(struct grep_pat *p, char *line, char *eol,
 812                    regmatch_t *match)
 813{
 814        struct kwsmatch kwsm;
 815        size_t offset = kwsexec(p->kws, line, eol - line, &kwsm);
 816        if (offset == -1) {
 817                match->rm_so = match->rm_eo = -1;
 818                return REG_NOMATCH;
 819        } else {
 820                match->rm_so = offset;
 821                match->rm_eo = match->rm_so + kwsm.size[0];
 822                return 0;
 823        }
 824}
 825
 826static int regmatch(const regex_t *preg, char *line, char *eol,
 827                    regmatch_t *match, int eflags)
 828{
 829#ifdef REG_STARTEND
 830        match->rm_so = 0;
 831        match->rm_eo = eol - line;
 832        eflags |= REG_STARTEND;
 833#endif
 834        return regexec(preg, line, 1, match, eflags);
 835}
 836
 837static int patmatch(struct grep_pat *p, char *line, char *eol,
 838                    regmatch_t *match, int eflags)
 839{
 840        int hit;
 841
 842        if (p->fixed)
 843                hit = !fixmatch(p, line, eol, match);
 844        else if (p->pcre_regexp)
 845                hit = !pcrematch(p, line, eol, match, eflags);
 846        else
 847                hit = !regmatch(&p->regexp, line, eol, match, eflags);
 848
 849        return hit;
 850}
 851
 852static int strip_timestamp(char *bol, char **eol_p)
 853{
 854        char *eol = *eol_p;
 855        int ch;
 856
 857        while (bol < --eol) {
 858                if (*eol != '>')
 859                        continue;
 860                *eol_p = ++eol;
 861                ch = *eol;
 862                *eol = '\0';
 863                return ch;
 864        }
 865        return 0;
 866}
 867
 868static struct {
 869        const char *field;
 870        size_t len;
 871} header_field[] = {
 872        { "author ", 7 },
 873        { "committer ", 10 },
 874        { "reflog ", 7 },
 875};
 876
 877static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
 878                             enum grep_context ctx,
 879                             regmatch_t *pmatch, int eflags)
 880{
 881        int hit = 0;
 882        int saved_ch = 0;
 883        const char *start = bol;
 884
 885        if ((p->token != GREP_PATTERN) &&
 886            ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
 887                return 0;
 888
 889        if (p->token == GREP_PATTERN_HEAD) {
 890                const char *field;
 891                size_t len;
 892                assert(p->field < ARRAY_SIZE(header_field));
 893                field = header_field[p->field].field;
 894                len = header_field[p->field].len;
 895                if (strncmp(bol, field, len))
 896                        return 0;
 897                bol += len;
 898                switch (p->field) {
 899                case GREP_HEADER_AUTHOR:
 900                case GREP_HEADER_COMMITTER:
 901                        saved_ch = strip_timestamp(bol, &eol);
 902                        break;
 903                default:
 904                        break;
 905                }
 906        }
 907
 908 again:
 909        hit = patmatch(p, bol, eol, pmatch, eflags);
 910
 911        if (hit && p->word_regexp) {
 912                if ((pmatch[0].rm_so < 0) ||
 913                    (eol - bol) < pmatch[0].rm_so ||
 914                    (pmatch[0].rm_eo < 0) ||
 915                    (eol - bol) < pmatch[0].rm_eo)
 916                        die("regexp returned nonsense");
 917
 918                /* Match beginning must be either beginning of the
 919                 * line, or at word boundary (i.e. the last char must
 920                 * not be a word char).  Similarly, match end must be
 921                 * either end of the line, or at word boundary
 922                 * (i.e. the next char must not be a word char).
 923                 */
 924                if ( ((pmatch[0].rm_so == 0) ||
 925                      !word_char(bol[pmatch[0].rm_so-1])) &&
 926                     ((pmatch[0].rm_eo == (eol-bol)) ||
 927                      !word_char(bol[pmatch[0].rm_eo])) )
 928                        ;
 929                else
 930                        hit = 0;
 931
 932                /* Words consist of at least one character. */
 933                if (pmatch->rm_so == pmatch->rm_eo)
 934                        hit = 0;
 935
 936                if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
 937                        /* There could be more than one match on the
 938                         * line, and the first match might not be
 939                         * strict word match.  But later ones could be!
 940                         * Forward to the next possible start, i.e. the
 941                         * next position following a non-word char.
 942                         */
 943                        bol = pmatch[0].rm_so + bol + 1;
 944                        while (word_char(bol[-1]) && bol < eol)
 945                                bol++;
 946                        eflags |= REG_NOTBOL;
 947                        if (bol < eol)
 948                                goto again;
 949                }
 950        }
 951        if (p->token == GREP_PATTERN_HEAD && saved_ch)
 952                *eol = saved_ch;
 953        if (hit) {
 954                pmatch[0].rm_so += bol - start;
 955                pmatch[0].rm_eo += bol - start;
 956        }
 957        return hit;
 958}
 959
 960static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
 961                           enum grep_context ctx, int collect_hits)
 962{
 963        int h = 0;
 964        regmatch_t match;
 965
 966        if (!x)
 967                die("Not a valid grep expression");
 968        switch (x->node) {
 969        case GREP_NODE_TRUE:
 970                h = 1;
 971                break;
 972        case GREP_NODE_ATOM:
 973                h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
 974                break;
 975        case GREP_NODE_NOT:
 976                h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
 977                break;
 978        case GREP_NODE_AND:
 979                if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
 980                        return 0;
 981                h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
 982                break;
 983        case GREP_NODE_OR:
 984                if (!collect_hits)
 985                        return (match_expr_eval(x->u.binary.left,
 986                                                bol, eol, ctx, 0) ||
 987                                match_expr_eval(x->u.binary.right,
 988                                                bol, eol, ctx, 0));
 989                h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
 990                x->u.binary.left->hit |= h;
 991                h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
 992                break;
 993        default:
 994                die("Unexpected node type (internal error) %d", x->node);
 995        }
 996        if (collect_hits)
 997                x->hit |= h;
 998        return h;
 999}
1000
1001static int match_expr(struct grep_opt *opt, char *bol, char *eol,
1002                      enum grep_context ctx, int collect_hits)
1003{
1004        struct grep_expr *x = opt->pattern_expression;
1005        return match_expr_eval(x, bol, eol, ctx, collect_hits);
1006}
1007
1008static int match_line(struct grep_opt *opt, char *bol, char *eol,
1009                      enum grep_context ctx, int collect_hits)
1010{
1011        struct grep_pat *p;
1012        regmatch_t match;
1013
1014        if (opt->extended)
1015                return match_expr(opt, bol, eol, ctx, collect_hits);
1016
1017        /* we do not call with collect_hits without being extended */
1018        for (p = opt->pattern_list; p; p = p->next) {
1019                if (match_one_pattern(p, bol, eol, ctx, &match, 0))
1020                        return 1;
1021        }
1022        return 0;
1023}
1024
1025static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
1026                              enum grep_context ctx,
1027                              regmatch_t *pmatch, int eflags)
1028{
1029        regmatch_t match;
1030
1031        if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
1032                return 0;
1033        if (match.rm_so < 0 || match.rm_eo < 0)
1034                return 0;
1035        if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
1036                if (match.rm_so > pmatch->rm_so)
1037                        return 1;
1038                if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
1039                        return 1;
1040        }
1041        pmatch->rm_so = match.rm_so;
1042        pmatch->rm_eo = match.rm_eo;
1043        return 1;
1044}
1045
1046static int next_match(struct grep_opt *opt, char *bol, char *eol,
1047                      enum grep_context ctx, regmatch_t *pmatch, int eflags)
1048{
1049        struct grep_pat *p;
1050        int hit = 0;
1051
1052        pmatch->rm_so = pmatch->rm_eo = -1;
1053        if (bol < eol) {
1054                for (p = opt->pattern_list; p; p = p->next) {
1055                        switch (p->token) {
1056                        case GREP_PATTERN: /* atom */
1057                        case GREP_PATTERN_HEAD:
1058                        case GREP_PATTERN_BODY:
1059                                hit |= match_next_pattern(p, bol, eol, ctx,
1060                                                          pmatch, eflags);
1061                                break;
1062                        default:
1063                                break;
1064                        }
1065                }
1066        }
1067        return hit;
1068}
1069
1070static void show_line(struct grep_opt *opt, char *bol, char *eol,
1071                      const char *name, unsigned lno, char sign)
1072{
1073        int rest = eol - bol;
1074        char *line_color = NULL;
1075
1076        if (opt->file_break && opt->last_shown == 0) {
1077                if (opt->show_hunk_mark)
1078                        opt->output(opt, "\n", 1);
1079        } else if (opt->pre_context || opt->post_context || opt->funcbody) {
1080                if (opt->last_shown == 0) {
1081                        if (opt->show_hunk_mark) {
1082                                output_color(opt, "--", 2, opt->color_sep);
1083                                opt->output(opt, "\n", 1);
1084                        }
1085                } else if (lno > opt->last_shown + 1) {
1086                        output_color(opt, "--", 2, opt->color_sep);
1087                        opt->output(opt, "\n", 1);
1088                }
1089        }
1090        if (opt->heading && opt->last_shown == 0) {
1091                output_color(opt, name, strlen(name), opt->color_filename);
1092                opt->output(opt, "\n", 1);
1093        }
1094        opt->last_shown = lno;
1095
1096        if (!opt->heading && opt->pathname) {
1097                output_color(opt, name, strlen(name), opt->color_filename);
1098                output_sep(opt, sign);
1099        }
1100        if (opt->linenum) {
1101                char buf[32];
1102                snprintf(buf, sizeof(buf), "%d", lno);
1103                output_color(opt, buf, strlen(buf), opt->color_lineno);
1104                output_sep(opt, sign);
1105        }
1106        if (opt->color) {
1107                regmatch_t match;
1108                enum grep_context ctx = GREP_CONTEXT_BODY;
1109                int ch = *eol;
1110                int eflags = 0;
1111
1112                if (sign == ':')
1113                        line_color = opt->color_selected;
1114                else if (sign == '-')
1115                        line_color = opt->color_context;
1116                else if (sign == '=')
1117                        line_color = opt->color_function;
1118                *eol = '\0';
1119                while (next_match(opt, bol, eol, ctx, &match, eflags)) {
1120                        if (match.rm_so == match.rm_eo)
1121                                break;
1122
1123                        output_color(opt, bol, match.rm_so, line_color);
1124                        output_color(opt, bol + match.rm_so,
1125                                     match.rm_eo - match.rm_so,
1126                                     opt->color_match);
1127                        bol += match.rm_eo;
1128                        rest -= match.rm_eo;
1129                        eflags = REG_NOTBOL;
1130                }
1131                *eol = ch;
1132        }
1133        output_color(opt, bol, rest, line_color);
1134        opt->output(opt, "\n", 1);
1135}
1136
1137#ifndef NO_PTHREADS
1138int grep_use_locks;
1139
1140/*
1141 * This lock protects access to the gitattributes machinery, which is
1142 * not thread-safe.
1143 */
1144pthread_mutex_t grep_attr_mutex;
1145
1146static inline void grep_attr_lock(void)
1147{
1148        if (grep_use_locks)
1149                pthread_mutex_lock(&grep_attr_mutex);
1150}
1151
1152static inline void grep_attr_unlock(void)
1153{
1154        if (grep_use_locks)
1155                pthread_mutex_unlock(&grep_attr_mutex);
1156}
1157
1158/*
1159 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
1160 */
1161pthread_mutex_t grep_read_mutex;
1162
1163#else
1164#define grep_attr_lock()
1165#define grep_attr_unlock()
1166#endif
1167
1168static int match_funcname(struct grep_opt *opt, struct grep_source *gs, char *bol, char *eol)
1169{
1170        xdemitconf_t *xecfg = opt->priv;
1171        if (xecfg && !xecfg->find_func) {
1172                grep_source_load_driver(gs);
1173                if (gs->driver->funcname.pattern) {
1174                        const struct userdiff_funcname *pe = &gs->driver->funcname;
1175                        xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
1176                } else {
1177                        xecfg = opt->priv = NULL;
1178                }
1179        }
1180
1181        if (xecfg) {
1182                char buf[1];
1183                return xecfg->find_func(bol, eol - bol, buf, 1,
1184                                        xecfg->find_func_priv) >= 0;
1185        }
1186
1187        if (bol == eol)
1188                return 0;
1189        if (isalpha(*bol) || *bol == '_' || *bol == '$')
1190                return 1;
1191        return 0;
1192}
1193
1194static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
1195                               char *bol, unsigned lno)
1196{
1197        while (bol > gs->buf) {
1198                char *eol = --bol;
1199
1200                while (bol > gs->buf && bol[-1] != '\n')
1201                        bol--;
1202                lno--;
1203
1204                if (lno <= opt->last_shown)
1205                        break;
1206
1207                if (match_funcname(opt, gs, bol, eol)) {
1208                        show_line(opt, bol, eol, gs->name, lno, '=');
1209                        break;
1210                }
1211        }
1212}
1213
1214static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
1215                             char *bol, char *end, unsigned lno)
1216{
1217        unsigned cur = lno, from = 1, funcname_lno = 0;
1218        int funcname_needed = !!opt->funcname;
1219
1220        if (opt->funcbody && !match_funcname(opt, gs, bol, end))
1221                funcname_needed = 2;
1222
1223        if (opt->pre_context < lno)
1224                from = lno - opt->pre_context;
1225        if (from <= opt->last_shown)
1226                from = opt->last_shown + 1;
1227
1228        /* Rewind. */
1229        while (bol > gs->buf &&
1230               cur > (funcname_needed == 2 ? opt->last_shown + 1 : from)) {
1231                char *eol = --bol;
1232
1233                while (bol > gs->buf && bol[-1] != '\n')
1234                        bol--;
1235                cur--;
1236                if (funcname_needed && match_funcname(opt, gs, bol, eol)) {
1237                        funcname_lno = cur;
1238                        funcname_needed = 0;
1239                }
1240        }
1241
1242        /* We need to look even further back to find a function signature. */
1243        if (opt->funcname && funcname_needed)
1244                show_funcname_line(opt, gs, bol, cur);
1245
1246        /* Back forward. */
1247        while (cur < lno) {
1248                char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
1249
1250                while (*eol != '\n')
1251                        eol++;
1252                show_line(opt, bol, eol, gs->name, cur, sign);
1253                bol = eol + 1;
1254                cur++;
1255        }
1256}
1257
1258static int should_lookahead(struct grep_opt *opt)
1259{
1260        struct grep_pat *p;
1261
1262        if (opt->extended)
1263                return 0; /* punt for too complex stuff */
1264        if (opt->invert)
1265                return 0;
1266        for (p = opt->pattern_list; p; p = p->next) {
1267                if (p->token != GREP_PATTERN)
1268                        return 0; /* punt for "header only" and stuff */
1269        }
1270        return 1;
1271}
1272
1273static int look_ahead(struct grep_opt *opt,
1274                      unsigned long *left_p,
1275                      unsigned *lno_p,
1276                      char **bol_p)
1277{
1278        unsigned lno = *lno_p;
1279        char *bol = *bol_p;
1280        struct grep_pat *p;
1281        char *sp, *last_bol;
1282        regoff_t earliest = -1;
1283
1284        for (p = opt->pattern_list; p; p = p->next) {
1285                int hit;
1286                regmatch_t m;
1287
1288                hit = patmatch(p, bol, bol + *left_p, &m, 0);
1289                if (!hit || m.rm_so < 0 || m.rm_eo < 0)
1290                        continue;
1291                if (earliest < 0 || m.rm_so < earliest)
1292                        earliest = m.rm_so;
1293        }
1294
1295        if (earliest < 0) {
1296                *bol_p = bol + *left_p;
1297                *left_p = 0;
1298                return 1;
1299        }
1300        for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
1301                ; /* find the beginning of the line */
1302        last_bol = sp;
1303
1304        for (sp = bol; sp < last_bol; sp++) {
1305                if (*sp == '\n')
1306                        lno++;
1307        }
1308        *left_p -= last_bol - bol;
1309        *bol_p = last_bol;
1310        *lno_p = lno;
1311        return 0;
1312}
1313
1314static void std_output(struct grep_opt *opt, const void *buf, size_t size)
1315{
1316        fwrite(buf, size, 1, stdout);
1317}
1318
1319static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits)
1320{
1321        char *bol;
1322        unsigned long left;
1323        unsigned lno = 1;
1324        unsigned last_hit = 0;
1325        int binary_match_only = 0;
1326        unsigned count = 0;
1327        int try_lookahead = 0;
1328        int show_function = 0;
1329        enum grep_context ctx = GREP_CONTEXT_HEAD;
1330        xdemitconf_t xecfg;
1331
1332        if (!opt->output)
1333                opt->output = std_output;
1334
1335        if (opt->pre_context || opt->post_context || opt->file_break ||
1336            opt->funcbody) {
1337                /* Show hunk marks, except for the first file. */
1338                if (opt->last_shown)
1339                        opt->show_hunk_mark = 1;
1340                /*
1341                 * If we're using threads then we can't easily identify
1342                 * the first file.  Always put hunk marks in that case
1343                 * and skip the very first one later in work_done().
1344                 */
1345                if (opt->output != std_output)
1346                        opt->show_hunk_mark = 1;
1347        }
1348        opt->last_shown = 0;
1349
1350        switch (opt->binary) {
1351        case GREP_BINARY_DEFAULT:
1352                if (grep_source_is_binary(gs))
1353                        binary_match_only = 1;
1354                break;
1355        case GREP_BINARY_NOMATCH:
1356                if (grep_source_is_binary(gs))
1357                        return 0; /* Assume unmatch */
1358                break;
1359        case GREP_BINARY_TEXT:
1360                break;
1361        default:
1362                die("bug: unknown binary handling mode");
1363        }
1364
1365        memset(&xecfg, 0, sizeof(xecfg));
1366        opt->priv = &xecfg;
1367
1368        try_lookahead = should_lookahead(opt);
1369
1370        if (grep_source_load(gs) < 0)
1371                return 0;
1372
1373        bol = gs->buf;
1374        left = gs->size;
1375        while (left) {
1376                char *eol, ch;
1377                int hit;
1378
1379                /*
1380                 * look_ahead() skips quickly to the line that possibly
1381                 * has the next hit; don't call it if we need to do
1382                 * something more than just skipping the current line
1383                 * in response to an unmatch for the current line.  E.g.
1384                 * inside a post-context window, we will show the current
1385                 * line as a context around the previous hit when it
1386                 * doesn't hit.
1387                 */
1388                if (try_lookahead
1389                    && !(last_hit
1390                         && (show_function ||
1391                             lno <= last_hit + opt->post_context))
1392                    && look_ahead(opt, &left, &lno, &bol))
1393                        break;
1394                eol = end_of_line(bol, &left);
1395                ch = *eol;
1396                *eol = 0;
1397
1398                if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
1399                        ctx = GREP_CONTEXT_BODY;
1400
1401                hit = match_line(opt, bol, eol, ctx, collect_hits);
1402                *eol = ch;
1403
1404                if (collect_hits)
1405                        goto next_line;
1406
1407                /* "grep -v -e foo -e bla" should list lines
1408                 * that do not have either, so inversion should
1409                 * be done outside.
1410                 */
1411                if (opt->invert)
1412                        hit = !hit;
1413                if (opt->unmatch_name_only) {
1414                        if (hit)
1415                                return 0;
1416                        goto next_line;
1417                }
1418                if (hit) {
1419                        count++;
1420                        if (opt->status_only)
1421                                return 1;
1422                        if (opt->name_only) {
1423                                show_name(opt, gs->name);
1424                                return 1;
1425                        }
1426                        if (opt->count)
1427                                goto next_line;
1428                        if (binary_match_only) {
1429                                opt->output(opt, "Binary file ", 12);
1430                                output_color(opt, gs->name, strlen(gs->name),
1431                                             opt->color_filename);
1432                                opt->output(opt, " matches\n", 9);
1433                                return 1;
1434                        }
1435                        /* Hit at this line.  If we haven't shown the
1436                         * pre-context lines, we would need to show them.
1437                         */
1438                        if (opt->pre_context || opt->funcbody)
1439                                show_pre_context(opt, gs, bol, eol, lno);
1440                        else if (opt->funcname)
1441                                show_funcname_line(opt, gs, bol, lno);
1442                        show_line(opt, bol, eol, gs->name, lno, ':');
1443                        last_hit = lno;
1444                        if (opt->funcbody)
1445                                show_function = 1;
1446                        goto next_line;
1447                }
1448                if (show_function && match_funcname(opt, gs, bol, eol))
1449                        show_function = 0;
1450                if (show_function ||
1451                    (last_hit && lno <= last_hit + opt->post_context)) {
1452                        /* If the last hit is within the post context,
1453                         * we need to show this line.
1454                         */
1455                        show_line(opt, bol, eol, gs->name, lno, '-');
1456                }
1457
1458        next_line:
1459                bol = eol + 1;
1460                if (!left)
1461                        break;
1462                left--;
1463                lno++;
1464        }
1465
1466        if (collect_hits)
1467                return 0;
1468
1469        if (opt->status_only)
1470                return 0;
1471        if (opt->unmatch_name_only) {
1472                /* We did not see any hit, so we want to show this */
1473                show_name(opt, gs->name);
1474                return 1;
1475        }
1476
1477        xdiff_clear_find_func(&xecfg);
1478        opt->priv = NULL;
1479
1480        /* NEEDSWORK:
1481         * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1482         * which feels mostly useless but sometimes useful.  Maybe
1483         * make it another option?  For now suppress them.
1484         */
1485        if (opt->count && count) {
1486                char buf[32];
1487                output_color(opt, gs->name, strlen(gs->name), opt->color_filename);
1488                output_sep(opt, ':');
1489                snprintf(buf, sizeof(buf), "%u\n", count);
1490                opt->output(opt, buf, strlen(buf));
1491                return 1;
1492        }
1493        return !!last_hit;
1494}
1495
1496static void clr_hit_marker(struct grep_expr *x)
1497{
1498        /* All-hit markers are meaningful only at the very top level
1499         * OR node.
1500         */
1501        while (1) {
1502                x->hit = 0;
1503                if (x->node != GREP_NODE_OR)
1504                        return;
1505                x->u.binary.left->hit = 0;
1506                x = x->u.binary.right;
1507        }
1508}
1509
1510static int chk_hit_marker(struct grep_expr *x)
1511{
1512        /* Top level nodes have hit markers.  See if they all are hits */
1513        while (1) {
1514                if (x->node != GREP_NODE_OR)
1515                        return x->hit;
1516                if (!x->u.binary.left->hit)
1517                        return 0;
1518                x = x->u.binary.right;
1519        }
1520}
1521
1522int grep_source(struct grep_opt *opt, struct grep_source *gs)
1523{
1524        /*
1525         * we do not have to do the two-pass grep when we do not check
1526         * buffer-wide "all-match".
1527         */
1528        if (!opt->all_match)
1529                return grep_source_1(opt, gs, 0);
1530
1531        /* Otherwise the toplevel "or" terms hit a bit differently.
1532         * We first clear hit markers from them.
1533         */
1534        clr_hit_marker(opt->pattern_expression);
1535        grep_source_1(opt, gs, 1);
1536
1537        if (!chk_hit_marker(opt->pattern_expression))
1538                return 0;
1539
1540        return grep_source_1(opt, gs, 0);
1541}
1542
1543int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size)
1544{
1545        struct grep_source gs;
1546        int r;
1547
1548        grep_source_init(&gs, GREP_SOURCE_BUF, NULL, NULL);
1549        gs.buf = buf;
1550        gs.size = size;
1551
1552        r = grep_source(opt, &gs);
1553
1554        grep_source_clear(&gs);
1555        return r;
1556}
1557
1558void grep_source_init(struct grep_source *gs, enum grep_source_type type,
1559                      const char *name, const void *identifier)
1560{
1561        gs->type = type;
1562        gs->name = name ? xstrdup(name) : NULL;
1563        gs->buf = NULL;
1564        gs->size = 0;
1565        gs->driver = NULL;
1566
1567        switch (type) {
1568        case GREP_SOURCE_FILE:
1569                gs->identifier = xstrdup(identifier);
1570                break;
1571        case GREP_SOURCE_SHA1:
1572                gs->identifier = xmalloc(20);
1573                memcpy(gs->identifier, identifier, 20);
1574                break;
1575        case GREP_SOURCE_BUF:
1576                gs->identifier = NULL;
1577        }
1578}
1579
1580void grep_source_clear(struct grep_source *gs)
1581{
1582        free(gs->name);
1583        gs->name = NULL;
1584        free(gs->identifier);
1585        gs->identifier = NULL;
1586        grep_source_clear_data(gs);
1587}
1588
1589void grep_source_clear_data(struct grep_source *gs)
1590{
1591        switch (gs->type) {
1592        case GREP_SOURCE_FILE:
1593        case GREP_SOURCE_SHA1:
1594                free(gs->buf);
1595                gs->buf = NULL;
1596                gs->size = 0;
1597                break;
1598        case GREP_SOURCE_BUF:
1599                /* leave user-provided buf intact */
1600                break;
1601        }
1602}
1603
1604static int grep_source_load_sha1(struct grep_source *gs)
1605{
1606        enum object_type type;
1607
1608        grep_read_lock();
1609        gs->buf = read_sha1_file(gs->identifier, &type, &gs->size);
1610        grep_read_unlock();
1611
1612        if (!gs->buf)
1613                return error(_("'%s': unable to read %s"),
1614                             gs->name,
1615                             sha1_to_hex(gs->identifier));
1616        return 0;
1617}
1618
1619static int grep_source_load_file(struct grep_source *gs)
1620{
1621        const char *filename = gs->identifier;
1622        struct stat st;
1623        char *data;
1624        size_t size;
1625        int i;
1626
1627        if (lstat(filename, &st) < 0) {
1628        err_ret:
1629                if (errno != ENOENT)
1630                        error(_("'%s': %s"), filename, strerror(errno));
1631                return -1;
1632        }
1633        if (!S_ISREG(st.st_mode))
1634                return -1;
1635        size = xsize_t(st.st_size);
1636        i = open(filename, O_RDONLY);
1637        if (i < 0)
1638                goto err_ret;
1639        data = xmalloc(size + 1);
1640        if (st.st_size != read_in_full(i, data, size)) {
1641                error(_("'%s': short read %s"), filename, strerror(errno));
1642                close(i);
1643                free(data);
1644                return -1;
1645        }
1646        close(i);
1647        data[size] = 0;
1648
1649        gs->buf = data;
1650        gs->size = size;
1651        return 0;
1652}
1653
1654static int grep_source_load(struct grep_source *gs)
1655{
1656        if (gs->buf)
1657                return 0;
1658
1659        switch (gs->type) {
1660        case GREP_SOURCE_FILE:
1661                return grep_source_load_file(gs);
1662        case GREP_SOURCE_SHA1:
1663                return grep_source_load_sha1(gs);
1664        case GREP_SOURCE_BUF:
1665                return gs->buf ? 0 : -1;
1666        }
1667        die("BUG: invalid grep_source type");
1668}
1669
1670void grep_source_load_driver(struct grep_source *gs)
1671{
1672        if (gs->driver)
1673                return;
1674
1675        grep_attr_lock();
1676        gs->driver = userdiff_find_by_path(gs->name);
1677        if (!gs->driver)
1678                gs->driver = userdiff_find_by_name("default");
1679        grep_attr_unlock();
1680}
1681
1682static int grep_source_is_binary(struct grep_source *gs)
1683{
1684        grep_source_load_driver(gs);
1685        if (gs->driver->binary != -1)
1686                return gs->driver->binary;
1687
1688        if (!grep_source_load(gs))
1689                return buffer_is_binary(gs->buf, gs->size);
1690
1691        return 0;
1692}