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 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 62 | static void compile_regexp(struct grep_pat *p, struct grep_opt *opt) |
| 63 | { |
René Scharfe | c822255 | 2009-01-10 00:18:34 +0100 | [diff] [blame] | 64 | int err; |
| 65 | |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 66 | p->word_regexp = opt->word_regexp; |
Brian Collins | 5183bf6 | 2009-11-06 01:22:35 -0800 | [diff] [blame] | 67 | p->ignore_case = opt->ignore_case; |
René Scharfe | 7928610 | 2010-02-03 19:16:30 +0100 | [diff] [blame] | 68 | p->fixed = opt->fixed; |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 69 | |
René Scharfe | c822255 | 2009-01-10 00:18:34 +0100 | [diff] [blame] | 70 | if (p->fixed) |
| 71 | return; |
| 72 | |
| 73 | err = regcomp(&p->regexp, p->pattern, opt->regflags); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 74 | if (err) { |
| 75 | char errbuf[1024]; |
| 76 | char where[1024]; |
| 77 | if (p->no) |
| 78 | sprintf(where, "In '%s' at %d, ", |
| 79 | p->origin, p->no); |
| 80 | else if (p->origin) |
| 81 | sprintf(where, "%s, ", p->origin); |
| 82 | else |
| 83 | where[0] = 0; |
| 84 | regerror(err, &p->regexp, errbuf, 1024); |
| 85 | regfree(&p->regexp); |
| 86 | die("%s'%s': %s", where, p->pattern, errbuf); |
| 87 | } |
| 88 | } |
| 89 | |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 90 | static struct grep_expr *compile_pattern_or(struct grep_pat **); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 91 | static struct grep_expr *compile_pattern_atom(struct grep_pat **list) |
| 92 | { |
| 93 | struct grep_pat *p; |
| 94 | struct grep_expr *x; |
| 95 | |
| 96 | p = *list; |
Linus Torvalds | c922b01 | 2009-04-27 11:10:24 -0700 | [diff] [blame] | 97 | if (!p) |
| 98 | return NULL; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 99 | switch (p->token) { |
| 100 | case GREP_PATTERN: /* atom */ |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 101 | case GREP_PATTERN_HEAD: |
| 102 | case GREP_PATTERN_BODY: |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 103 | x = xcalloc(1, sizeof (struct grep_expr)); |
| 104 | x->node = GREP_NODE_ATOM; |
| 105 | x->u.atom = p; |
| 106 | *list = p->next; |
| 107 | return x; |
| 108 | case GREP_OPEN_PAREN: |
| 109 | *list = p->next; |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 110 | x = compile_pattern_or(list); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 111 | if (!*list || (*list)->token != GREP_CLOSE_PAREN) |
| 112 | die("unmatched parenthesis"); |
| 113 | *list = (*list)->next; |
| 114 | return x; |
| 115 | default: |
| 116 | return NULL; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | static struct grep_expr *compile_pattern_not(struct grep_pat **list) |
| 121 | { |
| 122 | struct grep_pat *p; |
| 123 | struct grep_expr *x; |
| 124 | |
| 125 | p = *list; |
Linus Torvalds | c922b01 | 2009-04-27 11:10:24 -0700 | [diff] [blame] | 126 | if (!p) |
| 127 | return NULL; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 128 | switch (p->token) { |
| 129 | case GREP_NOT: |
| 130 | if (!p->next) |
| 131 | die("--not not followed by pattern expression"); |
| 132 | *list = p->next; |
| 133 | x = xcalloc(1, sizeof (struct grep_expr)); |
| 134 | x->node = GREP_NODE_NOT; |
| 135 | x->u.unary = compile_pattern_not(list); |
| 136 | if (!x->u.unary) |
| 137 | die("--not followed by non pattern expression"); |
| 138 | return x; |
| 139 | default: |
| 140 | return compile_pattern_atom(list); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | static struct grep_expr *compile_pattern_and(struct grep_pat **list) |
| 145 | { |
| 146 | struct grep_pat *p; |
| 147 | struct grep_expr *x, *y, *z; |
| 148 | |
| 149 | x = compile_pattern_not(list); |
| 150 | p = *list; |
| 151 | if (p && p->token == GREP_AND) { |
| 152 | if (!p->next) |
| 153 | die("--and not followed by pattern expression"); |
| 154 | *list = p->next; |
| 155 | y = compile_pattern_and(list); |
| 156 | if (!y) |
| 157 | die("--and not followed by pattern expression"); |
| 158 | z = xcalloc(1, sizeof (struct grep_expr)); |
| 159 | z->node = GREP_NODE_AND; |
| 160 | z->u.binary.left = x; |
| 161 | z->u.binary.right = y; |
| 162 | return z; |
| 163 | } |
| 164 | return x; |
| 165 | } |
| 166 | |
| 167 | static struct grep_expr *compile_pattern_or(struct grep_pat **list) |
| 168 | { |
| 169 | struct grep_pat *p; |
| 170 | struct grep_expr *x, *y, *z; |
| 171 | |
| 172 | x = compile_pattern_and(list); |
| 173 | p = *list; |
| 174 | if (x && p && p->token != GREP_CLOSE_PAREN) { |
| 175 | y = compile_pattern_or(list); |
| 176 | if (!y) |
| 177 | die("not a pattern expression %s", p->pattern); |
| 178 | z = xcalloc(1, sizeof (struct grep_expr)); |
| 179 | z->node = GREP_NODE_OR; |
| 180 | z->u.binary.left = x; |
| 181 | z->u.binary.right = y; |
| 182 | return z; |
| 183 | } |
| 184 | return x; |
| 185 | } |
| 186 | |
| 187 | static struct grep_expr *compile_pattern_expr(struct grep_pat **list) |
| 188 | { |
| 189 | return compile_pattern_or(list); |
| 190 | } |
| 191 | |
Junio C Hamano | 5aaeb73 | 2010-09-12 22:15:35 -0700 | [diff] [blame] | 192 | static struct grep_expr *grep_true_expr(void) |
| 193 | { |
| 194 | struct grep_expr *z = xcalloc(1, sizeof(*z)); |
| 195 | z->node = GREP_NODE_TRUE; |
| 196 | return z; |
| 197 | } |
| 198 | |
| 199 | static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right) |
| 200 | { |
| 201 | struct grep_expr *z = xcalloc(1, sizeof(*z)); |
| 202 | z->node = GREP_NODE_OR; |
| 203 | z->u.binary.left = left; |
| 204 | z->u.binary.right = right; |
| 205 | return z; |
| 206 | } |
| 207 | |
Junio C Hamano | 95ce9ce | 2010-09-12 19:30:48 -0700 | [diff] [blame] | 208 | static struct grep_expr *prep_header_patterns(struct grep_opt *opt) |
| 209 | { |
| 210 | struct grep_pat *p; |
| 211 | struct grep_expr *header_expr; |
Junio C Hamano | 5aaeb73 | 2010-09-12 22:15:35 -0700 | [diff] [blame] | 212 | struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]); |
| 213 | enum grep_header_field fld; |
Junio C Hamano | 95ce9ce | 2010-09-12 19:30:48 -0700 | [diff] [blame] | 214 | |
| 215 | if (!opt->header_list) |
| 216 | return NULL; |
| 217 | p = opt->header_list; |
Junio C Hamano | 95ce9ce | 2010-09-12 19:30:48 -0700 | [diff] [blame] | 218 | for (p = opt->header_list; p; p = p->next) { |
| 219 | if (p->token != GREP_PATTERN_HEAD) |
| 220 | die("bug: a non-header pattern in grep header list."); |
| 221 | if (p->field < 0 || GREP_HEADER_FIELD_MAX <= p->field) |
| 222 | die("bug: unknown header field %d", p->field); |
| 223 | compile_regexp(p, opt); |
| 224 | } |
Junio C Hamano | 5aaeb73 | 2010-09-12 22:15:35 -0700 | [diff] [blame] | 225 | |
| 226 | for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) |
| 227 | header_group[fld] = NULL; |
| 228 | |
| 229 | for (p = opt->header_list; p; p = p->next) { |
| 230 | struct grep_expr *h; |
| 231 | struct grep_pat *pp = p; |
| 232 | |
| 233 | h = compile_pattern_atom(&pp); |
| 234 | if (!h || pp != p->next) |
| 235 | die("bug: malformed header expr"); |
| 236 | if (!header_group[p->field]) { |
| 237 | header_group[p->field] = h; |
| 238 | continue; |
| 239 | } |
| 240 | header_group[p->field] = grep_or_expr(h, header_group[p->field]); |
| 241 | } |
| 242 | |
| 243 | header_expr = NULL; |
| 244 | |
| 245 | for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) { |
| 246 | if (!header_group[fld]) |
| 247 | continue; |
| 248 | if (!header_expr) |
| 249 | header_expr = grep_true_expr(); |
| 250 | header_expr = grep_or_expr(header_group[fld], header_expr); |
| 251 | } |
Junio C Hamano | 95ce9ce | 2010-09-12 19:30:48 -0700 | [diff] [blame] | 252 | return header_expr; |
| 253 | } |
| 254 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 255 | void compile_grep_patterns(struct grep_opt *opt) |
| 256 | { |
| 257 | struct grep_pat *p; |
Junio C Hamano | 95ce9ce | 2010-09-12 19:30:48 -0700 | [diff] [blame] | 258 | struct grep_expr *header_expr = prep_header_patterns(opt); |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 259 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 260 | for (p = opt->pattern_list; p; p = p->next) { |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 261 | switch (p->token) { |
| 262 | case GREP_PATTERN: /* atom */ |
| 263 | case GREP_PATTERN_HEAD: |
| 264 | case GREP_PATTERN_BODY: |
René Scharfe | c822255 | 2009-01-10 00:18:34 +0100 | [diff] [blame] | 265 | compile_regexp(p, opt); |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 266 | break; |
| 267 | default: |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 268 | opt->extended = 1; |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 269 | break; |
| 270 | } |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Junio C Hamano | 80235ba | 2010-01-17 20:09:06 -0800 | [diff] [blame] | 273 | if (opt->all_match || header_expr) |
| 274 | opt->extended = 1; |
| 275 | else if (!opt->extended) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 276 | return; |
| 277 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 278 | p = opt->pattern_list; |
Michele Ballabio | ba150a3 | 2009-03-18 21:53:27 +0100 | [diff] [blame] | 279 | if (p) |
| 280 | opt->pattern_expression = compile_pattern_expr(&p); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 281 | if (p) |
| 282 | die("incomplete pattern expression: %s", p->pattern); |
Junio C Hamano | 80235ba | 2010-01-17 20:09:06 -0800 | [diff] [blame] | 283 | |
| 284 | if (!header_expr) |
| 285 | return; |
| 286 | |
Junio C Hamano | 5aaeb73 | 2010-09-12 22:15:35 -0700 | [diff] [blame] | 287 | if (!opt->pattern_expression) |
Junio C Hamano | 80235ba | 2010-01-17 20:09:06 -0800 | [diff] [blame] | 288 | opt->pattern_expression = header_expr; |
Junio C Hamano | 5aaeb73 | 2010-09-12 22:15:35 -0700 | [diff] [blame] | 289 | else |
| 290 | opt->pattern_expression = grep_or_expr(opt->pattern_expression, |
| 291 | header_expr); |
Junio C Hamano | 80235ba | 2010-01-17 20:09:06 -0800 | [diff] [blame] | 292 | opt->all_match = 1; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Junio C Hamano | b48fb5b | 2006-09-27 16:27:10 -0700 | [diff] [blame] | 295 | static void free_pattern_expr(struct grep_expr *x) |
| 296 | { |
| 297 | switch (x->node) { |
Junio C Hamano | 5aaeb73 | 2010-09-12 22:15:35 -0700 | [diff] [blame] | 298 | case GREP_NODE_TRUE: |
Junio C Hamano | b48fb5b | 2006-09-27 16:27:10 -0700 | [diff] [blame] | 299 | case GREP_NODE_ATOM: |
| 300 | break; |
| 301 | case GREP_NODE_NOT: |
| 302 | free_pattern_expr(x->u.unary); |
| 303 | break; |
| 304 | case GREP_NODE_AND: |
| 305 | case GREP_NODE_OR: |
| 306 | free_pattern_expr(x->u.binary.left); |
| 307 | free_pattern_expr(x->u.binary.right); |
| 308 | break; |
| 309 | } |
| 310 | free(x); |
| 311 | } |
| 312 | |
| 313 | void free_grep_patterns(struct grep_opt *opt) |
| 314 | { |
| 315 | struct grep_pat *p, *n; |
| 316 | |
| 317 | for (p = opt->pattern_list; p; p = n) { |
| 318 | n = p->next; |
| 319 | switch (p->token) { |
| 320 | case GREP_PATTERN: /* atom */ |
| 321 | case GREP_PATTERN_HEAD: |
| 322 | case GREP_PATTERN_BODY: |
| 323 | regfree(&p->regexp); |
| 324 | break; |
| 325 | default: |
| 326 | break; |
| 327 | } |
| 328 | free(p); |
| 329 | } |
| 330 | |
| 331 | if (!opt->extended) |
| 332 | return; |
| 333 | free_pattern_expr(opt->pattern_expression); |
| 334 | } |
| 335 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 336 | static char *end_of_line(char *cp, unsigned long *left) |
| 337 | { |
| 338 | unsigned long l = *left; |
| 339 | while (l && *cp != '\n') { |
| 340 | l--; |
| 341 | cp++; |
| 342 | } |
| 343 | *left = l; |
| 344 | return cp; |
| 345 | } |
| 346 | |
| 347 | static int word_char(char ch) |
| 348 | { |
| 349 | return isalnum(ch) || ch == '_'; |
| 350 | } |
| 351 | |
Mark Lodato | 55f638b | 2010-03-07 11:52:46 -0500 | [diff] [blame] | 352 | static void output_color(struct grep_opt *opt, const void *data, size_t size, |
| 353 | const char *color) |
| 354 | { |
| 355 | if (opt->color && color && color[0]) { |
| 356 | opt->output(opt, color, strlen(color)); |
| 357 | opt->output(opt, data, size); |
| 358 | opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET)); |
| 359 | } else |
| 360 | opt->output(opt, data, size); |
| 361 | } |
| 362 | |
| 363 | static void output_sep(struct grep_opt *opt, char sign) |
| 364 | { |
| 365 | if (opt->null_following_name) |
| 366 | opt->output(opt, "\0", 1); |
| 367 | else |
| 368 | output_color(opt, &sign, 1, opt->color_sep); |
| 369 | } |
| 370 | |
Raphael Zimmerer | 83caecc | 2008-10-01 18:11:15 +0200 | [diff] [blame] | 371 | static void show_name(struct grep_opt *opt, const char *name) |
| 372 | { |
Mark Lodato | 55f638b | 2010-03-07 11:52:46 -0500 | [diff] [blame] | 373 | output_color(opt, name, strlen(name), opt->color_filename); |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 374 | opt->output(opt, opt->null_following_name ? "\0" : "\n", 1); |
Raphael Zimmerer | 83caecc | 2008-10-01 18:11:15 +0200 | [diff] [blame] | 375 | } |
| 376 | |
René Scharfe | ed40a09 | 2010-05-22 23:43:43 +0200 | [diff] [blame] | 377 | static int fixmatch(struct grep_pat *p, char *line, char *eol, |
| 378 | regmatch_t *match) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 379 | { |
Brian Collins | 5183bf6 | 2009-11-06 01:22:35 -0800 | [diff] [blame] | 380 | char *hit; |
René Scharfe | 1baddf4 | 2010-05-22 23:32:43 +0200 | [diff] [blame] | 381 | |
René Scharfe | ed40a09 | 2010-05-22 23:43:43 +0200 | [diff] [blame] | 382 | if (p->ignore_case) { |
René Scharfe | 52d799a | 2010-05-22 23:34:06 +0200 | [diff] [blame] | 383 | char *s = line; |
| 384 | do { |
René Scharfe | ed40a09 | 2010-05-22 23:43:43 +0200 | [diff] [blame] | 385 | hit = strcasestr(s, p->pattern); |
René Scharfe | 52d799a | 2010-05-22 23:34:06 +0200 | [diff] [blame] | 386 | if (hit) |
| 387 | break; |
| 388 | s += strlen(s) + 1; |
| 389 | } while (s < eol); |
| 390 | } else |
René Scharfe | ed40a09 | 2010-05-22 23:43:43 +0200 | [diff] [blame] | 391 | hit = memmem(line, eol - line, p->pattern, p->patternlen); |
Brian Collins | 5183bf6 | 2009-11-06 01:22:35 -0800 | [diff] [blame] | 392 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 393 | if (!hit) { |
| 394 | match->rm_so = match->rm_eo = -1; |
| 395 | return REG_NOMATCH; |
| 396 | } |
| 397 | else { |
| 398 | match->rm_so = hit - line; |
René Scharfe | ed40a09 | 2010-05-22 23:43:43 +0200 | [diff] [blame] | 399 | match->rm_eo = match->rm_so + p->patternlen; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 400 | return 0; |
| 401 | } |
| 402 | } |
| 403 | |
René Scharfe | f96e567 | 2010-05-22 23:35:07 +0200 | [diff] [blame] | 404 | static int regmatch(const regex_t *preg, char *line, char *eol, |
| 405 | regmatch_t *match, int eflags) |
| 406 | { |
| 407 | #ifdef REG_STARTEND |
| 408 | match->rm_so = 0; |
| 409 | match->rm_eo = eol - line; |
| 410 | eflags |= REG_STARTEND; |
| 411 | #endif |
| 412 | return regexec(preg, line, 1, match, eflags); |
| 413 | } |
| 414 | |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 415 | static int strip_timestamp(char *bol, char **eol_p) |
| 416 | { |
| 417 | char *eol = *eol_p; |
| 418 | int ch; |
| 419 | |
| 420 | while (bol < --eol) { |
| 421 | if (*eol != '>') |
| 422 | continue; |
| 423 | *eol_p = ++eol; |
| 424 | ch = *eol; |
| 425 | *eol = '\0'; |
| 426 | return ch; |
| 427 | } |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | static struct { |
| 432 | const char *field; |
| 433 | size_t len; |
| 434 | } header_field[] = { |
| 435 | { "author ", 7 }, |
| 436 | { "committer ", 10 }, |
| 437 | }; |
| 438 | |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 439 | 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] | 440 | enum grep_context ctx, |
| 441 | regmatch_t *pmatch, int eflags) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 442 | { |
| 443 | int hit = 0; |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 444 | int saved_ch = 0; |
René Scharfe | e701fad | 2009-05-20 23:31:53 +0200 | [diff] [blame] | 445 | const char *start = bol; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 446 | |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 447 | if ((p->token != GREP_PATTERN) && |
| 448 | ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD))) |
| 449 | return 0; |
| 450 | |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 451 | if (p->token == GREP_PATTERN_HEAD) { |
| 452 | const char *field; |
| 453 | size_t len; |
| 454 | assert(p->field < ARRAY_SIZE(header_field)); |
| 455 | field = header_field[p->field].field; |
| 456 | len = header_field[p->field].len; |
| 457 | if (strncmp(bol, field, len)) |
| 458 | return 0; |
| 459 | bol += len; |
| 460 | saved_ch = strip_timestamp(bol, &eol); |
| 461 | } |
| 462 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 463 | again: |
René Scharfe | 7921277 | 2009-03-07 13:30:27 +0100 | [diff] [blame] | 464 | if (p->fixed) |
René Scharfe | ed40a09 | 2010-05-22 23:43:43 +0200 | [diff] [blame] | 465 | hit = !fixmatch(p, bol, eol, pmatch); |
René Scharfe | 7921277 | 2009-03-07 13:30:27 +0100 | [diff] [blame] | 466 | else |
René Scharfe | f96e567 | 2010-05-22 23:35:07 +0200 | [diff] [blame] | 467 | hit = !regmatch(&p->regexp, bol, eol, pmatch, eflags); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 468 | |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 469 | if (hit && p->word_regexp) { |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 470 | if ((pmatch[0].rm_so < 0) || |
René Scharfe | 84201ea | 2009-06-03 18:19:01 +0200 | [diff] [blame] | 471 | (eol - bol) < pmatch[0].rm_so || |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 472 | (pmatch[0].rm_eo < 0) || |
| 473 | (eol - bol) < pmatch[0].rm_eo) |
| 474 | die("regexp returned nonsense"); |
| 475 | |
| 476 | /* Match beginning must be either beginning of the |
| 477 | * line, or at word boundary (i.e. the last char must |
| 478 | * not be a word char). Similarly, match end must be |
| 479 | * either end of the line, or at word boundary |
| 480 | * (i.e. the next char must not be a word char). |
| 481 | */ |
René Scharfe | fb62eb7 | 2009-01-10 00:08:40 +0100 | [diff] [blame] | 482 | if ( ((pmatch[0].rm_so == 0) || |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 483 | !word_char(bol[pmatch[0].rm_so-1])) && |
| 484 | ((pmatch[0].rm_eo == (eol-bol)) || |
| 485 | !word_char(bol[pmatch[0].rm_eo])) ) |
| 486 | ; |
| 487 | else |
| 488 | hit = 0; |
| 489 | |
René Scharfe | 84201ea | 2009-06-03 18:19:01 +0200 | [diff] [blame] | 490 | /* Words consist of at least one character. */ |
| 491 | if (pmatch->rm_so == pmatch->rm_eo) |
| 492 | hit = 0; |
| 493 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 494 | if (!hit && pmatch[0].rm_so + bol + 1 < eol) { |
| 495 | /* There could be more than one match on the |
| 496 | * line, and the first match might not be |
| 497 | * strict word match. But later ones could be! |
René Scharfe | fb62eb7 | 2009-01-10 00:08:40 +0100 | [diff] [blame] | 498 | * Forward to the next possible start, i.e. the |
| 499 | * next position following a non-word char. |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 500 | */ |
| 501 | bol = pmatch[0].rm_so + bol + 1; |
René Scharfe | fb62eb7 | 2009-01-10 00:08:40 +0100 | [diff] [blame] | 502 | while (word_char(bol[-1]) && bol < eol) |
| 503 | bol++; |
René Scharfe | dbb6a4a | 2009-05-23 13:45:26 +0200 | [diff] [blame] | 504 | eflags |= REG_NOTBOL; |
René Scharfe | fb62eb7 | 2009-01-10 00:08:40 +0100 | [diff] [blame] | 505 | if (bol < eol) |
| 506 | goto again; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 507 | } |
| 508 | } |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 509 | if (p->token == GREP_PATTERN_HEAD && saved_ch) |
| 510 | *eol = saved_ch; |
René Scharfe | e701fad | 2009-05-20 23:31:53 +0200 | [diff] [blame] | 511 | if (hit) { |
| 512 | pmatch[0].rm_so += bol - start; |
| 513 | pmatch[0].rm_eo += bol - start; |
| 514 | } |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 515 | return hit; |
| 516 | } |
| 517 | |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 518 | static int match_expr_eval(struct grep_expr *x, char *bol, char *eol, |
| 519 | enum grep_context ctx, int collect_hits) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 520 | { |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 521 | int h = 0; |
René Scharfe | 7921277 | 2009-03-07 13:30:27 +0100 | [diff] [blame] | 522 | regmatch_t match; |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 523 | |
Linus Torvalds | c922b01 | 2009-04-27 11:10:24 -0700 | [diff] [blame] | 524 | if (!x) |
| 525 | die("Not a valid grep expression"); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 526 | switch (x->node) { |
Junio C Hamano | 5aaeb73 | 2010-09-12 22:15:35 -0700 | [diff] [blame] | 527 | case GREP_NODE_TRUE: |
| 528 | h = 1; |
| 529 | break; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 530 | case GREP_NODE_ATOM: |
René Scharfe | 7921277 | 2009-03-07 13:30:27 +0100 | [diff] [blame] | 531 | 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] | 532 | break; |
| 533 | case GREP_NODE_NOT: |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 534 | h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0); |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 535 | break; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 536 | case GREP_NODE_AND: |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 537 | if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0)) |
René Scharfe | 252d560 | 2009-03-07 13:27:15 +0100 | [diff] [blame] | 538 | return 0; |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 539 | 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] | 540 | break; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 541 | case GREP_NODE_OR: |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 542 | if (!collect_hits) |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 543 | return (match_expr_eval(x->u.binary.left, |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 544 | bol, eol, ctx, 0) || |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 545 | match_expr_eval(x->u.binary.right, |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 546 | bol, eol, ctx, 0)); |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 547 | 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] | 548 | x->u.binary.left->hit |= h; |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 549 | 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] | 550 | break; |
| 551 | default: |
Alexander Potashev | d753070 | 2009-01-04 21:38:41 +0300 | [diff] [blame] | 552 | die("Unexpected node type (internal error) %d", x->node); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 553 | } |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 554 | if (collect_hits) |
| 555 | x->hit |= h; |
| 556 | return h; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 559 | 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] | 560 | enum grep_context ctx, int collect_hits) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 561 | { |
| 562 | struct grep_expr *x = opt->pattern_expression; |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 563 | return match_expr_eval(x, bol, eol, ctx, collect_hits); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 566 | 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] | 567 | enum grep_context ctx, int collect_hits) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 568 | { |
| 569 | struct grep_pat *p; |
René Scharfe | 7921277 | 2009-03-07 13:30:27 +0100 | [diff] [blame] | 570 | regmatch_t match; |
| 571 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 572 | if (opt->extended) |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 573 | return match_expr(opt, bol, eol, ctx, collect_hits); |
| 574 | |
| 575 | /* we do not call with collect_hits without being extended */ |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 576 | for (p = opt->pattern_list; p; p = p->next) { |
René Scharfe | 7921277 | 2009-03-07 13:30:27 +0100 | [diff] [blame] | 577 | if (match_one_pattern(p, bol, eol, ctx, &match, 0)) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 578 | return 1; |
| 579 | } |
| 580 | return 0; |
| 581 | } |
| 582 | |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 583 | static int match_next_pattern(struct grep_pat *p, char *bol, char *eol, |
| 584 | enum grep_context ctx, |
| 585 | regmatch_t *pmatch, int eflags) |
| 586 | { |
| 587 | regmatch_t match; |
| 588 | |
| 589 | if (!match_one_pattern(p, bol, eol, ctx, &match, eflags)) |
| 590 | return 0; |
| 591 | if (match.rm_so < 0 || match.rm_eo < 0) |
| 592 | return 0; |
| 593 | if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) { |
| 594 | if (match.rm_so > pmatch->rm_so) |
| 595 | return 1; |
| 596 | if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo) |
| 597 | return 1; |
| 598 | } |
| 599 | pmatch->rm_so = match.rm_so; |
| 600 | pmatch->rm_eo = match.rm_eo; |
| 601 | return 1; |
| 602 | } |
| 603 | |
| 604 | static int next_match(struct grep_opt *opt, char *bol, char *eol, |
| 605 | enum grep_context ctx, regmatch_t *pmatch, int eflags) |
| 606 | { |
| 607 | struct grep_pat *p; |
| 608 | int hit = 0; |
| 609 | |
| 610 | pmatch->rm_so = pmatch->rm_eo = -1; |
| 611 | if (bol < eol) { |
| 612 | for (p = opt->pattern_list; p; p = p->next) { |
| 613 | switch (p->token) { |
| 614 | case GREP_PATTERN: /* atom */ |
| 615 | case GREP_PATTERN_HEAD: |
| 616 | case GREP_PATTERN_BODY: |
| 617 | hit |= match_next_pattern(p, bol, eol, ctx, |
| 618 | pmatch, eflags); |
| 619 | break; |
| 620 | default: |
| 621 | break; |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | return hit; |
| 626 | } |
| 627 | |
| 628 | static void show_line(struct grep_opt *opt, char *bol, char *eol, |
| 629 | const char *name, unsigned lno, char sign) |
| 630 | { |
| 631 | int rest = eol - bol; |
Mark Lodato | 00588bb | 2010-03-07 11:52:47 -0500 | [diff] [blame] | 632 | char *line_color = NULL; |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 633 | |
René Scharfe | ed24e40 | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 634 | if (opt->pre_context || opt->post_context) { |
René Scharfe | 046802d | 2009-07-02 00:03:44 +0200 | [diff] [blame] | 635 | if (opt->last_shown == 0) { |
Mark Lodato | 55f638b | 2010-03-07 11:52:46 -0500 | [diff] [blame] | 636 | if (opt->show_hunk_mark) { |
| 637 | output_color(opt, "--", 2, opt->color_sep); |
| 638 | opt->output(opt, "\n", 1); |
Junio C Hamano | 07b838f | 2010-04-03 12:28:39 -0700 | [diff] [blame] | 639 | } |
Mark Lodato | 55f638b | 2010-03-07 11:52:46 -0500 | [diff] [blame] | 640 | } else if (lno > opt->last_shown + 1) { |
| 641 | output_color(opt, "--", 2, opt->color_sep); |
| 642 | opt->output(opt, "\n", 1); |
| 643 | } |
René Scharfe | 5dd06d3 | 2009-07-02 00:02:38 +0200 | [diff] [blame] | 644 | } |
| 645 | opt->last_shown = lno; |
| 646 | |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 647 | if (opt->pathname) { |
Mark Lodato | 55f638b | 2010-03-07 11:52:46 -0500 | [diff] [blame] | 648 | output_color(opt, name, strlen(name), opt->color_filename); |
| 649 | output_sep(opt, sign); |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 650 | } |
| 651 | if (opt->linenum) { |
| 652 | char buf[32]; |
| 653 | snprintf(buf, sizeof(buf), "%d", lno); |
Mark Lodato | 55f638b | 2010-03-07 11:52:46 -0500 | [diff] [blame] | 654 | output_color(opt, buf, strlen(buf), opt->color_lineno); |
| 655 | output_sep(opt, sign); |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 656 | } |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 657 | if (opt->color) { |
| 658 | regmatch_t match; |
| 659 | enum grep_context ctx = GREP_CONTEXT_BODY; |
| 660 | int ch = *eol; |
| 661 | int eflags = 0; |
| 662 | |
Mark Lodato | 00588bb | 2010-03-07 11:52:47 -0500 | [diff] [blame] | 663 | if (sign == ':') |
| 664 | line_color = opt->color_selected; |
| 665 | else if (sign == '-') |
| 666 | line_color = opt->color_context; |
| 667 | else if (sign == '=') |
| 668 | line_color = opt->color_function; |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 669 | *eol = '\0'; |
| 670 | while (next_match(opt, bol, eol, ctx, &match, eflags)) { |
René Scharfe | 1f5b9cc | 2009-06-01 23:53:05 +0200 | [diff] [blame] | 671 | if (match.rm_so == match.rm_eo) |
| 672 | break; |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 673 | |
Mark Lodato | 00588bb | 2010-03-07 11:52:47 -0500 | [diff] [blame] | 674 | output_color(opt, bol, match.rm_so, line_color); |
Mark Lodato | 55f638b | 2010-03-07 11:52:46 -0500 | [diff] [blame] | 675 | output_color(opt, bol + match.rm_so, |
| 676 | match.rm_eo - match.rm_so, |
| 677 | opt->color_match); |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 678 | bol += match.rm_eo; |
| 679 | rest -= match.rm_eo; |
| 680 | eflags = REG_NOTBOL; |
| 681 | } |
| 682 | *eol = ch; |
| 683 | } |
Mark Lodato | 00588bb | 2010-03-07 11:52:47 -0500 | [diff] [blame] | 684 | output_color(opt, bol, rest, line_color); |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 685 | opt->output(opt, "\n", 1); |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 686 | } |
| 687 | |
René Scharfe | 60ecac9 | 2009-07-02 00:07:24 +0200 | [diff] [blame] | 688 | static int match_funcname(struct grep_opt *opt, char *bol, char *eol) |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 689 | { |
René Scharfe | 60ecac9 | 2009-07-02 00:07:24 +0200 | [diff] [blame] | 690 | xdemitconf_t *xecfg = opt->priv; |
| 691 | if (xecfg && xecfg->find_func) { |
| 692 | char buf[1]; |
| 693 | return xecfg->find_func(bol, eol - bol, buf, 1, |
| 694 | xecfg->find_func_priv) >= 0; |
| 695 | } |
| 696 | |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 697 | if (bol == eol) |
| 698 | return 0; |
| 699 | if (isalpha(*bol) || *bol == '_' || *bol == '$') |
| 700 | return 1; |
| 701 | return 0; |
| 702 | } |
| 703 | |
| 704 | static void show_funcname_line(struct grep_opt *opt, const char *name, |
| 705 | char *buf, char *bol, unsigned lno) |
| 706 | { |
| 707 | while (bol > buf) { |
| 708 | char *eol = --bol; |
| 709 | |
| 710 | while (bol > buf && bol[-1] != '\n') |
| 711 | bol--; |
| 712 | lno--; |
| 713 | |
| 714 | if (lno <= opt->last_shown) |
| 715 | break; |
| 716 | |
René Scharfe | 60ecac9 | 2009-07-02 00:07:24 +0200 | [diff] [blame] | 717 | if (match_funcname(opt, bol, eol)) { |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 718 | show_line(opt, bol, eol, name, lno, '='); |
| 719 | break; |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | |
René Scharfe | 49de321 | 2009-07-02 00:05:17 +0200 | [diff] [blame] | 724 | static void show_pre_context(struct grep_opt *opt, const char *name, char *buf, |
| 725 | char *bol, unsigned lno) |
| 726 | { |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 727 | unsigned cur = lno, from = 1, funcname_lno = 0; |
| 728 | int funcname_needed = opt->funcname; |
René Scharfe | 49de321 | 2009-07-02 00:05:17 +0200 | [diff] [blame] | 729 | |
| 730 | if (opt->pre_context < lno) |
| 731 | from = lno - opt->pre_context; |
| 732 | if (from <= opt->last_shown) |
| 733 | from = opt->last_shown + 1; |
| 734 | |
| 735 | /* Rewind. */ |
| 736 | while (bol > buf && cur > from) { |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 737 | char *eol = --bol; |
| 738 | |
René Scharfe | 49de321 | 2009-07-02 00:05:17 +0200 | [diff] [blame] | 739 | while (bol > buf && bol[-1] != '\n') |
| 740 | bol--; |
| 741 | cur--; |
René Scharfe | 60ecac9 | 2009-07-02 00:07:24 +0200 | [diff] [blame] | 742 | if (funcname_needed && match_funcname(opt, bol, eol)) { |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 743 | funcname_lno = cur; |
| 744 | funcname_needed = 0; |
| 745 | } |
René Scharfe | 49de321 | 2009-07-02 00:05:17 +0200 | [diff] [blame] | 746 | } |
| 747 | |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 748 | /* We need to look even further back to find a function signature. */ |
| 749 | if (opt->funcname && funcname_needed) |
| 750 | show_funcname_line(opt, name, buf, bol, cur); |
| 751 | |
René Scharfe | 49de321 | 2009-07-02 00:05:17 +0200 | [diff] [blame] | 752 | /* Back forward. */ |
| 753 | while (cur < lno) { |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 754 | char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-'; |
René Scharfe | 49de321 | 2009-07-02 00:05:17 +0200 | [diff] [blame] | 755 | |
| 756 | while (*eol != '\n') |
| 757 | eol++; |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 758 | show_line(opt, bol, eol, name, cur, sign); |
René Scharfe | 49de321 | 2009-07-02 00:05:17 +0200 | [diff] [blame] | 759 | bol = eol + 1; |
| 760 | cur++; |
| 761 | } |
| 762 | } |
| 763 | |
Junio C Hamano | a26345b | 2010-01-10 22:39:36 -0800 | [diff] [blame] | 764 | static int should_lookahead(struct grep_opt *opt) |
| 765 | { |
| 766 | struct grep_pat *p; |
| 767 | |
| 768 | if (opt->extended) |
| 769 | return 0; /* punt for too complex stuff */ |
| 770 | if (opt->invert) |
| 771 | return 0; |
| 772 | for (p = opt->pattern_list; p; p = p->next) { |
| 773 | if (p->token != GREP_PATTERN) |
| 774 | return 0; /* punt for "header only" and stuff */ |
| 775 | } |
| 776 | return 1; |
| 777 | } |
| 778 | |
| 779 | static int look_ahead(struct grep_opt *opt, |
| 780 | unsigned long *left_p, |
| 781 | unsigned *lno_p, |
| 782 | char **bol_p) |
| 783 | { |
| 784 | unsigned lno = *lno_p; |
| 785 | char *bol = *bol_p; |
| 786 | struct grep_pat *p; |
| 787 | char *sp, *last_bol; |
| 788 | regoff_t earliest = -1; |
| 789 | |
| 790 | for (p = opt->pattern_list; p; p = p->next) { |
| 791 | int hit; |
| 792 | regmatch_t m; |
| 793 | |
René Scharfe | ed40a09 | 2010-05-22 23:43:43 +0200 | [diff] [blame] | 794 | if (p->fixed) |
| 795 | hit = !fixmatch(p, bol, bol + *left_p, &m); |
| 796 | else |
René Scharfe | f96e567 | 2010-05-22 23:35:07 +0200 | [diff] [blame] | 797 | hit = !regmatch(&p->regexp, bol, bol + *left_p, &m, 0); |
Junio C Hamano | a26345b | 2010-01-10 22:39:36 -0800 | [diff] [blame] | 798 | if (!hit || m.rm_so < 0 || m.rm_eo < 0) |
| 799 | continue; |
| 800 | if (earliest < 0 || m.rm_so < earliest) |
| 801 | earliest = m.rm_so; |
| 802 | } |
| 803 | |
| 804 | if (earliest < 0) { |
| 805 | *bol_p = bol + *left_p; |
| 806 | *left_p = 0; |
| 807 | return 1; |
| 808 | } |
| 809 | for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--) |
| 810 | ; /* find the beginning of the line */ |
| 811 | last_bol = sp; |
| 812 | |
| 813 | for (sp = bol; sp < last_bol; sp++) { |
| 814 | if (*sp == '\n') |
| 815 | lno++; |
| 816 | } |
| 817 | *left_p -= last_bol - bol; |
| 818 | *bol_p = last_bol; |
| 819 | *lno_p = lno; |
| 820 | return 0; |
| 821 | } |
| 822 | |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 823 | int grep_threads_ok(const struct grep_opt *opt) |
| 824 | { |
| 825 | /* If this condition is true, then we may use the attribute |
| 826 | * machinery in grep_buffer_1. The attribute code is not |
| 827 | * thread safe, so we disable the use of threads. |
| 828 | */ |
| 829 | if (opt->funcname && !opt->unmatch_name_only && !opt->status_only && |
| 830 | !opt->name_only) |
| 831 | return 0; |
| 832 | |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 833 | return 1; |
| 834 | } |
| 835 | |
| 836 | static void std_output(struct grep_opt *opt, const void *buf, size_t size) |
| 837 | { |
| 838 | fwrite(buf, size, 1, stdout); |
| 839 | } |
| 840 | |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 841 | static int grep_buffer_1(struct grep_opt *opt, const char *name, |
| 842 | char *buf, unsigned long size, int collect_hits) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 843 | { |
| 844 | char *bol = buf; |
| 845 | unsigned long left = size; |
| 846 | unsigned lno = 1; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 847 | unsigned last_hit = 0; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 848 | int binary_match_only = 0; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 849 | unsigned count = 0; |
Junio C Hamano | a26345b | 2010-01-10 22:39:36 -0800 | [diff] [blame] | 850 | int try_lookahead = 0; |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 851 | enum grep_context ctx = GREP_CONTEXT_HEAD; |
René Scharfe | 60ecac9 | 2009-07-02 00:07:24 +0200 | [diff] [blame] | 852 | xdemitconf_t xecfg; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 853 | |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 854 | if (!opt->output) |
| 855 | opt->output = std_output; |
| 856 | |
René Scharfe | 431d6e7 | 2010-03-15 17:21:10 +0100 | [diff] [blame] | 857 | if (opt->last_shown && (opt->pre_context || opt->post_context) && |
| 858 | opt->output == std_output) |
| 859 | opt->show_hunk_mark = 1; |
| 860 | opt->last_shown = 0; |
| 861 | |
René Scharfe | 64fcec7 | 2010-05-22 23:28:17 +0200 | [diff] [blame] | 862 | switch (opt->binary) { |
| 863 | case GREP_BINARY_DEFAULT: |
| 864 | if (buffer_is_binary(buf, size)) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 865 | binary_match_only = 1; |
René Scharfe | 64fcec7 | 2010-05-22 23:28:17 +0200 | [diff] [blame] | 866 | break; |
| 867 | case GREP_BINARY_NOMATCH: |
| 868 | if (buffer_is_binary(buf, size)) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 869 | return 0; /* Assume unmatch */ |
René Scharfe | 64fcec7 | 2010-05-22 23:28:17 +0200 | [diff] [blame] | 870 | break; |
| 871 | case GREP_BINARY_TEXT: |
| 872 | break; |
| 873 | default: |
| 874 | die("bug: unknown binary handling mode"); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 875 | } |
| 876 | |
René Scharfe | 60ecac9 | 2009-07-02 00:07:24 +0200 | [diff] [blame] | 877 | memset(&xecfg, 0, sizeof(xecfg)); |
| 878 | if (opt->funcname && !opt->unmatch_name_only && !opt->status_only && |
| 879 | !opt->name_only && !binary_match_only && !collect_hits) { |
| 880 | struct userdiff_driver *drv = userdiff_find_by_path(name); |
| 881 | if (drv && drv->funcname.pattern) { |
| 882 | const struct userdiff_funcname *pe = &drv->funcname; |
| 883 | xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags); |
| 884 | opt->priv = &xecfg; |
| 885 | } |
| 886 | } |
Junio C Hamano | a26345b | 2010-01-10 22:39:36 -0800 | [diff] [blame] | 887 | try_lookahead = should_lookahead(opt); |
René Scharfe | 60ecac9 | 2009-07-02 00:07:24 +0200 | [diff] [blame] | 888 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 889 | while (left) { |
| 890 | char *eol, ch; |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 891 | int hit; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 892 | |
Junio C Hamano | a26345b | 2010-01-10 22:39:36 -0800 | [diff] [blame] | 893 | /* |
| 894 | * look_ahead() skips quicly to the line that possibly |
| 895 | * has the next hit; don't call it if we need to do |
| 896 | * something more than just skipping the current line |
| 897 | * in response to an unmatch for the current line. E.g. |
| 898 | * inside a post-context window, we will show the current |
| 899 | * line as a context around the previous hit when it |
| 900 | * doesn't hit. |
| 901 | */ |
| 902 | if (try_lookahead |
| 903 | && !(last_hit |
| 904 | && lno <= last_hit + opt->post_context) |
| 905 | && look_ahead(opt, &left, &lno, &bol)) |
| 906 | break; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 907 | eol = end_of_line(bol, &left); |
| 908 | ch = *eol; |
| 909 | *eol = 0; |
| 910 | |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 911 | if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol)) |
| 912 | ctx = GREP_CONTEXT_BODY; |
| 913 | |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 914 | hit = match_line(opt, bol, eol, ctx, collect_hits); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 915 | *eol = ch; |
| 916 | |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 917 | if (collect_hits) |
| 918 | goto next_line; |
| 919 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 920 | /* "grep -v -e foo -e bla" should list lines |
| 921 | * that do not have either, so inversion should |
| 922 | * be done outside. |
| 923 | */ |
| 924 | if (opt->invert) |
| 925 | hit = !hit; |
| 926 | if (opt->unmatch_name_only) { |
| 927 | if (hit) |
| 928 | return 0; |
| 929 | goto next_line; |
| 930 | } |
| 931 | if (hit) { |
| 932 | count++; |
| 933 | if (opt->status_only) |
| 934 | return 1; |
René Scharfe | 321ffcc | 2010-05-22 23:30:48 +0200 | [diff] [blame] | 935 | if (opt->name_only) { |
| 936 | show_name(opt, name); |
| 937 | return 1; |
| 938 | } |
René Scharfe | c30c10c | 2010-05-22 23:29:35 +0200 | [diff] [blame] | 939 | if (opt->count) |
| 940 | goto next_line; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 941 | if (binary_match_only) { |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 942 | opt->output(opt, "Binary file ", 12); |
Mark Lodato | 55f638b | 2010-03-07 11:52:46 -0500 | [diff] [blame] | 943 | output_color(opt, name, strlen(name), |
| 944 | opt->color_filename); |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 945 | opt->output(opt, " matches\n", 9); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 946 | return 1; |
| 947 | } |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 948 | /* Hit at this line. If we haven't shown the |
| 949 | * pre-context lines, we would need to show them. |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 950 | */ |
René Scharfe | 49de321 | 2009-07-02 00:05:17 +0200 | [diff] [blame] | 951 | if (opt->pre_context) |
| 952 | show_pre_context(opt, name, buf, bol, lno); |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 953 | else if (opt->funcname) |
| 954 | show_funcname_line(opt, name, buf, bol, lno); |
René Scharfe | c30c10c | 2010-05-22 23:29:35 +0200 | [diff] [blame] | 955 | show_line(opt, bol, eol, name, lno, ':'); |
René Scharfe | 5dd06d3 | 2009-07-02 00:02:38 +0200 | [diff] [blame] | 956 | last_hit = lno; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 957 | } |
| 958 | else if (last_hit && |
| 959 | lno <= last_hit + opt->post_context) { |
| 960 | /* If the last hit is within the post context, |
| 961 | * we need to show this line. |
| 962 | */ |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 963 | show_line(opt, bol, eol, name, lno, '-'); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 964 | } |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 965 | |
| 966 | next_line: |
| 967 | bol = eol + 1; |
| 968 | if (!left) |
| 969 | break; |
| 970 | left--; |
| 971 | lno++; |
| 972 | } |
| 973 | |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 974 | if (collect_hits) |
| 975 | return 0; |
Junio C Hamano | b48fb5b | 2006-09-27 16:27:10 -0700 | [diff] [blame] | 976 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 977 | if (opt->status_only) |
| 978 | return 0; |
| 979 | if (opt->unmatch_name_only) { |
| 980 | /* We did not see any hit, so we want to show this */ |
Raphael Zimmerer | 83caecc | 2008-10-01 18:11:15 +0200 | [diff] [blame] | 981 | show_name(opt, name); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 982 | return 1; |
| 983 | } |
| 984 | |
René Scharfe | 60ecac9 | 2009-07-02 00:07:24 +0200 | [diff] [blame] | 985 | xdiff_clear_find_func(&xecfg); |
| 986 | opt->priv = NULL; |
| 987 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 988 | /* NEEDSWORK: |
| 989 | * The real "grep -c foo *.c" gives many "bar.c:0" lines, |
| 990 | * which feels mostly useless but sometimes useful. Maybe |
| 991 | * make it another option? For now suppress them. |
| 992 | */ |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 993 | if (opt->count && count) { |
| 994 | char buf[32]; |
Mark Lodato | 55f638b | 2010-03-07 11:52:46 -0500 | [diff] [blame] | 995 | output_color(opt, name, strlen(name), opt->color_filename); |
| 996 | output_sep(opt, ':'); |
| 997 | snprintf(buf, sizeof(buf), "%u\n", count); |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 998 | opt->output(opt, buf, strlen(buf)); |
René Scharfe | c30c10c | 2010-05-22 23:29:35 +0200 | [diff] [blame] | 999 | return 1; |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 1000 | } |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1001 | return !!last_hit; |
| 1002 | } |
| 1003 | |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 1004 | static void clr_hit_marker(struct grep_expr *x) |
| 1005 | { |
| 1006 | /* All-hit markers are meaningful only at the very top level |
| 1007 | * OR node. |
| 1008 | */ |
| 1009 | while (1) { |
| 1010 | x->hit = 0; |
| 1011 | if (x->node != GREP_NODE_OR) |
| 1012 | return; |
| 1013 | x->u.binary.left->hit = 0; |
| 1014 | x = x->u.binary.right; |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | static int chk_hit_marker(struct grep_expr *x) |
| 1019 | { |
| 1020 | /* Top level nodes have hit markers. See if they all are hits */ |
| 1021 | while (1) { |
| 1022 | if (x->node != GREP_NODE_OR) |
| 1023 | return x->hit; |
| 1024 | if (!x->u.binary.left->hit) |
| 1025 | return 0; |
| 1026 | x = x->u.binary.right; |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size) |
| 1031 | { |
| 1032 | /* |
| 1033 | * we do not have to do the two-pass grep when we do not check |
| 1034 | * buffer-wide "all-match". |
| 1035 | */ |
| 1036 | if (!opt->all_match) |
| 1037 | return grep_buffer_1(opt, name, buf, size, 0); |
| 1038 | |
| 1039 | /* Otherwise the toplevel "or" terms hit a bit differently. |
| 1040 | * We first clear hit markers from them. |
| 1041 | */ |
| 1042 | clr_hit_marker(opt->pattern_expression); |
| 1043 | grep_buffer_1(opt, name, buf, size, 1); |
| 1044 | |
| 1045 | if (!chk_hit_marker(opt->pattern_expression)) |
| 1046 | return 0; |
| 1047 | |
| 1048 | return grep_buffer_1(opt, name, buf, size, 0); |
| 1049 | } |