git-tar-tree: Remove duplicate git_config() call
[git/msvc.git] / grep.c
blobcc8d6846a59563b6b902f576262ec97b73f01562
1 #include "cache.h"
2 #include <regex.h>
3 #include "grep.h"
5 void append_grep_pattern(struct grep_opt *opt, const char *pat,
6 const char *origin, int no, enum grep_pat_token t)
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;
18 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
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);
37 static struct grep_expr *compile_pattern_expr(struct grep_pat **);
38 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
40 struct grep_pat *p;
41 struct grep_expr *x;
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;
67 static struct grep_expr *compile_pattern_not(struct grep_pat **list)
69 struct grep_pat *p;
70 struct grep_expr *x;
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);
89 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
91 struct grep_pat *p;
92 struct grep_expr *x, *y, *z;
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;
109 return x;
112 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
114 struct grep_pat *p;
115 struct grep_expr *x, *y, *z;
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;
129 return x;
132 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
134 return compile_pattern_or(list);
137 void compile_grep_patterns(struct grep_opt *opt)
139 struct grep_pat *p;
141 if (opt->fixed)
142 return;
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;
158 if (!opt->extended)
159 return;
161 /* Then bundle them up in an expression.
162 * A classic recursive descent parser would do.
164 p = opt->pattern_list;
165 opt->pattern_expression = compile_pattern_expr(&p);
166 if (p)
167 die("incomplete pattern expression: %s", p->pattern);
170 static char *end_of_line(char *cp, unsigned long *left)
172 unsigned long l = *left;
173 while (l && *cp != '\n') {
174 l--;
175 cp++;
177 *left = l;
178 return cp;
181 static int word_char(char ch)
183 return isalnum(ch) || ch == '_';
186 static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
187 const char *name, unsigned lno, char sign)
189 if (opt->pathname)
190 printf("%s%c", name, sign);
191 if (opt->linenum)
192 printf("%d%c", lno, sign);
193 printf("%.*s\n", (int)(eol-bol), bol);
197 * NEEDSWORK: share code with diff.c
199 #define FIRST_FEW_BYTES 8000
200 static int buffer_is_binary(const char *ptr, unsigned long size)
202 if (FIRST_FEW_BYTES < size)
203 size = FIRST_FEW_BYTES;
204 return !!memchr(ptr, 0, size);
207 static int fixmatch(const char *pattern, char *line, regmatch_t *match)
209 char *hit = strstr(line, pattern);
210 if (!hit) {
211 match->rm_so = match->rm_eo = -1;
212 return REG_NOMATCH;
214 else {
215 match->rm_so = hit - line;
216 match->rm_eo = match->rm_so + strlen(pattern);
217 return 0;
221 static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol, char *eol, enum grep_context ctx)
223 int hit = 0;
224 int at_true_bol = 1;
225 regmatch_t pmatch[10];
227 if ((p->token != GREP_PATTERN) &&
228 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
229 return 0;
231 again:
232 if (!opt->fixed) {
233 regex_t *exp = &p->regexp;
234 hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
235 pmatch, 0);
237 else {
238 hit = !fixmatch(p->pattern, bol, pmatch);
241 if (hit && opt->word_regexp) {
242 if ((pmatch[0].rm_so < 0) ||
243 (eol - bol) <= pmatch[0].rm_so ||
244 (pmatch[0].rm_eo < 0) ||
245 (eol - bol) < pmatch[0].rm_eo)
246 die("regexp returned nonsense");
248 /* Match beginning must be either beginning of the
249 * line, or at word boundary (i.e. the last char must
250 * not be a word char). Similarly, match end must be
251 * either end of the line, or at word boundary
252 * (i.e. the next char must not be a word char).
254 if ( ((pmatch[0].rm_so == 0 && at_true_bol) ||
255 !word_char(bol[pmatch[0].rm_so-1])) &&
256 ((pmatch[0].rm_eo == (eol-bol)) ||
257 !word_char(bol[pmatch[0].rm_eo])) )
259 else
260 hit = 0;
262 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
263 /* There could be more than one match on the
264 * line, and the first match might not be
265 * strict word match. But later ones could be!
267 bol = pmatch[0].rm_so + bol + 1;
268 at_true_bol = 0;
269 goto again;
272 return hit;
275 static int match_expr_eval(struct grep_opt *opt,
276 struct grep_expr *x,
277 char *bol, char *eol,
278 enum grep_context ctx)
280 switch (x->node) {
281 case GREP_NODE_ATOM:
282 return match_one_pattern(opt, x->u.atom, bol, eol, ctx);
283 break;
284 case GREP_NODE_NOT:
285 return !match_expr_eval(opt, x->u.unary, bol, eol, ctx);
286 case GREP_NODE_AND:
287 return (match_expr_eval(opt, x->u.binary.left, bol, eol, ctx) &&
288 match_expr_eval(opt, x->u.binary.right, bol, eol, ctx));
289 case GREP_NODE_OR:
290 return (match_expr_eval(opt, x->u.binary.left, bol, eol, ctx) ||
291 match_expr_eval(opt, x->u.binary.right, bol, eol, ctx));
293 die("Unexpected node type (internal error) %d\n", x->node);
296 static int match_expr(struct grep_opt *opt, char *bol, char *eol,
297 enum grep_context ctx)
299 struct grep_expr *x = opt->pattern_expression;
300 return match_expr_eval(opt, x, bol, eol, ctx);
303 static int match_line(struct grep_opt *opt, char *bol, char *eol,
304 enum grep_context ctx)
306 struct grep_pat *p;
307 if (opt->extended)
308 return match_expr(opt, bol, eol, ctx);
309 for (p = opt->pattern_list; p; p = p->next) {
310 if (match_one_pattern(opt, p, bol, eol, ctx))
311 return 1;
313 return 0;
316 int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size)
318 char *bol = buf;
319 unsigned long left = size;
320 unsigned lno = 1;
321 struct pre_context_line {
322 char *bol;
323 char *eol;
324 } *prev = NULL, *pcl;
325 unsigned last_hit = 0;
326 unsigned last_shown = 0;
327 int binary_match_only = 0;
328 const char *hunk_mark = "";
329 unsigned count = 0;
330 enum grep_context ctx = GREP_CONTEXT_HEAD;
332 if (buffer_is_binary(buf, size)) {
333 switch (opt->binary) {
334 case GREP_BINARY_DEFAULT:
335 binary_match_only = 1;
336 break;
337 case GREP_BINARY_NOMATCH:
338 return 0; /* Assume unmatch */
339 break;
340 default:
341 break;
345 if (opt->pre_context)
346 prev = xcalloc(opt->pre_context, sizeof(*prev));
347 if (opt->pre_context || opt->post_context)
348 hunk_mark = "--\n";
350 while (left) {
351 char *eol, ch;
352 int hit = 0;
354 eol = end_of_line(bol, &left);
355 ch = *eol;
356 *eol = 0;
358 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
359 ctx = GREP_CONTEXT_BODY;
361 hit = match_line(opt, bol, eol, ctx);
362 *eol = ch;
364 /* "grep -v -e foo -e bla" should list lines
365 * that do not have either, so inversion should
366 * be done outside.
368 if (opt->invert)
369 hit = !hit;
370 if (opt->unmatch_name_only) {
371 if (hit)
372 return 0;
373 goto next_line;
375 if (hit) {
376 count++;
377 if (opt->status_only)
378 return 1;
379 if (binary_match_only) {
380 printf("Binary file %s matches\n", name);
381 return 1;
383 if (opt->name_only) {
384 printf("%s\n", name);
385 return 1;
387 /* Hit at this line. If we haven't shown the
388 * pre-context lines, we would need to show them.
389 * When asked to do "count", this still show
390 * the context which is nonsense, but the user
391 * deserves to get that ;-).
393 if (opt->pre_context) {
394 unsigned from;
395 if (opt->pre_context < lno)
396 from = lno - opt->pre_context;
397 else
398 from = 1;
399 if (from <= last_shown)
400 from = last_shown + 1;
401 if (last_shown && from != last_shown + 1)
402 printf(hunk_mark);
403 while (from < lno) {
404 pcl = &prev[lno-from-1];
405 show_line(opt, pcl->bol, pcl->eol,
406 name, from, '-');
407 from++;
409 last_shown = lno-1;
411 if (last_shown && lno != last_shown + 1)
412 printf(hunk_mark);
413 if (!opt->count)
414 show_line(opt, bol, eol, name, lno, ':');
415 last_shown = last_hit = lno;
417 else if (last_hit &&
418 lno <= last_hit + opt->post_context) {
419 /* If the last hit is within the post context,
420 * we need to show this line.
422 if (last_shown && lno != last_shown + 1)
423 printf(hunk_mark);
424 show_line(opt, bol, eol, name, lno, '-');
425 last_shown = lno;
427 if (opt->pre_context) {
428 memmove(prev+1, prev,
429 (opt->pre_context-1) * sizeof(*prev));
430 prev->bol = bol;
431 prev->eol = eol;
434 next_line:
435 bol = eol + 1;
436 if (!left)
437 break;
438 left--;
439 lno++;
442 if (opt->status_only)
443 return 0;
444 if (opt->unmatch_name_only) {
445 /* We did not see any hit, so we want to show this */
446 printf("%s\n", name);
447 return 1;
450 /* NEEDSWORK:
451 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
452 * which feels mostly useless but sometimes useful. Maybe
453 * make it another option? For now suppress them.
455 if (opt->count && count)
456 printf("%s:%u\n", name, count);
457 return !!last_hit;