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