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