@matheusbsbs / matheus-bagliano / commits / 83b5d2f5b0
Fork 0

builtin-grep: make pieces of it available as library.

This makes three functions and associated option structures from builtin-grep available from other parts of the system. * options to drive built-in grep engine is stored in struct grep_opt; * pattern strings and extended grep expressions are added to struct grep_opt with append_grep_pattern(); * when finished calling append_grep_pattern(), call compile_grep_patterns() to prepare for execution; * call grep_buffer() to find matches in the in-core buffer. This also adds an internal option "status_only" to grep_opt, which suppresses any output from grep_buffer(). Callers of the function as library can use it to check if there is a match without producing any output. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Sep 17, 2006 at 16:02 UTC 83b5d2f5b0c95fe102bc3d1cc2947abbdf5e5c5b
4 files changed +531 -506
Makefile
+2 -2
@@ -234,7 +234,7 @@ LIB_FILE=libgit.a
234 XDIFF_LIB=xdiff/lib.a
235
236 LIB_H = \
237 - archive.h blob.h cache.h commit.h csum-file.h delta.h \
237 + archive.h blob.h cache.h commit.h csum-file.h delta.h grep.h \
238 diff.h object.h pack.h pkt-line.h quote.h refs.h list-objects.h sideband.h \
239 run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h \
240 tree-walk.h log-tree.h dir.h path-list.h unpack-trees.h builtin.h
@@ -252,7 +252,7 @@ LIB_OBJS = \
252 server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
253 tag.o tree.o usage.o config.o environment.o ctype.o copy.o \
254 fetch-clone.o revision.o pager.o tree-walk.o xdiff-interface.o \
255 - write_or_die.o trace.o list-objects.o \
255 + write_or_die.o trace.o list-objects.o grep.o \
256 alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
257 color.o wt-status.o
258
builtin-grep.c
+18 -504
@@ -11,6 +11,7 @@
11 #include "tree-walk.h"
12 #include "builtin.h"
13 #include <regex.h>
14 +#include "grep.h"
15 #include <fnmatch.h>
16 #include <sys/wait.h>
17
@@ -82,498 +83,6 @@ static int pathspec_matches(const char **paths, const char *name)
83 return 0;
84 }
85
85 -enum grep_pat_token {
86 - GREP_PATTERN,
87 - GREP_AND,
88 - GREP_OPEN_PAREN,
89 - GREP_CLOSE_PAREN,
90 - GREP_NOT,
91 - GREP_OR,
92 -};
93 -
94 -struct grep_pat {
95 - struct grep_pat *next;
96 - const char *origin;
97 - int no;
98 - enum grep_pat_token token;
99 - const char *pattern;
100 - regex_t regexp;
101 -};
102 -
103 -enum grep_expr_node {
104 - GREP_NODE_ATOM,
105 - GREP_NODE_NOT,
106 - GREP_NODE_AND,
107 - GREP_NODE_OR,
108 -};
109 -
110 -struct grep_expr {
111 - enum grep_expr_node node;
112 - union {
113 - struct grep_pat *atom;
114 - struct grep_expr *unary;
115 - struct {
116 - struct grep_expr *left;
117 - struct grep_expr *right;
118 - } binary;
119 - } u;
120 -};
121 -
122 -struct grep_opt {
123 - struct grep_pat *pattern_list;
124 - struct grep_pat **pattern_tail;
125 - struct grep_expr *pattern_expression;
126 - int prefix_length;
127 - regex_t regexp;
128 - unsigned linenum:1;
129 - unsigned invert:1;
130 - unsigned name_only:1;
131 - unsigned unmatch_name_only:1;
132 - unsigned count:1;
133 - unsigned word_regexp:1;
134 - unsigned fixed:1;
135 -#define GREP_BINARY_DEFAULT 0
136 -#define GREP_BINARY_NOMATCH 1
137 -#define GREP_BINARY_TEXT 2
138 - unsigned binary:2;
139 - unsigned extended:1;
140 - unsigned relative:1;
141 - unsigned pathname:1;
142 - int regflags;
143 - unsigned pre_context;
144 - unsigned post_context;
145 -};
146 -
147 -static void add_pattern(struct grep_opt *opt, const char *pat,
148 - const char *origin, int no, enum grep_pat_token t)
149 -{
150 - struct grep_pat *p = xcalloc(1, sizeof(*p));
151 - p->pattern = pat;
152 - p->origin = origin;
153 - p->no = no;
154 - p->token = t;
155 - *opt->pattern_tail = p;
156 - opt->pattern_tail = &p->next;
157 - p->next = NULL;
158 -}
159 -
160 -static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
161 -{
162 - int err = regcomp(&p->regexp, p->pattern, opt->regflags);
163 - if (err) {
164 - char errbuf[1024];
165 - char where[1024];
166 - if (p->no)
167 - sprintf(where, "In '%s' at %d, ",
168 - p->origin, p->no);
169 - else if (p->origin)
170 - sprintf(where, "%s, ", p->origin);
171 - else
172 - where[0] = 0;
173 - regerror(err, &p->regexp, errbuf, 1024);
174 - regfree(&p->regexp);
175 - die("%s'%s': %s", where, p->pattern, errbuf);
176 - }
177 -}
178 -
179 -static struct grep_expr *compile_pattern_expr(struct grep_pat **);
180 -static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
181 -{
182 - struct grep_pat *p;
183 - struct grep_expr *x;
184 -
185 - p = *list;
186 - switch (p->token) {
187 - case GREP_PATTERN: /* atom */
188 - x = xcalloc(1, sizeof (struct grep_expr));
189 - x->node = GREP_NODE_ATOM;
190 - x->u.atom = p;
191 - *list = p->next;
192 - return x;
193 - case GREP_OPEN_PAREN:
194 - *list = p->next;
195 - x = compile_pattern_expr(list);
196 - if (!x)
197 - return NULL;
198 - if (!*list || (*list)->token != GREP_CLOSE_PAREN)
199 - die("unmatched parenthesis");
200 - *list = (*list)->next;
201 - return x;
202 - default:
203 - return NULL;
204 - }
205 -}
206 -
207 -static struct grep_expr *compile_pattern_not(struct grep_pat **list)
208 -{
209 - struct grep_pat *p;
210 - struct grep_expr *x;
211 -
212 - p = *list;
213 - switch (p->token) {
214 - case GREP_NOT:
215 - if (!p->next)
216 - die("--not not followed by pattern expression");
217 - *list = p->next;
218 - x = xcalloc(1, sizeof (struct grep_expr));
219 - x->node = GREP_NODE_NOT;
220 - x->u.unary = compile_pattern_not(list);
221 - if (!x->u.unary)
222 - die("--not followed by non pattern expression");
223 - return x;
224 - default:
225 - return compile_pattern_atom(list);
226 - }
227 -}
228 -
229 -static struct grep_expr *compile_pattern_and(struct grep_pat **list)
230 -{
231 - struct grep_pat *p;
232 - struct grep_expr *x, *y, *z;
233 -
234 - x = compile_pattern_not(list);
235 - p = *list;
236 - if (p && p->token == GREP_AND) {
237 - if (!p->next)
238 - die("--and not followed by pattern expression");
239 - *list = p->next;
240 - y = compile_pattern_and(list);
241 - if (!y)
242 - die("--and not followed by pattern expression");
243 - z = xcalloc(1, sizeof (struct grep_expr));
244 - z->node = GREP_NODE_AND;
245 - z->u.binary.left = x;
246 - z->u.binary.right = y;
247 - return z;
248 - }
249 - return x;
250 -}
251 -
252 -static struct grep_expr *compile_pattern_or(struct grep_pat **list)
253 -{
254 - struct grep_pat *p;
255 - struct grep_expr *x, *y, *z;
256 -
257 - x = compile_pattern_and(list);
258 - p = *list;
259 - if (x && p && p->token != GREP_CLOSE_PAREN) {
260 - y = compile_pattern_or(list);
261 - if (!y)
262 - die("not a pattern expression %s", p->pattern);
263 - z = xcalloc(1, sizeof (struct grep_expr));
264 - z->node = GREP_NODE_OR;
265 - z->u.binary.left = x;
266 - z->u.binary.right = y;
267 - return z;
268 - }
269 - return x;
270 -}
271 -
272 -static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
273 -{
274 - return compile_pattern_or(list);
275 -}
276 -
277 -static void compile_patterns(struct grep_opt *opt)
278 -{
279 - struct grep_pat *p;
280 -
281 - /* First compile regexps */
282 - for (p = opt->pattern_list; p; p = p->next) {
283 - if (p->token == GREP_PATTERN)
284 - compile_regexp(p, opt);
285 - else
286 - opt->extended = 1;
287 - }
288 -
289 - if (!opt->extended)
290 - return;
291 -
292 - /* Then bundle them up in an expression.
293 - * A classic recursive descent parser would do.
294 - */
295 - p = opt->pattern_list;
296 - opt->pattern_expression = compile_pattern_expr(&p);
297 - if (p)
298 - die("incomplete pattern expression: %s", p->pattern);
299 -}
300 -
301 -static char *end_of_line(char *cp, unsigned long *left)
302 -{
303 - unsigned long l = *left;
304 - while (l && *cp != '\n') {
305 - l--;
306 - cp++;
307 - }
308 - *left = l;
309 - return cp;
310 -}
311 -
312 -static int word_char(char ch)
313 -{
314 - return isalnum(ch) || ch == '_';
315 -}
316 -
317 -static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
318 - const char *name, unsigned lno, char sign)
319 -{
320 - if (opt->pathname)
321 - printf("%s%c", name, sign);
322 - if (opt->linenum)
323 - printf("%d%c", lno, sign);
324 - printf("%.*s\n", (int)(eol-bol), bol);
325 -}
326 -
327 -/*
328 - * NEEDSWORK: share code with diff.c
329 - */
330 -#define FIRST_FEW_BYTES 8000
331 -static int buffer_is_binary(const char *ptr, unsigned long size)
332 -{
333 - if (FIRST_FEW_BYTES < size)
334 - size = FIRST_FEW_BYTES;
335 - return !!memchr(ptr, 0, size);
336 -}
337 -
338 -static int fixmatch(const char *pattern, char *line, regmatch_t *match)
339 -{
340 - char *hit = strstr(line, pattern);
341 - if (!hit) {
342 - match->rm_so = match->rm_eo = -1;
343 - return REG_NOMATCH;
344 - }
345 - else {
346 - match->rm_so = hit - line;
347 - match->rm_eo = match->rm_so + strlen(pattern);
348 - return 0;
349 - }
350 -}
351 -
352 -static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol, char *eol)
353 -{
354 - int hit = 0;
355 - int at_true_bol = 1;
356 - regmatch_t pmatch[10];
357 -
358 - again:
359 - if (!opt->fixed) {
360 - regex_t *exp = &p->regexp;
361 - hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
362 - pmatch, 0);
363 - }
364 - else {
365 - hit = !fixmatch(p->pattern, bol, pmatch);
366 - }
367 -
368 - if (hit && opt->word_regexp) {
369 - if ((pmatch[0].rm_so < 0) ||
370 - (eol - bol) <= pmatch[0].rm_so ||
371 - (pmatch[0].rm_eo < 0) ||
372 - (eol - bol) < pmatch[0].rm_eo)
373 - die("regexp returned nonsense");
374 -
375 - /* Match beginning must be either beginning of the
376 - * line, or at word boundary (i.e. the last char must
377 - * not be a word char). Similarly, match end must be
378 - * either end of the line, or at word boundary
379 - * (i.e. the next char must not be a word char).
380 - */
381 - if ( ((pmatch[0].rm_so == 0 && at_true_bol) ||
382 - !word_char(bol[pmatch[0].rm_so-1])) &&
383 - ((pmatch[0].rm_eo == (eol-bol)) ||
384 - !word_char(bol[pmatch[0].rm_eo])) )
385 - ;
386 - else
387 - hit = 0;
388 -
389 - if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
390 - /* There could be more than one match on the
391 - * line, and the first match might not be
392 - * strict word match. But later ones could be!
393 - */
394 - bol = pmatch[0].rm_so + bol + 1;
395 - at_true_bol = 0;
396 - goto again;
397 - }
398 - }
399 - return hit;
400 -}
401 -
402 -static int match_expr_eval(struct grep_opt *opt,
403 - struct grep_expr *x,
404 - char *bol, char *eol)
405 -{
406 - switch (x->node) {
407 - case GREP_NODE_ATOM:
408 - return match_one_pattern(opt, x->u.atom, bol, eol);
409 - break;
410 - case GREP_NODE_NOT:
411 - return !match_expr_eval(opt, x->u.unary, bol, eol);
412 - case GREP_NODE_AND:
413 - return (match_expr_eval(opt, x->u.binary.left, bol, eol) &&
414 - match_expr_eval(opt, x->u.binary.right, bol, eol));
415 - case GREP_NODE_OR:
416 - return (match_expr_eval(opt, x->u.binary.left, bol, eol) ||
417 - match_expr_eval(opt, x->u.binary.right, bol, eol));
418 - }
419 - die("Unexpected node type (internal error) %d\n", x->node);
420 -}
421 -
422 -static int match_expr(struct grep_opt *opt, char *bol, char *eol)
423 -{
424 - struct grep_expr *x = opt->pattern_expression;
425 - return match_expr_eval(opt, x, bol, eol);
426 -}
427 -
428 -static int match_line(struct grep_opt *opt, char *bol, char *eol)
429 -{
430 - struct grep_pat *p;
431 - if (opt->extended)
432 - return match_expr(opt, bol, eol);
433 - for (p = opt->pattern_list; p; p = p->next) {
434 - if (match_one_pattern(opt, p, bol, eol))
435 - return 1;
436 - }
437 - return 0;
438 -}
439 -
440 -static int grep_buffer(struct grep_opt *opt, const char *name,
441 - char *buf, unsigned long size)
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;
455 -
456 - if (buffer_is_binary(buf, size)) {
457 - switch (opt->binary) {
458 - case GREP_BINARY_DEFAULT:
459 - binary_match_only = 1;
460 - break;
461 - case GREP_BINARY_NOMATCH:
462 - return 0; /* Assume unmatch */
463 - break;
464 - default:
465 - break;
466 - }
467 - }
468 -
469 - if (opt->pre_context)
470 - prev = xcalloc(opt->pre_context, sizeof(*prev));
471 - if (opt->pre_context || opt->post_context)
472 - hunk_mark = "--\n";
473 -
474 - while (left) {
475 - char *eol, ch;
476 - int hit = 0;
477 -
478 - eol = end_of_line(bol, &left);
479 - ch = *eol;
480 - *eol = 0;
481 -
482 - hit = match_line(opt, bol, eol);
483 -
484 - /* "grep -v -e foo -e bla" should list lines
485 - * that do not have either, so inversion should
486 - * be done outside.
487 - */
488 - if (opt->invert)
489 - hit = !hit;
490 - if (opt->unmatch_name_only) {
491 - if (hit)
492 - return 0;
493 - goto next_line;
494 - }
495 - if (hit) {
496 - count++;
497 - if (binary_match_only) {
498 - printf("Binary file %s matches\n", name);
499 - return 1;
500 - }
501 - if (opt->name_only) {
502 - printf("%s\n", name);
503 - return 1;
504 - }
505 - /* Hit at this line. If we haven't shown the
506 - * pre-context lines, we would need to show them.
507 - * When asked to do "count", this still show
508 - * the context which is nonsense, but the user
509 - * deserves to get that ;-).
510 - */
511 - if (opt->pre_context) {
512 - unsigned from;
513 - if (opt->pre_context < lno)
514 - from = lno - opt->pre_context;
515 - else
516 - from = 1;
517 - if (from <= last_shown)
518 - from = last_shown + 1;
519 - if (last_shown && from != last_shown + 1)
520 - printf(hunk_mark);
521 - while (from < lno) {
522 - pcl = &prev[lno-from-1];
523 - show_line(opt, pcl->bol, pcl->eol,
524 - name, from, '-');
525 - from++;
526 - }
527 - last_shown = lno-1;
528 - }
529 - if (last_shown && lno != last_shown + 1)
530 - printf(hunk_mark);
531 - if (!opt->count)
532 - show_line(opt, bol, eol, name, lno, ':');
533 - last_shown = last_hit = lno;
534 - }
535 - else if (last_hit &&
536 - lno <= last_hit + opt->post_context) {
537 - /* If the last hit is within the post context,
538 - * we need to show this line.
539 - */
540 - if (last_shown && lno != last_shown + 1)
541 - printf(hunk_mark);
542 - show_line(opt, bol, eol, name, lno, '-');
543 - last_shown = lno;
544 - }
545 - if (opt->pre_context) {
546 - memmove(prev+1, prev,
547 - (opt->pre_context-1) * sizeof(*prev));
548 - prev->bol = bol;
549 - prev->eol = eol;
550 - }
551 -
552 - next_line:
553 - *eol = ch;
554 - bol = eol + 1;
555 - if (!left)
556 - break;
557 - left--;
558 - lno++;
559 - }
560 -
561 - if (opt->unmatch_name_only) {
562 - /* We did not see any hit, so we want to show this */
563 - printf("%s\n", name);
564 - return 1;
565 - }
566 -
567 - /* NEEDSWORK:
568 - * The real "grep -c foo *.c" gives many "bar.c:0" lines,
569 - * which feels mostly useless but sometimes useful. Maybe
570 - * make it another option? For now suppress them.
571 - */
572 - if (opt->count && count)
573 - printf("%s:%u\n", name, count);
574 - return !!last_hit;
575 -}
576 -
86 static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name, int tree_name_len)
87 {
88 unsigned long size;
@@ -1055,8 +564,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
564 /* ignore empty line like grep does */
565 if (!buf[0])
566 continue;
1058 - add_pattern(&opt, xstrdup(buf), argv[1], ++lno,
1059 - GREP_PATTERN);
567 + append_grep_pattern(&opt, xstrdup(buf),
568 + argv[1], ++lno,
569 + GREP_PATTERN);
570 }
571 fclose(patterns);
572 argv++;
@@ -1064,27 +574,32 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
574 continue;
575 }
576 if (!strcmp("--not", arg)) {
1067 - add_pattern(&opt, arg, "command line", 0, GREP_NOT);
577 + append_grep_pattern(&opt, arg, "command line", 0,
578 + GREP_NOT);
579 continue;
580 }
581 if (!strcmp("--and", arg)) {
1071 - add_pattern(&opt, arg, "command line", 0, GREP_AND);
582 + append_grep_pattern(&opt, arg, "command line", 0,
583 + GREP_AND);
584 continue;
585 }
586 if (!strcmp("--or", arg))
587 continue; /* no-op */
588 if (!strcmp("(", arg)) {
1077 - add_pattern(&opt, arg, "command line", 0, GREP_OPEN_PAREN);
589 + append_grep_pattern(&opt, arg, "command line", 0,
590 + GREP_OPEN_PAREN);
591 continue;
592 }
593 if (!strcmp(")", arg)) {
1081 - add_pattern(&opt, arg, "command line", 0, GREP_CLOSE_PAREN);
594 + append_grep_pattern(&opt, arg, "command line", 0,
595 + GREP_CLOSE_PAREN);
596 continue;
597 }
598 if (!strcmp("-e", arg)) {
599 if (1 < argc) {
1086 - add_pattern(&opt, argv[1], "-e option", 0,
1087 - GREP_PATTERN);
600 + append_grep_pattern(&opt, argv[1],
601 + "-e option", 0,
602 + GREP_PATTERN);
603 argv++;
604 argc--;
605 continue;
@@ -1106,8 +621,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
621
622 /* First unrecognized non-option token */
623 if (!opt.pattern_list) {
1109 - add_pattern(&opt, arg, "command line", 0,
1110 - GREP_PATTERN);
624 + append_grep_pattern(&opt, arg, "command line", 0,
625 + GREP_PATTERN);
626 break;
627 }
628 else {
@@ -1124,8 +639,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
639 die("no pattern given.");
640 if ((opt.regflags != REG_NEWLINE) && opt.fixed)
641 die("cannot mix --fixed-strings and regexp");
1127 - if (!opt.fixed)
1128 - compile_patterns(&opt);
642 + compile_grep_patterns(&opt);
643
644 /* Check revs and then paths */
645 for (i = 1; i < argc; i++) {
grep.c new
+440
@@ -0,0 +1,440 @@
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 + x = xcalloc(1, sizeof (struct grep_expr));
47 + x->node = GREP_NODE_ATOM;
48 + x->u.atom = p;
49 + *list = p->next;
50 + return x;
51 + case GREP_OPEN_PAREN:
52 + *list = p->next;
53 + x = compile_pattern_expr(list);
54 + if (!x)
55 + return NULL;
56 + if (!*list || (*list)->token != GREP_CLOSE_PAREN)
57 + die("unmatched parenthesis");
58 + *list = (*list)->next;
59 + return x;
60 + default:
61 + return NULL;
62 + }
63 +}
64 +
65 +static struct grep_expr *compile_pattern_not(struct grep_pat **list)
66 +{
67 + struct grep_pat *p;
68 + struct grep_expr *x;
69 +
70 + p = *list;
71 + switch (p->token) {
72 + case GREP_NOT:
73 + if (!p->next)
74 + die("--not not followed by pattern expression");
75 + *list = p->next;
76 + x = xcalloc(1, sizeof (struct grep_expr));
77 + x->node = GREP_NODE_NOT;
78 + x->u.unary = compile_pattern_not(list);
79 + if (!x->u.unary)
80 + die("--not followed by non pattern expression");
81 + return x;
82 + default:
83 + return compile_pattern_atom(list);
84 + }
85 +}
86 +
87 +static struct grep_expr *compile_pattern_and(struct grep_pat **list)
88 +{
89 + struct grep_pat *p;
90 + struct grep_expr *x, *y, *z;
91 +
92 + x = compile_pattern_not(list);
93 + p = *list;
94 + if (p && p->token == GREP_AND) {
95 + if (!p->next)
96 + die("--and not followed by pattern expression");
97 + *list = p->next;
98 + y = compile_pattern_and(list);
99 + if (!y)
100 + die("--and not followed by pattern expression");
101 + z = xcalloc(1, sizeof (struct grep_expr));
102 + z->node = GREP_NODE_AND;
103 + z->u.binary.left = x;
104 + z->u.binary.right = y;
105 + return z;
106 + }
107 + return x;
108 +}
109 +
110 +static struct grep_expr *compile_pattern_or(struct grep_pat **list)
111 +{
112 + struct grep_pat *p;
113 + struct grep_expr *x, *y, *z;
114 +
115 + x = compile_pattern_and(list);
116 + p = *list;
117 + if (x && p && p->token != GREP_CLOSE_PAREN) {
118 + y = compile_pattern_or(list);
119 + if (!y)
120 + die("not a pattern expression %s", p->pattern);
121 + z = xcalloc(1, sizeof (struct grep_expr));
122 + z->node = GREP_NODE_OR;
123 + z->u.binary.left = x;
124 + z->u.binary.right = y;
125 + return z;
126 + }
127 + return x;
128 +}
129 +
130 +static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
131 +{
132 + return compile_pattern_or(list);
133 +}
134 +
135 +void compile_grep_patterns(struct grep_opt *opt)
136 +{
137 + struct grep_pat *p;
138 +
139 + if (opt->fixed)
140 + return;
141 +
142 + /* First compile regexps */
143 + for (p = opt->pattern_list; p; p = p->next) {
144 + if (p->token == GREP_PATTERN)
145 + compile_regexp(p, opt);
146 + else
147 + opt->extended = 1;
148 + }
149 +
150 + if (!opt->extended)
151 + return;
152 +
153 + /* Then bundle them up in an expression.
154 + * A classic recursive descent parser would do.
155 + */
156 + p = opt->pattern_list;
157 + opt->pattern_expression = compile_pattern_expr(&p);
158 + if (p)
159 + die("incomplete pattern expression: %s", p->pattern);
160 +}
161 +
162 +static char *end_of_line(char *cp, unsigned long *left)
163 +{
164 + unsigned long l = *left;
165 + while (l && *cp != '\n') {
166 + l--;
167 + cp++;
168 + }
169 + *left = l;
170 + return cp;
171 +}
172 +
173 +static int word_char(char ch)
174 +{
175 + return isalnum(ch) || ch == '_';
176 +}
177 +
178 +static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
179 + const char *name, unsigned lno, char sign)
180 +{
181 + if (opt->pathname)
182 + printf("%s%c", name, sign);
183 + if (opt->linenum)
184 + printf("%d%c", lno, sign);
185 + printf("%.*s\n", (int)(eol-bol), bol);
186 +}
187 +
188 +/*
189 + * NEEDSWORK: share code with diff.c
190 + */
191 +#define FIRST_FEW_BYTES 8000
192 +static int buffer_is_binary(const char *ptr, unsigned long size)
193 +{
194 + if (FIRST_FEW_BYTES < size)
195 + size = FIRST_FEW_BYTES;
196 + return !!memchr(ptr, 0, size);
197 +}
198 +
199 +static int fixmatch(const char *pattern, char *line, regmatch_t *match)
200 +{
201 + char *hit = strstr(line, pattern);
202 + if (!hit) {
203 + match->rm_so = match->rm_eo = -1;
204 + return REG_NOMATCH;
205 + }
206 + else {
207 + match->rm_so = hit - line;
208 + match->rm_eo = match->rm_so + strlen(pattern);
209 + return 0;
210 + }
211 +}
212 +
213 +static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol, char *eol)
214 +{
215 + int hit = 0;
216 + int at_true_bol = 1;
217 + regmatch_t pmatch[10];
218 +
219 + again:
220 + if (!opt->fixed) {
221 + regex_t *exp = &p->regexp;
222 + hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
223 + pmatch, 0);
224 + }
225 + else {
226 + hit = !fixmatch(p->pattern, bol, pmatch);
227 + }
228 +
229 + if (hit && opt->word_regexp) {
230 + if ((pmatch[0].rm_so < 0) ||
231 + (eol - bol) <= pmatch[0].rm_so ||
232 + (pmatch[0].rm_eo < 0) ||
233 + (eol - bol) < pmatch[0].rm_eo)
234 + die("regexp returned nonsense");
235 +
236 + /* Match beginning must be either beginning of the
237 + * line, or at word boundary (i.e. the last char must
238 + * not be a word char). Similarly, match end must be
239 + * either end of the line, or at word boundary
240 + * (i.e. the next char must not be a word char).
241 + */
242 + if ( ((pmatch[0].rm_so == 0 && at_true_bol) ||
243 + !word_char(bol[pmatch[0].rm_so-1])) &&
244 + ((pmatch[0].rm_eo == (eol-bol)) ||
245 + !word_char(bol[pmatch[0].rm_eo])) )
246 + ;
247 + else
248 + hit = 0;
249 +
250 + if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
251 + /* There could be more than one match on the
252 + * line, and the first match might not be
253 + * strict word match. But later ones could be!
254 + */
255 + bol = pmatch[0].rm_so + bol + 1;
256 + at_true_bol = 0;
257 + goto again;
258 + }
259 + }
260 + return hit;
261 +}
262 +
263 +static int match_expr_eval(struct grep_opt *opt,
264 + struct grep_expr *x,
265 + char *bol, char *eol)
266 +{
267 + switch (x->node) {
268 + case GREP_NODE_ATOM:
269 + return match_one_pattern(opt, x->u.atom, bol, eol);
270 + break;
271 + case GREP_NODE_NOT:
272 + return !match_expr_eval(opt, x->u.unary, bol, eol);
273 + case GREP_NODE_AND:
274 + return (match_expr_eval(opt, x->u.binary.left, bol, eol) &&
275 + match_expr_eval(opt, x->u.binary.right, bol, eol));
276 + case GREP_NODE_OR:
277 + return (match_expr_eval(opt, x->u.binary.left, bol, eol) ||
278 + match_expr_eval(opt, x->u.binary.right, bol, eol));
279 + }
280 + die("Unexpected node type (internal error) %d\n", x->node);
281 +}
282 +
283 +static int match_expr(struct grep_opt *opt, char *bol, char *eol)
284 +{
285 + struct grep_expr *x = opt->pattern_expression;
286 + return match_expr_eval(opt, x, bol, eol);
287 +}
288 +
289 +static int match_line(struct grep_opt *opt, char *bol, char *eol)
290 +{
291 + struct grep_pat *p;
292 + if (opt->extended)
293 + return match_expr(opt, bol, eol);
294 + for (p = opt->pattern_list; p; p = p->next) {
295 + if (match_one_pattern(opt, p, bol, eol))
296 + return 1;
297 + }
298 + return 0;
299 +}
300 +
301 +int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size)
302 +{
303 + char *bol = buf;
304 + unsigned long left = size;
305 + unsigned lno = 1;
306 + struct pre_context_line {
307 + char *bol;
308 + char *eol;
309 + } *prev = NULL, *pcl;
310 + unsigned last_hit = 0;
311 + unsigned last_shown = 0;
312 + int binary_match_only = 0;
313 + const char *hunk_mark = "";
314 + unsigned count = 0;
315 +
316 + if (buffer_is_binary(buf, size)) {
317 + switch (opt->binary) {
318 + case GREP_BINARY_DEFAULT:
319 + binary_match_only = 1;
320 + break;
321 + case GREP_BINARY_NOMATCH:
322 + return 0; /* Assume unmatch */
323 + break;
324 + default:
325 + break;
326 + }
327 + }
328 +
329 + if (opt->pre_context)
330 + prev = xcalloc(opt->pre_context, sizeof(*prev));
331 + if (opt->pre_context || opt->post_context)
332 + hunk_mark = "--\n";
333 +
334 + while (left) {
335 + char *eol, ch;
336 + int hit = 0;
337 +
338 + eol = end_of_line(bol, &left);
339 + ch = *eol;
340 + *eol = 0;
341 +
342 + hit = match_line(opt, bol, eol);
343 + *eol = ch;
344 +
345 + /* "grep -v -e foo -e bla" should list lines
346 + * that do not have either, so inversion should
347 + * be done outside.
348 + */
349 + if (opt->invert)
350 + hit = !hit;
351 + if (opt->unmatch_name_only) {
352 + if (hit)
353 + return 0;
354 + goto next_line;
355 + }
356 + if (hit) {
357 + count++;
358 + if (opt->status_only)
359 + return 1;
360 + if (binary_match_only) {
361 + printf("Binary file %s matches\n", name);
362 + return 1;
363 + }
364 + if (opt->name_only) {
365 + printf("%s\n", name);
366 + return 1;
367 + }
368 + /* Hit at this line. If we haven't shown the
369 + * pre-context lines, we would need to show them.
370 + * When asked to do "count", this still show
371 + * the context which is nonsense, but the user
372 + * deserves to get that ;-).
373 + */
374 + if (opt->pre_context) {
375 + unsigned from;
376 + if (opt->pre_context < lno)
377 + from = lno - opt->pre_context;
378 + else
379 + from = 1;
380 + if (from <= last_shown)
381 + from = last_shown + 1;
382 + if (last_shown && from != last_shown + 1)
383 + printf(hunk_mark);
384 + while (from < lno) {
385 + pcl = &prev[lno-from-1];
386 + show_line(opt, pcl->bol, pcl->eol,
387 + name, from, '-');
388 + from++;
389 + }
390 + last_shown = lno-1;
391 + }
392 + if (last_shown && lno != last_shown + 1)
393 + printf(hunk_mark);
394 + if (!opt->count)
395 + show_line(opt, bol, eol, name, lno, ':');
396 + last_shown = last_hit = lno;
397 + }
398 + else if (last_hit &&
399 + lno <= last_hit + opt->post_context) {
400 + /* If the last hit is within the post context,
401 + * we need to show this line.
402 + */
403 + if (last_shown && lno != last_shown + 1)
404 + printf(hunk_mark);
405 + show_line(opt, bol, eol, name, lno, '-');
406 + last_shown = lno;
407 + }
408 + if (opt->pre_context) {
409 + memmove(prev+1, prev,
410 + (opt->pre_context-1) * sizeof(*prev));
411 + prev->bol = bol;
412 + prev->eol = eol;
413 + }
414 +
415 + next_line:
416 + bol = eol + 1;
417 + if (!left)
418 + break;
419 + left--;
420 + lno++;
421 + }
422 +
423 + if (opt->status_only)
424 + return 0;
425 + if (opt->unmatch_name_only) {
426 + /* We did not see any hit, so we want to show this */
427 + printf("%s\n", name);
428 + return 1;
429 + }
430 +
431 + /* NEEDSWORK:
432 + * The real "grep -c foo *.c" gives many "bar.c:0" lines,
433 + * which feels mostly useless but sometimes useful. Maybe
434 + * make it another option? For now suppress them.
435 + */
436 + if (opt->count && count)
437 + printf("%s:%u\n", name, count);
438 + return !!last_hit;
439 +}
440 +
grep.h new
+71
@@ -0,0 +1,71 @@
1 +#ifndef GREP_H
2 +#define GREP_H
3 +
4 +enum grep_pat_token {
5 + GREP_PATTERN,
6 + GREP_AND,
7 + GREP_OPEN_PAREN,
8 + GREP_CLOSE_PAREN,
9 + GREP_NOT,
10 + GREP_OR,
11 +};
12 +
13 +struct grep_pat {
14 + struct grep_pat *next;
15 + const char *origin;
16 + int no;
17 + enum grep_pat_token token;
18 + const char *pattern;
19 + regex_t regexp;
20 +};
21 +
22 +enum grep_expr_node {
23 + GREP_NODE_ATOM,
24 + GREP_NODE_NOT,
25 + GREP_NODE_AND,
26 + GREP_NODE_OR,
27 +};
28 +
29 +struct grep_expr {
30 + enum grep_expr_node node;
31 + union {
32 + struct grep_pat *atom;
33 + struct grep_expr *unary;
34 + struct {
35 + struct grep_expr *left;
36 + struct grep_expr *right;
37 + } binary;
38 + } u;
39 +};
40 +
41 +struct grep_opt {
42 + struct grep_pat *pattern_list;
43 + struct grep_pat **pattern_tail;
44 + struct grep_expr *pattern_expression;
45 + int prefix_length;
46 + regex_t regexp;
47 + unsigned linenum:1;
48 + unsigned invert:1;
49 + unsigned status_only:1;
50 + unsigned name_only:1;
51 + unsigned unmatch_name_only:1;
52 + unsigned count:1;
53 + unsigned word_regexp:1;
54 + unsigned fixed:1;
55 +#define GREP_BINARY_DEFAULT 0
56 +#define GREP_BINARY_NOMATCH 1
57 +#define GREP_BINARY_TEXT 2
58 + unsigned binary:2;
59 + unsigned extended:1;
60 + unsigned relative:1;
61 + unsigned pathname:1;
62 + int regflags;
63 + unsigned pre_context;
64 + unsigned post_context;
65 +};
66 +
67 +extern void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
68 +extern void compile_grep_patterns(struct grep_opt *opt);
69 +extern int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size);
70 +
71 +#endif