builtin/grep.c: make configuration callback more reusable
[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
07a7d656
JH
6static int grep_source_load(struct grep_source *gs);
7static int grep_source_is_binary(struct grep_source *gs);
8
9
fc456751
RS
10static struct grep_pat *create_grep_pat(const char *pat, size_t patlen,
11 const char *origin, int no,
12 enum grep_pat_token t,
13 enum grep_header_field field)
a4d7d2c6
JH
14{
15 struct grep_pat *p = xcalloc(1, sizeof(*p));
526a858a 16 p->pattern = xmemdupz(pat, patlen);
fc456751
RS
17 p->patternlen = patlen;
18 p->origin = origin;
19 p->no = no;
20 p->token = t;
a4d7d2c6 21 p->field = field;
fc456751
RS
22 return p;
23}
24
2b3873ff
RS
25static void do_append_grep_pat(struct grep_pat ***tail, struct grep_pat *p)
26{
27 **tail = p;
28 *tail = &p->next;
a4d7d2c6 29 p->next = NULL;
526a858a
RS
30
31 switch (p->token) {
32 case GREP_PATTERN: /* atom */
33 case GREP_PATTERN_HEAD:
34 case GREP_PATTERN_BODY:
35 for (;;) {
36 struct grep_pat *new_pat;
37 size_t len = 0;
38 char *cp = p->pattern + p->patternlen, *nl = NULL;
39 while (++len <= p->patternlen) {
40 if (*(--cp) == '\n') {
41 nl = cp;
42 break;
43 }
44 }
45 if (!nl)
46 break;
47 new_pat = create_grep_pat(nl + 1, len - 1, p->origin,
48 p->no, p->token, p->field);
49 new_pat->next = p->next;
50 if (!p->next)
51 *tail = &new_pat->next;
52 p->next = new_pat;
53 *nl = '\0';
54 p->patternlen -= len;
55 }
56 break;
57 default:
58 break;
59 }
2b3873ff
RS
60}
61
fc456751
RS
62void append_header_grep_pattern(struct grep_opt *opt,
63 enum grep_header_field field, const char *pat)
64{
65 struct grep_pat *p = create_grep_pat(pat, strlen(pat), "header", 0,
66 GREP_PATTERN_HEAD, field);
baa6378f
JH
67 if (field == GREP_HEADER_REFLOG)
68 opt->use_reflog_filter = 1;
2b3873ff 69 do_append_grep_pat(&opt->header_tail, p);
a4d7d2c6
JH
70}
71
83b5d2f5
JH
72void append_grep_pattern(struct grep_opt *opt, const char *pat,
73 const char *origin, int no, enum grep_pat_token t)
ed40a095
RS
74{
75 append_grep_pat(opt, pat, strlen(pat), origin, no, t);
76}
77
78void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen,
79 const char *origin, int no, enum grep_pat_token t)
83b5d2f5 80{
fc456751 81 struct grep_pat *p = create_grep_pat(pat, patlen, origin, no, t, 0);
2b3873ff 82 do_append_grep_pat(&opt->pattern_tail, p);
83b5d2f5
JH
83}
84
5b594f45
FK
85struct grep_opt *grep_opt_dup(const struct grep_opt *opt)
86{
87 struct grep_pat *pat;
88 struct grep_opt *ret = xmalloc(sizeof(struct grep_opt));
89 *ret = *opt;
90
91 ret->pattern_list = NULL;
92 ret->pattern_tail = &ret->pattern_list;
93
94 for(pat = opt->pattern_list; pat != NULL; pat = pat->next)
95 {
96 if(pat->token == GREP_PATTERN_HEAD)
97 append_header_grep_pattern(ret, pat->field,
98 pat->pattern);
99 else
ed40a095
RS
100 append_grep_pat(ret, pat->pattern, pat->patternlen,
101 pat->origin, pat->no, pat->token);
5b594f45
FK
102 }
103
104 return ret;
105}
106
a30c148a
MK
107static NORETURN void compile_regexp_failed(const struct grep_pat *p,
108 const char *error)
109{
110 char where[1024];
111
112 if (p->no)
113 sprintf(where, "In '%s' at %d, ", p->origin, p->no);
114 else if (p->origin)
115 sprintf(where, "%s, ", p->origin);
116 else
117 where[0] = 0;
118
119 die("%s'%s': %s", where, p->pattern, error);
120}
121
63e7e9d8
MK
122#ifdef USE_LIBPCRE
123static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
124{
125 const char *error;
126 int erroffset;
fba4f125 127 int options = PCRE_MULTILINE;
63e7e9d8
MK
128
129 if (opt->ignore_case)
130 options |= PCRE_CASELESS;
131
132 p->pcre_regexp = pcre_compile(p->pattern, options, &error, &erroffset,
133 NULL);
134 if (!p->pcre_regexp)
135 compile_regexp_failed(p, error);
136
137 p->pcre_extra_info = pcre_study(p->pcre_regexp, 0, &error);
138 if (!p->pcre_extra_info && error)
139 die("%s", error);
140}
141
142static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
143 regmatch_t *match, int eflags)
144{
145 int ovector[30], ret, flags = 0;
146
147 if (eflags & REG_NOTBOL)
148 flags |= PCRE_NOTBOL;
149
150 ret = pcre_exec(p->pcre_regexp, p->pcre_extra_info, line, eol - line,
151 0, flags, ovector, ARRAY_SIZE(ovector));
152 if (ret < 0 && ret != PCRE_ERROR_NOMATCH)
153 die("pcre_exec failed with error code %d", ret);
154 if (ret > 0) {
155 ret = 0;
156 match->rm_so = ovector[0];
157 match->rm_eo = ovector[1];
158 }
159
160 return ret;
161}
162
163static void free_pcre_regexp(struct grep_pat *p)
164{
165 pcre_free(p->pcre_regexp);
166 pcre_free(p->pcre_extra_info);
167}
168#else /* !USE_LIBPCRE */
169static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
170{
171 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
172}
173
174static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
175 regmatch_t *match, int eflags)
176{
177 return 1;
178}
179
180static void free_pcre_regexp(struct grep_pat *p)
181{
182}
183#endif /* !USE_LIBPCRE */
184
9eceddee
FK
185static int is_fixed(const char *s, size_t len)
186{
187 size_t i;
188
189 /* regcomp cannot accept patterns with NULs so we
190 * consider any pattern containing a NUL fixed.
191 */
192 if (memchr(s, 0, len))
193 return 1;
194
195 for (i = 0; i < len; i++) {
196 if (is_regex_special(s[i]))
197 return 0;
198 }
199
200 return 1;
201}
202
83b5d2f5
JH
203static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
204{
c822255c
RS
205 int err;
206
d7eb527d 207 p->word_regexp = opt->word_regexp;
5183bf67 208 p->ignore_case = opt->ignore_case;
d7eb527d 209
9eceddee
FK
210 if (opt->fixed || is_fixed(p->pattern, p->patternlen))
211 p->fixed = 1;
212 else
213 p->fixed = 0;
214
215 if (p->fixed) {
0f871cf5
JH
216 if (opt->regflags & REG_ICASE || p->ignore_case)
217 p->kws = kwsalloc(tolower_trans_tbl);
218 else
9eceddee 219 p->kws = kwsalloc(NULL);
9eceddee
FK
220 kwsincr(p->kws, p->pattern, p->patternlen);
221 kwsprep(p->kws);
c822255c 222 return;
9eceddee 223 }
c822255c 224
63e7e9d8
MK
225 if (opt->pcre) {
226 compile_pcre_regexp(p, opt);
227 return;
228 }
229
c822255c 230 err = regcomp(&p->regexp, p->pattern, opt->regflags);
83b5d2f5
JH
231 if (err) {
232 char errbuf[1024];
83b5d2f5
JH
233 regerror(err, &p->regexp, errbuf, 1024);
234 regfree(&p->regexp);
a30c148a 235 compile_regexp_failed(p, errbuf);
83b5d2f5
JH
236 }
237}
238
0ab7befa 239static struct grep_expr *compile_pattern_or(struct grep_pat **);
83b5d2f5
JH
240static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
241{
242 struct grep_pat *p;
243 struct grep_expr *x;
244
245 p = *list;
c922b01f
LT
246 if (!p)
247 return NULL;
83b5d2f5
JH
248 switch (p->token) {
249 case GREP_PATTERN: /* atom */
480c1ca6
JH
250 case GREP_PATTERN_HEAD:
251 case GREP_PATTERN_BODY:
83b5d2f5
JH
252 x = xcalloc(1, sizeof (struct grep_expr));
253 x->node = GREP_NODE_ATOM;
254 x->u.atom = p;
255 *list = p->next;
256 return x;
257 case GREP_OPEN_PAREN:
258 *list = p->next;
0ab7befa 259 x = compile_pattern_or(list);
83b5d2f5
JH
260 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
261 die("unmatched parenthesis");
262 *list = (*list)->next;
263 return x;
264 default:
265 return NULL;
266 }
267}
268
269static struct grep_expr *compile_pattern_not(struct grep_pat **list)
270{
271 struct grep_pat *p;
272 struct grep_expr *x;
273
274 p = *list;
c922b01f
LT
275 if (!p)
276 return NULL;
83b5d2f5
JH
277 switch (p->token) {
278 case GREP_NOT:
279 if (!p->next)
280 die("--not not followed by pattern expression");
281 *list = p->next;
282 x = xcalloc(1, sizeof (struct grep_expr));
283 x->node = GREP_NODE_NOT;
284 x->u.unary = compile_pattern_not(list);
285 if (!x->u.unary)
286 die("--not followed by non pattern expression");
287 return x;
288 default:
289 return compile_pattern_atom(list);
290 }
291}
292
293static struct grep_expr *compile_pattern_and(struct grep_pat **list)
294{
295 struct grep_pat *p;
296 struct grep_expr *x, *y, *z;
297
298 x = compile_pattern_not(list);
299 p = *list;
300 if (p && p->token == GREP_AND) {
301 if (!p->next)
302 die("--and not followed by pattern expression");
303 *list = p->next;
304 y = compile_pattern_and(list);
305 if (!y)
306 die("--and not followed by pattern expression");
307 z = xcalloc(1, sizeof (struct grep_expr));
308 z->node = GREP_NODE_AND;
309 z->u.binary.left = x;
310 z->u.binary.right = y;
311 return z;
312 }
313 return x;
314}
315
316static struct grep_expr *compile_pattern_or(struct grep_pat **list)
317{
318 struct grep_pat *p;
319 struct grep_expr *x, *y, *z;
320
321 x = compile_pattern_and(list);
322 p = *list;
323 if (x && p && p->token != GREP_CLOSE_PAREN) {
324 y = compile_pattern_or(list);
325 if (!y)
326 die("not a pattern expression %s", p->pattern);
327 z = xcalloc(1, sizeof (struct grep_expr));
328 z->node = GREP_NODE_OR;
329 z->u.binary.left = x;
330 z->u.binary.right = y;
331 return z;
332 }
333 return x;
334}
335
336static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
337{
338 return compile_pattern_or(list);
339}
340
17bf35a3
JH
341static void indent(int in)
342{
343 while (in-- > 0)
344 fputc(' ', stderr);
345}
346
347static void dump_grep_pat(struct grep_pat *p)
348{
349 switch (p->token) {
350 case GREP_AND: fprintf(stderr, "*and*"); break;
351 case GREP_OPEN_PAREN: fprintf(stderr, "*(*"); break;
352 case GREP_CLOSE_PAREN: fprintf(stderr, "*)*"); break;
353 case GREP_NOT: fprintf(stderr, "*not*"); break;
354 case GREP_OR: fprintf(stderr, "*or*"); break;
355
356 case GREP_PATTERN: fprintf(stderr, "pattern"); break;
357 case GREP_PATTERN_HEAD: fprintf(stderr, "pattern_head"); break;
358 case GREP_PATTERN_BODY: fprintf(stderr, "pattern_body"); break;
359 }
360
361 switch (p->token) {
362 default: break;
363 case GREP_PATTERN_HEAD:
364 fprintf(stderr, "<head %d>", p->field); break;
365 case GREP_PATTERN_BODY:
366 fprintf(stderr, "<body>"); break;
367 }
368 switch (p->token) {
369 default: break;
370 case GREP_PATTERN_HEAD:
371 case GREP_PATTERN_BODY:
372 case GREP_PATTERN:
373 fprintf(stderr, "%.*s", (int)p->patternlen, p->pattern);
374 break;
375 }
376 fputc('\n', stderr);
377}
378
379static void dump_grep_expression_1(struct grep_expr *x, int in)
380{
381 indent(in);
382 switch (x->node) {
383 case GREP_NODE_TRUE:
384 fprintf(stderr, "true\n");
385 break;
386 case GREP_NODE_ATOM:
387 dump_grep_pat(x->u.atom);
388 break;
389 case GREP_NODE_NOT:
390 fprintf(stderr, "(not\n");
391 dump_grep_expression_1(x->u.unary, in+1);
392 indent(in);
393 fprintf(stderr, ")\n");
394 break;
395 case GREP_NODE_AND:
396 fprintf(stderr, "(and\n");
397 dump_grep_expression_1(x->u.binary.left, in+1);
398 dump_grep_expression_1(x->u.binary.right, in+1);
399 indent(in);
400 fprintf(stderr, ")\n");
401 break;
402 case GREP_NODE_OR:
403 fprintf(stderr, "(or\n");
404 dump_grep_expression_1(x->u.binary.left, in+1);
405 dump_grep_expression_1(x->u.binary.right, in+1);
406 indent(in);
407 fprintf(stderr, ")\n");
408 break;
409 }
410}
411
07a7d656 412static void dump_grep_expression(struct grep_opt *opt)
17bf35a3
JH
413{
414 struct grep_expr *x = opt->pattern_expression;
415
416 if (opt->all_match)
417 fprintf(stderr, "[all-match]\n");
418 dump_grep_expression_1(x, 0);
419 fflush(NULL);
420}
421
5aaeb733
JH
422static struct grep_expr *grep_true_expr(void)
423{
424 struct grep_expr *z = xcalloc(1, sizeof(*z));
425 z->node = GREP_NODE_TRUE;
426 return z;
427}
428
429static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
430{
431 struct grep_expr *z = xcalloc(1, sizeof(*z));
432 z->node = GREP_NODE_OR;
433 z->u.binary.left = left;
434 z->u.binary.right = right;
435 return z;
436}
437
95ce9ce2 438static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
83b5d2f5
JH
439{
440 struct grep_pat *p;
95ce9ce2 441 struct grep_expr *header_expr;
5aaeb733
JH
442 struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]);
443 enum grep_header_field fld;
83b5d2f5 444
95ce9ce2
JH
445 if (!opt->header_list)
446 return NULL;
2385f246 447
95ce9ce2
JH
448 for (p = opt->header_list; p; p = p->next) {
449 if (p->token != GREP_PATTERN_HEAD)
450 die("bug: a non-header pattern in grep header list.");
451 if (p->field < 0 || GREP_HEADER_FIELD_MAX <= p->field)
452 die("bug: unknown header field %d", p->field);
453 compile_regexp(p, opt);
80235ba7 454 }
5aaeb733
JH
455
456 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++)
457 header_group[fld] = NULL;
458
459 for (p = opt->header_list; p; p = p->next) {
460 struct grep_expr *h;
461 struct grep_pat *pp = p;
462
463 h = compile_pattern_atom(&pp);
464 if (!h || pp != p->next)
465 die("bug: malformed header expr");
466 if (!header_group[p->field]) {
467 header_group[p->field] = h;
468 continue;
469 }
470 header_group[p->field] = grep_or_expr(h, header_group[p->field]);
471 }
472
473 header_expr = NULL;
474
475 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) {
476 if (!header_group[fld])
477 continue;
478 if (!header_expr)
479 header_expr = grep_true_expr();
480 header_expr = grep_or_expr(header_group[fld], header_expr);
481 }
95ce9ce2
JH
482 return header_expr;
483}
484
13e4fc7e
JH
485static struct grep_expr *grep_splice_or(struct grep_expr *x, struct grep_expr *y)
486{
487 struct grep_expr *z = x;
488
489 while (x) {
490 assert(x->node == GREP_NODE_OR);
491 if (x->u.binary.right &&
492 x->u.binary.right->node == GREP_NODE_TRUE) {
493 x->u.binary.right = y;
494 break;
495 }
496 x = x->u.binary.right;
497 }
498 return z;
499}
500
17bf35a3 501static void compile_grep_patterns_real(struct grep_opt *opt)
95ce9ce2
JH
502{
503 struct grep_pat *p;
504 struct grep_expr *header_expr = prep_header_patterns(opt);
0ab7befa 505
83b5d2f5 506 for (p = opt->pattern_list; p; p = p->next) {
480c1ca6
JH
507 switch (p->token) {
508 case GREP_PATTERN: /* atom */
509 case GREP_PATTERN_HEAD:
510 case GREP_PATTERN_BODY:
c822255c 511 compile_regexp(p, opt);
480c1ca6
JH
512 break;
513 default:
83b5d2f5 514 opt->extended = 1;
480c1ca6
JH
515 break;
516 }
83b5d2f5
JH
517 }
518
80235ba7
JH
519 if (opt->all_match || header_expr)
520 opt->extended = 1;
17bf35a3 521 else if (!opt->extended && !opt->debug)
83b5d2f5
JH
522 return;
523
83b5d2f5 524 p = opt->pattern_list;
ba150a3f
MB
525 if (p)
526 opt->pattern_expression = compile_pattern_expr(&p);
83b5d2f5
JH
527 if (p)
528 die("incomplete pattern expression: %s", p->pattern);
80235ba7
JH
529
530 if (!header_expr)
531 return;
532
5aaeb733 533 if (!opt->pattern_expression)
80235ba7 534 opt->pattern_expression = header_expr;
13e4fc7e
JH
535 else if (opt->all_match)
536 opt->pattern_expression = grep_splice_or(header_expr,
537 opt->pattern_expression);
5aaeb733
JH
538 else
539 opt->pattern_expression = grep_or_expr(opt->pattern_expression,
540 header_expr);
80235ba7 541 opt->all_match = 1;
83b5d2f5
JH
542}
543
17bf35a3
JH
544void compile_grep_patterns(struct grep_opt *opt)
545{
546 compile_grep_patterns_real(opt);
547 if (opt->debug)
548 dump_grep_expression(opt);
549}
550
b48fb5b6
JH
551static void free_pattern_expr(struct grep_expr *x)
552{
553 switch (x->node) {
5aaeb733 554 case GREP_NODE_TRUE:
b48fb5b6
JH
555 case GREP_NODE_ATOM:
556 break;
557 case GREP_NODE_NOT:
558 free_pattern_expr(x->u.unary);
559 break;
560 case GREP_NODE_AND:
561 case GREP_NODE_OR:
562 free_pattern_expr(x->u.binary.left);
563 free_pattern_expr(x->u.binary.right);
564 break;
565 }
566 free(x);
567}
568
569void free_grep_patterns(struct grep_opt *opt)
570{
571 struct grep_pat *p, *n;
572
573 for (p = opt->pattern_list; p; p = n) {
574 n = p->next;
575 switch (p->token) {
576 case GREP_PATTERN: /* atom */
577 case GREP_PATTERN_HEAD:
578 case GREP_PATTERN_BODY:
9eceddee
FK
579 if (p->kws)
580 kwsfree(p->kws);
581 else if (p->pcre_regexp)
63e7e9d8
MK
582 free_pcre_regexp(p);
583 else
584 regfree(&p->regexp);
526a858a 585 free(p->pattern);
b48fb5b6
JH
586 break;
587 default:
588 break;
589 }
590 free(p);
591 }
592
593 if (!opt->extended)
594 return;
595 free_pattern_expr(opt->pattern_expression);
596}
597
83b5d2f5
JH
598static char *end_of_line(char *cp, unsigned long *left)
599{
600 unsigned long l = *left;
601 while (l && *cp != '\n') {
602 l--;
603 cp++;
604 }
605 *left = l;
606 return cp;
607}
608
609static int word_char(char ch)
610{
611 return isalnum(ch) || ch == '_';
612}
613
55f638bd
ML
614static void output_color(struct grep_opt *opt, const void *data, size_t size,
615 const char *color)
616{
daa0c3d9 617 if (want_color(opt->color) && color && color[0]) {
55f638bd
ML
618 opt->output(opt, color, strlen(color));
619 opt->output(opt, data, size);
620 opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
621 } else
622 opt->output(opt, data, size);
623}
624
625static void output_sep(struct grep_opt *opt, char sign)
626{
627 if (opt->null_following_name)
628 opt->output(opt, "\0", 1);
629 else
630 output_color(opt, &sign, 1, opt->color_sep);
631}
632
83caecca
RZ
633static void show_name(struct grep_opt *opt, const char *name)
634{
55f638bd 635 output_color(opt, name, strlen(name), opt->color_filename);
5b594f45 636 opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
83caecca
RZ
637}
638
ed40a095
RS
639static int fixmatch(struct grep_pat *p, char *line, char *eol,
640 regmatch_t *match)
83b5d2f5 641{
9eceddee
FK
642 struct kwsmatch kwsm;
643 size_t offset = kwsexec(p->kws, line, eol - line, &kwsm);
644 if (offset == -1) {
83b5d2f5
JH
645 match->rm_so = match->rm_eo = -1;
646 return REG_NOMATCH;
9eceddee
FK
647 } else {
648 match->rm_so = offset;
649 match->rm_eo = match->rm_so + kwsm.size[0];
83b5d2f5
JH
650 return 0;
651 }
652}
653
f96e5673
RS
654static int regmatch(const regex_t *preg, char *line, char *eol,
655 regmatch_t *match, int eflags)
656{
657#ifdef REG_STARTEND
658 match->rm_so = 0;
659 match->rm_eo = eol - line;
660 eflags |= REG_STARTEND;
661#endif
662 return regexec(preg, line, 1, match, eflags);
663}
664
97e77784
MK
665static int patmatch(struct grep_pat *p, char *line, char *eol,
666 regmatch_t *match, int eflags)
667{
668 int hit;
669
670 if (p->fixed)
671 hit = !fixmatch(p, line, eol, match);
63e7e9d8
MK
672 else if (p->pcre_regexp)
673 hit = !pcrematch(p, line, eol, match, eflags);
97e77784
MK
674 else
675 hit = !regmatch(&p->regexp, line, eol, match, eflags);
676
677 return hit;
678}
679
a4d7d2c6
JH
680static int strip_timestamp(char *bol, char **eol_p)
681{
682 char *eol = *eol_p;
683 int ch;
684
685 while (bol < --eol) {
686 if (*eol != '>')
687 continue;
688 *eol_p = ++eol;
689 ch = *eol;
690 *eol = '\0';
691 return ch;
692 }
693 return 0;
694}
695
696static struct {
697 const char *field;
698 size_t len;
699} header_field[] = {
700 { "author ", 7 },
701 { "committer ", 10 },
72fd13f7 702 { "reflog ", 7 },
a4d7d2c6
JH
703};
704
d7eb527d 705static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
79212772
RS
706 enum grep_context ctx,
707 regmatch_t *pmatch, int eflags)
83b5d2f5
JH
708{
709 int hit = 0;
a4d7d2c6 710 int saved_ch = 0;
e701fadb 711 const char *start = bol;
83b5d2f5 712
480c1ca6
JH
713 if ((p->token != GREP_PATTERN) &&
714 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
715 return 0;
716
a4d7d2c6
JH
717 if (p->token == GREP_PATTERN_HEAD) {
718 const char *field;
719 size_t len;
720 assert(p->field < ARRAY_SIZE(header_field));
721 field = header_field[p->field].field;
722 len = header_field[p->field].len;
723 if (strncmp(bol, field, len))
724 return 0;
725 bol += len;
ad4813b3
NTND
726 switch (p->field) {
727 case GREP_HEADER_AUTHOR:
728 case GREP_HEADER_COMMITTER:
729 saved_ch = strip_timestamp(bol, &eol);
730 break;
731 default:
732 break;
733 }
a4d7d2c6
JH
734 }
735
83b5d2f5 736 again:
97e77784 737 hit = patmatch(p, bol, eol, pmatch, eflags);
83b5d2f5 738
d7eb527d 739 if (hit && p->word_regexp) {
83b5d2f5 740 if ((pmatch[0].rm_so < 0) ||
84201eae 741 (eol - bol) < pmatch[0].rm_so ||
83b5d2f5
JH
742 (pmatch[0].rm_eo < 0) ||
743 (eol - bol) < pmatch[0].rm_eo)
744 die("regexp returned nonsense");
745
746 /* Match beginning must be either beginning of the
747 * line, or at word boundary (i.e. the last char must
748 * not be a word char). Similarly, match end must be
749 * either end of the line, or at word boundary
750 * (i.e. the next char must not be a word char).
751 */
fb62eb7f 752 if ( ((pmatch[0].rm_so == 0) ||
83b5d2f5
JH
753 !word_char(bol[pmatch[0].rm_so-1])) &&
754 ((pmatch[0].rm_eo == (eol-bol)) ||
755 !word_char(bol[pmatch[0].rm_eo])) )
756 ;
757 else
758 hit = 0;
759
84201eae
RS
760 /* Words consist of at least one character. */
761 if (pmatch->rm_so == pmatch->rm_eo)
762 hit = 0;
763
83b5d2f5
JH
764 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
765 /* There could be more than one match on the
766 * line, and the first match might not be
767 * strict word match. But later ones could be!
fb62eb7f
RS
768 * Forward to the next possible start, i.e. the
769 * next position following a non-word char.
83b5d2f5
JH
770 */
771 bol = pmatch[0].rm_so + bol + 1;
fb62eb7f
RS
772 while (word_char(bol[-1]) && bol < eol)
773 bol++;
dbb6a4ad 774 eflags |= REG_NOTBOL;
fb62eb7f
RS
775 if (bol < eol)
776 goto again;
83b5d2f5
JH
777 }
778 }
a4d7d2c6
JH
779 if (p->token == GREP_PATTERN_HEAD && saved_ch)
780 *eol = saved_ch;
e701fadb
RS
781 if (hit) {
782 pmatch[0].rm_so += bol - start;
783 pmatch[0].rm_eo += bol - start;
784 }
83b5d2f5
JH
785 return hit;
786}
787
d7eb527d
RS
788static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
789 enum grep_context ctx, int collect_hits)
83b5d2f5 790{
0ab7befa 791 int h = 0;
79212772 792 regmatch_t match;
0ab7befa 793
c922b01f
LT
794 if (!x)
795 die("Not a valid grep expression");
83b5d2f5 796 switch (x->node) {
5aaeb733
JH
797 case GREP_NODE_TRUE:
798 h = 1;
799 break;
83b5d2f5 800 case GREP_NODE_ATOM:
79212772 801 h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
83b5d2f5
JH
802 break;
803 case GREP_NODE_NOT:
d7eb527d 804 h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
0ab7befa 805 break;
83b5d2f5 806 case GREP_NODE_AND:
d7eb527d 807 if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
252d560d 808 return 0;
d7eb527d 809 h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
0ab7befa 810 break;
83b5d2f5 811 case GREP_NODE_OR:
0ab7befa 812 if (!collect_hits)
d7eb527d 813 return (match_expr_eval(x->u.binary.left,
0ab7befa 814 bol, eol, ctx, 0) ||
d7eb527d 815 match_expr_eval(x->u.binary.right,
0ab7befa 816 bol, eol, ctx, 0));
d7eb527d 817 h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
0ab7befa 818 x->u.binary.left->hit |= h;
d7eb527d 819 h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
0ab7befa
JH
820 break;
821 default:
d7530708 822 die("Unexpected node type (internal error) %d", x->node);
83b5d2f5 823 }
0ab7befa
JH
824 if (collect_hits)
825 x->hit |= h;
826 return h;
83b5d2f5
JH
827}
828
480c1ca6 829static int match_expr(struct grep_opt *opt, char *bol, char *eol,
0ab7befa 830 enum grep_context ctx, int collect_hits)
83b5d2f5
JH
831{
832 struct grep_expr *x = opt->pattern_expression;
d7eb527d 833 return match_expr_eval(x, bol, eol, ctx, collect_hits);
83b5d2f5
JH
834}
835
480c1ca6 836static int match_line(struct grep_opt *opt, char *bol, char *eol,
0ab7befa 837 enum grep_context ctx, int collect_hits)
83b5d2f5
JH
838{
839 struct grep_pat *p;
79212772
RS
840 regmatch_t match;
841
83b5d2f5 842 if (opt->extended)
0ab7befa
JH
843 return match_expr(opt, bol, eol, ctx, collect_hits);
844
845 /* we do not call with collect_hits without being extended */
83b5d2f5 846 for (p = opt->pattern_list; p; p = p->next) {
79212772 847 if (match_one_pattern(p, bol, eol, ctx, &match, 0))
83b5d2f5
JH
848 return 1;
849 }
850 return 0;
851}
852
7e8f59d5
RS
853static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
854 enum grep_context ctx,
855 regmatch_t *pmatch, int eflags)
856{
857 regmatch_t match;
858
859 if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
860 return 0;
861 if (match.rm_so < 0 || match.rm_eo < 0)
862 return 0;
863 if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
864 if (match.rm_so > pmatch->rm_so)
865 return 1;
866 if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
867 return 1;
868 }
869 pmatch->rm_so = match.rm_so;
870 pmatch->rm_eo = match.rm_eo;
871 return 1;
872}
873
874static int next_match(struct grep_opt *opt, char *bol, char *eol,
875 enum grep_context ctx, regmatch_t *pmatch, int eflags)
876{
877 struct grep_pat *p;
878 int hit = 0;
879
880 pmatch->rm_so = pmatch->rm_eo = -1;
881 if (bol < eol) {
882 for (p = opt->pattern_list; p; p = p->next) {
883 switch (p->token) {
884 case GREP_PATTERN: /* atom */
885 case GREP_PATTERN_HEAD:
886 case GREP_PATTERN_BODY:
887 hit |= match_next_pattern(p, bol, eol, ctx,
888 pmatch, eflags);
889 break;
890 default:
891 break;
892 }
893 }
894 }
895 return hit;
896}
897
898static void show_line(struct grep_opt *opt, char *bol, char *eol,
899 const char *name, unsigned lno, char sign)
900{
901 int rest = eol - bol;
00588bb5 902 char *line_color = NULL;
7e8f59d5 903
a8f0e764
RS
904 if (opt->file_break && opt->last_shown == 0) {
905 if (opt->show_hunk_mark)
906 opt->output(opt, "\n", 1);
ba8ea749 907 } else if (opt->pre_context || opt->post_context || opt->funcbody) {
046802d0 908 if (opt->last_shown == 0) {
55f638bd
ML
909 if (opt->show_hunk_mark) {
910 output_color(opt, "--", 2, opt->color_sep);
911 opt->output(opt, "\n", 1);
07b838f0 912 }
55f638bd
ML
913 } else if (lno > opt->last_shown + 1) {
914 output_color(opt, "--", 2, opt->color_sep);
915 opt->output(opt, "\n", 1);
916 }
5dd06d38 917 }
1d84f72e
RS
918 if (opt->heading && opt->last_shown == 0) {
919 output_color(opt, name, strlen(name), opt->color_filename);
920 opt->output(opt, "\n", 1);
921 }
5dd06d38
RS
922 opt->last_shown = lno;
923
1d84f72e 924 if (!opt->heading && opt->pathname) {
55f638bd
ML
925 output_color(opt, name, strlen(name), opt->color_filename);
926 output_sep(opt, sign);
5b594f45
FK
927 }
928 if (opt->linenum) {
929 char buf[32];
930 snprintf(buf, sizeof(buf), "%d", lno);
55f638bd
ML
931 output_color(opt, buf, strlen(buf), opt->color_lineno);
932 output_sep(opt, sign);
5b594f45 933 }
7e8f59d5
RS
934 if (opt->color) {
935 regmatch_t match;
936 enum grep_context ctx = GREP_CONTEXT_BODY;
937 int ch = *eol;
938 int eflags = 0;
939
00588bb5
ML
940 if (sign == ':')
941 line_color = opt->color_selected;
942 else if (sign == '-')
943 line_color = opt->color_context;
944 else if (sign == '=')
945 line_color = opt->color_function;
7e8f59d5
RS
946 *eol = '\0';
947 while (next_match(opt, bol, eol, ctx, &match, eflags)) {
1f5b9cc4
RS
948 if (match.rm_so == match.rm_eo)
949 break;
5b594f45 950
00588bb5 951 output_color(opt, bol, match.rm_so, line_color);
55f638bd
ML
952 output_color(opt, bol + match.rm_so,
953 match.rm_eo - match.rm_so,
954 opt->color_match);
7e8f59d5
RS
955 bol += match.rm_eo;
956 rest -= match.rm_eo;
957 eflags = REG_NOTBOL;
958 }
959 *eol = ch;
960 }
00588bb5 961 output_color(opt, bol, rest, line_color);
5b594f45 962 opt->output(opt, "\n", 1);
7e8f59d5
RS
963}
964
0579f91d 965#ifndef NO_PTHREADS
78db6ea9
JK
966int grep_use_locks;
967
0579f91d
TR
968/*
969 * This lock protects access to the gitattributes machinery, which is
970 * not thread-safe.
971 */
972pthread_mutex_t grep_attr_mutex;
973
78db6ea9 974static inline void grep_attr_lock(void)
0579f91d 975{
78db6ea9 976 if (grep_use_locks)
0579f91d
TR
977 pthread_mutex_lock(&grep_attr_mutex);
978}
979
78db6ea9 980static inline void grep_attr_unlock(void)
0579f91d 981{
78db6ea9 982 if (grep_use_locks)
0579f91d
TR
983 pthread_mutex_unlock(&grep_attr_mutex);
984}
b3aeb285
JK
985
986/*
987 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
988 */
989pthread_mutex_t grep_read_mutex;
990
0579f91d 991#else
78db6ea9
JK
992#define grep_attr_lock()
993#define grep_attr_unlock()
0579f91d
TR
994#endif
995
e1327023 996static int match_funcname(struct grep_opt *opt, struct grep_source *gs, char *bol, char *eol)
2944e4e6 997{
60ecac98 998 xdemitconf_t *xecfg = opt->priv;
0579f91d 999 if (xecfg && !xecfg->find_func) {
94ad9d9e
JK
1000 grep_source_load_driver(gs);
1001 if (gs->driver->funcname.pattern) {
1002 const struct userdiff_funcname *pe = &gs->driver->funcname;
0579f91d
TR
1003 xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
1004 } else {
1005 xecfg = opt->priv = NULL;
1006 }
1007 }
1008
1009 if (xecfg) {
60ecac98
RS
1010 char buf[1];
1011 return xecfg->find_func(bol, eol - bol, buf, 1,
1012 xecfg->find_func_priv) >= 0;
1013 }
1014
2944e4e6
RS
1015 if (bol == eol)
1016 return 0;
1017 if (isalpha(*bol) || *bol == '_' || *bol == '$')
1018 return 1;
1019 return 0;
1020}
1021
e1327023
JK
1022static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
1023 char *bol, unsigned lno)
2944e4e6 1024{
e1327023 1025 while (bol > gs->buf) {
2944e4e6
RS
1026 char *eol = --bol;
1027
e1327023 1028 while (bol > gs->buf && bol[-1] != '\n')
2944e4e6
RS
1029 bol--;
1030 lno--;
1031
1032 if (lno <= opt->last_shown)
1033 break;
1034
e1327023
JK
1035 if (match_funcname(opt, gs, bol, eol)) {
1036 show_line(opt, bol, eol, gs->name, lno, '=');
2944e4e6
RS
1037 break;
1038 }
1039 }
1040}
1041
e1327023 1042static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
ba8ea749 1043 char *bol, char *end, unsigned lno)
49de3216 1044{
2944e4e6 1045 unsigned cur = lno, from = 1, funcname_lno = 0;
ba8ea749
RS
1046 int funcname_needed = !!opt->funcname;
1047
e1327023 1048 if (opt->funcbody && !match_funcname(opt, gs, bol, end))
ba8ea749 1049 funcname_needed = 2;
49de3216
RS
1050
1051 if (opt->pre_context < lno)
1052 from = lno - opt->pre_context;
1053 if (from <= opt->last_shown)
1054 from = opt->last_shown + 1;
1055
1056 /* Rewind. */
e1327023 1057 while (bol > gs->buf &&
ba8ea749 1058 cur > (funcname_needed == 2 ? opt->last_shown + 1 : from)) {
2944e4e6
RS
1059 char *eol = --bol;
1060
e1327023 1061 while (bol > gs->buf && bol[-1] != '\n')
49de3216
RS
1062 bol--;
1063 cur--;
e1327023 1064 if (funcname_needed && match_funcname(opt, gs, bol, eol)) {
2944e4e6
RS
1065 funcname_lno = cur;
1066 funcname_needed = 0;
1067 }
49de3216
RS
1068 }
1069
2944e4e6
RS
1070 /* We need to look even further back to find a function signature. */
1071 if (opt->funcname && funcname_needed)
e1327023 1072 show_funcname_line(opt, gs, bol, cur);
2944e4e6 1073
49de3216
RS
1074 /* Back forward. */
1075 while (cur < lno) {
2944e4e6 1076 char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
49de3216
RS
1077
1078 while (*eol != '\n')
1079 eol++;
e1327023 1080 show_line(opt, bol, eol, gs->name, cur, sign);
49de3216
RS
1081 bol = eol + 1;
1082 cur++;
1083 }
1084}
1085
a26345b6
JH
1086static int should_lookahead(struct grep_opt *opt)
1087{
1088 struct grep_pat *p;
1089
1090 if (opt->extended)
1091 return 0; /* punt for too complex stuff */
1092 if (opt->invert)
1093 return 0;
1094 for (p = opt->pattern_list; p; p = p->next) {
1095 if (p->token != GREP_PATTERN)
1096 return 0; /* punt for "header only" and stuff */
1097 }
1098 return 1;
1099}
1100
1101static int look_ahead(struct grep_opt *opt,
1102 unsigned long *left_p,
1103 unsigned *lno_p,
1104 char **bol_p)
1105{
1106 unsigned lno = *lno_p;
1107 char *bol = *bol_p;
1108 struct grep_pat *p;
1109 char *sp, *last_bol;
1110 regoff_t earliest = -1;
1111
1112 for (p = opt->pattern_list; p; p = p->next) {
1113 int hit;
1114 regmatch_t m;
1115
97e77784 1116 hit = patmatch(p, bol, bol + *left_p, &m, 0);
a26345b6
JH
1117 if (!hit || m.rm_so < 0 || m.rm_eo < 0)
1118 continue;
1119 if (earliest < 0 || m.rm_so < earliest)
1120 earliest = m.rm_so;
1121 }
1122
1123 if (earliest < 0) {
1124 *bol_p = bol + *left_p;
1125 *left_p = 0;
1126 return 1;
1127 }
1128 for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
1129 ; /* find the beginning of the line */
1130 last_bol = sp;
1131
1132 for (sp = bol; sp < last_bol; sp++) {
1133 if (*sp == '\n')
1134 lno++;
1135 }
1136 *left_p -= last_bol - bol;
1137 *bol_p = last_bol;
1138 *lno_p = lno;
1139 return 0;
1140}
1141
5b594f45
FK
1142static void std_output(struct grep_opt *opt, const void *buf, size_t size)
1143{
1144 fwrite(buf, size, 1, stdout);
1145}
1146
e1327023 1147static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits)
83b5d2f5 1148{
e1327023
JK
1149 char *bol;
1150 unsigned long left;
83b5d2f5 1151 unsigned lno = 1;
83b5d2f5 1152 unsigned last_hit = 0;
83b5d2f5 1153 int binary_match_only = 0;
83b5d2f5 1154 unsigned count = 0;
a26345b6 1155 int try_lookahead = 0;
ba8ea749 1156 int show_function = 0;
480c1ca6 1157 enum grep_context ctx = GREP_CONTEXT_HEAD;
60ecac98 1158 xdemitconf_t xecfg;
83b5d2f5 1159
5b594f45
FK
1160 if (!opt->output)
1161 opt->output = std_output;
1162
ba8ea749
RS
1163 if (opt->pre_context || opt->post_context || opt->file_break ||
1164 opt->funcbody) {
08303c36
RS
1165 /* Show hunk marks, except for the first file. */
1166 if (opt->last_shown)
1167 opt->show_hunk_mark = 1;
1168 /*
1169 * If we're using threads then we can't easily identify
1170 * the first file. Always put hunk marks in that case
1171 * and skip the very first one later in work_done().
1172 */
1173 if (opt->output != std_output)
1174 opt->show_hunk_mark = 1;
1175 }
431d6e7b
RS
1176 opt->last_shown = 0;
1177
64fcec78
RS
1178 switch (opt->binary) {
1179 case GREP_BINARY_DEFAULT:
41b59bfc 1180 if (grep_source_is_binary(gs))
83b5d2f5 1181 binary_match_only = 1;
64fcec78
RS
1182 break;
1183 case GREP_BINARY_NOMATCH:
41b59bfc 1184 if (grep_source_is_binary(gs))
83b5d2f5 1185 return 0; /* Assume unmatch */
64fcec78
RS
1186 break;
1187 case GREP_BINARY_TEXT:
1188 break;
1189 default:
1190 die("bug: unknown binary handling mode");
83b5d2f5
JH
1191 }
1192
60ecac98 1193 memset(&xecfg, 0, sizeof(xecfg));
0579f91d
TR
1194 opt->priv = &xecfg;
1195
a26345b6 1196 try_lookahead = should_lookahead(opt);
60ecac98 1197
08265798
JK
1198 if (grep_source_load(gs) < 0)
1199 return 0;
1200
e1327023
JK
1201 bol = gs->buf;
1202 left = gs->size;
83b5d2f5
JH
1203 while (left) {
1204 char *eol, ch;
0ab7befa 1205 int hit;
83b5d2f5 1206
a26345b6 1207 /*
8997da38 1208 * look_ahead() skips quickly to the line that possibly
a26345b6
JH
1209 * has the next hit; don't call it if we need to do
1210 * something more than just skipping the current line
1211 * in response to an unmatch for the current line. E.g.
1212 * inside a post-context window, we will show the current
1213 * line as a context around the previous hit when it
1214 * doesn't hit.
1215 */
1216 if (try_lookahead
1217 && !(last_hit
ba8ea749
RS
1218 && (show_function ||
1219 lno <= last_hit + opt->post_context))
a26345b6
JH
1220 && look_ahead(opt, &left, &lno, &bol))
1221 break;
83b5d2f5
JH
1222 eol = end_of_line(bol, &left);
1223 ch = *eol;
1224 *eol = 0;
1225
480c1ca6
JH
1226 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
1227 ctx = GREP_CONTEXT_BODY;
1228
0ab7befa 1229 hit = match_line(opt, bol, eol, ctx, collect_hits);
83b5d2f5
JH
1230 *eol = ch;
1231
0ab7befa
JH
1232 if (collect_hits)
1233 goto next_line;
1234
83b5d2f5
JH
1235 /* "grep -v -e foo -e bla" should list lines
1236 * that do not have either, so inversion should
1237 * be done outside.
1238 */
1239 if (opt->invert)
1240 hit = !hit;
1241 if (opt->unmatch_name_only) {
1242 if (hit)
1243 return 0;
1244 goto next_line;
1245 }
1246 if (hit) {
1247 count++;
1248 if (opt->status_only)
1249 return 1;
321ffcc0 1250 if (opt->name_only) {
e1327023 1251 show_name(opt, gs->name);
321ffcc0
RS
1252 return 1;
1253 }
c30c10cf
RS
1254 if (opt->count)
1255 goto next_line;
83b5d2f5 1256 if (binary_match_only) {
5b594f45 1257 opt->output(opt, "Binary file ", 12);
e1327023 1258 output_color(opt, gs->name, strlen(gs->name),
55f638bd 1259 opt->color_filename);
5b594f45 1260 opt->output(opt, " matches\n", 9);
83b5d2f5
JH
1261 return 1;
1262 }
83b5d2f5
JH
1263 /* Hit at this line. If we haven't shown the
1264 * pre-context lines, we would need to show them.
83b5d2f5 1265 */
ba8ea749 1266 if (opt->pre_context || opt->funcbody)
e1327023 1267 show_pre_context(opt, gs, bol, eol, lno);
2944e4e6 1268 else if (opt->funcname)
e1327023
JK
1269 show_funcname_line(opt, gs, bol, lno);
1270 show_line(opt, bol, eol, gs->name, lno, ':');
5dd06d38 1271 last_hit = lno;
ba8ea749
RS
1272 if (opt->funcbody)
1273 show_function = 1;
1274 goto next_line;
83b5d2f5 1275 }
e1327023 1276 if (show_function && match_funcname(opt, gs, bol, eol))
ba8ea749
RS
1277 show_function = 0;
1278 if (show_function ||
1279 (last_hit && lno <= last_hit + opt->post_context)) {
83b5d2f5
JH
1280 /* If the last hit is within the post context,
1281 * we need to show this line.
1282 */
e1327023 1283 show_line(opt, bol, eol, gs->name, lno, '-');
83b5d2f5 1284 }
83b5d2f5
JH
1285
1286 next_line:
1287 bol = eol + 1;
1288 if (!left)
1289 break;
1290 left--;
1291 lno++;
1292 }
1293
0ab7befa
JH
1294 if (collect_hits)
1295 return 0;
b48fb5b6 1296
83b5d2f5
JH
1297 if (opt->status_only)
1298 return 0;
1299 if (opt->unmatch_name_only) {
1300 /* We did not see any hit, so we want to show this */
e1327023 1301 show_name(opt, gs->name);
83b5d2f5
JH
1302 return 1;
1303 }
1304
60ecac98
RS
1305 xdiff_clear_find_func(&xecfg);
1306 opt->priv = NULL;
1307
83b5d2f5
JH
1308 /* NEEDSWORK:
1309 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1310 * which feels mostly useless but sometimes useful. Maybe
1311 * make it another option? For now suppress them.
1312 */
5b594f45
FK
1313 if (opt->count && count) {
1314 char buf[32];
e1327023 1315 output_color(opt, gs->name, strlen(gs->name), opt->color_filename);
55f638bd
ML
1316 output_sep(opt, ':');
1317 snprintf(buf, sizeof(buf), "%u\n", count);
5b594f45 1318 opt->output(opt, buf, strlen(buf));
c30c10cf 1319 return 1;
5b594f45 1320 }
83b5d2f5
JH
1321 return !!last_hit;
1322}
1323
0ab7befa
JH
1324static void clr_hit_marker(struct grep_expr *x)
1325{
1326 /* All-hit markers are meaningful only at the very top level
1327 * OR node.
1328 */
1329 while (1) {
1330 x->hit = 0;
1331 if (x->node != GREP_NODE_OR)
1332 return;
1333 x->u.binary.left->hit = 0;
1334 x = x->u.binary.right;
1335 }
1336}
1337
1338static int chk_hit_marker(struct grep_expr *x)
1339{
1340 /* Top level nodes have hit markers. See if they all are hits */
1341 while (1) {
1342 if (x->node != GREP_NODE_OR)
1343 return x->hit;
1344 if (!x->u.binary.left->hit)
1345 return 0;
1346 x = x->u.binary.right;
1347 }
1348}
1349
e1327023 1350int grep_source(struct grep_opt *opt, struct grep_source *gs)
0ab7befa
JH
1351{
1352 /*
1353 * we do not have to do the two-pass grep when we do not check
1354 * buffer-wide "all-match".
1355 */
1356 if (!opt->all_match)
e1327023 1357 return grep_source_1(opt, gs, 0);
0ab7befa
JH
1358
1359 /* Otherwise the toplevel "or" terms hit a bit differently.
1360 * We first clear hit markers from them.
1361 */
1362 clr_hit_marker(opt->pattern_expression);
e1327023 1363 grep_source_1(opt, gs, 1);
0ab7befa
JH
1364
1365 if (!chk_hit_marker(opt->pattern_expression))
1366 return 0;
1367
e1327023
JK
1368 return grep_source_1(opt, gs, 0);
1369}
1370
c876d6da 1371int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size)
e1327023
JK
1372{
1373 struct grep_source gs;
1374 int r;
1375
c876d6da 1376 grep_source_init(&gs, GREP_SOURCE_BUF, NULL, NULL);
e1327023
JK
1377 gs.buf = buf;
1378 gs.size = size;
1379
1380 r = grep_source(opt, &gs);
1381
1382 grep_source_clear(&gs);
1383 return r;
1384}
1385
1386void grep_source_init(struct grep_source *gs, enum grep_source_type type,
1387 const char *name, const void *identifier)
1388{
1389 gs->type = type;
1390 gs->name = name ? xstrdup(name) : NULL;
1391 gs->buf = NULL;
1392 gs->size = 0;
94ad9d9e 1393 gs->driver = NULL;
e1327023
JK
1394
1395 switch (type) {
1396 case GREP_SOURCE_FILE:
1397 gs->identifier = xstrdup(identifier);
1398 break;
1399 case GREP_SOURCE_SHA1:
1400 gs->identifier = xmalloc(20);
1401 memcpy(gs->identifier, identifier, 20);
1402 break;
1403 case GREP_SOURCE_BUF:
1404 gs->identifier = NULL;
1405 }
1406}
1407
1408void grep_source_clear(struct grep_source *gs)
1409{
1410 free(gs->name);
1411 gs->name = NULL;
1412 free(gs->identifier);
1413 gs->identifier = NULL;
1414 grep_source_clear_data(gs);
1415}
1416
1417void grep_source_clear_data(struct grep_source *gs)
1418{
1419 switch (gs->type) {
1420 case GREP_SOURCE_FILE:
1421 case GREP_SOURCE_SHA1:
1422 free(gs->buf);
1423 gs->buf = NULL;
1424 gs->size = 0;
1425 break;
1426 case GREP_SOURCE_BUF:
1427 /* leave user-provided buf intact */
1428 break;
1429 }
1430}
1431
1432static int grep_source_load_sha1(struct grep_source *gs)
1433{
1434 enum object_type type;
1435
1436 grep_read_lock();
1437 gs->buf = read_sha1_file(gs->identifier, &type, &gs->size);
1438 grep_read_unlock();
1439
1440 if (!gs->buf)
1441 return error(_("'%s': unable to read %s"),
1442 gs->name,
1443 sha1_to_hex(gs->identifier));
1444 return 0;
1445}
1446
1447static int grep_source_load_file(struct grep_source *gs)
1448{
1449 const char *filename = gs->identifier;
1450 struct stat st;
1451 char *data;
1452 size_t size;
1453 int i;
1454
1455 if (lstat(filename, &st) < 0) {
1456 err_ret:
1457 if (errno != ENOENT)
1458 error(_("'%s': %s"), filename, strerror(errno));
1459 return -1;
1460 }
1461 if (!S_ISREG(st.st_mode))
1462 return -1;
1463 size = xsize_t(st.st_size);
1464 i = open(filename, O_RDONLY);
1465 if (i < 0)
1466 goto err_ret;
1467 data = xmalloc(size + 1);
1468 if (st.st_size != read_in_full(i, data, size)) {
1469 error(_("'%s': short read %s"), filename, strerror(errno));
1470 close(i);
1471 free(data);
1472 return -1;
1473 }
1474 close(i);
1475 data[size] = 0;
1476
1477 gs->buf = data;
1478 gs->size = size;
1479 return 0;
1480}
1481
3083301e 1482static int grep_source_load(struct grep_source *gs)
e1327023
JK
1483{
1484 if (gs->buf)
1485 return 0;
1486
1487 switch (gs->type) {
1488 case GREP_SOURCE_FILE:
1489 return grep_source_load_file(gs);
1490 case GREP_SOURCE_SHA1:
1491 return grep_source_load_sha1(gs);
1492 case GREP_SOURCE_BUF:
1493 return gs->buf ? 0 : -1;
1494 }
1495 die("BUG: invalid grep_source type");
0ab7befa 1496}
94ad9d9e
JK
1497
1498void grep_source_load_driver(struct grep_source *gs)
1499{
1500 if (gs->driver)
1501 return;
1502
1503 grep_attr_lock();
1504 gs->driver = userdiff_find_by_path(gs->name);
1505 if (!gs->driver)
1506 gs->driver = userdiff_find_by_name("default");
1507 grep_attr_unlock();
1508}
41b59bfc 1509
3083301e 1510static int grep_source_is_binary(struct grep_source *gs)
41b59bfc
JK
1511{
1512 grep_source_load_driver(gs);
1513 if (gs->driver->binary != -1)
1514 return gs->driver->binary;
1515
1516 if (!grep_source_load(gs))
1517 return buffer_is_binary(gs->buf, gs->size);
1518
1519 return 0;
1520}