]> git.ipfire.org Git - thirdparty/git.git/blame - grep.c
Use kwset in pickaxe
[thirdparty/git.git] / grep.c
CommitLineData
83b5d2f5 1#include "cache.h"
83b5d2f5 2#include "grep.h"
60ecac98 3#include "userdiff.h"
6bfce93e 4#include "xdiff-interface.h"
83b5d2f5 5
a4d7d2c6
JH
6void 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;
ed40a095 10 p->patternlen = strlen(pat);
a4d7d2c6
JH
11 p->origin = "header";
12 p->no = 0;
13 p->token = GREP_PATTERN_HEAD;
14 p->field = field;
80235ba7
JH
15 *opt->header_tail = p;
16 opt->header_tail = &p->next;
a4d7d2c6
JH
17 p->next = NULL;
18}
19
83b5d2f5
JH
20void append_grep_pattern(struct grep_opt *opt, const char *pat,
21 const char *origin, int no, enum grep_pat_token t)
ed40a095
RS
22{
23 append_grep_pat(opt, pat, strlen(pat), origin, no, t);
24}
25
26void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen,
27 const char *origin, int no, enum grep_pat_token t)
83b5d2f5
JH
28{
29 struct grep_pat *p = xcalloc(1, sizeof(*p));
30 p->pattern = pat;
ed40a095 31 p->patternlen = patlen;
83b5d2f5
JH
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
5b594f45
FK
40struct 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
ed40a095
RS
55 append_grep_pat(ret, pat->pattern, pat->patternlen,
56 pat->origin, pat->no, pat->token);
5b594f45
FK
57 }
58
59 return ret;
60}
61
a30c148a
MK
62static 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
63e7e9d8
MK
77#ifdef USE_LIBPCRE
78static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
79{
80 const char *error;
81 int erroffset;
82 int options = 0;
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
97static 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
118static 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 */
124static 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
129static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
130 regmatch_t *match, int eflags)
131{
132 return 1;
133}
134
135static void free_pcre_regexp(struct grep_pat *p)
136{
137}
138#endif /* !USE_LIBPCRE */
139
83b5d2f5
JH
140static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
141{
c822255c
RS
142 int err;
143
d7eb527d 144 p->word_regexp = opt->word_regexp;
5183bf67 145 p->ignore_case = opt->ignore_case;
79286102 146 p->fixed = opt->fixed;
d7eb527d 147
c822255c
RS
148 if (p->fixed)
149 return;
150
63e7e9d8
MK
151 if (opt->pcre) {
152 compile_pcre_regexp(p, opt);
153 return;
154 }
155
c822255c 156 err = regcomp(&p->regexp, p->pattern, opt->regflags);
83b5d2f5
JH
157 if (err) {
158 char errbuf[1024];
83b5d2f5
JH
159 regerror(err, &p->regexp, errbuf, 1024);
160 regfree(&p->regexp);
a30c148a 161 compile_regexp_failed(p, errbuf);
83b5d2f5
JH
162 }
163}
164
0ab7befa 165static struct grep_expr *compile_pattern_or(struct grep_pat **);
83b5d2f5
JH
166static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
167{
168 struct grep_pat *p;
169 struct grep_expr *x;
170
171 p = *list;
c922b01f
LT
172 if (!p)
173 return NULL;
83b5d2f5
JH
174 switch (p->token) {
175 case GREP_PATTERN: /* atom */
480c1ca6
JH
176 case GREP_PATTERN_HEAD:
177 case GREP_PATTERN_BODY:
83b5d2f5
JH
178 x = xcalloc(1, sizeof (struct grep_expr));
179 x->node = GREP_NODE_ATOM;
180 x->u.atom = p;
181 *list = p->next;
182 return x;
183 case GREP_OPEN_PAREN:
184 *list = p->next;
0ab7befa 185 x = compile_pattern_or(list);
83b5d2f5
JH
186 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
187 die("unmatched parenthesis");
188 *list = (*list)->next;
189 return x;
190 default:
191 return NULL;
192 }
193}
194
195static struct grep_expr *compile_pattern_not(struct grep_pat **list)
196{
197 struct grep_pat *p;
198 struct grep_expr *x;
199
200 p = *list;
c922b01f
LT
201 if (!p)
202 return NULL;
83b5d2f5
JH
203 switch (p->token) {
204 case GREP_NOT:
205 if (!p->next)
206 die("--not not followed by pattern expression");
207 *list = p->next;
208 x = xcalloc(1, sizeof (struct grep_expr));
209 x->node = GREP_NODE_NOT;
210 x->u.unary = compile_pattern_not(list);
211 if (!x->u.unary)
212 die("--not followed by non pattern expression");
213 return x;
214 default:
215 return compile_pattern_atom(list);
216 }
217}
218
219static struct grep_expr *compile_pattern_and(struct grep_pat **list)
220{
221 struct grep_pat *p;
222 struct grep_expr *x, *y, *z;
223
224 x = compile_pattern_not(list);
225 p = *list;
226 if (p && p->token == GREP_AND) {
227 if (!p->next)
228 die("--and not followed by pattern expression");
229 *list = p->next;
230 y = compile_pattern_and(list);
231 if (!y)
232 die("--and not followed by pattern expression");
233 z = xcalloc(1, sizeof (struct grep_expr));
234 z->node = GREP_NODE_AND;
235 z->u.binary.left = x;
236 z->u.binary.right = y;
237 return z;
238 }
239 return x;
240}
241
242static struct grep_expr *compile_pattern_or(struct grep_pat **list)
243{
244 struct grep_pat *p;
245 struct grep_expr *x, *y, *z;
246
247 x = compile_pattern_and(list);
248 p = *list;
249 if (x && p && p->token != GREP_CLOSE_PAREN) {
250 y = compile_pattern_or(list);
251 if (!y)
252 die("not a pattern expression %s", p->pattern);
253 z = xcalloc(1, sizeof (struct grep_expr));
254 z->node = GREP_NODE_OR;
255 z->u.binary.left = x;
256 z->u.binary.right = y;
257 return z;
258 }
259 return x;
260}
261
262static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
263{
264 return compile_pattern_or(list);
265}
266
5aaeb733
JH
267static struct grep_expr *grep_true_expr(void)
268{
269 struct grep_expr *z = xcalloc(1, sizeof(*z));
270 z->node = GREP_NODE_TRUE;
271 return z;
272}
273
274static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
275{
276 struct grep_expr *z = xcalloc(1, sizeof(*z));
277 z->node = GREP_NODE_OR;
278 z->u.binary.left = left;
279 z->u.binary.right = right;
280 return z;
281}
282
95ce9ce2 283static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
83b5d2f5
JH
284{
285 struct grep_pat *p;
95ce9ce2 286 struct grep_expr *header_expr;
5aaeb733
JH
287 struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]);
288 enum grep_header_field fld;
83b5d2f5 289
95ce9ce2
JH
290 if (!opt->header_list)
291 return NULL;
292 p = opt->header_list;
95ce9ce2
JH
293 for (p = opt->header_list; p; p = p->next) {
294 if (p->token != GREP_PATTERN_HEAD)
295 die("bug: a non-header pattern in grep header list.");
296 if (p->field < 0 || GREP_HEADER_FIELD_MAX <= p->field)
297 die("bug: unknown header field %d", p->field);
298 compile_regexp(p, opt);
80235ba7 299 }
5aaeb733
JH
300
301 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++)
302 header_group[fld] = NULL;
303
304 for (p = opt->header_list; p; p = p->next) {
305 struct grep_expr *h;
306 struct grep_pat *pp = p;
307
308 h = compile_pattern_atom(&pp);
309 if (!h || pp != p->next)
310 die("bug: malformed header expr");
311 if (!header_group[p->field]) {
312 header_group[p->field] = h;
313 continue;
314 }
315 header_group[p->field] = grep_or_expr(h, header_group[p->field]);
316 }
317
318 header_expr = NULL;
319
320 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) {
321 if (!header_group[fld])
322 continue;
323 if (!header_expr)
324 header_expr = grep_true_expr();
325 header_expr = grep_or_expr(header_group[fld], header_expr);
326 }
95ce9ce2
JH
327 return header_expr;
328}
329
330void compile_grep_patterns(struct grep_opt *opt)
331{
332 struct grep_pat *p;
333 struct grep_expr *header_expr = prep_header_patterns(opt);
0ab7befa 334
83b5d2f5 335 for (p = opt->pattern_list; p; p = p->next) {
480c1ca6
JH
336 switch (p->token) {
337 case GREP_PATTERN: /* atom */
338 case GREP_PATTERN_HEAD:
339 case GREP_PATTERN_BODY:
c822255c 340 compile_regexp(p, opt);
480c1ca6
JH
341 break;
342 default:
83b5d2f5 343 opt->extended = 1;
480c1ca6
JH
344 break;
345 }
83b5d2f5
JH
346 }
347
80235ba7
JH
348 if (opt->all_match || header_expr)
349 opt->extended = 1;
350 else if (!opt->extended)
83b5d2f5
JH
351 return;
352
83b5d2f5 353 p = opt->pattern_list;
ba150a3f
MB
354 if (p)
355 opt->pattern_expression = compile_pattern_expr(&p);
83b5d2f5
JH
356 if (p)
357 die("incomplete pattern expression: %s", p->pattern);
80235ba7
JH
358
359 if (!header_expr)
360 return;
361
5aaeb733 362 if (!opt->pattern_expression)
80235ba7 363 opt->pattern_expression = header_expr;
5aaeb733
JH
364 else
365 opt->pattern_expression = grep_or_expr(opt->pattern_expression,
366 header_expr);
80235ba7 367 opt->all_match = 1;
83b5d2f5
JH
368}
369
b48fb5b6
JH
370static void free_pattern_expr(struct grep_expr *x)
371{
372 switch (x->node) {
5aaeb733 373 case GREP_NODE_TRUE:
b48fb5b6
JH
374 case GREP_NODE_ATOM:
375 break;
376 case GREP_NODE_NOT:
377 free_pattern_expr(x->u.unary);
378 break;
379 case GREP_NODE_AND:
380 case GREP_NODE_OR:
381 free_pattern_expr(x->u.binary.left);
382 free_pattern_expr(x->u.binary.right);
383 break;
384 }
385 free(x);
386}
387
388void free_grep_patterns(struct grep_opt *opt)
389{
390 struct grep_pat *p, *n;
391
392 for (p = opt->pattern_list; p; p = n) {
393 n = p->next;
394 switch (p->token) {
395 case GREP_PATTERN: /* atom */
396 case GREP_PATTERN_HEAD:
397 case GREP_PATTERN_BODY:
63e7e9d8
MK
398 if (p->pcre_regexp)
399 free_pcre_regexp(p);
400 else
401 regfree(&p->regexp);
b48fb5b6
JH
402 break;
403 default:
404 break;
405 }
406 free(p);
407 }
408
409 if (!opt->extended)
410 return;
411 free_pattern_expr(opt->pattern_expression);
412}
413
83b5d2f5
JH
414static char *end_of_line(char *cp, unsigned long *left)
415{
416 unsigned long l = *left;
417 while (l && *cp != '\n') {
418 l--;
419 cp++;
420 }
421 *left = l;
422 return cp;
423}
424
425static int word_char(char ch)
426{
427 return isalnum(ch) || ch == '_';
428}
429
55f638bd
ML
430static void output_color(struct grep_opt *opt, const void *data, size_t size,
431 const char *color)
432{
433 if (opt->color && color && color[0]) {
434 opt->output(opt, color, strlen(color));
435 opt->output(opt, data, size);
436 opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
437 } else
438 opt->output(opt, data, size);
439}
440
441static void output_sep(struct grep_opt *opt, char sign)
442{
443 if (opt->null_following_name)
444 opt->output(opt, "\0", 1);
445 else
446 output_color(opt, &sign, 1, opt->color_sep);
447}
448
83caecca
RZ
449static void show_name(struct grep_opt *opt, const char *name)
450{
55f638bd 451 output_color(opt, name, strlen(name), opt->color_filename);
5b594f45 452 opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
83caecca
RZ
453}
454
ed40a095
RS
455static int fixmatch(struct grep_pat *p, char *line, char *eol,
456 regmatch_t *match)
83b5d2f5 457{
5183bf67 458 char *hit;
1baddf4b 459
ed40a095 460 if (p->ignore_case) {
52d799a7
RS
461 char *s = line;
462 do {
ed40a095 463 hit = strcasestr(s, p->pattern);
52d799a7
RS
464 if (hit)
465 break;
466 s += strlen(s) + 1;
467 } while (s < eol);
468 } else
ed40a095 469 hit = memmem(line, eol - line, p->pattern, p->patternlen);
5183bf67 470
83b5d2f5
JH
471 if (!hit) {
472 match->rm_so = match->rm_eo = -1;
473 return REG_NOMATCH;
474 }
475 else {
476 match->rm_so = hit - line;
ed40a095 477 match->rm_eo = match->rm_so + p->patternlen;
83b5d2f5
JH
478 return 0;
479 }
480}
481
f96e5673
RS
482static int regmatch(const regex_t *preg, char *line, char *eol,
483 regmatch_t *match, int eflags)
484{
485#ifdef REG_STARTEND
486 match->rm_so = 0;
487 match->rm_eo = eol - line;
488 eflags |= REG_STARTEND;
489#endif
490 return regexec(preg, line, 1, match, eflags);
491}
492
97e77784
MK
493static int patmatch(struct grep_pat *p, char *line, char *eol,
494 regmatch_t *match, int eflags)
495{
496 int hit;
497
498 if (p->fixed)
499 hit = !fixmatch(p, line, eol, match);
63e7e9d8
MK
500 else if (p->pcre_regexp)
501 hit = !pcrematch(p, line, eol, match, eflags);
97e77784
MK
502 else
503 hit = !regmatch(&p->regexp, line, eol, match, eflags);
504
505 return hit;
506}
507
a4d7d2c6
JH
508static int strip_timestamp(char *bol, char **eol_p)
509{
510 char *eol = *eol_p;
511 int ch;
512
513 while (bol < --eol) {
514 if (*eol != '>')
515 continue;
516 *eol_p = ++eol;
517 ch = *eol;
518 *eol = '\0';
519 return ch;
520 }
521 return 0;
522}
523
524static struct {
525 const char *field;
526 size_t len;
527} header_field[] = {
528 { "author ", 7 },
529 { "committer ", 10 },
530};
531
d7eb527d 532static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
79212772
RS
533 enum grep_context ctx,
534 regmatch_t *pmatch, int eflags)
83b5d2f5
JH
535{
536 int hit = 0;
a4d7d2c6 537 int saved_ch = 0;
e701fadb 538 const char *start = bol;
83b5d2f5 539
480c1ca6
JH
540 if ((p->token != GREP_PATTERN) &&
541 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
542 return 0;
543
a4d7d2c6
JH
544 if (p->token == GREP_PATTERN_HEAD) {
545 const char *field;
546 size_t len;
547 assert(p->field < ARRAY_SIZE(header_field));
548 field = header_field[p->field].field;
549 len = header_field[p->field].len;
550 if (strncmp(bol, field, len))
551 return 0;
552 bol += len;
553 saved_ch = strip_timestamp(bol, &eol);
554 }
555
83b5d2f5 556 again:
97e77784 557 hit = patmatch(p, bol, eol, pmatch, eflags);
83b5d2f5 558
d7eb527d 559 if (hit && p->word_regexp) {
83b5d2f5 560 if ((pmatch[0].rm_so < 0) ||
84201eae 561 (eol - bol) < pmatch[0].rm_so ||
83b5d2f5
JH
562 (pmatch[0].rm_eo < 0) ||
563 (eol - bol) < pmatch[0].rm_eo)
564 die("regexp returned nonsense");
565
566 /* Match beginning must be either beginning of the
567 * line, or at word boundary (i.e. the last char must
568 * not be a word char). Similarly, match end must be
569 * either end of the line, or at word boundary
570 * (i.e. the next char must not be a word char).
571 */
fb62eb7f 572 if ( ((pmatch[0].rm_so == 0) ||
83b5d2f5
JH
573 !word_char(bol[pmatch[0].rm_so-1])) &&
574 ((pmatch[0].rm_eo == (eol-bol)) ||
575 !word_char(bol[pmatch[0].rm_eo])) )
576 ;
577 else
578 hit = 0;
579
84201eae
RS
580 /* Words consist of at least one character. */
581 if (pmatch->rm_so == pmatch->rm_eo)
582 hit = 0;
583
83b5d2f5
JH
584 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
585 /* There could be more than one match on the
586 * line, and the first match might not be
587 * strict word match. But later ones could be!
fb62eb7f
RS
588 * Forward to the next possible start, i.e. the
589 * next position following a non-word char.
83b5d2f5
JH
590 */
591 bol = pmatch[0].rm_so + bol + 1;
fb62eb7f
RS
592 while (word_char(bol[-1]) && bol < eol)
593 bol++;
dbb6a4ad 594 eflags |= REG_NOTBOL;
fb62eb7f
RS
595 if (bol < eol)
596 goto again;
83b5d2f5
JH
597 }
598 }
a4d7d2c6
JH
599 if (p->token == GREP_PATTERN_HEAD && saved_ch)
600 *eol = saved_ch;
e701fadb
RS
601 if (hit) {
602 pmatch[0].rm_so += bol - start;
603 pmatch[0].rm_eo += bol - start;
604 }
83b5d2f5
JH
605 return hit;
606}
607
d7eb527d
RS
608static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
609 enum grep_context ctx, int collect_hits)
83b5d2f5 610{
0ab7befa 611 int h = 0;
79212772 612 regmatch_t match;
0ab7befa 613
c922b01f
LT
614 if (!x)
615 die("Not a valid grep expression");
83b5d2f5 616 switch (x->node) {
5aaeb733
JH
617 case GREP_NODE_TRUE:
618 h = 1;
619 break;
83b5d2f5 620 case GREP_NODE_ATOM:
79212772 621 h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
83b5d2f5
JH
622 break;
623 case GREP_NODE_NOT:
d7eb527d 624 h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
0ab7befa 625 break;
83b5d2f5 626 case GREP_NODE_AND:
d7eb527d 627 if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
252d560d 628 return 0;
d7eb527d 629 h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
0ab7befa 630 break;
83b5d2f5 631 case GREP_NODE_OR:
0ab7befa 632 if (!collect_hits)
d7eb527d 633 return (match_expr_eval(x->u.binary.left,
0ab7befa 634 bol, eol, ctx, 0) ||
d7eb527d 635 match_expr_eval(x->u.binary.right,
0ab7befa 636 bol, eol, ctx, 0));
d7eb527d 637 h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
0ab7befa 638 x->u.binary.left->hit |= h;
d7eb527d 639 h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
0ab7befa
JH
640 break;
641 default:
d7530708 642 die("Unexpected node type (internal error) %d", x->node);
83b5d2f5 643 }
0ab7befa
JH
644 if (collect_hits)
645 x->hit |= h;
646 return h;
83b5d2f5
JH
647}
648
480c1ca6 649static int match_expr(struct grep_opt *opt, char *bol, char *eol,
0ab7befa 650 enum grep_context ctx, int collect_hits)
83b5d2f5
JH
651{
652 struct grep_expr *x = opt->pattern_expression;
d7eb527d 653 return match_expr_eval(x, bol, eol, ctx, collect_hits);
83b5d2f5
JH
654}
655
480c1ca6 656static int match_line(struct grep_opt *opt, char *bol, char *eol,
0ab7befa 657 enum grep_context ctx, int collect_hits)
83b5d2f5
JH
658{
659 struct grep_pat *p;
79212772
RS
660 regmatch_t match;
661
83b5d2f5 662 if (opt->extended)
0ab7befa
JH
663 return match_expr(opt, bol, eol, ctx, collect_hits);
664
665 /* we do not call with collect_hits without being extended */
83b5d2f5 666 for (p = opt->pattern_list; p; p = p->next) {
79212772 667 if (match_one_pattern(p, bol, eol, ctx, &match, 0))
83b5d2f5
JH
668 return 1;
669 }
670 return 0;
671}
672
7e8f59d5
RS
673static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
674 enum grep_context ctx,
675 regmatch_t *pmatch, int eflags)
676{
677 regmatch_t match;
678
679 if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
680 return 0;
681 if (match.rm_so < 0 || match.rm_eo < 0)
682 return 0;
683 if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
684 if (match.rm_so > pmatch->rm_so)
685 return 1;
686 if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
687 return 1;
688 }
689 pmatch->rm_so = match.rm_so;
690 pmatch->rm_eo = match.rm_eo;
691 return 1;
692}
693
694static int next_match(struct grep_opt *opt, char *bol, char *eol,
695 enum grep_context ctx, regmatch_t *pmatch, int eflags)
696{
697 struct grep_pat *p;
698 int hit = 0;
699
700 pmatch->rm_so = pmatch->rm_eo = -1;
701 if (bol < eol) {
702 for (p = opt->pattern_list; p; p = p->next) {
703 switch (p->token) {
704 case GREP_PATTERN: /* atom */
705 case GREP_PATTERN_HEAD:
706 case GREP_PATTERN_BODY:
707 hit |= match_next_pattern(p, bol, eol, ctx,
708 pmatch, eflags);
709 break;
710 default:
711 break;
712 }
713 }
714 }
715 return hit;
716}
717
718static void show_line(struct grep_opt *opt, char *bol, char *eol,
719 const char *name, unsigned lno, char sign)
720{
721 int rest = eol - bol;
00588bb5 722 char *line_color = NULL;
7e8f59d5 723
a8f0e764
RS
724 if (opt->file_break && opt->last_shown == 0) {
725 if (opt->show_hunk_mark)
726 opt->output(opt, "\n", 1);
ba8ea749 727 } else if (opt->pre_context || opt->post_context || opt->funcbody) {
046802d0 728 if (opt->last_shown == 0) {
55f638bd
ML
729 if (opt->show_hunk_mark) {
730 output_color(opt, "--", 2, opt->color_sep);
731 opt->output(opt, "\n", 1);
07b838f0 732 }
55f638bd
ML
733 } else if (lno > opt->last_shown + 1) {
734 output_color(opt, "--", 2, opt->color_sep);
735 opt->output(opt, "\n", 1);
736 }
5dd06d38 737 }
1d84f72e
RS
738 if (opt->heading && opt->last_shown == 0) {
739 output_color(opt, name, strlen(name), opt->color_filename);
740 opt->output(opt, "\n", 1);
741 }
5dd06d38
RS
742 opt->last_shown = lno;
743
1d84f72e 744 if (!opt->heading && opt->pathname) {
55f638bd
ML
745 output_color(opt, name, strlen(name), opt->color_filename);
746 output_sep(opt, sign);
5b594f45
FK
747 }
748 if (opt->linenum) {
749 char buf[32];
750 snprintf(buf, sizeof(buf), "%d", lno);
55f638bd
ML
751 output_color(opt, buf, strlen(buf), opt->color_lineno);
752 output_sep(opt, sign);
5b594f45 753 }
7e8f59d5
RS
754 if (opt->color) {
755 regmatch_t match;
756 enum grep_context ctx = GREP_CONTEXT_BODY;
757 int ch = *eol;
758 int eflags = 0;
759
00588bb5
ML
760 if (sign == ':')
761 line_color = opt->color_selected;
762 else if (sign == '-')
763 line_color = opt->color_context;
764 else if (sign == '=')
765 line_color = opt->color_function;
7e8f59d5
RS
766 *eol = '\0';
767 while (next_match(opt, bol, eol, ctx, &match, eflags)) {
1f5b9cc4
RS
768 if (match.rm_so == match.rm_eo)
769 break;
5b594f45 770
00588bb5 771 output_color(opt, bol, match.rm_so, line_color);
55f638bd
ML
772 output_color(opt, bol + match.rm_so,
773 match.rm_eo - match.rm_so,
774 opt->color_match);
7e8f59d5
RS
775 bol += match.rm_eo;
776 rest -= match.rm_eo;
777 eflags = REG_NOTBOL;
778 }
779 *eol = ch;
780 }
00588bb5 781 output_color(opt, bol, rest, line_color);
5b594f45 782 opt->output(opt, "\n", 1);
7e8f59d5
RS
783}
784
60ecac98 785static int match_funcname(struct grep_opt *opt, char *bol, char *eol)
2944e4e6 786{
60ecac98
RS
787 xdemitconf_t *xecfg = opt->priv;
788 if (xecfg && xecfg->find_func) {
789 char buf[1];
790 return xecfg->find_func(bol, eol - bol, buf, 1,
791 xecfg->find_func_priv) >= 0;
792 }
793
2944e4e6
RS
794 if (bol == eol)
795 return 0;
796 if (isalpha(*bol) || *bol == '_' || *bol == '$')
797 return 1;
798 return 0;
799}
800
801static void show_funcname_line(struct grep_opt *opt, const char *name,
802 char *buf, char *bol, unsigned lno)
803{
804 while (bol > buf) {
805 char *eol = --bol;
806
807 while (bol > buf && bol[-1] != '\n')
808 bol--;
809 lno--;
810
811 if (lno <= opt->last_shown)
812 break;
813
60ecac98 814 if (match_funcname(opt, bol, eol)) {
2944e4e6
RS
815 show_line(opt, bol, eol, name, lno, '=');
816 break;
817 }
818 }
819}
820
49de3216 821static void show_pre_context(struct grep_opt *opt, const char *name, char *buf,
ba8ea749 822 char *bol, char *end, unsigned lno)
49de3216 823{
2944e4e6 824 unsigned cur = lno, from = 1, funcname_lno = 0;
ba8ea749
RS
825 int funcname_needed = !!opt->funcname;
826
827 if (opt->funcbody && !match_funcname(opt, bol, end))
828 funcname_needed = 2;
49de3216
RS
829
830 if (opt->pre_context < lno)
831 from = lno - opt->pre_context;
832 if (from <= opt->last_shown)
833 from = opt->last_shown + 1;
834
835 /* Rewind. */
ba8ea749
RS
836 while (bol > buf &&
837 cur > (funcname_needed == 2 ? opt->last_shown + 1 : from)) {
2944e4e6
RS
838 char *eol = --bol;
839
49de3216
RS
840 while (bol > buf && bol[-1] != '\n')
841 bol--;
842 cur--;
60ecac98 843 if (funcname_needed && match_funcname(opt, bol, eol)) {
2944e4e6
RS
844 funcname_lno = cur;
845 funcname_needed = 0;
846 }
49de3216
RS
847 }
848
2944e4e6
RS
849 /* We need to look even further back to find a function signature. */
850 if (opt->funcname && funcname_needed)
851 show_funcname_line(opt, name, buf, bol, cur);
852
49de3216
RS
853 /* Back forward. */
854 while (cur < lno) {
2944e4e6 855 char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
49de3216
RS
856
857 while (*eol != '\n')
858 eol++;
2944e4e6 859 show_line(opt, bol, eol, name, cur, sign);
49de3216
RS
860 bol = eol + 1;
861 cur++;
862 }
863}
864
a26345b6
JH
865static int should_lookahead(struct grep_opt *opt)
866{
867 struct grep_pat *p;
868
869 if (opt->extended)
870 return 0; /* punt for too complex stuff */
871 if (opt->invert)
872 return 0;
873 for (p = opt->pattern_list; p; p = p->next) {
874 if (p->token != GREP_PATTERN)
875 return 0; /* punt for "header only" and stuff */
876 }
877 return 1;
878}
879
880static int look_ahead(struct grep_opt *opt,
881 unsigned long *left_p,
882 unsigned *lno_p,
883 char **bol_p)
884{
885 unsigned lno = *lno_p;
886 char *bol = *bol_p;
887 struct grep_pat *p;
888 char *sp, *last_bol;
889 regoff_t earliest = -1;
890
891 for (p = opt->pattern_list; p; p = p->next) {
892 int hit;
893 regmatch_t m;
894
97e77784 895 hit = patmatch(p, bol, bol + *left_p, &m, 0);
a26345b6
JH
896 if (!hit || m.rm_so < 0 || m.rm_eo < 0)
897 continue;
898 if (earliest < 0 || m.rm_so < earliest)
899 earliest = m.rm_so;
900 }
901
902 if (earliest < 0) {
903 *bol_p = bol + *left_p;
904 *left_p = 0;
905 return 1;
906 }
907 for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
908 ; /* find the beginning of the line */
909 last_bol = sp;
910
911 for (sp = bol; sp < last_bol; sp++) {
912 if (*sp == '\n')
913 lno++;
914 }
915 *left_p -= last_bol - bol;
916 *bol_p = last_bol;
917 *lno_p = lno;
918 return 0;
919}
920
5b594f45
FK
921int grep_threads_ok(const struct grep_opt *opt)
922{
923 /* If this condition is true, then we may use the attribute
924 * machinery in grep_buffer_1. The attribute code is not
925 * thread safe, so we disable the use of threads.
926 */
927 if (opt->funcname && !opt->unmatch_name_only && !opt->status_only &&
928 !opt->name_only)
929 return 0;
930
5b594f45
FK
931 return 1;
932}
933
934static void std_output(struct grep_opt *opt, const void *buf, size_t size)
935{
936 fwrite(buf, size, 1, stdout);
937}
938
0ab7befa
JH
939static int grep_buffer_1(struct grep_opt *opt, const char *name,
940 char *buf, unsigned long size, int collect_hits)
83b5d2f5
JH
941{
942 char *bol = buf;
943 unsigned long left = size;
944 unsigned lno = 1;
83b5d2f5 945 unsigned last_hit = 0;
83b5d2f5 946 int binary_match_only = 0;
83b5d2f5 947 unsigned count = 0;
a26345b6 948 int try_lookahead = 0;
ba8ea749 949 int show_function = 0;
480c1ca6 950 enum grep_context ctx = GREP_CONTEXT_HEAD;
60ecac98 951 xdemitconf_t xecfg;
83b5d2f5 952
5b594f45
FK
953 if (!opt->output)
954 opt->output = std_output;
955
ba8ea749
RS
956 if (opt->pre_context || opt->post_context || opt->file_break ||
957 opt->funcbody) {
08303c36
RS
958 /* Show hunk marks, except for the first file. */
959 if (opt->last_shown)
960 opt->show_hunk_mark = 1;
961 /*
962 * If we're using threads then we can't easily identify
963 * the first file. Always put hunk marks in that case
964 * and skip the very first one later in work_done().
965 */
966 if (opt->output != std_output)
967 opt->show_hunk_mark = 1;
968 }
431d6e7b
RS
969 opt->last_shown = 0;
970
64fcec78
RS
971 switch (opt->binary) {
972 case GREP_BINARY_DEFAULT:
973 if (buffer_is_binary(buf, size))
83b5d2f5 974 binary_match_only = 1;
64fcec78
RS
975 break;
976 case GREP_BINARY_NOMATCH:
977 if (buffer_is_binary(buf, size))
83b5d2f5 978 return 0; /* Assume unmatch */
64fcec78
RS
979 break;
980 case GREP_BINARY_TEXT:
981 break;
982 default:
983 die("bug: unknown binary handling mode");
83b5d2f5
JH
984 }
985
60ecac98
RS
986 memset(&xecfg, 0, sizeof(xecfg));
987 if (opt->funcname && !opt->unmatch_name_only && !opt->status_only &&
988 !opt->name_only && !binary_match_only && !collect_hits) {
989 struct userdiff_driver *drv = userdiff_find_by_path(name);
990 if (drv && drv->funcname.pattern) {
991 const struct userdiff_funcname *pe = &drv->funcname;
992 xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
993 opt->priv = &xecfg;
994 }
995 }
a26345b6 996 try_lookahead = should_lookahead(opt);
60ecac98 997
83b5d2f5
JH
998 while (left) {
999 char *eol, ch;
0ab7befa 1000 int hit;
83b5d2f5 1001
a26345b6 1002 /*
8997da38 1003 * look_ahead() skips quickly to the line that possibly
a26345b6
JH
1004 * has the next hit; don't call it if we need to do
1005 * something more than just skipping the current line
1006 * in response to an unmatch for the current line. E.g.
1007 * inside a post-context window, we will show the current
1008 * line as a context around the previous hit when it
1009 * doesn't hit.
1010 */
1011 if (try_lookahead
1012 && !(last_hit
ba8ea749
RS
1013 && (show_function ||
1014 lno <= last_hit + opt->post_context))
a26345b6
JH
1015 && look_ahead(opt, &left, &lno, &bol))
1016 break;
83b5d2f5
JH
1017 eol = end_of_line(bol, &left);
1018 ch = *eol;
1019 *eol = 0;
1020
480c1ca6
JH
1021 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
1022 ctx = GREP_CONTEXT_BODY;
1023
0ab7befa 1024 hit = match_line(opt, bol, eol, ctx, collect_hits);
83b5d2f5
JH
1025 *eol = ch;
1026
0ab7befa
JH
1027 if (collect_hits)
1028 goto next_line;
1029
83b5d2f5
JH
1030 /* "grep -v -e foo -e bla" should list lines
1031 * that do not have either, so inversion should
1032 * be done outside.
1033 */
1034 if (opt->invert)
1035 hit = !hit;
1036 if (opt->unmatch_name_only) {
1037 if (hit)
1038 return 0;
1039 goto next_line;
1040 }
1041 if (hit) {
1042 count++;
1043 if (opt->status_only)
1044 return 1;
321ffcc0
RS
1045 if (opt->name_only) {
1046 show_name(opt, name);
1047 return 1;
1048 }
c30c10cf
RS
1049 if (opt->count)
1050 goto next_line;
83b5d2f5 1051 if (binary_match_only) {
5b594f45 1052 opt->output(opt, "Binary file ", 12);
55f638bd
ML
1053 output_color(opt, name, strlen(name),
1054 opt->color_filename);
5b594f45 1055 opt->output(opt, " matches\n", 9);
83b5d2f5
JH
1056 return 1;
1057 }
83b5d2f5
JH
1058 /* Hit at this line. If we haven't shown the
1059 * pre-context lines, we would need to show them.
83b5d2f5 1060 */
ba8ea749
RS
1061 if (opt->pre_context || opt->funcbody)
1062 show_pre_context(opt, name, buf, bol, eol, lno);
2944e4e6
RS
1063 else if (opt->funcname)
1064 show_funcname_line(opt, name, buf, bol, lno);
c30c10cf 1065 show_line(opt, bol, eol, name, lno, ':');
5dd06d38 1066 last_hit = lno;
ba8ea749
RS
1067 if (opt->funcbody)
1068 show_function = 1;
1069 goto next_line;
83b5d2f5 1070 }
ba8ea749
RS
1071 if (show_function && match_funcname(opt, bol, eol))
1072 show_function = 0;
1073 if (show_function ||
1074 (last_hit && lno <= last_hit + opt->post_context)) {
83b5d2f5
JH
1075 /* If the last hit is within the post context,
1076 * we need to show this line.
1077 */
83b5d2f5 1078 show_line(opt, bol, eol, name, lno, '-');
83b5d2f5 1079 }
83b5d2f5
JH
1080
1081 next_line:
1082 bol = eol + 1;
1083 if (!left)
1084 break;
1085 left--;
1086 lno++;
1087 }
1088
0ab7befa
JH
1089 if (collect_hits)
1090 return 0;
b48fb5b6 1091
83b5d2f5
JH
1092 if (opt->status_only)
1093 return 0;
1094 if (opt->unmatch_name_only) {
1095 /* We did not see any hit, so we want to show this */
83caecca 1096 show_name(opt, name);
83b5d2f5
JH
1097 return 1;
1098 }
1099
60ecac98
RS
1100 xdiff_clear_find_func(&xecfg);
1101 opt->priv = NULL;
1102
83b5d2f5
JH
1103 /* NEEDSWORK:
1104 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1105 * which feels mostly useless but sometimes useful. Maybe
1106 * make it another option? For now suppress them.
1107 */
5b594f45
FK
1108 if (opt->count && count) {
1109 char buf[32];
55f638bd
ML
1110 output_color(opt, name, strlen(name), opt->color_filename);
1111 output_sep(opt, ':');
1112 snprintf(buf, sizeof(buf), "%u\n", count);
5b594f45 1113 opt->output(opt, buf, strlen(buf));
c30c10cf 1114 return 1;
5b594f45 1115 }
83b5d2f5
JH
1116 return !!last_hit;
1117}
1118
0ab7befa
JH
1119static void clr_hit_marker(struct grep_expr *x)
1120{
1121 /* All-hit markers are meaningful only at the very top level
1122 * OR node.
1123 */
1124 while (1) {
1125 x->hit = 0;
1126 if (x->node != GREP_NODE_OR)
1127 return;
1128 x->u.binary.left->hit = 0;
1129 x = x->u.binary.right;
1130 }
1131}
1132
1133static int chk_hit_marker(struct grep_expr *x)
1134{
1135 /* Top level nodes have hit markers. See if they all are hits */
1136 while (1) {
1137 if (x->node != GREP_NODE_OR)
1138 return x->hit;
1139 if (!x->u.binary.left->hit)
1140 return 0;
1141 x = x->u.binary.right;
1142 }
1143}
1144
1145int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size)
1146{
1147 /*
1148 * we do not have to do the two-pass grep when we do not check
1149 * buffer-wide "all-match".
1150 */
1151 if (!opt->all_match)
1152 return grep_buffer_1(opt, name, buf, size, 0);
1153
1154 /* Otherwise the toplevel "or" terms hit a bit differently.
1155 * We first clear hit markers from them.
1156 */
1157 clr_hit_marker(opt->pattern_expression);
1158 grep_buffer_1(opt, name, buf, size, 1);
1159
1160 if (!chk_hit_marker(opt->pattern_expression))
1161 return 0;
1162
1163 return grep_buffer_1(opt, name, buf, size, 0);
1164}