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

[PATCH v3 2/6] grep: Extract compile_regexp_failed() from compile_regexp()

From
Michał Kiedrowicz <michal.kiedrowicz@gmail.com>
Date
May 9, 2011, 21:52 UTC
Message-ID
<1304977928-7142-3-git-send-email-michal.kiedrowicz@gmail.com>
In-Reply-To
<1304977928-7142-1-git-send-email-michal.kiedrowicz@gmail.com>

This simplifies compile_regexp() a little and allows re-using error handling code.

Signed-off-by: Michał Kiedrowicz <michal.kiedrowicz@gmail.com>
---
 grep.c |   25 ++++++++++++++++---------
 1 files changed, 16 insertions(+), 9 deletions(-)
diff --git a/grep.c b/grep.c
index 250462e..870d10c 100644
--- a/grep.c
+++ b/grep.c
@@ -59,6 +59,21 @@ struct grep_opt *grep_opt_dup(const struct grep_opt *opt)
 	return ret;
 }
 
+static NORETURN void compile_regexp_failed(const struct grep_pat *p,
+		const char *error)
+{
+	char where[1024];
+
+	if (p->no)
+		sprintf(where, "In '%s' at %d, ", p->origin, p->no);
+	else if (p->origin)
+		sprintf(where, "%s, ", p->origin);
+	else
+		where[0] = 0;
+
+	die("%s'%s': %s", where, p->pattern, error);
+}
+
 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
 {
 	int err;
@@ -73,17 +88,9 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
 	err = regcomp(&p->regexp, p->pattern, opt->regflags);
 	if (err) {
 		char errbuf[1024];
-		char where[1024];
-		if (p->no)
-			sprintf(where, "In '%s' at %d, ",
-				p->origin, p->no);
-		else if (p->origin)
-			sprintf(where, "%s, ", p->origin);
-		else
-			where[0] = 0;
 		regerror(err, &p->regexp, errbuf, 1024);
 		regfree(&p->regexp);
-		die("%s'%s': %s", where, p->pattern, errbuf);
+		compile_regexp_failed(p, errbuf);
 	}
 }
 
-- 
1.7.3.4
Previous: Michał KiedrowiczNext: Michał Kiedrowicz
Message 3 of 10 in “Add PCRE support to git-grep”
  1. 0/6 Add PCRE support to git-grepMichał Kiedrowicz, May 9, 2011
  2. 1/6 grep: Fix a typo in a commentMichał Kiedrowicz, May 9, 2011
  3. 2/6 grep: Extract compile_regexp_failed() from compile_regexp()Michał Kiedrowicz, May 9, 2011
  4. 3/6 git-grep: Learn PCREMichał Kiedrowicz, May 9, 2011
  5. 4/6 configure: Check for libpcreMichał Kiedrowicz, May 9, 2011
  6. 5/6 grep: Add basic testsMichał Kiedrowicz, May 9, 2011
  7. 6/6 git-grep: Bail out when -P is used with -F or -EMichał Kiedrowicz, May 9, 2011
  8. Junio C HamanoMay 10, 2011
  9. Michal KiedrowiczMay 10, 2011
  10. Junio C HamanoMay 10, 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.