@matheusbsbs / matheus-bagliano / commits / 480c1ca6fd
Fork 0

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>

Junio C Hamano committed Sep 20, 2006 at 12:39 UTC 480c1ca6fd8df58a783e231648b489ed2bfd17f1
2 files changed +42 -16
grep.c
+35 -16
@@ -43,6 +43,8 @@ static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
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;
@@ -141,10 +143,16 @@ void compile_grep_patterns(struct grep_opt *opt)
143
144 /* First compile regexps */
145 for (p = opt->pattern_list; p; p = p->next) {
144 - if (p->token == GREP_PATTERN)
146 + switch (p->token) {
147 + case GREP_PATTERN: /* atom */
148 + case GREP_PATTERN_HEAD:
149 + case GREP_PATTERN_BODY:
150 compile_regexp(p, opt);
146 - else
151 + break;
152 + default:
153 opt->extended = 1;
154 + break;
155 + }
156 }
157
158 if (!opt->extended)
@@ -210,12 +218,16 @@ static int fixmatch(const char *pattern, char *line, regmatch_t *match)
218 }
219 }
220
213 -static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol, char *eol)
221 +static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol, char *eol, enum grep_context ctx)
222 {
223 int hit = 0;
224 int at_true_bol = 1;
225 regmatch_t pmatch[10];
226
227 + if ((p->token != GREP_PATTERN) &&
228 + ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
229 + return 0;
230 +
231 again:
232 if (!opt->fixed) {
233 regex_t *exp = &p->regexp;
@@ -262,37 +274,40 @@ static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol
274
275 static int match_expr_eval(struct grep_opt *opt,
276 struct grep_expr *x,
265 - char *bol, char *eol)
277 + char *bol, char *eol,
278 + enum grep_context ctx)
279 {
280 switch (x->node) {
281 case GREP_NODE_ATOM:
269 - return match_one_pattern(opt, x->u.atom, bol, eol);
282 + return match_one_pattern(opt, x->u.atom, bol, eol, ctx);
283 break;
284 case GREP_NODE_NOT:
272 - return !match_expr_eval(opt, x->u.unary, bol, eol);
285 + return !match_expr_eval(opt, x->u.unary, bol, eol, ctx);
286 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));
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:
277 - return (match_expr_eval(opt, x->u.binary.left, bol, eol) ||
278 - match_expr_eval(opt, x->u.binary.right, bol, eol));
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));
292 }
293 die("Unexpected node type (internal error) %d\n", x->node);
294 }
295
283 -static int match_expr(struct grep_opt *opt, char *bol, char *eol)
296 +static int match_expr(struct grep_opt *opt, char *bol, char *eol,
297 + enum grep_context ctx)
298 {
299 struct grep_expr *x = opt->pattern_expression;
286 - return match_expr_eval(opt, x, bol, eol);
300 + return match_expr_eval(opt, x, bol, eol, ctx);
301 }
302
289 -static int match_line(struct grep_opt *opt, char *bol, char *eol)
303 +static int match_line(struct grep_opt *opt, char *bol, char *eol,
304 + enum grep_context ctx)
305 {
306 struct grep_pat *p;
307 if (opt->extended)
293 - return match_expr(opt, bol, eol);
308 + return match_expr(opt, bol, eol, ctx);
309 for (p = opt->pattern_list; p; p = p->next) {
295 - if (match_one_pattern(opt, p, bol, eol))
310 + if (match_one_pattern(opt, p, bol, eol, ctx))
311 return 1;
312 }
313 return 0;
@@ -312,6 +327,7 @@ int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long
327 int binary_match_only = 0;
328 const char *hunk_mark = "";
329 unsigned count = 0;
330 + enum grep_context ctx = GREP_CONTEXT_HEAD;
331
332 if (buffer_is_binary(buf, size)) {
333 switch (opt->binary) {
@@ -339,7 +355,10 @@ int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long
355 ch = *eol;
356 *eol = 0;
357
342 - hit = match_line(opt, bol, eol);
358 + if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
359 + ctx = GREP_CONTEXT_BODY;
360 +
361 + hit = match_line(opt, bol, eol, ctx);
362 *eol = ch;
363
364 /* "grep -v -e foo -e bla" should list lines
grep.h
+7
@@ -3,6 +3,8 @@
3
4 enum grep_pat_token {
5 GREP_PATTERN,
6 + GREP_PATTERN_HEAD,
7 + GREP_PATTERN_BODY,
8 GREP_AND,
9 GREP_OPEN_PAREN,
10 GREP_CLOSE_PAREN,
@@ -10,6 +12,11 @@ enum grep_pat_token {
12 GREP_OR,
13 };
14
15 +enum grep_context {
16 + GREP_CONTEXT_HEAD,
17 + GREP_CONTEXT_BODY,
18 +};
19 +
20 struct grep_pat {
21 struct grep_pat *next;
22 const char *origin;