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