blob: 4bd1b8b1dd67e4f5c2223536b435198ce67463ba [file] [log] [blame]
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001#include "cache.h"
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07002#include "grep.h"
René Scharfe60ecac92009-07-02 00:07:24 +02003#include "userdiff.h"
Johannes Schindelin6bfce932007-06-05 03:36:11 +01004#include "xdiff-interface.h"
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07005
Junio C Hamano07a7d652012-09-15 14:04:36 -07006static int grep_source_load(struct grep_source *gs);
7static int grep_source_is_binary(struct grep_source *gs);
8
Junio C Hamano7687a052012-10-09 16:17:50 -07009static struct grep_opt grep_defaults;
10
11/*
12 * Initialize the grep_defaults template with hardcoded defaults.
13 * We could let the compiler do this, but without C99 initializers
14 * the code gets unwieldy and unreadable, so...
15 */
16void init_grep_defaults(void)
17{
18 struct grep_opt *opt = &grep_defaults;
Junio C Hamano918d4e12012-10-09 16:40:03 -070019 static int run_once;
20
21 if (run_once)
22 return;
23 run_once++;
Junio C Hamano7687a052012-10-09 16:17:50 -070024
25 memset(opt, 0, sizeof(*opt));
26 opt->relative = 1;
27 opt->pathname = 1;
28 opt->regflags = REG_NEWLINE;
29 opt->max_depth = -1;
30 opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
31 opt->extended_regexp_option = 0;
32 strcpy(opt->color_context, "");
33 strcpy(opt->color_filename, "");
34 strcpy(opt->color_function, "");
35 strcpy(opt->color_lineno, "");
36 strcpy(opt->color_match, GIT_COLOR_BOLD_RED);
37 strcpy(opt->color_selected, "");
38 strcpy(opt->color_sep, GIT_COLOR_CYAN);
39 opt->color = -1;
40}
41
42static int parse_pattern_type_arg(const char *opt, const char *arg)
43{
44 if (!strcmp(arg, "default"))
45 return GREP_PATTERN_TYPE_UNSPECIFIED;
46 else if (!strcmp(arg, "basic"))
47 return GREP_PATTERN_TYPE_BRE;
48 else if (!strcmp(arg, "extended"))
49 return GREP_PATTERN_TYPE_ERE;
50 else if (!strcmp(arg, "fixed"))
51 return GREP_PATTERN_TYPE_FIXED;
52 else if (!strcmp(arg, "perl"))
53 return GREP_PATTERN_TYPE_PCRE;
54 die("bad %s argument: %s", opt, arg);
55}
56
57/*
58 * Read the configuration file once and store it in
59 * the grep_defaults template.
60 */
61int grep_config(const char *var, const char *value, void *cb)
62{
63 struct grep_opt *opt = &grep_defaults;
64 char *color = NULL;
65
66 if (userdiff_config(var, value) < 0)
67 return -1;
68
69 if (!strcmp(var, "grep.extendedregexp")) {
70 if (git_config_bool(var, value))
71 opt->extended_regexp_option = 1;
72 else
73 opt->extended_regexp_option = 0;
74 return 0;
75 }
76
77 if (!strcmp(var, "grep.patterntype")) {
78 opt->pattern_type_option = parse_pattern_type_arg(var, value);
79 return 0;
80 }
81
82 if (!strcmp(var, "grep.linenumber")) {
83 opt->linenum = git_config_bool(var, value);
84 return 0;
85 }
86
87 if (!strcmp(var, "color.grep"))
88 opt->color = git_config_colorbool(var, value);
89 else if (!strcmp(var, "color.grep.context"))
90 color = opt->color_context;
91 else if (!strcmp(var, "color.grep.filename"))
92 color = opt->color_filename;
93 else if (!strcmp(var, "color.grep.function"))
94 color = opt->color_function;
95 else if (!strcmp(var, "color.grep.linenumber"))
96 color = opt->color_lineno;
97 else if (!strcmp(var, "color.grep.match"))
98 color = opt->color_match;
99 else if (!strcmp(var, "color.grep.selected"))
100 color = opt->color_selected;
101 else if (!strcmp(var, "color.grep.separator"))
102 color = opt->color_sep;
103
104 if (color) {
105 if (!value)
106 return config_error_nonbool(var);
107 color_parse(value, var, color);
108 }
109 return 0;
110}
111
112/*
113 * Initialize one instance of grep_opt and copy the
114 * default values from the template we read the configuration
115 * information in an earlier call to git_config(grep_config).
116 */
117void grep_init(struct grep_opt *opt, const char *prefix)
118{
119 struct grep_opt *def = &grep_defaults;
120
121 memset(opt, 0, sizeof(*opt));
122 opt->prefix = prefix;
123 opt->prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
124 opt->pattern_tail = &opt->pattern_list;
125 opt->header_tail = &opt->header_list;
126
127 opt->color = def->color;
128 opt->extended_regexp_option = def->extended_regexp_option;
129 opt->pattern_type_option = def->pattern_type_option;
130 opt->linenum = def->linenum;
131 opt->max_depth = def->max_depth;
132 opt->pathname = def->pathname;
133 opt->regflags = def->regflags;
134 opt->relative = def->relative;
135
136 strcpy(opt->color_context, def->color_context);
137 strcpy(opt->color_filename, def->color_filename);
138 strcpy(opt->color_function, def->color_function);
139 strcpy(opt->color_lineno, def->color_lineno);
140 strcpy(opt->color_match, def->color_match);
141 strcpy(opt->color_selected, def->color_selected);
142 strcpy(opt->color_sep, def->color_sep);
143}
Junio C Hamano07a7d652012-09-15 14:04:36 -0700144
Junio C Hamanoc5c31d32012-10-03 14:47:48 -0700145void grep_commit_pattern_type(enum grep_pattern_type pattern_type, struct grep_opt *opt)
146{
147 if (pattern_type != GREP_PATTERN_TYPE_UNSPECIFIED)
148 grep_set_pattern_type_option(pattern_type, opt);
149 else if (opt->pattern_type_option != GREP_PATTERN_TYPE_UNSPECIFIED)
150 grep_set_pattern_type_option(opt->pattern_type_option, opt);
151 else if (opt->extended_regexp_option)
152 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, opt);
153}
154
155void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, struct grep_opt *opt)
156{
157 switch (pattern_type) {
158 case GREP_PATTERN_TYPE_UNSPECIFIED:
159 /* fall through */
160
161 case GREP_PATTERN_TYPE_BRE:
162 opt->fixed = 0;
163 opt->pcre = 0;
164 opt->regflags &= ~REG_EXTENDED;
165 break;
166
167 case GREP_PATTERN_TYPE_ERE:
168 opt->fixed = 0;
169 opt->pcre = 0;
170 opt->regflags |= REG_EXTENDED;
171 break;
172
173 case GREP_PATTERN_TYPE_FIXED:
174 opt->fixed = 1;
175 opt->pcre = 0;
176 opt->regflags &= ~REG_EXTENDED;
177 break;
178
179 case GREP_PATTERN_TYPE_PCRE:
180 opt->fixed = 0;
181 opt->pcre = 1;
182 opt->regflags &= ~REG_EXTENDED;
183 break;
184 }
185}
186
René Scharfefc456752012-05-20 16:32:39 +0200187static struct grep_pat *create_grep_pat(const char *pat, size_t patlen,
188 const char *origin, int no,
189 enum grep_pat_token t,
190 enum grep_header_field field)
Junio C Hamanoa4d7d2c2008-09-04 22:15:02 -0700191{
192 struct grep_pat *p = xcalloc(1, sizeof(*p));
René Scharfe526a8582012-05-20 16:33:07 +0200193 p->pattern = xmemdupz(pat, patlen);
René Scharfefc456752012-05-20 16:32:39 +0200194 p->patternlen = patlen;
195 p->origin = origin;
196 p->no = no;
197 p->token = t;
Junio C Hamanoa4d7d2c2008-09-04 22:15:02 -0700198 p->field = field;
René Scharfefc456752012-05-20 16:32:39 +0200199 return p;
200}
201
René Scharfe2b3873f2012-05-20 16:32:54 +0200202static void do_append_grep_pat(struct grep_pat ***tail, struct grep_pat *p)
203{
204 **tail = p;
205 *tail = &p->next;
Junio C Hamanoa4d7d2c2008-09-04 22:15:02 -0700206 p->next = NULL;
René Scharfe526a8582012-05-20 16:33:07 +0200207
208 switch (p->token) {
209 case GREP_PATTERN: /* atom */
210 case GREP_PATTERN_HEAD:
211 case GREP_PATTERN_BODY:
212 for (;;) {
213 struct grep_pat *new_pat;
214 size_t len = 0;
215 char *cp = p->pattern + p->patternlen, *nl = NULL;
216 while (++len <= p->patternlen) {
217 if (*(--cp) == '\n') {
218 nl = cp;
219 break;
220 }
221 }
222 if (!nl)
223 break;
224 new_pat = create_grep_pat(nl + 1, len - 1, p->origin,
225 p->no, p->token, p->field);
226 new_pat->next = p->next;
227 if (!p->next)
228 *tail = &new_pat->next;
229 p->next = new_pat;
230 *nl = '\0';
231 p->patternlen -= len;
232 }
233 break;
234 default:
235 break;
236 }
René Scharfe2b3873f2012-05-20 16:32:54 +0200237}
238
René Scharfefc456752012-05-20 16:32:39 +0200239void append_header_grep_pattern(struct grep_opt *opt,
240 enum grep_header_field field, const char *pat)
241{
242 struct grep_pat *p = create_grep_pat(pat, strlen(pat), "header", 0,
243 GREP_PATTERN_HEAD, field);
Junio C Hamanobaa63782012-09-29 11:59:52 -0700244 if (field == GREP_HEADER_REFLOG)
245 opt->use_reflog_filter = 1;
René Scharfe2b3873f2012-05-20 16:32:54 +0200246 do_append_grep_pat(&opt->header_tail, p);
Junio C Hamanoa4d7d2c2008-09-04 22:15:02 -0700247}
248
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700249void append_grep_pattern(struct grep_opt *opt, const char *pat,
250 const char *origin, int no, enum grep_pat_token t)
251{
René Scharfeed40a092010-05-22 23:43:43 +0200252 append_grep_pat(opt, pat, strlen(pat), origin, no, t);
253}
254
255void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen,
256 const char *origin, int no, enum grep_pat_token t)
257{
René Scharfefc456752012-05-20 16:32:39 +0200258 struct grep_pat *p = create_grep_pat(pat, patlen, origin, no, t, 0);
René Scharfe2b3873f2012-05-20 16:32:54 +0200259 do_append_grep_pat(&opt->pattern_tail, p);
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700260}
261
Fredrik Kuivinen5b594f42010-01-25 23:51:39 +0100262struct grep_opt *grep_opt_dup(const struct grep_opt *opt)
263{
264 struct grep_pat *pat;
265 struct grep_opt *ret = xmalloc(sizeof(struct grep_opt));
266 *ret = *opt;
267
268 ret->pattern_list = NULL;
269 ret->pattern_tail = &ret->pattern_list;
270
271 for(pat = opt->pattern_list; pat != NULL; pat = pat->next)
272 {
273 if(pat->token == GREP_PATTERN_HEAD)
274 append_header_grep_pattern(ret, pat->field,
275 pat->pattern);
276 else
René Scharfeed40a092010-05-22 23:43:43 +0200277 append_grep_pat(ret, pat->pattern, pat->patternlen,
278 pat->origin, pat->no, pat->token);
Fredrik Kuivinen5b594f42010-01-25 23:51:39 +0100279 }
280
281 return ret;
282}
283
Michał Kiedrowicza30c1482011-05-09 23:52:04 +0200284static NORETURN void compile_regexp_failed(const struct grep_pat *p,
285 const char *error)
286{
287 char where[1024];
288
289 if (p->no)
290 sprintf(where, "In '%s' at %d, ", p->origin, p->no);
291 else if (p->origin)
292 sprintf(where, "%s, ", p->origin);
293 else
294 where[0] = 0;
295
296 die("%s'%s': %s", where, p->pattern, error);
297}
298
Michał Kiedrowicz63e7e9d2011-05-09 23:52:05 +0200299#ifdef USE_LIBPCRE
300static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
301{
302 const char *error;
303 int erroffset;
Michał Kiedrowiczfba4f122012-02-25 10:24:28 +0100304 int options = PCRE_MULTILINE;
Michał Kiedrowicz63e7e9d2011-05-09 23:52:05 +0200305
306 if (opt->ignore_case)
307 options |= PCRE_CASELESS;
308
309 p->pcre_regexp = pcre_compile(p->pattern, options, &error, &erroffset,
310 NULL);
311 if (!p->pcre_regexp)
312 compile_regexp_failed(p, error);
313
314 p->pcre_extra_info = pcre_study(p->pcre_regexp, 0, &error);
315 if (!p->pcre_extra_info && error)
316 die("%s", error);
317}
318
319static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
320 regmatch_t *match, int eflags)
321{
322 int ovector[30], ret, flags = 0;
323
324 if (eflags & REG_NOTBOL)
325 flags |= PCRE_NOTBOL;
326
327 ret = pcre_exec(p->pcre_regexp, p->pcre_extra_info, line, eol - line,
328 0, flags, ovector, ARRAY_SIZE(ovector));
329 if (ret < 0 && ret != PCRE_ERROR_NOMATCH)
330 die("pcre_exec failed with error code %d", ret);
331 if (ret > 0) {
332 ret = 0;
333 match->rm_so = ovector[0];
334 match->rm_eo = ovector[1];
335 }
336
337 return ret;
338}
339
340static void free_pcre_regexp(struct grep_pat *p)
341{
342 pcre_free(p->pcre_regexp);
343 pcre_free(p->pcre_extra_info);
344}
345#else /* !USE_LIBPCRE */
346static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
347{
348 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
349}
350
351static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
352 regmatch_t *match, int eflags)
353{
354 return 1;
355}
356
357static void free_pcre_regexp(struct grep_pat *p)
358{
359}
360#endif /* !USE_LIBPCRE */
361
Fredrik Kuivinen9ecedde2011-08-21 00:42:18 +0200362static int is_fixed(const char *s, size_t len)
363{
364 size_t i;
365
366 /* regcomp cannot accept patterns with NULs so we
367 * consider any pattern containing a NUL fixed.
368 */
369 if (memchr(s, 0, len))
370 return 1;
371
372 for (i = 0; i < len; i++) {
373 if (is_regex_special(s[i]))
374 return 0;
375 }
376
377 return 1;
378}
379
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700380static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
381{
René Scharfec8222552009-01-10 00:18:34 +0100382 int err;
383
René Scharfed7eb5272009-03-07 13:28:40 +0100384 p->word_regexp = opt->word_regexp;
Brian Collins5183bf62009-11-06 01:22:35 -0800385 p->ignore_case = opt->ignore_case;
René Scharfed7eb5272009-03-07 13:28:40 +0100386
Fredrik Kuivinen9ecedde2011-08-21 00:42:18 +0200387 if (opt->fixed || is_fixed(p->pattern, p->patternlen))
388 p->fixed = 1;
389 else
390 p->fixed = 0;
391
392 if (p->fixed) {
Junio C Hamano0f871cf2012-02-28 14:20:53 -0800393 if (opt->regflags & REG_ICASE || p->ignore_case)
394 p->kws = kwsalloc(tolower_trans_tbl);
395 else
Fredrik Kuivinen9ecedde2011-08-21 00:42:18 +0200396 p->kws = kwsalloc(NULL);
Fredrik Kuivinen9ecedde2011-08-21 00:42:18 +0200397 kwsincr(p->kws, p->pattern, p->patternlen);
398 kwsprep(p->kws);
René Scharfec8222552009-01-10 00:18:34 +0100399 return;
Fredrik Kuivinen9ecedde2011-08-21 00:42:18 +0200400 }
René Scharfec8222552009-01-10 00:18:34 +0100401
Michał Kiedrowicz63e7e9d2011-05-09 23:52:05 +0200402 if (opt->pcre) {
403 compile_pcre_regexp(p, opt);
404 return;
405 }
406
René Scharfec8222552009-01-10 00:18:34 +0100407 err = regcomp(&p->regexp, p->pattern, opt->regflags);
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700408 if (err) {
409 char errbuf[1024];
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700410 regerror(err, &p->regexp, errbuf, 1024);
411 regfree(&p->regexp);
Michał Kiedrowicza30c1482011-05-09 23:52:04 +0200412 compile_regexp_failed(p, errbuf);
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700413 }
414}
415
Junio C Hamano0ab7bef2006-09-27 17:50:52 -0700416static struct grep_expr *compile_pattern_or(struct grep_pat **);
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700417static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
418{
419 struct grep_pat *p;
420 struct grep_expr *x;
421
422 p = *list;
Linus Torvaldsc922b012009-04-27 11:10:24 -0700423 if (!p)
424 return NULL;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700425 switch (p->token) {
426 case GREP_PATTERN: /* atom */
Junio C Hamano480c1ca2006-09-20 12:39:46 -0700427 case GREP_PATTERN_HEAD:
428 case GREP_PATTERN_BODY:
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700429 x = xcalloc(1, sizeof (struct grep_expr));
430 x->node = GREP_NODE_ATOM;
431 x->u.atom = p;
432 *list = p->next;
433 return x;
434 case GREP_OPEN_PAREN:
435 *list = p->next;
Junio C Hamano0ab7bef2006-09-27 17:50:52 -0700436 x = compile_pattern_or(list);
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700437 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
438 die("unmatched parenthesis");
439 *list = (*list)->next;
440 return x;
441 default:
442 return NULL;
443 }
444}
445
446static struct grep_expr *compile_pattern_not(struct grep_pat **list)
447{
448 struct grep_pat *p;
449 struct grep_expr *x;
450
451 p = *list;
Linus Torvaldsc922b012009-04-27 11:10:24 -0700452 if (!p)
453 return NULL;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700454 switch (p->token) {
455 case GREP_NOT:
456 if (!p->next)
457 die("--not not followed by pattern expression");
458 *list = p->next;
459 x = xcalloc(1, sizeof (struct grep_expr));
460 x->node = GREP_NODE_NOT;
461 x->u.unary = compile_pattern_not(list);
462 if (!x->u.unary)
463 die("--not followed by non pattern expression");
464 return x;
465 default:
466 return compile_pattern_atom(list);
467 }
468}
469
470static struct grep_expr *compile_pattern_and(struct grep_pat **list)
471{
472 struct grep_pat *p;
473 struct grep_expr *x, *y, *z;
474
475 x = compile_pattern_not(list);
476 p = *list;
477 if (p && p->token == GREP_AND) {
478 if (!p->next)
479 die("--and not followed by pattern expression");
480 *list = p->next;
481 y = compile_pattern_and(list);
482 if (!y)
483 die("--and not followed by pattern expression");
484 z = xcalloc(1, sizeof (struct grep_expr));
485 z->node = GREP_NODE_AND;
486 z->u.binary.left = x;
487 z->u.binary.right = y;
488 return z;
489 }
490 return x;
491}
492
493static struct grep_expr *compile_pattern_or(struct grep_pat **list)
494{
495 struct grep_pat *p;
496 struct grep_expr *x, *y, *z;
497
498 x = compile_pattern_and(list);
499 p = *list;
500 if (x && p && p->token != GREP_CLOSE_PAREN) {
501 y = compile_pattern_or(list);
502 if (!y)
503 die("not a pattern expression %s", p->pattern);
504 z = xcalloc(1, sizeof (struct grep_expr));
505 z->node = GREP_NODE_OR;
506 z->u.binary.left = x;
507 z->u.binary.right = y;
508 return z;
509 }
510 return x;
511}
512
513static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
514{
515 return compile_pattern_or(list);
516}
517
Junio C Hamano17bf35a2012-09-13 14:21:44 -0700518static void indent(int in)
519{
520 while (in-- > 0)
521 fputc(' ', stderr);
522}
523
524static void dump_grep_pat(struct grep_pat *p)
525{
526 switch (p->token) {
527 case GREP_AND: fprintf(stderr, "*and*"); break;
528 case GREP_OPEN_PAREN: fprintf(stderr, "*(*"); break;
529 case GREP_CLOSE_PAREN: fprintf(stderr, "*)*"); break;
530 case GREP_NOT: fprintf(stderr, "*not*"); break;
531 case GREP_OR: fprintf(stderr, "*or*"); break;
532
533 case GREP_PATTERN: fprintf(stderr, "pattern"); break;
534 case GREP_PATTERN_HEAD: fprintf(stderr, "pattern_head"); break;
535 case GREP_PATTERN_BODY: fprintf(stderr, "pattern_body"); break;
536 }
537
538 switch (p->token) {
539 default: break;
540 case GREP_PATTERN_HEAD:
541 fprintf(stderr, "<head %d>", p->field); break;
542 case GREP_PATTERN_BODY:
543 fprintf(stderr, "<body>"); break;
544 }
545 switch (p->token) {
546 default: break;
547 case GREP_PATTERN_HEAD:
548 case GREP_PATTERN_BODY:
549 case GREP_PATTERN:
550 fprintf(stderr, "%.*s", (int)p->patternlen, p->pattern);
551 break;
552 }
553 fputc('\n', stderr);
554}
555
556static void dump_grep_expression_1(struct grep_expr *x, int in)
557{
558 indent(in);
559 switch (x->node) {
560 case GREP_NODE_TRUE:
561 fprintf(stderr, "true\n");
562 break;
563 case GREP_NODE_ATOM:
564 dump_grep_pat(x->u.atom);
565 break;
566 case GREP_NODE_NOT:
567 fprintf(stderr, "(not\n");
568 dump_grep_expression_1(x->u.unary, in+1);
569 indent(in);
570 fprintf(stderr, ")\n");
571 break;
572 case GREP_NODE_AND:
573 fprintf(stderr, "(and\n");
574 dump_grep_expression_1(x->u.binary.left, in+1);
575 dump_grep_expression_1(x->u.binary.right, in+1);
576 indent(in);
577 fprintf(stderr, ")\n");
578 break;
579 case GREP_NODE_OR:
580 fprintf(stderr, "(or\n");
581 dump_grep_expression_1(x->u.binary.left, in+1);
582 dump_grep_expression_1(x->u.binary.right, in+1);
583 indent(in);
584 fprintf(stderr, ")\n");
585 break;
586 }
587}
588
Junio C Hamano07a7d652012-09-15 14:04:36 -0700589static void dump_grep_expression(struct grep_opt *opt)
Junio C Hamano17bf35a2012-09-13 14:21:44 -0700590{
591 struct grep_expr *x = opt->pattern_expression;
592
593 if (opt->all_match)
594 fprintf(stderr, "[all-match]\n");
595 dump_grep_expression_1(x, 0);
596 fflush(NULL);
597}
598
Junio C Hamano5aaeb732010-09-12 22:15:35 -0700599static struct grep_expr *grep_true_expr(void)
600{
601 struct grep_expr *z = xcalloc(1, sizeof(*z));
602 z->node = GREP_NODE_TRUE;
603 return z;
604}
605
606static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
607{
608 struct grep_expr *z = xcalloc(1, sizeof(*z));
609 z->node = GREP_NODE_OR;
610 z->u.binary.left = left;
611 z->u.binary.right = right;
612 return z;
613}
614
Junio C Hamano95ce9ce2010-09-12 19:30:48 -0700615static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
616{
617 struct grep_pat *p;
618 struct grep_expr *header_expr;
Junio C Hamano5aaeb732010-09-12 22:15:35 -0700619 struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]);
620 enum grep_header_field fld;
Junio C Hamano95ce9ce2010-09-12 19:30:48 -0700621
622 if (!opt->header_list)
623 return NULL;
Angus Hammond2385f242012-05-06 19:17:15 +0100624
Junio C Hamano95ce9ce2010-09-12 19:30:48 -0700625 for (p = opt->header_list; p; p = p->next) {
626 if (p->token != GREP_PATTERN_HEAD)
627 die("bug: a non-header pattern in grep header list.");
628 if (p->field < 0 || GREP_HEADER_FIELD_MAX <= p->field)
629 die("bug: unknown header field %d", p->field);
630 compile_regexp(p, opt);
631 }
Junio C Hamano5aaeb732010-09-12 22:15:35 -0700632
633 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++)
634 header_group[fld] = NULL;
635
636 for (p = opt->header_list; p; p = p->next) {
637 struct grep_expr *h;
638 struct grep_pat *pp = p;
639
640 h = compile_pattern_atom(&pp);
641 if (!h || pp != p->next)
642 die("bug: malformed header expr");
643 if (!header_group[p->field]) {
644 header_group[p->field] = h;
645 continue;
646 }
647 header_group[p->field] = grep_or_expr(h, header_group[p->field]);
648 }
649
650 header_expr = NULL;
651
652 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) {
653 if (!header_group[fld])
654 continue;
655 if (!header_expr)
656 header_expr = grep_true_expr();
657 header_expr = grep_or_expr(header_group[fld], header_expr);
658 }
Junio C Hamano95ce9ce2010-09-12 19:30:48 -0700659 return header_expr;
660}
661
Junio C Hamano13e4fc72012-09-13 16:26:57 -0700662static struct grep_expr *grep_splice_or(struct grep_expr *x, struct grep_expr *y)
663{
664 struct grep_expr *z = x;
665
666 while (x) {
667 assert(x->node == GREP_NODE_OR);
668 if (x->u.binary.right &&
669 x->u.binary.right->node == GREP_NODE_TRUE) {
670 x->u.binary.right = y;
671 break;
672 }
673 x = x->u.binary.right;
674 }
675 return z;
676}
677
Junio C Hamano17bf35a2012-09-13 14:21:44 -0700678static void compile_grep_patterns_real(struct grep_opt *opt)
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700679{
680 struct grep_pat *p;
Junio C Hamano95ce9ce2010-09-12 19:30:48 -0700681 struct grep_expr *header_expr = prep_header_patterns(opt);
Junio C Hamano0ab7bef2006-09-27 17:50:52 -0700682
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700683 for (p = opt->pattern_list; p; p = p->next) {
Junio C Hamano480c1ca2006-09-20 12:39:46 -0700684 switch (p->token) {
685 case GREP_PATTERN: /* atom */
686 case GREP_PATTERN_HEAD:
687 case GREP_PATTERN_BODY:
René Scharfec8222552009-01-10 00:18:34 +0100688 compile_regexp(p, opt);
Junio C Hamano480c1ca2006-09-20 12:39:46 -0700689 break;
690 default:
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700691 opt->extended = 1;
Junio C Hamano480c1ca2006-09-20 12:39:46 -0700692 break;
693 }
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700694 }
695
Junio C Hamano80235ba2010-01-17 20:09:06 -0800696 if (opt->all_match || header_expr)
697 opt->extended = 1;
Junio C Hamano17bf35a2012-09-13 14:21:44 -0700698 else if (!opt->extended && !opt->debug)
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700699 return;
700
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700701 p = opt->pattern_list;
Michele Ballabioba150a32009-03-18 21:53:27 +0100702 if (p)
703 opt->pattern_expression = compile_pattern_expr(&p);
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700704 if (p)
705 die("incomplete pattern expression: %s", p->pattern);
Junio C Hamano80235ba2010-01-17 20:09:06 -0800706
707 if (!header_expr)
708 return;
709
Junio C Hamano5aaeb732010-09-12 22:15:35 -0700710 if (!opt->pattern_expression)
Junio C Hamano80235ba2010-01-17 20:09:06 -0800711 opt->pattern_expression = header_expr;
Junio C Hamano13e4fc72012-09-13 16:26:57 -0700712 else if (opt->all_match)
713 opt->pattern_expression = grep_splice_or(header_expr,
714 opt->pattern_expression);
Junio C Hamano5aaeb732010-09-12 22:15:35 -0700715 else
716 opt->pattern_expression = grep_or_expr(opt->pattern_expression,
717 header_expr);
Junio C Hamano80235ba2010-01-17 20:09:06 -0800718 opt->all_match = 1;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700719}
720
Junio C Hamano17bf35a2012-09-13 14:21:44 -0700721void compile_grep_patterns(struct grep_opt *opt)
722{
723 compile_grep_patterns_real(opt);
724 if (opt->debug)
725 dump_grep_expression(opt);
726}
727
Junio C Hamanob48fb5b2006-09-27 16:27:10 -0700728static void free_pattern_expr(struct grep_expr *x)
729{
730 switch (x->node) {
Junio C Hamano5aaeb732010-09-12 22:15:35 -0700731 case GREP_NODE_TRUE:
Junio C Hamanob48fb5b2006-09-27 16:27:10 -0700732 case GREP_NODE_ATOM:
733 break;
734 case GREP_NODE_NOT:
735 free_pattern_expr(x->u.unary);
736 break;
737 case GREP_NODE_AND:
738 case GREP_NODE_OR:
739 free_pattern_expr(x->u.binary.left);
740 free_pattern_expr(x->u.binary.right);
741 break;
742 }
743 free(x);
744}
745
746void free_grep_patterns(struct grep_opt *opt)
747{
748 struct grep_pat *p, *n;
749
750 for (p = opt->pattern_list; p; p = n) {
751 n = p->next;
752 switch (p->token) {
753 case GREP_PATTERN: /* atom */
754 case GREP_PATTERN_HEAD:
755 case GREP_PATTERN_BODY:
Fredrik Kuivinen9ecedde2011-08-21 00:42:18 +0200756 if (p->kws)
757 kwsfree(p->kws);
758 else if (p->pcre_regexp)
Michał Kiedrowicz63e7e9d2011-05-09 23:52:05 +0200759 free_pcre_regexp(p);
760 else
761 regfree(&p->regexp);
René Scharfe526a8582012-05-20 16:33:07 +0200762 free(p->pattern);
Junio C Hamanob48fb5b2006-09-27 16:27:10 -0700763 break;
764 default:
765 break;
766 }
767 free(p);
768 }
769
770 if (!opt->extended)
771 return;
772 free_pattern_expr(opt->pattern_expression);
773}
774
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700775static char *end_of_line(char *cp, unsigned long *left)
776{
777 unsigned long l = *left;
778 while (l && *cp != '\n') {
779 l--;
780 cp++;
781 }
782 *left = l;
783 return cp;
784}
785
786static int word_char(char ch)
787{
788 return isalnum(ch) || ch == '_';
789}
790
Mark Lodato55f638b2010-03-07 11:52:46 -0500791static void output_color(struct grep_opt *opt, const void *data, size_t size,
792 const char *color)
793{
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700794 if (want_color(opt->color) && color && color[0]) {
Mark Lodato55f638b2010-03-07 11:52:46 -0500795 opt->output(opt, color, strlen(color));
796 opt->output(opt, data, size);
797 opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
798 } else
799 opt->output(opt, data, size);
800}
801
802static void output_sep(struct grep_opt *opt, char sign)
803{
804 if (opt->null_following_name)
805 opt->output(opt, "\0", 1);
806 else
807 output_color(opt, &sign, 1, opt->color_sep);
808}
809
Raphael Zimmerer83caecc2008-10-01 18:11:15 +0200810static void show_name(struct grep_opt *opt, const char *name)
811{
Mark Lodato55f638b2010-03-07 11:52:46 -0500812 output_color(opt, name, strlen(name), opt->color_filename);
Fredrik Kuivinen5b594f42010-01-25 23:51:39 +0100813 opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
Raphael Zimmerer83caecc2008-10-01 18:11:15 +0200814}
815
René Scharfeed40a092010-05-22 23:43:43 +0200816static int fixmatch(struct grep_pat *p, char *line, char *eol,
817 regmatch_t *match)
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700818{
Fredrik Kuivinen9ecedde2011-08-21 00:42:18 +0200819 struct kwsmatch kwsm;
820 size_t offset = kwsexec(p->kws, line, eol - line, &kwsm);
821 if (offset == -1) {
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700822 match->rm_so = match->rm_eo = -1;
823 return REG_NOMATCH;
Fredrik Kuivinen9ecedde2011-08-21 00:42:18 +0200824 } else {
825 match->rm_so = offset;
826 match->rm_eo = match->rm_so + kwsm.size[0];
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700827 return 0;
828 }
829}
830
René Scharfef96e5672010-05-22 23:35:07 +0200831static int regmatch(const regex_t *preg, char *line, char *eol,
832 regmatch_t *match, int eflags)
833{
834#ifdef REG_STARTEND
835 match->rm_so = 0;
836 match->rm_eo = eol - line;
837 eflags |= REG_STARTEND;
838#endif
839 return regexec(preg, line, 1, match, eflags);
840}
841
Michał Kiedrowicz97e77782011-05-05 00:00:19 +0200842static int patmatch(struct grep_pat *p, char *line, char *eol,
843 regmatch_t *match, int eflags)
844{
845 int hit;
846
847 if (p->fixed)
848 hit = !fixmatch(p, line, eol, match);
Michał Kiedrowicz63e7e9d2011-05-09 23:52:05 +0200849 else if (p->pcre_regexp)
850 hit = !pcrematch(p, line, eol, match, eflags);
Michał Kiedrowicz97e77782011-05-05 00:00:19 +0200851 else
852 hit = !regmatch(&p->regexp, line, eol, match, eflags);
853
854 return hit;
855}
856
Junio C Hamanoa4d7d2c2008-09-04 22:15:02 -0700857static int strip_timestamp(char *bol, char **eol_p)
858{
859 char *eol = *eol_p;
860 int ch;
861
862 while (bol < --eol) {
863 if (*eol != '>')
864 continue;
865 *eol_p = ++eol;
866 ch = *eol;
867 *eol = '\0';
868 return ch;
869 }
870 return 0;
871}
872
873static struct {
874 const char *field;
875 size_t len;
876} header_field[] = {
877 { "author ", 7 },
878 { "committer ", 10 },
Nguyễn Thái Ngọc Duy72fd13f2012-09-29 11:41:28 +0700879 { "reflog ", 7 },
Junio C Hamanoa4d7d2c2008-09-04 22:15:02 -0700880};
881
René Scharfed7eb5272009-03-07 13:28:40 +0100882static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
René Scharfe79212772009-03-07 13:30:27 +0100883 enum grep_context ctx,
884 regmatch_t *pmatch, int eflags)
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700885{
886 int hit = 0;
Junio C Hamanoa4d7d2c2008-09-04 22:15:02 -0700887 int saved_ch = 0;
René Scharfee701fad2009-05-20 23:31:53 +0200888 const char *start = bol;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700889
Junio C Hamano480c1ca2006-09-20 12:39:46 -0700890 if ((p->token != GREP_PATTERN) &&
891 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
892 return 0;
893
Junio C Hamanoa4d7d2c2008-09-04 22:15:02 -0700894 if (p->token == GREP_PATTERN_HEAD) {
895 const char *field;
896 size_t len;
897 assert(p->field < ARRAY_SIZE(header_field));
898 field = header_field[p->field].field;
899 len = header_field[p->field].len;
900 if (strncmp(bol, field, len))
901 return 0;
902 bol += len;
Nguyễn Thái Ngọc Duyad4813b2012-09-29 11:41:27 +0700903 switch (p->field) {
904 case GREP_HEADER_AUTHOR:
905 case GREP_HEADER_COMMITTER:
906 saved_ch = strip_timestamp(bol, &eol);
907 break;
908 default:
909 break;
910 }
Junio C Hamanoa4d7d2c2008-09-04 22:15:02 -0700911 }
912
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700913 again:
Michał Kiedrowicz97e77782011-05-05 00:00:19 +0200914 hit = patmatch(p, bol, eol, pmatch, eflags);
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700915
René Scharfed7eb5272009-03-07 13:28:40 +0100916 if (hit && p->word_regexp) {
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700917 if ((pmatch[0].rm_so < 0) ||
René Scharfe84201ea2009-06-03 18:19:01 +0200918 (eol - bol) < pmatch[0].rm_so ||
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700919 (pmatch[0].rm_eo < 0) ||
920 (eol - bol) < pmatch[0].rm_eo)
921 die("regexp returned nonsense");
922
923 /* Match beginning must be either beginning of the
924 * line, or at word boundary (i.e. the last char must
925 * not be a word char). Similarly, match end must be
926 * either end of the line, or at word boundary
927 * (i.e. the next char must not be a word char).
928 */
René Scharfefb62eb72009-01-10 00:08:40 +0100929 if ( ((pmatch[0].rm_so == 0) ||
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700930 !word_char(bol[pmatch[0].rm_so-1])) &&
931 ((pmatch[0].rm_eo == (eol-bol)) ||
932 !word_char(bol[pmatch[0].rm_eo])) )
933 ;
934 else
935 hit = 0;
936
René Scharfe84201ea2009-06-03 18:19:01 +0200937 /* Words consist of at least one character. */
938 if (pmatch->rm_so == pmatch->rm_eo)
939 hit = 0;
940
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700941 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
942 /* There could be more than one match on the
943 * line, and the first match might not be
944 * strict word match. But later ones could be!
René Scharfefb62eb72009-01-10 00:08:40 +0100945 * Forward to the next possible start, i.e. the
946 * next position following a non-word char.
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700947 */
948 bol = pmatch[0].rm_so + bol + 1;
René Scharfefb62eb72009-01-10 00:08:40 +0100949 while (word_char(bol[-1]) && bol < eol)
950 bol++;
René Scharfedbb6a4a2009-05-23 13:45:26 +0200951 eflags |= REG_NOTBOL;
René Scharfefb62eb72009-01-10 00:08:40 +0100952 if (bol < eol)
953 goto again;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700954 }
955 }
Junio C Hamanoa4d7d2c2008-09-04 22:15:02 -0700956 if (p->token == GREP_PATTERN_HEAD && saved_ch)
957 *eol = saved_ch;
René Scharfee701fad2009-05-20 23:31:53 +0200958 if (hit) {
959 pmatch[0].rm_so += bol - start;
960 pmatch[0].rm_eo += bol - start;
961 }
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700962 return hit;
963}
964
René Scharfed7eb5272009-03-07 13:28:40 +0100965static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
966 enum grep_context ctx, int collect_hits)
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700967{
Junio C Hamano0ab7bef2006-09-27 17:50:52 -0700968 int h = 0;
René Scharfe79212772009-03-07 13:30:27 +0100969 regmatch_t match;
Junio C Hamano0ab7bef2006-09-27 17:50:52 -0700970
Linus Torvaldsc922b012009-04-27 11:10:24 -0700971 if (!x)
972 die("Not a valid grep expression");
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700973 switch (x->node) {
Junio C Hamano5aaeb732010-09-12 22:15:35 -0700974 case GREP_NODE_TRUE:
975 h = 1;
976 break;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700977 case GREP_NODE_ATOM:
René Scharfe79212772009-03-07 13:30:27 +0100978 h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700979 break;
980 case GREP_NODE_NOT:
René Scharfed7eb5272009-03-07 13:28:40 +0100981 h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
Junio C Hamano0ab7bef2006-09-27 17:50:52 -0700982 break;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700983 case GREP_NODE_AND:
René Scharfed7eb5272009-03-07 13:28:40 +0100984 if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
René Scharfe252d5602009-03-07 13:27:15 +0100985 return 0;
René Scharfed7eb5272009-03-07 13:28:40 +0100986 h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
Junio C Hamano0ab7bef2006-09-27 17:50:52 -0700987 break;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700988 case GREP_NODE_OR:
Junio C Hamano0ab7bef2006-09-27 17:50:52 -0700989 if (!collect_hits)
René Scharfed7eb5272009-03-07 13:28:40 +0100990 return (match_expr_eval(x->u.binary.left,
Junio C Hamano0ab7bef2006-09-27 17:50:52 -0700991 bol, eol, ctx, 0) ||
René Scharfed7eb5272009-03-07 13:28:40 +0100992 match_expr_eval(x->u.binary.right,
Junio C Hamano0ab7bef2006-09-27 17:50:52 -0700993 bol, eol, ctx, 0));
René Scharfed7eb5272009-03-07 13:28:40 +0100994 h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
Junio C Hamano0ab7bef2006-09-27 17:50:52 -0700995 x->u.binary.left->hit |= h;
René Scharfed7eb5272009-03-07 13:28:40 +0100996 h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
Junio C Hamano0ab7bef2006-09-27 17:50:52 -0700997 break;
998 default:
Alexander Potashevd7530702009-01-04 21:38:41 +0300999 die("Unexpected node type (internal error) %d", x->node);
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001000 }
Junio C Hamano0ab7bef2006-09-27 17:50:52 -07001001 if (collect_hits)
1002 x->hit |= h;
1003 return h;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001004}
1005
Junio C Hamano480c1ca2006-09-20 12:39:46 -07001006static int match_expr(struct grep_opt *opt, char *bol, char *eol,
Junio C Hamano0ab7bef2006-09-27 17:50:52 -07001007 enum grep_context ctx, int collect_hits)
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001008{
1009 struct grep_expr *x = opt->pattern_expression;
René Scharfed7eb5272009-03-07 13:28:40 +01001010 return match_expr_eval(x, bol, eol, ctx, collect_hits);
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001011}
1012
Junio C Hamano480c1ca2006-09-20 12:39:46 -07001013static int match_line(struct grep_opt *opt, char *bol, char *eol,
Junio C Hamano0ab7bef2006-09-27 17:50:52 -07001014 enum grep_context ctx, int collect_hits)
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001015{
1016 struct grep_pat *p;
René Scharfe79212772009-03-07 13:30:27 +01001017 regmatch_t match;
1018
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001019 if (opt->extended)
Junio C Hamano0ab7bef2006-09-27 17:50:52 -07001020 return match_expr(opt, bol, eol, ctx, collect_hits);
1021
1022 /* we do not call with collect_hits without being extended */
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001023 for (p = opt->pattern_list; p; p = p->next) {
René Scharfe79212772009-03-07 13:30:27 +01001024 if (match_one_pattern(p, bol, eol, ctx, &match, 0))
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001025 return 1;
1026 }
1027 return 0;
1028}
1029
René Scharfe7e8f59d2009-03-07 13:32:32 +01001030static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
1031 enum grep_context ctx,
1032 regmatch_t *pmatch, int eflags)
1033{
1034 regmatch_t match;
1035
1036 if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
1037 return 0;
1038 if (match.rm_so < 0 || match.rm_eo < 0)
1039 return 0;
1040 if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
1041 if (match.rm_so > pmatch->rm_so)
1042 return 1;
1043 if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
1044 return 1;
1045 }
1046 pmatch->rm_so = match.rm_so;
1047 pmatch->rm_eo = match.rm_eo;
1048 return 1;
1049}
1050
1051static int next_match(struct grep_opt *opt, char *bol, char *eol,
1052 enum grep_context ctx, regmatch_t *pmatch, int eflags)
1053{
1054 struct grep_pat *p;
1055 int hit = 0;
1056
1057 pmatch->rm_so = pmatch->rm_eo = -1;
1058 if (bol < eol) {
1059 for (p = opt->pattern_list; p; p = p->next) {
1060 switch (p->token) {
1061 case GREP_PATTERN: /* atom */
1062 case GREP_PATTERN_HEAD:
1063 case GREP_PATTERN_BODY:
1064 hit |= match_next_pattern(p, bol, eol, ctx,
1065 pmatch, eflags);
1066 break;
1067 default:
1068 break;
1069 }
1070 }
1071 }
1072 return hit;
1073}
1074
1075static void show_line(struct grep_opt *opt, char *bol, char *eol,
1076 const char *name, unsigned lno, char sign)
1077{
1078 int rest = eol - bol;
Mark Lodato00588bb2010-03-07 11:52:47 -05001079 char *line_color = NULL;
René Scharfe7e8f59d2009-03-07 13:32:32 +01001080
René Scharfea8f0e762011-06-05 17:24:25 +02001081 if (opt->file_break && opt->last_shown == 0) {
1082 if (opt->show_hunk_mark)
1083 opt->output(opt, "\n", 1);
René Scharfeba8ea742011-08-01 19:20:53 +02001084 } else if (opt->pre_context || opt->post_context || opt->funcbody) {
René Scharfe046802d2009-07-02 00:03:44 +02001085 if (opt->last_shown == 0) {
Mark Lodato55f638b2010-03-07 11:52:46 -05001086 if (opt->show_hunk_mark) {
1087 output_color(opt, "--", 2, opt->color_sep);
1088 opt->output(opt, "\n", 1);
Junio C Hamano07b838f2010-04-03 12:28:39 -07001089 }
Mark Lodato55f638b2010-03-07 11:52:46 -05001090 } else if (lno > opt->last_shown + 1) {
1091 output_color(opt, "--", 2, opt->color_sep);
1092 opt->output(opt, "\n", 1);
1093 }
René Scharfe5dd06d32009-07-02 00:02:38 +02001094 }
René Scharfe1d84f722011-06-05 17:24:36 +02001095 if (opt->heading && opt->last_shown == 0) {
1096 output_color(opt, name, strlen(name), opt->color_filename);
1097 opt->output(opt, "\n", 1);
1098 }
René Scharfe5dd06d32009-07-02 00:02:38 +02001099 opt->last_shown = lno;
1100
René Scharfe1d84f722011-06-05 17:24:36 +02001101 if (!opt->heading && opt->pathname) {
Mark Lodato55f638b2010-03-07 11:52:46 -05001102 output_color(opt, name, strlen(name), opt->color_filename);
1103 output_sep(opt, sign);
Fredrik Kuivinen5b594f42010-01-25 23:51:39 +01001104 }
1105 if (opt->linenum) {
1106 char buf[32];
1107 snprintf(buf, sizeof(buf), "%d", lno);
Mark Lodato55f638b2010-03-07 11:52:46 -05001108 output_color(opt, buf, strlen(buf), opt->color_lineno);
1109 output_sep(opt, sign);
Fredrik Kuivinen5b594f42010-01-25 23:51:39 +01001110 }
René Scharfe7e8f59d2009-03-07 13:32:32 +01001111 if (opt->color) {
1112 regmatch_t match;
1113 enum grep_context ctx = GREP_CONTEXT_BODY;
1114 int ch = *eol;
1115 int eflags = 0;
1116
Mark Lodato00588bb2010-03-07 11:52:47 -05001117 if (sign == ':')
1118 line_color = opt->color_selected;
1119 else if (sign == '-')
1120 line_color = opt->color_context;
1121 else if (sign == '=')
1122 line_color = opt->color_function;
René Scharfe7e8f59d2009-03-07 13:32:32 +01001123 *eol = '\0';
1124 while (next_match(opt, bol, eol, ctx, &match, eflags)) {
René Scharfe1f5b9cc2009-06-01 23:53:05 +02001125 if (match.rm_so == match.rm_eo)
1126 break;
Fredrik Kuivinen5b594f42010-01-25 23:51:39 +01001127
Mark Lodato00588bb2010-03-07 11:52:47 -05001128 output_color(opt, bol, match.rm_so, line_color);
Mark Lodato55f638b2010-03-07 11:52:46 -05001129 output_color(opt, bol + match.rm_so,
1130 match.rm_eo - match.rm_so,
1131 opt->color_match);
René Scharfe7e8f59d2009-03-07 13:32:32 +01001132 bol += match.rm_eo;
1133 rest -= match.rm_eo;
1134 eflags = REG_NOTBOL;
1135 }
1136 *eol = ch;
1137 }
Mark Lodato00588bb2010-03-07 11:52:47 -05001138 output_color(opt, bol, rest, line_color);
Fredrik Kuivinen5b594f42010-01-25 23:51:39 +01001139 opt->output(opt, "\n", 1);
René Scharfe7e8f59d2009-03-07 13:32:32 +01001140}
1141
Thomas Rast0579f912011-12-12 22:16:07 +01001142#ifndef NO_PTHREADS
Jeff King78db6ea2012-02-02 03:18:29 -05001143int grep_use_locks;
1144
Thomas Rast0579f912011-12-12 22:16:07 +01001145/*
1146 * This lock protects access to the gitattributes machinery, which is
1147 * not thread-safe.
1148 */
1149pthread_mutex_t grep_attr_mutex;
1150
Jeff King78db6ea2012-02-02 03:18:29 -05001151static inline void grep_attr_lock(void)
Thomas Rast0579f912011-12-12 22:16:07 +01001152{
Jeff King78db6ea2012-02-02 03:18:29 -05001153 if (grep_use_locks)
Thomas Rast0579f912011-12-12 22:16:07 +01001154 pthread_mutex_lock(&grep_attr_mutex);
1155}
1156
Jeff King78db6ea2012-02-02 03:18:29 -05001157static inline void grep_attr_unlock(void)
Thomas Rast0579f912011-12-12 22:16:07 +01001158{
Jeff King78db6ea2012-02-02 03:18:29 -05001159 if (grep_use_locks)
Thomas Rast0579f912011-12-12 22:16:07 +01001160 pthread_mutex_unlock(&grep_attr_mutex);
1161}
Jeff Kingb3aeb282012-02-02 03:18:41 -05001162
1163/*
1164 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
1165 */
1166pthread_mutex_t grep_read_mutex;
1167
Thomas Rast0579f912011-12-12 22:16:07 +01001168#else
Jeff King78db6ea2012-02-02 03:18:29 -05001169#define grep_attr_lock()
1170#define grep_attr_unlock()
Thomas Rast0579f912011-12-12 22:16:07 +01001171#endif
1172
Jeff Kinge1327022012-02-02 03:19:28 -05001173static int match_funcname(struct grep_opt *opt, struct grep_source *gs, char *bol, char *eol)
René Scharfe2944e4e2009-07-02 00:06:34 +02001174{
René Scharfe60ecac92009-07-02 00:07:24 +02001175 xdemitconf_t *xecfg = opt->priv;
Thomas Rast0579f912011-12-12 22:16:07 +01001176 if (xecfg && !xecfg->find_func) {
Jeff King94ad9d92012-02-02 03:20:43 -05001177 grep_source_load_driver(gs);
1178 if (gs->driver->funcname.pattern) {
1179 const struct userdiff_funcname *pe = &gs->driver->funcname;
Thomas Rast0579f912011-12-12 22:16:07 +01001180 xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
1181 } else {
1182 xecfg = opt->priv = NULL;
1183 }
1184 }
1185
1186 if (xecfg) {
René Scharfe60ecac92009-07-02 00:07:24 +02001187 char buf[1];
1188 return xecfg->find_func(bol, eol - bol, buf, 1,
1189 xecfg->find_func_priv) >= 0;
1190 }
1191
René Scharfe2944e4e2009-07-02 00:06:34 +02001192 if (bol == eol)
1193 return 0;
1194 if (isalpha(*bol) || *bol == '_' || *bol == '$')
1195 return 1;
1196 return 0;
1197}
1198
Jeff Kinge1327022012-02-02 03:19:28 -05001199static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
1200 char *bol, unsigned lno)
René Scharfe2944e4e2009-07-02 00:06:34 +02001201{
Jeff Kinge1327022012-02-02 03:19:28 -05001202 while (bol > gs->buf) {
René Scharfe2944e4e2009-07-02 00:06:34 +02001203 char *eol = --bol;
1204
Jeff Kinge1327022012-02-02 03:19:28 -05001205 while (bol > gs->buf && bol[-1] != '\n')
René Scharfe2944e4e2009-07-02 00:06:34 +02001206 bol--;
1207 lno--;
1208
1209 if (lno <= opt->last_shown)
1210 break;
1211
Jeff Kinge1327022012-02-02 03:19:28 -05001212 if (match_funcname(opt, gs, bol, eol)) {
1213 show_line(opt, bol, eol, gs->name, lno, '=');
René Scharfe2944e4e2009-07-02 00:06:34 +02001214 break;
1215 }
1216 }
1217}
1218
Jeff Kinge1327022012-02-02 03:19:28 -05001219static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
René Scharfeba8ea742011-08-01 19:20:53 +02001220 char *bol, char *end, unsigned lno)
René Scharfe49de3212009-07-02 00:05:17 +02001221{
René Scharfe2944e4e2009-07-02 00:06:34 +02001222 unsigned cur = lno, from = 1, funcname_lno = 0;
René Scharfeba8ea742011-08-01 19:20:53 +02001223 int funcname_needed = !!opt->funcname;
1224
Jeff Kinge1327022012-02-02 03:19:28 -05001225 if (opt->funcbody && !match_funcname(opt, gs, bol, end))
René Scharfeba8ea742011-08-01 19:20:53 +02001226 funcname_needed = 2;
René Scharfe49de3212009-07-02 00:05:17 +02001227
1228 if (opt->pre_context < lno)
1229 from = lno - opt->pre_context;
1230 if (from <= opt->last_shown)
1231 from = opt->last_shown + 1;
1232
1233 /* Rewind. */
Jeff Kinge1327022012-02-02 03:19:28 -05001234 while (bol > gs->buf &&
René Scharfeba8ea742011-08-01 19:20:53 +02001235 cur > (funcname_needed == 2 ? opt->last_shown + 1 : from)) {
René Scharfe2944e4e2009-07-02 00:06:34 +02001236 char *eol = --bol;
1237
Jeff Kinge1327022012-02-02 03:19:28 -05001238 while (bol > gs->buf && bol[-1] != '\n')
René Scharfe49de3212009-07-02 00:05:17 +02001239 bol--;
1240 cur--;
Jeff Kinge1327022012-02-02 03:19:28 -05001241 if (funcname_needed && match_funcname(opt, gs, bol, eol)) {
René Scharfe2944e4e2009-07-02 00:06:34 +02001242 funcname_lno = cur;
1243 funcname_needed = 0;
1244 }
René Scharfe49de3212009-07-02 00:05:17 +02001245 }
1246
René Scharfe2944e4e2009-07-02 00:06:34 +02001247 /* We need to look even further back to find a function signature. */
1248 if (opt->funcname && funcname_needed)
Jeff Kinge1327022012-02-02 03:19:28 -05001249 show_funcname_line(opt, gs, bol, cur);
René Scharfe2944e4e2009-07-02 00:06:34 +02001250
René Scharfe49de3212009-07-02 00:05:17 +02001251 /* Back forward. */
1252 while (cur < lno) {
René Scharfe2944e4e2009-07-02 00:06:34 +02001253 char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
René Scharfe49de3212009-07-02 00:05:17 +02001254
1255 while (*eol != '\n')
1256 eol++;
Jeff Kinge1327022012-02-02 03:19:28 -05001257 show_line(opt, bol, eol, gs->name, cur, sign);
René Scharfe49de3212009-07-02 00:05:17 +02001258 bol = eol + 1;
1259 cur++;
1260 }
1261}
1262
Junio C Hamanoa26345b2010-01-10 22:39:36 -08001263static int should_lookahead(struct grep_opt *opt)
1264{
1265 struct grep_pat *p;
1266
1267 if (opt->extended)
1268 return 0; /* punt for too complex stuff */
1269 if (opt->invert)
1270 return 0;
1271 for (p = opt->pattern_list; p; p = p->next) {
1272 if (p->token != GREP_PATTERN)
1273 return 0; /* punt for "header only" and stuff */
1274 }
1275 return 1;
1276}
1277
1278static int look_ahead(struct grep_opt *opt,
1279 unsigned long *left_p,
1280 unsigned *lno_p,
1281 char **bol_p)
1282{
1283 unsigned lno = *lno_p;
1284 char *bol = *bol_p;
1285 struct grep_pat *p;
1286 char *sp, *last_bol;
1287 regoff_t earliest = -1;
1288
1289 for (p = opt->pattern_list; p; p = p->next) {
1290 int hit;
1291 regmatch_t m;
1292
Michał Kiedrowicz97e77782011-05-05 00:00:19 +02001293 hit = patmatch(p, bol, bol + *left_p, &m, 0);
Junio C Hamanoa26345b2010-01-10 22:39:36 -08001294 if (!hit || m.rm_so < 0 || m.rm_eo < 0)
1295 continue;
1296 if (earliest < 0 || m.rm_so < earliest)
1297 earliest = m.rm_so;
1298 }
1299
1300 if (earliest < 0) {
1301 *bol_p = bol + *left_p;
1302 *left_p = 0;
1303 return 1;
1304 }
1305 for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
1306 ; /* find the beginning of the line */
1307 last_bol = sp;
1308
1309 for (sp = bol; sp < last_bol; sp++) {
1310 if (*sp == '\n')
1311 lno++;
1312 }
1313 *left_p -= last_bol - bol;
1314 *bol_p = last_bol;
1315 *lno_p = lno;
1316 return 0;
1317}
1318
Fredrik Kuivinen5b594f42010-01-25 23:51:39 +01001319static void std_output(struct grep_opt *opt, const void *buf, size_t size)
1320{
1321 fwrite(buf, size, 1, stdout);
1322}
1323
Jeff Kinge1327022012-02-02 03:19:28 -05001324static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits)
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001325{
Jeff Kinge1327022012-02-02 03:19:28 -05001326 char *bol;
1327 unsigned long left;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001328 unsigned lno = 1;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001329 unsigned last_hit = 0;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001330 int binary_match_only = 0;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001331 unsigned count = 0;
Junio C Hamanoa26345b2010-01-10 22:39:36 -08001332 int try_lookahead = 0;
René Scharfeba8ea742011-08-01 19:20:53 +02001333 int show_function = 0;
Junio C Hamano480c1ca2006-09-20 12:39:46 -07001334 enum grep_context ctx = GREP_CONTEXT_HEAD;
René Scharfe60ecac92009-07-02 00:07:24 +02001335 xdemitconf_t xecfg;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001336
Fredrik Kuivinen5b594f42010-01-25 23:51:39 +01001337 if (!opt->output)
1338 opt->output = std_output;
1339
René Scharfeba8ea742011-08-01 19:20:53 +02001340 if (opt->pre_context || opt->post_context || opt->file_break ||
1341 opt->funcbody) {
René Scharfe08303c32011-06-05 17:24:15 +02001342 /* Show hunk marks, except for the first file. */
1343 if (opt->last_shown)
1344 opt->show_hunk_mark = 1;
1345 /*
1346 * If we're using threads then we can't easily identify
1347 * the first file. Always put hunk marks in that case
1348 * and skip the very first one later in work_done().
1349 */
1350 if (opt->output != std_output)
1351 opt->show_hunk_mark = 1;
1352 }
René Scharfe431d6e72010-03-15 17:21:10 +01001353 opt->last_shown = 0;
1354
René Scharfe64fcec72010-05-22 23:28:17 +02001355 switch (opt->binary) {
1356 case GREP_BINARY_DEFAULT:
Jeff King41b59bf2012-02-02 03:21:02 -05001357 if (grep_source_is_binary(gs))
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001358 binary_match_only = 1;
René Scharfe64fcec72010-05-22 23:28:17 +02001359 break;
1360 case GREP_BINARY_NOMATCH:
Jeff King41b59bf2012-02-02 03:21:02 -05001361 if (grep_source_is_binary(gs))
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001362 return 0; /* Assume unmatch */
René Scharfe64fcec72010-05-22 23:28:17 +02001363 break;
1364 case GREP_BINARY_TEXT:
1365 break;
1366 default:
1367 die("bug: unknown binary handling mode");
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001368 }
1369
René Scharfe60ecac92009-07-02 00:07:24 +02001370 memset(&xecfg, 0, sizeof(xecfg));
Thomas Rast0579f912011-12-12 22:16:07 +01001371 opt->priv = &xecfg;
1372
Junio C Hamanoa26345b2010-01-10 22:39:36 -08001373 try_lookahead = should_lookahead(opt);
René Scharfe60ecac92009-07-02 00:07:24 +02001374
Jeff King08265792012-02-02 03:21:11 -05001375 if (grep_source_load(gs) < 0)
1376 return 0;
1377
Jeff Kinge1327022012-02-02 03:19:28 -05001378 bol = gs->buf;
1379 left = gs->size;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001380 while (left) {
1381 char *eol, ch;
Junio C Hamano0ab7bef2006-09-27 17:50:52 -07001382 int hit;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001383
Junio C Hamanoa26345b2010-01-10 22:39:36 -08001384 /*
Michał Kiedrowicz8997da32011-05-09 23:52:03 +02001385 * look_ahead() skips quickly to the line that possibly
Junio C Hamanoa26345b2010-01-10 22:39:36 -08001386 * has the next hit; don't call it if we need to do
1387 * something more than just skipping the current line
1388 * in response to an unmatch for the current line. E.g.
1389 * inside a post-context window, we will show the current
1390 * line as a context around the previous hit when it
1391 * doesn't hit.
1392 */
1393 if (try_lookahead
1394 && !(last_hit
René Scharfeba8ea742011-08-01 19:20:53 +02001395 && (show_function ||
1396 lno <= last_hit + opt->post_context))
Junio C Hamanoa26345b2010-01-10 22:39:36 -08001397 && look_ahead(opt, &left, &lno, &bol))
1398 break;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001399 eol = end_of_line(bol, &left);
1400 ch = *eol;
1401 *eol = 0;
1402
Junio C Hamano480c1ca2006-09-20 12:39:46 -07001403 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
1404 ctx = GREP_CONTEXT_BODY;
1405
Junio C Hamano0ab7bef2006-09-27 17:50:52 -07001406 hit = match_line(opt, bol, eol, ctx, collect_hits);
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001407 *eol = ch;
1408
Junio C Hamano0ab7bef2006-09-27 17:50:52 -07001409 if (collect_hits)
1410 goto next_line;
1411
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001412 /* "grep -v -e foo -e bla" should list lines
1413 * that do not have either, so inversion should
1414 * be done outside.
1415 */
1416 if (opt->invert)
1417 hit = !hit;
1418 if (opt->unmatch_name_only) {
1419 if (hit)
1420 return 0;
1421 goto next_line;
1422 }
1423 if (hit) {
1424 count++;
1425 if (opt->status_only)
1426 return 1;
René Scharfe321ffcc2010-05-22 23:30:48 +02001427 if (opt->name_only) {
Jeff Kinge1327022012-02-02 03:19:28 -05001428 show_name(opt, gs->name);
René Scharfe321ffcc2010-05-22 23:30:48 +02001429 return 1;
1430 }
René Scharfec30c10c2010-05-22 23:29:35 +02001431 if (opt->count)
1432 goto next_line;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001433 if (binary_match_only) {
Fredrik Kuivinen5b594f42010-01-25 23:51:39 +01001434 opt->output(opt, "Binary file ", 12);
Jeff Kinge1327022012-02-02 03:19:28 -05001435 output_color(opt, gs->name, strlen(gs->name),
Mark Lodato55f638b2010-03-07 11:52:46 -05001436 opt->color_filename);
Fredrik Kuivinen5b594f42010-01-25 23:51:39 +01001437 opt->output(opt, " matches\n", 9);
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001438 return 1;
1439 }
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001440 /* Hit at this line. If we haven't shown the
1441 * pre-context lines, we would need to show them.
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001442 */
René Scharfeba8ea742011-08-01 19:20:53 +02001443 if (opt->pre_context || opt->funcbody)
Jeff Kinge1327022012-02-02 03:19:28 -05001444 show_pre_context(opt, gs, bol, eol, lno);
René Scharfe2944e4e2009-07-02 00:06:34 +02001445 else if (opt->funcname)
Jeff Kinge1327022012-02-02 03:19:28 -05001446 show_funcname_line(opt, gs, bol, lno);
1447 show_line(opt, bol, eol, gs->name, lno, ':');
René Scharfe5dd06d32009-07-02 00:02:38 +02001448 last_hit = lno;
René Scharfeba8ea742011-08-01 19:20:53 +02001449 if (opt->funcbody)
1450 show_function = 1;
1451 goto next_line;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001452 }
Jeff Kinge1327022012-02-02 03:19:28 -05001453 if (show_function && match_funcname(opt, gs, bol, eol))
René Scharfeba8ea742011-08-01 19:20:53 +02001454 show_function = 0;
1455 if (show_function ||
1456 (last_hit && lno <= last_hit + opt->post_context)) {
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001457 /* If the last hit is within the post context,
1458 * we need to show this line.
1459 */
Jeff Kinge1327022012-02-02 03:19:28 -05001460 show_line(opt, bol, eol, gs->name, lno, '-');
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001461 }
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001462
1463 next_line:
1464 bol = eol + 1;
1465 if (!left)
1466 break;
1467 left--;
1468 lno++;
1469 }
1470
Junio C Hamano0ab7bef2006-09-27 17:50:52 -07001471 if (collect_hits)
1472 return 0;
Junio C Hamanob48fb5b2006-09-27 16:27:10 -07001473
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001474 if (opt->status_only)
1475 return 0;
1476 if (opt->unmatch_name_only) {
1477 /* We did not see any hit, so we want to show this */
Jeff Kinge1327022012-02-02 03:19:28 -05001478 show_name(opt, gs->name);
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001479 return 1;
1480 }
1481
René Scharfe60ecac92009-07-02 00:07:24 +02001482 xdiff_clear_find_func(&xecfg);
1483 opt->priv = NULL;
1484
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001485 /* NEEDSWORK:
1486 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1487 * which feels mostly useless but sometimes useful. Maybe
1488 * make it another option? For now suppress them.
1489 */
Fredrik Kuivinen5b594f42010-01-25 23:51:39 +01001490 if (opt->count && count) {
1491 char buf[32];
Jeff Kinge1327022012-02-02 03:19:28 -05001492 output_color(opt, gs->name, strlen(gs->name), opt->color_filename);
Mark Lodato55f638b2010-03-07 11:52:46 -05001493 output_sep(opt, ':');
1494 snprintf(buf, sizeof(buf), "%u\n", count);
Fredrik Kuivinen5b594f42010-01-25 23:51:39 +01001495 opt->output(opt, buf, strlen(buf));
René Scharfec30c10c2010-05-22 23:29:35 +02001496 return 1;
Fredrik Kuivinen5b594f42010-01-25 23:51:39 +01001497 }
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001498 return !!last_hit;
1499}
1500
Junio C Hamano0ab7bef2006-09-27 17:50:52 -07001501static void clr_hit_marker(struct grep_expr *x)
1502{
1503 /* All-hit markers are meaningful only at the very top level
1504 * OR node.
1505 */
1506 while (1) {
1507 x->hit = 0;
1508 if (x->node != GREP_NODE_OR)
1509 return;
1510 x->u.binary.left->hit = 0;
1511 x = x->u.binary.right;
1512 }
1513}
1514
1515static int chk_hit_marker(struct grep_expr *x)
1516{
1517 /* Top level nodes have hit markers. See if they all are hits */
1518 while (1) {
1519 if (x->node != GREP_NODE_OR)
1520 return x->hit;
1521 if (!x->u.binary.left->hit)
1522 return 0;
1523 x = x->u.binary.right;
1524 }
1525}
1526
Jeff Kinge1327022012-02-02 03:19:28 -05001527int grep_source(struct grep_opt *opt, struct grep_source *gs)
Junio C Hamano0ab7bef2006-09-27 17:50:52 -07001528{
1529 /*
1530 * we do not have to do the two-pass grep when we do not check
1531 * buffer-wide "all-match".
1532 */
1533 if (!opt->all_match)
Jeff Kinge1327022012-02-02 03:19:28 -05001534 return grep_source_1(opt, gs, 0);
Junio C Hamano0ab7bef2006-09-27 17:50:52 -07001535
1536 /* Otherwise the toplevel "or" terms hit a bit differently.
1537 * We first clear hit markers from them.
1538 */
1539 clr_hit_marker(opt->pattern_expression);
Jeff Kinge1327022012-02-02 03:19:28 -05001540 grep_source_1(opt, gs, 1);
Junio C Hamano0ab7bef2006-09-27 17:50:52 -07001541
1542 if (!chk_hit_marker(opt->pattern_expression))
1543 return 0;
1544
Jeff Kinge1327022012-02-02 03:19:28 -05001545 return grep_source_1(opt, gs, 0);
1546}
1547
Jeff Kingc876d6d2012-02-02 03:20:10 -05001548int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size)
Jeff Kinge1327022012-02-02 03:19:28 -05001549{
1550 struct grep_source gs;
1551 int r;
1552
Nguyễn Thái Ngọc Duy55c61682012-10-12 17:49:38 +07001553 grep_source_init(&gs, GREP_SOURCE_BUF, NULL, NULL, NULL);
Jeff Kinge1327022012-02-02 03:19:28 -05001554 gs.buf = buf;
1555 gs.size = size;
1556
1557 r = grep_source(opt, &gs);
1558
1559 grep_source_clear(&gs);
1560 return r;
1561}
1562
1563void grep_source_init(struct grep_source *gs, enum grep_source_type type,
Nguyễn Thái Ngọc Duy55c61682012-10-12 17:49:38 +07001564 const char *name, const char *path,
1565 const void *identifier)
Jeff Kinge1327022012-02-02 03:19:28 -05001566{
1567 gs->type = type;
1568 gs->name = name ? xstrdup(name) : NULL;
Nguyễn Thái Ngọc Duy55c61682012-10-12 17:49:38 +07001569 gs->path = path ? xstrdup(path) : NULL;
Jeff Kinge1327022012-02-02 03:19:28 -05001570 gs->buf = NULL;
1571 gs->size = 0;
Jeff King94ad9d92012-02-02 03:20:43 -05001572 gs->driver = NULL;
Jeff Kinge1327022012-02-02 03:19:28 -05001573
1574 switch (type) {
1575 case GREP_SOURCE_FILE:
1576 gs->identifier = xstrdup(identifier);
1577 break;
1578 case GREP_SOURCE_SHA1:
1579 gs->identifier = xmalloc(20);
1580 memcpy(gs->identifier, identifier, 20);
1581 break;
1582 case GREP_SOURCE_BUF:
1583 gs->identifier = NULL;
1584 }
1585}
1586
1587void grep_source_clear(struct grep_source *gs)
1588{
1589 free(gs->name);
1590 gs->name = NULL;
Nguyễn Thái Ngọc Duy55c61682012-10-12 17:49:38 +07001591 free(gs->path);
1592 gs->path = NULL;
Jeff Kinge1327022012-02-02 03:19:28 -05001593 free(gs->identifier);
1594 gs->identifier = NULL;
1595 grep_source_clear_data(gs);
1596}
1597
1598void grep_source_clear_data(struct grep_source *gs)
1599{
1600 switch (gs->type) {
1601 case GREP_SOURCE_FILE:
1602 case GREP_SOURCE_SHA1:
1603 free(gs->buf);
1604 gs->buf = NULL;
1605 gs->size = 0;
1606 break;
1607 case GREP_SOURCE_BUF:
1608 /* leave user-provided buf intact */
1609 break;
1610 }
1611}
1612
1613static int grep_source_load_sha1(struct grep_source *gs)
1614{
1615 enum object_type type;
1616
1617 grep_read_lock();
1618 gs->buf = read_sha1_file(gs->identifier, &type, &gs->size);
1619 grep_read_unlock();
1620
1621 if (!gs->buf)
1622 return error(_("'%s': unable to read %s"),
1623 gs->name,
1624 sha1_to_hex(gs->identifier));
1625 return 0;
1626}
1627
1628static int grep_source_load_file(struct grep_source *gs)
1629{
1630 const char *filename = gs->identifier;
1631 struct stat st;
1632 char *data;
1633 size_t size;
1634 int i;
1635
1636 if (lstat(filename, &st) < 0) {
1637 err_ret:
1638 if (errno != ENOENT)
1639 error(_("'%s': %s"), filename, strerror(errno));
1640 return -1;
1641 }
1642 if (!S_ISREG(st.st_mode))
1643 return -1;
1644 size = xsize_t(st.st_size);
1645 i = open(filename, O_RDONLY);
1646 if (i < 0)
1647 goto err_ret;
1648 data = xmalloc(size + 1);
1649 if (st.st_size != read_in_full(i, data, size)) {
1650 error(_("'%s': short read %s"), filename, strerror(errno));
1651 close(i);
1652 free(data);
1653 return -1;
1654 }
1655 close(i);
1656 data[size] = 0;
1657
1658 gs->buf = data;
1659 gs->size = size;
1660 return 0;
1661}
1662
Junio C Hamano30833012012-09-20 14:20:09 -07001663static int grep_source_load(struct grep_source *gs)
Jeff Kinge1327022012-02-02 03:19:28 -05001664{
1665 if (gs->buf)
1666 return 0;
1667
1668 switch (gs->type) {
1669 case GREP_SOURCE_FILE:
1670 return grep_source_load_file(gs);
1671 case GREP_SOURCE_SHA1:
1672 return grep_source_load_sha1(gs);
1673 case GREP_SOURCE_BUF:
1674 return gs->buf ? 0 : -1;
1675 }
1676 die("BUG: invalid grep_source type");
Junio C Hamano0ab7bef2006-09-27 17:50:52 -07001677}
Jeff King94ad9d92012-02-02 03:20:43 -05001678
1679void grep_source_load_driver(struct grep_source *gs)
1680{
1681 if (gs->driver)
1682 return;
1683
1684 grep_attr_lock();
Nguyễn Thái Ngọc Duy55c61682012-10-12 17:49:38 +07001685 if (gs->path)
1686 gs->driver = userdiff_find_by_path(gs->path);
Jeff King94ad9d92012-02-02 03:20:43 -05001687 if (!gs->driver)
1688 gs->driver = userdiff_find_by_name("default");
1689 grep_attr_unlock();
1690}
Jeff King41b59bf2012-02-02 03:21:02 -05001691
Junio C Hamano30833012012-09-20 14:20:09 -07001692static int grep_source_is_binary(struct grep_source *gs)
Jeff King41b59bf2012-02-02 03:21:02 -05001693{
1694 grep_source_load_driver(gs);
1695 if (gs->driver->binary != -1)
1696 return gs->driver->binary;
1697
1698 if (!grep_source_load(gs))
1699 return buffer_is_binary(gs->buf, gs->size);
1700
1701 return 0;
1702}