[hackers] [sbase] util: add eregcomp: show descriptive error message on regcomp error || Hiltjo Posthuma

From: <git_AT_suckless.org>
Date: Sun, 16 Nov 2014 15:36:51 +0100

commit ce90cc57d46faa40232dd963213c021708ea0d0e
Author: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Sun Nov 16 15:16:14 2014 +0100

    util: add eregcomp: show descriptive error message on regcomp error

diff --git a/Makefile b/Makefile
index 1ad68ed..575f31b 100644
--- a/Makefile
+++ b/Makefile
_AT_@ -25,6 +25,7 @@ LIB = \
         util/ealloc.o \
         util/enmasse.o \
         util/eprintf.o \
+ util/eregcomp.o \
         util/estrtod.o \
         util/estrtol.o \
         util/fnck.o \
diff --git a/util.h b/util.h
index e438357..0b94152 100644
--- a/util.h
+++ b/util.h
_AT_@ -1,4 +1,5 @@
 /* See LICENSE file for copyright and license details. */
+#include <regex.h>
 #include <stddef.h>
 #include <sys/types.h>
 #include "arg.h"
_AT_@ -34,6 +35,10 @@ size_t strlcat(char *, const char *, size_t);
 #undef strlcpy
 size_t strlcpy(char *, const char *, size_t);
 
+/* regex */
+int enregcomp(int, regex_t *, const char *, int);
+int eregcomp(regex_t *, const char *, int);
+
 /* misc */
 void enmasse(int, char **, int (*)(const char *, const char *));
 void fnck(const char *, const char *, int (*)(const char *, const char *));
diff --git a/util/eregcomp.c b/util/eregcomp.c
new file mode 100644
index 0000000..49c1bc5
--- /dev/null
+++ b/util/eregcomp.c
_AT_@ -0,0 +1,25 @@
+#include <regex.h>
+#include <stdio.h>
+#include <sys/types.h>
+
+#include "../util.h"
+
+int
+enregcomp(int status, regex_t *preg, const char *regex, int cflags)
+{
+ char errbuf[BUFSIZ] = "";
+ int r;
+
+ if((r = regcomp(preg, regex, cflags)) == 0)
+ return r;
+
+ regerror(r, preg, errbuf, sizeof(errbuf));
+ enprintf(status, "invalid regex: %s\n", errbuf);
+ return r;
+}
+
+int
+eregcomp(regex_t *preg, const char *regex, int cflags)
+{
+ return enregcomp(1, preg, regex, cflags);
+}
Received on Sun Nov 16 2014 - 15:36:51 CET

This archive was generated by hypermail 2.3.0 : Sun Nov 16 2014 - 15:48:08 CET