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