]> git.ipfire.org Git - thirdparty/git.git/blob - grep.c
Remove support for v1 of the PCRE library
[thirdparty/git.git] / grep.c
1 #include "cache.h"
2 #include "config.h"
3 #include "grep.h"
4 #include "object-store.h"
5 #include "userdiff.h"
6 #include "xdiff-interface.h"
7 #include "diff.h"
8 #include "diffcore.h"
9 #include "commit.h"
10 #include "quote.h"
11 #include "help.h"
12
13 static int grep_source_load(struct grep_source *gs);
14 static int grep_source_is_binary(struct grep_source *gs,
15 struct index_state *istate);
16
17 static void std_output(struct grep_opt *opt, const void *buf, size_t size)
18 {
19 fwrite(buf, size, 1, stdout);
20 }
21
22 static struct grep_opt grep_defaults = {
23 .relative = 1,
24 .pathname = 1,
25 .max_depth = -1,
26 .pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED,
27 .colors = {
28 [GREP_COLOR_CONTEXT] = "",
29 [GREP_COLOR_FILENAME] = "",
30 [GREP_COLOR_FUNCTION] = "",
31 [GREP_COLOR_LINENO] = "",
32 [GREP_COLOR_COLUMNNO] = "",
33 [GREP_COLOR_MATCH_CONTEXT] = GIT_COLOR_BOLD_RED,
34 [GREP_COLOR_MATCH_SELECTED] = GIT_COLOR_BOLD_RED,
35 [GREP_COLOR_SELECTED] = "",
36 [GREP_COLOR_SEP] = GIT_COLOR_CYAN,
37 },
38 .only_matching = 0,
39 .color = -1,
40 .output = std_output,
41 };
42
43 #ifdef USE_LIBPCRE2
44 static pcre2_general_context *pcre2_global_context;
45
46 static void *pcre2_malloc(PCRE2_SIZE size, MAYBE_UNUSED void *memory_data)
47 {
48 return malloc(size);
49 }
50
51 static void pcre2_free(void *pointer, MAYBE_UNUSED void *memory_data)
52 {
53 free(pointer);
54 }
55 #endif
56
57 static const char *color_grep_slots[] = {
58 [GREP_COLOR_CONTEXT] = "context",
59 [GREP_COLOR_FILENAME] = "filename",
60 [GREP_COLOR_FUNCTION] = "function",
61 [GREP_COLOR_LINENO] = "lineNumber",
62 [GREP_COLOR_COLUMNNO] = "column",
63 [GREP_COLOR_MATCH_CONTEXT] = "matchContext",
64 [GREP_COLOR_MATCH_SELECTED] = "matchSelected",
65 [GREP_COLOR_SELECTED] = "selected",
66 [GREP_COLOR_SEP] = "separator",
67 };
68
69 static int parse_pattern_type_arg(const char *opt, const char *arg)
70 {
71 if (!strcmp(arg, "default"))
72 return GREP_PATTERN_TYPE_UNSPECIFIED;
73 else if (!strcmp(arg, "basic"))
74 return GREP_PATTERN_TYPE_BRE;
75 else if (!strcmp(arg, "extended"))
76 return GREP_PATTERN_TYPE_ERE;
77 else if (!strcmp(arg, "fixed"))
78 return GREP_PATTERN_TYPE_FIXED;
79 else if (!strcmp(arg, "perl"))
80 return GREP_PATTERN_TYPE_PCRE;
81 die("bad %s argument: %s", opt, arg);
82 }
83
84 define_list_config_array_extra(color_grep_slots, {"match"});
85
86 /*
87 * Read the configuration file once and store it in
88 * the grep_defaults template.
89 */
90 int grep_config(const char *var, const char *value, void *cb)
91 {
92 struct grep_opt *opt = &grep_defaults;
93 const char *slot;
94
95 if (userdiff_config(var, value) < 0)
96 return -1;
97
98 /*
99 * The instance of grep_opt that we set up here is copied by
100 * grep_init() to be used by each individual invocation.
101 * When populating a new field of this structure here, be
102 * sure to think about ownership -- e.g., you might need to
103 * override the shallow copy in grep_init() with a deep copy.
104 */
105
106 if (!strcmp(var, "grep.extendedregexp")) {
107 opt->extended_regexp_option = git_config_bool(var, value);
108 return 0;
109 }
110
111 if (!strcmp(var, "grep.patterntype")) {
112 opt->pattern_type_option = parse_pattern_type_arg(var, value);
113 return 0;
114 }
115
116 if (!strcmp(var, "grep.linenumber")) {
117 opt->linenum = git_config_bool(var, value);
118 return 0;
119 }
120 if (!strcmp(var, "grep.column")) {
121 opt->columnnum = git_config_bool(var, value);
122 return 0;
123 }
124
125 if (!strcmp(var, "grep.fullname")) {
126 opt->relative = !git_config_bool(var, value);
127 return 0;
128 }
129
130 if (!strcmp(var, "color.grep"))
131 opt->color = git_config_colorbool(var, value);
132 if (!strcmp(var, "color.grep.match")) {
133 if (grep_config("color.grep.matchcontext", value, cb) < 0)
134 return -1;
135 if (grep_config("color.grep.matchselected", value, cb) < 0)
136 return -1;
137 } else if (skip_prefix(var, "color.grep.", &slot)) {
138 int i = LOOKUP_CONFIG(color_grep_slots, slot);
139 char *color;
140
141 if (i < 0)
142 return -1;
143 color = opt->colors[i];
144 if (!value)
145 return config_error_nonbool(var);
146 return color_parse(value, color);
147 }
148 return 0;
149 }
150
151 /*
152 * Initialize one instance of grep_opt and copy the
153 * default values from the template we read the configuration
154 * information in an earlier call to git_config(grep_config).
155 *
156 * If using PCRE, make sure that the library is configured
157 * to use the same allocator as Git (e.g. nedmalloc on Windows).
158 *
159 * Any allocated memory needs to be released in grep_destroy().
160 */
161 void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix)
162 {
163 #if defined(USE_LIBPCRE2)
164 if (!pcre2_global_context)
165 pcre2_global_context = pcre2_general_context_create(
166 pcre2_malloc, pcre2_free, NULL);
167 #endif
168
169 *opt = grep_defaults;
170
171 opt->repo = repo;
172 opt->prefix = prefix;
173 opt->prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
174 opt->pattern_tail = &opt->pattern_list;
175 opt->header_tail = &opt->header_list;
176 }
177
178 void grep_destroy(void)
179 {
180 #ifdef USE_LIBPCRE2
181 pcre2_general_context_free(pcre2_global_context);
182 #endif
183 }
184
185 static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, struct grep_opt *opt)
186 {
187 /*
188 * When committing to the pattern type by setting the relevant
189 * fields in grep_opt it's generally not necessary to zero out
190 * the fields we're not choosing, since they won't have been
191 * set by anything. The extended_regexp_option field is the
192 * only exception to this.
193 *
194 * This is because in the process of parsing grep.patternType
195 * & grep.extendedRegexp we set opt->pattern_type_option and
196 * opt->extended_regexp_option, respectively. We then
197 * internally use opt->extended_regexp_option to see if we're
198 * compiling an ERE. It must be unset if that's not actually
199 * the case.
200 */
201 if (pattern_type != GREP_PATTERN_TYPE_ERE &&
202 opt->extended_regexp_option)
203 opt->extended_regexp_option = 0;
204
205 switch (pattern_type) {
206 case GREP_PATTERN_TYPE_UNSPECIFIED:
207 /* fall through */
208
209 case GREP_PATTERN_TYPE_BRE:
210 break;
211
212 case GREP_PATTERN_TYPE_ERE:
213 opt->extended_regexp_option = 1;
214 break;
215
216 case GREP_PATTERN_TYPE_FIXED:
217 opt->fixed = 1;
218 break;
219
220 case GREP_PATTERN_TYPE_PCRE:
221 opt->pcre2 = 1;
222 break;
223 }
224 }
225
226 void grep_commit_pattern_type(enum grep_pattern_type pattern_type, struct grep_opt *opt)
227 {
228 if (pattern_type != GREP_PATTERN_TYPE_UNSPECIFIED)
229 grep_set_pattern_type_option(pattern_type, opt);
230 else if (opt->pattern_type_option != GREP_PATTERN_TYPE_UNSPECIFIED)
231 grep_set_pattern_type_option(opt->pattern_type_option, opt);
232 else if (opt->extended_regexp_option)
233 /*
234 * This branch *must* happen after setting from the
235 * opt->pattern_type_option above, we don't want
236 * grep.extendedRegexp to override grep.patternType!
237 */
238 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, opt);
239 }
240
241 static struct grep_pat *create_grep_pat(const char *pat, size_t patlen,
242 const char *origin, int no,
243 enum grep_pat_token t,
244 enum grep_header_field field)
245 {
246 struct grep_pat *p = xcalloc(1, sizeof(*p));
247 p->pattern = xmemdupz(pat, patlen);
248 p->patternlen = patlen;
249 p->origin = origin;
250 p->no = no;
251 p->token = t;
252 p->field = field;
253 return p;
254 }
255
256 static void do_append_grep_pat(struct grep_pat ***tail, struct grep_pat *p)
257 {
258 **tail = p;
259 *tail = &p->next;
260 p->next = NULL;
261
262 switch (p->token) {
263 case GREP_PATTERN: /* atom */
264 case GREP_PATTERN_HEAD:
265 case GREP_PATTERN_BODY:
266 for (;;) {
267 struct grep_pat *new_pat;
268 size_t len = 0;
269 char *cp = p->pattern + p->patternlen, *nl = NULL;
270 while (++len <= p->patternlen) {
271 if (*(--cp) == '\n') {
272 nl = cp;
273 break;
274 }
275 }
276 if (!nl)
277 break;
278 new_pat = create_grep_pat(nl + 1, len - 1, p->origin,
279 p->no, p->token, p->field);
280 new_pat->next = p->next;
281 if (!p->next)
282 *tail = &new_pat->next;
283 p->next = new_pat;
284 *nl = '\0';
285 p->patternlen -= len;
286 }
287 break;
288 default:
289 break;
290 }
291 }
292
293 void append_header_grep_pattern(struct grep_opt *opt,
294 enum grep_header_field field, const char *pat)
295 {
296 struct grep_pat *p = create_grep_pat(pat, strlen(pat), "header", 0,
297 GREP_PATTERN_HEAD, field);
298 if (field == GREP_HEADER_REFLOG)
299 opt->use_reflog_filter = 1;
300 do_append_grep_pat(&opt->header_tail, p);
301 }
302
303 void append_grep_pattern(struct grep_opt *opt, const char *pat,
304 const char *origin, int no, enum grep_pat_token t)
305 {
306 append_grep_pat(opt, pat, strlen(pat), origin, no, t);
307 }
308
309 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen,
310 const char *origin, int no, enum grep_pat_token t)
311 {
312 struct grep_pat *p = create_grep_pat(pat, patlen, origin, no, t, 0);
313 do_append_grep_pat(&opt->pattern_tail, p);
314 }
315
316 struct grep_opt *grep_opt_dup(const struct grep_opt *opt)
317 {
318 struct grep_pat *pat;
319 struct grep_opt *ret = xmalloc(sizeof(struct grep_opt));
320 *ret = *opt;
321
322 ret->pattern_list = NULL;
323 ret->pattern_tail = &ret->pattern_list;
324
325 for(pat = opt->pattern_list; pat != NULL; pat = pat->next)
326 {
327 if(pat->token == GREP_PATTERN_HEAD)
328 append_header_grep_pattern(ret, pat->field,
329 pat->pattern);
330 else
331 append_grep_pat(ret, pat->pattern, pat->patternlen,
332 pat->origin, pat->no, pat->token);
333 }
334
335 return ret;
336 }
337
338 static NORETURN void compile_regexp_failed(const struct grep_pat *p,
339 const char *error)
340 {
341 char where[1024];
342
343 if (p->no)
344 xsnprintf(where, sizeof(where), "In '%s' at %d, ", p->origin, p->no);
345 else if (p->origin)
346 xsnprintf(where, sizeof(where), "%s, ", p->origin);
347 else
348 where[0] = 0;
349
350 die("%s'%s': %s", where, p->pattern, error);
351 }
352
353 static int is_fixed(const char *s, size_t len)
354 {
355 size_t i;
356
357 for (i = 0; i < len; i++) {
358 if (is_regex_special(s[i]))
359 return 0;
360 }
361
362 return 1;
363 }
364
365 #ifdef USE_LIBPCRE2
366 static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt)
367 {
368 int error;
369 PCRE2_UCHAR errbuf[256];
370 PCRE2_SIZE erroffset;
371 int options = PCRE2_MULTILINE;
372 int jitret;
373 int patinforet;
374 size_t jitsizearg;
375
376 assert(opt->pcre2);
377
378 p->pcre2_compile_context = NULL;
379
380 /* pcre2_global_context is initialized in append_grep_pattern */
381 if (opt->ignore_case) {
382 if (!opt->ignore_locale && has_non_ascii(p->pattern)) {
383 if (!pcre2_global_context)
384 BUG("pcre2_global_context uninitialized");
385 p->pcre2_tables = pcre2_maketables(pcre2_global_context);
386 p->pcre2_compile_context = pcre2_compile_context_create(NULL);
387 pcre2_set_character_tables(p->pcre2_compile_context,
388 p->pcre2_tables);
389 }
390 options |= PCRE2_CASELESS;
391 }
392 if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
393 !(!opt->ignore_case && (p->fixed || p->is_fixed)))
394 options |= PCRE2_UTF;
395
396 p->pcre2_pattern = pcre2_compile((PCRE2_SPTR)p->pattern,
397 p->patternlen, options, &error, &erroffset,
398 p->pcre2_compile_context);
399
400 if (p->pcre2_pattern) {
401 p->pcre2_match_data = pcre2_match_data_create_from_pattern(p->pcre2_pattern, NULL);
402 if (!p->pcre2_match_data)
403 die("Couldn't allocate PCRE2 match data");
404 } else {
405 pcre2_get_error_message(error, errbuf, sizeof(errbuf));
406 compile_regexp_failed(p, (const char *)&errbuf);
407 }
408
409 pcre2_config(PCRE2_CONFIG_JIT, &p->pcre2_jit_on);
410 if (opt->debug)
411 fprintf(stderr, "pcre2_jit_on=%d\n", p->pcre2_jit_on);
412 if (p->pcre2_jit_on) {
413 jitret = pcre2_jit_compile(p->pcre2_pattern, PCRE2_JIT_COMPLETE);
414 if (jitret)
415 die("Couldn't JIT the PCRE2 pattern '%s', got '%d'\n", p->pattern, jitret);
416
417 /*
418 * The pcre2_config(PCRE2_CONFIG_JIT, ...) call just
419 * tells us whether the library itself supports JIT,
420 * but to see whether we're going to be actually using
421 * JIT we need to extract PCRE2_INFO_JITSIZE from the
422 * pattern *after* we do pcre2_jit_compile() above.
423 *
424 * This is because if the pattern contains the
425 * (*NO_JIT) verb (see pcre2syntax(3))
426 * pcre2_jit_compile() will exit early with 0. If we
427 * then proceed to call pcre2_jit_match() further down
428 * the line instead of pcre2_match() we'll either
429 * segfault (pre PCRE 10.31) or run into a fatal error
430 * (post PCRE2 10.31)
431 */
432 patinforet = pcre2_pattern_info(p->pcre2_pattern, PCRE2_INFO_JITSIZE, &jitsizearg);
433 if (patinforet)
434 BUG("pcre2_pattern_info() failed: %d", patinforet);
435 if (jitsizearg == 0) {
436 p->pcre2_jit_on = 0;
437 if (opt->debug)
438 fprintf(stderr, "pcre2_jit_on=%d: (*NO_JIT) in regex\n",
439 p->pcre2_jit_on);
440 return;
441 }
442 }
443 }
444
445 static int pcre2match(struct grep_pat *p, const char *line, const char *eol,
446 regmatch_t *match, int eflags)
447 {
448 int ret, flags = 0;
449 PCRE2_SIZE *ovector;
450 PCRE2_UCHAR errbuf[256];
451
452 if (eflags & REG_NOTBOL)
453 flags |= PCRE2_NOTBOL;
454
455 if (p->pcre2_jit_on)
456 ret = pcre2_jit_match(p->pcre2_pattern, (unsigned char *)line,
457 eol - line, 0, flags, p->pcre2_match_data,
458 NULL);
459 else
460 ret = pcre2_match(p->pcre2_pattern, (unsigned char *)line,
461 eol - line, 0, flags, p->pcre2_match_data,
462 NULL);
463
464 if (ret < 0 && ret != PCRE2_ERROR_NOMATCH) {
465 pcre2_get_error_message(ret, errbuf, sizeof(errbuf));
466 die("%s failed with error code %d: %s",
467 (p->pcre2_jit_on ? "pcre2_jit_match" : "pcre2_match"), ret,
468 errbuf);
469 }
470 if (ret > 0) {
471 ovector = pcre2_get_ovector_pointer(p->pcre2_match_data);
472 ret = 0;
473 match->rm_so = (int)ovector[0];
474 match->rm_eo = (int)ovector[1];
475 }
476
477 return ret;
478 }
479
480 static void free_pcre2_pattern(struct grep_pat *p)
481 {
482 pcre2_compile_context_free(p->pcre2_compile_context);
483 pcre2_code_free(p->pcre2_pattern);
484 pcre2_match_data_free(p->pcre2_match_data);
485 free((void *)p->pcre2_tables);
486 }
487 #else /* !USE_LIBPCRE2 */
488 static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt)
489 {
490 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
491 }
492
493 static int pcre2match(struct grep_pat *p, const char *line, const char *eol,
494 regmatch_t *match, int eflags)
495 {
496 return 1;
497 }
498
499 static void free_pcre2_pattern(struct grep_pat *p)
500 {
501 }
502
503 static void compile_fixed_regexp(struct grep_pat *p, struct grep_opt *opt)
504 {
505 struct strbuf sb = STRBUF_INIT;
506 int err;
507 int regflags = 0;
508
509 basic_regex_quote_buf(&sb, p->pattern);
510 if (opt->ignore_case)
511 regflags |= REG_ICASE;
512 err = regcomp(&p->regexp, sb.buf, regflags);
513 if (opt->debug)
514 fprintf(stderr, "fixed %s\n", sb.buf);
515 strbuf_release(&sb);
516 if (err) {
517 char errbuf[1024];
518 regerror(err, &p->regexp, errbuf, sizeof(errbuf));
519 compile_regexp_failed(p, errbuf);
520 }
521 }
522 #endif /* !USE_LIBPCRE2 */
523
524 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
525 {
526 int err;
527 int regflags = REG_NEWLINE;
528
529 p->word_regexp = opt->word_regexp;
530 p->ignore_case = opt->ignore_case;
531 p->fixed = opt->fixed;
532
533 if (memchr(p->pattern, 0, p->patternlen) && !opt->pcre2)
534 die(_("given pattern contains NULL byte (via -f <file>). This is only supported with -P under PCRE v2"));
535
536 p->is_fixed = is_fixed(p->pattern, p->patternlen);
537 #ifdef USE_LIBPCRE2
538 if (!p->fixed && !p->is_fixed) {
539 const char *no_jit = "(*NO_JIT)";
540 const int no_jit_len = strlen(no_jit);
541 if (starts_with(p->pattern, no_jit) &&
542 is_fixed(p->pattern + no_jit_len,
543 p->patternlen - no_jit_len))
544 p->is_fixed = 1;
545 }
546 #endif
547 if (p->fixed || p->is_fixed) {
548 #ifdef USE_LIBPCRE2
549 opt->pcre2 = 1;
550 if (p->is_fixed) {
551 compile_pcre2_pattern(p, opt);
552 } else {
553 /*
554 * E.g. t7811-grep-open.sh relies on the
555 * pattern being restored.
556 */
557 char *old_pattern = p->pattern;
558 size_t old_patternlen = p->patternlen;
559 struct strbuf sb = STRBUF_INIT;
560
561 /*
562 * There is the PCRE2_LITERAL flag, but it's
563 * only in PCRE v2 10.30 and later. Needing to
564 * ifdef our way around that and dealing with
565 * it + PCRE2_MULTILINE being an error is more
566 * complex than just quoting this ourselves.
567 */
568 strbuf_add(&sb, "\\Q", 2);
569 strbuf_add(&sb, p->pattern, p->patternlen);
570 strbuf_add(&sb, "\\E", 2);
571
572 p->pattern = sb.buf;
573 p->patternlen = sb.len;
574 compile_pcre2_pattern(p, opt);
575 p->pattern = old_pattern;
576 p->patternlen = old_patternlen;
577 strbuf_release(&sb);
578 }
579 #else /* !USE_LIBPCRE2 */
580 compile_fixed_regexp(p, opt);
581 #endif /* !USE_LIBPCRE2 */
582 return;
583 }
584
585 if (opt->pcre2) {
586 compile_pcre2_pattern(p, opt);
587 return;
588 }
589
590 if (p->ignore_case)
591 regflags |= REG_ICASE;
592 if (opt->extended_regexp_option)
593 regflags |= REG_EXTENDED;
594 err = regcomp(&p->regexp, p->pattern, regflags);
595 if (err) {
596 char errbuf[1024];
597 regerror(err, &p->regexp, errbuf, 1024);
598 compile_regexp_failed(p, errbuf);
599 }
600 }
601
602 static struct grep_expr *compile_pattern_or(struct grep_pat **);
603 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
604 {
605 struct grep_pat *p;
606 struct grep_expr *x;
607
608 p = *list;
609 if (!p)
610 return NULL;
611 switch (p->token) {
612 case GREP_PATTERN: /* atom */
613 case GREP_PATTERN_HEAD:
614 case GREP_PATTERN_BODY:
615 x = xcalloc(1, sizeof (struct grep_expr));
616 x->node = GREP_NODE_ATOM;
617 x->u.atom = p;
618 *list = p->next;
619 return x;
620 case GREP_OPEN_PAREN:
621 *list = p->next;
622 x = compile_pattern_or(list);
623 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
624 die("unmatched parenthesis");
625 *list = (*list)->next;
626 return x;
627 default:
628 return NULL;
629 }
630 }
631
632 static struct grep_expr *compile_pattern_not(struct grep_pat **list)
633 {
634 struct grep_pat *p;
635 struct grep_expr *x;
636
637 p = *list;
638 if (!p)
639 return NULL;
640 switch (p->token) {
641 case GREP_NOT:
642 if (!p->next)
643 die("--not not followed by pattern expression");
644 *list = p->next;
645 x = xcalloc(1, sizeof (struct grep_expr));
646 x->node = GREP_NODE_NOT;
647 x->u.unary = compile_pattern_not(list);
648 if (!x->u.unary)
649 die("--not followed by non pattern expression");
650 return x;
651 default:
652 return compile_pattern_atom(list);
653 }
654 }
655
656 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
657 {
658 struct grep_pat *p;
659 struct grep_expr *x, *y, *z;
660
661 x = compile_pattern_not(list);
662 p = *list;
663 if (p && p->token == GREP_AND) {
664 if (!p->next)
665 die("--and not followed by pattern expression");
666 *list = p->next;
667 y = compile_pattern_and(list);
668 if (!y)
669 die("--and not followed by pattern expression");
670 z = xcalloc(1, sizeof (struct grep_expr));
671 z->node = GREP_NODE_AND;
672 z->u.binary.left = x;
673 z->u.binary.right = y;
674 return z;
675 }
676 return x;
677 }
678
679 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
680 {
681 struct grep_pat *p;
682 struct grep_expr *x, *y, *z;
683
684 x = compile_pattern_and(list);
685 p = *list;
686 if (x && p && p->token != GREP_CLOSE_PAREN) {
687 y = compile_pattern_or(list);
688 if (!y)
689 die("not a pattern expression %s", p->pattern);
690 z = xcalloc(1, sizeof (struct grep_expr));
691 z->node = GREP_NODE_OR;
692 z->u.binary.left = x;
693 z->u.binary.right = y;
694 return z;
695 }
696 return x;
697 }
698
699 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
700 {
701 return compile_pattern_or(list);
702 }
703
704 static void indent(int in)
705 {
706 while (in-- > 0)
707 fputc(' ', stderr);
708 }
709
710 static void dump_grep_pat(struct grep_pat *p)
711 {
712 switch (p->token) {
713 case GREP_AND: fprintf(stderr, "*and*"); break;
714 case GREP_OPEN_PAREN: fprintf(stderr, "*(*"); break;
715 case GREP_CLOSE_PAREN: fprintf(stderr, "*)*"); break;
716 case GREP_NOT: fprintf(stderr, "*not*"); break;
717 case GREP_OR: fprintf(stderr, "*or*"); break;
718
719 case GREP_PATTERN: fprintf(stderr, "pattern"); break;
720 case GREP_PATTERN_HEAD: fprintf(stderr, "pattern_head"); break;
721 case GREP_PATTERN_BODY: fprintf(stderr, "pattern_body"); break;
722 }
723
724 switch (p->token) {
725 default: break;
726 case GREP_PATTERN_HEAD:
727 fprintf(stderr, "<head %d>", p->field); break;
728 case GREP_PATTERN_BODY:
729 fprintf(stderr, "<body>"); break;
730 }
731 switch (p->token) {
732 default: break;
733 case GREP_PATTERN_HEAD:
734 case GREP_PATTERN_BODY:
735 case GREP_PATTERN:
736 fprintf(stderr, "%.*s", (int)p->patternlen, p->pattern);
737 break;
738 }
739 fputc('\n', stderr);
740 }
741
742 static void dump_grep_expression_1(struct grep_expr *x, int in)
743 {
744 indent(in);
745 switch (x->node) {
746 case GREP_NODE_TRUE:
747 fprintf(stderr, "true\n");
748 break;
749 case GREP_NODE_ATOM:
750 dump_grep_pat(x->u.atom);
751 break;
752 case GREP_NODE_NOT:
753 fprintf(stderr, "(not\n");
754 dump_grep_expression_1(x->u.unary, in+1);
755 indent(in);
756 fprintf(stderr, ")\n");
757 break;
758 case GREP_NODE_AND:
759 fprintf(stderr, "(and\n");
760 dump_grep_expression_1(x->u.binary.left, in+1);
761 dump_grep_expression_1(x->u.binary.right, in+1);
762 indent(in);
763 fprintf(stderr, ")\n");
764 break;
765 case GREP_NODE_OR:
766 fprintf(stderr, "(or\n");
767 dump_grep_expression_1(x->u.binary.left, in+1);
768 dump_grep_expression_1(x->u.binary.right, in+1);
769 indent(in);
770 fprintf(stderr, ")\n");
771 break;
772 }
773 }
774
775 static void dump_grep_expression(struct grep_opt *opt)
776 {
777 struct grep_expr *x = opt->pattern_expression;
778
779 if (opt->all_match)
780 fprintf(stderr, "[all-match]\n");
781 dump_grep_expression_1(x, 0);
782 fflush(NULL);
783 }
784
785 static struct grep_expr *grep_true_expr(void)
786 {
787 struct grep_expr *z = xcalloc(1, sizeof(*z));
788 z->node = GREP_NODE_TRUE;
789 return z;
790 }
791
792 static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
793 {
794 struct grep_expr *z = xcalloc(1, sizeof(*z));
795 z->node = GREP_NODE_OR;
796 z->u.binary.left = left;
797 z->u.binary.right = right;
798 return z;
799 }
800
801 static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
802 {
803 struct grep_pat *p;
804 struct grep_expr *header_expr;
805 struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]);
806 enum grep_header_field fld;
807
808 if (!opt->header_list)
809 return NULL;
810
811 for (p = opt->header_list; p; p = p->next) {
812 if (p->token != GREP_PATTERN_HEAD)
813 BUG("a non-header pattern in grep header list.");
814 if (p->field < GREP_HEADER_FIELD_MIN ||
815 GREP_HEADER_FIELD_MAX <= p->field)
816 BUG("unknown header field %d", p->field);
817 compile_regexp(p, opt);
818 }
819
820 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++)
821 header_group[fld] = NULL;
822
823 for (p = opt->header_list; p; p = p->next) {
824 struct grep_expr *h;
825 struct grep_pat *pp = p;
826
827 h = compile_pattern_atom(&pp);
828 if (!h || pp != p->next)
829 BUG("malformed header expr");
830 if (!header_group[p->field]) {
831 header_group[p->field] = h;
832 continue;
833 }
834 header_group[p->field] = grep_or_expr(h, header_group[p->field]);
835 }
836
837 header_expr = NULL;
838
839 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) {
840 if (!header_group[fld])
841 continue;
842 if (!header_expr)
843 header_expr = grep_true_expr();
844 header_expr = grep_or_expr(header_group[fld], header_expr);
845 }
846 return header_expr;
847 }
848
849 static struct grep_expr *grep_splice_or(struct grep_expr *x, struct grep_expr *y)
850 {
851 struct grep_expr *z = x;
852
853 while (x) {
854 assert(x->node == GREP_NODE_OR);
855 if (x->u.binary.right &&
856 x->u.binary.right->node == GREP_NODE_TRUE) {
857 x->u.binary.right = y;
858 break;
859 }
860 x = x->u.binary.right;
861 }
862 return z;
863 }
864
865 static void compile_grep_patterns_real(struct grep_opt *opt)
866 {
867 struct grep_pat *p;
868 struct grep_expr *header_expr = prep_header_patterns(opt);
869
870 for (p = opt->pattern_list; p; p = p->next) {
871 switch (p->token) {
872 case GREP_PATTERN: /* atom */
873 case GREP_PATTERN_HEAD:
874 case GREP_PATTERN_BODY:
875 compile_regexp(p, opt);
876 break;
877 default:
878 opt->extended = 1;
879 break;
880 }
881 }
882
883 if (opt->all_match || header_expr)
884 opt->extended = 1;
885 else if (!opt->extended && !opt->debug)
886 return;
887
888 p = opt->pattern_list;
889 if (p)
890 opt->pattern_expression = compile_pattern_expr(&p);
891 if (p)
892 die("incomplete pattern expression: %s", p->pattern);
893
894 if (!header_expr)
895 return;
896
897 if (!opt->pattern_expression)
898 opt->pattern_expression = header_expr;
899 else if (opt->all_match)
900 opt->pattern_expression = grep_splice_or(header_expr,
901 opt->pattern_expression);
902 else
903 opt->pattern_expression = grep_or_expr(opt->pattern_expression,
904 header_expr);
905 opt->all_match = 1;
906 }
907
908 void compile_grep_patterns(struct grep_opt *opt)
909 {
910 compile_grep_patterns_real(opt);
911 if (opt->debug)
912 dump_grep_expression(opt);
913 }
914
915 static void free_pattern_expr(struct grep_expr *x)
916 {
917 switch (x->node) {
918 case GREP_NODE_TRUE:
919 case GREP_NODE_ATOM:
920 break;
921 case GREP_NODE_NOT:
922 free_pattern_expr(x->u.unary);
923 break;
924 case GREP_NODE_AND:
925 case GREP_NODE_OR:
926 free_pattern_expr(x->u.binary.left);
927 free_pattern_expr(x->u.binary.right);
928 break;
929 }
930 free(x);
931 }
932
933 void free_grep_patterns(struct grep_opt *opt)
934 {
935 struct grep_pat *p, *n;
936
937 for (p = opt->pattern_list; p; p = n) {
938 n = p->next;
939 switch (p->token) {
940 case GREP_PATTERN: /* atom */
941 case GREP_PATTERN_HEAD:
942 case GREP_PATTERN_BODY:
943 if (p->pcre2_pattern)
944 free_pcre2_pattern(p);
945 else
946 regfree(&p->regexp);
947 free(p->pattern);
948 break;
949 default:
950 break;
951 }
952 free(p);
953 }
954
955 if (!opt->extended)
956 return;
957 free_pattern_expr(opt->pattern_expression);
958 }
959
960 static char *end_of_line(char *cp, unsigned long *left)
961 {
962 unsigned long l = *left;
963 while (l && *cp != '\n') {
964 l--;
965 cp++;
966 }
967 *left = l;
968 return cp;
969 }
970
971 static int word_char(char ch)
972 {
973 return isalnum(ch) || ch == '_';
974 }
975
976 static void output_color(struct grep_opt *opt, const void *data, size_t size,
977 const char *color)
978 {
979 if (want_color(opt->color) && color && color[0]) {
980 opt->output(opt, color, strlen(color));
981 opt->output(opt, data, size);
982 opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
983 } else
984 opt->output(opt, data, size);
985 }
986
987 static void output_sep(struct grep_opt *opt, char sign)
988 {
989 if (opt->null_following_name)
990 opt->output(opt, "\0", 1);
991 else
992 output_color(opt, &sign, 1, opt->colors[GREP_COLOR_SEP]);
993 }
994
995 static void show_name(struct grep_opt *opt, const char *name)
996 {
997 output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]);
998 opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
999 }
1000
1001 static int patmatch(struct grep_pat *p, char *line, char *eol,
1002 regmatch_t *match, int eflags)
1003 {
1004 int hit;
1005
1006 if (p->pcre2_pattern)
1007 hit = !pcre2match(p, line, eol, match, eflags);
1008 else
1009 hit = !regexec_buf(&p->regexp, line, eol - line, 1, match,
1010 eflags);
1011
1012 return hit;
1013 }
1014
1015 static int strip_timestamp(char *bol, char **eol_p)
1016 {
1017 char *eol = *eol_p;
1018 int ch;
1019
1020 while (bol < --eol) {
1021 if (*eol != '>')
1022 continue;
1023 *eol_p = ++eol;
1024 ch = *eol;
1025 *eol = '\0';
1026 return ch;
1027 }
1028 return 0;
1029 }
1030
1031 static struct {
1032 const char *field;
1033 size_t len;
1034 } header_field[] = {
1035 { "author ", 7 },
1036 { "committer ", 10 },
1037 { "reflog ", 7 },
1038 };
1039
1040 static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
1041 enum grep_context ctx,
1042 regmatch_t *pmatch, int eflags)
1043 {
1044 int hit = 0;
1045 int saved_ch = 0;
1046 const char *start = bol;
1047
1048 if ((p->token != GREP_PATTERN) &&
1049 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
1050 return 0;
1051
1052 if (p->token == GREP_PATTERN_HEAD) {
1053 const char *field;
1054 size_t len;
1055 assert(p->field < ARRAY_SIZE(header_field));
1056 field = header_field[p->field].field;
1057 len = header_field[p->field].len;
1058 if (strncmp(bol, field, len))
1059 return 0;
1060 bol += len;
1061 switch (p->field) {
1062 case GREP_HEADER_AUTHOR:
1063 case GREP_HEADER_COMMITTER:
1064 saved_ch = strip_timestamp(bol, &eol);
1065 break;
1066 default:
1067 break;
1068 }
1069 }
1070
1071 again:
1072 hit = patmatch(p, bol, eol, pmatch, eflags);
1073
1074 if (hit && p->word_regexp) {
1075 if ((pmatch[0].rm_so < 0) ||
1076 (eol - bol) < pmatch[0].rm_so ||
1077 (pmatch[0].rm_eo < 0) ||
1078 (eol - bol) < pmatch[0].rm_eo)
1079 die("regexp returned nonsense");
1080
1081 /* Match beginning must be either beginning of the
1082 * line, or at word boundary (i.e. the last char must
1083 * not be a word char). Similarly, match end must be
1084 * either end of the line, or at word boundary
1085 * (i.e. the next char must not be a word char).
1086 */
1087 if ( ((pmatch[0].rm_so == 0) ||
1088 !word_char(bol[pmatch[0].rm_so-1])) &&
1089 ((pmatch[0].rm_eo == (eol-bol)) ||
1090 !word_char(bol[pmatch[0].rm_eo])) )
1091 ;
1092 else
1093 hit = 0;
1094
1095 /* Words consist of at least one character. */
1096 if (pmatch->rm_so == pmatch->rm_eo)
1097 hit = 0;
1098
1099 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
1100 /* There could be more than one match on the
1101 * line, and the first match might not be
1102 * strict word match. But later ones could be!
1103 * Forward to the next possible start, i.e. the
1104 * next position following a non-word char.
1105 */
1106 bol = pmatch[0].rm_so + bol + 1;
1107 while (word_char(bol[-1]) && bol < eol)
1108 bol++;
1109 eflags |= REG_NOTBOL;
1110 if (bol < eol)
1111 goto again;
1112 }
1113 }
1114 if (p->token == GREP_PATTERN_HEAD && saved_ch)
1115 *eol = saved_ch;
1116 if (hit) {
1117 pmatch[0].rm_so += bol - start;
1118 pmatch[0].rm_eo += bol - start;
1119 }
1120 return hit;
1121 }
1122
1123 static int match_expr_eval(struct grep_opt *opt, struct grep_expr *x, char *bol,
1124 char *eol, enum grep_context ctx, ssize_t *col,
1125 ssize_t *icol, int collect_hits)
1126 {
1127 int h = 0;
1128
1129 if (!x)
1130 die("Not a valid grep expression");
1131 switch (x->node) {
1132 case GREP_NODE_TRUE:
1133 h = 1;
1134 break;
1135 case GREP_NODE_ATOM:
1136 {
1137 regmatch_t tmp;
1138 h = match_one_pattern(x->u.atom, bol, eol, ctx,
1139 &tmp, 0);
1140 if (h && (*col < 0 || tmp.rm_so < *col))
1141 *col = tmp.rm_so;
1142 }
1143 break;
1144 case GREP_NODE_NOT:
1145 /*
1146 * Upon visiting a GREP_NODE_NOT, col and icol become swapped.
1147 */
1148 h = !match_expr_eval(opt, x->u.unary, bol, eol, ctx, icol, col,
1149 0);
1150 break;
1151 case GREP_NODE_AND:
1152 h = match_expr_eval(opt, x->u.binary.left, bol, eol, ctx, col,
1153 icol, 0);
1154 if (h || opt->columnnum) {
1155 /*
1156 * Don't short-circuit AND when given --column, since a
1157 * NOT earlier in the tree may turn this into an OR. In
1158 * this case, see the below comment.
1159 */
1160 h &= match_expr_eval(opt, x->u.binary.right, bol, eol,
1161 ctx, col, icol, 0);
1162 }
1163 break;
1164 case GREP_NODE_OR:
1165 if (!(collect_hits || opt->columnnum)) {
1166 /*
1167 * Don't short-circuit OR when given --column (or
1168 * collecting hits) to ensure we don't skip a later
1169 * child that would produce an earlier match.
1170 */
1171 return (match_expr_eval(opt, x->u.binary.left, bol, eol,
1172 ctx, col, icol, 0) ||
1173 match_expr_eval(opt, x->u.binary.right, bol,
1174 eol, ctx, col, icol, 0));
1175 }
1176 h = match_expr_eval(opt, x->u.binary.left, bol, eol, ctx, col,
1177 icol, 0);
1178 if (collect_hits)
1179 x->u.binary.left->hit |= h;
1180 h |= match_expr_eval(opt, x->u.binary.right, bol, eol, ctx, col,
1181 icol, collect_hits);
1182 break;
1183 default:
1184 die("Unexpected node type (internal error) %d", x->node);
1185 }
1186 if (collect_hits)
1187 x->hit |= h;
1188 return h;
1189 }
1190
1191 static int match_expr(struct grep_opt *opt, char *bol, char *eol,
1192 enum grep_context ctx, ssize_t *col,
1193 ssize_t *icol, int collect_hits)
1194 {
1195 struct grep_expr *x = opt->pattern_expression;
1196 return match_expr_eval(opt, x, bol, eol, ctx, col, icol, collect_hits);
1197 }
1198
1199 static int match_line(struct grep_opt *opt, char *bol, char *eol,
1200 ssize_t *col, ssize_t *icol,
1201 enum grep_context ctx, int collect_hits)
1202 {
1203 struct grep_pat *p;
1204 int hit = 0;
1205
1206 if (opt->extended)
1207 return match_expr(opt, bol, eol, ctx, col, icol,
1208 collect_hits);
1209
1210 /* we do not call with collect_hits without being extended */
1211 for (p = opt->pattern_list; p; p = p->next) {
1212 regmatch_t tmp;
1213 if (match_one_pattern(p, bol, eol, ctx, &tmp, 0)) {
1214 hit |= 1;
1215 if (!opt->columnnum) {
1216 /*
1217 * Without --column, any single match on a line
1218 * is enough to know that it needs to be
1219 * printed. With --column, scan _all_ patterns
1220 * to find the earliest.
1221 */
1222 break;
1223 }
1224 if (*col < 0 || tmp.rm_so < *col)
1225 *col = tmp.rm_so;
1226 }
1227 }
1228 return hit;
1229 }
1230
1231 static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
1232 enum grep_context ctx,
1233 regmatch_t *pmatch, int eflags)
1234 {
1235 regmatch_t match;
1236
1237 if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
1238 return 0;
1239 if (match.rm_so < 0 || match.rm_eo < 0)
1240 return 0;
1241 if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
1242 if (match.rm_so > pmatch->rm_so)
1243 return 1;
1244 if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
1245 return 1;
1246 }
1247 pmatch->rm_so = match.rm_so;
1248 pmatch->rm_eo = match.rm_eo;
1249 return 1;
1250 }
1251
1252 static int next_match(struct grep_opt *opt, char *bol, char *eol,
1253 enum grep_context ctx, regmatch_t *pmatch, int eflags)
1254 {
1255 struct grep_pat *p;
1256 int hit = 0;
1257
1258 pmatch->rm_so = pmatch->rm_eo = -1;
1259 if (bol < eol) {
1260 for (p = opt->pattern_list; p; p = p->next) {
1261 switch (p->token) {
1262 case GREP_PATTERN: /* atom */
1263 case GREP_PATTERN_HEAD:
1264 case GREP_PATTERN_BODY:
1265 hit |= match_next_pattern(p, bol, eol, ctx,
1266 pmatch, eflags);
1267 break;
1268 default:
1269 break;
1270 }
1271 }
1272 }
1273 return hit;
1274 }
1275
1276 static void show_line_header(struct grep_opt *opt, const char *name,
1277 unsigned lno, ssize_t cno, char sign)
1278 {
1279 if (opt->heading && opt->last_shown == 0) {
1280 output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]);
1281 opt->output(opt, "\n", 1);
1282 }
1283 opt->last_shown = lno;
1284
1285 if (!opt->heading && opt->pathname) {
1286 output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]);
1287 output_sep(opt, sign);
1288 }
1289 if (opt->linenum) {
1290 char buf[32];
1291 xsnprintf(buf, sizeof(buf), "%d", lno);
1292 output_color(opt, buf, strlen(buf), opt->colors[GREP_COLOR_LINENO]);
1293 output_sep(opt, sign);
1294 }
1295 /*
1296 * Treat 'cno' as the 1-indexed offset from the start of a non-context
1297 * line to its first match. Otherwise, 'cno' is 0 indicating that we are
1298 * being called with a context line.
1299 */
1300 if (opt->columnnum && cno) {
1301 char buf[32];
1302 xsnprintf(buf, sizeof(buf), "%"PRIuMAX, (uintmax_t)cno);
1303 output_color(opt, buf, strlen(buf), opt->colors[GREP_COLOR_COLUMNNO]);
1304 output_sep(opt, sign);
1305 }
1306 }
1307
1308 static void show_line(struct grep_opt *opt, char *bol, char *eol,
1309 const char *name, unsigned lno, ssize_t cno, char sign)
1310 {
1311 int rest = eol - bol;
1312 const char *match_color = NULL;
1313 const char *line_color = NULL;
1314
1315 if (opt->file_break && opt->last_shown == 0) {
1316 if (opt->show_hunk_mark)
1317 opt->output(opt, "\n", 1);
1318 } else if (opt->pre_context || opt->post_context || opt->funcbody) {
1319 if (opt->last_shown == 0) {
1320 if (opt->show_hunk_mark) {
1321 output_color(opt, "--", 2, opt->colors[GREP_COLOR_SEP]);
1322 opt->output(opt, "\n", 1);
1323 }
1324 } else if (lno > opt->last_shown + 1) {
1325 output_color(opt, "--", 2, opt->colors[GREP_COLOR_SEP]);
1326 opt->output(opt, "\n", 1);
1327 }
1328 }
1329 if (!opt->only_matching) {
1330 /*
1331 * In case the line we're being called with contains more than
1332 * one match, leave printing each header to the loop below.
1333 */
1334 show_line_header(opt, name, lno, cno, sign);
1335 }
1336 if (opt->color || opt->only_matching) {
1337 regmatch_t match;
1338 enum grep_context ctx = GREP_CONTEXT_BODY;
1339 int ch = *eol;
1340 int eflags = 0;
1341
1342 if (opt->color) {
1343 if (sign == ':')
1344 match_color = opt->colors[GREP_COLOR_MATCH_SELECTED];
1345 else
1346 match_color = opt->colors[GREP_COLOR_MATCH_CONTEXT];
1347 if (sign == ':')
1348 line_color = opt->colors[GREP_COLOR_SELECTED];
1349 else if (sign == '-')
1350 line_color = opt->colors[GREP_COLOR_CONTEXT];
1351 else if (sign == '=')
1352 line_color = opt->colors[GREP_COLOR_FUNCTION];
1353 }
1354 *eol = '\0';
1355 while (next_match(opt, bol, eol, ctx, &match, eflags)) {
1356 if (match.rm_so == match.rm_eo)
1357 break;
1358
1359 if (opt->only_matching)
1360 show_line_header(opt, name, lno, cno, sign);
1361 else
1362 output_color(opt, bol, match.rm_so, line_color);
1363 output_color(opt, bol + match.rm_so,
1364 match.rm_eo - match.rm_so, match_color);
1365 if (opt->only_matching)
1366 opt->output(opt, "\n", 1);
1367 bol += match.rm_eo;
1368 cno += match.rm_eo;
1369 rest -= match.rm_eo;
1370 eflags = REG_NOTBOL;
1371 }
1372 *eol = ch;
1373 }
1374 if (!opt->only_matching) {
1375 output_color(opt, bol, rest, line_color);
1376 opt->output(opt, "\n", 1);
1377 }
1378 }
1379
1380 int grep_use_locks;
1381
1382 /*
1383 * This lock protects access to the gitattributes machinery, which is
1384 * not thread-safe.
1385 */
1386 pthread_mutex_t grep_attr_mutex;
1387
1388 static inline void grep_attr_lock(void)
1389 {
1390 if (grep_use_locks)
1391 pthread_mutex_lock(&grep_attr_mutex);
1392 }
1393
1394 static inline void grep_attr_unlock(void)
1395 {
1396 if (grep_use_locks)
1397 pthread_mutex_unlock(&grep_attr_mutex);
1398 }
1399
1400 static int match_funcname(struct grep_opt *opt, struct grep_source *gs, char *bol, char *eol)
1401 {
1402 xdemitconf_t *xecfg = opt->priv;
1403 if (xecfg && !xecfg->find_func) {
1404 grep_source_load_driver(gs, opt->repo->index);
1405 if (gs->driver->funcname.pattern) {
1406 const struct userdiff_funcname *pe = &gs->driver->funcname;
1407 xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
1408 } else {
1409 xecfg = opt->priv = NULL;
1410 }
1411 }
1412
1413 if (xecfg) {
1414 char buf[1];
1415 return xecfg->find_func(bol, eol - bol, buf, 1,
1416 xecfg->find_func_priv) >= 0;
1417 }
1418
1419 if (bol == eol)
1420 return 0;
1421 if (isalpha(*bol) || *bol == '_' || *bol == '$')
1422 return 1;
1423 return 0;
1424 }
1425
1426 static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
1427 char *bol, unsigned lno)
1428 {
1429 while (bol > gs->buf) {
1430 char *eol = --bol;
1431
1432 while (bol > gs->buf && bol[-1] != '\n')
1433 bol--;
1434 lno--;
1435
1436 if (lno <= opt->last_shown)
1437 break;
1438
1439 if (match_funcname(opt, gs, bol, eol)) {
1440 show_line(opt, bol, eol, gs->name, lno, 0, '=');
1441 break;
1442 }
1443 }
1444 }
1445
1446 static int is_empty_line(const char *bol, const char *eol);
1447
1448 static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
1449 char *bol, char *end, unsigned lno)
1450 {
1451 unsigned cur = lno, from = 1, funcname_lno = 0, orig_from;
1452 int funcname_needed = !!opt->funcname, comment_needed = 0;
1453
1454 if (opt->pre_context < lno)
1455 from = lno - opt->pre_context;
1456 if (from <= opt->last_shown)
1457 from = opt->last_shown + 1;
1458 orig_from = from;
1459 if (opt->funcbody) {
1460 if (match_funcname(opt, gs, bol, end))
1461 comment_needed = 1;
1462 else
1463 funcname_needed = 1;
1464 from = opt->last_shown + 1;
1465 }
1466
1467 /* Rewind. */
1468 while (bol > gs->buf && cur > from) {
1469 char *next_bol = bol;
1470 char *eol = --bol;
1471
1472 while (bol > gs->buf && bol[-1] != '\n')
1473 bol--;
1474 cur--;
1475 if (comment_needed && (is_empty_line(bol, eol) ||
1476 match_funcname(opt, gs, bol, eol))) {
1477 comment_needed = 0;
1478 from = orig_from;
1479 if (cur < from) {
1480 cur++;
1481 bol = next_bol;
1482 break;
1483 }
1484 }
1485 if (funcname_needed && match_funcname(opt, gs, bol, eol)) {
1486 funcname_lno = cur;
1487 funcname_needed = 0;
1488 if (opt->funcbody)
1489 comment_needed = 1;
1490 else
1491 from = orig_from;
1492 }
1493 }
1494
1495 /* We need to look even further back to find a function signature. */
1496 if (opt->funcname && funcname_needed)
1497 show_funcname_line(opt, gs, bol, cur);
1498
1499 /* Back forward. */
1500 while (cur < lno) {
1501 char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
1502
1503 while (*eol != '\n')
1504 eol++;
1505 show_line(opt, bol, eol, gs->name, cur, 0, sign);
1506 bol = eol + 1;
1507 cur++;
1508 }
1509 }
1510
1511 static int should_lookahead(struct grep_opt *opt)
1512 {
1513 struct grep_pat *p;
1514
1515 if (opt->extended)
1516 return 0; /* punt for too complex stuff */
1517 if (opt->invert)
1518 return 0;
1519 for (p = opt->pattern_list; p; p = p->next) {
1520 if (p->token != GREP_PATTERN)
1521 return 0; /* punt for "header only" and stuff */
1522 }
1523 return 1;
1524 }
1525
1526 static int look_ahead(struct grep_opt *opt,
1527 unsigned long *left_p,
1528 unsigned *lno_p,
1529 char **bol_p)
1530 {
1531 unsigned lno = *lno_p;
1532 char *bol = *bol_p;
1533 struct grep_pat *p;
1534 char *sp, *last_bol;
1535 regoff_t earliest = -1;
1536
1537 for (p = opt->pattern_list; p; p = p->next) {
1538 int hit;
1539 regmatch_t m;
1540
1541 hit = patmatch(p, bol, bol + *left_p, &m, 0);
1542 if (!hit || m.rm_so < 0 || m.rm_eo < 0)
1543 continue;
1544 if (earliest < 0 || m.rm_so < earliest)
1545 earliest = m.rm_so;
1546 }
1547
1548 if (earliest < 0) {
1549 *bol_p = bol + *left_p;
1550 *left_p = 0;
1551 return 1;
1552 }
1553 for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
1554 ; /* find the beginning of the line */
1555 last_bol = sp;
1556
1557 for (sp = bol; sp < last_bol; sp++) {
1558 if (*sp == '\n')
1559 lno++;
1560 }
1561 *left_p -= last_bol - bol;
1562 *bol_p = last_bol;
1563 *lno_p = lno;
1564 return 0;
1565 }
1566
1567 static int fill_textconv_grep(struct repository *r,
1568 struct userdiff_driver *driver,
1569 struct grep_source *gs)
1570 {
1571 struct diff_filespec *df;
1572 char *buf;
1573 size_t size;
1574
1575 if (!driver || !driver->textconv)
1576 return grep_source_load(gs);
1577
1578 /*
1579 * The textconv interface is intimately tied to diff_filespecs, so we
1580 * have to pretend to be one. If we could unify the grep_source
1581 * and diff_filespec structs, this mess could just go away.
1582 */
1583 df = alloc_filespec(gs->path);
1584 switch (gs->type) {
1585 case GREP_SOURCE_OID:
1586 fill_filespec(df, gs->identifier, 1, 0100644);
1587 break;
1588 case GREP_SOURCE_FILE:
1589 fill_filespec(df, &null_oid, 0, 0100644);
1590 break;
1591 default:
1592 BUG("attempt to textconv something without a path?");
1593 }
1594
1595 /*
1596 * fill_textconv is not remotely thread-safe; it modifies the global
1597 * diff tempfile structure, writes to the_repo's odb and might
1598 * internally call thread-unsafe functions such as the
1599 * prepare_packed_git() lazy-initializator. Because of the last two, we
1600 * must ensure mutual exclusion between this call and the object reading
1601 * API, thus we use obj_read_lock() here.
1602 *
1603 * TODO: allowing text conversion to run in parallel with object
1604 * reading operations might increase performance in the multithreaded
1605 * non-worktreee git-grep with --textconv.
1606 */
1607 obj_read_lock();
1608 size = fill_textconv(r, driver, df, &buf);
1609 obj_read_unlock();
1610 free_filespec(df);
1611
1612 /*
1613 * The normal fill_textconv usage by the diff machinery would just keep
1614 * the textconv'd buf separate from the diff_filespec. But much of the
1615 * grep code passes around a grep_source and assumes that its "buf"
1616 * pointer is the beginning of the thing we are searching. So let's
1617 * install our textconv'd version into the grep_source, taking care not
1618 * to leak any existing buffer.
1619 */
1620 grep_source_clear_data(gs);
1621 gs->buf = buf;
1622 gs->size = size;
1623
1624 return 0;
1625 }
1626
1627 static int is_empty_line(const char *bol, const char *eol)
1628 {
1629 while (bol < eol && isspace(*bol))
1630 bol++;
1631 return bol == eol;
1632 }
1633
1634 static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits)
1635 {
1636 char *bol;
1637 char *peek_bol = NULL;
1638 unsigned long left;
1639 unsigned lno = 1;
1640 unsigned last_hit = 0;
1641 int binary_match_only = 0;
1642 unsigned count = 0;
1643 int try_lookahead = 0;
1644 int show_function = 0;
1645 struct userdiff_driver *textconv = NULL;
1646 enum grep_context ctx = GREP_CONTEXT_HEAD;
1647 xdemitconf_t xecfg;
1648
1649 if (!opt->status_only && gs->name == NULL)
1650 BUG("grep call which could print a name requires "
1651 "grep_source.name be non-NULL");
1652
1653 if (!opt->output)
1654 opt->output = std_output;
1655
1656 if (opt->pre_context || opt->post_context || opt->file_break ||
1657 opt->funcbody) {
1658 /* Show hunk marks, except for the first file. */
1659 if (opt->last_shown)
1660 opt->show_hunk_mark = 1;
1661 /*
1662 * If we're using threads then we can't easily identify
1663 * the first file. Always put hunk marks in that case
1664 * and skip the very first one later in work_done().
1665 */
1666 if (opt->output != std_output)
1667 opt->show_hunk_mark = 1;
1668 }
1669 opt->last_shown = 0;
1670
1671 if (opt->allow_textconv) {
1672 grep_source_load_driver(gs, opt->repo->index);
1673 /*
1674 * We might set up the shared textconv cache data here, which
1675 * is not thread-safe. Also, get_oid_with_context() and
1676 * parse_object() might be internally called. As they are not
1677 * currently thread-safe and might be racy with object reading,
1678 * obj_read_lock() must be called.
1679 */
1680 grep_attr_lock();
1681 obj_read_lock();
1682 textconv = userdiff_get_textconv(opt->repo, gs->driver);
1683 obj_read_unlock();
1684 grep_attr_unlock();
1685 }
1686
1687 /*
1688 * We know the result of a textconv is text, so we only have to care
1689 * about binary handling if we are not using it.
1690 */
1691 if (!textconv) {
1692 switch (opt->binary) {
1693 case GREP_BINARY_DEFAULT:
1694 if (grep_source_is_binary(gs, opt->repo->index))
1695 binary_match_only = 1;
1696 break;
1697 case GREP_BINARY_NOMATCH:
1698 if (grep_source_is_binary(gs, opt->repo->index))
1699 return 0; /* Assume unmatch */
1700 break;
1701 case GREP_BINARY_TEXT:
1702 break;
1703 default:
1704 BUG("unknown binary handling mode");
1705 }
1706 }
1707
1708 memset(&xecfg, 0, sizeof(xecfg));
1709 opt->priv = &xecfg;
1710
1711 try_lookahead = should_lookahead(opt);
1712
1713 if (fill_textconv_grep(opt->repo, textconv, gs) < 0)
1714 return 0;
1715
1716 bol = gs->buf;
1717 left = gs->size;
1718 while (left) {
1719 char *eol, ch;
1720 int hit;
1721 ssize_t cno;
1722 ssize_t col = -1, icol = -1;
1723
1724 /*
1725 * look_ahead() skips quickly to the line that possibly
1726 * has the next hit; don't call it if we need to do
1727 * something more than just skipping the current line
1728 * in response to an unmatch for the current line. E.g.
1729 * inside a post-context window, we will show the current
1730 * line as a context around the previous hit when it
1731 * doesn't hit.
1732 */
1733 if (try_lookahead
1734 && !(last_hit
1735 && (show_function ||
1736 lno <= last_hit + opt->post_context))
1737 && look_ahead(opt, &left, &lno, &bol))
1738 break;
1739 eol = end_of_line(bol, &left);
1740 ch = *eol;
1741 *eol = 0;
1742
1743 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
1744 ctx = GREP_CONTEXT_BODY;
1745
1746 hit = match_line(opt, bol, eol, &col, &icol, ctx, collect_hits);
1747 *eol = ch;
1748
1749 if (collect_hits)
1750 goto next_line;
1751
1752 /* "grep -v -e foo -e bla" should list lines
1753 * that do not have either, so inversion should
1754 * be done outside.
1755 */
1756 if (opt->invert)
1757 hit = !hit;
1758 if (opt->unmatch_name_only) {
1759 if (hit)
1760 return 0;
1761 goto next_line;
1762 }
1763 if (hit) {
1764 count++;
1765 if (opt->status_only)
1766 return 1;
1767 if (opt->name_only) {
1768 show_name(opt, gs->name);
1769 return 1;
1770 }
1771 if (opt->count)
1772 goto next_line;
1773 if (binary_match_only) {
1774 opt->output(opt, "Binary file ", 12);
1775 output_color(opt, gs->name, strlen(gs->name),
1776 opt->colors[GREP_COLOR_FILENAME]);
1777 opt->output(opt, " matches\n", 9);
1778 return 1;
1779 }
1780 /* Hit at this line. If we haven't shown the
1781 * pre-context lines, we would need to show them.
1782 */
1783 if (opt->pre_context || opt->funcbody)
1784 show_pre_context(opt, gs, bol, eol, lno);
1785 else if (opt->funcname)
1786 show_funcname_line(opt, gs, bol, lno);
1787 cno = opt->invert ? icol : col;
1788 if (cno < 0) {
1789 /*
1790 * A negative cno indicates that there was no
1791 * match on the line. We are thus inverted and
1792 * being asked to show all lines that _don't_
1793 * match a given expression. Therefore, set cno
1794 * to 0 to suggest the whole line matches.
1795 */
1796 cno = 0;
1797 }
1798 show_line(opt, bol, eol, gs->name, lno, cno + 1, ':');
1799 last_hit = lno;
1800 if (opt->funcbody)
1801 show_function = 1;
1802 goto next_line;
1803 }
1804 if (show_function && (!peek_bol || peek_bol < bol)) {
1805 unsigned long peek_left = left;
1806 char *peek_eol = eol;
1807
1808 /*
1809 * Trailing empty lines are not interesting.
1810 * Peek past them to see if they belong to the
1811 * body of the current function.
1812 */
1813 peek_bol = bol;
1814 while (is_empty_line(peek_bol, peek_eol)) {
1815 peek_bol = peek_eol + 1;
1816 peek_eol = end_of_line(peek_bol, &peek_left);
1817 }
1818
1819 if (match_funcname(opt, gs, peek_bol, peek_eol))
1820 show_function = 0;
1821 }
1822 if (show_function ||
1823 (last_hit && lno <= last_hit + opt->post_context)) {
1824 /* If the last hit is within the post context,
1825 * we need to show this line.
1826 */
1827 show_line(opt, bol, eol, gs->name, lno, col + 1, '-');
1828 }
1829
1830 next_line:
1831 bol = eol + 1;
1832 if (!left)
1833 break;
1834 left--;
1835 lno++;
1836 }
1837
1838 if (collect_hits)
1839 return 0;
1840
1841 if (opt->status_only)
1842 return opt->unmatch_name_only;
1843 if (opt->unmatch_name_only) {
1844 /* We did not see any hit, so we want to show this */
1845 show_name(opt, gs->name);
1846 return 1;
1847 }
1848
1849 xdiff_clear_find_func(&xecfg);
1850 opt->priv = NULL;
1851
1852 /* NEEDSWORK:
1853 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1854 * which feels mostly useless but sometimes useful. Maybe
1855 * make it another option? For now suppress them.
1856 */
1857 if (opt->count && count) {
1858 char buf[32];
1859 if (opt->pathname) {
1860 output_color(opt, gs->name, strlen(gs->name),
1861 opt->colors[GREP_COLOR_FILENAME]);
1862 output_sep(opt, ':');
1863 }
1864 xsnprintf(buf, sizeof(buf), "%u\n", count);
1865 opt->output(opt, buf, strlen(buf));
1866 return 1;
1867 }
1868 return !!last_hit;
1869 }
1870
1871 static void clr_hit_marker(struct grep_expr *x)
1872 {
1873 /* All-hit markers are meaningful only at the very top level
1874 * OR node.
1875 */
1876 while (1) {
1877 x->hit = 0;
1878 if (x->node != GREP_NODE_OR)
1879 return;
1880 x->u.binary.left->hit = 0;
1881 x = x->u.binary.right;
1882 }
1883 }
1884
1885 static int chk_hit_marker(struct grep_expr *x)
1886 {
1887 /* Top level nodes have hit markers. See if they all are hits */
1888 while (1) {
1889 if (x->node != GREP_NODE_OR)
1890 return x->hit;
1891 if (!x->u.binary.left->hit)
1892 return 0;
1893 x = x->u.binary.right;
1894 }
1895 }
1896
1897 int grep_source(struct grep_opt *opt, struct grep_source *gs)
1898 {
1899 /*
1900 * we do not have to do the two-pass grep when we do not check
1901 * buffer-wide "all-match".
1902 */
1903 if (!opt->all_match)
1904 return grep_source_1(opt, gs, 0);
1905
1906 /* Otherwise the toplevel "or" terms hit a bit differently.
1907 * We first clear hit markers from them.
1908 */
1909 clr_hit_marker(opt->pattern_expression);
1910 grep_source_1(opt, gs, 1);
1911
1912 if (!chk_hit_marker(opt->pattern_expression))
1913 return 0;
1914
1915 return grep_source_1(opt, gs, 0);
1916 }
1917
1918 int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size)
1919 {
1920 struct grep_source gs;
1921 int r;
1922
1923 grep_source_init(&gs, GREP_SOURCE_BUF, NULL, NULL, NULL);
1924 gs.buf = buf;
1925 gs.size = size;
1926
1927 r = grep_source(opt, &gs);
1928
1929 grep_source_clear(&gs);
1930 return r;
1931 }
1932
1933 void grep_source_init(struct grep_source *gs, enum grep_source_type type,
1934 const char *name, const char *path,
1935 const void *identifier)
1936 {
1937 gs->type = type;
1938 gs->name = xstrdup_or_null(name);
1939 gs->path = xstrdup_or_null(path);
1940 gs->buf = NULL;
1941 gs->size = 0;
1942 gs->driver = NULL;
1943
1944 switch (type) {
1945 case GREP_SOURCE_FILE:
1946 gs->identifier = xstrdup(identifier);
1947 break;
1948 case GREP_SOURCE_OID:
1949 gs->identifier = oiddup(identifier);
1950 break;
1951 case GREP_SOURCE_BUF:
1952 gs->identifier = NULL;
1953 break;
1954 }
1955 }
1956
1957 void grep_source_clear(struct grep_source *gs)
1958 {
1959 FREE_AND_NULL(gs->name);
1960 FREE_AND_NULL(gs->path);
1961 FREE_AND_NULL(gs->identifier);
1962 grep_source_clear_data(gs);
1963 }
1964
1965 void grep_source_clear_data(struct grep_source *gs)
1966 {
1967 switch (gs->type) {
1968 case GREP_SOURCE_FILE:
1969 case GREP_SOURCE_OID:
1970 FREE_AND_NULL(gs->buf);
1971 gs->size = 0;
1972 break;
1973 case GREP_SOURCE_BUF:
1974 /* leave user-provided buf intact */
1975 break;
1976 }
1977 }
1978
1979 static int grep_source_load_oid(struct grep_source *gs)
1980 {
1981 enum object_type type;
1982
1983 gs->buf = read_object_file(gs->identifier, &type, &gs->size);
1984 if (!gs->buf)
1985 return error(_("'%s': unable to read %s"),
1986 gs->name,
1987 oid_to_hex(gs->identifier));
1988 return 0;
1989 }
1990
1991 static int grep_source_load_file(struct grep_source *gs)
1992 {
1993 const char *filename = gs->identifier;
1994 struct stat st;
1995 char *data;
1996 size_t size;
1997 int i;
1998
1999 if (lstat(filename, &st) < 0) {
2000 err_ret:
2001 if (errno != ENOENT)
2002 error_errno(_("failed to stat '%s'"), filename);
2003 return -1;
2004 }
2005 if (!S_ISREG(st.st_mode))
2006 return -1;
2007 size = xsize_t(st.st_size);
2008 i = open(filename, O_RDONLY);
2009 if (i < 0)
2010 goto err_ret;
2011 data = xmallocz(size);
2012 if (st.st_size != read_in_full(i, data, size)) {
2013 error_errno(_("'%s': short read"), filename);
2014 close(i);
2015 free(data);
2016 return -1;
2017 }
2018 close(i);
2019
2020 gs->buf = data;
2021 gs->size = size;
2022 return 0;
2023 }
2024
2025 static int grep_source_load(struct grep_source *gs)
2026 {
2027 if (gs->buf)
2028 return 0;
2029
2030 switch (gs->type) {
2031 case GREP_SOURCE_FILE:
2032 return grep_source_load_file(gs);
2033 case GREP_SOURCE_OID:
2034 return grep_source_load_oid(gs);
2035 case GREP_SOURCE_BUF:
2036 return gs->buf ? 0 : -1;
2037 }
2038 BUG("invalid grep_source type to load");
2039 }
2040
2041 void grep_source_load_driver(struct grep_source *gs,
2042 struct index_state *istate)
2043 {
2044 if (gs->driver)
2045 return;
2046
2047 grep_attr_lock();
2048 if (gs->path)
2049 gs->driver = userdiff_find_by_path(istate, gs->path);
2050 if (!gs->driver)
2051 gs->driver = userdiff_find_by_name("default");
2052 grep_attr_unlock();
2053 }
2054
2055 static int grep_source_is_binary(struct grep_source *gs,
2056 struct index_state *istate)
2057 {
2058 grep_source_load_driver(gs, istate);
2059 if (gs->driver->binary != -1)
2060 return gs->driver->binary;
2061
2062 if (!grep_source_load(gs))
2063 return buffer_is_binary(gs->buf, gs->size);
2064
2065 return 0;
2066 }