Update grep internal for grepping only in head/body

This further updates the built-in grep engine so that we can say
something like "this pattern should match only in head".  This
can be used to simplify grepping in the log messages.

Signed-off-by: Junio C Hamano <junkio@cox.net>
This commit is contained in:
Junio C Hamano 2006-09-20 12:39:46 -07:00
parent a2ed6ae402
commit 480c1ca6fd
2 changed files with 42 additions and 16 deletions

51
grep.c
View File

@ -43,6 +43,8 @@ static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
p = *list; p = *list;
switch (p->token) { switch (p->token) {
case GREP_PATTERN: /* atom */ case GREP_PATTERN: /* atom */
case GREP_PATTERN_HEAD:
case GREP_PATTERN_BODY:
x = xcalloc(1, sizeof (struct grep_expr)); x = xcalloc(1, sizeof (struct grep_expr));
x->node = GREP_NODE_ATOM; x->node = GREP_NODE_ATOM;
x->u.atom = p; x->u.atom = p;
@ -141,10 +143,16 @@ void compile_grep_patterns(struct grep_opt *opt)
/* First compile regexps */ /* First compile regexps */
for (p = opt->pattern_list; p; p = p->next) { for (p = opt->pattern_list; p; p = p->next) {
if (p->token == GREP_PATTERN) switch (p->token) {
case GREP_PATTERN: /* atom */
case GREP_PATTERN_HEAD:
case GREP_PATTERN_BODY:
compile_regexp(p, opt); compile_regexp(p, opt);
else break;
default:
opt->extended = 1; opt->extended = 1;
break;
}
} }
if (!opt->extended) if (!opt->extended)
@ -210,12 +218,16 @@ static int fixmatch(const char *pattern, char *line, regmatch_t *match)
} }
} }
static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol, char *eol) static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol, char *eol, enum grep_context ctx)
{ {
int hit = 0; int hit = 0;
int at_true_bol = 1; int at_true_bol = 1;
regmatch_t pmatch[10]; regmatch_t pmatch[10];
if ((p->token != GREP_PATTERN) &&
((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
return 0;
again: again:
if (!opt->fixed) { if (!opt->fixed) {
regex_t *exp = &p->regexp; regex_t *exp = &p->regexp;
@ -262,37 +274,40 @@ static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol
static int match_expr_eval(struct grep_opt *opt, static int match_expr_eval(struct grep_opt *opt,
struct grep_expr *x, struct grep_expr *x,
char *bol, char *eol) char *bol, char *eol,
enum grep_context ctx)
{ {
switch (x->node) { switch (x->node) {
case GREP_NODE_ATOM: case GREP_NODE_ATOM:
return match_one_pattern(opt, x->u.atom, bol, eol); return match_one_pattern(opt, x->u.atom, bol, eol, ctx);
break; break;
case GREP_NODE_NOT: case GREP_NODE_NOT:
return !match_expr_eval(opt, x->u.unary, bol, eol); return !match_expr_eval(opt, x->u.unary, bol, eol, ctx);
case GREP_NODE_AND: case GREP_NODE_AND:
return (match_expr_eval(opt, x->u.binary.left, bol, eol) && return (match_expr_eval(opt, x->u.binary.left, bol, eol, ctx) &&
match_expr_eval(opt, x->u.binary.right, bol, eol)); match_expr_eval(opt, x->u.binary.right, bol, eol, ctx));
case GREP_NODE_OR: case GREP_NODE_OR:
return (match_expr_eval(opt, x->u.binary.left, bol, eol) || return (match_expr_eval(opt, x->u.binary.left, bol, eol, ctx) ||
match_expr_eval(opt, x->u.binary.right, bol, eol)); match_expr_eval(opt, x->u.binary.right, bol, eol, ctx));
} }
die("Unexpected node type (internal error) %d\n", x->node); die("Unexpected node type (internal error) %d\n", x->node);
} }
static int match_expr(struct grep_opt *opt, char *bol, char *eol) static int match_expr(struct grep_opt *opt, char *bol, char *eol,
enum grep_context ctx)
{ {
struct grep_expr *x = opt->pattern_expression; struct grep_expr *x = opt->pattern_expression;
return match_expr_eval(opt, x, bol, eol); return match_expr_eval(opt, x, bol, eol, ctx);
} }
static int match_line(struct grep_opt *opt, char *bol, char *eol) static int match_line(struct grep_opt *opt, char *bol, char *eol,
enum grep_context ctx)
{ {
struct grep_pat *p; struct grep_pat *p;
if (opt->extended) if (opt->extended)
return match_expr(opt, bol, eol); return match_expr(opt, bol, eol, ctx);
for (p = opt->pattern_list; p; p = p->next) { for (p = opt->pattern_list; p; p = p->next) {
if (match_one_pattern(opt, p, bol, eol)) if (match_one_pattern(opt, p, bol, eol, ctx))
return 1; return 1;
} }
return 0; return 0;
@ -312,6 +327,7 @@ int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long
int binary_match_only = 0; int binary_match_only = 0;
const char *hunk_mark = ""; const char *hunk_mark = "";
unsigned count = 0; unsigned count = 0;
enum grep_context ctx = GREP_CONTEXT_HEAD;
if (buffer_is_binary(buf, size)) { if (buffer_is_binary(buf, size)) {
switch (opt->binary) { switch (opt->binary) {
@ -339,7 +355,10 @@ int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long
ch = *eol; ch = *eol;
*eol = 0; *eol = 0;
hit = match_line(opt, bol, eol); if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
ctx = GREP_CONTEXT_BODY;
hit = match_line(opt, bol, eol, ctx);
*eol = ch; *eol = ch;
/* "grep -v -e foo -e bla" should list lines /* "grep -v -e foo -e bla" should list lines

7
grep.h
View File

@ -3,6 +3,8 @@
enum grep_pat_token { enum grep_pat_token {
GREP_PATTERN, GREP_PATTERN,
GREP_PATTERN_HEAD,
GREP_PATTERN_BODY,
GREP_AND, GREP_AND,
GREP_OPEN_PAREN, GREP_OPEN_PAREN,
GREP_CLOSE_PAREN, GREP_CLOSE_PAREN,
@ -10,6 +12,11 @@ enum grep_pat_token {
GREP_OR, GREP_OR,
}; };
enum grep_context {
GREP_CONTEXT_HEAD,
GREP_CONTEXT_BODY,
};
struct grep_pat { struct grep_pat {
struct grep_pat *next; struct grep_pat *next;
const char *origin; const char *origin;