Subject: bug#24100: [PATCH 2/4] Get rid of re_set_syntax

From: Michal Nazarewicz <[email-masked]>
Date: Thu, 28 Jul 2016 20:07:15 +0200

Instead of using a global variable for storing regex syntax, pass it
to re_compile_pattern.  This is only enabled when compiling Emacs (i.e.
‘#ifdef emacs’).

* src/regex.h (re_set_syntax): Declare only #ifndef emacs.
(re_compile_pattern): Now takes syntax argument #ifdef emacs.

* src/regex.c (re_syntax_options): Define only #ifndef emacs.
(re_compile_pattern): Use the new syntax argument #ifdef emacs.

* src/search.c (compile_pattern_1): Don’t use re_set_syntax and
instead pass syntax to re_compile_pattern directly.
---
 src/regex.c  | 12 +++++++++++-
 src/regex.h  | 14 ++++++++++++++
 src/search.c | 10 ++++------
 3 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/src/regex.c b/src/regex.c
index 261d299..4edc064 100644
--- a/src/regex.c
+++ b/src/regex.c
@@ -1149,6 +1149,8 @@ print_double_string (re_char *where, re_char *string1, ssize_t size1,
 
 #endif /* not DEBUG */
 
+#ifndef emacs
+
 /* Set by `re_set_syntax' to the current regexp syntax to recognize.  Can
    also be assigned to arbitrarily: each pattern buffer stores its own
    syntax, so it can be changed between regex compilations.  */
@@ -1174,6 +1176,8 @@ re_set_syntax (reg_syntax_t syntax)
 }
 WEAK_ALIAS (__re_set_syntax, re_set_syntax)
 
+#endif
+
 /* Regexp to use to replace spaces, or NULL meaning don't.  */
 static const_re_char *whitespace_regexp;
 
@@ -6271,8 +6275,14 @@ bcmp_translate (const_re_char *s1, const_re_char *s2, register ssize_t len,
 
 const char *
 re_compile_pattern (const char *pattern, size_t length,
+#ifdef emacs
+		    reg_syntax_t syntax,
+#endif
 		    struct re_pattern_buffer *bufp)
 {
+#ifndef emacs
+  const reg_syntax_t syntax = re_syntax_options;
+#endif
   reg_errcode_t ret;
 
   /* GNU code is written to assume at least RE_NREGS registers will be set
@@ -6284,7 +6294,7 @@ re_compile_pattern (const char *pattern, size_t length,
      setting no_sub.  */
   bufp->no_sub = 0;
 
-  ret = regex_compile ((re_char*) pattern, length, re_syntax_options, bufp);
+  ret = regex_compile ((re_char*) pattern, length, syntax, bufp);
 
   if (!ret)
     return NULL;
diff --git a/src/regex.h b/src/regex.h
index 01b659a..4497333 100644
--- a/src/regex.h
+++ b/src/regex.h
@@ -20,6 +20,13 @@
 #ifndef _REGEX_H
 #define _REGEX_H 1
 
+#if defined emacs && (defined _REGEX_RE_COMP || defined _LIBC)
+/* We’re not defining re_set_syntax and using a different prototype of
+   re_compile_pattern when building Emacs so fail compilation early with
+   a (somewhat helpful) error message when conflict is detected. */
+# error "_REGEX_RE_COMP nor _LIBC can be defined if emacs is defined."
+#endif
+
 /* Allow the use in C++ code.  */
 #ifdef __cplusplus
 extern "C" {
@@ -453,14 +460,21 @@ typedef struct
 
 /* Declarations for routines.  */
 
+#ifndef emacs
+
 /* Sets the current default syntax to SYNTAX, and return the old syntax.
    You can also simply assign to the `re_syntax_options' variable.  */
 extern reg_syntax_t re_set_syntax (reg_syntax_t __syntax);
 
+#endif
+
 /* Compile the regular expression PATTERN, with length LENGTH
    and syntax given by the global `re_syntax_options', into the buffer
    BUFFER.  Return NULL if successful, and an error string if not.  */
 extern const char *re_compile_pattern (const char *__pattern, size_t __length,
+#ifdef emacs
+				       reg_syntax_t syntax,
+#endif
 				       struct re_pattern_buffer *__buffer);

diff --git a/src/search.c b/src/search.c
index 7cb18a2..f041952 100644
--- a/src/search.c
+++ b/src/search.c
@@ -113,8 +113,8 @@ static void
 compile_pattern_1 (struct regexp_cache *cp, Lisp_Object pattern,
 		   Lisp_Object translate, bool posix)
 {
+  reg_syntax_t syntax;
   char *val;
-  reg_syntax_t old;
 
   cp->regexp = Qnil;
   cp->buf.translate = (! NILP (translate) ? translate : make_number (0));
@@ -131,16 +131,15 @@ compile_pattern_1 (struct regexp_cache *cp, Lisp_Object pattern,
      Using BLOCK_INPUT here means the debugger won't run if an error occurs.
      So let's turn it off.  */
   /*  BLOCK_INPUT;  */
-  old = re_set_syntax (RE_SYNTAX_EMACS
-		       | (posix ? 0 : RE_NO_POSIX_BACKTRACKING));
 
   if (STRINGP (Vsearch_spaces_regexp))
     re_set_whitespace_regexp (SSDATA (Vsearch_spaces_regexp));
   else
     re_set_whitespace_regexp (NULL);
 
-  val = (char *) re_compile_pattern (SSDATA (pattern),
-				     SBYTES (pattern), &cp->buf);
+  syntax = RE_SYNTAX_EMACS | (posix ? 0 : RE_NO_POSIX_BACKTRACKING);
+  val = (char *) re_compile_pattern (SSDATA (pattern), SBYTES (pattern),
+				     syntax, &cp->buf);
 
   /* If the compiled pattern hard codes some of the contents of the
      syntax-table, it can only be reused with *this* syntax table.  */
@@ -148,7 +147,6 @@ compile_pattern_1 (struct regexp_cache *cp, Lisp_Object pattern,
 
   re_set_whitespace_regexp (NULL);
 
-  re_set_syntax (old);
   /* unblock_input ();  */
   if (val)
     xsignal1 (Qinvalid_regexp, build_string (val));
-- 
2.8.0.rc3.226.g39d4020