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