aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--xutil.c39
-rw-r--r--xutil.h7
2 files changed, 46 insertions, 0 deletions
diff --git a/xutil.c b/xutil.c
index 7ee7a69..eadd378 100644
--- a/xutil.c
+++ b/xutil.c
@@ -92,3 +92,42 @@ xstrndup (const char *s, size_t n)
return ret;
}
+
+void
+xregcomp (regex_t *preg, const char *regex, int cflags)
+{
+ int rerr;
+
+ rerr = regcomp (preg, regex, cflags);
+ if (rerr) {
+ size_t error_size = regerror (rerr, preg, NULL, 0);
+ char *error = xmalloc (error_size);
+
+ regerror (rerr, preg, error, error_size);
+ fprintf (stderr, "Internal error compiling regex %s: %s\n",
+ regex, error);
+ free (error);
+ exit (1);
+ }
+}
+
+int
+xregexec (const regex_t *preg, const char *string,
+ size_t nmatch, regmatch_t pmatch[], int eflags)
+{
+ int i, rerr;
+
+ rerr = regexec (preg, string, nmatch, pmatch, eflags);
+ if (rerr)
+ return rerr;
+
+ for (i = 0; i < nmatch; i++) {
+ if (pmatch[i].rm_so == -1) {
+ fprintf (stderr, "Internal error matching regex against %s: Sub-match %d not found\n",
+ string, i);
+ exit (1);
+ }
+ }
+
+ return 0;
+}
diff --git a/xutil.h b/xutil.h
index 7a089a5..b973f7d 100644
--- a/xutil.h
+++ b/xutil.h
@@ -41,4 +41,11 @@ xstrdup (const char *s);
char *
xstrndup (const char *s, size_t n);
+void
+xregcomp (regex_t *preg, const char *regex, int cflags);
+
+int
+xregexec (const regex_t *preg, const char *string,
+ size_t nmatch, regmatch_t pmatch[], int eflags);
+
#endif