Re: [PATCH/RFC 2/4] grep: pass current line number down to match_one_pattern
- From
Thiago Farina <tfransosi@gmail.com>
- Date
- May 2, 2011, 13:30 UTC
- Message-ID
- <BANLkTi=HtStcEFezT9sEeRFzsGw1mMyg7Q@mail.gmail.com>
- In-Reply-To
- <2681b60988c7c4d059f83df368395eca0520012c.1304321122.git.bert.wesarg@googlemail.com>
On Mon, May 2, 2011 at 8:39 AM, Bert Wesarg <bert.wesarg@googlemail.com> wrote:
Show 15 quoted lines
> Signed-off-by: Bert Wesarg <bert.wesarg@googlemail.com>
> ---
> grep.c | 42 +++++++++++++++++++++---------------------
> 1 files changed, 21 insertions(+), 21 deletions(-)
>
> diff --git a/grep.c b/grep.c
> index b8eda9e..f21b022 100644
> --- a/grep.c
> +++ b/grep.c
> @@ -437,7 +437,7 @@ static struct {
> };
>
> static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
> - enum grep_context ctx,
> + unsigned lno, enum grep_context ctx,I'd rename lno to line_nr, so it's more clearer. Also, I'd add the new paramenter at the end, not in some random position (or was there some particular reason to put here but not in the end?).
Show 134 quoted lines
> regmatch_t *pmatch, int eflags)
> {
> int hit = 0;
> @@ -516,7 +516,7 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
> }
>
> static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
> - enum grep_context ctx, int collect_hits)
> + unsigned lno, enum grep_context ctx, int collect_hits)
> {
> int h = 0;
> regmatch_t match;
> @@ -528,25 +528,25 @@ static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
> h = 1;
> break;
> case GREP_NODE_ATOM:
> - h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
> + h = match_one_pattern(x->u.atom, bol, eol, lno, ctx, &match, 0);
> break;
> case GREP_NODE_NOT:
> - h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
> + h = !match_expr_eval(x->u.unary, bol, eol, lno, ctx, 0);
> break;
> case GREP_NODE_AND:
> - if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
> + if (!match_expr_eval(x->u.binary.left, bol, eol, lno, ctx, 0))
> return 0;
> - h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
> + h = match_expr_eval(x->u.binary.right, bol, eol, lno, ctx, 0);
> break;
> case GREP_NODE_OR:
> if (!collect_hits)
> return (match_expr_eval(x->u.binary.left,
> - bol, eol, ctx, 0) ||
> + bol, eol, lno, ctx, 0) ||
> match_expr_eval(x->u.binary.right,
> - bol, eol, ctx, 0));
> - h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
> + bol, eol, lno, ctx, 0));
> + h = match_expr_eval(x->u.binary.left, bol, eol, lno, ctx, 0);
> x->u.binary.left->hit |= h;
> - h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
> + h |= match_expr_eval(x->u.binary.right, bol, eol, lno, ctx, 1);
> break;
> default:
> die("Unexpected node type (internal error) %d", x->node);
> @@ -557,36 +557,36 @@ static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
> }
>
> static int match_expr(struct grep_opt *opt, char *bol, char *eol,
> - enum grep_context ctx, int collect_hits)
> + unsigned lno, enum grep_context ctx, int collect_hits)
> {
> struct grep_expr *x = opt->pattern_expression;
> - return match_expr_eval(x, bol, eol, ctx, collect_hits);
> + return match_expr_eval(x, bol, eol, lno, ctx, collect_hits);
> }
>
> static int match_line(struct grep_opt *opt, char *bol, char *eol,
> - enum grep_context ctx, int collect_hits)
> + unsigned lno, enum grep_context ctx, int collect_hits)
> {
> struct grep_pat *p;
> regmatch_t match;
>
> if (opt->extended)
> - return match_expr(opt, bol, eol, ctx, collect_hits);
> + return match_expr(opt, bol, eol, lno, ctx, collect_hits);
>
> /* we do not call with collect_hits without being extended */
> for (p = opt->pattern_list; p; p = p->next) {
> - if (match_one_pattern(p, bol, eol, ctx, &match, 0))
> + if (match_one_pattern(p, bol, eol, lno, ctx, &match, 0))
> return 1;
> }
> return 0;
> }
>
> static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
> - enum grep_context ctx,
> + unsigned lno, enum grep_context ctx,
> regmatch_t *pmatch, int eflags)
> {
> regmatch_t match;
>
> - if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
> + if (!match_one_pattern(p, bol, eol, lno, ctx, &match, eflags))
> return 0;
> if (match.rm_so < 0 || match.rm_eo < 0)
> return 0;
> @@ -601,7 +601,7 @@ static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
> return 1;
> }
>
> -static int next_match(struct grep_opt *opt, char *bol, char *eol,
> +static int next_match(struct grep_opt *opt, char *bol, char *eol, unsigned lno,
> enum grep_context ctx, regmatch_t *pmatch, int eflags)
> {
> struct grep_pat *p;
> @@ -614,7 +614,7 @@ static int next_match(struct grep_opt *opt, char *bol, char *eol,
> case GREP_PATTERN: /* atom */
> case GREP_PATTERN_HEAD:
> case GREP_PATTERN_BODY:
> - hit |= match_next_pattern(p, bol, eol, ctx,
> + hit |= match_next_pattern(p, bol, eol, lno, ctx,
> pmatch, eflags);
> break;
> default:
> @@ -667,7 +667,7 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
> else if (sign == '=')
> line_color = opt->color_function;
> *eol = '\0';
> - while (next_match(opt, bol, eol, ctx, &match, eflags)) {
> + while (next_match(opt, bol, eol, lno, ctx, &match, eflags)) {
> if (match.rm_so == match.rm_eo)
> break;
>
> @@ -911,7 +911,7 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
> if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
> ctx = GREP_CONTEXT_BODY;
>
> - hit = match_line(opt, bol, eol, ctx, collect_hits);
> + hit = match_line(opt, bol, eol, lno, ctx, collect_hits);
> *eol = ch;
>
> if (collect_hits)
> --
> 1.7.5.349.gfeb1a
>
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>