grep: adjust a redundant grep pattern type assignment
[git.git] / grep.c
blob817270d0819b6b0c3a474941a66af07dcb9c68d3
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"
11 static int grep_source_load(struct grep_source *gs);
12 static int grep_source_is_binary(struct grep_source *gs);
14 static struct grep_opt grep_defaults;
16 static void std_output(struct grep_opt *opt, const void *buf, size_t size)
18 fwrite(buf, size, 1, stdout);
22 * Initialize the grep_defaults template with hardcoded defaults.
23 * We could let the compiler do this, but without C99 initializers
24 * the code gets unwieldy and unreadable, so...
26 void init_grep_defaults(void)
28 struct grep_opt *opt = &grep_defaults;
29 static int run_once;
31 if (run_once)
32 return;
33 run_once++;
35 memset(opt, 0, sizeof(*opt));
36 opt->relative = 1;
37 opt->pathname = 1;
38 opt->regflags = REG_NEWLINE;
39 opt->max_depth = -1;
40 opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
41 color_set(opt->color_context, "");
42 color_set(opt->color_filename, "");
43 color_set(opt->color_function, "");
44 color_set(opt->color_lineno, "");
45 color_set(opt->color_match_context, GIT_COLOR_BOLD_RED);
46 color_set(opt->color_match_selected, GIT_COLOR_BOLD_RED);
47 color_set(opt->color_selected, "");
48 color_set(opt->color_sep, GIT_COLOR_CYAN);
49 opt->color = -1;
50 opt->output = std_output;
53 static int parse_pattern_type_arg(const char *opt, const char *arg)
55 if (!strcmp(arg, "default"))
56 return GREP_PATTERN_TYPE_UNSPECIFIED;
57 else if (!strcmp(arg, "basic"))
58 return GREP_PATTERN_TYPE_BRE;
59 else if (!strcmp(arg, "extended"))
60 return GREP_PATTERN_TYPE_ERE;
61 else if (!strcmp(arg, "fixed"))
62 return GREP_PATTERN_TYPE_FIXED;
63 else if (!strcmp(arg, "perl"))
64 return GREP_PATTERN_TYPE_PCRE;
65 die("bad %s argument: %s", opt, arg);
69 * Read the configuration file once and store it in
70 * the grep_defaults template.
72 int grep_config(const char *var, const char *value, void *cb)
74 struct grep_opt *opt = &grep_defaults;
75 char *color = NULL;
77 if (userdiff_config(var, value) < 0)
78 return -1;
80 if (!strcmp(var, "grep.extendedregexp")) {
81 opt->extended_regexp_option = git_config_bool(var, value);
82 return 0;
85 if (!strcmp(var, "grep.patterntype")) {
86 opt->pattern_type_option = parse_pattern_type_arg(var, value);
87 return 0;
90 if (!strcmp(var, "grep.linenumber")) {
91 opt->linenum = git_config_bool(var, value);
92 return 0;
95 if (!strcmp(var, "grep.fullname")) {
96 opt->relative = !git_config_bool(var, value);
97 return 0;
100 if (!strcmp(var, "color.grep"))
101 opt->color = git_config_colorbool(var, value);
102 else if (!strcmp(var, "color.grep.context"))
103 color = opt->color_context;
104 else if (!strcmp(var, "color.grep.filename"))
105 color = opt->color_filename;
106 else if (!strcmp(var, "color.grep.function"))
107 color = opt->color_function;
108 else if (!strcmp(var, "color.grep.linenumber"))
109 color = opt->color_lineno;
110 else if (!strcmp(var, "color.grep.matchcontext"))
111 color = opt->color_match_context;
112 else if (!strcmp(var, "color.grep.matchselected"))
113 color = opt->color_match_selected;
114 else if (!strcmp(var, "color.grep.selected"))
115 color = opt->color_selected;
116 else if (!strcmp(var, "color.grep.separator"))
117 color = opt->color_sep;
118 else if (!strcmp(var, "color.grep.match")) {
119 int rc = 0;
120 if (!value)
121 return config_error_nonbool(var);
122 rc |= color_parse(value, opt->color_match_context);
123 rc |= color_parse(value, opt->color_match_selected);
124 return rc;
127 if (color) {
128 if (!value)
129 return config_error_nonbool(var);
130 return color_parse(value, color);
132 return 0;
136 * Initialize one instance of grep_opt and copy the
137 * default values from the template we read the configuration
138 * information in an earlier call to git_config(grep_config).
140 void grep_init(struct grep_opt *opt, const char *prefix)
142 struct grep_opt *def = &grep_defaults;
144 memset(opt, 0, sizeof(*opt));
145 opt->prefix = prefix;
146 opt->prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
147 opt->pattern_tail = &opt->pattern_list;
148 opt->header_tail = &opt->header_list;
150 opt->color = def->color;
151 opt->extended_regexp_option = def->extended_regexp_option;
152 opt->pattern_type_option = def->pattern_type_option;
153 opt->linenum = def->linenum;
154 opt->max_depth = def->max_depth;
155 opt->pathname = def->pathname;
156 opt->regflags = def->regflags;
157 opt->relative = def->relative;
158 opt->output = def->output;
160 color_set(opt->color_context, def->color_context);
161 color_set(opt->color_filename, def->color_filename);
162 color_set(opt->color_function, def->color_function);
163 color_set(opt->color_lineno, def->color_lineno);
164 color_set(opt->color_match_context, def->color_match_context);
165 color_set(opt->color_match_selected, def->color_match_selected);
166 color_set(opt->color_selected, def->color_selected);
167 color_set(opt->color_sep, def->color_sep);
170 static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, struct grep_opt *opt)
172 switch (pattern_type) {
173 case GREP_PATTERN_TYPE_UNSPECIFIED:
174 /* fall through */
176 case GREP_PATTERN_TYPE_BRE:
177 opt->fixed = 0;
178 opt->pcre1 = 0;
179 opt->pcre2 = 0;
180 break;
182 case GREP_PATTERN_TYPE_ERE:
183 opt->fixed = 0;
184 opt->pcre1 = 0;
185 opt->pcre2 = 0;
186 opt->regflags |= REG_EXTENDED;
187 break;
189 case GREP_PATTERN_TYPE_FIXED:
190 opt->fixed = 1;
191 opt->pcre1 = 0;
192 opt->pcre2 = 0;
193 break;
195 case GREP_PATTERN_TYPE_PCRE:
196 opt->fixed = 0;
197 #ifdef USE_LIBPCRE2
198 opt->pcre1 = 0;
199 opt->pcre2 = 1;
200 #else
202 * It's important that pcre1 always be assigned to
203 * even when there's no USE_LIBPCRE* defined. We still
204 * call the PCRE stub function, it just dies with
205 * "cannot use Perl-compatible regexes[...]".
207 opt->pcre1 = 1;
208 opt->pcre2 = 0;
209 #endif
210 break;
214 void grep_commit_pattern_type(enum grep_pattern_type pattern_type, struct grep_opt *opt)
216 if (pattern_type != GREP_PATTERN_TYPE_UNSPECIFIED)
217 grep_set_pattern_type_option(pattern_type, opt);
218 else if (opt->pattern_type_option != GREP_PATTERN_TYPE_UNSPECIFIED)
219 grep_set_pattern_type_option(opt->pattern_type_option, opt);
220 else if (opt->extended_regexp_option)
221 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, opt);
224 static struct grep_pat *create_grep_pat(const char *pat, size_t patlen,
225 const char *origin, int no,
226 enum grep_pat_token t,
227 enum grep_header_field field)
229 struct grep_pat *p = xcalloc(1, sizeof(*p));
230 p->pattern = xmemdupz(pat, patlen);
231 p->patternlen = patlen;
232 p->origin = origin;
233 p->no = no;
234 p->token = t;
235 p->field = field;
236 return p;
239 static void do_append_grep_pat(struct grep_pat ***tail, struct grep_pat *p)
241 **tail = p;
242 *tail = &p->next;
243 p->next = NULL;
245 switch (p->token) {
246 case GREP_PATTERN: /* atom */
247 case GREP_PATTERN_HEAD:
248 case GREP_PATTERN_BODY:
249 for (;;) {
250 struct grep_pat *new_pat;
251 size_t len = 0;
252 char *cp = p->pattern + p->patternlen, *nl = NULL;
253 while (++len <= p->patternlen) {
254 if (*(--cp) == '\n') {
255 nl = cp;
256 break;
259 if (!nl)
260 break;
261 new_pat = create_grep_pat(nl + 1, len - 1, p->origin,
262 p->no, p->token, p->field);
263 new_pat->next = p->next;
264 if (!p->next)
265 *tail = &new_pat->next;
266 p->next = new_pat;
267 *nl = '\0';
268 p->patternlen -= len;
270 break;
271 default:
272 break;
276 void append_header_grep_pattern(struct grep_opt *opt,
277 enum grep_header_field field, const char *pat)
279 struct grep_pat *p = create_grep_pat(pat, strlen(pat), "header", 0,
280 GREP_PATTERN_HEAD, field);
281 if (field == GREP_HEADER_REFLOG)
282 opt->use_reflog_filter = 1;
283 do_append_grep_pat(&opt->header_tail, p);
286 void append_grep_pattern(struct grep_opt *opt, const char *pat,
287 const char *origin, int no, enum grep_pat_token t)
289 append_grep_pat(opt, pat, strlen(pat), origin, no, t);
292 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen,
293 const char *origin, int no, enum grep_pat_token t)
295 struct grep_pat *p = create_grep_pat(pat, patlen, origin, no, t, 0);
296 do_append_grep_pat(&opt->pattern_tail, p);
299 struct grep_opt *grep_opt_dup(const struct grep_opt *opt)
301 struct grep_pat *pat;
302 struct grep_opt *ret = xmalloc(sizeof(struct grep_opt));
303 *ret = *opt;
305 ret->pattern_list = NULL;
306 ret->pattern_tail = &ret->pattern_list;
308 for(pat = opt->pattern_list; pat != NULL; pat = pat->next)
310 if(pat->token == GREP_PATTERN_HEAD)
311 append_header_grep_pattern(ret, pat->field,
312 pat->pattern);
313 else
314 append_grep_pat(ret, pat->pattern, pat->patternlen,
315 pat->origin, pat->no, pat->token);
318 return ret;
321 static NORETURN void compile_regexp_failed(const struct grep_pat *p,
322 const char *error)
324 char where[1024];
326 if (p->no)
327 xsnprintf(where, sizeof(where), "In '%s' at %d, ", p->origin, p->no);
328 else if (p->origin)
329 xsnprintf(where, sizeof(where), "%s, ", p->origin);
330 else
331 where[0] = 0;
333 die("%s'%s': %s", where, p->pattern, error);
336 static int is_fixed(const char *s, size_t len)
338 size_t i;
340 for (i = 0; i < len; i++) {
341 if (is_regex_special(s[i]))
342 return 0;
345 return 1;
348 static int has_null(const char *s, size_t len)
351 * regcomp cannot accept patterns with NULs so when using it
352 * we consider any pattern containing a NUL fixed.
354 if (memchr(s, 0, len))
355 return 1;
357 return 0;
360 #ifdef USE_LIBPCRE1
361 static void compile_pcre1_regexp(struct grep_pat *p, const struct grep_opt *opt)
363 const char *error;
364 int erroffset;
365 int options = PCRE_MULTILINE;
367 if (opt->ignore_case) {
368 if (has_non_ascii(p->pattern))
369 p->pcre1_tables = pcre_maketables();
370 options |= PCRE_CASELESS;
372 if (is_utf8_locale() && has_non_ascii(p->pattern))
373 options |= PCRE_UTF8;
375 p->pcre1_regexp = pcre_compile(p->pattern, options, &error, &erroffset,
376 p->pcre1_tables);
377 if (!p->pcre1_regexp)
378 compile_regexp_failed(p, error);
380 p->pcre1_extra_info = pcre_study(p->pcre1_regexp, PCRE_STUDY_JIT_COMPILE, &error);
381 if (!p->pcre1_extra_info && error)
382 die("%s", error);
384 #ifdef GIT_PCRE1_USE_JIT
385 pcre_config(PCRE_CONFIG_JIT, &p->pcre1_jit_on);
386 if (p->pcre1_jit_on == 1) {
387 p->pcre1_jit_stack = pcre_jit_stack_alloc(1, 1024 * 1024);
388 if (!p->pcre1_jit_stack)
389 die("Couldn't allocate PCRE JIT stack");
390 pcre_assign_jit_stack(p->pcre1_extra_info, NULL, p->pcre1_jit_stack);
391 } else if (p->pcre1_jit_on != 0) {
392 die("BUG: The pcre1_jit_on variable should be 0 or 1, not %d",
393 p->pcre1_jit_on);
395 #endif
398 static int pcre1match(struct grep_pat *p, const char *line, const char *eol,
399 regmatch_t *match, int eflags)
401 int ovector[30], ret, flags = 0;
403 if (eflags & REG_NOTBOL)
404 flags |= PCRE_NOTBOL;
406 #ifdef GIT_PCRE1_USE_JIT
407 if (p->pcre1_jit_on) {
408 ret = pcre_jit_exec(p->pcre1_regexp, p->pcre1_extra_info, line,
409 eol - line, 0, flags, ovector,
410 ARRAY_SIZE(ovector), p->pcre1_jit_stack);
411 } else
412 #endif
414 ret = pcre_exec(p->pcre1_regexp, p->pcre1_extra_info, line,
415 eol - line, 0, flags, ovector,
416 ARRAY_SIZE(ovector));
419 if (ret < 0 && ret != PCRE_ERROR_NOMATCH)
420 die("pcre_exec failed with error code %d", ret);
421 if (ret > 0) {
422 ret = 0;
423 match->rm_so = ovector[0];
424 match->rm_eo = ovector[1];
427 return ret;
430 static void free_pcre1_regexp(struct grep_pat *p)
432 pcre_free(p->pcre1_regexp);
433 #ifdef GIT_PCRE1_USE_JIT
434 if (p->pcre1_jit_on) {
435 pcre_free_study(p->pcre1_extra_info);
436 pcre_jit_stack_free(p->pcre1_jit_stack);
437 } else
438 #endif
440 pcre_free(p->pcre1_extra_info);
442 pcre_free((void *)p->pcre1_tables);
444 #else /* !USE_LIBPCRE1 */
445 static void compile_pcre1_regexp(struct grep_pat *p, const struct grep_opt *opt)
447 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
450 static int pcre1match(struct grep_pat *p, const char *line, const char *eol,
451 regmatch_t *match, int eflags)
453 return 1;
456 static void free_pcre1_regexp(struct grep_pat *p)
459 #endif /* !USE_LIBPCRE1 */
461 #ifdef USE_LIBPCRE2
462 static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt)
464 int error;
465 PCRE2_UCHAR errbuf[256];
466 PCRE2_SIZE erroffset;
467 int options = PCRE2_MULTILINE;
468 const uint8_t *character_tables = NULL;
469 int jitret;
471 assert(opt->pcre2);
473 p->pcre2_compile_context = NULL;
475 if (opt->ignore_case) {
476 if (has_non_ascii(p->pattern)) {
477 character_tables = pcre2_maketables(NULL);
478 p->pcre2_compile_context = pcre2_compile_context_create(NULL);
479 pcre2_set_character_tables(p->pcre2_compile_context, character_tables);
481 options |= PCRE2_CASELESS;
483 if (is_utf8_locale() && has_non_ascii(p->pattern))
484 options |= PCRE2_UTF;
486 p->pcre2_pattern = pcre2_compile((PCRE2_SPTR)p->pattern,
487 p->patternlen, options, &error, &erroffset,
488 p->pcre2_compile_context);
490 if (p->pcre2_pattern) {
491 p->pcre2_match_data = pcre2_match_data_create_from_pattern(p->pcre2_pattern, NULL);
492 if (!p->pcre2_match_data)
493 die("Couldn't allocate PCRE2 match data");
494 } else {
495 pcre2_get_error_message(error, errbuf, sizeof(errbuf));
496 compile_regexp_failed(p, (const char *)&errbuf);
499 pcre2_config(PCRE2_CONFIG_JIT, &p->pcre2_jit_on);
500 if (p->pcre2_jit_on == 1) {
501 jitret = pcre2_jit_compile(p->pcre2_pattern, PCRE2_JIT_COMPLETE);
502 if (jitret)
503 die("Couldn't JIT the PCRE2 pattern '%s', got '%d'\n", p->pattern, jitret);
504 p->pcre2_jit_stack = pcre2_jit_stack_create(1, 1024 * 1024, NULL);
505 if (!p->pcre2_jit_stack)
506 die("Couldn't allocate PCRE2 JIT stack");
507 p->pcre2_match_context = pcre2_match_context_create(NULL);
508 if (!p->pcre2_match_context)
509 die("Couldn't allocate PCRE2 match context");
510 pcre2_jit_stack_assign(p->pcre2_match_context, NULL, p->pcre2_jit_stack);
511 } else if (p->pcre2_jit_on != 0) {
512 die("BUG: The pcre2_jit_on variable should be 0 or 1, not %d",
513 p->pcre1_jit_on);
517 static int pcre2match(struct grep_pat *p, const char *line, const char *eol,
518 regmatch_t *match, int eflags)
520 int ret, flags = 0;
521 PCRE2_SIZE *ovector;
522 PCRE2_UCHAR errbuf[256];
524 if (eflags & REG_NOTBOL)
525 flags |= PCRE2_NOTBOL;
527 if (p->pcre2_jit_on)
528 ret = pcre2_jit_match(p->pcre2_pattern, (unsigned char *)line,
529 eol - line, 0, flags, p->pcre2_match_data,
530 NULL);
531 else
532 ret = pcre2_match(p->pcre2_pattern, (unsigned char *)line,
533 eol - line, 0, flags, p->pcre2_match_data,
534 NULL);
536 if (ret < 0 && ret != PCRE2_ERROR_NOMATCH) {
537 pcre2_get_error_message(ret, errbuf, sizeof(errbuf));
538 die("%s failed with error code %d: %s",
539 (p->pcre2_jit_on ? "pcre2_jit_match" : "pcre2_match"), ret,
540 errbuf);
542 if (ret > 0) {
543 ovector = pcre2_get_ovector_pointer(p->pcre2_match_data);
544 ret = 0;
545 match->rm_so = (int)ovector[0];
546 match->rm_eo = (int)ovector[1];
549 return ret;
552 static void free_pcre2_pattern(struct grep_pat *p)
554 pcre2_compile_context_free(p->pcre2_compile_context);
555 pcre2_code_free(p->pcre2_pattern);
556 pcre2_match_data_free(p->pcre2_match_data);
557 pcre2_jit_stack_free(p->pcre2_jit_stack);
558 pcre2_match_context_free(p->pcre2_match_context);
560 #else /* !USE_LIBPCRE2 */
561 static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt)
564 * Unreachable until USE_LIBPCRE2 becomes synonymous with
565 * USE_LIBPCRE. See the sibling comment in
566 * grep_set_pattern_type_option().
568 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
571 static int pcre2match(struct grep_pat *p, const char *line, const char *eol,
572 regmatch_t *match, int eflags)
574 return 1;
577 static void free_pcre2_pattern(struct grep_pat *p)
580 #endif /* !USE_LIBPCRE2 */
582 static void compile_fixed_regexp(struct grep_pat *p, struct grep_opt *opt)
584 struct strbuf sb = STRBUF_INIT;
585 int err;
586 int regflags = opt->regflags;
588 basic_regex_quote_buf(&sb, p->pattern);
589 if (opt->ignore_case)
590 regflags |= REG_ICASE;
591 err = regcomp(&p->regexp, sb.buf, regflags);
592 if (opt->debug)
593 fprintf(stderr, "fixed %s\n", sb.buf);
594 strbuf_release(&sb);
595 if (err) {
596 char errbuf[1024];
597 regerror(err, &p->regexp, errbuf, sizeof(errbuf));
598 regfree(&p->regexp);
599 compile_regexp_failed(p, errbuf);
603 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
605 int icase, ascii_only;
606 int err;
608 p->word_regexp = opt->word_regexp;
609 p->ignore_case = opt->ignore_case;
610 icase = opt->regflags & REG_ICASE || p->ignore_case;
611 ascii_only = !has_non_ascii(p->pattern);
614 * Even when -F (fixed) asks us to do a non-regexp search, we
615 * may not be able to correctly case-fold when -i
616 * (ignore-case) is asked (in which case, we'll synthesize a
617 * regexp to match the pattern that matches regexp special
618 * characters literally, while ignoring case differences). On
619 * the other hand, even without -F, if the pattern does not
620 * have any regexp special characters and there is no need for
621 * case-folding search, we can internally turn it into a
622 * simple string match using kws. p->fixed tells us if we
623 * want to use kws.
625 if (opt->fixed ||
626 has_null(p->pattern, p->patternlen) ||
627 is_fixed(p->pattern, p->patternlen))
628 p->fixed = !icase || ascii_only;
629 else
630 p->fixed = 0;
632 if (p->fixed) {
633 p->kws = kwsalloc(icase ? tolower_trans_tbl : NULL);
634 kwsincr(p->kws, p->pattern, p->patternlen);
635 kwsprep(p->kws);
636 return;
637 } else if (opt->fixed) {
639 * We come here when the pattern has the non-ascii
640 * characters we cannot case-fold, and asked to
641 * ignore-case.
643 compile_fixed_regexp(p, opt);
644 return;
647 if (opt->pcre2) {
648 compile_pcre2_pattern(p, opt);
649 return;
652 if (opt->pcre1) {
653 compile_pcre1_regexp(p, opt);
654 return;
657 err = regcomp(&p->regexp, p->pattern, opt->regflags);
658 if (err) {
659 char errbuf[1024];
660 regerror(err, &p->regexp, errbuf, 1024);
661 regfree(&p->regexp);
662 compile_regexp_failed(p, errbuf);
666 static struct grep_expr *compile_pattern_or(struct grep_pat **);
667 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
669 struct grep_pat *p;
670 struct grep_expr *x;
672 p = *list;
673 if (!p)
674 return NULL;
675 switch (p->token) {
676 case GREP_PATTERN: /* atom */
677 case GREP_PATTERN_HEAD:
678 case GREP_PATTERN_BODY:
679 x = xcalloc(1, sizeof (struct grep_expr));
680 x->node = GREP_NODE_ATOM;
681 x->u.atom = p;
682 *list = p->next;
683 return x;
684 case GREP_OPEN_PAREN:
685 *list = p->next;
686 x = compile_pattern_or(list);
687 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
688 die("unmatched parenthesis");
689 *list = (*list)->next;
690 return x;
691 default:
692 return NULL;
696 static struct grep_expr *compile_pattern_not(struct grep_pat **list)
698 struct grep_pat *p;
699 struct grep_expr *x;
701 p = *list;
702 if (!p)
703 return NULL;
704 switch (p->token) {
705 case GREP_NOT:
706 if (!p->next)
707 die("--not not followed by pattern expression");
708 *list = p->next;
709 x = xcalloc(1, sizeof (struct grep_expr));
710 x->node = GREP_NODE_NOT;
711 x->u.unary = compile_pattern_not(list);
712 if (!x->u.unary)
713 die("--not followed by non pattern expression");
714 return x;
715 default:
716 return compile_pattern_atom(list);
720 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
722 struct grep_pat *p;
723 struct grep_expr *x, *y, *z;
725 x = compile_pattern_not(list);
726 p = *list;
727 if (p && p->token == GREP_AND) {
728 if (!p->next)
729 die("--and not followed by pattern expression");
730 *list = p->next;
731 y = compile_pattern_and(list);
732 if (!y)
733 die("--and not followed by pattern expression");
734 z = xcalloc(1, sizeof (struct grep_expr));
735 z->node = GREP_NODE_AND;
736 z->u.binary.left = x;
737 z->u.binary.right = y;
738 return z;
740 return x;
743 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
745 struct grep_pat *p;
746 struct grep_expr *x, *y, *z;
748 x = compile_pattern_and(list);
749 p = *list;
750 if (x && p && p->token != GREP_CLOSE_PAREN) {
751 y = compile_pattern_or(list);
752 if (!y)
753 die("not a pattern expression %s", p->pattern);
754 z = xcalloc(1, sizeof (struct grep_expr));
755 z->node = GREP_NODE_OR;
756 z->u.binary.left = x;
757 z->u.binary.right = y;
758 return z;
760 return x;
763 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
765 return compile_pattern_or(list);
768 static void indent(int in)
770 while (in-- > 0)
771 fputc(' ', stderr);
774 static void dump_grep_pat(struct grep_pat *p)
776 switch (p->token) {
777 case GREP_AND: fprintf(stderr, "*and*"); break;
778 case GREP_OPEN_PAREN: fprintf(stderr, "*(*"); break;
779 case GREP_CLOSE_PAREN: fprintf(stderr, "*)*"); break;
780 case GREP_NOT: fprintf(stderr, "*not*"); break;
781 case GREP_OR: fprintf(stderr, "*or*"); break;
783 case GREP_PATTERN: fprintf(stderr, "pattern"); break;
784 case GREP_PATTERN_HEAD: fprintf(stderr, "pattern_head"); break;
785 case GREP_PATTERN_BODY: fprintf(stderr, "pattern_body"); break;
788 switch (p->token) {
789 default: break;
790 case GREP_PATTERN_HEAD:
791 fprintf(stderr, "<head %d>", p->field); break;
792 case GREP_PATTERN_BODY:
793 fprintf(stderr, "<body>"); break;
795 switch (p->token) {
796 default: break;
797 case GREP_PATTERN_HEAD:
798 case GREP_PATTERN_BODY:
799 case GREP_PATTERN:
800 fprintf(stderr, "%.*s", (int)p->patternlen, p->pattern);
801 break;
803 fputc('\n', stderr);
806 static void dump_grep_expression_1(struct grep_expr *x, int in)
808 indent(in);
809 switch (x->node) {
810 case GREP_NODE_TRUE:
811 fprintf(stderr, "true\n");
812 break;
813 case GREP_NODE_ATOM:
814 dump_grep_pat(x->u.atom);
815 break;
816 case GREP_NODE_NOT:
817 fprintf(stderr, "(not\n");
818 dump_grep_expression_1(x->u.unary, in+1);
819 indent(in);
820 fprintf(stderr, ")\n");
821 break;
822 case GREP_NODE_AND:
823 fprintf(stderr, "(and\n");
824 dump_grep_expression_1(x->u.binary.left, in+1);
825 dump_grep_expression_1(x->u.binary.right, in+1);
826 indent(in);
827 fprintf(stderr, ")\n");
828 break;
829 case GREP_NODE_OR:
830 fprintf(stderr, "(or\n");
831 dump_grep_expression_1(x->u.binary.left, in+1);
832 dump_grep_expression_1(x->u.binary.right, in+1);
833 indent(in);
834 fprintf(stderr, ")\n");
835 break;
839 static void dump_grep_expression(struct grep_opt *opt)
841 struct grep_expr *x = opt->pattern_expression;
843 if (opt->all_match)
844 fprintf(stderr, "[all-match]\n");
845 dump_grep_expression_1(x, 0);
846 fflush(NULL);
849 static struct grep_expr *grep_true_expr(void)
851 struct grep_expr *z = xcalloc(1, sizeof(*z));
852 z->node = GREP_NODE_TRUE;
853 return z;
856 static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
858 struct grep_expr *z = xcalloc(1, sizeof(*z));
859 z->node = GREP_NODE_OR;
860 z->u.binary.left = left;
861 z->u.binary.right = right;
862 return z;
865 static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
867 struct grep_pat *p;
868 struct grep_expr *header_expr;
869 struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]);
870 enum grep_header_field fld;
872 if (!opt->header_list)
873 return NULL;
875 for (p = opt->header_list; p; p = p->next) {
876 if (p->token != GREP_PATTERN_HEAD)
877 die("BUG: a non-header pattern in grep header list.");
878 if (p->field < GREP_HEADER_FIELD_MIN ||
879 GREP_HEADER_FIELD_MAX <= p->field)
880 die("BUG: unknown header field %d", p->field);
881 compile_regexp(p, opt);
884 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++)
885 header_group[fld] = NULL;
887 for (p = opt->header_list; p; p = p->next) {
888 struct grep_expr *h;
889 struct grep_pat *pp = p;
891 h = compile_pattern_atom(&pp);
892 if (!h || pp != p->next)
893 die("BUG: malformed header expr");
894 if (!header_group[p->field]) {
895 header_group[p->field] = h;
896 continue;
898 header_group[p->field] = grep_or_expr(h, header_group[p->field]);
901 header_expr = NULL;
903 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) {
904 if (!header_group[fld])
905 continue;
906 if (!header_expr)
907 header_expr = grep_true_expr();
908 header_expr = grep_or_expr(header_group[fld], header_expr);
910 return header_expr;
913 static struct grep_expr *grep_splice_or(struct grep_expr *x, struct grep_expr *y)
915 struct grep_expr *z = x;
917 while (x) {
918 assert(x->node == GREP_NODE_OR);
919 if (x->u.binary.right &&
920 x->u.binary.right->node == GREP_NODE_TRUE) {
921 x->u.binary.right = y;
922 break;
924 x = x->u.binary.right;
926 return z;
929 static void compile_grep_patterns_real(struct grep_opt *opt)
931 struct grep_pat *p;
932 struct grep_expr *header_expr = prep_header_patterns(opt);
934 for (p = opt->pattern_list; p; p = p->next) {
935 switch (p->token) {
936 case GREP_PATTERN: /* atom */
937 case GREP_PATTERN_HEAD:
938 case GREP_PATTERN_BODY:
939 compile_regexp(p, opt);
940 break;
941 default:
942 opt->extended = 1;
943 break;
947 if (opt->all_match || header_expr)
948 opt->extended = 1;
949 else if (!opt->extended && !opt->debug)
950 return;
952 p = opt->pattern_list;
953 if (p)
954 opt->pattern_expression = compile_pattern_expr(&p);
955 if (p)
956 die("incomplete pattern expression: %s", p->pattern);
958 if (!header_expr)
959 return;
961 if (!opt->pattern_expression)
962 opt->pattern_expression = header_expr;
963 else if (opt->all_match)
964 opt->pattern_expression = grep_splice_or(header_expr,
965 opt->pattern_expression);
966 else
967 opt->pattern_expression = grep_or_expr(opt->pattern_expression,
968 header_expr);
969 opt->all_match = 1;
972 void compile_grep_patterns(struct grep_opt *opt)
974 compile_grep_patterns_real(opt);
975 if (opt->debug)
976 dump_grep_expression(opt);
979 static void free_pattern_expr(struct grep_expr *x)
981 switch (x->node) {
982 case GREP_NODE_TRUE:
983 case GREP_NODE_ATOM:
984 break;
985 case GREP_NODE_NOT:
986 free_pattern_expr(x->u.unary);
987 break;
988 case GREP_NODE_AND:
989 case GREP_NODE_OR:
990 free_pattern_expr(x->u.binary.left);
991 free_pattern_expr(x->u.binary.right);
992 break;
994 free(x);
997 void free_grep_patterns(struct grep_opt *opt)
999 struct grep_pat *p, *n;
1001 for (p = opt->pattern_list; p; p = n) {
1002 n = p->next;
1003 switch (p->token) {
1004 case GREP_PATTERN: /* atom */
1005 case GREP_PATTERN_HEAD:
1006 case GREP_PATTERN_BODY:
1007 if (p->kws)
1008 kwsfree(p->kws);
1009 else if (p->pcre1_regexp)
1010 free_pcre1_regexp(p);
1011 else if (p->pcre2_pattern)
1012 free_pcre2_pattern(p);
1013 else
1014 regfree(&p->regexp);
1015 free(p->pattern);
1016 break;
1017 default:
1018 break;
1020 free(p);
1023 if (!opt->extended)
1024 return;
1025 free_pattern_expr(opt->pattern_expression);
1028 static char *end_of_line(char *cp, unsigned long *left)
1030 unsigned long l = *left;
1031 while (l && *cp != '\n') {
1032 l--;
1033 cp++;
1035 *left = l;
1036 return cp;
1039 static int word_char(char ch)
1041 return isalnum(ch) || ch == '_';
1044 static void output_color(struct grep_opt *opt, const void *data, size_t size,
1045 const char *color)
1047 if (want_color(opt->color) && color && color[0]) {
1048 opt->output(opt, color, strlen(color));
1049 opt->output(opt, data, size);
1050 opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
1051 } else
1052 opt->output(opt, data, size);
1055 static void output_sep(struct grep_opt *opt, char sign)
1057 if (opt->null_following_name)
1058 opt->output(opt, "\0", 1);
1059 else
1060 output_color(opt, &sign, 1, opt->color_sep);
1063 static void show_name(struct grep_opt *opt, const char *name)
1065 output_color(opt, name, strlen(name), opt->color_filename);
1066 opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
1069 static int fixmatch(struct grep_pat *p, char *line, char *eol,
1070 regmatch_t *match)
1072 struct kwsmatch kwsm;
1073 size_t offset = kwsexec(p->kws, line, eol - line, &kwsm);
1074 if (offset == -1) {
1075 match->rm_so = match->rm_eo = -1;
1076 return REG_NOMATCH;
1077 } else {
1078 match->rm_so = offset;
1079 match->rm_eo = match->rm_so + kwsm.size[0];
1080 return 0;
1084 static int patmatch(struct grep_pat *p, char *line, char *eol,
1085 regmatch_t *match, int eflags)
1087 int hit;
1089 if (p->fixed)
1090 hit = !fixmatch(p, line, eol, match);
1091 else if (p->pcre1_regexp)
1092 hit = !pcre1match(p, line, eol, match, eflags);
1093 else if (p->pcre2_pattern)
1094 hit = !pcre2match(p, line, eol, match, eflags);
1095 else
1096 hit = !regexec_buf(&p->regexp, line, eol - line, 1, match,
1097 eflags);
1099 return hit;
1102 static int strip_timestamp(char *bol, char **eol_p)
1104 char *eol = *eol_p;
1105 int ch;
1107 while (bol < --eol) {
1108 if (*eol != '>')
1109 continue;
1110 *eol_p = ++eol;
1111 ch = *eol;
1112 *eol = '\0';
1113 return ch;
1115 return 0;
1118 static struct {
1119 const char *field;
1120 size_t len;
1121 } header_field[] = {
1122 { "author ", 7 },
1123 { "committer ", 10 },
1124 { "reflog ", 7 },
1127 static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
1128 enum grep_context ctx,
1129 regmatch_t *pmatch, int eflags)
1131 int hit = 0;
1132 int saved_ch = 0;
1133 const char *start = bol;
1135 if ((p->token != GREP_PATTERN) &&
1136 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
1137 return 0;
1139 if (p->token == GREP_PATTERN_HEAD) {
1140 const char *field;
1141 size_t len;
1142 assert(p->field < ARRAY_SIZE(header_field));
1143 field = header_field[p->field].field;
1144 len = header_field[p->field].len;
1145 if (strncmp(bol, field, len))
1146 return 0;
1147 bol += len;
1148 switch (p->field) {
1149 case GREP_HEADER_AUTHOR:
1150 case GREP_HEADER_COMMITTER:
1151 saved_ch = strip_timestamp(bol, &eol);
1152 break;
1153 default:
1154 break;
1158 again:
1159 hit = patmatch(p, bol, eol, pmatch, eflags);
1161 if (hit && p->word_regexp) {
1162 if ((pmatch[0].rm_so < 0) ||
1163 (eol - bol) < pmatch[0].rm_so ||
1164 (pmatch[0].rm_eo < 0) ||
1165 (eol - bol) < pmatch[0].rm_eo)
1166 die("regexp returned nonsense");
1168 /* Match beginning must be either beginning of the
1169 * line, or at word boundary (i.e. the last char must
1170 * not be a word char). Similarly, match end must be
1171 * either end of the line, or at word boundary
1172 * (i.e. the next char must not be a word char).
1174 if ( ((pmatch[0].rm_so == 0) ||
1175 !word_char(bol[pmatch[0].rm_so-1])) &&
1176 ((pmatch[0].rm_eo == (eol-bol)) ||
1177 !word_char(bol[pmatch[0].rm_eo])) )
1179 else
1180 hit = 0;
1182 /* Words consist of at least one character. */
1183 if (pmatch->rm_so == pmatch->rm_eo)
1184 hit = 0;
1186 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
1187 /* There could be more than one match on the
1188 * line, and the first match might not be
1189 * strict word match. But later ones could be!
1190 * Forward to the next possible start, i.e. the
1191 * next position following a non-word char.
1193 bol = pmatch[0].rm_so + bol + 1;
1194 while (word_char(bol[-1]) && bol < eol)
1195 bol++;
1196 eflags |= REG_NOTBOL;
1197 if (bol < eol)
1198 goto again;
1201 if (p->token == GREP_PATTERN_HEAD && saved_ch)
1202 *eol = saved_ch;
1203 if (hit) {
1204 pmatch[0].rm_so += bol - start;
1205 pmatch[0].rm_eo += bol - start;
1207 return hit;
1210 static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
1211 enum grep_context ctx, int collect_hits)
1213 int h = 0;
1214 regmatch_t match;
1216 if (!x)
1217 die("Not a valid grep expression");
1218 switch (x->node) {
1219 case GREP_NODE_TRUE:
1220 h = 1;
1221 break;
1222 case GREP_NODE_ATOM:
1223 h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
1224 break;
1225 case GREP_NODE_NOT:
1226 h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
1227 break;
1228 case GREP_NODE_AND:
1229 if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
1230 return 0;
1231 h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
1232 break;
1233 case GREP_NODE_OR:
1234 if (!collect_hits)
1235 return (match_expr_eval(x->u.binary.left,
1236 bol, eol, ctx, 0) ||
1237 match_expr_eval(x->u.binary.right,
1238 bol, eol, ctx, 0));
1239 h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
1240 x->u.binary.left->hit |= h;
1241 h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
1242 break;
1243 default:
1244 die("Unexpected node type (internal error) %d", x->node);
1246 if (collect_hits)
1247 x->hit |= h;
1248 return h;
1251 static int match_expr(struct grep_opt *opt, char *bol, char *eol,
1252 enum grep_context ctx, int collect_hits)
1254 struct grep_expr *x = opt->pattern_expression;
1255 return match_expr_eval(x, bol, eol, ctx, collect_hits);
1258 static int match_line(struct grep_opt *opt, char *bol, char *eol,
1259 enum grep_context ctx, int collect_hits)
1261 struct grep_pat *p;
1262 regmatch_t match;
1264 if (opt->extended)
1265 return match_expr(opt, bol, eol, ctx, collect_hits);
1267 /* we do not call with collect_hits without being extended */
1268 for (p = opt->pattern_list; p; p = p->next) {
1269 if (match_one_pattern(p, bol, eol, ctx, &match, 0))
1270 return 1;
1272 return 0;
1275 static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
1276 enum grep_context ctx,
1277 regmatch_t *pmatch, int eflags)
1279 regmatch_t match;
1281 if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
1282 return 0;
1283 if (match.rm_so < 0 || match.rm_eo < 0)
1284 return 0;
1285 if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
1286 if (match.rm_so > pmatch->rm_so)
1287 return 1;
1288 if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
1289 return 1;
1291 pmatch->rm_so = match.rm_so;
1292 pmatch->rm_eo = match.rm_eo;
1293 return 1;
1296 static int next_match(struct grep_opt *opt, char *bol, char *eol,
1297 enum grep_context ctx, regmatch_t *pmatch, int eflags)
1299 struct grep_pat *p;
1300 int hit = 0;
1302 pmatch->rm_so = pmatch->rm_eo = -1;
1303 if (bol < eol) {
1304 for (p = opt->pattern_list; p; p = p->next) {
1305 switch (p->token) {
1306 case GREP_PATTERN: /* atom */
1307 case GREP_PATTERN_HEAD:
1308 case GREP_PATTERN_BODY:
1309 hit |= match_next_pattern(p, bol, eol, ctx,
1310 pmatch, eflags);
1311 break;
1312 default:
1313 break;
1317 return hit;
1320 static void show_line(struct grep_opt *opt, char *bol, char *eol,
1321 const char *name, unsigned lno, char sign)
1323 int rest = eol - bol;
1324 const char *match_color, *line_color = NULL;
1326 if (opt->file_break && opt->last_shown == 0) {
1327 if (opt->show_hunk_mark)
1328 opt->output(opt, "\n", 1);
1329 } else if (opt->pre_context || opt->post_context || opt->funcbody) {
1330 if (opt->last_shown == 0) {
1331 if (opt->show_hunk_mark) {
1332 output_color(opt, "--", 2, opt->color_sep);
1333 opt->output(opt, "\n", 1);
1335 } else if (lno > opt->last_shown + 1) {
1336 output_color(opt, "--", 2, opt->color_sep);
1337 opt->output(opt, "\n", 1);
1340 if (opt->heading && opt->last_shown == 0) {
1341 output_color(opt, name, strlen(name), opt->color_filename);
1342 opt->output(opt, "\n", 1);
1344 opt->last_shown = lno;
1346 if (!opt->heading && opt->pathname) {
1347 output_color(opt, name, strlen(name), opt->color_filename);
1348 output_sep(opt, sign);
1350 if (opt->linenum) {
1351 char buf[32];
1352 xsnprintf(buf, sizeof(buf), "%d", lno);
1353 output_color(opt, buf, strlen(buf), opt->color_lineno);
1354 output_sep(opt, sign);
1356 if (opt->color) {
1357 regmatch_t match;
1358 enum grep_context ctx = GREP_CONTEXT_BODY;
1359 int ch = *eol;
1360 int eflags = 0;
1362 if (sign == ':')
1363 match_color = opt->color_match_selected;
1364 else
1365 match_color = opt->color_match_context;
1366 if (sign == ':')
1367 line_color = opt->color_selected;
1368 else if (sign == '-')
1369 line_color = opt->color_context;
1370 else if (sign == '=')
1371 line_color = opt->color_function;
1372 *eol = '\0';
1373 while (next_match(opt, bol, eol, ctx, &match, eflags)) {
1374 if (match.rm_so == match.rm_eo)
1375 break;
1377 output_color(opt, bol, match.rm_so, line_color);
1378 output_color(opt, bol + match.rm_so,
1379 match.rm_eo - match.rm_so, match_color);
1380 bol += match.rm_eo;
1381 rest -= match.rm_eo;
1382 eflags = REG_NOTBOL;
1384 *eol = ch;
1386 output_color(opt, bol, rest, line_color);
1387 opt->output(opt, "\n", 1);
1390 #ifndef NO_PTHREADS
1391 int grep_use_locks;
1394 * This lock protects access to the gitattributes machinery, which is
1395 * not thread-safe.
1397 pthread_mutex_t grep_attr_mutex;
1399 static inline void grep_attr_lock(void)
1401 if (grep_use_locks)
1402 pthread_mutex_lock(&grep_attr_mutex);
1405 static inline void grep_attr_unlock(void)
1407 if (grep_use_locks)
1408 pthread_mutex_unlock(&grep_attr_mutex);
1412 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
1414 pthread_mutex_t grep_read_mutex;
1416 #else
1417 #define grep_attr_lock()
1418 #define grep_attr_unlock()
1419 #endif
1421 static int match_funcname(struct grep_opt *opt, struct grep_source *gs, char *bol, char *eol)
1423 xdemitconf_t *xecfg = opt->priv;
1424 if (xecfg && !xecfg->find_func) {
1425 grep_source_load_driver(gs);
1426 if (gs->driver->funcname.pattern) {
1427 const struct userdiff_funcname *pe = &gs->driver->funcname;
1428 xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
1429 } else {
1430 xecfg = opt->priv = NULL;
1434 if (xecfg) {
1435 char buf[1];
1436 return xecfg->find_func(bol, eol - bol, buf, 1,
1437 xecfg->find_func_priv) >= 0;
1440 if (bol == eol)
1441 return 0;
1442 if (isalpha(*bol) || *bol == '_' || *bol == '$')
1443 return 1;
1444 return 0;
1447 static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
1448 char *bol, unsigned lno)
1450 while (bol > gs->buf) {
1451 char *eol = --bol;
1453 while (bol > gs->buf && bol[-1] != '\n')
1454 bol--;
1455 lno--;
1457 if (lno <= opt->last_shown)
1458 break;
1460 if (match_funcname(opt, gs, bol, eol)) {
1461 show_line(opt, bol, eol, gs->name, lno, '=');
1462 break;
1467 static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
1468 char *bol, char *end, unsigned lno)
1470 unsigned cur = lno, from = 1, funcname_lno = 0;
1471 int funcname_needed = !!opt->funcname;
1473 if (opt->funcbody && !match_funcname(opt, gs, bol, end))
1474 funcname_needed = 2;
1476 if (opt->pre_context < lno)
1477 from = lno - opt->pre_context;
1478 if (from <= opt->last_shown)
1479 from = opt->last_shown + 1;
1481 /* Rewind. */
1482 while (bol > gs->buf &&
1483 cur > (funcname_needed == 2 ? opt->last_shown + 1 : from)) {
1484 char *eol = --bol;
1486 while (bol > gs->buf && bol[-1] != '\n')
1487 bol--;
1488 cur--;
1489 if (funcname_needed && match_funcname(opt, gs, bol, eol)) {
1490 funcname_lno = cur;
1491 funcname_needed = 0;
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);
1499 /* Back forward. */
1500 while (cur < lno) {
1501 char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
1503 while (*eol != '\n')
1504 eol++;
1505 show_line(opt, bol, eol, gs->name, cur, sign);
1506 bol = eol + 1;
1507 cur++;
1511 static int should_lookahead(struct grep_opt *opt)
1513 struct grep_pat *p;
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 */
1523 return 1;
1526 static int look_ahead(struct grep_opt *opt,
1527 unsigned long *left_p,
1528 unsigned *lno_p,
1529 char **bol_p)
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;
1537 for (p = opt->pattern_list; p; p = p->next) {
1538 int hit;
1539 regmatch_t m;
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;
1548 if (earliest < 0) {
1549 *bol_p = bol + *left_p;
1550 *left_p = 0;
1551 return 1;
1553 for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
1554 ; /* find the beginning of the line */
1555 last_bol = sp;
1557 for (sp = bol; sp < last_bol; sp++) {
1558 if (*sp == '\n')
1559 lno++;
1561 *left_p -= last_bol - bol;
1562 *bol_p = last_bol;
1563 *lno_p = lno;
1564 return 0;
1567 static int fill_textconv_grep(struct userdiff_driver *driver,
1568 struct grep_source *gs)
1570 struct diff_filespec *df;
1571 char *buf;
1572 size_t size;
1574 if (!driver || !driver->textconv)
1575 return grep_source_load(gs);
1578 * The textconv interface is intimately tied to diff_filespecs, so we
1579 * have to pretend to be one. If we could unify the grep_source
1580 * and diff_filespec structs, this mess could just go away.
1582 df = alloc_filespec(gs->path);
1583 switch (gs->type) {
1584 case GREP_SOURCE_OID:
1585 fill_filespec(df, gs->identifier, 1, 0100644);
1586 break;
1587 case GREP_SOURCE_FILE:
1588 fill_filespec(df, &null_oid, 0, 0100644);
1589 break;
1590 default:
1591 die("BUG: attempt to textconv something without a path?");
1595 * fill_textconv is not remotely thread-safe; it may load objects
1596 * behind the scenes, and it modifies the global diff tempfile
1597 * structure.
1599 grep_read_lock();
1600 size = fill_textconv(driver, df, &buf);
1601 grep_read_unlock();
1602 free_filespec(df);
1605 * The normal fill_textconv usage by the diff machinery would just keep
1606 * the textconv'd buf separate from the diff_filespec. But much of the
1607 * grep code passes around a grep_source and assumes that its "buf"
1608 * pointer is the beginning of the thing we are searching. So let's
1609 * install our textconv'd version into the grep_source, taking care not
1610 * to leak any existing buffer.
1612 grep_source_clear_data(gs);
1613 gs->buf = buf;
1614 gs->size = size;
1616 return 0;
1619 static int is_empty_line(const char *bol, const char *eol)
1621 while (bol < eol && isspace(*bol))
1622 bol++;
1623 return bol == eol;
1626 static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits)
1628 char *bol;
1629 char *peek_bol = NULL;
1630 unsigned long left;
1631 unsigned lno = 1;
1632 unsigned last_hit = 0;
1633 int binary_match_only = 0;
1634 unsigned count = 0;
1635 int try_lookahead = 0;
1636 int show_function = 0;
1637 struct userdiff_driver *textconv = NULL;
1638 enum grep_context ctx = GREP_CONTEXT_HEAD;
1639 xdemitconf_t xecfg;
1641 if (!opt->output)
1642 opt->output = std_output;
1644 if (opt->pre_context || opt->post_context || opt->file_break ||
1645 opt->funcbody) {
1646 /* Show hunk marks, except for the first file. */
1647 if (opt->last_shown)
1648 opt->show_hunk_mark = 1;
1650 * If we're using threads then we can't easily identify
1651 * the first file. Always put hunk marks in that case
1652 * and skip the very first one later in work_done().
1654 if (opt->output != std_output)
1655 opt->show_hunk_mark = 1;
1657 opt->last_shown = 0;
1659 if (opt->allow_textconv) {
1660 grep_source_load_driver(gs);
1662 * We might set up the shared textconv cache data here, which
1663 * is not thread-safe.
1665 grep_attr_lock();
1666 textconv = userdiff_get_textconv(gs->driver);
1667 grep_attr_unlock();
1671 * We know the result of a textconv is text, so we only have to care
1672 * about binary handling if we are not using it.
1674 if (!textconv) {
1675 switch (opt->binary) {
1676 case GREP_BINARY_DEFAULT:
1677 if (grep_source_is_binary(gs))
1678 binary_match_only = 1;
1679 break;
1680 case GREP_BINARY_NOMATCH:
1681 if (grep_source_is_binary(gs))
1682 return 0; /* Assume unmatch */
1683 break;
1684 case GREP_BINARY_TEXT:
1685 break;
1686 default:
1687 die("BUG: unknown binary handling mode");
1691 memset(&xecfg, 0, sizeof(xecfg));
1692 opt->priv = &xecfg;
1694 try_lookahead = should_lookahead(opt);
1696 if (fill_textconv_grep(textconv, gs) < 0)
1697 return 0;
1699 bol = gs->buf;
1700 left = gs->size;
1701 while (left) {
1702 char *eol, ch;
1703 int hit;
1706 * look_ahead() skips quickly to the line that possibly
1707 * has the next hit; don't call it if we need to do
1708 * something more than just skipping the current line
1709 * in response to an unmatch for the current line. E.g.
1710 * inside a post-context window, we will show the current
1711 * line as a context around the previous hit when it
1712 * doesn't hit.
1714 if (try_lookahead
1715 && !(last_hit
1716 && (show_function ||
1717 lno <= last_hit + opt->post_context))
1718 && look_ahead(opt, &left, &lno, &bol))
1719 break;
1720 eol = end_of_line(bol, &left);
1721 ch = *eol;
1722 *eol = 0;
1724 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
1725 ctx = GREP_CONTEXT_BODY;
1727 hit = match_line(opt, bol, eol, ctx, collect_hits);
1728 *eol = ch;
1730 if (collect_hits)
1731 goto next_line;
1733 /* "grep -v -e foo -e bla" should list lines
1734 * that do not have either, so inversion should
1735 * be done outside.
1737 if (opt->invert)
1738 hit = !hit;
1739 if (opt->unmatch_name_only) {
1740 if (hit)
1741 return 0;
1742 goto next_line;
1744 if (hit) {
1745 count++;
1746 if (opt->status_only)
1747 return 1;
1748 if (opt->name_only) {
1749 show_name(opt, gs->name);
1750 return 1;
1752 if (opt->count)
1753 goto next_line;
1754 if (binary_match_only) {
1755 opt->output(opt, "Binary file ", 12);
1756 output_color(opt, gs->name, strlen(gs->name),
1757 opt->color_filename);
1758 opt->output(opt, " matches\n", 9);
1759 return 1;
1761 /* Hit at this line. If we haven't shown the
1762 * pre-context lines, we would need to show them.
1764 if (opt->pre_context || opt->funcbody)
1765 show_pre_context(opt, gs, bol, eol, lno);
1766 else if (opt->funcname)
1767 show_funcname_line(opt, gs, bol, lno);
1768 show_line(opt, bol, eol, gs->name, lno, ':');
1769 last_hit = lno;
1770 if (opt->funcbody)
1771 show_function = 1;
1772 goto next_line;
1774 if (show_function && (!peek_bol || peek_bol < bol)) {
1775 unsigned long peek_left = left;
1776 char *peek_eol = eol;
1779 * Trailing empty lines are not interesting.
1780 * Peek past them to see if they belong to the
1781 * body of the current function.
1783 peek_bol = bol;
1784 while (is_empty_line(peek_bol, peek_eol)) {
1785 peek_bol = peek_eol + 1;
1786 peek_eol = end_of_line(peek_bol, &peek_left);
1789 if (match_funcname(opt, gs, peek_bol, peek_eol))
1790 show_function = 0;
1792 if (show_function ||
1793 (last_hit && lno <= last_hit + opt->post_context)) {
1794 /* If the last hit is within the post context,
1795 * we need to show this line.
1797 show_line(opt, bol, eol, gs->name, lno, '-');
1800 next_line:
1801 bol = eol + 1;
1802 if (!left)
1803 break;
1804 left--;
1805 lno++;
1808 if (collect_hits)
1809 return 0;
1811 if (opt->status_only)
1812 return 0;
1813 if (opt->unmatch_name_only) {
1814 /* We did not see any hit, so we want to show this */
1815 show_name(opt, gs->name);
1816 return 1;
1819 xdiff_clear_find_func(&xecfg);
1820 opt->priv = NULL;
1822 /* NEEDSWORK:
1823 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1824 * which feels mostly useless but sometimes useful. Maybe
1825 * make it another option? For now suppress them.
1827 if (opt->count && count) {
1828 char buf[32];
1829 if (opt->pathname) {
1830 output_color(opt, gs->name, strlen(gs->name),
1831 opt->color_filename);
1832 output_sep(opt, ':');
1834 xsnprintf(buf, sizeof(buf), "%u\n", count);
1835 opt->output(opt, buf, strlen(buf));
1836 return 1;
1838 return !!last_hit;
1841 static void clr_hit_marker(struct grep_expr *x)
1843 /* All-hit markers are meaningful only at the very top level
1844 * OR node.
1846 while (1) {
1847 x->hit = 0;
1848 if (x->node != GREP_NODE_OR)
1849 return;
1850 x->u.binary.left->hit = 0;
1851 x = x->u.binary.right;
1855 static int chk_hit_marker(struct grep_expr *x)
1857 /* Top level nodes have hit markers. See if they all are hits */
1858 while (1) {
1859 if (x->node != GREP_NODE_OR)
1860 return x->hit;
1861 if (!x->u.binary.left->hit)
1862 return 0;
1863 x = x->u.binary.right;
1867 int grep_source(struct grep_opt *opt, struct grep_source *gs)
1870 * we do not have to do the two-pass grep when we do not check
1871 * buffer-wide "all-match".
1873 if (!opt->all_match)
1874 return grep_source_1(opt, gs, 0);
1876 /* Otherwise the toplevel "or" terms hit a bit differently.
1877 * We first clear hit markers from them.
1879 clr_hit_marker(opt->pattern_expression);
1880 grep_source_1(opt, gs, 1);
1882 if (!chk_hit_marker(opt->pattern_expression))
1883 return 0;
1885 return grep_source_1(opt, gs, 0);
1888 int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size)
1890 struct grep_source gs;
1891 int r;
1893 grep_source_init(&gs, GREP_SOURCE_BUF, NULL, NULL, NULL);
1894 gs.buf = buf;
1895 gs.size = size;
1897 r = grep_source(opt, &gs);
1899 grep_source_clear(&gs);
1900 return r;
1903 void grep_source_init(struct grep_source *gs, enum grep_source_type type,
1904 const char *name, const char *path,
1905 const void *identifier)
1907 gs->type = type;
1908 gs->name = xstrdup_or_null(name);
1909 gs->path = xstrdup_or_null(path);
1910 gs->buf = NULL;
1911 gs->size = 0;
1912 gs->driver = NULL;
1914 switch (type) {
1915 case GREP_SOURCE_FILE:
1916 gs->identifier = xstrdup(identifier);
1917 break;
1918 case GREP_SOURCE_SUBMODULE:
1919 if (!identifier) {
1920 gs->identifier = NULL;
1921 break;
1924 * FALL THROUGH
1925 * If the identifier is non-NULL (in the submodule case) it
1926 * will be a SHA1 that needs to be copied.
1928 case GREP_SOURCE_OID:
1929 gs->identifier = oiddup(identifier);
1930 break;
1931 case GREP_SOURCE_BUF:
1932 gs->identifier = NULL;
1933 break;
1937 void grep_source_clear(struct grep_source *gs)
1939 FREE_AND_NULL(gs->name);
1940 FREE_AND_NULL(gs->path);
1941 FREE_AND_NULL(gs->identifier);
1942 grep_source_clear_data(gs);
1945 void grep_source_clear_data(struct grep_source *gs)
1947 switch (gs->type) {
1948 case GREP_SOURCE_FILE:
1949 case GREP_SOURCE_OID:
1950 case GREP_SOURCE_SUBMODULE:
1951 FREE_AND_NULL(gs->buf);
1952 gs->size = 0;
1953 break;
1954 case GREP_SOURCE_BUF:
1955 /* leave user-provided buf intact */
1956 break;
1960 static int grep_source_load_oid(struct grep_source *gs)
1962 enum object_type type;
1964 grep_read_lock();
1965 gs->buf = read_sha1_file(gs->identifier, &type, &gs->size);
1966 grep_read_unlock();
1968 if (!gs->buf)
1969 return error(_("'%s': unable to read %s"),
1970 gs->name,
1971 oid_to_hex(gs->identifier));
1972 return 0;
1975 static int grep_source_load_file(struct grep_source *gs)
1977 const char *filename = gs->identifier;
1978 struct stat st;
1979 char *data;
1980 size_t size;
1981 int i;
1983 if (lstat(filename, &st) < 0) {
1984 err_ret:
1985 if (errno != ENOENT)
1986 error_errno(_("failed to stat '%s'"), filename);
1987 return -1;
1989 if (!S_ISREG(st.st_mode))
1990 return -1;
1991 size = xsize_t(st.st_size);
1992 i = open(filename, O_RDONLY);
1993 if (i < 0)
1994 goto err_ret;
1995 data = xmallocz(size);
1996 if (st.st_size != read_in_full(i, data, size)) {
1997 error_errno(_("'%s': short read"), filename);
1998 close(i);
1999 free(data);
2000 return -1;
2002 close(i);
2004 gs->buf = data;
2005 gs->size = size;
2006 return 0;
2009 static int grep_source_load(struct grep_source *gs)
2011 if (gs->buf)
2012 return 0;
2014 switch (gs->type) {
2015 case GREP_SOURCE_FILE:
2016 return grep_source_load_file(gs);
2017 case GREP_SOURCE_OID:
2018 return grep_source_load_oid(gs);
2019 case GREP_SOURCE_BUF:
2020 return gs->buf ? 0 : -1;
2021 case GREP_SOURCE_SUBMODULE:
2022 break;
2024 die("BUG: invalid grep_source type to load");
2027 void grep_source_load_driver(struct grep_source *gs)
2029 if (gs->driver)
2030 return;
2032 grep_attr_lock();
2033 if (gs->path)
2034 gs->driver = userdiff_find_by_path(gs->path);
2035 if (!gs->driver)
2036 gs->driver = userdiff_find_by_name("default");
2037 grep_attr_unlock();
2040 static int grep_source_is_binary(struct grep_source *gs)
2042 grep_source_load_driver(gs);
2043 if (gs->driver->binary != -1)
2044 return gs->driver->binary;
2046 if (!grep_source_load(gs))
2047 return buffer_is_binary(gs->buf, gs->size);
2049 return 0;