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