]> git.ipfire.org Git - thirdparty/git.git/blame - grep.c
grep: add pmatch and eflags arguments to match_one_pattern()
[thirdparty/git.git] / grep.c
CommitLineData
83b5d2f5 1#include "cache.h"
83b5d2f5 2#include "grep.h"
6bfce93e 3#include "xdiff-interface.h"
83b5d2f5 4
a4d7d2c6
JH
5void append_header_grep_pattern(struct grep_opt *opt, enum grep_header_field field, const char *pat)
6{
7 struct grep_pat *p = xcalloc(1, sizeof(*p));
8 p->pattern = pat;
9 p->origin = "header";
10 p->no = 0;
11 p->token = GREP_PATTERN_HEAD;
12 p->field = field;
13 *opt->pattern_tail = p;
14 opt->pattern_tail = &p->next;
15 p->next = NULL;
16}
17
83b5d2f5
JH
18void append_grep_pattern(struct grep_opt *opt, const char *pat,
19 const char *origin, int no, enum grep_pat_token t)
20{
21 struct grep_pat *p = xcalloc(1, sizeof(*p));
22 p->pattern = pat;
23 p->origin = origin;
24 p->no = no;
25 p->token = t;
26 *opt->pattern_tail = p;
27 opt->pattern_tail = &p->next;
28 p->next = NULL;
29}
30
c822255c
RS
31static int is_fixed(const char *s)
32{
f9b7cce6 33 while (*s && !is_regex_special(*s))
c822255c
RS
34 s++;
35 return !*s;
36}
37
83b5d2f5
JH
38static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
39{
c822255c
RS
40 int err;
41
d7eb527d
RS
42 p->word_regexp = opt->word_regexp;
43
c822255c
RS
44 if (opt->fixed || is_fixed(p->pattern))
45 p->fixed = 1;
46 if (opt->regflags & REG_ICASE)
47 p->fixed = 0;
48 if (p->fixed)
49 return;
50
51 err = regcomp(&p->regexp, p->pattern, opt->regflags);
83b5d2f5
JH
52 if (err) {
53 char errbuf[1024];
54 char where[1024];
55 if (p->no)
56 sprintf(where, "In '%s' at %d, ",
57 p->origin, p->no);
58 else if (p->origin)
59 sprintf(where, "%s, ", p->origin);
60 else
61 where[0] = 0;
62 regerror(err, &p->regexp, errbuf, 1024);
63 regfree(&p->regexp);
64 die("%s'%s': %s", where, p->pattern, errbuf);
65 }
66}
67
0ab7befa 68static struct grep_expr *compile_pattern_or(struct grep_pat **);
83b5d2f5
JH
69static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
70{
71 struct grep_pat *p;
72 struct grep_expr *x;
73
74 p = *list;
75 switch (p->token) {
76 case GREP_PATTERN: /* atom */
480c1ca6
JH
77 case GREP_PATTERN_HEAD:
78 case GREP_PATTERN_BODY:
83b5d2f5
JH
79 x = xcalloc(1, sizeof (struct grep_expr));
80 x->node = GREP_NODE_ATOM;
81 x->u.atom = p;
82 *list = p->next;
83 return x;
84 case GREP_OPEN_PAREN:
85 *list = p->next;
0ab7befa 86 x = compile_pattern_or(list);
83b5d2f5
JH
87 if (!x)
88 return NULL;
89 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
90 die("unmatched parenthesis");
91 *list = (*list)->next;
92 return x;
93 default:
94 return NULL;
95 }
96}
97
98static struct grep_expr *compile_pattern_not(struct grep_pat **list)
99{
100 struct grep_pat *p;
101 struct grep_expr *x;
102
103 p = *list;
104 switch (p->token) {
105 case GREP_NOT:
106 if (!p->next)
107 die("--not not followed by pattern expression");
108 *list = p->next;
109 x = xcalloc(1, sizeof (struct grep_expr));
110 x->node = GREP_NODE_NOT;
111 x->u.unary = compile_pattern_not(list);
112 if (!x->u.unary)
113 die("--not followed by non pattern expression");
114 return x;
115 default:
116 return compile_pattern_atom(list);
117 }
118}
119
120static struct grep_expr *compile_pattern_and(struct grep_pat **list)
121{
122 struct grep_pat *p;
123 struct grep_expr *x, *y, *z;
124
125 x = compile_pattern_not(list);
126 p = *list;
127 if (p && p->token == GREP_AND) {
128 if (!p->next)
129 die("--and not followed by pattern expression");
130 *list = p->next;
131 y = compile_pattern_and(list);
132 if (!y)
133 die("--and not followed by pattern expression");
134 z = xcalloc(1, sizeof (struct grep_expr));
135 z->node = GREP_NODE_AND;
136 z->u.binary.left = x;
137 z->u.binary.right = y;
138 return z;
139 }
140 return x;
141}
142
143static struct grep_expr *compile_pattern_or(struct grep_pat **list)
144{
145 struct grep_pat *p;
146 struct grep_expr *x, *y, *z;
147
148 x = compile_pattern_and(list);
149 p = *list;
150 if (x && p && p->token != GREP_CLOSE_PAREN) {
151 y = compile_pattern_or(list);
152 if (!y)
153 die("not a pattern expression %s", p->pattern);
154 z = xcalloc(1, sizeof (struct grep_expr));
155 z->node = GREP_NODE_OR;
156 z->u.binary.left = x;
157 z->u.binary.right = y;
158 return z;
159 }
160 return x;
161}
162
163static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
164{
165 return compile_pattern_or(list);
166}
167
168void compile_grep_patterns(struct grep_opt *opt)
169{
170 struct grep_pat *p;
171
0ab7befa
JH
172 if (opt->all_match)
173 opt->extended = 1;
174
83b5d2f5 175 for (p = opt->pattern_list; p; p = p->next) {
480c1ca6
JH
176 switch (p->token) {
177 case GREP_PATTERN: /* atom */
178 case GREP_PATTERN_HEAD:
179 case GREP_PATTERN_BODY:
c822255c 180 compile_regexp(p, opt);
480c1ca6
JH
181 break;
182 default:
83b5d2f5 183 opt->extended = 1;
480c1ca6
JH
184 break;
185 }
83b5d2f5
JH
186 }
187
188 if (!opt->extended)
189 return;
190
191 /* Then bundle them up in an expression.
192 * A classic recursive descent parser would do.
193 */
194 p = opt->pattern_list;
195 opt->pattern_expression = compile_pattern_expr(&p);
196 if (p)
197 die("incomplete pattern expression: %s", p->pattern);
198}
199
b48fb5b6
JH
200static void free_pattern_expr(struct grep_expr *x)
201{
202 switch (x->node) {
203 case GREP_NODE_ATOM:
204 break;
205 case GREP_NODE_NOT:
206 free_pattern_expr(x->u.unary);
207 break;
208 case GREP_NODE_AND:
209 case GREP_NODE_OR:
210 free_pattern_expr(x->u.binary.left);
211 free_pattern_expr(x->u.binary.right);
212 break;
213 }
214 free(x);
215}
216
217void free_grep_patterns(struct grep_opt *opt)
218{
219 struct grep_pat *p, *n;
220
221 for (p = opt->pattern_list; p; p = n) {
222 n = p->next;
223 switch (p->token) {
224 case GREP_PATTERN: /* atom */
225 case GREP_PATTERN_HEAD:
226 case GREP_PATTERN_BODY:
227 regfree(&p->regexp);
228 break;
229 default:
230 break;
231 }
232 free(p);
233 }
234
235 if (!opt->extended)
236 return;
237 free_pattern_expr(opt->pattern_expression);
238}
239
83b5d2f5
JH
240static char *end_of_line(char *cp, unsigned long *left)
241{
242 unsigned long l = *left;
243 while (l && *cp != '\n') {
244 l--;
245 cp++;
246 }
247 *left = l;
248 return cp;
249}
250
251static int word_char(char ch)
252{
253 return isalnum(ch) || ch == '_';
254}
255
256static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
257 const char *name, unsigned lno, char sign)
258{
83caecca
RZ
259 if (opt->null_following_name)
260 sign = '\0';
83b5d2f5
JH
261 if (opt->pathname)
262 printf("%s%c", name, sign);
263 if (opt->linenum)
264 printf("%d%c", lno, sign);
265 printf("%.*s\n", (int)(eol-bol), bol);
266}
267
83caecca
RZ
268static void show_name(struct grep_opt *opt, const char *name)
269{
270 printf("%s%c", name, opt->null_following_name ? '\0' : '\n');
271}
272
83b5d2f5
JH
273static int fixmatch(const char *pattern, char *line, regmatch_t *match)
274{
275 char *hit = strstr(line, pattern);
276 if (!hit) {
277 match->rm_so = match->rm_eo = -1;
278 return REG_NOMATCH;
279 }
280 else {
281 match->rm_so = hit - line;
282 match->rm_eo = match->rm_so + strlen(pattern);
283 return 0;
284 }
285}
286
a4d7d2c6
JH
287static int strip_timestamp(char *bol, char **eol_p)
288{
289 char *eol = *eol_p;
290 int ch;
291
292 while (bol < --eol) {
293 if (*eol != '>')
294 continue;
295 *eol_p = ++eol;
296 ch = *eol;
297 *eol = '\0';
298 return ch;
299 }
300 return 0;
301}
302
303static struct {
304 const char *field;
305 size_t len;
306} header_field[] = {
307 { "author ", 7 },
308 { "committer ", 10 },
309};
310
d7eb527d 311static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
79212772
RS
312 enum grep_context ctx,
313 regmatch_t *pmatch, int eflags)
83b5d2f5
JH
314{
315 int hit = 0;
a4d7d2c6 316 int saved_ch = 0;
83b5d2f5 317
480c1ca6
JH
318 if ((p->token != GREP_PATTERN) &&
319 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
320 return 0;
321
a4d7d2c6
JH
322 if (p->token == GREP_PATTERN_HEAD) {
323 const char *field;
324 size_t len;
325 assert(p->field < ARRAY_SIZE(header_field));
326 field = header_field[p->field].field;
327 len = header_field[p->field].len;
328 if (strncmp(bol, field, len))
329 return 0;
330 bol += len;
331 saved_ch = strip_timestamp(bol, &eol);
332 }
333
83b5d2f5 334 again:
79212772 335 if (p->fixed)
83b5d2f5 336 hit = !fixmatch(p->pattern, bol, pmatch);
79212772
RS
337 else
338 hit = !regexec(&p->regexp, bol, 1, pmatch, eflags);
83b5d2f5 339
d7eb527d 340 if (hit && p->word_regexp) {
83b5d2f5
JH
341 if ((pmatch[0].rm_so < 0) ||
342 (eol - bol) <= pmatch[0].rm_so ||
343 (pmatch[0].rm_eo < 0) ||
344 (eol - bol) < pmatch[0].rm_eo)
345 die("regexp returned nonsense");
346
347 /* Match beginning must be either beginning of the
348 * line, or at word boundary (i.e. the last char must
349 * not be a word char). Similarly, match end must be
350 * either end of the line, or at word boundary
351 * (i.e. the next char must not be a word char).
352 */
fb62eb7f 353 if ( ((pmatch[0].rm_so == 0) ||
83b5d2f5
JH
354 !word_char(bol[pmatch[0].rm_so-1])) &&
355 ((pmatch[0].rm_eo == (eol-bol)) ||
356 !word_char(bol[pmatch[0].rm_eo])) )
357 ;
358 else
359 hit = 0;
360
361 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
362 /* There could be more than one match on the
363 * line, and the first match might not be
364 * strict word match. But later ones could be!
fb62eb7f
RS
365 * Forward to the next possible start, i.e. the
366 * next position following a non-word char.
83b5d2f5
JH
367 */
368 bol = pmatch[0].rm_so + bol + 1;
fb62eb7f
RS
369 while (word_char(bol[-1]) && bol < eol)
370 bol++;
371 if (bol < eol)
372 goto again;
83b5d2f5
JH
373 }
374 }
a4d7d2c6
JH
375 if (p->token == GREP_PATTERN_HEAD && saved_ch)
376 *eol = saved_ch;
83b5d2f5
JH
377 return hit;
378}
379
d7eb527d
RS
380static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
381 enum grep_context ctx, int collect_hits)
83b5d2f5 382{
0ab7befa 383 int h = 0;
79212772 384 regmatch_t match;
0ab7befa 385
83b5d2f5
JH
386 switch (x->node) {
387 case GREP_NODE_ATOM:
79212772 388 h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
83b5d2f5
JH
389 break;
390 case GREP_NODE_NOT:
d7eb527d 391 h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
0ab7befa 392 break;
83b5d2f5 393 case GREP_NODE_AND:
d7eb527d 394 if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
252d560d 395 return 0;
d7eb527d 396 h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
0ab7befa 397 break;
83b5d2f5 398 case GREP_NODE_OR:
0ab7befa 399 if (!collect_hits)
d7eb527d 400 return (match_expr_eval(x->u.binary.left,
0ab7befa 401 bol, eol, ctx, 0) ||
d7eb527d 402 match_expr_eval(x->u.binary.right,
0ab7befa 403 bol, eol, ctx, 0));
d7eb527d 404 h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
0ab7befa 405 x->u.binary.left->hit |= h;
d7eb527d 406 h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
0ab7befa
JH
407 break;
408 default:
d7530708 409 die("Unexpected node type (internal error) %d", x->node);
83b5d2f5 410 }
0ab7befa
JH
411 if (collect_hits)
412 x->hit |= h;
413 return h;
83b5d2f5
JH
414}
415
480c1ca6 416static int match_expr(struct grep_opt *opt, char *bol, char *eol,
0ab7befa 417 enum grep_context ctx, int collect_hits)
83b5d2f5
JH
418{
419 struct grep_expr *x = opt->pattern_expression;
d7eb527d 420 return match_expr_eval(x, bol, eol, ctx, collect_hits);
83b5d2f5
JH
421}
422
480c1ca6 423static int match_line(struct grep_opt *opt, char *bol, char *eol,
0ab7befa 424 enum grep_context ctx, int collect_hits)
83b5d2f5
JH
425{
426 struct grep_pat *p;
79212772
RS
427 regmatch_t match;
428
83b5d2f5 429 if (opt->extended)
0ab7befa
JH
430 return match_expr(opt, bol, eol, ctx, collect_hits);
431
432 /* we do not call with collect_hits without being extended */
83b5d2f5 433 for (p = opt->pattern_list; p; p = p->next) {
79212772 434 if (match_one_pattern(p, bol, eol, ctx, &match, 0))
83b5d2f5
JH
435 return 1;
436 }
437 return 0;
438}
439
0ab7befa
JH
440static int grep_buffer_1(struct grep_opt *opt, const char *name,
441 char *buf, unsigned long size, int collect_hits)
83b5d2f5
JH
442{
443 char *bol = buf;
444 unsigned long left = size;
445 unsigned lno = 1;
446 struct pre_context_line {
447 char *bol;
448 char *eol;
449 } *prev = NULL, *pcl;
450 unsigned last_hit = 0;
451 unsigned last_shown = 0;
452 int binary_match_only = 0;
453 const char *hunk_mark = "";
454 unsigned count = 0;
480c1ca6 455 enum grep_context ctx = GREP_CONTEXT_HEAD;
83b5d2f5
JH
456
457 if (buffer_is_binary(buf, size)) {
458 switch (opt->binary) {
459 case GREP_BINARY_DEFAULT:
460 binary_match_only = 1;
461 break;
462 case GREP_BINARY_NOMATCH:
463 return 0; /* Assume unmatch */
464 break;
465 default:
466 break;
467 }
468 }
469
470 if (opt->pre_context)
471 prev = xcalloc(opt->pre_context, sizeof(*prev));
472 if (opt->pre_context || opt->post_context)
473 hunk_mark = "--\n";
474
475 while (left) {
476 char *eol, ch;
0ab7befa 477 int hit;
83b5d2f5
JH
478
479 eol = end_of_line(bol, &left);
480 ch = *eol;
481 *eol = 0;
482
480c1ca6
JH
483 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
484 ctx = GREP_CONTEXT_BODY;
485
0ab7befa 486 hit = match_line(opt, bol, eol, ctx, collect_hits);
83b5d2f5
JH
487 *eol = ch;
488
0ab7befa
JH
489 if (collect_hits)
490 goto next_line;
491
83b5d2f5
JH
492 /* "grep -v -e foo -e bla" should list lines
493 * that do not have either, so inversion should
494 * be done outside.
495 */
496 if (opt->invert)
497 hit = !hit;
498 if (opt->unmatch_name_only) {
499 if (hit)
500 return 0;
501 goto next_line;
502 }
503 if (hit) {
504 count++;
505 if (opt->status_only)
506 return 1;
507 if (binary_match_only) {
508 printf("Binary file %s matches\n", name);
509 return 1;
510 }
511 if (opt->name_only) {
83caecca 512 show_name(opt, name);
83b5d2f5
JH
513 return 1;
514 }
515 /* Hit at this line. If we haven't shown the
516 * pre-context lines, we would need to show them.
517 * When asked to do "count", this still show
518 * the context which is nonsense, but the user
519 * deserves to get that ;-).
520 */
521 if (opt->pre_context) {
522 unsigned from;
523 if (opt->pre_context < lno)
524 from = lno - opt->pre_context;
525 else
526 from = 1;
527 if (from <= last_shown)
528 from = last_shown + 1;
529 if (last_shown && from != last_shown + 1)
9db56f71 530 fputs(hunk_mark, stdout);
83b5d2f5
JH
531 while (from < lno) {
532 pcl = &prev[lno-from-1];
533 show_line(opt, pcl->bol, pcl->eol,
534 name, from, '-');
535 from++;
536 }
537 last_shown = lno-1;
538 }
539 if (last_shown && lno != last_shown + 1)
9db56f71 540 fputs(hunk_mark, stdout);
83b5d2f5
JH
541 if (!opt->count)
542 show_line(opt, bol, eol, name, lno, ':');
543 last_shown = last_hit = lno;
544 }
545 else if (last_hit &&
546 lno <= last_hit + opt->post_context) {
547 /* If the last hit is within the post context,
548 * we need to show this line.
549 */
550 if (last_shown && lno != last_shown + 1)
9db56f71 551 fputs(hunk_mark, stdout);
83b5d2f5
JH
552 show_line(opt, bol, eol, name, lno, '-');
553 last_shown = lno;
554 }
555 if (opt->pre_context) {
556 memmove(prev+1, prev,
557 (opt->pre_context-1) * sizeof(*prev));
558 prev->bol = bol;
559 prev->eol = eol;
560 }
561
562 next_line:
563 bol = eol + 1;
564 if (!left)
565 break;
566 left--;
567 lno++;
568 }
569
b48fb5b6 570 free(prev);
0ab7befa
JH
571 if (collect_hits)
572 return 0;
b48fb5b6 573
83b5d2f5
JH
574 if (opt->status_only)
575 return 0;
576 if (opt->unmatch_name_only) {
577 /* We did not see any hit, so we want to show this */
83caecca 578 show_name(opt, name);
83b5d2f5
JH
579 return 1;
580 }
581
582 /* NEEDSWORK:
583 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
584 * which feels mostly useless but sometimes useful. Maybe
585 * make it another option? For now suppress them.
586 */
587 if (opt->count && count)
83caecca
RZ
588 printf("%s%c%u\n", name,
589 opt->null_following_name ? '\0' : ':', count);
83b5d2f5
JH
590 return !!last_hit;
591}
592
0ab7befa
JH
593static void clr_hit_marker(struct grep_expr *x)
594{
595 /* All-hit markers are meaningful only at the very top level
596 * OR node.
597 */
598 while (1) {
599 x->hit = 0;
600 if (x->node != GREP_NODE_OR)
601 return;
602 x->u.binary.left->hit = 0;
603 x = x->u.binary.right;
604 }
605}
606
607static int chk_hit_marker(struct grep_expr *x)
608{
609 /* Top level nodes have hit markers. See if they all are hits */
610 while (1) {
611 if (x->node != GREP_NODE_OR)
612 return x->hit;
613 if (!x->u.binary.left->hit)
614 return 0;
615 x = x->u.binary.right;
616 }
617}
618
619int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size)
620{
621 /*
622 * we do not have to do the two-pass grep when we do not check
623 * buffer-wide "all-match".
624 */
625 if (!opt->all_match)
626 return grep_buffer_1(opt, name, buf, size, 0);
627
628 /* Otherwise the toplevel "or" terms hit a bit differently.
629 * We first clear hit markers from them.
630 */
631 clr_hit_marker(opt->pattern_expression);
632 grep_buffer_1(opt, name, buf, size, 1);
633
634 if (!chk_hit_marker(opt->pattern_expression))
635 return 0;
636
637 return grep_buffer_1(opt, name, buf, size, 0);
638}