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