git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH/RFC 3/4] grep: introduce pattern which matches at line number

From
Thiago Farina <tfransosi@gmail.com>
Date
May 2, 2011, 13:33 UTC
Message-ID
<BANLkTinUWbhCKyfpimUuvCsxnCmRi3E=dg@mail.gmail.com>
In-Reply-To
<f85d783d43bfe7f9e9f23a10e72e8a2a83033cac.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 |   33 +++++++++++++++++++++++++++++++++
>  grep.h |    7 +++++--
>  2 files changed, 38 insertions(+), 2 deletions(-)
>
> diff --git a/grep.c b/grep.c
> index f21b022..cbb3757 100644
> --- a/grep.c
> +++ b/grep.c
> @@ -87,6 +87,15 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
>        }
>  }
>
> +static void parse_lno(struct grep_pat *p, struct grep_opt *opt)
Maybe parse_line_number?
Show 88 quoted lines
> +{
> +       char *eon;
> +
> +       p->u.lno = strtoul(p->pattern, &eon, 10);
> +       if (*eon || p->u.lno == 0)
> +               die("'%s': Invalid number for line match", p->pattern);
> +}
> +
>  static struct grep_expr *compile_pattern_or(struct grep_pat **);
>  static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
>  {
> @@ -100,6 +109,7 @@ static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
>        case GREP_PATTERN: /* atom */
>        case GREP_PATTERN_HEAD:
>        case GREP_PATTERN_BODY:
> +       case GREP_LNO:
>                x = xcalloc(1, sizeof (struct grep_expr));
>                x->node = GREP_NODE_ATOM;
>                x->u.atom = p;
> @@ -264,6 +274,9 @@ void compile_grep_patterns(struct grep_opt *opt)
>                case GREP_PATTERN_BODY:
>                        compile_regexp(p, opt);
>                        break;
> +               case GREP_LNO:
> +                       parse_lno(p, opt);
> +                       break;
>                default:
>                        opt->extended = 1;
>                        break;
> @@ -297,6 +310,7 @@ static void free_pattern_expr(struct grep_expr *x)
>        switch (x->node) {
>        case GREP_NODE_TRUE:
>        case GREP_NODE_ATOM:
> +       case GREP_NODE_LNO:
>                break;
>        case GREP_NODE_NOT:
>                free_pattern_expr(x->u.unary);
> @@ -436,6 +450,21 @@ static struct {
>        { "committer ", 10 },
>  };
>
> +static int match_lno(struct grep_pat *p, char *bol, char *eol, unsigned lno,
> +                    regmatch_t *pmatch)
> +{
> +       if (p->u.lno == lno) {
> +               /*
> +                * because coloring will stop at so == eo, match at the end
> +                * of line, so that the whole line can be colored
> +                */
> +               pmatch->rm_so = eol - bol;
> +               pmatch->rm_eo = eol - bol;
> +               return 1;
> +       }
> +       return 0;
> +}
> +
>  static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
>                             unsigned lno, enum grep_context ctx,
>                             regmatch_t *pmatch, int eflags)
> @@ -444,6 +473,9 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
>        int saved_ch = 0;
>        const char *start = bol;
>
> +       if (p->token == GREP_LNO)
> +               return match_lno(p, bol, eol, lno, pmatch);
> +
>        if ((p->token != GREP_PATTERN) &&
>            ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
>                return 0;
> @@ -614,6 +646,7 @@ static int next_match(struct grep_opt *opt, char *bol, char *eol, unsigned lno,
>                        case GREP_PATTERN: /* atom */
>                        case GREP_PATTERN_HEAD:
>                        case GREP_PATTERN_BODY:
> +                       case GREP_LNO:
>                                hit |= match_next_pattern(p, bol, eol, lno, ctx,
>                                                          pmatch, eflags);
>                                break;
> diff --git a/grep.h b/grep.h
> index 9912c11..41821f3 100644
> --- a/grep.h
> +++ b/grep.h
> @@ -10,7 +10,8 @@ enum grep_pat_token {
>        GREP_OPEN_PAREN,
>        GREP_CLOSE_PAREN,
>        GREP_NOT,
> -       GREP_OR
> +       GREP_OR,
> +       GREP_LNO
Maybe GREP_LINE_NR?
Show 29 quoted lines
>  };
>
>  enum grep_context {
> @@ -34,6 +35,7 @@ struct grep_pat {
>        enum grep_header_field field;
>        union {
>                regex_t regexp;
> +               unsigned lno;
>        } u;
>        unsigned fixed:1;
>        unsigned ignore_case:1;
> @@ -45,7 +47,8 @@ enum grep_expr_node {
>        GREP_NODE_NOT,
>        GREP_NODE_AND,
>        GREP_NODE_TRUE,
> -       GREP_NODE_OR
> +       GREP_NODE_OR,
> +       GREP_NODE_LNO
>  };
>
>  struct grep_expr {
> --
> 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
>
Previous: Bert WesargNext: Bert Wesarg
Message 6 of 22 in “grep: support to match by line number”
  1. 0/4 grep: support to match by line numberBert Wesarg, May 2, 2011
  2. 1/4 grep: prepare for re-using the space of the regexp member in struct grep_patBert Wesarg, May 2, 2011
  3. 2/4 grep: pass current line number down to match_one_patternBert Wesarg, May 2, 2011
  4. 3/4 grep: introduce pattern which matches at line numberBert Wesarg, May 2, 2011
  5. 4/4 grep: provide option to match line numberBert Wesarg, May 2, 2011
  6. Thiago FarinaMay 2, 2011
  7. Bert WesargMay 2, 2011
  8. Thiago FarinaMay 2, 2011
  9. Bert WesargMay 2, 2011
  10. Junio C HamanoMay 2, 2011
  11. Thiago FarinaMay 2, 2011
  12. Bert WesargMay 2, 2011
  13. Sverre RabbelierMay 2, 2011
  14. Bert WesargMay 2, 2011
  15. Junio C HamanoMay 2, 2011
  16. Bert WesargMay 2, 2011
  17. Junio C HamanoMay 2, 2011
  18. Junio C HamanoMay 2, 2011
  19. Jakub NarebskiMay 2, 2011
  20. Bert WesargMay 2, 2011
  21. Junio C HamanoMay 2, 2011
  22. Bert WesargMay 2, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.