]> git.ipfire.org Git - thirdparty/git.git/blob - grep.c
grep: free expressions and patterns when done.
[thirdparty/git.git] / grep.c
1 #include "cache.h"
2 #include <regex.h>
3 #include "grep.h"
4
5 void append_grep_pattern(struct grep_opt *opt, const char *pat,
6 const char *origin, int no, enum grep_pat_token t)
7 {
8 struct grep_pat *p = xcalloc(1, sizeof(*p));
9 p->pattern = pat;
10 p->origin = origin;
11 p->no = no;
12 p->token = t;
13 *opt->pattern_tail = p;
14 opt->pattern_tail = &p->next;
15 p->next = NULL;
16 }
17
18 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
19 {
20 int err = regcomp(&p->regexp, p->pattern, opt->regflags);
21 if (err) {
22 char errbuf[1024];
23 char where[1024];
24 if (p->no)
25 sprintf(where, "In '%s' at %d, ",
26 p->origin, p->no);
27 else if (p->origin)
28 sprintf(where, "%s, ", p->origin);
29 else
30 where[0] = 0;
31 regerror(err, &p->regexp, errbuf, 1024);
32 regfree(&p->regexp);
33 die("%s'%s': %s", where, p->pattern, errbuf);
34 }
35 }
36
37 static struct grep_expr *compile_pattern_expr(struct grep_pat **);
38 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
39 {
40 struct grep_pat *p;
41 struct grep_expr *x;
42
43 p = *list;
44 switch (p->token) {
45 case GREP_PATTERN: /* atom */
46 case GREP_PATTERN_HEAD:
47 case GREP_PATTERN_BODY:
48 x = xcalloc(1, sizeof (struct grep_expr));
49 x->node = GREP_NODE_ATOM;
50 x->u.atom = p;
51 *list = p->next;
52 return x;
53 case GREP_OPEN_PAREN:
54 *list = p->next;
55 x = compile_pattern_expr(list);
56 if (!x)
57 return NULL;
58 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
59 die("unmatched parenthesis");
60 *list = (*list)->next;
61 return x;
62 default:
63 return NULL;
64 }
65 }
66
67 static struct grep_expr *compile_pattern_not(struct grep_pat **list)
68 {
69 struct grep_pat *p;
70 struct grep_expr *x;
71
72 p = *list;
73 switch (p->token) {
74 case GREP_NOT:
75 if (!p->next)
76 die("--not not followed by pattern expression");
77 *list = p->next;
78 x = xcalloc(1, sizeof (struct grep_expr));
79 x->node = GREP_NODE_NOT;
80 x->u.unary = compile_pattern_not(list);
81 if (!x->u.unary)
82 die("--not followed by non pattern expression");
83 return x;
84 default:
85 return compile_pattern_atom(list);
86 }
87 }
88
89 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
90 {
91 struct grep_pat *p;
92 struct grep_expr *x, *y, *z;
93
94 x = compile_pattern_not(list);
95 p = *list;
96 if (p && p->token == GREP_AND) {
97 if (!p->next)
98 die("--and not followed by pattern expression");
99 *list = p->next;
100 y = compile_pattern_and(list);
101 if (!y)
102 die("--and not followed by pattern expression");
103 z = xcalloc(1, sizeof (struct grep_expr));
104 z->node = GREP_NODE_AND;
105 z->u.binary.left = x;
106 z->u.binary.right = y;
107 return z;
108 }
109 return x;
110 }
111
112 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
113 {
114 struct grep_pat *p;
115 struct grep_expr *x, *y, *z;
116
117 x = compile_pattern_and(list);
118 p = *list;
119 if (x && p && p->token != GREP_CLOSE_PAREN) {
120 y = compile_pattern_or(list);
121 if (!y)
122 die("not a pattern expression %s", p->pattern);
123 z = xcalloc(1, sizeof (struct grep_expr));
124 z->node = GREP_NODE_OR;
125 z->u.binary.left = x;
126 z->u.binary.right = y;
127 return z;
128 }
129 return x;
130 }
131
132 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
133 {
134 return compile_pattern_or(list);
135 }
136
137 void compile_grep_patterns(struct grep_opt *opt)
138 {
139 struct grep_pat *p;
140
141 if (opt->fixed)
142 return;
143
144 /* First compile regexps */
145 for (p = opt->pattern_list; p; p = p->next) {
146 switch (p->token) {
147 case GREP_PATTERN: /* atom */
148 case GREP_PATTERN_HEAD:
149 case GREP_PATTERN_BODY:
150 compile_regexp(p, opt);
151 break;
152 default:
153 opt->extended = 1;
154 break;
155 }
156 }
157
158 if (!opt->extended)
159 return;
160
161 /* Then bundle them up in an expression.
162 * A classic recursive descent parser would do.
163 */
164 p = opt->pattern_list;
165 opt->pattern_expression = compile_pattern_expr(&p);
166 if (p)
167 die("incomplete pattern expression: %s", p->pattern);
168 }
169
170 static void free_pattern_expr(struct grep_expr *x)
171 {
172 switch (x->node) {
173 case GREP_NODE_ATOM:
174 break;
175 case GREP_NODE_NOT:
176 free_pattern_expr(x->u.unary);
177 break;
178 case GREP_NODE_AND:
179 case GREP_NODE_OR:
180 free_pattern_expr(x->u.binary.left);
181 free_pattern_expr(x->u.binary.right);
182 break;
183 }
184 free(x);
185 }
186
187 void free_grep_patterns(struct grep_opt *opt)
188 {
189 struct grep_pat *p, *n;
190
191 for (p = opt->pattern_list; p; p = n) {
192 n = p->next;
193 switch (p->token) {
194 case GREP_PATTERN: /* atom */
195 case GREP_PATTERN_HEAD:
196 case GREP_PATTERN_BODY:
197 regfree(&p->regexp);
198 break;
199 default:
200 break;
201 }
202 free(p);
203 }
204
205 if (!opt->extended)
206 return;
207 free_pattern_expr(opt->pattern_expression);
208 }
209
210 static char *end_of_line(char *cp, unsigned long *left)
211 {
212 unsigned long l = *left;
213 while (l && *cp != '\n') {
214 l--;
215 cp++;
216 }
217 *left = l;
218 return cp;
219 }
220
221 static int word_char(char ch)
222 {
223 return isalnum(ch) || ch == '_';
224 }
225
226 static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
227 const char *name, unsigned lno, char sign)
228 {
229 if (opt->pathname)
230 printf("%s%c", name, sign);
231 if (opt->linenum)
232 printf("%d%c", lno, sign);
233 printf("%.*s\n", (int)(eol-bol), bol);
234 }
235
236 /*
237 * NEEDSWORK: share code with diff.c
238 */
239 #define FIRST_FEW_BYTES 8000
240 static int buffer_is_binary(const char *ptr, unsigned long size)
241 {
242 if (FIRST_FEW_BYTES < size)
243 size = FIRST_FEW_BYTES;
244 return !!memchr(ptr, 0, size);
245 }
246
247 static int fixmatch(const char *pattern, char *line, regmatch_t *match)
248 {
249 char *hit = strstr(line, pattern);
250 if (!hit) {
251 match->rm_so = match->rm_eo = -1;
252 return REG_NOMATCH;
253 }
254 else {
255 match->rm_so = hit - line;
256 match->rm_eo = match->rm_so + strlen(pattern);
257 return 0;
258 }
259 }
260
261 static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol, char *eol, enum grep_context ctx)
262 {
263 int hit = 0;
264 int at_true_bol = 1;
265 regmatch_t pmatch[10];
266
267 if ((p->token != GREP_PATTERN) &&
268 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
269 return 0;
270
271 again:
272 if (!opt->fixed) {
273 regex_t *exp = &p->regexp;
274 hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
275 pmatch, 0);
276 }
277 else {
278 hit = !fixmatch(p->pattern, bol, pmatch);
279 }
280
281 if (hit && opt->word_regexp) {
282 if ((pmatch[0].rm_so < 0) ||
283 (eol - bol) <= pmatch[0].rm_so ||
284 (pmatch[0].rm_eo < 0) ||
285 (eol - bol) < pmatch[0].rm_eo)
286 die("regexp returned nonsense");
287
288 /* Match beginning must be either beginning of the
289 * line, or at word boundary (i.e. the last char must
290 * not be a word char). Similarly, match end must be
291 * either end of the line, or at word boundary
292 * (i.e. the next char must not be a word char).
293 */
294 if ( ((pmatch[0].rm_so == 0 && at_true_bol) ||
295 !word_char(bol[pmatch[0].rm_so-1])) &&
296 ((pmatch[0].rm_eo == (eol-bol)) ||
297 !word_char(bol[pmatch[0].rm_eo])) )
298 ;
299 else
300 hit = 0;
301
302 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
303 /* There could be more than one match on the
304 * line, and the first match might not be
305 * strict word match. But later ones could be!
306 */
307 bol = pmatch[0].rm_so + bol + 1;
308 at_true_bol = 0;
309 goto again;
310 }
311 }
312 return hit;
313 }
314
315 static int match_expr_eval(struct grep_opt *opt,
316 struct grep_expr *x,
317 char *bol, char *eol,
318 enum grep_context ctx)
319 {
320 switch (x->node) {
321 case GREP_NODE_ATOM:
322 return match_one_pattern(opt, x->u.atom, bol, eol, ctx);
323 break;
324 case GREP_NODE_NOT:
325 return !match_expr_eval(opt, x->u.unary, bol, eol, ctx);
326 case GREP_NODE_AND:
327 return (match_expr_eval(opt, x->u.binary.left, bol, eol, ctx) &&
328 match_expr_eval(opt, x->u.binary.right, bol, eol, ctx));
329 case GREP_NODE_OR:
330 return (match_expr_eval(opt, x->u.binary.left, bol, eol, ctx) ||
331 match_expr_eval(opt, x->u.binary.right, bol, eol, ctx));
332 }
333 die("Unexpected node type (internal error) %d\n", x->node);
334 }
335
336 static int match_expr(struct grep_opt *opt, char *bol, char *eol,
337 enum grep_context ctx)
338 {
339 struct grep_expr *x = opt->pattern_expression;
340 return match_expr_eval(opt, x, bol, eol, ctx);
341 }
342
343 static int match_line(struct grep_opt *opt, char *bol, char *eol,
344 enum grep_context ctx)
345 {
346 struct grep_pat *p;
347 if (opt->extended)
348 return match_expr(opt, bol, eol, ctx);
349 for (p = opt->pattern_list; p; p = p->next) {
350 if (match_one_pattern(opt, p, bol, eol, ctx))
351 return 1;
352 }
353 return 0;
354 }
355
356 int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size)
357 {
358 char *bol = buf;
359 unsigned long left = size;
360 unsigned lno = 1;
361 struct pre_context_line {
362 char *bol;
363 char *eol;
364 } *prev = NULL, *pcl;
365 unsigned last_hit = 0;
366 unsigned last_shown = 0;
367 int binary_match_only = 0;
368 const char *hunk_mark = "";
369 unsigned count = 0;
370 enum grep_context ctx = GREP_CONTEXT_HEAD;
371
372 if (buffer_is_binary(buf, size)) {
373 switch (opt->binary) {
374 case GREP_BINARY_DEFAULT:
375 binary_match_only = 1;
376 break;
377 case GREP_BINARY_NOMATCH:
378 return 0; /* Assume unmatch */
379 break;
380 default:
381 break;
382 }
383 }
384
385 if (opt->pre_context)
386 prev = xcalloc(opt->pre_context, sizeof(*prev));
387 if (opt->pre_context || opt->post_context)
388 hunk_mark = "--\n";
389
390 while (left) {
391 char *eol, ch;
392 int hit = 0;
393
394 eol = end_of_line(bol, &left);
395 ch = *eol;
396 *eol = 0;
397
398 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
399 ctx = GREP_CONTEXT_BODY;
400
401 hit = match_line(opt, bol, eol, ctx);
402 *eol = ch;
403
404 /* "grep -v -e foo -e bla" should list lines
405 * that do not have either, so inversion should
406 * be done outside.
407 */
408 if (opt->invert)
409 hit = !hit;
410 if (opt->unmatch_name_only) {
411 if (hit)
412 return 0;
413 goto next_line;
414 }
415 if (hit) {
416 count++;
417 if (opt->status_only)
418 return 1;
419 if (binary_match_only) {
420 printf("Binary file %s matches\n", name);
421 return 1;
422 }
423 if (opt->name_only) {
424 printf("%s\n", name);
425 return 1;
426 }
427 /* Hit at this line. If we haven't shown the
428 * pre-context lines, we would need to show them.
429 * When asked to do "count", this still show
430 * the context which is nonsense, but the user
431 * deserves to get that ;-).
432 */
433 if (opt->pre_context) {
434 unsigned from;
435 if (opt->pre_context < lno)
436 from = lno - opt->pre_context;
437 else
438 from = 1;
439 if (from <= last_shown)
440 from = last_shown + 1;
441 if (last_shown && from != last_shown + 1)
442 printf(hunk_mark);
443 while (from < lno) {
444 pcl = &prev[lno-from-1];
445 show_line(opt, pcl->bol, pcl->eol,
446 name, from, '-');
447 from++;
448 }
449 last_shown = lno-1;
450 }
451 if (last_shown && lno != last_shown + 1)
452 printf(hunk_mark);
453 if (!opt->count)
454 show_line(opt, bol, eol, name, lno, ':');
455 last_shown = last_hit = lno;
456 }
457 else if (last_hit &&
458 lno <= last_hit + opt->post_context) {
459 /* If the last hit is within the post context,
460 * we need to show this line.
461 */
462 if (last_shown && lno != last_shown + 1)
463 printf(hunk_mark);
464 show_line(opt, bol, eol, name, lno, '-');
465 last_shown = lno;
466 }
467 if (opt->pre_context) {
468 memmove(prev+1, prev,
469 (opt->pre_context-1) * sizeof(*prev));
470 prev->bol = bol;
471 prev->eol = eol;
472 }
473
474 next_line:
475 bol = eol + 1;
476 if (!left)
477 break;
478 left--;
479 lno++;
480 }
481
482 free(prev);
483
484 if (opt->status_only)
485 return 0;
486 if (opt->unmatch_name_only) {
487 /* We did not see any hit, so we want to show this */
488 printf("%s\n", name);
489 return 1;
490 }
491
492 /* NEEDSWORK:
493 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
494 * which feels mostly useless but sometimes useful. Maybe
495 * make it another option? For now suppress them.
496 */
497 if (opt->count && count)
498 printf("%s:%u\n", name, count);
499 return !!last_hit;
500 }
501