From: Bert Wesarg <bert.wesarg@googlemail.com>
To: git@vger.kernel.org
Cc: Bert Wesarg <bert.wesarg@googlemail.com>
Subject: [PATCH/RFC 3/4] grep: introduce pattern which matches at line number
Date: Mon, 2 May 2011 13:39:12 +0200 [thread overview]
Message-ID: <f85d783d43bfe7f9e9f23a10e72e8a2a83033cac.1304321122.git.bert.wesarg@googlemail.com> (raw)
In-Reply-To: <2681b60988c7c4d059f83df368395eca0520012c.1304321122.git.bert.wesarg@googlemail.com>
In-Reply-To: <cover.1304321122.git.bert.wesarg@googlemail.com>
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)
+{
+ 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
};
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
next prev parent reply other threads:[~2011-05-02 11:39 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-05-02 11:39 [PATCH/RFC 0/4] grep: support to match by line number Bert Wesarg
[not found] ` <cover.1304321122.git.bert.wesarg@googlemail.com>
2011-05-02 11:39 ` [PATCH/RFC 1/4] grep: prepare for re-using the space of the regexp member in struct grep_pat Bert Wesarg
2011-05-02 13:27 ` Thiago Farina
2011-05-02 14:25 ` Bert Wesarg
2011-05-02 11:39 ` [PATCH/RFC 2/4] grep: pass current line number down to match_one_pattern Bert Wesarg
2011-05-02 13:30 ` Thiago Farina
2011-05-02 14:29 ` Bert Wesarg
2011-05-02 16:40 ` Junio C Hamano
2011-05-02 11:39 ` Bert Wesarg [this message]
2011-05-02 13:33 ` [PATCH/RFC 3/4] grep: introduce pattern which matches at line number Thiago Farina
2011-05-02 14:32 ` Bert Wesarg
2011-05-02 11:39 ` [PATCH/RFC 4/4] grep: provide option to match " Bert Wesarg
2011-05-02 11:54 ` [PATCH/RFC 0/4] grep: support to match by " Sverre Rabbelier
2011-05-02 12:20 ` Bert Wesarg
2011-05-02 16:46 ` Junio C Hamano
2011-05-02 19:14 ` Bert Wesarg
2011-05-02 19:30 ` Junio C Hamano
2011-05-02 16:38 ` Junio C Hamano
2011-05-02 17:11 ` Jakub Narebski
2011-05-02 18:54 ` Bert Wesarg
2011-05-02 19:08 ` Junio C Hamano
2011-05-02 19:33 ` Bert Wesarg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=f85d783d43bfe7f9e9f23a10e72e8a2a83033cac.1304321122.git.bert.wesarg@googlemail.com \
--to=bert.wesarg@googlemail.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).