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