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