From ab3324be477b2663196e0cc73d96aa38d59da65a Mon Sep 17 00:00:00 2001
From: John Regan <john@jrjrtech.com>
Date: Thu, 13 Aug 2015 14:28:17 -0400
Subject: [PATCH] Enable PCRE as RegEx Library

---
 Makefile    | 8 +++++++-
 probe.c     | 6 +++++-
 sslh-main.c | 6 +++++-
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/Makefile b/Makefile
index 146d29d..2c71550 100644
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,9 @@
 # Configuration
 
 VERSION=$(shell ./genver.sh -r)
+ENABLE_REGEX=1  # Enable regex probes
 USELIBCONFIG=1	# Use libconfig? (necessary to use configuration files)
-USELIBPCRE=1	# Use libpcre? (necessary to use regex probe)
+USELIBPCRE=1	# Use libpcre? (needed for regex on musl)
 USELIBWRAP?=	# Use libwrap?
 USELIBCAP=	# Use libcap?
 COV_TEST= 	# Perform test coverage?
@@ -30,8 +31,13 @@ ifneq ($(strip $(USELIBWRAP)),)
 	CPPFLAGS+=-DLIBWRAP
 endif
 
+ifneq ($(strip $(ENABLE_REGEX)),)
+	CPPFLAGS+=-DENABLE_REGEX
+endif
+
 ifneq ($(strip $(USELIBPCRE)),)
 	CPPFLAGS+=-DLIBPCRE
+	LIBS:=$(LIBS) -lpcre
 endif
 
 ifneq ($(strip $(USELIBCONFIG)),)
diff --git a/probe.c b/probe.c
index 27120c4..3514a48 100644
--- a/probe.c
+++ b/probe.c
@@ -21,9 +21,13 @@
 
 #define _GNU_SOURCE
 #include <stdio.h>
+#ifdef ENABLE_REGEX
 #ifdef LIBPCRE
+#include <pcreposix.h>
+#else
 #include <regex.h>
 #endif
+#endif
 #include <ctype.h>
 #include "probe.h"
 
@@ -275,7 +279,7 @@ static int is_adb_protocol(const char *p, int len, struct proto *proto)
 
 static int regex_probe(const char *p, int len, struct proto *proto)
 {
-#ifdef LIBPCRE
+#ifdef ENABLE_REGEX
     regex_t **probe = proto->data;
     regmatch_t pos = { 0, len };
 
diff --git a/sslh-main.c b/sslh-main.c
index 0025017..e5043b5 100644
--- a/sslh-main.c
+++ b/sslh-main.c
@@ -25,9 +25,13 @@
 #ifdef LIBCONFIG
 #include <libconfig.h>
 #endif
+#ifdef ENABLE_REGEX
 #ifdef LIBPCRE
+#include <pcreposix.h>
+#else
 #include <regex.h>
 #endif
+#endif
 
 #include "common.h"
 #include "probe.h"
@@ -176,7 +180,7 @@ static int config_listen(config_t *config, struct addrinfo **listen)
 #ifdef LIBCONFIG
 static void setup_regex_probe(struct proto *p, config_setting_t* probes)
 {
-#ifdef LIBPCRE
+#ifdef ENABLE_REGEX
     int num_probes, errsize, i, res;
     char *err;
     const char * expr;