Jaroslav Škarvada d61264
--- ./src/search.c.old	2010-02-12 14:29:01.000000000 +0100
Jaroslav Škarvada d61264
+++ ./src/search.c	2010-02-12 14:29:18.207410166 +0100
Jaroslav Škarvada d61264
@@ -278,6 +278,8 @@
Jaroslav Škarvada d61264
   char eol = eolbyte;
Jaroslav Škarvada d61264
   int backref, start, len, best_len;
Jaroslav Škarvada d61264
   struct kwsmatch kwsm;
Jaroslav Škarvada d61264
+  static int use_dfa;
Jaroslav Škarvada d61264
+  static int use_dfa_checked = 0;
Jaroslav Škarvada d61264
   size_t i, ret_val;
Jaroslav Škarvada d61264
 #ifdef MBS_SUPPORT
Jaroslav Škarvada d61264
   int mb_cur_max = MB_CUR_MAX;
Jaroslav Škarvada d61264
@@ -285,6 +287,26 @@
Jaroslav Škarvada d61264
   memset (&mbs, '\0', sizeof (mbstate_t));
Jaroslav Škarvada d61264
 #endif /* MBS_SUPPORT */
Jaroslav Škarvada d61264
 
Jaroslav Škarvada d61264
+  if (!use_dfa_checked)
Jaroslav Škarvada d61264
+    {
Jaroslav Škarvada d61264
+      char *grep_use_dfa = getenv ("GREP_USE_DFA");
Jaroslav Škarvada d61264
+      if (!grep_use_dfa)
Jaroslav Škarvada d61264
+	{
Jaroslav Škarvada d61264
+#ifdef MBS_SUPPORT
Jaroslav Škarvada d61264
+	  /* Turn off DFA when processing multibyte input. */
Jaroslav Škarvada d61264
+	  use_dfa = (MB_CUR_MAX == 1);
Jaroslav Škarvada d61264
+#else
Jaroslav Škarvada d61264
+	  use_dfa = 1;
Jaroslav Škarvada d61264
+#endif /* MBS_SUPPORT */
Jaroslav Škarvada d61264
+	}
Jaroslav Škarvada d61264
+      else
Jaroslav Škarvada d61264
+	{
Jaroslav Škarvada d61264
+	  use_dfa = atoi (grep_use_dfa);
Jaroslav Škarvada d61264
+	}
Jaroslav Škarvada d61264
+
Jaroslav Škarvada d61264
+      use_dfa_checked = 1;
Jaroslav Škarvada d61264
+    }
Jaroslav Škarvada d61264
+
Jaroslav Škarvada d61264
   buflim = buf + size;
Jaroslav Škarvada d61264
 
Jaroslav Škarvada d61264
   for (beg = end = buf; end < buflim; beg = end)
Jaroslav Škarvada d61264
@@ -353,7 +375,8 @@
Jaroslav Škarvada d61264
 #endif /* MBS_SUPPORT */
Jaroslav Škarvada d61264
 		  (kwsm.index < kwset_exact_matches))
Jaroslav Škarvada d61264
 		goto success;
Jaroslav Škarvada d61264
-	      if (dfaexec (&dfa, beg, end - beg, &backref) == (size_t) -1)
Jaroslav Škarvada d61264
+	      if (use_dfa &&
Jaroslav Škarvada d61264
+		  dfaexec (&dfa, beg, end - beg, &backref) == (size_t) -1)
Jaroslav Škarvada d61264
 		continue;
Jaroslav Škarvada d61264
 	    }
Jaroslav Škarvada d61264
 	  else
Jaroslav Škarvada d61264
@@ -362,7 +385,9 @@
Jaroslav Škarvada d61264
 #ifdef MBS_SUPPORT
Jaroslav Škarvada d61264
 	      size_t bytes_left = 0;
Jaroslav Škarvada d61264
 #endif /* MBS_SUPPORT */
Jaroslav Škarvada d61264
-	      size_t offset = dfaexec (&dfa, beg, buflim - beg, &backref);
Jaroslav Škarvada d61264
+	      size_t offset = 0;
Jaroslav Škarvada d61264
+	      if (use_dfa)
Jaroslav Škarvada d61264
+		offset = dfaexec (&dfa, beg, buflim - beg, &backref);
Jaroslav Škarvada d61264
 	      if (offset == (size_t) -1)
Jaroslav Škarvada d61264
 		break;
Jaroslav Škarvada d61264
 	      /* Narrow down to the line we've found. */
Jaroslav Škarvada d61264
@@ -404,7 +429,7 @@
Jaroslav Škarvada d61264
 		--beg;
Jaroslav Škarvada d61264
 	    }
Jaroslav Škarvada d61264
 	  /* Successful, no backreferences encountered! */
Jaroslav Škarvada d61264
-	  if (!backref)
Jaroslav Škarvada d61264
+	  if (use_dfa && !backref)
Jaroslav Škarvada d61264
 	    goto success;
Jaroslav Škarvada d61264
 	}
Jaroslav Škarvada d61264
       else