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