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