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