grep.con commit grep: remove grep_opt argument from match_expr_eval() (d7eb527)
   1#include "cache.h"
   2#include "grep.h"
   3#include "xdiff-interface.h"
   4
   5void append_header_grep_pattern(struct grep_opt *opt, enum grep_header_field field, const char *pat)
   6{
   7        struct grep_pat *p = xcalloc(1, sizeof(*p));
   8        p->pattern = pat;
   9        p->origin = "header";
  10        p->no = 0;
  11        p->token = GREP_PATTERN_HEAD;
  12        p->field = field;
  13        *opt->pattern_tail = p;
  14        opt->pattern_tail = &p->next;
  15        p->next = NULL;
  16}
  17
  18void append_grep_pattern(struct grep_opt *opt, const char *pat,
  19                         const char *origin, int no, enum grep_pat_token t)
  20{
  21        struct grep_pat *p = xcalloc(1, sizeof(*p));
  22        p->pattern = pat;
  23        p->origin = origin;
  24        p->no = no;
  25        p->token = t;
  26        *opt->pattern_tail = p;
  27        opt->pattern_tail = &p->next;
  28        p->next = NULL;
  29}
  30
  31static int is_fixed(const char *s)
  32{
  33        while (*s && !is_regex_special(*s))
  34                s++;
  35        return !*s;
  36}
  37
  38static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
  39{
  40        int err;
  41
  42        p->word_regexp = opt->word_regexp;
  43
  44        if (opt->fixed || is_fixed(p->pattern))
  45                p->fixed = 1;
  46        if (opt->regflags & REG_ICASE)
  47                p->fixed = 0;
  48        if (p->fixed)
  49                return;
  50
  51        err = regcomp(&p->regexp, p->pattern, opt->regflags);
  52        if (err) {
  53                char errbuf[1024];
  54                char where[1024];
  55                if (p->no)
  56                        sprintf(where, "In '%s' at %d, ",
  57                                p->origin, p->no);
  58                else if (p->origin)
  59                        sprintf(where, "%s, ", p->origin);
  60                else
  61                        where[0] = 0;
  62                regerror(err, &p->regexp, errbuf, 1024);
  63                regfree(&p->regexp);
  64                die("%s'%s': %s", where, p->pattern, errbuf);
  65        }
  66}
  67
  68static struct grep_expr *compile_pattern_or(struct grep_pat **);
  69static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
  70{
  71        struct grep_pat *p;
  72        struct grep_expr *x;
  73
  74        p = *list;
  75        switch (p->token) {
  76        case GREP_PATTERN: /* atom */
  77        case GREP_PATTERN_HEAD:
  78        case GREP_PATTERN_BODY:
  79                x = xcalloc(1, sizeof (struct grep_expr));
  80                x->node = GREP_NODE_ATOM;
  81                x->u.atom = p;
  82                *list = p->next;
  83                return x;
  84        case GREP_OPEN_PAREN:
  85                *list = p->next;
  86                x = compile_pattern_or(list);
  87                if (!x)
  88                        return NULL;
  89                if (!*list || (*list)->token != GREP_CLOSE_PAREN)
  90                        die("unmatched parenthesis");
  91                *list = (*list)->next;
  92                return x;
  93        default:
  94                return NULL;
  95        }
  96}
  97
  98static struct grep_expr *compile_pattern_not(struct grep_pat **list)
  99{
 100        struct grep_pat *p;
 101        struct grep_expr *x;
 102
 103        p = *list;
 104        switch (p->token) {
 105        case GREP_NOT:
 106                if (!p->next)
 107                        die("--not not followed by pattern expression");
 108                *list = p->next;
 109                x = xcalloc(1, sizeof (struct grep_expr));
 110                x->node = GREP_NODE_NOT;
 111                x->u.unary = compile_pattern_not(list);
 112                if (!x->u.unary)
 113                        die("--not followed by non pattern expression");
 114                return x;
 115        default:
 116                return compile_pattern_atom(list);
 117        }
 118}
 119
 120static struct grep_expr *compile_pattern_and(struct grep_pat **list)
 121{
 122        struct grep_pat *p;
 123        struct grep_expr *x, *y, *z;
 124
 125        x = compile_pattern_not(list);
 126        p = *list;
 127        if (p && p->token == GREP_AND) {
 128                if (!p->next)
 129                        die("--and not followed by pattern expression");
 130                *list = p->next;
 131                y = compile_pattern_and(list);
 132                if (!y)
 133                        die("--and not followed by pattern expression");
 134                z = xcalloc(1, sizeof (struct grep_expr));
 135                z->node = GREP_NODE_AND;
 136                z->u.binary.left = x;
 137                z->u.binary.right = y;
 138                return z;
 139        }
 140        return x;
 141}
 142
 143static struct grep_expr *compile_pattern_or(struct grep_pat **list)
 144{
 145        struct grep_pat *p;
 146        struct grep_expr *x, *y, *z;
 147
 148        x = compile_pattern_and(list);
 149        p = *list;
 150        if (x && p && p->token != GREP_CLOSE_PAREN) {
 151                y = compile_pattern_or(list);
 152                if (!y)
 153                        die("not a pattern expression %s", p->pattern);
 154                z = xcalloc(1, sizeof (struct grep_expr));
 155                z->node = GREP_NODE_OR;
 156                z->u.binary.left = x;
 157                z->u.binary.right = y;
 158                return z;
 159        }
 160        return x;
 161}
 162
 163static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
 164{
 165        return compile_pattern_or(list);
 166}
 167
 168void compile_grep_patterns(struct grep_opt *opt)
 169{
 170        struct grep_pat *p;
 171
 172        if (opt->all_match)
 173                opt->extended = 1;
 174
 175        for (p = opt->pattern_list; p; p = p->next) {
 176                switch (p->token) {
 177                case GREP_PATTERN: /* atom */
 178                case GREP_PATTERN_HEAD:
 179                case GREP_PATTERN_BODY:
 180                        compile_regexp(p, opt);
 181                        break;
 182                default:
 183                        opt->extended = 1;
 184                        break;
 185                }
 186        }
 187
 188        if (!opt->extended)
 189                return;
 190
 191        /* Then bundle them up in an expression.
 192         * A classic recursive descent parser would do.
 193         */
 194        p = opt->pattern_list;
 195        opt->pattern_expression = compile_pattern_expr(&p);
 196        if (p)
 197                die("incomplete pattern expression: %s", p->pattern);
 198}
 199
 200static void free_pattern_expr(struct grep_expr *x)
 201{
 202        switch (x->node) {
 203        case GREP_NODE_ATOM:
 204                break;
 205        case GREP_NODE_NOT:
 206                free_pattern_expr(x->u.unary);
 207                break;
 208        case GREP_NODE_AND:
 209        case GREP_NODE_OR:
 210                free_pattern_expr(x->u.binary.left);
 211                free_pattern_expr(x->u.binary.right);
 212                break;
 213        }
 214        free(x);
 215}
 216
 217void free_grep_patterns(struct grep_opt *opt)
 218{
 219        struct grep_pat *p, *n;
 220
 221        for (p = opt->pattern_list; p; p = n) {
 222                n = p->next;
 223                switch (p->token) {
 224                case GREP_PATTERN: /* atom */
 225                case GREP_PATTERN_HEAD:
 226                case GREP_PATTERN_BODY:
 227                        regfree(&p->regexp);
 228                        break;
 229                default:
 230                        break;
 231                }
 232                free(p);
 233        }
 234
 235        if (!opt->extended)
 236                return;
 237        free_pattern_expr(opt->pattern_expression);
 238}
 239
 240static char *end_of_line(char *cp, unsigned long *left)
 241{
 242        unsigned long l = *left;
 243        while (l && *cp != '\n') {
 244                l--;
 245                cp++;
 246        }
 247        *left = l;
 248        return cp;
 249}
 250
 251static int word_char(char ch)
 252{
 253        return isalnum(ch) || ch == '_';
 254}
 255
 256static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
 257                      const char *name, unsigned lno, char sign)
 258{
 259        if (opt->null_following_name)
 260                sign = '\0';
 261        if (opt->pathname)
 262                printf("%s%c", name, sign);
 263        if (opt->linenum)
 264                printf("%d%c", lno, sign);
 265        printf("%.*s\n", (int)(eol-bol), bol);
 266}
 267
 268static void show_name(struct grep_opt *opt, const char *name)
 269{
 270        printf("%s%c", name, opt->null_following_name ? '\0' : '\n');
 271}
 272
 273static int fixmatch(const char *pattern, char *line, regmatch_t *match)
 274{
 275        char *hit = strstr(line, pattern);
 276        if (!hit) {
 277                match->rm_so = match->rm_eo = -1;
 278                return REG_NOMATCH;
 279        }
 280        else {
 281                match->rm_so = hit - line;
 282                match->rm_eo = match->rm_so + strlen(pattern);
 283                return 0;
 284        }
 285}
 286
 287static int strip_timestamp(char *bol, char **eol_p)
 288{
 289        char *eol = *eol_p;
 290        int ch;
 291
 292        while (bol < --eol) {
 293                if (*eol != '>')
 294                        continue;
 295                *eol_p = ++eol;
 296                ch = *eol;
 297                *eol = '\0';
 298                return ch;
 299        }
 300        return 0;
 301}
 302
 303static struct {
 304        const char *field;
 305        size_t len;
 306} header_field[] = {
 307        { "author ", 7 },
 308        { "committer ", 10 },
 309};
 310
 311static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
 312                             enum grep_context ctx)
 313{
 314        int hit = 0;
 315        int saved_ch = 0;
 316        regmatch_t pmatch[10];
 317
 318        if ((p->token != GREP_PATTERN) &&
 319            ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
 320                return 0;
 321
 322        if (p->token == GREP_PATTERN_HEAD) {
 323                const char *field;
 324                size_t len;
 325                assert(p->field < ARRAY_SIZE(header_field));
 326                field = header_field[p->field].field;
 327                len = header_field[p->field].len;
 328                if (strncmp(bol, field, len))
 329                        return 0;
 330                bol += len;
 331                saved_ch = strip_timestamp(bol, &eol);
 332        }
 333
 334 again:
 335        if (!p->fixed) {
 336                regex_t *exp = &p->regexp;
 337                hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
 338                               pmatch, 0);
 339        }
 340        else {
 341                hit = !fixmatch(p->pattern, bol, pmatch);
 342        }
 343
 344        if (hit && p->word_regexp) {
 345                if ((pmatch[0].rm_so < 0) ||
 346                    (eol - bol) <= pmatch[0].rm_so ||
 347                    (pmatch[0].rm_eo < 0) ||
 348                    (eol - bol) < pmatch[0].rm_eo)
 349                        die("regexp returned nonsense");
 350
 351                /* Match beginning must be either beginning of the
 352                 * line, or at word boundary (i.e. the last char must
 353                 * not be a word char).  Similarly, match end must be
 354                 * either end of the line, or at word boundary
 355                 * (i.e. the next char must not be a word char).
 356                 */
 357                if ( ((pmatch[0].rm_so == 0) ||
 358                      !word_char(bol[pmatch[0].rm_so-1])) &&
 359                     ((pmatch[0].rm_eo == (eol-bol)) ||
 360                      !word_char(bol[pmatch[0].rm_eo])) )
 361                        ;
 362                else
 363                        hit = 0;
 364
 365                if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
 366                        /* There could be more than one match on the
 367                         * line, and the first match might not be
 368                         * strict word match.  But later ones could be!
 369                         * Forward to the next possible start, i.e. the
 370                         * next position following a non-word char.
 371                         */
 372                        bol = pmatch[0].rm_so + bol + 1;
 373                        while (word_char(bol[-1]) && bol < eol)
 374                                bol++;
 375                        if (bol < eol)
 376                                goto again;
 377                }
 378        }
 379        if (p->token == GREP_PATTERN_HEAD && saved_ch)
 380                *eol = saved_ch;
 381        return hit;
 382}
 383
 384static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
 385                           enum grep_context ctx, int collect_hits)
 386{
 387        int h = 0;
 388
 389        switch (x->node) {
 390        case GREP_NODE_ATOM:
 391                h = match_one_pattern(x->u.atom, bol, eol, ctx);
 392                break;
 393        case GREP_NODE_NOT:
 394                h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
 395                break;
 396        case GREP_NODE_AND:
 397                if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
 398                        return 0;
 399                h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
 400                break;
 401        case GREP_NODE_OR:
 402                if (!collect_hits)
 403                        return (match_expr_eval(x->u.binary.left,
 404                                                bol, eol, ctx, 0) ||
 405                                match_expr_eval(x->u.binary.right,
 406                                                bol, eol, ctx, 0));
 407                h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
 408                x->u.binary.left->hit |= h;
 409                h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
 410                break;
 411        default:
 412                die("Unexpected node type (internal error) %d", x->node);
 413        }
 414        if (collect_hits)
 415                x->hit |= h;
 416        return h;
 417}
 418
 419static int match_expr(struct grep_opt *opt, char *bol, char *eol,
 420                      enum grep_context ctx, int collect_hits)
 421{
 422        struct grep_expr *x = opt->pattern_expression;
 423        return match_expr_eval(x, bol, eol, ctx, collect_hits);
 424}
 425
 426static int match_line(struct grep_opt *opt, char *bol, char *eol,
 427                      enum grep_context ctx, int collect_hits)
 428{
 429        struct grep_pat *p;
 430        if (opt->extended)
 431                return match_expr(opt, bol, eol, ctx, collect_hits);
 432
 433        /* we do not call with collect_hits without being extended */
 434        for (p = opt->pattern_list; p; p = p->next) {
 435                if (match_one_pattern(p, bol, eol, ctx))
 436                        return 1;
 437        }
 438        return 0;
 439}
 440
 441static int grep_buffer_1(struct grep_opt *opt, const char *name,
 442                         char *buf, unsigned long size, int collect_hits)
 443{
 444        char *bol = buf;
 445        unsigned long left = size;
 446        unsigned lno = 1;
 447        struct pre_context_line {
 448                char *bol;
 449                char *eol;
 450        } *prev = NULL, *pcl;
 451        unsigned last_hit = 0;
 452        unsigned last_shown = 0;
 453        int binary_match_only = 0;
 454        const char *hunk_mark = "";
 455        unsigned count = 0;
 456        enum grep_context ctx = GREP_CONTEXT_HEAD;
 457
 458        if (buffer_is_binary(buf, size)) {
 459                switch (opt->binary) {
 460                case GREP_BINARY_DEFAULT:
 461                        binary_match_only = 1;
 462                        break;
 463                case GREP_BINARY_NOMATCH:
 464                        return 0; /* Assume unmatch */
 465                        break;
 466                default:
 467                        break;
 468                }
 469        }
 470
 471        if (opt->pre_context)
 472                prev = xcalloc(opt->pre_context, sizeof(*prev));
 473        if (opt->pre_context || opt->post_context)
 474                hunk_mark = "--\n";
 475
 476        while (left) {
 477                char *eol, ch;
 478                int hit;
 479
 480                eol = end_of_line(bol, &left);
 481                ch = *eol;
 482                *eol = 0;
 483
 484                if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
 485                        ctx = GREP_CONTEXT_BODY;
 486
 487                hit = match_line(opt, bol, eol, ctx, collect_hits);
 488                *eol = ch;
 489
 490                if (collect_hits)
 491                        goto next_line;
 492
 493                /* "grep -v -e foo -e bla" should list lines
 494                 * that do not have either, so inversion should
 495                 * be done outside.
 496                 */
 497                if (opt->invert)
 498                        hit = !hit;
 499                if (opt->unmatch_name_only) {
 500                        if (hit)
 501                                return 0;
 502                        goto next_line;
 503                }
 504                if (hit) {
 505                        count++;
 506                        if (opt->status_only)
 507                                return 1;
 508                        if (binary_match_only) {
 509                                printf("Binary file %s matches\n", name);
 510                                return 1;
 511                        }
 512                        if (opt->name_only) {
 513                                show_name(opt, name);
 514                                return 1;
 515                        }
 516                        /* Hit at this line.  If we haven't shown the
 517                         * pre-context lines, we would need to show them.
 518                         * When asked to do "count", this still show
 519                         * the context which is nonsense, but the user
 520                         * deserves to get that ;-).
 521                         */
 522                        if (opt->pre_context) {
 523                                unsigned from;
 524                                if (opt->pre_context < lno)
 525                                        from = lno - opt->pre_context;
 526                                else
 527                                        from = 1;
 528                                if (from <= last_shown)
 529                                        from = last_shown + 1;
 530                                if (last_shown && from != last_shown + 1)
 531                                        fputs(hunk_mark, stdout);
 532                                while (from < lno) {
 533                                        pcl = &prev[lno-from-1];
 534                                        show_line(opt, pcl->bol, pcl->eol,
 535                                                  name, from, '-');
 536                                        from++;
 537                                }
 538                                last_shown = lno-1;
 539                        }
 540                        if (last_shown && lno != last_shown + 1)
 541                                fputs(hunk_mark, stdout);
 542                        if (!opt->count)
 543                                show_line(opt, bol, eol, name, lno, ':');
 544                        last_shown = last_hit = lno;
 545                }
 546                else if (last_hit &&
 547                         lno <= last_hit + opt->post_context) {
 548                        /* If the last hit is within the post context,
 549                         * we need to show this line.
 550                         */
 551                        if (last_shown && lno != last_shown + 1)
 552                                fputs(hunk_mark, stdout);
 553                        show_line(opt, bol, eol, name, lno, '-');
 554                        last_shown = lno;
 555                }
 556                if (opt->pre_context) {
 557                        memmove(prev+1, prev,
 558                                (opt->pre_context-1) * sizeof(*prev));
 559                        prev->bol = bol;
 560                        prev->eol = eol;
 561                }
 562
 563        next_line:
 564                bol = eol + 1;
 565                if (!left)
 566                        break;
 567                left--;
 568                lno++;
 569        }
 570
 571        free(prev);
 572        if (collect_hits)
 573                return 0;
 574
 575        if (opt->status_only)
 576                return 0;
 577        if (opt->unmatch_name_only) {
 578                /* We did not see any hit, so we want to show this */
 579                show_name(opt, name);
 580                return 1;
 581        }
 582
 583        /* NEEDSWORK:
 584         * The real "grep -c foo *.c" gives many "bar.c:0" lines,
 585         * which feels mostly useless but sometimes useful.  Maybe
 586         * make it another option?  For now suppress them.
 587         */
 588        if (opt->count && count)
 589                printf("%s%c%u\n", name,
 590                       opt->null_following_name ? '\0' : ':', count);
 591        return !!last_hit;
 592}
 593
 594static void clr_hit_marker(struct grep_expr *x)
 595{
 596        /* All-hit markers are meaningful only at the very top level
 597         * OR node.
 598         */
 599        while (1) {
 600                x->hit = 0;
 601                if (x->node != GREP_NODE_OR)
 602                        return;
 603                x->u.binary.left->hit = 0;
 604                x = x->u.binary.right;
 605        }
 606}
 607
 608static int chk_hit_marker(struct grep_expr *x)
 609{
 610        /* Top level nodes have hit markers.  See if they all are hits */
 611        while (1) {
 612                if (x->node != GREP_NODE_OR)
 613                        return x->hit;
 614                if (!x->u.binary.left->hit)
 615                        return 0;
 616                x = x->u.binary.right;
 617        }
 618}
 619
 620int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size)
 621{
 622        /*
 623         * we do not have to do the two-pass grep when we do not check
 624         * buffer-wide "all-match".
 625         */
 626        if (!opt->all_match)
 627                return grep_buffer_1(opt, name, buf, size, 0);
 628
 629        /* Otherwise the toplevel "or" terms hit a bit differently.
 630         * We first clear hit markers from them.
 631         */
 632        clr_hit_marker(opt->pattern_expression);
 633        grep_buffer_1(opt, name, buf, size, 1);
 634
 635        if (!chk_hit_marker(opt->pattern_expression))
 636                return 0;
 637
 638        return grep_buffer_1(opt, name, buf, size, 0);
 639}