| 490 | #endif /* !USE_LIBPCRE2 */ |
| 491 | |
| 492 | static void compile_regexp(struct grep_pat *p, struct grep_opt *opt) |
| 493 | { |
| 494 | int err; |
| 495 | int regflags = REG_NEWLINE; |
| 496 | |
| 497 | if (opt->pattern_type_option == GREP_PATTERN_TYPE_UNSPECIFIED) |
| 498 | opt->pattern_type_option = (opt->extended_regexp_option |
| 499 | ? GREP_PATTERN_TYPE_ERE |
| 500 | : GREP_PATTERN_TYPE_BRE); |
| 501 | |
| 502 | p->word_regexp = opt->word_regexp; |
| 503 | p->ignore_case = opt->ignore_case; |
| 504 | p->fixed = opt->pattern_type_option == GREP_PATTERN_TYPE_FIXED; |
| 505 | |
| 506 | if (opt->pattern_type_option != GREP_PATTERN_TYPE_PCRE && |
| 507 | memchr(p->pattern, 0, p->patternlen)) |
| 508 | die(_("given pattern contains NULL byte (via -f <file>). This is only supported with -P under PCRE v2")); |
| 509 | |
| 510 | p->is_fixed = is_fixed(p->pattern, p->patternlen); |
| 511 | #ifdef USE_LIBPCRE2 |
| 512 | if (!p->fixed && !p->is_fixed) { |
| 513 | const char *no_jit = "(*NO_JIT)"; |
| 514 | const int no_jit_len = strlen(no_jit); |
| 515 | if (starts_with(p->pattern, no_jit) && |
| 516 | is_fixed(p->pattern + no_jit_len, |
| 517 | p->patternlen - no_jit_len)) |
| 518 | p->is_fixed = 1; |
| 519 | } |
| 520 | #endif |
| 521 | if (p->fixed || p->is_fixed) { |
| 522 | #ifdef USE_LIBPCRE2 |
| 523 | if (p->is_fixed) { |
| 524 | compile_pcre2_pattern(p, opt); |
| 525 | } else { |
| 526 | /* |
| 527 | * E.g. t7811-grep-open.sh relies on the |
| 528 | * pattern being restored. |
| 529 | */ |
| 530 | char *old_pattern = p->pattern; |
| 531 | size_t old_patternlen = p->patternlen; |
| 532 | struct strbuf sb = STRBUF_INIT; |
| 533 | |
| 534 | /* |
| 535 | * There is the PCRE2_LITERAL flag, but it's |
| 536 | * only in PCRE v2 10.30 and later. Needing to |
| 537 | * ifdef our way around that and dealing with |
| 538 | * it + PCRE2_MULTILINE being an error is more |
| 539 | * complex than just quoting this ourselves. |
| 540 | */ |
| 541 | strbuf_add(&sb, "\\Q", 2); |
| 542 | strbuf_add(&sb, p->pattern, p->patternlen); |
| 543 | strbuf_add(&sb, "\\E", 2); |
| 544 | |
| 545 | p->pattern = sb.buf; |
| 546 | p->patternlen = sb.len; |
| 547 | compile_pcre2_pattern(p, opt); |
| 548 | p->pattern = old_pattern; |
| 549 | p->patternlen = old_patternlen; |
no test coverage detected