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