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