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

[PATCH/RFC 2/4] grep: pass current line number down to match_one_pattern

From
Bert Wesarg <bert.wesarg@googlemail.com>
Date
May 2, 2011, 11:39 UTC
Message-ID
<2681b60988c7c4d059f83df368395eca0520012c.1304321122.git.bert.wesarg@googlemail.com>
In-Reply-To
<f768ea6e107cdd229a18df0bac3bf583eb1f9fc5.1304321122.git.bert.wesarg@googlemail.com>
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,
 			     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
Previous: Bert WesargNext: Bert Wesarg
Message 3 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.