Elijah Newren | 4f6728d | 2023-03-21 06:25:56 +0000 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 2 | #include "config.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 3 | #include "gettext.h" |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 4 | #include "grep.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 5 | #include "hex.h" |
Elijah Newren | a034e91 | 2023-05-16 06:34:06 +0000 | [diff] [blame] | 6 | #include "object-store-ll.h" |
Elijah Newren | d4a4f92 | 2023-04-22 20:17:26 +0000 | [diff] [blame] | 7 | #include "pretty.h" |
René Scharfe | 60ecac9 | 2009-07-02 00:07:24 +0200 | [diff] [blame] | 8 | #include "userdiff.h" |
Johannes Schindelin | 6bfce93 | 2007-06-05 03:36:11 +0100 | [diff] [blame] | 9 | #include "xdiff-interface.h" |
Jeff King | 335ec3b | 2013-05-10 17:10:15 +0200 | [diff] [blame] | 10 | #include "diff.h" |
| 11 | #include "diffcore.h" |
Nguyễn Thái Ngọc Duy | 793dc67 | 2016-06-25 07:22:31 +0200 | [diff] [blame] | 12 | #include "quote.h" |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 13 | #include "help.h" |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 14 | |
Junio C Hamano | 07a7d65 | 2012-09-15 14:04:36 -0700 | [diff] [blame] | 15 | static int grep_source_load(struct grep_source *gs); |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 16 | static int grep_source_is_binary(struct grep_source *gs, |
| 17 | struct index_state *istate); |
Junio C Hamano | 07a7d65 | 2012-09-15 14:04:36 -0700 | [diff] [blame] | 18 | |
Jeff King | bcba446 | 2023-08-29 19:45:27 -0400 | [diff] [blame] | 19 | static void std_output(struct grep_opt *opt UNUSED, const void *buf, size_t size) |
Martin Ågren | 9631342 | 2020-11-21 19:31:08 +0100 | [diff] [blame] | 20 | { |
| 21 | fwrite(buf, size, 1, stdout); |
| 22 | } |
| 23 | |
Nguyễn Thái Ngọc Duy | fa151dc | 2018-05-26 15:55:22 +0200 | [diff] [blame] | 24 | static const char *color_grep_slots[] = { |
| 25 | [GREP_COLOR_CONTEXT] = "context", |
| 26 | [GREP_COLOR_FILENAME] = "filename", |
| 27 | [GREP_COLOR_FUNCTION] = "function", |
| 28 | [GREP_COLOR_LINENO] = "lineNumber", |
Junio C Hamano | d036d66 | 2018-07-18 12:20:31 -0700 | [diff] [blame] | 29 | [GREP_COLOR_COLUMNNO] = "column", |
Nguyễn Thái Ngọc Duy | fa151dc | 2018-05-26 15:55:22 +0200 | [diff] [blame] | 30 | [GREP_COLOR_MATCH_CONTEXT] = "matchContext", |
| 31 | [GREP_COLOR_MATCH_SELECTED] = "matchSelected", |
| 32 | [GREP_COLOR_SELECTED] = "selected", |
| 33 | [GREP_COLOR_SEP] = "separator", |
| 34 | }; |
| 35 | |
Junio C Hamano | 7687a05 | 2012-10-09 16:17:50 -0700 | [diff] [blame] | 36 | static int parse_pattern_type_arg(const char *opt, const char *arg) |
| 37 | { |
| 38 | if (!strcmp(arg, "default")) |
| 39 | return GREP_PATTERN_TYPE_UNSPECIFIED; |
| 40 | else if (!strcmp(arg, "basic")) |
| 41 | return GREP_PATTERN_TYPE_BRE; |
| 42 | else if (!strcmp(arg, "extended")) |
| 43 | return GREP_PATTERN_TYPE_ERE; |
| 44 | else if (!strcmp(arg, "fixed")) |
| 45 | return GREP_PATTERN_TYPE_FIXED; |
| 46 | else if (!strcmp(arg, "perl")) |
| 47 | return GREP_PATTERN_TYPE_PCRE; |
| 48 | die("bad %s argument: %s", opt, arg); |
| 49 | } |
| 50 | |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 51 | define_list_config_array_extra(color_grep_slots, {"match"}); |
| 52 | |
Junio C Hamano | 7687a05 | 2012-10-09 16:17:50 -0700 | [diff] [blame] | 53 | /* |
| 54 | * Read the configuration file once and store it in |
| 55 | * the grep_defaults template. |
| 56 | */ |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 57 | int grep_config(const char *var, const char *value, |
| 58 | const struct config_context *ctx, void *cb) |
Junio C Hamano | 7687a05 | 2012-10-09 16:17:50 -0700 | [diff] [blame] | 59 | { |
Ævar Arnfjörð Bjarmason | 72365bb | 2022-02-16 01:00:36 +0100 | [diff] [blame] | 60 | struct grep_opt *opt = cb; |
Nguyễn Thái Ngọc Duy | fa151dc | 2018-05-26 15:55:22 +0200 | [diff] [blame] | 61 | const char *slot; |
Junio C Hamano | 7687a05 | 2012-10-09 16:17:50 -0700 | [diff] [blame] | 62 | |
| 63 | if (userdiff_config(var, value) < 0) |
| 64 | return -1; |
| 65 | |
| 66 | if (!strcmp(var, "grep.extendedregexp")) { |
Ævar Arnfjörð Bjarmason | c7e3855 | 2017-06-29 22:22:18 +0000 | [diff] [blame] | 67 | opt->extended_regexp_option = git_config_bool(var, value); |
Junio C Hamano | 7687a05 | 2012-10-09 16:17:50 -0700 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | if (!strcmp(var, "grep.patterntype")) { |
| 72 | opt->pattern_type_option = parse_pattern_type_arg(var, value); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | if (!strcmp(var, "grep.linenumber")) { |
| 77 | opt->linenum = git_config_bool(var, value); |
| 78 | return 0; |
| 79 | } |
Taylor Blau | 6653fec | 2018-06-22 10:49:49 -0500 | [diff] [blame] | 80 | if (!strcmp(var, "grep.column")) { |
| 81 | opt->columnnum = git_config_bool(var, value); |
| 82 | return 0; |
| 83 | } |
Junio C Hamano | 7687a05 | 2012-10-09 16:17:50 -0700 | [diff] [blame] | 84 | |
Andreas Schwab | 6453f7b | 2014-03-17 20:16:05 +0100 | [diff] [blame] | 85 | if (!strcmp(var, "grep.fullname")) { |
| 86 | opt->relative = !git_config_bool(var, value); |
| 87 | return 0; |
| 88 | } |
| 89 | |
Junio C Hamano | 7687a05 | 2012-10-09 16:17:50 -0700 | [diff] [blame] | 90 | if (!strcmp(var, "color.grep")) |
| 91 | opt->color = git_config_colorbool(var, value); |
Nguyễn Thái Ngọc Duy | fa151dc | 2018-05-26 15:55:22 +0200 | [diff] [blame] | 92 | if (!strcmp(var, "color.grep.match")) { |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 93 | if (grep_config("color.grep.matchcontext", value, ctx, cb) < 0) |
Nguyễn Thái Ngọc Duy | fa151dc | 2018-05-26 15:55:22 +0200 | [diff] [blame] | 94 | return -1; |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 95 | if (grep_config("color.grep.matchselected", value, ctx, cb) < 0) |
Nguyễn Thái Ngọc Duy | fa151dc | 2018-05-26 15:55:22 +0200 | [diff] [blame] | 96 | return -1; |
| 97 | } else if (skip_prefix(var, "color.grep.", &slot)) { |
| 98 | int i = LOOKUP_CONFIG(color_grep_slots, slot); |
| 99 | char *color; |
Junio C Hamano | 7687a05 | 2012-10-09 16:17:50 -0700 | [diff] [blame] | 100 | |
Nguyễn Thái Ngọc Duy | fa151dc | 2018-05-26 15:55:22 +0200 | [diff] [blame] | 101 | if (i < 0) |
| 102 | return -1; |
| 103 | color = opt->colors[i]; |
Junio C Hamano | 7687a05 | 2012-10-09 16:17:50 -0700 | [diff] [blame] | 104 | if (!value) |
| 105 | return config_error_nonbool(var); |
Jeff King | f6c5a29 | 2014-10-07 15:33:09 -0400 | [diff] [blame] | 106 | return color_parse(value, color); |
Junio C Hamano | 7687a05 | 2012-10-09 16:17:50 -0700 | [diff] [blame] | 107 | } |
| 108 | return 0; |
| 109 | } |
| 110 | |
Ævar Arnfjörð Bjarmason | 9725c8d | 2022-02-16 01:00:34 +0100 | [diff] [blame] | 111 | void grep_init(struct grep_opt *opt, struct repository *repo) |
Junio C Hamano | 7687a05 | 2012-10-09 16:17:50 -0700 | [diff] [blame] | 112 | { |
Ævar Arnfjörð Bjarmason | 72365bb | 2022-02-16 01:00:36 +0100 | [diff] [blame] | 113 | struct grep_opt blank = GREP_OPT_INIT; |
| 114 | memcpy(opt, &blank, sizeof(*opt)); |
Martin Ågren | 6ba9bb7 | 2020-11-29 20:52:21 +0100 | [diff] [blame] | 115 | |
Nguyễn Thái Ngọc Duy | 38bbc2e | 2018-09-21 17:57:23 +0200 | [diff] [blame] | 116 | opt->repo = repo; |
Junio C Hamano | 7687a05 | 2012-10-09 16:17:50 -0700 | [diff] [blame] | 117 | opt->pattern_tail = &opt->pattern_list; |
| 118 | opt->header_tail = &opt->header_list; |
Junio C Hamano | 7687a05 | 2012-10-09 16:17:50 -0700 | [diff] [blame] | 119 | } |
Junio C Hamano | 07a7d65 | 2012-09-15 14:04:36 -0700 | [diff] [blame] | 120 | |
René Scharfe | fc45675 | 2012-05-20 16:32:39 +0200 | [diff] [blame] | 121 | static struct grep_pat *create_grep_pat(const char *pat, size_t patlen, |
| 122 | const char *origin, int no, |
| 123 | enum grep_pat_token t, |
| 124 | enum grep_header_field field) |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 125 | { |
| 126 | struct grep_pat *p = xcalloc(1, sizeof(*p)); |
René Scharfe | 526a858 | 2012-05-20 16:33:07 +0200 | [diff] [blame] | 127 | p->pattern = xmemdupz(pat, patlen); |
René Scharfe | fc45675 | 2012-05-20 16:32:39 +0200 | [diff] [blame] | 128 | p->patternlen = patlen; |
| 129 | p->origin = origin; |
| 130 | p->no = no; |
| 131 | p->token = t; |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 132 | p->field = field; |
René Scharfe | fc45675 | 2012-05-20 16:32:39 +0200 | [diff] [blame] | 133 | return p; |
| 134 | } |
| 135 | |
René Scharfe | 2b3873f | 2012-05-20 16:32:54 +0200 | [diff] [blame] | 136 | static void do_append_grep_pat(struct grep_pat ***tail, struct grep_pat *p) |
| 137 | { |
| 138 | **tail = p; |
| 139 | *tail = &p->next; |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 140 | p->next = NULL; |
René Scharfe | 526a858 | 2012-05-20 16:33:07 +0200 | [diff] [blame] | 141 | |
| 142 | switch (p->token) { |
| 143 | case GREP_PATTERN: /* atom */ |
| 144 | case GREP_PATTERN_HEAD: |
| 145 | case GREP_PATTERN_BODY: |
| 146 | for (;;) { |
| 147 | struct grep_pat *new_pat; |
| 148 | size_t len = 0; |
| 149 | char *cp = p->pattern + p->patternlen, *nl = NULL; |
| 150 | while (++len <= p->patternlen) { |
| 151 | if (*(--cp) == '\n') { |
| 152 | nl = cp; |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | if (!nl) |
| 157 | break; |
| 158 | new_pat = create_grep_pat(nl + 1, len - 1, p->origin, |
| 159 | p->no, p->token, p->field); |
| 160 | new_pat->next = p->next; |
| 161 | if (!p->next) |
| 162 | *tail = &new_pat->next; |
| 163 | p->next = new_pat; |
| 164 | *nl = '\0'; |
| 165 | p->patternlen -= len; |
| 166 | } |
| 167 | break; |
| 168 | default: |
| 169 | break; |
| 170 | } |
René Scharfe | 2b3873f | 2012-05-20 16:32:54 +0200 | [diff] [blame] | 171 | } |
| 172 | |
René Scharfe | fc45675 | 2012-05-20 16:32:39 +0200 | [diff] [blame] | 173 | void append_header_grep_pattern(struct grep_opt *opt, |
| 174 | enum grep_header_field field, const char *pat) |
| 175 | { |
| 176 | struct grep_pat *p = create_grep_pat(pat, strlen(pat), "header", 0, |
| 177 | GREP_PATTERN_HEAD, field); |
Junio C Hamano | baa6378 | 2012-09-29 11:59:52 -0700 | [diff] [blame] | 178 | if (field == GREP_HEADER_REFLOG) |
| 179 | opt->use_reflog_filter = 1; |
René Scharfe | 2b3873f | 2012-05-20 16:32:54 +0200 | [diff] [blame] | 180 | do_append_grep_pat(&opt->header_tail, p); |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 183 | void append_grep_pattern(struct grep_opt *opt, const char *pat, |
| 184 | const char *origin, int no, enum grep_pat_token t) |
| 185 | { |
René Scharfe | ed40a09 | 2010-05-22 23:43:43 +0200 | [diff] [blame] | 186 | append_grep_pat(opt, pat, strlen(pat), origin, no, t); |
| 187 | } |
| 188 | |
| 189 | void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, |
| 190 | const char *origin, int no, enum grep_pat_token t) |
| 191 | { |
René Scharfe | fc45675 | 2012-05-20 16:32:39 +0200 | [diff] [blame] | 192 | struct grep_pat *p = create_grep_pat(pat, patlen, origin, no, t, 0); |
René Scharfe | 2b3873f | 2012-05-20 16:32:54 +0200 | [diff] [blame] | 193 | do_append_grep_pat(&opt->pattern_tail, p); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 196 | struct grep_opt *grep_opt_dup(const struct grep_opt *opt) |
| 197 | { |
| 198 | struct grep_pat *pat; |
| 199 | struct grep_opt *ret = xmalloc(sizeof(struct grep_opt)); |
| 200 | *ret = *opt; |
| 201 | |
| 202 | ret->pattern_list = NULL; |
| 203 | ret->pattern_tail = &ret->pattern_list; |
| 204 | |
| 205 | for(pat = opt->pattern_list; pat != NULL; pat = pat->next) |
| 206 | { |
| 207 | if(pat->token == GREP_PATTERN_HEAD) |
| 208 | append_header_grep_pattern(ret, pat->field, |
| 209 | pat->pattern); |
| 210 | else |
René Scharfe | ed40a09 | 2010-05-22 23:43:43 +0200 | [diff] [blame] | 211 | append_grep_pat(ret, pat->pattern, pat->patternlen, |
| 212 | pat->origin, pat->no, pat->token); |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | return ret; |
| 216 | } |
| 217 | |
Michał Kiedrowicz | a30c148 | 2011-05-09 23:52:04 +0200 | [diff] [blame] | 218 | static NORETURN void compile_regexp_failed(const struct grep_pat *p, |
| 219 | const char *error) |
| 220 | { |
| 221 | char where[1024]; |
| 222 | |
| 223 | if (p->no) |
Jeff King | 19bdd3e | 2015-09-24 17:06:51 -0400 | [diff] [blame] | 224 | xsnprintf(where, sizeof(where), "In '%s' at %d, ", p->origin, p->no); |
Michał Kiedrowicz | a30c148 | 2011-05-09 23:52:04 +0200 | [diff] [blame] | 225 | else if (p->origin) |
Jeff King | 19bdd3e | 2015-09-24 17:06:51 -0400 | [diff] [blame] | 226 | xsnprintf(where, sizeof(where), "%s, ", p->origin); |
Michał Kiedrowicz | a30c148 | 2011-05-09 23:52:04 +0200 | [diff] [blame] | 227 | else |
| 228 | where[0] = 0; |
| 229 | |
| 230 | die("%s'%s': %s", where, p->pattern, error); |
| 231 | } |
| 232 | |
Ævar Arnfjörð Bjarmason | 543f1c0 | 2017-05-25 19:45:30 +0000 | [diff] [blame] | 233 | static int is_fixed(const char *s, size_t len) |
| 234 | { |
| 235 | size_t i; |
| 236 | |
| 237 | for (i = 0; i < len; i++) { |
| 238 | if (is_regex_special(s[i])) |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | return 1; |
| 243 | } |
| 244 | |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 245 | #ifdef USE_LIBPCRE2 |
Ævar Arnfjörð Bjarmason | c176035 | 2021-02-18 01:07:28 +0100 | [diff] [blame] | 246 | #define GREP_PCRE2_DEBUG_MALLOC 0 |
| 247 | |
| 248 | static void *pcre2_malloc(PCRE2_SIZE size, MAYBE_UNUSED void *memory_data) |
| 249 | { |
| 250 | void *pointer = malloc(size); |
| 251 | #if GREP_PCRE2_DEBUG_MALLOC |
| 252 | static int count = 1; |
| 253 | fprintf(stderr, "PCRE2:%p -> #%02d: alloc(%lu)\n", pointer, count++, size); |
| 254 | #endif |
| 255 | return pointer; |
| 256 | } |
| 257 | |
| 258 | static void pcre2_free(void *pointer, MAYBE_UNUSED void *memory_data) |
| 259 | { |
| 260 | #if GREP_PCRE2_DEBUG_MALLOC |
| 261 | static int count = 1; |
| 262 | if (pointer) |
| 263 | fprintf(stderr, "PCRE2:%p -> #%02d: free()\n", pointer, count++); |
| 264 | #endif |
| 265 | free(pointer); |
| 266 | } |
| 267 | |
Mathias Krause | 50b6ad5 | 2023-01-31 19:56:11 +0100 | [diff] [blame] | 268 | static int pcre2_jit_functional(void) |
| 269 | { |
| 270 | static int jit_working = -1; |
| 271 | pcre2_code *code; |
| 272 | size_t off; |
| 273 | int err; |
| 274 | |
| 275 | if (jit_working != -1) |
| 276 | return jit_working; |
| 277 | |
| 278 | /* |
| 279 | * Try to JIT compile a simple pattern to probe if the JIT is |
| 280 | * working in general. It might fail for systems where creating |
| 281 | * memory mappings for runtime code generation is restricted. |
| 282 | */ |
| 283 | code = pcre2_compile((PCRE2_SPTR)".", 1, 0, &err, &off, NULL); |
| 284 | if (!code) |
| 285 | return 0; |
| 286 | |
| 287 | jit_working = pcre2_jit_compile(code, PCRE2_JIT_COMPLETE) == 0; |
| 288 | pcre2_code_free(code); |
| 289 | |
| 290 | return jit_working; |
| 291 | } |
| 292 | |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 293 | static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt) |
| 294 | { |
| 295 | int error; |
| 296 | PCRE2_UCHAR errbuf[256]; |
| 297 | PCRE2_SIZE erroffset; |
| 298 | int options = PCRE2_MULTILINE; |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 299 | int jitret; |
Ævar Arnfjörð Bjarmason | a25b908 | 2017-11-23 14:16:58 +0000 | [diff] [blame] | 300 | int patinforet; |
| 301 | size_t jitsizearg; |
René Scharfe | 32e3e8b | 2021-12-18 20:53:15 +0100 | [diff] [blame] | 302 | int literal = !opt->ignore_case && (p->fixed || p->is_fixed); |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 303 | |
Ævar Arnfjörð Bjarmason | cbe81e6 | 2021-02-18 01:07:27 +0100 | [diff] [blame] | 304 | /* |
| 305 | * Call pcre2_general_context_create() before calling any |
| 306 | * other pcre2_*(). It sets up our malloc()/free() functions |
| 307 | * with which everything else is allocated. |
| 308 | */ |
| 309 | p->pcre2_general_context = pcre2_general_context_create( |
| 310 | pcre2_malloc, pcre2_free, NULL); |
| 311 | if (!p->pcre2_general_context) |
| 312 | die("Couldn't allocate PCRE2 general context"); |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 313 | |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 314 | if (opt->ignore_case) { |
Ævar Arnfjörð Bjarmason | 4457018 | 2019-06-28 01:39:05 +0200 | [diff] [blame] | 315 | if (!opt->ignore_locale && has_non_ascii(p->pattern)) { |
Ævar Arnfjörð Bjarmason | cbe81e6 | 2021-02-18 01:07:27 +0100 | [diff] [blame] | 316 | p->pcre2_tables = pcre2_maketables(p->pcre2_general_context); |
| 317 | p->pcre2_compile_context = pcre2_compile_context_create(p->pcre2_general_context); |
Carlo Marcelo Arenas Belón | 10da030 | 2019-10-16 12:10:24 +0000 | [diff] [blame] | 318 | pcre2_set_character_tables(p->pcre2_compile_context, |
| 319 | p->pcre2_tables); |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 320 | } |
| 321 | options |= PCRE2_CASELESS; |
| 322 | } |
René Scharfe | 32e3e8b | 2021-12-18 20:53:15 +0100 | [diff] [blame] | 323 | if (!opt->ignore_locale && is_utf8_locale() && !literal) |
Carlo Marcelo Arenas Belón | acabd20 | 2023-01-08 07:52:17 -0800 | [diff] [blame] | 324 | options |= (PCRE2_UTF | PCRE2_UCP | PCRE2_MATCH_INVALID_UTF); |
Ævar Arnfjörð Bjarmason | 95ca1f9 | 2021-01-24 18:28:13 +0100 | [diff] [blame] | 325 | |
Mathias Krause | 14b9a04 | 2023-03-23 18:25:39 +0100 | [diff] [blame] | 326 | #ifndef GIT_PCRE2_VERSION_10_35_OR_HIGHER |
| 327 | /* |
| 328 | * Work around a JIT bug related to invalid Unicode character handling |
| 329 | * fixed in 10.35: |
| 330 | * https://github.com/PCRE2Project/pcre2/commit/c21bd977547d |
| 331 | */ |
| 332 | options &= ~PCRE2_UCP; |
| 333 | #endif |
| 334 | |
René Scharfe | 97169fc | 2022-02-17 22:14:29 +0100 | [diff] [blame] | 335 | #ifndef GIT_PCRE2_VERSION_10_36_OR_HIGHER |
Ævar Arnfjörð Bjarmason | 95ca1f9 | 2021-01-24 18:28:13 +0100 | [diff] [blame] | 336 | /* Work around https://bugs.exim.org/show_bug.cgi?id=2642 fixed in 10.36 */ |
Ævar Arnfjörð Bjarmason | 797c359 | 2021-02-18 01:07:24 +0100 | [diff] [blame] | 337 | if (PCRE2_MATCH_INVALID_UTF && options & (PCRE2_UTF | PCRE2_CASELESS)) |
| 338 | options |= PCRE2_NO_START_OPTIMIZE; |
| 339 | #endif |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 340 | |
| 341 | p->pcre2_pattern = pcre2_compile((PCRE2_SPTR)p->pattern, |
| 342 | p->patternlen, options, &error, &erroffset, |
| 343 | p->pcre2_compile_context); |
| 344 | |
| 345 | if (p->pcre2_pattern) { |
Ævar Arnfjörð Bjarmason | cbe81e6 | 2021-02-18 01:07:27 +0100 | [diff] [blame] | 346 | p->pcre2_match_data = pcre2_match_data_create_from_pattern(p->pcre2_pattern, p->pcre2_general_context); |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 347 | if (!p->pcre2_match_data) |
| 348 | die("Couldn't allocate PCRE2 match data"); |
| 349 | } else { |
| 350 | pcre2_get_error_message(error, errbuf, sizeof(errbuf)); |
| 351 | compile_regexp_failed(p, (const char *)&errbuf); |
| 352 | } |
| 353 | |
| 354 | pcre2_config(PCRE2_CONFIG_JIT, &p->pcre2_jit_on); |
Ævar Arnfjörð Bjarmason | 04bef50 | 2019-07-26 17:08:11 +0200 | [diff] [blame] | 355 | if (p->pcre2_jit_on) { |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 356 | jitret = pcre2_jit_compile(p->pcre2_pattern, PCRE2_JIT_COMPLETE); |
Mathias Krause | 50b6ad5 | 2023-01-31 19:56:11 +0100 | [diff] [blame] | 357 | if (jitret == PCRE2_ERROR_NOMEMORY && !pcre2_jit_functional()) { |
| 358 | /* |
| 359 | * Even though pcre2_config(PCRE2_CONFIG_JIT, ...) |
| 360 | * indicated JIT support, the library might still |
| 361 | * fail to generate JIT code for various reasons, |
| 362 | * e.g. when SELinux's 'deny_execmem' or PaX's |
| 363 | * MPROTECT prevent creating W|X memory mappings. |
| 364 | * |
| 365 | * Instead of faling hard, fall back to interpreter |
| 366 | * mode, just as if the pattern was prefixed with |
| 367 | * '(*NO_JIT)'. |
| 368 | */ |
| 369 | p->pcre2_jit_on = 0; |
| 370 | return; |
| 371 | } else if (jitret) { |
| 372 | int need_clip = p->patternlen > 64; |
| 373 | int clip_len = need_clip ? 64 : p->patternlen; |
| 374 | die("Couldn't JIT the PCRE2 pattern '%.*s'%s, got '%d'%s", |
| 375 | clip_len, p->pattern, need_clip ? "..." : "", jitret, |
| 376 | pcre2_jit_functional() |
| 377 | ? "\nPerhaps prefix (*NO_JIT) to your pattern?" |
| 378 | : ""); |
| 379 | } |
Ævar Arnfjörð Bjarmason | a25b908 | 2017-11-23 14:16:58 +0000 | [diff] [blame] | 380 | |
| 381 | /* |
| 382 | * The pcre2_config(PCRE2_CONFIG_JIT, ...) call just |
| 383 | * tells us whether the library itself supports JIT, |
| 384 | * but to see whether we're going to be actually using |
| 385 | * JIT we need to extract PCRE2_INFO_JITSIZE from the |
| 386 | * pattern *after* we do pcre2_jit_compile() above. |
| 387 | * |
| 388 | * This is because if the pattern contains the |
| 389 | * (*NO_JIT) verb (see pcre2syntax(3)) |
| 390 | * pcre2_jit_compile() will exit early with 0. If we |
| 391 | * then proceed to call pcre2_jit_match() further down |
| 392 | * the line instead of pcre2_match() we'll either |
| 393 | * segfault (pre PCRE 10.31) or run into a fatal error |
| 394 | * (post PCRE2 10.31) |
| 395 | */ |
| 396 | patinforet = pcre2_pattern_info(p->pcre2_pattern, PCRE2_INFO_JITSIZE, &jitsizearg); |
| 397 | if (patinforet) |
| 398 | BUG("pcre2_pattern_info() failed: %d", patinforet); |
| 399 | if (jitsizearg == 0) { |
| 400 | p->pcre2_jit_on = 0; |
| 401 | return; |
| 402 | } |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
| 406 | static int pcre2match(struct grep_pat *p, const char *line, const char *eol, |
| 407 | regmatch_t *match, int eflags) |
| 408 | { |
| 409 | int ret, flags = 0; |
| 410 | PCRE2_SIZE *ovector; |
| 411 | PCRE2_UCHAR errbuf[256]; |
| 412 | |
| 413 | if (eflags & REG_NOTBOL) |
| 414 | flags |= PCRE2_NOTBOL; |
| 415 | |
| 416 | if (p->pcre2_jit_on) |
| 417 | ret = pcre2_jit_match(p->pcre2_pattern, (unsigned char *)line, |
| 418 | eol - line, 0, flags, p->pcre2_match_data, |
| 419 | NULL); |
| 420 | else |
| 421 | ret = pcre2_match(p->pcre2_pattern, (unsigned char *)line, |
| 422 | eol - line, 0, flags, p->pcre2_match_data, |
| 423 | NULL); |
| 424 | |
| 425 | if (ret < 0 && ret != PCRE2_ERROR_NOMATCH) { |
| 426 | pcre2_get_error_message(ret, errbuf, sizeof(errbuf)); |
| 427 | die("%s failed with error code %d: %s", |
| 428 | (p->pcre2_jit_on ? "pcre2_jit_match" : "pcre2_match"), ret, |
| 429 | errbuf); |
| 430 | } |
| 431 | if (ret > 0) { |
| 432 | ovector = pcre2_get_ovector_pointer(p->pcre2_match_data); |
| 433 | ret = 0; |
| 434 | match->rm_so = (int)ovector[0]; |
| 435 | match->rm_eo = (int)ovector[1]; |
| 436 | } |
| 437 | |
| 438 | return ret; |
| 439 | } |
| 440 | |
| 441 | static void free_pcre2_pattern(struct grep_pat *p) |
| 442 | { |
| 443 | pcre2_compile_context_free(p->pcre2_compile_context); |
| 444 | pcre2_code_free(p->pcre2_pattern); |
| 445 | pcre2_match_data_free(p->pcre2_match_data); |
Ævar Arnfjörð Bjarmason | b76bf27 | 2021-02-18 01:07:25 +0100 | [diff] [blame] | 446 | #ifdef GIT_PCRE2_VERSION_10_34_OR_HIGHER |
Ævar Arnfjörð Bjarmason | cbe81e6 | 2021-02-18 01:07:27 +0100 | [diff] [blame] | 447 | pcre2_maketables_free(p->pcre2_general_context, p->pcre2_tables); |
Ævar Arnfjörð Bjarmason | b76bf27 | 2021-02-18 01:07:25 +0100 | [diff] [blame] | 448 | #else |
Carlo Marcelo Arenas Belón | 10da030 | 2019-10-16 12:10:24 +0000 | [diff] [blame] | 449 | free((void *)p->pcre2_tables); |
Ævar Arnfjörð Bjarmason | b76bf27 | 2021-02-18 01:07:25 +0100 | [diff] [blame] | 450 | #endif |
Ævar Arnfjörð Bjarmason | cbe81e6 | 2021-02-18 01:07:27 +0100 | [diff] [blame] | 451 | pcre2_general_context_free(p->pcre2_general_context); |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 452 | } |
| 453 | #else /* !USE_LIBPCRE2 */ |
Jeff King | 4548b01 | 2023-08-29 19:45:34 -0400 | [diff] [blame] | 454 | static void compile_pcre2_pattern(struct grep_pat *p UNUSED, |
| 455 | const struct grep_opt *opt UNUSED) |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 456 | { |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 457 | die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE"); |
| 458 | } |
| 459 | |
Jeff King | 4548b01 | 2023-08-29 19:45:34 -0400 | [diff] [blame] | 460 | static int pcre2match(struct grep_pat *p UNUSED, const char *line UNUSED, |
| 461 | const char *eol UNUSED, regmatch_t *match UNUSED, |
| 462 | int eflags UNUSED) |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 463 | { |
| 464 | return 1; |
| 465 | } |
| 466 | |
Jeff King | 4548b01 | 2023-08-29 19:45:34 -0400 | [diff] [blame] | 467 | static void free_pcre2_pattern(struct grep_pat *p UNUSED) |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 468 | { |
| 469 | } |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 470 | |
Nguyễn Thái Ngọc Duy | 793dc67 | 2016-06-25 07:22:31 +0200 | [diff] [blame] | 471 | static void compile_fixed_regexp(struct grep_pat *p, struct grep_opt *opt) |
| 472 | { |
| 473 | struct strbuf sb = STRBUF_INIT; |
| 474 | int err; |
Ævar Arnfjörð Bjarmason | 1ceabab | 2017-06-29 22:22:22 +0000 | [diff] [blame] | 475 | int regflags = 0; |
Nguyễn Thái Ngọc Duy | 793dc67 | 2016-06-25 07:22:31 +0200 | [diff] [blame] | 476 | |
| 477 | basic_regex_quote_buf(&sb, p->pattern); |
Nguyễn Thái Ngọc Duy | 793dc67 | 2016-06-25 07:22:31 +0200 | [diff] [blame] | 478 | if (opt->ignore_case) |
| 479 | regflags |= REG_ICASE; |
| 480 | err = regcomp(&p->regexp, sb.buf, regflags); |
Nguyễn Thái Ngọc Duy | 793dc67 | 2016-06-25 07:22:31 +0200 | [diff] [blame] | 481 | strbuf_release(&sb); |
| 482 | if (err) { |
| 483 | char errbuf[1024]; |
| 484 | regerror(err, &p->regexp, errbuf, sizeof(errbuf)); |
Nguyễn Thái Ngọc Duy | 793dc67 | 2016-06-25 07:22:31 +0200 | [diff] [blame] | 485 | compile_regexp_failed(p, errbuf); |
| 486 | } |
| 487 | } |
Ævar Arnfjörð Bjarmason | b65abca | 2019-07-01 23:21:00 +0200 | [diff] [blame] | 488 | #endif /* !USE_LIBPCRE2 */ |
Nguyễn Thái Ngọc Duy | 793dc67 | 2016-06-25 07:22:31 +0200 | [diff] [blame] | 489 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 490 | static void compile_regexp(struct grep_pat *p, struct grep_opt *opt) |
| 491 | { |
René Scharfe | c822255 | 2009-01-10 00:18:34 +0100 | [diff] [blame] | 492 | int err; |
Ævar Arnfjörð Bjarmason | 07a3d41 | 2017-06-29 22:22:21 +0000 | [diff] [blame] | 493 | int regflags = REG_NEWLINE; |
René Scharfe | c822255 | 2009-01-10 00:18:34 +0100 | [diff] [blame] | 494 | |
Ævar Arnfjörð Bjarmason | 04bf052 | 2022-02-16 01:00:39 +0100 | [diff] [blame] | 495 | if (opt->pattern_type_option == GREP_PATTERN_TYPE_UNSPECIFIED) |
| 496 | opt->pattern_type_option = (opt->extended_regexp_option |
| 497 | ? GREP_PATTERN_TYPE_ERE |
| 498 | : GREP_PATTERN_TYPE_BRE); |
| 499 | |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 500 | p->word_regexp = opt->word_regexp; |
Brian Collins | 5183bf6 | 2009-11-06 01:22:35 -0800 | [diff] [blame] | 501 | p->ignore_case = opt->ignore_case; |
Ævar Arnfjörð Bjarmason | 04bf052 | 2022-02-16 01:00:39 +0100 | [diff] [blame] | 502 | p->fixed = opt->pattern_type_option == GREP_PATTERN_TYPE_FIXED; |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 503 | |
Ævar Arnfjörð Bjarmason | 04bf052 | 2022-02-16 01:00:39 +0100 | [diff] [blame] | 504 | if (opt->pattern_type_option != GREP_PATTERN_TYPE_PCRE && |
Ævar Arnfjörð Bjarmason | ae807d7 | 2022-02-16 01:00:38 +0100 | [diff] [blame] | 505 | memchr(p->pattern, 0, p->patternlen)) |
Ævar Arnfjörð Bjarmason | 45d1f37 | 2019-07-01 23:20:58 +0200 | [diff] [blame] | 506 | die(_("given pattern contains NULL byte (via -f <file>). This is only supported with -P under PCRE v2")); |
Fredrik Kuivinen | 9ecedde | 2011-08-21 00:42:18 +0200 | [diff] [blame] | 507 | |
Ævar Arnfjörð Bjarmason | 09872f6 | 2019-07-26 17:08:15 +0200 | [diff] [blame] | 508 | p->is_fixed = is_fixed(p->pattern, p->patternlen); |
Ævar Arnfjörð Bjarmason | 8a59998 | 2019-07-26 17:08:16 +0200 | [diff] [blame] | 509 | #ifdef USE_LIBPCRE2 |
| 510 | if (!p->fixed && !p->is_fixed) { |
| 511 | const char *no_jit = "(*NO_JIT)"; |
| 512 | const int no_jit_len = strlen(no_jit); |
| 513 | if (starts_with(p->pattern, no_jit) && |
| 514 | is_fixed(p->pattern + no_jit_len, |
| 515 | p->patternlen - no_jit_len)) |
| 516 | p->is_fixed = 1; |
| 517 | } |
| 518 | #endif |
Ævar Arnfjörð Bjarmason | 09872f6 | 2019-07-26 17:08:15 +0200 | [diff] [blame] | 519 | if (p->fixed || p->is_fixed) { |
Ævar Arnfjörð Bjarmason | b65abca | 2019-07-01 23:21:00 +0200 | [diff] [blame] | 520 | #ifdef USE_LIBPCRE2 |
Ævar Arnfjörð Bjarmason | 09872f6 | 2019-07-26 17:08:15 +0200 | [diff] [blame] | 521 | if (p->is_fixed) { |
Ævar Arnfjörð Bjarmason | b65abca | 2019-07-01 23:21:00 +0200 | [diff] [blame] | 522 | compile_pcre2_pattern(p, opt); |
| 523 | } else { |
| 524 | /* |
| 525 | * E.g. t7811-grep-open.sh relies on the |
| 526 | * pattern being restored. |
| 527 | */ |
| 528 | char *old_pattern = p->pattern; |
| 529 | size_t old_patternlen = p->patternlen; |
| 530 | struct strbuf sb = STRBUF_INIT; |
| 531 | |
| 532 | /* |
| 533 | * There is the PCRE2_LITERAL flag, but it's |
| 534 | * only in PCRE v2 10.30 and later. Needing to |
| 535 | * ifdef our way around that and dealing with |
| 536 | * it + PCRE2_MULTILINE being an error is more |
| 537 | * complex than just quoting this ourselves. |
| 538 | */ |
| 539 | strbuf_add(&sb, "\\Q", 2); |
| 540 | strbuf_add(&sb, p->pattern, p->patternlen); |
| 541 | strbuf_add(&sb, "\\E", 2); |
| 542 | |
| 543 | p->pattern = sb.buf; |
| 544 | p->patternlen = sb.len; |
| 545 | compile_pcre2_pattern(p, opt); |
| 546 | p->pattern = old_pattern; |
| 547 | p->patternlen = old_patternlen; |
| 548 | strbuf_release(&sb); |
| 549 | } |
| 550 | #else /* !USE_LIBPCRE2 */ |
Nguyễn Thái Ngọc Duy | 793dc67 | 2016-06-25 07:22:31 +0200 | [diff] [blame] | 551 | compile_fixed_regexp(p, opt); |
Ævar Arnfjörð Bjarmason | b65abca | 2019-07-01 23:21:00 +0200 | [diff] [blame] | 552 | #endif /* !USE_LIBPCRE2 */ |
Nguyễn Thái Ngọc Duy | 793dc67 | 2016-06-25 07:22:31 +0200 | [diff] [blame] | 553 | return; |
Fredrik Kuivinen | 9ecedde | 2011-08-21 00:42:18 +0200 | [diff] [blame] | 554 | } |
René Scharfe | c822255 | 2009-01-10 00:18:34 +0100 | [diff] [blame] | 555 | |
Ævar Arnfjörð Bjarmason | 04bf052 | 2022-02-16 01:00:39 +0100 | [diff] [blame] | 556 | if (opt->pattern_type_option == GREP_PATTERN_TYPE_PCRE) { |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 557 | compile_pcre2_pattern(p, opt); |
| 558 | return; |
| 559 | } |
| 560 | |
Ævar Arnfjörð Bjarmason | 07a3d41 | 2017-06-29 22:22:21 +0000 | [diff] [blame] | 561 | if (p->ignore_case) |
| 562 | regflags |= REG_ICASE; |
Ævar Arnfjörð Bjarmason | 04bf052 | 2022-02-16 01:00:39 +0100 | [diff] [blame] | 563 | if (opt->pattern_type_option == GREP_PATTERN_TYPE_ERE) |
Ævar Arnfjörð Bjarmason | 07a3d41 | 2017-06-29 22:22:21 +0000 | [diff] [blame] | 564 | regflags |= REG_EXTENDED; |
| 565 | err = regcomp(&p->regexp, p->pattern, regflags); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 566 | if (err) { |
| 567 | char errbuf[1024]; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 568 | regerror(err, &p->regexp, errbuf, 1024); |
Michał Kiedrowicz | a30c148 | 2011-05-09 23:52:04 +0200 | [diff] [blame] | 569 | compile_regexp_failed(p, errbuf); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
René Scharfe | e2b1542 | 2022-01-06 10:54:19 +0100 | [diff] [blame] | 573 | static struct grep_expr *grep_not_expr(struct grep_expr *expr) |
| 574 | { |
| 575 | struct grep_expr *z = xcalloc(1, sizeof(*z)); |
| 576 | z->node = GREP_NODE_NOT; |
| 577 | z->u.unary = expr; |
| 578 | return z; |
| 579 | } |
| 580 | |
Taylor Blau | f2d2759 | 2022-01-06 14:50:12 -0500 | [diff] [blame] | 581 | static struct grep_expr *grep_binexp(enum grep_expr_node kind, |
| 582 | struct grep_expr *left, |
| 583 | struct grep_expr *right) |
René Scharfe | 9dbf00b | 2022-01-06 10:51:00 +0100 | [diff] [blame] | 584 | { |
| 585 | struct grep_expr *z = xcalloc(1, sizeof(*z)); |
Taylor Blau | f2d2759 | 2022-01-06 14:50:12 -0500 | [diff] [blame] | 586 | z->node = kind; |
René Scharfe | 9dbf00b | 2022-01-06 10:51:00 +0100 | [diff] [blame] | 587 | z->u.binary.left = left; |
| 588 | z->u.binary.right = right; |
| 589 | return z; |
| 590 | } |
| 591 | |
Taylor Blau | f2d2759 | 2022-01-06 14:50:12 -0500 | [diff] [blame] | 592 | static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right) |
| 593 | { |
| 594 | return grep_binexp(GREP_NODE_OR, left, right); |
| 595 | } |
| 596 | |
Taylor Blau | 0a6adc2 | 2022-01-06 14:50:15 -0500 | [diff] [blame] | 597 | static struct grep_expr *grep_and_expr(struct grep_expr *left, struct grep_expr *right) |
| 598 | { |
| 599 | return grep_binexp(GREP_NODE_AND, left, right); |
| 600 | } |
| 601 | |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 602 | static struct grep_expr *compile_pattern_or(struct grep_pat **); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 603 | static struct grep_expr *compile_pattern_atom(struct grep_pat **list) |
| 604 | { |
| 605 | struct grep_pat *p; |
| 606 | struct grep_expr *x; |
| 607 | |
| 608 | p = *list; |
Linus Torvalds | c922b01 | 2009-04-27 11:10:24 -0700 | [diff] [blame] | 609 | if (!p) |
| 610 | return NULL; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 611 | switch (p->token) { |
| 612 | case GREP_PATTERN: /* atom */ |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 613 | case GREP_PATTERN_HEAD: |
| 614 | case GREP_PATTERN_BODY: |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 615 | CALLOC_ARRAY(x, 1); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 616 | x->node = GREP_NODE_ATOM; |
| 617 | x->u.atom = p; |
| 618 | *list = p->next; |
| 619 | return x; |
| 620 | case GREP_OPEN_PAREN: |
| 621 | *list = p->next; |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 622 | x = compile_pattern_or(list); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 623 | if (!*list || (*list)->token != GREP_CLOSE_PAREN) |
Ahelenia Ziemiańska | 0d52784 | 2024-03-23 14:18:08 +0100 | [diff] [blame] | 624 | die("unmatched ( for expression group"); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 625 | *list = (*list)->next; |
| 626 | return x; |
| 627 | default: |
| 628 | return NULL; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | static struct grep_expr *compile_pattern_not(struct grep_pat **list) |
| 633 | { |
| 634 | struct grep_pat *p; |
| 635 | struct grep_expr *x; |
| 636 | |
| 637 | p = *list; |
Linus Torvalds | c922b01 | 2009-04-27 11:10:24 -0700 | [diff] [blame] | 638 | if (!p) |
| 639 | return NULL; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 640 | switch (p->token) { |
| 641 | case GREP_NOT: |
| 642 | if (!p->next) |
| 643 | die("--not not followed by pattern expression"); |
| 644 | *list = p->next; |
René Scharfe | e2b1542 | 2022-01-06 10:54:19 +0100 | [diff] [blame] | 645 | x = compile_pattern_not(list); |
| 646 | if (!x) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 647 | die("--not followed by non pattern expression"); |
René Scharfe | e2b1542 | 2022-01-06 10:54:19 +0100 | [diff] [blame] | 648 | return grep_not_expr(x); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 649 | default: |
| 650 | return compile_pattern_atom(list); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | static struct grep_expr *compile_pattern_and(struct grep_pat **list) |
| 655 | { |
| 656 | struct grep_pat *p; |
Taylor Blau | 0a6adc2 | 2022-01-06 14:50:15 -0500 | [diff] [blame] | 657 | struct grep_expr *x, *y; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 658 | |
| 659 | x = compile_pattern_not(list); |
| 660 | p = *list; |
| 661 | if (p && p->token == GREP_AND) { |
René Scharfe | fe7fe62 | 2021-06-30 18:12:43 +0200 | [diff] [blame] | 662 | if (!x) |
| 663 | die("--and not preceded by pattern expression"); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 664 | if (!p->next) |
| 665 | die("--and not followed by pattern expression"); |
| 666 | *list = p->next; |
| 667 | y = compile_pattern_and(list); |
| 668 | if (!y) |
| 669 | die("--and not followed by pattern expression"); |
Taylor Blau | 0a6adc2 | 2022-01-06 14:50:15 -0500 | [diff] [blame] | 670 | return grep_and_expr(x, y); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 671 | } |
| 672 | return x; |
| 673 | } |
| 674 | |
| 675 | static struct grep_expr *compile_pattern_or(struct grep_pat **list) |
| 676 | { |
| 677 | struct grep_pat *p; |
René Scharfe | 9dbf00b | 2022-01-06 10:51:00 +0100 | [diff] [blame] | 678 | struct grep_expr *x, *y; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 679 | |
| 680 | x = compile_pattern_and(list); |
| 681 | p = *list; |
| 682 | if (x && p && p->token != GREP_CLOSE_PAREN) { |
| 683 | y = compile_pattern_or(list); |
| 684 | if (!y) |
| 685 | die("not a pattern expression %s", p->pattern); |
René Scharfe | 9dbf00b | 2022-01-06 10:51:00 +0100 | [diff] [blame] | 686 | return grep_or_expr(x, y); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 687 | } |
| 688 | return x; |
| 689 | } |
| 690 | |
| 691 | static struct grep_expr *compile_pattern_expr(struct grep_pat **list) |
| 692 | { |
| 693 | return compile_pattern_or(list); |
| 694 | } |
| 695 | |
Junio C Hamano | 5aaeb73 | 2010-09-12 22:15:35 -0700 | [diff] [blame] | 696 | static struct grep_expr *grep_true_expr(void) |
| 697 | { |
| 698 | struct grep_expr *z = xcalloc(1, sizeof(*z)); |
| 699 | z->node = GREP_NODE_TRUE; |
| 700 | return z; |
| 701 | } |
| 702 | |
Junio C Hamano | 95ce9ce | 2010-09-12 19:30:48 -0700 | [diff] [blame] | 703 | static struct grep_expr *prep_header_patterns(struct grep_opt *opt) |
| 704 | { |
| 705 | struct grep_pat *p; |
| 706 | struct grep_expr *header_expr; |
Junio C Hamano | 5aaeb73 | 2010-09-12 22:15:35 -0700 | [diff] [blame] | 707 | struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]); |
| 708 | enum grep_header_field fld; |
Junio C Hamano | 95ce9ce | 2010-09-12 19:30:48 -0700 | [diff] [blame] | 709 | |
| 710 | if (!opt->header_list) |
| 711 | return NULL; |
Angus Hammond | 2385f24 | 2012-05-06 19:17:15 +0100 | [diff] [blame] | 712 | |
Junio C Hamano | 95ce9ce | 2010-09-12 19:30:48 -0700 | [diff] [blame] | 713 | for (p = opt->header_list; p; p = p->next) { |
| 714 | if (p->token != GREP_PATTERN_HEAD) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 715 | BUG("a non-header pattern in grep header list."); |
Antoine Pelisse | 3ce3ffb | 2013-02-03 14:37:09 +0000 | [diff] [blame] | 716 | if (p->field < GREP_HEADER_FIELD_MIN || |
| 717 | GREP_HEADER_FIELD_MAX <= p->field) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 718 | BUG("unknown header field %d", p->field); |
Junio C Hamano | 95ce9ce | 2010-09-12 19:30:48 -0700 | [diff] [blame] | 719 | compile_regexp(p, opt); |
| 720 | } |
Junio C Hamano | 5aaeb73 | 2010-09-12 22:15:35 -0700 | [diff] [blame] | 721 | |
| 722 | for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) |
| 723 | header_group[fld] = NULL; |
| 724 | |
| 725 | for (p = opt->header_list; p; p = p->next) { |
| 726 | struct grep_expr *h; |
| 727 | struct grep_pat *pp = p; |
| 728 | |
| 729 | h = compile_pattern_atom(&pp); |
| 730 | if (!h || pp != p->next) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 731 | BUG("malformed header expr"); |
Junio C Hamano | 5aaeb73 | 2010-09-12 22:15:35 -0700 | [diff] [blame] | 732 | if (!header_group[p->field]) { |
| 733 | header_group[p->field] = h; |
| 734 | continue; |
| 735 | } |
| 736 | header_group[p->field] = grep_or_expr(h, header_group[p->field]); |
| 737 | } |
| 738 | |
| 739 | header_expr = NULL; |
| 740 | |
| 741 | for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) { |
| 742 | if (!header_group[fld]) |
| 743 | continue; |
| 744 | if (!header_expr) |
| 745 | header_expr = grep_true_expr(); |
| 746 | header_expr = grep_or_expr(header_group[fld], header_expr); |
| 747 | } |
Junio C Hamano | 95ce9ce | 2010-09-12 19:30:48 -0700 | [diff] [blame] | 748 | return header_expr; |
| 749 | } |
| 750 | |
Junio C Hamano | 13e4fc7 | 2012-09-13 16:26:57 -0700 | [diff] [blame] | 751 | static struct grep_expr *grep_splice_or(struct grep_expr *x, struct grep_expr *y) |
| 752 | { |
| 753 | struct grep_expr *z = x; |
| 754 | |
| 755 | while (x) { |
| 756 | assert(x->node == GREP_NODE_OR); |
| 757 | if (x->u.binary.right && |
| 758 | x->u.binary.right->node == GREP_NODE_TRUE) { |
| 759 | x->u.binary.right = y; |
| 760 | break; |
| 761 | } |
| 762 | x = x->u.binary.right; |
| 763 | } |
| 764 | return z; |
| 765 | } |
| 766 | |
Ævar Arnfjörð Bjarmason | 15c9649 | 2021-01-26 00:36:51 +0100 | [diff] [blame] | 767 | void compile_grep_patterns(struct grep_opt *opt) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 768 | { |
| 769 | struct grep_pat *p; |
Junio C Hamano | 95ce9ce | 2010-09-12 19:30:48 -0700 | [diff] [blame] | 770 | struct grep_expr *header_expr = prep_header_patterns(opt); |
Ævar Arnfjörð Bjarmason | db84376 | 2022-10-11 11:48:45 +0200 | [diff] [blame] | 771 | int extended = 0; |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 772 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 773 | for (p = opt->pattern_list; p; p = p->next) { |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 774 | switch (p->token) { |
| 775 | case GREP_PATTERN: /* atom */ |
| 776 | case GREP_PATTERN_HEAD: |
| 777 | case GREP_PATTERN_BODY: |
René Scharfe | c822255 | 2009-01-10 00:18:34 +0100 | [diff] [blame] | 778 | compile_regexp(p, opt); |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 779 | break; |
| 780 | default: |
Ævar Arnfjörð Bjarmason | db84376 | 2022-10-11 11:48:45 +0200 | [diff] [blame] | 781 | extended = 1; |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 782 | break; |
| 783 | } |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 784 | } |
| 785 | |
René Scharfe | 794c000 | 2021-12-17 17:48:49 +0100 | [diff] [blame] | 786 | if (opt->all_match || opt->no_body_match || header_expr) |
Ævar Arnfjörð Bjarmason | db84376 | 2022-10-11 11:48:45 +0200 | [diff] [blame] | 787 | extended = 1; |
| 788 | else if (!extended) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 789 | return; |
| 790 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 791 | p = opt->pattern_list; |
Michele Ballabio | ba150a3 | 2009-03-18 21:53:27 +0100 | [diff] [blame] | 792 | if (p) |
| 793 | opt->pattern_expression = compile_pattern_expr(&p); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 794 | if (p) |
Ahelenia Ziemiańska | 0d52784 | 2024-03-23 14:18:08 +0100 | [diff] [blame] | 795 | die("incomplete pattern expression group: %s", p->pattern); |
Junio C Hamano | 80235ba | 2010-01-17 20:09:06 -0800 | [diff] [blame] | 796 | |
René Scharfe | 794c000 | 2021-12-17 17:48:49 +0100 | [diff] [blame] | 797 | if (opt->no_body_match && opt->pattern_expression) |
| 798 | opt->pattern_expression = grep_not_expr(opt->pattern_expression); |
| 799 | |
Junio C Hamano | 80235ba | 2010-01-17 20:09:06 -0800 | [diff] [blame] | 800 | if (!header_expr) |
| 801 | return; |
| 802 | |
Junio C Hamano | 5aaeb73 | 2010-09-12 22:15:35 -0700 | [diff] [blame] | 803 | if (!opt->pattern_expression) |
Junio C Hamano | 80235ba | 2010-01-17 20:09:06 -0800 | [diff] [blame] | 804 | opt->pattern_expression = header_expr; |
Junio C Hamano | 13e4fc7 | 2012-09-13 16:26:57 -0700 | [diff] [blame] | 805 | else if (opt->all_match) |
| 806 | opt->pattern_expression = grep_splice_or(header_expr, |
| 807 | opt->pattern_expression); |
Junio C Hamano | 5aaeb73 | 2010-09-12 22:15:35 -0700 | [diff] [blame] | 808 | else |
| 809 | opt->pattern_expression = grep_or_expr(opt->pattern_expression, |
| 810 | header_expr); |
Junio C Hamano | 80235ba | 2010-01-17 20:09:06 -0800 | [diff] [blame] | 811 | opt->all_match = 1; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 812 | } |
| 813 | |
Junio C Hamano | b48fb5b | 2006-09-27 16:27:10 -0700 | [diff] [blame] | 814 | static void free_pattern_expr(struct grep_expr *x) |
| 815 | { |
| 816 | switch (x->node) { |
Junio C Hamano | 5aaeb73 | 2010-09-12 22:15:35 -0700 | [diff] [blame] | 817 | case GREP_NODE_TRUE: |
Junio C Hamano | b48fb5b | 2006-09-27 16:27:10 -0700 | [diff] [blame] | 818 | case GREP_NODE_ATOM: |
| 819 | break; |
| 820 | case GREP_NODE_NOT: |
| 821 | free_pattern_expr(x->u.unary); |
| 822 | break; |
| 823 | case GREP_NODE_AND: |
| 824 | case GREP_NODE_OR: |
| 825 | free_pattern_expr(x->u.binary.left); |
| 826 | free_pattern_expr(x->u.binary.right); |
| 827 | break; |
| 828 | } |
| 829 | free(x); |
| 830 | } |
| 831 | |
Ævar Arnfjörð Bjarmason | 891c996 | 2023-02-07 00:07:50 +0100 | [diff] [blame] | 832 | static void free_grep_pat(struct grep_pat *pattern) |
Junio C Hamano | b48fb5b | 2006-09-27 16:27:10 -0700 | [diff] [blame] | 833 | { |
| 834 | struct grep_pat *p, *n; |
| 835 | |
Ævar Arnfjörð Bjarmason | 891c996 | 2023-02-07 00:07:50 +0100 | [diff] [blame] | 836 | for (p = pattern; p; p = n) { |
Junio C Hamano | b48fb5b | 2006-09-27 16:27:10 -0700 | [diff] [blame] | 837 | n = p->next; |
| 838 | switch (p->token) { |
| 839 | case GREP_PATTERN: /* atom */ |
| 840 | case GREP_PATTERN_HEAD: |
| 841 | case GREP_PATTERN_BODY: |
Ævar Arnfjörð Bjarmason | 7599730 | 2021-01-24 02:58:33 +0100 | [diff] [blame] | 842 | if (p->pcre2_pattern) |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 843 | free_pcre2_pattern(p); |
Michał Kiedrowicz | 63e7e9d | 2011-05-09 23:52:05 +0200 | [diff] [blame] | 844 | else |
| 845 | regfree(&p->regexp); |
René Scharfe | 526a858 | 2012-05-20 16:33:07 +0200 | [diff] [blame] | 846 | free(p->pattern); |
Junio C Hamano | b48fb5b | 2006-09-27 16:27:10 -0700 | [diff] [blame] | 847 | break; |
| 848 | default: |
| 849 | break; |
| 850 | } |
| 851 | free(p); |
| 852 | } |
Ævar Arnfjörð Bjarmason | 891c996 | 2023-02-07 00:07:50 +0100 | [diff] [blame] | 853 | } |
Junio C Hamano | b48fb5b | 2006-09-27 16:27:10 -0700 | [diff] [blame] | 854 | |
Ævar Arnfjörð Bjarmason | 891c996 | 2023-02-07 00:07:50 +0100 | [diff] [blame] | 855 | void free_grep_patterns(struct grep_opt *opt) |
| 856 | { |
| 857 | free_grep_pat(opt->pattern_list); |
Ævar Arnfjörð Bjarmason | fb2ebe7 | 2023-02-07 00:07:51 +0100 | [diff] [blame] | 858 | free_grep_pat(opt->header_list); |
Ævar Arnfjörð Bjarmason | 891c996 | 2023-02-07 00:07:50 +0100 | [diff] [blame] | 859 | |
| 860 | if (opt->pattern_expression) |
| 861 | free_pattern_expr(opt->pattern_expression); |
Junio C Hamano | b48fb5b | 2006-09-27 16:27:10 -0700 | [diff] [blame] | 862 | } |
| 863 | |
Jeff King | 1a845fb | 2021-09-20 23:49:49 -0400 | [diff] [blame] | 864 | static const char *end_of_line(const char *cp, unsigned long *left) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 865 | { |
| 866 | unsigned long l = *left; |
| 867 | while (l && *cp != '\n') { |
| 868 | l--; |
| 869 | cp++; |
| 870 | } |
| 871 | *left = l; |
| 872 | return cp; |
| 873 | } |
| 874 | |
| 875 | static int word_char(char ch) |
| 876 | { |
| 877 | return isalnum(ch) || ch == '_'; |
| 878 | } |
| 879 | |
Mark Lodato | 55f638b | 2010-03-07 11:52:46 -0500 | [diff] [blame] | 880 | static void output_color(struct grep_opt *opt, const void *data, size_t size, |
| 881 | const char *color) |
| 882 | { |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 883 | if (want_color(opt->color) && color && color[0]) { |
Mark Lodato | 55f638b | 2010-03-07 11:52:46 -0500 | [diff] [blame] | 884 | opt->output(opt, color, strlen(color)); |
| 885 | opt->output(opt, data, size); |
| 886 | opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET)); |
| 887 | } else |
| 888 | opt->output(opt, data, size); |
| 889 | } |
| 890 | |
| 891 | static void output_sep(struct grep_opt *opt, char sign) |
| 892 | { |
| 893 | if (opt->null_following_name) |
| 894 | opt->output(opt, "\0", 1); |
| 895 | else |
Nguyễn Thái Ngọc Duy | fa151dc | 2018-05-26 15:55:22 +0200 | [diff] [blame] | 896 | output_color(opt, &sign, 1, opt->colors[GREP_COLOR_SEP]); |
Mark Lodato | 55f638b | 2010-03-07 11:52:46 -0500 | [diff] [blame] | 897 | } |
| 898 | |
Raphael Zimmerer | 83caecc | 2008-10-01 18:11:15 +0200 | [diff] [blame] | 899 | static void show_name(struct grep_opt *opt, const char *name) |
| 900 | { |
Nguyễn Thái Ngọc Duy | fa151dc | 2018-05-26 15:55:22 +0200 | [diff] [blame] | 901 | output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]); |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 902 | opt->output(opt, opt->null_following_name ? "\0" : "\n", 1); |
Raphael Zimmerer | 83caecc | 2008-10-01 18:11:15 +0200 | [diff] [blame] | 903 | } |
| 904 | |
Jeff King | 1a845fb | 2021-09-20 23:49:49 -0400 | [diff] [blame] | 905 | static int patmatch(struct grep_pat *p, |
| 906 | const char *line, const char *eol, |
Michał Kiedrowicz | 97e7778 | 2011-05-05 00:00:19 +0200 | [diff] [blame] | 907 | regmatch_t *match, int eflags) |
| 908 | { |
| 909 | int hit; |
| 910 | |
Ævar Arnfjörð Bjarmason | 7599730 | 2021-01-24 02:58:33 +0100 | [diff] [blame] | 911 | if (p->pcre2_pattern) |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 912 | hit = !pcre2match(p, line, eol, match, eflags); |
Michał Kiedrowicz | 97e7778 | 2011-05-05 00:00:19 +0200 | [diff] [blame] | 913 | else |
Johannes Schindelin | b7d36ff | 2016-09-21 20:24:14 +0200 | [diff] [blame] | 914 | hit = !regexec_buf(&p->regexp, line, eol - line, 1, match, |
| 915 | eflags); |
Michał Kiedrowicz | 97e7778 | 2011-05-05 00:00:19 +0200 | [diff] [blame] | 916 | |
| 917 | return hit; |
| 918 | } |
| 919 | |
Jeff King | 1a845fb | 2021-09-20 23:49:49 -0400 | [diff] [blame] | 920 | static void strip_timestamp(const char *bol, const char **eol_p) |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 921 | { |
Jeff King | 1a845fb | 2021-09-20 23:49:49 -0400 | [diff] [blame] | 922 | const char *eol = *eol_p; |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 923 | |
| 924 | while (bol < --eol) { |
| 925 | if (*eol != '>') |
| 926 | continue; |
| 927 | *eol_p = ++eol; |
Jeff King | cc8e26e | 2021-09-20 23:46:56 -0400 | [diff] [blame] | 928 | break; |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 929 | } |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | static struct { |
| 933 | const char *field; |
| 934 | size_t len; |
| 935 | } header_field[] = { |
| 936 | { "author ", 7 }, |
| 937 | { "committer ", 10 }, |
Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 11:41:28 +0700 | [diff] [blame] | 938 | { "reflog ", 7 }, |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 939 | }; |
| 940 | |
Hamza Mahfooz | 3f566c4 | 2021-09-29 07:57:15 -0400 | [diff] [blame] | 941 | static int headerless_match_one_pattern(struct grep_pat *p, |
| 942 | const char *bol, const char *eol, |
| 943 | enum grep_context ctx, |
| 944 | regmatch_t *pmatch, int eflags) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 945 | { |
| 946 | int hit = 0; |
René Scharfe | e701fad | 2009-05-20 23:31:53 +0200 | [diff] [blame] | 947 | const char *start = bol; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 948 | |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 949 | if ((p->token != GREP_PATTERN) && |
| 950 | ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD))) |
| 951 | return 0; |
| 952 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 953 | again: |
Michał Kiedrowicz | 97e7778 | 2011-05-05 00:00:19 +0200 | [diff] [blame] | 954 | hit = patmatch(p, bol, eol, pmatch, eflags); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 955 | |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 956 | if (hit && p->word_regexp) { |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 957 | if ((pmatch[0].rm_so < 0) || |
René Scharfe | 84201ea | 2009-06-03 18:19:01 +0200 | [diff] [blame] | 958 | (eol - bol) < pmatch[0].rm_so || |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 959 | (pmatch[0].rm_eo < 0) || |
| 960 | (eol - bol) < pmatch[0].rm_eo) |
| 961 | die("regexp returned nonsense"); |
| 962 | |
| 963 | /* Match beginning must be either beginning of the |
| 964 | * line, or at word boundary (i.e. the last char must |
| 965 | * not be a word char). Similarly, match end must be |
| 966 | * either end of the line, or at word boundary |
| 967 | * (i.e. the next char must not be a word char). |
| 968 | */ |
René Scharfe | fb62eb7 | 2009-01-10 00:08:40 +0100 | [diff] [blame] | 969 | if ( ((pmatch[0].rm_so == 0) || |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 970 | !word_char(bol[pmatch[0].rm_so-1])) && |
| 971 | ((pmatch[0].rm_eo == (eol-bol)) || |
| 972 | !word_char(bol[pmatch[0].rm_eo])) ) |
| 973 | ; |
| 974 | else |
| 975 | hit = 0; |
| 976 | |
René Scharfe | 84201ea | 2009-06-03 18:19:01 +0200 | [diff] [blame] | 977 | /* Words consist of at least one character. */ |
| 978 | if (pmatch->rm_so == pmatch->rm_eo) |
| 979 | hit = 0; |
| 980 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 981 | if (!hit && pmatch[0].rm_so + bol + 1 < eol) { |
| 982 | /* There could be more than one match on the |
| 983 | * line, and the first match might not be |
| 984 | * strict word match. But later ones could be! |
René Scharfe | fb62eb7 | 2009-01-10 00:08:40 +0100 | [diff] [blame] | 985 | * Forward to the next possible start, i.e. the |
| 986 | * next position following a non-word char. |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 987 | */ |
| 988 | bol = pmatch[0].rm_so + bol + 1; |
René Scharfe | fb62eb7 | 2009-01-10 00:08:40 +0100 | [diff] [blame] | 989 | while (word_char(bol[-1]) && bol < eol) |
| 990 | bol++; |
René Scharfe | dbb6a4a | 2009-05-23 13:45:26 +0200 | [diff] [blame] | 991 | eflags |= REG_NOTBOL; |
René Scharfe | fb62eb7 | 2009-01-10 00:08:40 +0100 | [diff] [blame] | 992 | if (bol < eol) |
| 993 | goto again; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 994 | } |
| 995 | } |
René Scharfe | e701fad | 2009-05-20 23:31:53 +0200 | [diff] [blame] | 996 | if (hit) { |
| 997 | pmatch[0].rm_so += bol - start; |
| 998 | pmatch[0].rm_eo += bol - start; |
| 999 | } |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1000 | return hit; |
| 1001 | } |
| 1002 | |
Hamza Mahfooz | 3f566c4 | 2021-09-29 07:57:15 -0400 | [diff] [blame] | 1003 | static int match_one_pattern(struct grep_pat *p, |
| 1004 | const char *bol, const char *eol, |
| 1005 | enum grep_context ctx, regmatch_t *pmatch, |
| 1006 | int eflags) |
| 1007 | { |
| 1008 | const char *field; |
| 1009 | size_t len; |
| 1010 | |
| 1011 | if (p->token == GREP_PATTERN_HEAD) { |
| 1012 | assert(p->field < ARRAY_SIZE(header_field)); |
| 1013 | field = header_field[p->field].field; |
| 1014 | len = header_field[p->field].len; |
| 1015 | if (strncmp(bol, field, len)) |
| 1016 | return 0; |
| 1017 | bol += len; |
| 1018 | |
| 1019 | switch (p->field) { |
| 1020 | case GREP_HEADER_AUTHOR: |
| 1021 | case GREP_HEADER_COMMITTER: |
| 1022 | strip_timestamp(bol, &eol); |
| 1023 | break; |
| 1024 | default: |
| 1025 | break; |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | return headerless_match_one_pattern(p, bol, eol, ctx, pmatch, eflags); |
| 1030 | } |
| 1031 | |
| 1032 | |
Jeff King | 1a845fb | 2021-09-20 23:49:49 -0400 | [diff] [blame] | 1033 | static int match_expr_eval(struct grep_opt *opt, struct grep_expr *x, |
| 1034 | const char *bol, const char *eol, |
| 1035 | enum grep_context ctx, ssize_t *col, |
Taylor Blau | 68d686e | 2018-06-22 10:49:35 -0500 | [diff] [blame] | 1036 | ssize_t *icol, int collect_hits) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1037 | { |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 1038 | int h = 0; |
| 1039 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1040 | switch (x->node) { |
Junio C Hamano | 5aaeb73 | 2010-09-12 22:15:35 -0700 | [diff] [blame] | 1041 | case GREP_NODE_TRUE: |
| 1042 | h = 1; |
| 1043 | break; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1044 | case GREP_NODE_ATOM: |
Taylor Blau | 68d686e | 2018-06-22 10:49:35 -0500 | [diff] [blame] | 1045 | { |
| 1046 | regmatch_t tmp; |
| 1047 | h = match_one_pattern(x->u.atom, bol, eol, ctx, |
| 1048 | &tmp, 0); |
| 1049 | if (h && (*col < 0 || tmp.rm_so < *col)) |
| 1050 | *col = tmp.rm_so; |
| 1051 | } |
René Scharfe | 794c000 | 2021-12-17 17:48:49 +0100 | [diff] [blame] | 1052 | if (x->u.atom->token == GREP_PATTERN_BODY) |
| 1053 | opt->body_hit |= h; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1054 | break; |
| 1055 | case GREP_NODE_NOT: |
Taylor Blau | 68d686e | 2018-06-22 10:49:35 -0500 | [diff] [blame] | 1056 | /* |
| 1057 | * Upon visiting a GREP_NODE_NOT, col and icol become swapped. |
| 1058 | */ |
| 1059 | h = !match_expr_eval(opt, x->u.unary, bol, eol, ctx, icol, col, |
| 1060 | 0); |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 1061 | break; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1062 | case GREP_NODE_AND: |
Taylor Blau | 017c0fc | 2018-06-22 10:49:39 -0500 | [diff] [blame] | 1063 | h = match_expr_eval(opt, x->u.binary.left, bol, eol, ctx, col, |
Taylor Blau | 68d686e | 2018-06-22 10:49:35 -0500 | [diff] [blame] | 1064 | icol, 0); |
Taylor Blau | 017c0fc | 2018-06-22 10:49:39 -0500 | [diff] [blame] | 1065 | if (h || opt->columnnum) { |
| 1066 | /* |
| 1067 | * Don't short-circuit AND when given --column, since a |
| 1068 | * NOT earlier in the tree may turn this into an OR. In |
| 1069 | * this case, see the below comment. |
| 1070 | */ |
| 1071 | h &= match_expr_eval(opt, x->u.binary.right, bol, eol, |
| 1072 | ctx, col, icol, 0); |
| 1073 | } |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 1074 | break; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1075 | case GREP_NODE_OR: |
Taylor Blau | 017c0fc | 2018-06-22 10:49:39 -0500 | [diff] [blame] | 1076 | if (!(collect_hits || opt->columnnum)) { |
| 1077 | /* |
| 1078 | * Don't short-circuit OR when given --column (or |
| 1079 | * collecting hits) to ensure we don't skip a later |
| 1080 | * child that would produce an earlier match. |
| 1081 | */ |
Taylor Blau | 68d686e | 2018-06-22 10:49:35 -0500 | [diff] [blame] | 1082 | return (match_expr_eval(opt, x->u.binary.left, bol, eol, |
| 1083 | ctx, col, icol, 0) || |
| 1084 | match_expr_eval(opt, x->u.binary.right, bol, |
| 1085 | eol, ctx, col, icol, 0)); |
Taylor Blau | 017c0fc | 2018-06-22 10:49:39 -0500 | [diff] [blame] | 1086 | } |
Taylor Blau | 68d686e | 2018-06-22 10:49:35 -0500 | [diff] [blame] | 1087 | h = match_expr_eval(opt, x->u.binary.left, bol, eol, ctx, col, |
| 1088 | icol, 0); |
Taylor Blau | 017c0fc | 2018-06-22 10:49:39 -0500 | [diff] [blame] | 1089 | if (collect_hits) |
| 1090 | x->u.binary.left->hit |= h; |
Taylor Blau | 68d686e | 2018-06-22 10:49:35 -0500 | [diff] [blame] | 1091 | h |= match_expr_eval(opt, x->u.binary.right, bol, eol, ctx, col, |
Taylor Blau | 017c0fc | 2018-06-22 10:49:39 -0500 | [diff] [blame] | 1092 | icol, collect_hits); |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 1093 | break; |
| 1094 | default: |
Alexander Potashev | d753070 | 2009-01-04 21:38:41 +0300 | [diff] [blame] | 1095 | die("Unexpected node type (internal error) %d", x->node); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1096 | } |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 1097 | if (collect_hits) |
| 1098 | x->hit |= h; |
| 1099 | return h; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
Jeff King | 1a845fb | 2021-09-20 23:49:49 -0400 | [diff] [blame] | 1102 | static int match_expr(struct grep_opt *opt, |
| 1103 | const char *bol, const char *eol, |
Taylor Blau | 68d686e | 2018-06-22 10:49:35 -0500 | [diff] [blame] | 1104 | enum grep_context ctx, ssize_t *col, |
| 1105 | ssize_t *icol, int collect_hits) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1106 | { |
| 1107 | struct grep_expr *x = opt->pattern_expression; |
Taylor Blau | 68d686e | 2018-06-22 10:49:35 -0500 | [diff] [blame] | 1108 | return match_expr_eval(opt, x, bol, eol, ctx, col, icol, collect_hits); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1109 | } |
| 1110 | |
Jeff King | 1a845fb | 2021-09-20 23:49:49 -0400 | [diff] [blame] | 1111 | static int match_line(struct grep_opt *opt, |
| 1112 | const char *bol, const char *eol, |
Taylor Blau | 68d686e | 2018-06-22 10:49:35 -0500 | [diff] [blame] | 1113 | ssize_t *col, ssize_t *icol, |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 1114 | enum grep_context ctx, int collect_hits) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1115 | { |
| 1116 | struct grep_pat *p; |
Taylor Blau | 017c0fc | 2018-06-22 10:49:39 -0500 | [diff] [blame] | 1117 | int hit = 0; |
René Scharfe | 7921277 | 2009-03-07 13:30:27 +0100 | [diff] [blame] | 1118 | |
Ævar Arnfjörð Bjarmason | db84376 | 2022-10-11 11:48:45 +0200 | [diff] [blame] | 1119 | if (opt->pattern_expression) |
Taylor Blau | 68d686e | 2018-06-22 10:49:35 -0500 | [diff] [blame] | 1120 | return match_expr(opt, bol, eol, ctx, col, icol, |
| 1121 | collect_hits); |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 1122 | |
| 1123 | /* we do not call with collect_hits without being extended */ |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1124 | for (p = opt->pattern_list; p; p = p->next) { |
Taylor Blau | 68d686e | 2018-06-22 10:49:35 -0500 | [diff] [blame] | 1125 | regmatch_t tmp; |
| 1126 | if (match_one_pattern(p, bol, eol, ctx, &tmp, 0)) { |
Taylor Blau | 017c0fc | 2018-06-22 10:49:39 -0500 | [diff] [blame] | 1127 | hit |= 1; |
| 1128 | if (!opt->columnnum) { |
| 1129 | /* |
| 1130 | * Without --column, any single match on a line |
| 1131 | * is enough to know that it needs to be |
| 1132 | * printed. With --column, scan _all_ patterns |
| 1133 | * to find the earliest. |
| 1134 | */ |
| 1135 | break; |
| 1136 | } |
| 1137 | if (*col < 0 || tmp.rm_so < *col) |
| 1138 | *col = tmp.rm_so; |
Taylor Blau | 68d686e | 2018-06-22 10:49:35 -0500 | [diff] [blame] | 1139 | } |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1140 | } |
Taylor Blau | 017c0fc | 2018-06-22 10:49:39 -0500 | [diff] [blame] | 1141 | return hit; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
Jeff King | 1a845fb | 2021-09-20 23:49:49 -0400 | [diff] [blame] | 1144 | static int match_next_pattern(struct grep_pat *p, |
| 1145 | const char *bol, const char *eol, |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 1146 | enum grep_context ctx, |
| 1147 | regmatch_t *pmatch, int eflags) |
| 1148 | { |
| 1149 | regmatch_t match; |
| 1150 | |
Hamza Mahfooz | 3f566c4 | 2021-09-29 07:57:15 -0400 | [diff] [blame] | 1151 | if (!headerless_match_one_pattern(p, bol, eol, ctx, &match, eflags)) |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 1152 | return 0; |
| 1153 | if (match.rm_so < 0 || match.rm_eo < 0) |
| 1154 | return 0; |
| 1155 | if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) { |
| 1156 | if (match.rm_so > pmatch->rm_so) |
| 1157 | return 1; |
| 1158 | if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo) |
| 1159 | return 1; |
| 1160 | } |
| 1161 | pmatch->rm_so = match.rm_so; |
| 1162 | pmatch->rm_eo = match.rm_eo; |
| 1163 | return 1; |
| 1164 | } |
| 1165 | |
Hamza Mahfooz | 3f566c4 | 2021-09-29 07:57:15 -0400 | [diff] [blame] | 1166 | int grep_next_match(struct grep_opt *opt, |
| 1167 | const char *bol, const char *eol, |
| 1168 | enum grep_context ctx, regmatch_t *pmatch, |
| 1169 | enum grep_header_field field, int eflags) |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 1170 | { |
| 1171 | struct grep_pat *p; |
| 1172 | int hit = 0; |
| 1173 | |
| 1174 | pmatch->rm_so = pmatch->rm_eo = -1; |
| 1175 | if (bol < eol) { |
Hamza Mahfooz | 3f566c4 | 2021-09-29 07:57:15 -0400 | [diff] [blame] | 1176 | for (p = ((ctx == GREP_CONTEXT_HEAD) |
| 1177 | ? opt->header_list : opt->pattern_list); |
| 1178 | p; p = p->next) { |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 1179 | switch (p->token) { |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 1180 | case GREP_PATTERN_HEAD: |
Hamza Mahfooz | 3f566c4 | 2021-09-29 07:57:15 -0400 | [diff] [blame] | 1181 | if ((field != GREP_HEADER_FIELD_MAX) && |
| 1182 | (p->field != field)) |
| 1183 | continue; |
| 1184 | /* fall thru */ |
| 1185 | case GREP_PATTERN: /* atom */ |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 1186 | case GREP_PATTERN_BODY: |
| 1187 | hit |= match_next_pattern(p, bol, eol, ctx, |
| 1188 | pmatch, eflags); |
| 1189 | break; |
| 1190 | default: |
| 1191 | break; |
| 1192 | } |
| 1193 | } |
| 1194 | } |
| 1195 | return hit; |
| 1196 | } |
| 1197 | |
Taylor Blau | c707ded | 2018-07-03 16:51:56 -0500 | [diff] [blame] | 1198 | static void show_line_header(struct grep_opt *opt, const char *name, |
| 1199 | unsigned lno, ssize_t cno, char sign) |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 1200 | { |
René Scharfe | 1d84f72 | 2011-06-05 17:24:36 +0200 | [diff] [blame] | 1201 | if (opt->heading && opt->last_shown == 0) { |
Nguyễn Thái Ngọc Duy | fa151dc | 2018-05-26 15:55:22 +0200 | [diff] [blame] | 1202 | output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]); |
René Scharfe | 1d84f72 | 2011-06-05 17:24:36 +0200 | [diff] [blame] | 1203 | opt->output(opt, "\n", 1); |
| 1204 | } |
René Scharfe | 5dd06d3 | 2009-07-02 00:02:38 +0200 | [diff] [blame] | 1205 | opt->last_shown = lno; |
| 1206 | |
René Scharfe | 1d84f72 | 2011-06-05 17:24:36 +0200 | [diff] [blame] | 1207 | if (!opt->heading && opt->pathname) { |
Nguyễn Thái Ngọc Duy | fa151dc | 2018-05-26 15:55:22 +0200 | [diff] [blame] | 1208 | output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]); |
Mark Lodato | 55f638b | 2010-03-07 11:52:46 -0500 | [diff] [blame] | 1209 | output_sep(opt, sign); |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 1210 | } |
| 1211 | if (opt->linenum) { |
| 1212 | char buf[32]; |
Jeff King | 1a168e5 | 2017-03-28 15:46:56 -0400 | [diff] [blame] | 1213 | xsnprintf(buf, sizeof(buf), "%d", lno); |
Nguyễn Thái Ngọc Duy | fa151dc | 2018-05-26 15:55:22 +0200 | [diff] [blame] | 1214 | output_color(opt, buf, strlen(buf), opt->colors[GREP_COLOR_LINENO]); |
Mark Lodato | 55f638b | 2010-03-07 11:52:46 -0500 | [diff] [blame] | 1215 | output_sep(opt, sign); |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 1216 | } |
Taylor Blau | 89252cd | 2018-06-22 10:49:42 -0500 | [diff] [blame] | 1217 | /* |
| 1218 | * Treat 'cno' as the 1-indexed offset from the start of a non-context |
| 1219 | * line to its first match. Otherwise, 'cno' is 0 indicating that we are |
| 1220 | * being called with a context line. |
| 1221 | */ |
| 1222 | if (opt->columnnum && cno) { |
| 1223 | char buf[32]; |
| 1224 | xsnprintf(buf, sizeof(buf), "%"PRIuMAX, (uintmax_t)cno); |
Junio C Hamano | d036d66 | 2018-07-18 12:20:31 -0700 | [diff] [blame] | 1225 | output_color(opt, buf, strlen(buf), opt->colors[GREP_COLOR_COLUMNNO]); |
Taylor Blau | 89252cd | 2018-06-22 10:49:42 -0500 | [diff] [blame] | 1226 | output_sep(opt, sign); |
| 1227 | } |
Taylor Blau | c707ded | 2018-07-03 16:51:56 -0500 | [diff] [blame] | 1228 | } |
| 1229 | |
Jeff King | 1a845fb | 2021-09-20 23:49:49 -0400 | [diff] [blame] | 1230 | static void show_line(struct grep_opt *opt, |
| 1231 | const char *bol, const char *eol, |
Taylor Blau | c707ded | 2018-07-03 16:51:56 -0500 | [diff] [blame] | 1232 | const char *name, unsigned lno, ssize_t cno, char sign) |
| 1233 | { |
| 1234 | int rest = eol - bol; |
Taylor Blau | 9d8db06 | 2018-07-09 15:33:47 -0500 | [diff] [blame] | 1235 | const char *match_color = NULL; |
| 1236 | const char *line_color = NULL; |
Taylor Blau | c707ded | 2018-07-03 16:51:56 -0500 | [diff] [blame] | 1237 | |
| 1238 | if (opt->file_break && opt->last_shown == 0) { |
| 1239 | if (opt->show_hunk_mark) |
| 1240 | opt->output(opt, "\n", 1); |
| 1241 | } else if (opt->pre_context || opt->post_context || opt->funcbody) { |
| 1242 | if (opt->last_shown == 0) { |
| 1243 | if (opt->show_hunk_mark) { |
Junio C Hamano | 87ece7c | 2018-08-02 15:30:44 -0700 | [diff] [blame] | 1244 | output_color(opt, "--", 2, opt->colors[GREP_COLOR_SEP]); |
Taylor Blau | c707ded | 2018-07-03 16:51:56 -0500 | [diff] [blame] | 1245 | opt->output(opt, "\n", 1); |
| 1246 | } |
| 1247 | } else if (lno > opt->last_shown + 1) { |
Junio C Hamano | 87ece7c | 2018-08-02 15:30:44 -0700 | [diff] [blame] | 1248 | output_color(opt, "--", 2, opt->colors[GREP_COLOR_SEP]); |
Taylor Blau | c707ded | 2018-07-03 16:51:56 -0500 | [diff] [blame] | 1249 | opt->output(opt, "\n", 1); |
| 1250 | } |
| 1251 | } |
Taylor Blau | 9d8db06 | 2018-07-09 15:33:47 -0500 | [diff] [blame] | 1252 | if (!opt->only_matching) { |
| 1253 | /* |
| 1254 | * In case the line we're being called with contains more than |
| 1255 | * one match, leave printing each header to the loop below. |
| 1256 | */ |
| 1257 | show_line_header(opt, name, lno, cno, sign); |
| 1258 | } |
| 1259 | if (opt->color || opt->only_matching) { |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 1260 | regmatch_t match; |
| 1261 | enum grep_context ctx = GREP_CONTEXT_BODY; |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 1262 | int eflags = 0; |
| 1263 | |
Taylor Blau | 9d8db06 | 2018-07-09 15:33:47 -0500 | [diff] [blame] | 1264 | if (opt->color) { |
| 1265 | if (sign == ':') |
Junio C Hamano | 87ece7c | 2018-08-02 15:30:44 -0700 | [diff] [blame] | 1266 | match_color = opt->colors[GREP_COLOR_MATCH_SELECTED]; |
Taylor Blau | 9d8db06 | 2018-07-09 15:33:47 -0500 | [diff] [blame] | 1267 | else |
Junio C Hamano | 87ece7c | 2018-08-02 15:30:44 -0700 | [diff] [blame] | 1268 | match_color = opt->colors[GREP_COLOR_MATCH_CONTEXT]; |
Taylor Blau | 9d8db06 | 2018-07-09 15:33:47 -0500 | [diff] [blame] | 1269 | if (sign == ':') |
Junio C Hamano | 87ece7c | 2018-08-02 15:30:44 -0700 | [diff] [blame] | 1270 | line_color = opt->colors[GREP_COLOR_SELECTED]; |
Taylor Blau | 9d8db06 | 2018-07-09 15:33:47 -0500 | [diff] [blame] | 1271 | else if (sign == '-') |
Junio C Hamano | 87ece7c | 2018-08-02 15:30:44 -0700 | [diff] [blame] | 1272 | line_color = opt->colors[GREP_COLOR_CONTEXT]; |
Taylor Blau | 9d8db06 | 2018-07-09 15:33:47 -0500 | [diff] [blame] | 1273 | else if (sign == '=') |
Junio C Hamano | 87ece7c | 2018-08-02 15:30:44 -0700 | [diff] [blame] | 1274 | line_color = opt->colors[GREP_COLOR_FUNCTION]; |
Taylor Blau | 9d8db06 | 2018-07-09 15:33:47 -0500 | [diff] [blame] | 1275 | } |
Hamza Mahfooz | 3f566c4 | 2021-09-29 07:57:15 -0400 | [diff] [blame] | 1276 | while (grep_next_match(opt, bol, eol, ctx, &match, |
| 1277 | GREP_HEADER_FIELD_MAX, eflags)) { |
René Scharfe | 1f5b9cc | 2009-06-01 23:53:05 +0200 | [diff] [blame] | 1278 | if (match.rm_so == match.rm_eo) |
| 1279 | break; |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 1280 | |
Taylor Blau | 9d8db06 | 2018-07-09 15:33:47 -0500 | [diff] [blame] | 1281 | if (opt->only_matching) |
| 1282 | show_line_header(opt, name, lno, cno, sign); |
| 1283 | else |
| 1284 | output_color(opt, bol, match.rm_so, line_color); |
Mark Lodato | 55f638b | 2010-03-07 11:52:46 -0500 | [diff] [blame] | 1285 | output_color(opt, bol + match.rm_so, |
René Scharfe | 79a7710 | 2014-10-27 19:23:05 +0100 | [diff] [blame] | 1286 | match.rm_eo - match.rm_so, match_color); |
Taylor Blau | 9d8db06 | 2018-07-09 15:33:47 -0500 | [diff] [blame] | 1287 | if (opt->only_matching) |
| 1288 | opt->output(opt, "\n", 1); |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 1289 | bol += match.rm_eo; |
Taylor Blau | 9d8db06 | 2018-07-09 15:33:47 -0500 | [diff] [blame] | 1290 | cno += match.rm_eo; |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 1291 | rest -= match.rm_eo; |
| 1292 | eflags = REG_NOTBOL; |
| 1293 | } |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 1294 | } |
Taylor Blau | 9d8db06 | 2018-07-09 15:33:47 -0500 | [diff] [blame] | 1295 | if (!opt->only_matching) { |
| 1296 | output_color(opt, bol, rest, line_color); |
| 1297 | opt->output(opt, "\n", 1); |
| 1298 | } |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 1299 | } |
| 1300 | |
Jeff King | 78db6ea | 2012-02-02 03:18:29 -0500 | [diff] [blame] | 1301 | int grep_use_locks; |
| 1302 | |
Thomas Rast | 0579f91 | 2011-12-12 22:16:07 +0100 | [diff] [blame] | 1303 | /* |
| 1304 | * This lock protects access to the gitattributes machinery, which is |
| 1305 | * not thread-safe. |
| 1306 | */ |
| 1307 | pthread_mutex_t grep_attr_mutex; |
| 1308 | |
Jeff King | 78db6ea | 2012-02-02 03:18:29 -0500 | [diff] [blame] | 1309 | static inline void grep_attr_lock(void) |
Thomas Rast | 0579f91 | 2011-12-12 22:16:07 +0100 | [diff] [blame] | 1310 | { |
Jeff King | 78db6ea | 2012-02-02 03:18:29 -0500 | [diff] [blame] | 1311 | if (grep_use_locks) |
Thomas Rast | 0579f91 | 2011-12-12 22:16:07 +0100 | [diff] [blame] | 1312 | pthread_mutex_lock(&grep_attr_mutex); |
| 1313 | } |
| 1314 | |
Jeff King | 78db6ea | 2012-02-02 03:18:29 -0500 | [diff] [blame] | 1315 | static inline void grep_attr_unlock(void) |
Thomas Rast | 0579f91 | 2011-12-12 22:16:07 +0100 | [diff] [blame] | 1316 | { |
Jeff King | 78db6ea | 2012-02-02 03:18:29 -0500 | [diff] [blame] | 1317 | if (grep_use_locks) |
Thomas Rast | 0579f91 | 2011-12-12 22:16:07 +0100 | [diff] [blame] | 1318 | pthread_mutex_unlock(&grep_attr_mutex); |
| 1319 | } |
Jeff King | b3aeb28 | 2012-02-02 03:18:41 -0500 | [diff] [blame] | 1320 | |
Jeff King | 1a845fb | 2021-09-20 23:49:49 -0400 | [diff] [blame] | 1321 | static int match_funcname(struct grep_opt *opt, struct grep_source *gs, |
| 1322 | const char *bol, const char *eol) |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 1323 | { |
René Scharfe | 60ecac9 | 2009-07-02 00:07:24 +0200 | [diff] [blame] | 1324 | xdemitconf_t *xecfg = opt->priv; |
Thomas Rast | 0579f91 | 2011-12-12 22:16:07 +0100 | [diff] [blame] | 1325 | if (xecfg && !xecfg->find_func) { |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 1326 | grep_source_load_driver(gs, opt->repo->index); |
Jeff King | 94ad9d9 | 2012-02-02 03:20:43 -0500 | [diff] [blame] | 1327 | if (gs->driver->funcname.pattern) { |
| 1328 | const struct userdiff_funcname *pe = &gs->driver->funcname; |
Thomas Rast | 0579f91 | 2011-12-12 22:16:07 +0100 | [diff] [blame] | 1329 | xdiff_set_find_func(xecfg, pe->pattern, pe->cflags); |
| 1330 | } else { |
| 1331 | xecfg = opt->priv = NULL; |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | if (xecfg) { |
René Scharfe | 60ecac9 | 2009-07-02 00:07:24 +0200 | [diff] [blame] | 1336 | char buf[1]; |
| 1337 | return xecfg->find_func(bol, eol - bol, buf, 1, |
| 1338 | xecfg->find_func_priv) >= 0; |
| 1339 | } |
| 1340 | |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 1341 | if (bol == eol) |
| 1342 | return 0; |
| 1343 | if (isalpha(*bol) || *bol == '_' || *bol == '$') |
| 1344 | return 1; |
| 1345 | return 0; |
| 1346 | } |
| 1347 | |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1348 | static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs, |
Jeff King | 1a845fb | 2021-09-20 23:49:49 -0400 | [diff] [blame] | 1349 | const char *bol, unsigned lno) |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 1350 | { |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1351 | while (bol > gs->buf) { |
Jeff King | 1a845fb | 2021-09-20 23:49:49 -0400 | [diff] [blame] | 1352 | const char *eol = --bol; |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 1353 | |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1354 | while (bol > gs->buf && bol[-1] != '\n') |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 1355 | bol--; |
| 1356 | lno--; |
| 1357 | |
| 1358 | if (lno <= opt->last_shown) |
| 1359 | break; |
| 1360 | |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1361 | if (match_funcname(opt, gs, bol, eol)) { |
Taylor Blau | 89252cd | 2018-06-22 10:49:42 -0500 | [diff] [blame] | 1362 | show_line(opt, bol, eol, gs->name, lno, 0, '='); |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 1363 | break; |
| 1364 | } |
| 1365 | } |
| 1366 | } |
| 1367 | |
René Scharfe | a5dc20b | 2017-11-18 19:08:08 +0100 | [diff] [blame] | 1368 | static int is_empty_line(const char *bol, const char *eol); |
| 1369 | |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1370 | static void show_pre_context(struct grep_opt *opt, struct grep_source *gs, |
Jeff King | 1a845fb | 2021-09-20 23:49:49 -0400 | [diff] [blame] | 1371 | const char *bol, const char *end, unsigned lno) |
René Scharfe | 49de321 | 2009-07-02 00:05:17 +0200 | [diff] [blame] | 1372 | { |
René Scharfe | 6653a01 | 2017-11-18 19:07:13 +0100 | [diff] [blame] | 1373 | unsigned cur = lno, from = 1, funcname_lno = 0, orig_from; |
René Scharfe | a5dc20b | 2017-11-18 19:08:08 +0100 | [diff] [blame] | 1374 | int funcname_needed = !!opt->funcname, comment_needed = 0; |
René Scharfe | ba8ea74 | 2011-08-01 19:20:53 +0200 | [diff] [blame] | 1375 | |
René Scharfe | 49de321 | 2009-07-02 00:05:17 +0200 | [diff] [blame] | 1376 | if (opt->pre_context < lno) |
| 1377 | from = lno - opt->pre_context; |
| 1378 | if (from <= opt->last_shown) |
| 1379 | from = opt->last_shown + 1; |
René Scharfe | 6653a01 | 2017-11-18 19:07:13 +0100 | [diff] [blame] | 1380 | orig_from = from; |
René Scharfe | a5dc20b | 2017-11-18 19:08:08 +0100 | [diff] [blame] | 1381 | if (opt->funcbody) { |
| 1382 | if (match_funcname(opt, gs, bol, end)) |
| 1383 | comment_needed = 1; |
| 1384 | else |
| 1385 | funcname_needed = 1; |
René Scharfe | 6653a01 | 2017-11-18 19:07:13 +0100 | [diff] [blame] | 1386 | from = opt->last_shown + 1; |
| 1387 | } |
René Scharfe | 49de321 | 2009-07-02 00:05:17 +0200 | [diff] [blame] | 1388 | |
| 1389 | /* Rewind. */ |
René Scharfe | 6653a01 | 2017-11-18 19:07:13 +0100 | [diff] [blame] | 1390 | while (bol > gs->buf && cur > from) { |
Jeff King | 1a845fb | 2021-09-20 23:49:49 -0400 | [diff] [blame] | 1391 | const char *next_bol = bol; |
| 1392 | const char *eol = --bol; |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 1393 | |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1394 | while (bol > gs->buf && bol[-1] != '\n') |
René Scharfe | 49de321 | 2009-07-02 00:05:17 +0200 | [diff] [blame] | 1395 | bol--; |
| 1396 | cur--; |
René Scharfe | a5dc20b | 2017-11-18 19:08:08 +0100 | [diff] [blame] | 1397 | if (comment_needed && (is_empty_line(bol, eol) || |
| 1398 | match_funcname(opt, gs, bol, eol))) { |
| 1399 | comment_needed = 0; |
| 1400 | from = orig_from; |
| 1401 | if (cur < from) { |
| 1402 | cur++; |
| 1403 | bol = next_bol; |
| 1404 | break; |
| 1405 | } |
| 1406 | } |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1407 | if (funcname_needed && match_funcname(opt, gs, bol, eol)) { |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 1408 | funcname_lno = cur; |
| 1409 | funcname_needed = 0; |
René Scharfe | a5dc20b | 2017-11-18 19:08:08 +0100 | [diff] [blame] | 1410 | if (opt->funcbody) |
| 1411 | comment_needed = 1; |
| 1412 | else |
| 1413 | from = orig_from; |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 1414 | } |
René Scharfe | 49de321 | 2009-07-02 00:05:17 +0200 | [diff] [blame] | 1415 | } |
| 1416 | |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 1417 | /* We need to look even further back to find a function signature. */ |
| 1418 | if (opt->funcname && funcname_needed) |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1419 | show_funcname_line(opt, gs, bol, cur); |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 1420 | |
René Scharfe | 49de321 | 2009-07-02 00:05:17 +0200 | [diff] [blame] | 1421 | /* Back forward. */ |
| 1422 | while (cur < lno) { |
Jeff King | 1a845fb | 2021-09-20 23:49:49 -0400 | [diff] [blame] | 1423 | const char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-'; |
René Scharfe | 49de321 | 2009-07-02 00:05:17 +0200 | [diff] [blame] | 1424 | |
| 1425 | while (*eol != '\n') |
| 1426 | eol++; |
Taylor Blau | 89252cd | 2018-06-22 10:49:42 -0500 | [diff] [blame] | 1427 | show_line(opt, bol, eol, gs->name, cur, 0, sign); |
René Scharfe | 49de321 | 2009-07-02 00:05:17 +0200 | [diff] [blame] | 1428 | bol = eol + 1; |
| 1429 | cur++; |
| 1430 | } |
| 1431 | } |
| 1432 | |
Junio C Hamano | a26345b | 2010-01-10 22:39:36 -0800 | [diff] [blame] | 1433 | static int should_lookahead(struct grep_opt *opt) |
| 1434 | { |
| 1435 | struct grep_pat *p; |
| 1436 | |
Ævar Arnfjörð Bjarmason | db84376 | 2022-10-11 11:48:45 +0200 | [diff] [blame] | 1437 | if (opt->pattern_expression) |
Junio C Hamano | a26345b | 2010-01-10 22:39:36 -0800 | [diff] [blame] | 1438 | return 0; /* punt for too complex stuff */ |
| 1439 | if (opt->invert) |
| 1440 | return 0; |
| 1441 | for (p = opt->pattern_list; p; p = p->next) { |
| 1442 | if (p->token != GREP_PATTERN) |
| 1443 | return 0; /* punt for "header only" and stuff */ |
| 1444 | } |
| 1445 | return 1; |
| 1446 | } |
| 1447 | |
| 1448 | static int look_ahead(struct grep_opt *opt, |
| 1449 | unsigned long *left_p, |
| 1450 | unsigned *lno_p, |
Jeff King | 1a845fb | 2021-09-20 23:49:49 -0400 | [diff] [blame] | 1451 | const char **bol_p) |
Junio C Hamano | a26345b | 2010-01-10 22:39:36 -0800 | [diff] [blame] | 1452 | { |
| 1453 | unsigned lno = *lno_p; |
Jeff King | 1a845fb | 2021-09-20 23:49:49 -0400 | [diff] [blame] | 1454 | const char *bol = *bol_p; |
Junio C Hamano | a26345b | 2010-01-10 22:39:36 -0800 | [diff] [blame] | 1455 | struct grep_pat *p; |
Jeff King | 1a845fb | 2021-09-20 23:49:49 -0400 | [diff] [blame] | 1456 | const char *sp, *last_bol; |
Junio C Hamano | a26345b | 2010-01-10 22:39:36 -0800 | [diff] [blame] | 1457 | regoff_t earliest = -1; |
| 1458 | |
| 1459 | for (p = opt->pattern_list; p; p = p->next) { |
| 1460 | int hit; |
| 1461 | regmatch_t m; |
| 1462 | |
Michał Kiedrowicz | 97e7778 | 2011-05-05 00:00:19 +0200 | [diff] [blame] | 1463 | hit = patmatch(p, bol, bol + *left_p, &m, 0); |
Junio C Hamano | a26345b | 2010-01-10 22:39:36 -0800 | [diff] [blame] | 1464 | if (!hit || m.rm_so < 0 || m.rm_eo < 0) |
| 1465 | continue; |
| 1466 | if (earliest < 0 || m.rm_so < earliest) |
| 1467 | earliest = m.rm_so; |
| 1468 | } |
| 1469 | |
| 1470 | if (earliest < 0) { |
| 1471 | *bol_p = bol + *left_p; |
| 1472 | *left_p = 0; |
| 1473 | return 1; |
| 1474 | } |
| 1475 | for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--) |
| 1476 | ; /* find the beginning of the line */ |
| 1477 | last_bol = sp; |
| 1478 | |
| 1479 | for (sp = bol; sp < last_bol; sp++) { |
| 1480 | if (*sp == '\n') |
| 1481 | lno++; |
| 1482 | } |
| 1483 | *left_p -= last_bol - bol; |
| 1484 | *bol_p = last_bol; |
| 1485 | *lno_p = lno; |
| 1486 | return 0; |
| 1487 | } |
| 1488 | |
Nguyễn Thái Ngọc Duy | 38bbc2e | 2018-09-21 17:57:23 +0200 | [diff] [blame] | 1489 | static int fill_textconv_grep(struct repository *r, |
| 1490 | struct userdiff_driver *driver, |
Jeff King | 335ec3b | 2013-05-10 17:10:15 +0200 | [diff] [blame] | 1491 | struct grep_source *gs) |
| 1492 | { |
| 1493 | struct diff_filespec *df; |
| 1494 | char *buf; |
| 1495 | size_t size; |
| 1496 | |
| 1497 | if (!driver || !driver->textconv) |
| 1498 | return grep_source_load(gs); |
| 1499 | |
| 1500 | /* |
| 1501 | * The textconv interface is intimately tied to diff_filespecs, so we |
| 1502 | * have to pretend to be one. If we could unify the grep_source |
| 1503 | * and diff_filespec structs, this mess could just go away. |
| 1504 | */ |
| 1505 | df = alloc_filespec(gs->path); |
| 1506 | switch (gs->type) { |
Brandon Williams | 1c41c82 | 2017-05-30 10:30:44 -0700 | [diff] [blame] | 1507 | case GREP_SOURCE_OID: |
Jeff King | 335ec3b | 2013-05-10 17:10:15 +0200 | [diff] [blame] | 1508 | fill_filespec(df, gs->identifier, 1, 0100644); |
| 1509 | break; |
| 1510 | case GREP_SOURCE_FILE: |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 1511 | fill_filespec(df, null_oid(), 0, 0100644); |
Jeff King | 335ec3b | 2013-05-10 17:10:15 +0200 | [diff] [blame] | 1512 | break; |
| 1513 | default: |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 1514 | BUG("attempt to textconv something without a path?"); |
Jeff King | 335ec3b | 2013-05-10 17:10:15 +0200 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | /* |
Matheus Tavares | 1d1729c | 2020-01-15 23:39:54 -0300 | [diff] [blame] | 1518 | * fill_textconv is not remotely thread-safe; it modifies the global |
| 1519 | * diff tempfile structure, writes to the_repo's odb and might |
| 1520 | * internally call thread-unsafe functions such as the |
| 1521 | * prepare_packed_git() lazy-initializator. Because of the last two, we |
| 1522 | * must ensure mutual exclusion between this call and the object reading |
| 1523 | * API, thus we use obj_read_lock() here. |
| 1524 | * |
| 1525 | * TODO: allowing text conversion to run in parallel with object |
| 1526 | * reading operations might increase performance in the multithreaded |
| 1527 | * non-worktreee git-grep with --textconv. |
Jeff King | 335ec3b | 2013-05-10 17:10:15 +0200 | [diff] [blame] | 1528 | */ |
Matheus Tavares | 1d1729c | 2020-01-15 23:39:54 -0300 | [diff] [blame] | 1529 | obj_read_lock(); |
Nguyễn Thái Ngọc Duy | 38bbc2e | 2018-09-21 17:57:23 +0200 | [diff] [blame] | 1530 | size = fill_textconv(r, driver, df, &buf); |
Matheus Tavares | 1d1729c | 2020-01-15 23:39:54 -0300 | [diff] [blame] | 1531 | obj_read_unlock(); |
Jeff King | 335ec3b | 2013-05-10 17:10:15 +0200 | [diff] [blame] | 1532 | free_filespec(df); |
| 1533 | |
| 1534 | /* |
| 1535 | * The normal fill_textconv usage by the diff machinery would just keep |
| 1536 | * the textconv'd buf separate from the diff_filespec. But much of the |
| 1537 | * grep code passes around a grep_source and assumes that its "buf" |
| 1538 | * pointer is the beginning of the thing we are searching. So let's |
| 1539 | * install our textconv'd version into the grep_source, taking care not |
| 1540 | * to leak any existing buffer. |
| 1541 | */ |
| 1542 | grep_source_clear_data(gs); |
| 1543 | gs->buf = buf; |
| 1544 | gs->size = size; |
| 1545 | |
| 1546 | return 0; |
| 1547 | } |
| 1548 | |
René Scharfe | 4aa2c47 | 2016-05-28 17:06:19 +0200 | [diff] [blame] | 1549 | static int is_empty_line(const char *bol, const char *eol) |
| 1550 | { |
| 1551 | while (bol < eol && isspace(*bol)) |
| 1552 | bol++; |
| 1553 | return bol == eol; |
| 1554 | } |
| 1555 | |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1556 | static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1557 | { |
Jeff King | 1a845fb | 2021-09-20 23:49:49 -0400 | [diff] [blame] | 1558 | const char *bol; |
| 1559 | const char *peek_bol = NULL; |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1560 | unsigned long left; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1561 | unsigned lno = 1; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1562 | unsigned last_hit = 0; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1563 | int binary_match_only = 0; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1564 | unsigned count = 0; |
Junio C Hamano | a26345b | 2010-01-10 22:39:36 -0800 | [diff] [blame] | 1565 | int try_lookahead = 0; |
René Scharfe | ba8ea74 | 2011-08-01 19:20:53 +0200 | [diff] [blame] | 1566 | int show_function = 0; |
Jeff King | 335ec3b | 2013-05-10 17:10:15 +0200 | [diff] [blame] | 1567 | struct userdiff_driver *textconv = NULL; |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 1568 | enum grep_context ctx = GREP_CONTEXT_HEAD; |
René Scharfe | 60ecac9 | 2009-07-02 00:07:24 +0200 | [diff] [blame] | 1569 | xdemitconf_t xecfg; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1570 | |
Emily Shaffer | de99eb0 | 2019-05-23 13:23:56 -0700 | [diff] [blame] | 1571 | if (!opt->status_only && gs->name == NULL) |
| 1572 | BUG("grep call which could print a name requires " |
| 1573 | "grep_source.name be non-NULL"); |
| 1574 | |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 1575 | if (!opt->output) |
| 1576 | opt->output = std_output; |
| 1577 | |
René Scharfe | ba8ea74 | 2011-08-01 19:20:53 +0200 | [diff] [blame] | 1578 | if (opt->pre_context || opt->post_context || opt->file_break || |
| 1579 | opt->funcbody) { |
René Scharfe | 08303c3 | 2011-06-05 17:24:15 +0200 | [diff] [blame] | 1580 | /* Show hunk marks, except for the first file. */ |
| 1581 | if (opt->last_shown) |
| 1582 | opt->show_hunk_mark = 1; |
| 1583 | /* |
| 1584 | * If we're using threads then we can't easily identify |
| 1585 | * the first file. Always put hunk marks in that case |
| 1586 | * and skip the very first one later in work_done(). |
| 1587 | */ |
| 1588 | if (opt->output != std_output) |
| 1589 | opt->show_hunk_mark = 1; |
| 1590 | } |
René Scharfe | 431d6e7 | 2010-03-15 17:21:10 +0100 | [diff] [blame] | 1591 | opt->last_shown = 0; |
| 1592 | |
Jeff King | 335ec3b | 2013-05-10 17:10:15 +0200 | [diff] [blame] | 1593 | if (opt->allow_textconv) { |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 1594 | grep_source_load_driver(gs, opt->repo->index); |
Jeff King | 335ec3b | 2013-05-10 17:10:15 +0200 | [diff] [blame] | 1595 | /* |
| 1596 | * We might set up the shared textconv cache data here, which |
Matheus Tavares | 1d1729c | 2020-01-15 23:39:54 -0300 | [diff] [blame] | 1597 | * is not thread-safe. Also, get_oid_with_context() and |
| 1598 | * parse_object() might be internally called. As they are not |
Steve Kemp | 84544f2 | 2020-07-29 03:33:28 +0000 | [diff] [blame] | 1599 | * currently thread-safe and might be racy with object reading, |
Matheus Tavares | 1d1729c | 2020-01-15 23:39:54 -0300 | [diff] [blame] | 1600 | * obj_read_lock() must be called. |
Jeff King | 335ec3b | 2013-05-10 17:10:15 +0200 | [diff] [blame] | 1601 | */ |
| 1602 | grep_attr_lock(); |
Matheus Tavares | 1d1729c | 2020-01-15 23:39:54 -0300 | [diff] [blame] | 1603 | obj_read_lock(); |
Nguyễn Thái Ngọc Duy | bd7ad45 | 2018-11-10 06:49:06 +0100 | [diff] [blame] | 1604 | textconv = userdiff_get_textconv(opt->repo, gs->driver); |
Matheus Tavares | 1d1729c | 2020-01-15 23:39:54 -0300 | [diff] [blame] | 1605 | obj_read_unlock(); |
Jeff King | 335ec3b | 2013-05-10 17:10:15 +0200 | [diff] [blame] | 1606 | grep_attr_unlock(); |
| 1607 | } |
| 1608 | |
| 1609 | /* |
| 1610 | * We know the result of a textconv is text, so we only have to care |
| 1611 | * about binary handling if we are not using it. |
| 1612 | */ |
| 1613 | if (!textconv) { |
| 1614 | switch (opt->binary) { |
| 1615 | case GREP_BINARY_DEFAULT: |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 1616 | if (grep_source_is_binary(gs, opt->repo->index)) |
Jeff King | 335ec3b | 2013-05-10 17:10:15 +0200 | [diff] [blame] | 1617 | binary_match_only = 1; |
| 1618 | break; |
| 1619 | case GREP_BINARY_NOMATCH: |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 1620 | if (grep_source_is_binary(gs, opt->repo->index)) |
Jeff King | 335ec3b | 2013-05-10 17:10:15 +0200 | [diff] [blame] | 1621 | return 0; /* Assume unmatch */ |
| 1622 | break; |
| 1623 | case GREP_BINARY_TEXT: |
| 1624 | break; |
| 1625 | default: |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 1626 | BUG("unknown binary handling mode"); |
Jeff King | 335ec3b | 2013-05-10 17:10:15 +0200 | [diff] [blame] | 1627 | } |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1628 | } |
| 1629 | |
René Scharfe | 60ecac9 | 2009-07-02 00:07:24 +0200 | [diff] [blame] | 1630 | memset(&xecfg, 0, sizeof(xecfg)); |
Thomas Rast | 0579f91 | 2011-12-12 22:16:07 +0100 | [diff] [blame] | 1631 | opt->priv = &xecfg; |
| 1632 | |
Junio C Hamano | a26345b | 2010-01-10 22:39:36 -0800 | [diff] [blame] | 1633 | try_lookahead = should_lookahead(opt); |
René Scharfe | 60ecac9 | 2009-07-02 00:07:24 +0200 | [diff] [blame] | 1634 | |
Nguyễn Thái Ngọc Duy | 38bbc2e | 2018-09-21 17:57:23 +0200 | [diff] [blame] | 1635 | if (fill_textconv_grep(opt->repo, textconv, gs) < 0) |
Jeff King | 0826579 | 2012-02-02 03:21:11 -0500 | [diff] [blame] | 1636 | return 0; |
| 1637 | |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1638 | bol = gs->buf; |
| 1639 | left = gs->size; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1640 | while (left) { |
Jeff King | 1a845fb | 2021-09-20 23:49:49 -0400 | [diff] [blame] | 1641 | const char *eol; |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 1642 | int hit; |
Taylor Blau | 89252cd | 2018-06-22 10:49:42 -0500 | [diff] [blame] | 1643 | ssize_t cno; |
Taylor Blau | 68d686e | 2018-06-22 10:49:35 -0500 | [diff] [blame] | 1644 | ssize_t col = -1, icol = -1; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1645 | |
Junio C Hamano | a26345b | 2010-01-10 22:39:36 -0800 | [diff] [blame] | 1646 | /* |
Michał Kiedrowicz | 8997da3 | 2011-05-09 23:52:03 +0200 | [diff] [blame] | 1647 | * look_ahead() skips quickly to the line that possibly |
Junio C Hamano | a26345b | 2010-01-10 22:39:36 -0800 | [diff] [blame] | 1648 | * has the next hit; don't call it if we need to do |
| 1649 | * something more than just skipping the current line |
| 1650 | * in response to an unmatch for the current line. E.g. |
| 1651 | * inside a post-context window, we will show the current |
| 1652 | * line as a context around the previous hit when it |
| 1653 | * doesn't hit. |
| 1654 | */ |
| 1655 | if (try_lookahead |
| 1656 | && !(last_hit |
René Scharfe | ba8ea74 | 2011-08-01 19:20:53 +0200 | [diff] [blame] | 1657 | && (show_function || |
| 1658 | lno <= last_hit + opt->post_context)) |
Junio C Hamano | a26345b | 2010-01-10 22:39:36 -0800 | [diff] [blame] | 1659 | && look_ahead(opt, &left, &lno, &bol)) |
| 1660 | break; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1661 | eol = end_of_line(bol, &left); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1662 | |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 1663 | if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol)) |
| 1664 | ctx = GREP_CONTEXT_BODY; |
| 1665 | |
Taylor Blau | 68d686e | 2018-06-22 10:49:35 -0500 | [diff] [blame] | 1666 | hit = match_line(opt, bol, eol, &col, &icol, ctx, collect_hits); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1667 | |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 1668 | if (collect_hits) |
| 1669 | goto next_line; |
| 1670 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1671 | /* "grep -v -e foo -e bla" should list lines |
| 1672 | * that do not have either, so inversion should |
| 1673 | * be done outside. |
| 1674 | */ |
| 1675 | if (opt->invert) |
| 1676 | hit = !hit; |
| 1677 | if (opt->unmatch_name_only) { |
| 1678 | if (hit) |
| 1679 | return 0; |
| 1680 | goto next_line; |
| 1681 | } |
Carlos López | 68437ed | 2022-06-22 19:47:32 +0000 | [diff] [blame] | 1682 | if (hit && (opt->max_count < 0 || count < opt->max_count)) { |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1683 | count++; |
| 1684 | if (opt->status_only) |
| 1685 | return 1; |
René Scharfe | 321ffcc | 2010-05-22 23:30:48 +0200 | [diff] [blame] | 1686 | if (opt->name_only) { |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1687 | show_name(opt, gs->name); |
René Scharfe | 321ffcc | 2010-05-22 23:30:48 +0200 | [diff] [blame] | 1688 | return 1; |
| 1689 | } |
René Scharfe | c30c10c | 2010-05-22 23:29:35 +0200 | [diff] [blame] | 1690 | if (opt->count) |
| 1691 | goto next_line; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1692 | if (binary_match_only) { |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 1693 | opt->output(opt, "Binary file ", 12); |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1694 | output_color(opt, gs->name, strlen(gs->name), |
Nguyễn Thái Ngọc Duy | fa151dc | 2018-05-26 15:55:22 +0200 | [diff] [blame] | 1695 | opt->colors[GREP_COLOR_FILENAME]); |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 1696 | opt->output(opt, " matches\n", 9); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1697 | return 1; |
| 1698 | } |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1699 | /* Hit at this line. If we haven't shown the |
| 1700 | * pre-context lines, we would need to show them. |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1701 | */ |
René Scharfe | ba8ea74 | 2011-08-01 19:20:53 +0200 | [diff] [blame] | 1702 | if (opt->pre_context || opt->funcbody) |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1703 | show_pre_context(opt, gs, bol, eol, lno); |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 1704 | else if (opt->funcname) |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1705 | show_funcname_line(opt, gs, bol, lno); |
Taylor Blau | 89252cd | 2018-06-22 10:49:42 -0500 | [diff] [blame] | 1706 | cno = opt->invert ? icol : col; |
| 1707 | if (cno < 0) { |
| 1708 | /* |
| 1709 | * A negative cno indicates that there was no |
| 1710 | * match on the line. We are thus inverted and |
| 1711 | * being asked to show all lines that _don't_ |
| 1712 | * match a given expression. Therefore, set cno |
| 1713 | * to 0 to suggest the whole line matches. |
| 1714 | */ |
| 1715 | cno = 0; |
| 1716 | } |
| 1717 | show_line(opt, bol, eol, gs->name, lno, cno + 1, ':'); |
René Scharfe | 5dd06d3 | 2009-07-02 00:02:38 +0200 | [diff] [blame] | 1718 | last_hit = lno; |
René Scharfe | ba8ea74 | 2011-08-01 19:20:53 +0200 | [diff] [blame] | 1719 | if (opt->funcbody) |
| 1720 | show_function = 1; |
| 1721 | goto next_line; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1722 | } |
René Scharfe | 4aa2c47 | 2016-05-28 17:06:19 +0200 | [diff] [blame] | 1723 | if (show_function && (!peek_bol || peek_bol < bol)) { |
| 1724 | unsigned long peek_left = left; |
Jeff King | 1a845fb | 2021-09-20 23:49:49 -0400 | [diff] [blame] | 1725 | const char *peek_eol = eol; |
René Scharfe | 4aa2c47 | 2016-05-28 17:06:19 +0200 | [diff] [blame] | 1726 | |
| 1727 | /* |
| 1728 | * Trailing empty lines are not interesting. |
| 1729 | * Peek past them to see if they belong to the |
| 1730 | * body of the current function. |
| 1731 | */ |
| 1732 | peek_bol = bol; |
| 1733 | while (is_empty_line(peek_bol, peek_eol)) { |
| 1734 | peek_bol = peek_eol + 1; |
| 1735 | peek_eol = end_of_line(peek_bol, &peek_left); |
| 1736 | } |
| 1737 | |
| 1738 | if (match_funcname(opt, gs, peek_bol, peek_eol)) |
| 1739 | show_function = 0; |
| 1740 | } |
René Scharfe | ba8ea74 | 2011-08-01 19:20:53 +0200 | [diff] [blame] | 1741 | if (show_function || |
| 1742 | (last_hit && lno <= last_hit + opt->post_context)) { |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1743 | /* If the last hit is within the post context, |
| 1744 | * we need to show this line. |
| 1745 | */ |
Taylor Blau | 89252cd | 2018-06-22 10:49:42 -0500 | [diff] [blame] | 1746 | show_line(opt, bol, eol, gs->name, lno, col + 1, '-'); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1747 | } |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1748 | |
| 1749 | next_line: |
| 1750 | bol = eol + 1; |
| 1751 | if (!left) |
| 1752 | break; |
| 1753 | left--; |
| 1754 | lno++; |
| 1755 | } |
| 1756 | |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 1757 | if (collect_hits) |
| 1758 | return 0; |
Junio C Hamano | b48fb5b | 2006-09-27 16:27:10 -0700 | [diff] [blame] | 1759 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1760 | if (opt->status_only) |
Anthony Sottile | e1f68c6 | 2017-08-17 18:38:51 -0700 | [diff] [blame] | 1761 | return opt->unmatch_name_only; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1762 | if (opt->unmatch_name_only) { |
| 1763 | /* We did not see any hit, so we want to show this */ |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1764 | show_name(opt, gs->name); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1765 | return 1; |
| 1766 | } |
| 1767 | |
René Scharfe | 60ecac9 | 2009-07-02 00:07:24 +0200 | [diff] [blame] | 1768 | xdiff_clear_find_func(&xecfg); |
| 1769 | opt->priv = NULL; |
| 1770 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1771 | /* NEEDSWORK: |
| 1772 | * The real "grep -c foo *.c" gives many "bar.c:0" lines, |
| 1773 | * which feels mostly useless but sometimes useful. Maybe |
| 1774 | * make it another option? For now suppress them. |
| 1775 | */ |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 1776 | if (opt->count && count) { |
| 1777 | char buf[32]; |
René Scharfe | f76d947 | 2014-03-11 22:15:49 +0100 | [diff] [blame] | 1778 | if (opt->pathname) { |
| 1779 | output_color(opt, gs->name, strlen(gs->name), |
Nguyễn Thái Ngọc Duy | fa151dc | 2018-05-26 15:55:22 +0200 | [diff] [blame] | 1780 | opt->colors[GREP_COLOR_FILENAME]); |
René Scharfe | f76d947 | 2014-03-11 22:15:49 +0100 | [diff] [blame] | 1781 | output_sep(opt, ':'); |
| 1782 | } |
Jeff King | 1a168e5 | 2017-03-28 15:46:56 -0400 | [diff] [blame] | 1783 | xsnprintf(buf, sizeof(buf), "%u\n", count); |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 1784 | opt->output(opt, buf, strlen(buf)); |
René Scharfe | c30c10c | 2010-05-22 23:29:35 +0200 | [diff] [blame] | 1785 | return 1; |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 1786 | } |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1787 | return !!last_hit; |
| 1788 | } |
| 1789 | |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 1790 | static void clr_hit_marker(struct grep_expr *x) |
| 1791 | { |
| 1792 | /* All-hit markers are meaningful only at the very top level |
| 1793 | * OR node. |
| 1794 | */ |
| 1795 | while (1) { |
| 1796 | x->hit = 0; |
| 1797 | if (x->node != GREP_NODE_OR) |
| 1798 | return; |
| 1799 | x->u.binary.left->hit = 0; |
| 1800 | x = x->u.binary.right; |
| 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | static int chk_hit_marker(struct grep_expr *x) |
| 1805 | { |
| 1806 | /* Top level nodes have hit markers. See if they all are hits */ |
| 1807 | while (1) { |
| 1808 | if (x->node != GREP_NODE_OR) |
| 1809 | return x->hit; |
| 1810 | if (!x->u.binary.left->hit) |
| 1811 | return 0; |
| 1812 | x = x->u.binary.right; |
| 1813 | } |
| 1814 | } |
| 1815 | |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1816 | int grep_source(struct grep_opt *opt, struct grep_source *gs) |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 1817 | { |
| 1818 | /* |
| 1819 | * we do not have to do the two-pass grep when we do not check |
| 1820 | * buffer-wide "all-match". |
| 1821 | */ |
René Scharfe | 794c000 | 2021-12-17 17:48:49 +0100 | [diff] [blame] | 1822 | if (!opt->all_match && !opt->no_body_match) |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1823 | return grep_source_1(opt, gs, 0); |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 1824 | |
| 1825 | /* Otherwise the toplevel "or" terms hit a bit differently. |
| 1826 | * We first clear hit markers from them. |
| 1827 | */ |
| 1828 | clr_hit_marker(opt->pattern_expression); |
René Scharfe | 794c000 | 2021-12-17 17:48:49 +0100 | [diff] [blame] | 1829 | opt->body_hit = 0; |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1830 | grep_source_1(opt, gs, 1); |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 1831 | |
René Scharfe | 794c000 | 2021-12-17 17:48:49 +0100 | [diff] [blame] | 1832 | if (opt->all_match && !chk_hit_marker(opt->pattern_expression)) |
| 1833 | return 0; |
| 1834 | if (opt->no_body_match && opt->body_hit) |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 1835 | return 0; |
| 1836 | |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1837 | return grep_source_1(opt, gs, 0); |
| 1838 | } |
| 1839 | |
Jeff King | 1e66871 | 2021-09-20 23:51:28 -0400 | [diff] [blame] | 1840 | static void grep_source_init_buf(struct grep_source *gs, |
| 1841 | const char *buf, |
Jonathan Tan | 50d92b5 | 2021-08-16 14:09:53 -0700 | [diff] [blame] | 1842 | unsigned long size) |
| 1843 | { |
| 1844 | gs->type = GREP_SOURCE_BUF; |
| 1845 | gs->name = NULL; |
| 1846 | gs->path = NULL; |
| 1847 | gs->buf = buf; |
| 1848 | gs->size = size; |
| 1849 | gs->driver = NULL; |
| 1850 | gs->identifier = NULL; |
| 1851 | } |
| 1852 | |
Jeff King | 1e66871 | 2021-09-20 23:51:28 -0400 | [diff] [blame] | 1853 | int grep_buffer(struct grep_opt *opt, const char *buf, unsigned long size) |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1854 | { |
| 1855 | struct grep_source gs; |
| 1856 | int r; |
| 1857 | |
Jonathan Tan | 50d92b5 | 2021-08-16 14:09:53 -0700 | [diff] [blame] | 1858 | grep_source_init_buf(&gs, buf, size); |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1859 | |
| 1860 | r = grep_source(opt, &gs); |
| 1861 | |
| 1862 | grep_source_clear(&gs); |
| 1863 | return r; |
| 1864 | } |
| 1865 | |
Jonathan Tan | 50d92b5 | 2021-08-16 14:09:53 -0700 | [diff] [blame] | 1866 | void grep_source_init_file(struct grep_source *gs, const char *name, |
| 1867 | const char *path) |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1868 | { |
Jonathan Tan | 50d92b5 | 2021-08-16 14:09:53 -0700 | [diff] [blame] | 1869 | gs->type = GREP_SOURCE_FILE; |
Jeff King | 8c53f07 | 2015-01-12 20:59:09 -0500 | [diff] [blame] | 1870 | gs->name = xstrdup_or_null(name); |
| 1871 | gs->path = xstrdup_or_null(path); |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1872 | gs->buf = NULL; |
| 1873 | gs->size = 0; |
Jeff King | 94ad9d9 | 2012-02-02 03:20:43 -0500 | [diff] [blame] | 1874 | gs->driver = NULL; |
Jonathan Tan | 50d92b5 | 2021-08-16 14:09:53 -0700 | [diff] [blame] | 1875 | gs->identifier = xstrdup(path); |
| 1876 | } |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1877 | |
Jonathan Tan | 50d92b5 | 2021-08-16 14:09:53 -0700 | [diff] [blame] | 1878 | void grep_source_init_oid(struct grep_source *gs, const char *name, |
Jonathan Tan | 0693806 | 2021-08-16 14:09:56 -0700 | [diff] [blame] | 1879 | const char *path, const struct object_id *oid, |
| 1880 | struct repository *repo) |
Jonathan Tan | 50d92b5 | 2021-08-16 14:09:53 -0700 | [diff] [blame] | 1881 | { |
| 1882 | gs->type = GREP_SOURCE_OID; |
| 1883 | gs->name = xstrdup_or_null(name); |
| 1884 | gs->path = xstrdup_or_null(path); |
| 1885 | gs->buf = NULL; |
| 1886 | gs->size = 0; |
| 1887 | gs->driver = NULL; |
| 1888 | gs->identifier = oiddup(oid); |
Jonathan Tan | 0693806 | 2021-08-16 14:09:56 -0700 | [diff] [blame] | 1889 | gs->repo = repo; |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | void grep_source_clear(struct grep_source *gs) |
| 1893 | { |
Ævar Arnfjörð Bjarmason | 88ce3ef | 2017-06-15 23:15:49 +0000 | [diff] [blame] | 1894 | FREE_AND_NULL(gs->name); |
| 1895 | FREE_AND_NULL(gs->path); |
| 1896 | FREE_AND_NULL(gs->identifier); |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1897 | grep_source_clear_data(gs); |
| 1898 | } |
| 1899 | |
| 1900 | void grep_source_clear_data(struct grep_source *gs) |
| 1901 | { |
| 1902 | switch (gs->type) { |
| 1903 | case GREP_SOURCE_FILE: |
Brandon Williams | 1c41c82 | 2017-05-30 10:30:44 -0700 | [diff] [blame] | 1904 | case GREP_SOURCE_OID: |
Jeff King | 1e66871 | 2021-09-20 23:51:28 -0400 | [diff] [blame] | 1905 | /* these types own the buffer */ |
| 1906 | free((char *)gs->buf); |
| 1907 | gs->buf = NULL; |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1908 | gs->size = 0; |
| 1909 | break; |
| 1910 | case GREP_SOURCE_BUF: |
| 1911 | /* leave user-provided buf intact */ |
| 1912 | break; |
| 1913 | } |
| 1914 | } |
| 1915 | |
Brandon Williams | 1c41c82 | 2017-05-30 10:30:44 -0700 | [diff] [blame] | 1916 | static int grep_source_load_oid(struct grep_source *gs) |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1917 | { |
| 1918 | enum object_type type; |
| 1919 | |
Jonathan Tan | 0693806 | 2021-08-16 14:09:56 -0700 | [diff] [blame] | 1920 | gs->buf = repo_read_object_file(gs->repo, gs->identifier, &type, |
| 1921 | &gs->size); |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1922 | if (!gs->buf) |
| 1923 | return error(_("'%s': unable to read %s"), |
| 1924 | gs->name, |
Brandon Williams | 1c41c82 | 2017-05-30 10:30:44 -0700 | [diff] [blame] | 1925 | oid_to_hex(gs->identifier)); |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1926 | return 0; |
| 1927 | } |
| 1928 | |
| 1929 | static int grep_source_load_file(struct grep_source *gs) |
| 1930 | { |
| 1931 | const char *filename = gs->identifier; |
| 1932 | struct stat st; |
| 1933 | char *data; |
| 1934 | size_t size; |
| 1935 | int i; |
| 1936 | |
| 1937 | if (lstat(filename, &st) < 0) { |
| 1938 | err_ret: |
| 1939 | if (errno != ENOENT) |
Nguyễn Thái Ngọc Duy | 7645d8f | 2016-05-08 16:47:47 +0700 | [diff] [blame] | 1940 | error_errno(_("failed to stat '%s'"), filename); |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1941 | return -1; |
| 1942 | } |
| 1943 | if (!S_ISREG(st.st_mode)) |
| 1944 | return -1; |
| 1945 | size = xsize_t(st.st_size); |
| 1946 | i = open(filename, O_RDONLY); |
| 1947 | if (i < 0) |
| 1948 | goto err_ret; |
Jeff King | 3733e69 | 2016-02-22 17:44:28 -0500 | [diff] [blame] | 1949 | data = xmallocz(size); |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1950 | if (st.st_size != read_in_full(i, data, size)) { |
Nguyễn Thái Ngọc Duy | 7645d8f | 2016-05-08 16:47:47 +0700 | [diff] [blame] | 1951 | error_errno(_("'%s': short read"), filename); |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1952 | close(i); |
| 1953 | free(data); |
| 1954 | return -1; |
| 1955 | } |
| 1956 | close(i); |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1957 | |
| 1958 | gs->buf = data; |
| 1959 | gs->size = size; |
| 1960 | return 0; |
| 1961 | } |
| 1962 | |
Junio C Hamano | 3083301 | 2012-09-20 14:20:09 -0700 | [diff] [blame] | 1963 | static int grep_source_load(struct grep_source *gs) |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1964 | { |
| 1965 | if (gs->buf) |
| 1966 | return 0; |
| 1967 | |
| 1968 | switch (gs->type) { |
| 1969 | case GREP_SOURCE_FILE: |
| 1970 | return grep_source_load_file(gs); |
Brandon Williams | 1c41c82 | 2017-05-30 10:30:44 -0700 | [diff] [blame] | 1971 | case GREP_SOURCE_OID: |
| 1972 | return grep_source_load_oid(gs); |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 1973 | case GREP_SOURCE_BUF: |
| 1974 | return gs->buf ? 0 : -1; |
| 1975 | } |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 1976 | BUG("invalid grep_source type to load"); |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 1977 | } |
Jeff King | 94ad9d9 | 2012-02-02 03:20:43 -0500 | [diff] [blame] | 1978 | |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 1979 | void grep_source_load_driver(struct grep_source *gs, |
| 1980 | struct index_state *istate) |
Jeff King | 94ad9d9 | 2012-02-02 03:20:43 -0500 | [diff] [blame] | 1981 | { |
| 1982 | if (gs->driver) |
| 1983 | return; |
| 1984 | |
| 1985 | grep_attr_lock(); |
Matheus Tavares | 1d1729c | 2020-01-15 23:39:54 -0300 | [diff] [blame] | 1986 | if (gs->path) |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 1987 | gs->driver = userdiff_find_by_path(istate, gs->path); |
Jeff King | 94ad9d9 | 2012-02-02 03:20:43 -0500 | [diff] [blame] | 1988 | if (!gs->driver) |
| 1989 | gs->driver = userdiff_find_by_name("default"); |
| 1990 | grep_attr_unlock(); |
| 1991 | } |
Jeff King | 41b59bf | 2012-02-02 03:21:02 -0500 | [diff] [blame] | 1992 | |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 1993 | static int grep_source_is_binary(struct grep_source *gs, |
| 1994 | struct index_state *istate) |
Jeff King | 41b59bf | 2012-02-02 03:21:02 -0500 | [diff] [blame] | 1995 | { |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 1996 | grep_source_load_driver(gs, istate); |
Jeff King | 41b59bf | 2012-02-02 03:21:02 -0500 | [diff] [blame] | 1997 | if (gs->driver->binary != -1) |
| 1998 | return gs->driver->binary; |
| 1999 | |
| 2000 | if (!grep_source_load(gs)) |
| 2001 | return buffer_is_binary(gs->buf, gs->size); |
| 2002 | |
| 2003 | return 0; |
| 2004 | } |