>From cdc0b9cce43e38463af0b2b7ad4a0181f41995a2 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Fri, 30 Oct 2015 18:03:18 +0900
Subject: [PATCH 1/2] Allow regex module to be used outside server.

To make regular expression available on frontend, enable pg_regex
module and the files included from it to be detached from the features
not available when used outside backend.
---
 src/backend/regex/regc_pg_locale.c |  7 +++++++
 src/backend/regex/regcomp.c        | 12 ++++++++++++
 src/include/regex/regcustom.h      |  6 ++++++
 src/include/utils/pg_locale.h      |  7 +++++--
 4 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/src/backend/regex/regc_pg_locale.c b/src/backend/regex/regc_pg_locale.c
index b707b06..a631ac2 100644
--- a/src/backend/regex/regc_pg_locale.c
+++ b/src/backend/regex/regc_pg_locale.c
@@ -220,6 +220,13 @@ static const unsigned char pg_char_properties[128] = {
 };
 
 
+/* Get rid of using server-side feature elsewhere of server. */
+#ifdef FRONTEND
+#define lc_ctype_is_c(col) (true)
+#define GetDatabaseEncoding() PG_UTF8
+#define ereport(x,...) exit(1)
+#endif
+
 /*
  * pg_set_regex_collation: set collation for these functions to obey
  *
diff --git a/src/backend/regex/regcomp.c b/src/backend/regex/regcomp.c
index a165b3b..b35ccb4 100644
--- a/src/backend/regex/regcomp.c
+++ b/src/backend/regex/regcomp.c
@@ -2040,11 +2040,17 @@ rfree(regex_t *re)
  * The current implementation is Postgres-specific.  If we ever get around
  * to splitting the regex code out as a standalone library, there will need
  * to be some API to let applications define a callback function for this.
+ *
+ * This check is available only on server side.
  */
 static int
 rcancelrequested(void)
 {
+#ifndef FRONTEND
 	return InterruptPending && (QueryCancelPending || ProcDiePending);
+#else
+	return 0;
+#endif
 }
 
 /*
@@ -2056,11 +2062,17 @@ rcancelrequested(void)
  * The current implementation is Postgres-specific.  If we ever get around
  * to splitting the regex code out as a standalone library, there will need
  * to be some API to let applications define a callback function for this.
+ *
+ * This check is available only on server side.
  */
 static int
 rstacktoodeep(void)
 {
+#ifndef FRONTEND
 	return stack_is_too_deep();
+#else
+	return 0;
+#endif
 }
 
 #ifdef REG_DEBUG
diff --git a/src/include/regex/regcustom.h b/src/include/regex/regcustom.h
index dbb461a..ffc4031 100644
--- a/src/include/regex/regcustom.h
+++ b/src/include/regex/regcustom.h
@@ -29,7 +29,11 @@
  */
 
 /* headers if any */
+#ifdef FRONTEND
+#include "postgres_fe.h"
+#else
 #include "postgres.h"
+#endif
 
 #include <ctype.h>
 #include <limits.h>
@@ -53,7 +57,9 @@
 #define MALLOC(n)		malloc(n)
 #define FREE(p)			free(VS(p))
 #define REALLOC(p,n)	realloc(VS(p),n)
+#ifndef assert
 #define assert(x)		Assert(x)
+#endif
 
 /* internal character type and related */
 typedef pg_wchar chr;			/* the type itself */
diff --git a/src/include/utils/pg_locale.h b/src/include/utils/pg_locale.h
index 8e91033..d71ec07 100644
--- a/src/include/utils/pg_locale.h
+++ b/src/include/utils/pg_locale.h
@@ -17,14 +17,16 @@
 #include <xlocale.h>
 #endif
 
+/* Don't look GUCs elsewhere of server */
+#ifndef FRONTEND
 #include "utils/guc.h"
 
-
 /* GUC settings */
 extern char *locale_messages;
 extern char *locale_monetary;
 extern char *locale_numeric;
 extern char *locale_time;
+#endif
 
 /* lc_time localization cache */
 extern char *localized_abbrev_days[];
@@ -32,7 +34,7 @@ extern char *localized_full_days[];
 extern char *localized_abbrev_months[];
 extern char *localized_full_months[];
 
-
+#ifndef FRONTEND
 extern bool check_locale_messages(char **newval, void **extra, GucSource source);
 extern void assign_locale_messages(const char *newval, void *extra);
 extern bool check_locale_monetary(char **newval, void **extra, GucSource source);
@@ -41,6 +43,7 @@ extern bool check_locale_numeric(char **newval, void **extra, GucSource source);
 extern void assign_locale_numeric(const char *newval, void *extra);
 extern bool check_locale_time(char **newval, void **extra, GucSource source);
 extern void assign_locale_time(const char *newval, void *extra);
+#endif
 
 extern bool check_locale(int category, const char *locale, char **canonname);
 extern char *pg_perm_setlocale(int category, const char *locale);
-- 
1.8.3.1

