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" |
Johannes Schindelin | 6bfce93 | 2007-06-05 03:36:11 +0100 | [diff] [blame] | 3 | #include "xdiff-interface.h" |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 4 | |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 5 | void append_header_grep_pattern(struct grep_opt *opt, enum grep_header_field field, const char *pat) |
| 6 | { |
| 7 | struct grep_pat *p = xcalloc(1, sizeof(*p)); |
| 8 | p->pattern = pat; |
| 9 | p->origin = "header"; |
| 10 | p->no = 0; |
| 11 | p->token = GREP_PATTERN_HEAD; |
| 12 | p->field = field; |
| 13 | *opt->pattern_tail = p; |
| 14 | opt->pattern_tail = &p->next; |
| 15 | p->next = NULL; |
| 16 | } |
| 17 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 18 | void append_grep_pattern(struct grep_opt *opt, const char *pat, |
| 19 | const char *origin, int no, enum grep_pat_token t) |
| 20 | { |
| 21 | struct grep_pat *p = xcalloc(1, sizeof(*p)); |
| 22 | p->pattern = pat; |
| 23 | p->origin = origin; |
| 24 | p->no = no; |
| 25 | p->token = t; |
| 26 | *opt->pattern_tail = p; |
| 27 | opt->pattern_tail = &p->next; |
| 28 | p->next = NULL; |
| 29 | } |
| 30 | |
René Scharfe | c822255 | 2009-01-10 00:18:34 +0100 | [diff] [blame] | 31 | static int is_fixed(const char *s) |
| 32 | { |
René Scharfe | f9b7cce | 2009-01-17 16:50:37 +0100 | [diff] [blame] | 33 | while (*s && !is_regex_special(*s)) |
René Scharfe | c822255 | 2009-01-10 00:18:34 +0100 | [diff] [blame] | 34 | s++; |
| 35 | return !*s; |
| 36 | } |
| 37 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 38 | static void compile_regexp(struct grep_pat *p, struct grep_opt *opt) |
| 39 | { |
René Scharfe | c822255 | 2009-01-10 00:18:34 +0100 | [diff] [blame] | 40 | int err; |
| 41 | |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 42 | p->word_regexp = opt->word_regexp; |
| 43 | |
René Scharfe | c822255 | 2009-01-10 00:18:34 +0100 | [diff] [blame] | 44 | if (opt->fixed || is_fixed(p->pattern)) |
| 45 | p->fixed = 1; |
| 46 | if (opt->regflags & REG_ICASE) |
| 47 | p->fixed = 0; |
| 48 | if (p->fixed) |
| 49 | return; |
| 50 | |
| 51 | err = regcomp(&p->regexp, p->pattern, opt->regflags); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 52 | if (err) { |
| 53 | char errbuf[1024]; |
| 54 | char where[1024]; |
| 55 | if (p->no) |
| 56 | sprintf(where, "In '%s' at %d, ", |
| 57 | p->origin, p->no); |
| 58 | else if (p->origin) |
| 59 | sprintf(where, "%s, ", p->origin); |
| 60 | else |
| 61 | where[0] = 0; |
| 62 | regerror(err, &p->regexp, errbuf, 1024); |
| 63 | regfree(&p->regexp); |
| 64 | die("%s'%s': %s", where, p->pattern, errbuf); |
| 65 | } |
| 66 | } |
| 67 | |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 68 | static struct grep_expr *compile_pattern_or(struct grep_pat **); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 69 | static struct grep_expr *compile_pattern_atom(struct grep_pat **list) |
| 70 | { |
| 71 | struct grep_pat *p; |
| 72 | struct grep_expr *x; |
| 73 | |
| 74 | p = *list; |
Linus Torvalds | c922b01 | 2009-04-27 11:10:24 -0700 | [diff] [blame] | 75 | if (!p) |
| 76 | return NULL; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 77 | switch (p->token) { |
| 78 | case GREP_PATTERN: /* atom */ |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 79 | case GREP_PATTERN_HEAD: |
| 80 | case GREP_PATTERN_BODY: |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 81 | x = xcalloc(1, sizeof (struct grep_expr)); |
| 82 | x->node = GREP_NODE_ATOM; |
| 83 | x->u.atom = p; |
| 84 | *list = p->next; |
| 85 | return x; |
| 86 | case GREP_OPEN_PAREN: |
| 87 | *list = p->next; |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 88 | x = compile_pattern_or(list); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 89 | if (!*list || (*list)->token != GREP_CLOSE_PAREN) |
| 90 | die("unmatched parenthesis"); |
| 91 | *list = (*list)->next; |
| 92 | return x; |
| 93 | default: |
| 94 | return NULL; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | static struct grep_expr *compile_pattern_not(struct grep_pat **list) |
| 99 | { |
| 100 | struct grep_pat *p; |
| 101 | struct grep_expr *x; |
| 102 | |
| 103 | p = *list; |
Linus Torvalds | c922b01 | 2009-04-27 11:10:24 -0700 | [diff] [blame] | 104 | if (!p) |
| 105 | return NULL; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 106 | switch (p->token) { |
| 107 | case GREP_NOT: |
| 108 | if (!p->next) |
| 109 | die("--not not followed by pattern expression"); |
| 110 | *list = p->next; |
| 111 | x = xcalloc(1, sizeof (struct grep_expr)); |
| 112 | x->node = GREP_NODE_NOT; |
| 113 | x->u.unary = compile_pattern_not(list); |
| 114 | if (!x->u.unary) |
| 115 | die("--not followed by non pattern expression"); |
| 116 | return x; |
| 117 | default: |
| 118 | return compile_pattern_atom(list); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | static struct grep_expr *compile_pattern_and(struct grep_pat **list) |
| 123 | { |
| 124 | struct grep_pat *p; |
| 125 | struct grep_expr *x, *y, *z; |
| 126 | |
| 127 | x = compile_pattern_not(list); |
| 128 | p = *list; |
| 129 | if (p && p->token == GREP_AND) { |
| 130 | if (!p->next) |
| 131 | die("--and not followed by pattern expression"); |
| 132 | *list = p->next; |
| 133 | y = compile_pattern_and(list); |
| 134 | if (!y) |
| 135 | die("--and not followed by pattern expression"); |
| 136 | z = xcalloc(1, sizeof (struct grep_expr)); |
| 137 | z->node = GREP_NODE_AND; |
| 138 | z->u.binary.left = x; |
| 139 | z->u.binary.right = y; |
| 140 | return z; |
| 141 | } |
| 142 | return x; |
| 143 | } |
| 144 | |
| 145 | static struct grep_expr *compile_pattern_or(struct grep_pat **list) |
| 146 | { |
| 147 | struct grep_pat *p; |
| 148 | struct grep_expr *x, *y, *z; |
| 149 | |
| 150 | x = compile_pattern_and(list); |
| 151 | p = *list; |
| 152 | if (x && p && p->token != GREP_CLOSE_PAREN) { |
| 153 | y = compile_pattern_or(list); |
| 154 | if (!y) |
| 155 | die("not a pattern expression %s", p->pattern); |
| 156 | z = xcalloc(1, sizeof (struct grep_expr)); |
| 157 | z->node = GREP_NODE_OR; |
| 158 | z->u.binary.left = x; |
| 159 | z->u.binary.right = y; |
| 160 | return z; |
| 161 | } |
| 162 | return x; |
| 163 | } |
| 164 | |
| 165 | static struct grep_expr *compile_pattern_expr(struct grep_pat **list) |
| 166 | { |
| 167 | return compile_pattern_or(list); |
| 168 | } |
| 169 | |
| 170 | void compile_grep_patterns(struct grep_opt *opt) |
| 171 | { |
| 172 | struct grep_pat *p; |
| 173 | |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 174 | if (opt->all_match) |
| 175 | opt->extended = 1; |
| 176 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 177 | for (p = opt->pattern_list; p; p = p->next) { |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 178 | switch (p->token) { |
| 179 | case GREP_PATTERN: /* atom */ |
| 180 | case GREP_PATTERN_HEAD: |
| 181 | case GREP_PATTERN_BODY: |
René Scharfe | c822255 | 2009-01-10 00:18:34 +0100 | [diff] [blame] | 182 | compile_regexp(p, opt); |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 183 | break; |
| 184 | default: |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 185 | opt->extended = 1; |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 186 | break; |
| 187 | } |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | if (!opt->extended) |
| 191 | return; |
| 192 | |
| 193 | /* Then bundle them up in an expression. |
| 194 | * A classic recursive descent parser would do. |
| 195 | */ |
| 196 | p = opt->pattern_list; |
Michele Ballabio | ba150a3 | 2009-03-18 21:53:27 +0100 | [diff] [blame] | 197 | if (p) |
| 198 | opt->pattern_expression = compile_pattern_expr(&p); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 199 | if (p) |
| 200 | die("incomplete pattern expression: %s", p->pattern); |
| 201 | } |
| 202 | |
Junio C Hamano | b48fb5b | 2006-09-27 16:27:10 -0700 | [diff] [blame] | 203 | static void free_pattern_expr(struct grep_expr *x) |
| 204 | { |
| 205 | switch (x->node) { |
| 206 | case GREP_NODE_ATOM: |
| 207 | break; |
| 208 | case GREP_NODE_NOT: |
| 209 | free_pattern_expr(x->u.unary); |
| 210 | break; |
| 211 | case GREP_NODE_AND: |
| 212 | case GREP_NODE_OR: |
| 213 | free_pattern_expr(x->u.binary.left); |
| 214 | free_pattern_expr(x->u.binary.right); |
| 215 | break; |
| 216 | } |
| 217 | free(x); |
| 218 | } |
| 219 | |
| 220 | void free_grep_patterns(struct grep_opt *opt) |
| 221 | { |
| 222 | struct grep_pat *p, *n; |
| 223 | |
| 224 | for (p = opt->pattern_list; p; p = n) { |
| 225 | n = p->next; |
| 226 | switch (p->token) { |
| 227 | case GREP_PATTERN: /* atom */ |
| 228 | case GREP_PATTERN_HEAD: |
| 229 | case GREP_PATTERN_BODY: |
| 230 | regfree(&p->regexp); |
| 231 | break; |
| 232 | default: |
| 233 | break; |
| 234 | } |
| 235 | free(p); |
| 236 | } |
| 237 | |
| 238 | if (!opt->extended) |
| 239 | return; |
| 240 | free_pattern_expr(opt->pattern_expression); |
| 241 | } |
| 242 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 243 | static char *end_of_line(char *cp, unsigned long *left) |
| 244 | { |
| 245 | unsigned long l = *left; |
| 246 | while (l && *cp != '\n') { |
| 247 | l--; |
| 248 | cp++; |
| 249 | } |
| 250 | *left = l; |
| 251 | return cp; |
| 252 | } |
| 253 | |
| 254 | static int word_char(char ch) |
| 255 | { |
| 256 | return isalnum(ch) || ch == '_'; |
| 257 | } |
| 258 | |
Raphael Zimmerer | 83caecc | 2008-10-01 18:11:15 +0200 | [diff] [blame] | 259 | static void show_name(struct grep_opt *opt, const char *name) |
| 260 | { |
| 261 | printf("%s%c", name, opt->null_following_name ? '\0' : '\n'); |
| 262 | } |
| 263 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 264 | static int fixmatch(const char *pattern, char *line, regmatch_t *match) |
| 265 | { |
| 266 | char *hit = strstr(line, pattern); |
| 267 | if (!hit) { |
| 268 | match->rm_so = match->rm_eo = -1; |
| 269 | return REG_NOMATCH; |
| 270 | } |
| 271 | else { |
| 272 | match->rm_so = hit - line; |
| 273 | match->rm_eo = match->rm_so + strlen(pattern); |
| 274 | return 0; |
| 275 | } |
| 276 | } |
| 277 | |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 278 | static int strip_timestamp(char *bol, char **eol_p) |
| 279 | { |
| 280 | char *eol = *eol_p; |
| 281 | int ch; |
| 282 | |
| 283 | while (bol < --eol) { |
| 284 | if (*eol != '>') |
| 285 | continue; |
| 286 | *eol_p = ++eol; |
| 287 | ch = *eol; |
| 288 | *eol = '\0'; |
| 289 | return ch; |
| 290 | } |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static struct { |
| 295 | const char *field; |
| 296 | size_t len; |
| 297 | } header_field[] = { |
| 298 | { "author ", 7 }, |
| 299 | { "committer ", 10 }, |
| 300 | }; |
| 301 | |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 302 | 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] | 303 | enum grep_context ctx, |
| 304 | regmatch_t *pmatch, int eflags) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 305 | { |
| 306 | int hit = 0; |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 307 | int saved_ch = 0; |
René Scharfe | e701fad | 2009-05-20 23:31:53 +0200 | [diff] [blame] | 308 | const char *start = bol; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 309 | |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 310 | if ((p->token != GREP_PATTERN) && |
| 311 | ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD))) |
| 312 | return 0; |
| 313 | |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 314 | if (p->token == GREP_PATTERN_HEAD) { |
| 315 | const char *field; |
| 316 | size_t len; |
| 317 | assert(p->field < ARRAY_SIZE(header_field)); |
| 318 | field = header_field[p->field].field; |
| 319 | len = header_field[p->field].len; |
| 320 | if (strncmp(bol, field, len)) |
| 321 | return 0; |
| 322 | bol += len; |
| 323 | saved_ch = strip_timestamp(bol, &eol); |
| 324 | } |
| 325 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 326 | again: |
René Scharfe | 7921277 | 2009-03-07 13:30:27 +0100 | [diff] [blame] | 327 | if (p->fixed) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 328 | hit = !fixmatch(p->pattern, bol, pmatch); |
René Scharfe | 7921277 | 2009-03-07 13:30:27 +0100 | [diff] [blame] | 329 | else |
| 330 | hit = !regexec(&p->regexp, bol, 1, pmatch, eflags); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 331 | |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 332 | if (hit && p->word_regexp) { |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 333 | if ((pmatch[0].rm_so < 0) || |
| 334 | (eol - bol) <= pmatch[0].rm_so || |
| 335 | (pmatch[0].rm_eo < 0) || |
| 336 | (eol - bol) < pmatch[0].rm_eo) |
| 337 | die("regexp returned nonsense"); |
| 338 | |
| 339 | /* Match beginning must be either beginning of the |
| 340 | * line, or at word boundary (i.e. the last char must |
| 341 | * not be a word char). Similarly, match end must be |
| 342 | * either end of the line, or at word boundary |
| 343 | * (i.e. the next char must not be a word char). |
| 344 | */ |
René Scharfe | fb62eb7 | 2009-01-10 00:08:40 +0100 | [diff] [blame] | 345 | if ( ((pmatch[0].rm_so == 0) || |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 346 | !word_char(bol[pmatch[0].rm_so-1])) && |
| 347 | ((pmatch[0].rm_eo == (eol-bol)) || |
| 348 | !word_char(bol[pmatch[0].rm_eo])) ) |
| 349 | ; |
| 350 | else |
| 351 | hit = 0; |
| 352 | |
| 353 | if (!hit && pmatch[0].rm_so + bol + 1 < eol) { |
| 354 | /* There could be more than one match on the |
| 355 | * line, and the first match might not be |
| 356 | * strict word match. But later ones could be! |
René Scharfe | fb62eb7 | 2009-01-10 00:08:40 +0100 | [diff] [blame] | 357 | * Forward to the next possible start, i.e. the |
| 358 | * next position following a non-word char. |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 359 | */ |
| 360 | bol = pmatch[0].rm_so + bol + 1; |
René Scharfe | fb62eb7 | 2009-01-10 00:08:40 +0100 | [diff] [blame] | 361 | while (word_char(bol[-1]) && bol < eol) |
| 362 | bol++; |
René Scharfe | dbb6a4a | 2009-05-23 13:45:26 +0200 | [diff] [blame] | 363 | eflags |= REG_NOTBOL; |
René Scharfe | fb62eb7 | 2009-01-10 00:08:40 +0100 | [diff] [blame] | 364 | if (bol < eol) |
| 365 | goto again; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 366 | } |
| 367 | } |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 368 | if (p->token == GREP_PATTERN_HEAD && saved_ch) |
| 369 | *eol = saved_ch; |
René Scharfe | e701fad | 2009-05-20 23:31:53 +0200 | [diff] [blame] | 370 | if (hit) { |
| 371 | pmatch[0].rm_so += bol - start; |
| 372 | pmatch[0].rm_eo += bol - start; |
| 373 | } |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 374 | return hit; |
| 375 | } |
| 376 | |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 377 | static int match_expr_eval(struct grep_expr *x, char *bol, char *eol, |
| 378 | enum grep_context ctx, int collect_hits) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 379 | { |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 380 | int h = 0; |
René Scharfe | 7921277 | 2009-03-07 13:30:27 +0100 | [diff] [blame] | 381 | regmatch_t match; |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 382 | |
Linus Torvalds | c922b01 | 2009-04-27 11:10:24 -0700 | [diff] [blame] | 383 | if (!x) |
| 384 | die("Not a valid grep expression"); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 385 | switch (x->node) { |
| 386 | case GREP_NODE_ATOM: |
René Scharfe | 7921277 | 2009-03-07 13:30:27 +0100 | [diff] [blame] | 387 | 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] | 388 | break; |
| 389 | case GREP_NODE_NOT: |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 390 | h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0); |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 391 | break; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 392 | case GREP_NODE_AND: |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 393 | if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0)) |
René Scharfe | 252d560 | 2009-03-07 13:27:15 +0100 | [diff] [blame] | 394 | return 0; |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 395 | 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] | 396 | break; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 397 | case GREP_NODE_OR: |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 398 | if (!collect_hits) |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 399 | return (match_expr_eval(x->u.binary.left, |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 400 | bol, eol, ctx, 0) || |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 401 | match_expr_eval(x->u.binary.right, |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 402 | bol, eol, ctx, 0)); |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 403 | 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] | 404 | x->u.binary.left->hit |= h; |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 405 | 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] | 406 | break; |
| 407 | default: |
Alexander Potashev | d753070 | 2009-01-04 21:38:41 +0300 | [diff] [blame] | 408 | die("Unexpected node type (internal error) %d", x->node); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 409 | } |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 410 | if (collect_hits) |
| 411 | x->hit |= h; |
| 412 | return h; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 415 | 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] | 416 | enum grep_context ctx, int collect_hits) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 417 | { |
| 418 | struct grep_expr *x = opt->pattern_expression; |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 419 | return match_expr_eval(x, bol, eol, ctx, collect_hits); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 422 | 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] | 423 | enum grep_context ctx, int collect_hits) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 424 | { |
| 425 | struct grep_pat *p; |
René Scharfe | 7921277 | 2009-03-07 13:30:27 +0100 | [diff] [blame] | 426 | regmatch_t match; |
| 427 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 428 | if (opt->extended) |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 429 | return match_expr(opt, bol, eol, ctx, collect_hits); |
| 430 | |
| 431 | /* we do not call with collect_hits without being extended */ |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 432 | for (p = opt->pattern_list; p; p = p->next) { |
René Scharfe | 7921277 | 2009-03-07 13:30:27 +0100 | [diff] [blame] | 433 | if (match_one_pattern(p, bol, eol, ctx, &match, 0)) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 434 | return 1; |
| 435 | } |
| 436 | return 0; |
| 437 | } |
| 438 | |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 439 | static int match_next_pattern(struct grep_pat *p, char *bol, char *eol, |
| 440 | enum grep_context ctx, |
| 441 | regmatch_t *pmatch, int eflags) |
| 442 | { |
| 443 | regmatch_t match; |
| 444 | |
| 445 | if (!match_one_pattern(p, bol, eol, ctx, &match, eflags)) |
| 446 | return 0; |
| 447 | if (match.rm_so < 0 || match.rm_eo < 0) |
| 448 | return 0; |
| 449 | if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) { |
| 450 | if (match.rm_so > pmatch->rm_so) |
| 451 | return 1; |
| 452 | if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo) |
| 453 | return 1; |
| 454 | } |
| 455 | pmatch->rm_so = match.rm_so; |
| 456 | pmatch->rm_eo = match.rm_eo; |
| 457 | return 1; |
| 458 | } |
| 459 | |
| 460 | static int next_match(struct grep_opt *opt, char *bol, char *eol, |
| 461 | enum grep_context ctx, regmatch_t *pmatch, int eflags) |
| 462 | { |
| 463 | struct grep_pat *p; |
| 464 | int hit = 0; |
| 465 | |
| 466 | pmatch->rm_so = pmatch->rm_eo = -1; |
| 467 | if (bol < eol) { |
| 468 | for (p = opt->pattern_list; p; p = p->next) { |
| 469 | switch (p->token) { |
| 470 | case GREP_PATTERN: /* atom */ |
| 471 | case GREP_PATTERN_HEAD: |
| 472 | case GREP_PATTERN_BODY: |
| 473 | hit |= match_next_pattern(p, bol, eol, ctx, |
| 474 | pmatch, eflags); |
| 475 | break; |
| 476 | default: |
| 477 | break; |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | return hit; |
| 482 | } |
| 483 | |
| 484 | static void show_line(struct grep_opt *opt, char *bol, char *eol, |
| 485 | const char *name, unsigned lno, char sign) |
| 486 | { |
| 487 | int rest = eol - bol; |
| 488 | |
| 489 | if (opt->null_following_name) |
| 490 | sign = '\0'; |
| 491 | if (opt->pathname) |
| 492 | printf("%s%c", name, sign); |
| 493 | if (opt->linenum) |
| 494 | printf("%d%c", lno, sign); |
| 495 | if (opt->color) { |
| 496 | regmatch_t match; |
| 497 | enum grep_context ctx = GREP_CONTEXT_BODY; |
| 498 | int ch = *eol; |
| 499 | int eflags = 0; |
| 500 | |
| 501 | *eol = '\0'; |
| 502 | while (next_match(opt, bol, eol, ctx, &match, eflags)) { |
| 503 | printf("%.*s%s%.*s%s", |
Junio C Hamano | 747a322 | 2009-03-08 18:22:44 -0700 | [diff] [blame] | 504 | (int)match.rm_so, bol, |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 505 | opt->color_match, |
Junio C Hamano | 747a322 | 2009-03-08 18:22:44 -0700 | [diff] [blame] | 506 | (int)(match.rm_eo - match.rm_so), bol + match.rm_so, |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 507 | GIT_COLOR_RESET); |
| 508 | bol += match.rm_eo; |
| 509 | rest -= match.rm_eo; |
| 510 | eflags = REG_NOTBOL; |
| 511 | } |
| 512 | *eol = ch; |
| 513 | } |
| 514 | printf("%.*s\n", rest, bol); |
| 515 | } |
| 516 | |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 517 | static int grep_buffer_1(struct grep_opt *opt, const char *name, |
| 518 | char *buf, unsigned long size, int collect_hits) |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 519 | { |
| 520 | char *bol = buf; |
| 521 | unsigned long left = size; |
| 522 | unsigned lno = 1; |
| 523 | struct pre_context_line { |
| 524 | char *bol; |
| 525 | char *eol; |
| 526 | } *prev = NULL, *pcl; |
| 527 | unsigned last_hit = 0; |
| 528 | unsigned last_shown = 0; |
| 529 | int binary_match_only = 0; |
| 530 | const char *hunk_mark = ""; |
| 531 | unsigned count = 0; |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 532 | enum grep_context ctx = GREP_CONTEXT_HEAD; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 533 | |
| 534 | if (buffer_is_binary(buf, size)) { |
| 535 | switch (opt->binary) { |
| 536 | case GREP_BINARY_DEFAULT: |
| 537 | binary_match_only = 1; |
| 538 | break; |
| 539 | case GREP_BINARY_NOMATCH: |
| 540 | return 0; /* Assume unmatch */ |
| 541 | break; |
| 542 | default: |
| 543 | break; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | if (opt->pre_context) |
| 548 | prev = xcalloc(opt->pre_context, sizeof(*prev)); |
| 549 | if (opt->pre_context || opt->post_context) |
| 550 | hunk_mark = "--\n"; |
| 551 | |
| 552 | while (left) { |
| 553 | char *eol, ch; |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 554 | int hit; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 555 | |
| 556 | eol = end_of_line(bol, &left); |
| 557 | ch = *eol; |
| 558 | *eol = 0; |
| 559 | |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 560 | if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol)) |
| 561 | ctx = GREP_CONTEXT_BODY; |
| 562 | |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 563 | hit = match_line(opt, bol, eol, ctx, collect_hits); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 564 | *eol = ch; |
| 565 | |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 566 | if (collect_hits) |
| 567 | goto next_line; |
| 568 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 569 | /* "grep -v -e foo -e bla" should list lines |
| 570 | * that do not have either, so inversion should |
| 571 | * be done outside. |
| 572 | */ |
| 573 | if (opt->invert) |
| 574 | hit = !hit; |
| 575 | if (opt->unmatch_name_only) { |
| 576 | if (hit) |
| 577 | return 0; |
| 578 | goto next_line; |
| 579 | } |
| 580 | if (hit) { |
| 581 | count++; |
| 582 | if (opt->status_only) |
| 583 | return 1; |
| 584 | if (binary_match_only) { |
| 585 | printf("Binary file %s matches\n", name); |
| 586 | return 1; |
| 587 | } |
| 588 | if (opt->name_only) { |
Raphael Zimmerer | 83caecc | 2008-10-01 18:11:15 +0200 | [diff] [blame] | 589 | show_name(opt, name); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 590 | return 1; |
| 591 | } |
| 592 | /* Hit at this line. If we haven't shown the |
| 593 | * pre-context lines, we would need to show them. |
| 594 | * When asked to do "count", this still show |
| 595 | * the context which is nonsense, but the user |
| 596 | * deserves to get that ;-). |
| 597 | */ |
| 598 | if (opt->pre_context) { |
| 599 | unsigned from; |
| 600 | if (opt->pre_context < lno) |
| 601 | from = lno - opt->pre_context; |
| 602 | else |
| 603 | from = 1; |
| 604 | if (from <= last_shown) |
| 605 | from = last_shown + 1; |
| 606 | if (last_shown && from != last_shown + 1) |
Daniel Lowe | 9db56f7 | 2008-11-10 16:07:52 -0500 | [diff] [blame] | 607 | fputs(hunk_mark, stdout); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 608 | while (from < lno) { |
| 609 | pcl = &prev[lno-from-1]; |
| 610 | show_line(opt, pcl->bol, pcl->eol, |
| 611 | name, from, '-'); |
| 612 | from++; |
| 613 | } |
| 614 | last_shown = lno-1; |
| 615 | } |
| 616 | if (last_shown && lno != last_shown + 1) |
Daniel Lowe | 9db56f7 | 2008-11-10 16:07:52 -0500 | [diff] [blame] | 617 | fputs(hunk_mark, stdout); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 618 | if (!opt->count) |
| 619 | show_line(opt, bol, eol, name, lno, ':'); |
| 620 | last_shown = last_hit = lno; |
| 621 | } |
| 622 | else if (last_hit && |
| 623 | lno <= last_hit + opt->post_context) { |
| 624 | /* If the last hit is within the post context, |
| 625 | * we need to show this line. |
| 626 | */ |
| 627 | if (last_shown && lno != last_shown + 1) |
Daniel Lowe | 9db56f7 | 2008-11-10 16:07:52 -0500 | [diff] [blame] | 628 | fputs(hunk_mark, stdout); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 629 | show_line(opt, bol, eol, name, lno, '-'); |
| 630 | last_shown = lno; |
| 631 | } |
| 632 | if (opt->pre_context) { |
| 633 | memmove(prev+1, prev, |
| 634 | (opt->pre_context-1) * sizeof(*prev)); |
| 635 | prev->bol = bol; |
| 636 | prev->eol = eol; |
| 637 | } |
| 638 | |
| 639 | next_line: |
| 640 | bol = eol + 1; |
| 641 | if (!left) |
| 642 | break; |
| 643 | left--; |
| 644 | lno++; |
| 645 | } |
| 646 | |
Junio C Hamano | b48fb5b | 2006-09-27 16:27:10 -0700 | [diff] [blame] | 647 | free(prev); |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 648 | if (collect_hits) |
| 649 | return 0; |
Junio C Hamano | b48fb5b | 2006-09-27 16:27:10 -0700 | [diff] [blame] | 650 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 651 | if (opt->status_only) |
| 652 | return 0; |
| 653 | if (opt->unmatch_name_only) { |
| 654 | /* We did not see any hit, so we want to show this */ |
Raphael Zimmerer | 83caecc | 2008-10-01 18:11:15 +0200 | [diff] [blame] | 655 | show_name(opt, name); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 656 | return 1; |
| 657 | } |
| 658 | |
| 659 | /* NEEDSWORK: |
| 660 | * The real "grep -c foo *.c" gives many "bar.c:0" lines, |
| 661 | * which feels mostly useless but sometimes useful. Maybe |
| 662 | * make it another option? For now suppress them. |
| 663 | */ |
| 664 | if (opt->count && count) |
Raphael Zimmerer | 83caecc | 2008-10-01 18:11:15 +0200 | [diff] [blame] | 665 | printf("%s%c%u\n", name, |
| 666 | opt->null_following_name ? '\0' : ':', count); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 667 | return !!last_hit; |
| 668 | } |
| 669 | |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 670 | static void clr_hit_marker(struct grep_expr *x) |
| 671 | { |
| 672 | /* All-hit markers are meaningful only at the very top level |
| 673 | * OR node. |
| 674 | */ |
| 675 | while (1) { |
| 676 | x->hit = 0; |
| 677 | if (x->node != GREP_NODE_OR) |
| 678 | return; |
| 679 | x->u.binary.left->hit = 0; |
| 680 | x = x->u.binary.right; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | static int chk_hit_marker(struct grep_expr *x) |
| 685 | { |
| 686 | /* Top level nodes have hit markers. See if they all are hits */ |
| 687 | while (1) { |
| 688 | if (x->node != GREP_NODE_OR) |
| 689 | return x->hit; |
| 690 | if (!x->u.binary.left->hit) |
| 691 | return 0; |
| 692 | x = x->u.binary.right; |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size) |
| 697 | { |
| 698 | /* |
| 699 | * we do not have to do the two-pass grep when we do not check |
| 700 | * buffer-wide "all-match". |
| 701 | */ |
| 702 | if (!opt->all_match) |
| 703 | return grep_buffer_1(opt, name, buf, size, 0); |
| 704 | |
| 705 | /* Otherwise the toplevel "or" terms hit a bit differently. |
| 706 | * We first clear hit markers from them. |
| 707 | */ |
| 708 | clr_hit_marker(opt->pattern_expression); |
| 709 | grep_buffer_1(opt, name, buf, size, 1); |
| 710 | |
| 711 | if (!chk_hit_marker(opt->pattern_expression)) |
| 712 | return 0; |
| 713 | |
| 714 | return grep_buffer_1(opt, name, buf, size, 0); |
| 715 | } |