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