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