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