Tim Waugh e7e163
--- grep-2.5.1/src/search.c	2004-12-21 13:37:15.700555594 +0000
Tim Waugh e7e163
+++ grep-2.5.1/src/search.c	2004-12-21 13:49:05.873811016 +0000
Tim Waugh e7e163
@@ -21,6 +21,7 @@
Tim Waugh e7e163
 #ifdef HAVE_CONFIG_H
Tim Waugh e7e163
 # include <config.h>
Tim Waugh e7e163
 #endif
Tim Waugh e7e163
+#include <assert.h>
Tim Waugh e7e163
 #include <sys/types.h>
Tim Waugh e7e163
 #if defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H && defined HAVE_MBRTOWC
Tim Waugh e7e163
 /* We can handle multibyte string.  */
Tim Waugh e7e163
@@ -39,6 +40,9 @@
Tim Waugh c3fd6f
 #ifdef HAVE_LIBPCRE
Tim Waugh c3fd6f
 # include <pcre.h>
Tim Waugh c3fd6f
 #endif
Tim Waugh c3fd6f
+#ifdef HAVE_LANGINFO_CODESET
Tim Waugh c3fd6f
+# include <langinfo.h>
Tim Waugh c3fd6f
+#endif
Tim Waugh c3fd6f
 
Tim Waugh c3fd6f
 #define NCHAR (UCHAR_MAX + 1)
Tim Waugh c3fd6f
 
Tim Waugh e7e163
@@ -70,9 +74,10 @@
Tim Waugh e1ca94
    call the regexp matcher at all. */
Tim Waugh e1ca94
 static int kwset_exact_matches;
Tim Waugh e1ca94
 
Tim Waugh e1ca94
-#if defined(MBS_SUPPORT)
Tim Waugh e1ca94
-static char* check_multibyte_string PARAMS ((char const *buf, size_t size));
Tim Waugh e1ca94
-#endif
Tim Waugh c3fd6f
+/* UTF-8 encoding allows some optimizations that we can't otherwise
Tim Waugh c3fd6f
+   assume in a multibyte encoding. */
Tim Waugh c3fd6f
+static int using_utf8;
Tim Waugh c3fd6f
+
Tim Waugh e1ca94
 static void kwsinit PARAMS ((void));
Tim Waugh e1ca94
 static void kwsmusts PARAMS ((void));
Tim Waugh e1ca94
 static void Gcompile PARAMS ((char const *, size_t));
Tim Waugh e7e163
@@ -84,6 +89,15 @@
Tim Waugh c3fd6f
 static size_t Pexecute PARAMS ((char const *, size_t, size_t *, int));
Tim Waugh c3fd6f
 
Tim Waugh c3fd6f
 void
Tim Waugh c3fd6f
+check_utf8 (void)
Tim Waugh c3fd6f
+{
Tim Waugh c3fd6f
+#ifdef HAVE_LANGINFO_CODESET
Tim Waugh c3fd6f
+  if (strcmp (nl_langinfo (CODESET), "UTF-8") == 0)
Tim Waugh c3fd6f
+    using_utf8 = 1;
Tim Waugh c3fd6f
+#endif
Tim Waugh c3fd6f
+}
Tim Waugh c3fd6f
+
Tim Waugh c3fd6f
+void
Tim Waugh c3fd6f
 dfaerror (char const *mesg)
Tim Waugh c3fd6f
 {
Tim Waugh c3fd6f
   error (2, 0, mesg);
Tim Waugh e7e163
@@ -141,47 +155,6 @@
Tim Waugh e1ca94
     }
Tim Waugh e1ca94
 }
Tim Waugh e1ca94
 
Tim Waugh e1ca94
-#ifdef MBS_SUPPORT
Tim Waugh e1ca94
-/* This function allocate the array which correspond to "buf".
Tim Waugh e1ca94
-   Then this check multibyte string and mark on the positions which
Tim Waugh e1ca94
-   are not singlebyte character nor the first byte of a multibyte
Tim Waugh e1ca94
-   character.  Caller must free the array.  */
Tim Waugh e1ca94
-static char*
Tim Waugh e1ca94
-check_multibyte_string(char const *buf, size_t size)
Tim Waugh e1ca94
-{
Tim Waugh e1ca94
-  char *mb_properties = xmalloc(size);
Tim Waugh e1ca94
-  mbstate_t cur_state;
Tim Waugh e1ca94
-  wchar_t wc;
Tim Waugh e1ca94
-  int i;
Tim Waugh e1ca94
-  memset(&cur_state, 0, sizeof(mbstate_t));
Tim Waugh e1ca94
-  memset(mb_properties, 0, sizeof(char)*size);
Tim Waugh e1ca94
-  for (i = 0; i < size ;)
Tim Waugh e1ca94
-    {
Tim Waugh e1ca94
-      size_t mbclen;
Tim Waugh e1ca94
-      mbclen = mbrtowc(&wc, buf + i, size - i, &cur_state);
Tim Waugh e1ca94
-
Tim Waugh e1ca94
-      if (mbclen == (size_t) -1 || mbclen == (size_t) -2 || mbclen == 0)
Tim Waugh e1ca94
-	{
Tim Waugh e1ca94
-	  /* An invalid sequence, or a truncated multibyte character.
Tim Waugh e1ca94
-	     We treat it as a singlebyte character.  */
Tim Waugh e1ca94
-	  mbclen = 1;
Tim Waugh e1ca94
-	}
Tim Waugh e1ca94
-      else if (match_icase)
Tim Waugh e1ca94
-	{
Tim Waugh e1ca94
-	  if (iswupper((wint_t)wc))
Tim Waugh e1ca94
-	    {
Tim Waugh e1ca94
-	      wc = towlower((wint_t)wc);
Tim Waugh e1ca94
-	      wcrtomb(buf + i, wc, &cur_state);
Tim Waugh e1ca94
-	    }
Tim Waugh e1ca94
-	}
Tim Waugh e1ca94
-      mb_properties[i] = mbclen;
Tim Waugh e1ca94
-      i += mbclen;
Tim Waugh e1ca94
-    }
Tim Waugh e1ca94
-
Tim Waugh e1ca94
-  return mb_properties;
Tim Waugh e1ca94
-}
Tim Waugh e1ca94
-#endif
Tim Waugh e1ca94
-
Tim Waugh e1ca94
 static void
Tim Waugh e1ca94
 Gcompile (char const *pattern, size_t size)
Tim Waugh e1ca94
 {
Tim Waugh e7e163
@@ -190,6 +163,7 @@
Tim Waugh c3fd6f
   size_t total = size;
Tim Waugh c3fd6f
   char const *motif = pattern;
Tim Waugh c3fd6f
 
Tim Waugh c3fd6f
+  check_utf8 ();
Tim Waugh c3fd6f
   re_set_syntax (RE_SYNTAX_GREP | RE_HAT_LISTS_NOT_NEWLINE | (match_icase ? RE_ICASE : 0));
Tim Waugh c3fd6f
   dfasyntax (RE_SYNTAX_GREP | RE_HAT_LISTS_NOT_NEWLINE, match_icase, eolbyte);
Tim Waugh c3fd6f
 
Tim Waugh e7e163
@@ -266,6 +240,7 @@
Tim Waugh c3fd6f
   size_t total = size;
Tim Waugh c3fd6f
   char const *motif = pattern;
Tim Waugh c3fd6f
 
Tim Waugh c3fd6f
+  check_utf8 ();
Tim Waugh c3fd6f
   if (strcmp (matcher, "awk") == 0)
Tim Waugh c3fd6f
     {
Tim Waugh c3fd6f
       re_set_syntax (RE_SYNTAX_AWK | (match_icase ? RE_ICASE : 0));
Tim Waugh e7e163
@@ -350,18 +325,8 @@
Tim Waugh e1ca94
   struct kwsmatch kwsm;
Tim Waugh e1ca94
   size_t i, ret_val;
Tim Waugh e1ca94
 #ifdef MBS_SUPPORT
Tim Waugh e1ca94
-  char *mb_properties = NULL;
Tim Waugh e1ca94
-  if (MB_CUR_MAX > 1)
Tim Waugh e1ca94
-    {
Tim Waugh e1ca94
-      if (match_icase)
Tim Waugh e1ca94
-        {
Tim Waugh e1ca94
-          char *case_buf = xmalloc(size);
Tim Waugh e1ca94
-          memcpy(case_buf, buf, size);
Tim Waugh e1ca94
-          buf = case_buf;
Tim Waugh e1ca94
-        }
Tim Waugh e1ca94
-      if (kwset)
Tim Waugh e1ca94
-        mb_properties = check_multibyte_string(buf, size);
Tim Waugh e1ca94
-    }
Tim Waugh e1ca94
+  mbstate_t mbs;
Tim Waugh e1ca94
+  memset (&mbs, '\0', sizeof (mbstate_t));
Tim Waugh e1ca94
 #endif /* MBS_SUPPORT */
Tim Waugh e1ca94
 
Tim Waugh e1ca94
   buflim = buf + size;
Tim Waugh e7e163
@@ -373,21 +338,63 @@
Tim Waugh 9fa0ec
 	  if (kwset)
Tim Waugh 9fa0ec
 	    {
Tim Waugh 9fa0ec
 	      /* Find a possible match using the KWset matcher. */
Tim Waugh 8443e1
-	      size_t offset = kwsexec (kwset, beg, buflim - beg, &kwsm);
Tim Waugh 9fa0ec
+#ifdef MBS_SUPPORT
Tim Waugh 9fa0ec
+	      size_t bytes_left = 0;
Tim Waugh 9fa0ec
+#endif /* MBS_SUPPORT */
Tim Waugh 8443e1
+	      size_t offset;
Tim Waugh 8443e1
+#ifdef MBS_SUPPORT
Tim Waugh a5ca95
+	      /* kwsexec doesn't work with match_icase and multibyte input. */
Tim Waugh 8443e1
+	      if (match_icase && MB_CUR_MAX > 1)
Tim Waugh 8443e1
+		/* Avoid kwset */
Tim Waugh 8443e1
+		offset = 0;
Tim Waugh 8443e1
+	      else
Tim Waugh 8443e1
+#endif /* MBS_SUPPORT */
Tim Waugh 8443e1
+	      offset = kwsexec (kwset, beg, buflim - beg, &kwsm);
Tim Waugh e1ca94
 	      if (offset == (size_t) -1)
Tim Waugh e1ca94
 	        goto failure;
Tim Waugh e1ca94
+#ifdef MBS_SUPPORT
Tim Waugh c3fd6f
+	      if (MB_CUR_MAX > 1 && !using_utf8)
Tim Waugh e1ca94
+		{
Tim Waugh 9fa0ec
+		  bytes_left = offset;
Tim Waugh 9fa0ec
+		  while (bytes_left)
Tim Waugh e1ca94
+		    {
Tim Waugh 9fa0ec
+		      size_t len = mbrlen (beg, bytes_left, &mbs);
Tim Waugh 2bb286
+		      if (len == (size_t) -1 || len == 0)
Tim Waugh 6f2e16
+			{
Tim Waugh 2bb286
+			  /* Incomplete character: treat as single-byte. */
Tim Waugh 6f2e16
+			  memset (&mbs, '\0', sizeof (mbstate_t));
Tim Waugh 2bb286
+			  beg++;
Tim Waugh 2bb286
+			  bytes_left--;
Tim Waugh 2bb286
+			  continue;
Tim Waugh 6f2e16
+			}
Tim Waugh e1ca94
+
Tim Waugh 2bb286
+		      if (len == (size_t) -2)
Tim Waugh 2bb286
+			/* Offset points inside multibyte character:
Tim Waugh 2bb286
+			 * no good. */
Tim Waugh 2bb286
+			break;
Tim Waugh 2bb286
+
Tim Waugh 6f2e16
+		      beg += len;
Tim Waugh 9fa0ec
+		      bytes_left -= len;
Tim Waugh e1ca94
+		    }
Tim Waugh e1ca94
+		}
Tim Waugh 6f2e16
+	      else
Tim Waugh 6f2e16
+#endif /* MBS_SUPPORT */
Tim Waugh e1ca94
 	      beg += offset;
Tim Waugh e1ca94
 	      /* Narrow down to the line containing the candidate, and
Tim Waugh e1ca94
 		 run it through DFA. */
Tim Waugh e1ca94
 	      end = memchr(beg, eol, buflim - beg);
Tim Waugh e1ca94
 	      end++;
Tim Waugh 9fa0ec
 #ifdef MBS_SUPPORT
Tim Waugh e1ca94
-	      if (MB_CUR_MAX > 1 && mb_properties[beg - buf] == 0)
Tim Waugh 9fa0ec
+	      if (MB_CUR_MAX > 1 && bytes_left)
Tim Waugh 9fa0ec
 		continue;
Tim Waugh e1ca94
-#endif
Tim Waugh 9fa0ec
+#endif /* MBS_SUPPORT */
Tim Waugh e1ca94
 	      while (beg > buf && beg[-1] != eol)
Tim Waugh e1ca94
 		--beg;
Tim Waugh a5ca95
-	      if (kwsm.index < kwset_exact_matches)
Tim Waugh e5092c
+	      if (
Tim Waugh e5092c
+#ifdef MBS_SUPPORT
Tim Waugh e5092c
+		  !(match_icase && MB_CUR_MAX > 1) &&
Tim Waugh e5092c
+#endif /* MBS_SUPPORT */
Tim Waugh a5ca95
+		  (kwsm.index < kwset_exact_matches))
Tim Waugh a5ca95
 		goto success_in_beg_and_end;
Tim Waugh a5ca95
 	      if (dfaexec (&dfa, beg, end - beg, &backref) == (size_t) -1)
Tim Waugh a5ca95
 		continue;
Tim Waugh e7e163
@@ -395,13 +402,47 @@
Tim Waugh 9fa0ec
 	  else
Tim Waugh 9fa0ec
 	    {
Tim Waugh 9fa0ec
 	      /* No good fixed strings; start with DFA. */
Tim Waugh 9fa0ec
+#ifdef MBS_SUPPORT
Tim Waugh 9fa0ec
+	      size_t bytes_left = 0;
Tim Waugh 9fa0ec
+#endif /* MBS_SUPPORT */
Tim Waugh 9fa0ec
 	      size_t offset = dfaexec (&dfa, beg, buflim - beg, &backref);
Tim Waugh e1ca94
 	      if (offset == (size_t) -1)
Tim Waugh e1ca94
 		break;
Tim Waugh e1ca94
 	      /* Narrow down to the line we've found. */
Tim Waugh e1ca94
+#ifdef MBS_SUPPORT
Tim Waugh c3fd6f
+	      if (MB_CUR_MAX > 1 && !using_utf8)
Tim Waugh e1ca94
+		{
Tim Waugh 9fa0ec
+		  bytes_left = offset;
Tim Waugh 9fa0ec
+		  while (bytes_left)
Tim Waugh e1ca94
+		    {
Tim Waugh 9fa0ec
+		      size_t len = mbrlen (beg, bytes_left, &mbs);
Tim Waugh 2bb286
+		      if (len == (size_t) -1 || len == 0)
Tim Waugh 6f2e16
+			{
Tim Waugh 2bb286
+			  /* Incomplete character: treat as single-byte. */
Tim Waugh 6f2e16
+			  memset (&mbs, '\0', sizeof (mbstate_t));
Tim Waugh 2bb286
+			  beg++;
Tim Waugh 2bb286
+			  bytes_left--;
Tim Waugh 2bb286
+			  continue;
Tim Waugh 6f2e16
+			}
Tim Waugh e1ca94
+
Tim Waugh 2bb286
+		      if (len == (size_t) -2)
Tim Waugh 2bb286
+			/* Offset points inside multibyte character:
Tim Waugh 2bb286
+			 * no good. */
Tim Waugh 2bb286
+			break;
Tim Waugh 2bb286
+
Tim Waugh 6f2e16
+		      beg += len;
Tim Waugh 9fa0ec
+		      bytes_left -= len;
Tim Waugh e1ca94
+		    }
Tim Waugh e1ca94
+		}
Tim Waugh 6f2e16
+	      else
Tim Waugh 6f2e16
+#endif /* MBS_SUPPORT */
Tim Waugh e1ca94
 	      beg += offset;
Tim Waugh e1ca94
 	      end = memchr (beg, eol, buflim - beg);
Tim Waugh e1ca94
 	      end++;
Tim Waugh 9fa0ec
+#ifdef MBS_SUPPORT
Tim Waugh 9fa0ec
+	      if (MB_CUR_MAX > 1 && bytes_left)
Tim Waugh 9fa0ec
+		continue;
Tim Waugh 9fa0ec
+#endif /* MBS_SUPPORT */
Tim Waugh 9fa0ec
 	      while (beg > buf && beg[-1] != eol)
Tim Waugh 9fa0ec
 		--beg;
Tim Waugh 9fa0ec
 	    }
Tim Waugh e7e163
@@ -469,15 +510,6 @@
Tim Waugh e1ca94
     } /* for (beg = end ..) */
Tim Waugh e1ca94
 
Tim Waugh e1ca94
  failure:
Tim Waugh e1ca94
-#ifdef MBS_SUPPORT
Tim Waugh e1ca94
-  if (MB_CUR_MAX > 1)
Tim Waugh e1ca94
-    {
Tim Waugh e1ca94
-      if (mb_properties)
Tim Waugh e1ca94
-	free (mb_properties);
Tim Waugh e1ca94
-      if (match_icase)
Tim Waugh e1ca94
-	free ((char *) buf);
Tim Waugh e1ca94
-    }
Tim Waugh e1ca94
-#endif /* MBS_SUPPORT */
Tim Waugh e1ca94
   return (size_t) -1;
Tim Waugh e1ca94
 
Tim Waugh e1ca94
  success_in_beg_and_end:
Tim Waugh e7e163
@@ -486,24 +518,125 @@
Tim Waugh e1ca94
   /* FALLTHROUGH */
Tim Waugh e1ca94
 
Tim Waugh e1ca94
  success_in_start_and_len:
Tim Waugh e1ca94
-#ifdef MBS_SUPPORT
Tim Waugh e1ca94
-  if (MB_CUR_MAX > 1)
Tim Waugh e1ca94
-    {
Tim Waugh e1ca94
-      if (mb_properties)
Tim Waugh e1ca94
-	free (mb_properties);
Tim Waugh e1ca94
-      if (match_icase)
Tim Waugh e1ca94
-	free ((char *) buf);
Tim Waugh e1ca94
-    }
Tim Waugh e1ca94
-#endif /* MBS_SUPPORT */
Tim Waugh e1ca94
   *match_size = len;
Tim Waugh e1ca94
   return start;
Tim Waugh e1ca94
 }
Tim Waugh e7e163
 
Tim Waugh e7e163
+static wchar_t **f_pattern;
Tim Waugh e7e163
+static char *f_initial_byte;
Tim Waugh e7e163
+static size_t f_pattern_count;
Tim Waugh e7e163
+static int f_i_multibyte; /* whether we're using the new -Fi MB method */
Tim Waugh e7e163
+
Tim Waugh e7e163
 static void
Tim Waugh e7e163
 Fcompile (char const *pattern, size_t size)
Tim Waugh c3fd6f
 {
Tim Waugh c3fd6f
   char const *beg, *lim, *err;
Tim Waugh c3fd6f
 
Tim Waugh c3fd6f
+  check_utf8 ();
Tim Waugh e7e163
+#ifdef MBS_SUPPORT
Tim Waugh e7e163
+  /* Support -F -i for UTF-8 input. */
Tim Waugh e7e163
+  if (match_icase && MB_CUR_MAX > 1)
Tim Waugh e7e163
+    {
Tim Waugh e7e163
+      size_t in = 0;
Tim Waugh e7e163
+
Tim Waugh e7e163
+      while (f_i_multibyte != -1 && in < size)
Tim Waugh e7e163
+	{
Tim Waugh e7e163
+	  wchar_t *f_this_pattern;
Tim Waugh e7e163
+	  size_t f_this_pattern_allocated = sizeof (wchar_t) * 1000;
Tim Waugh e7e163
+	  mbstate_t mbs;
Tim Waugh e7e163
+	  size_t out = 0;
Tim Waugh e7e163
+	  f_pattern_count++;
Tim Waugh e7e163
+	  f_pattern = xrealloc (f_pattern,
Tim Waugh e7e163
+				sizeof (wchar_t *) * f_pattern_count);
Tim Waugh e7e163
+	  f_initial_byte = xrealloc (f_initial_byte,
Tim Waugh e7e163
+				     sizeof (char) *
Tim Waugh e7e163
+				     (2 * f_pattern_count + 1));
Tim Waugh e7e163
+	  if (f_pattern_count == 1)
Tim Waugh e7e163
+	    f_initial_byte[0] = '\0';
Tim Waugh e7e163
+
Tim Waugh e7e163
+	  /* Convert pattern into wchar_t*, storing them in this_pattern.
Tim Waugh e7e163
+	     Don't read more than we're given. */
Tim Waugh e7e163
+	  f_this_pattern = xmalloc (f_this_pattern_allocated);
Tim Waugh e7e163
+	  memset (&mbs, '\0', sizeof (mbs));
Tim Waugh e7e163
+	  while (in < size)
Tim Waugh e7e163
+	    {
Tim Waugh e7e163
+	      size_t c;
Tim Waugh e7e163
+	      wchar_t this_wc;
Tim Waugh e7e163
+	      if (out == f_this_pattern_allocated)
Tim Waugh e7e163
+		{
Tim Waugh e7e163
+		  f_this_pattern_allocated *= 2;
Tim Waugh e7e163
+		  f_this_pattern = xrealloc (f_this_pattern,
Tim Waugh e7e163
+					     f_this_pattern_allocated);
Tim Waugh e7e163
+		}
Tim Waugh e7e163
+
Tim Waugh e7e163
+	      c = mbrtowc (&this_wc, pattern + in, size - in, &mbs);
Tim Waugh e7e163
+	      if (c < 1)
Tim Waugh e7e163
+		{
Tim Waugh e7e163
+		  /* Fall back to old method. */
Tim Waugh e7e163
+		  f_i_multibyte = -1;
Tim Waugh e7e163
+		  while (f_pattern_count--)
Tim Waugh e7e163
+		    free (f_pattern[f_pattern_count]);
Tim Waugh e7e163
+		  free (f_pattern);
Tim Waugh e7e163
+		  f_pattern = NULL;
Tim Waugh e7e163
+		  break;
Tim Waugh e7e163
+		}
Tim Waugh e7e163
+
Tim Waugh e7e163
+	      f_this_pattern[out] = towlower (this_wc);
Tim Waugh e7e163
+	      if (out == 0)
Tim Waugh e7e163
+		{
Tim Waugh e7e163
+		  /* First character.  Work out the first byte of upper and
Tim Waugh e7e163
+		     lower case multibyte strings for the first character. */
Tim Waugh e7e163
+		  wchar_t wc;
Tim Waugh e7e163
+		  char mbs[MB_CUR_MAX];
Tim Waugh e7e163
+		  mbstate_t ps;
Tim Waugh e7e163
+
Tim Waugh e7e163
+		  if (iswupper (this_wc))
Tim Waugh e7e163
+		    {
Tim Waugh e7e163
+		      wc = towlower (this_wc);
Tim Waugh e7e163
+		    }
Tim Waugh e7e163
+		  else
Tim Waugh e7e163
+		    {
Tim Waugh e7e163
+		      wc = towupper (this_wc);
Tim Waugh e7e163
+		    }
Tim Waugh e7e163
+
Tim Waugh e7e163
+		  memset (&ps, '\0', sizeof (ps));
Tim Waugh e7e163
+		  wcrtomb (mbs, this_wc, &ps);
Tim Waugh e7e163
+		  mbs[1] = '\0';
Tim Waugh e7e163
+		  strcat (f_initial_byte, mbs);
Tim Waugh e7e163
+
Tim Waugh e7e163
+		  memset (&ps, '\0', sizeof (ps));
Tim Waugh e7e163
+		  wcrtomb (mbs, wc, &ps);
Tim Waugh e7e163
+		  mbs[1] = '\0';
Tim Waugh e7e163
+		  strcat (f_initial_byte, mbs);
Tim Waugh e7e163
+		}
Tim Waugh e7e163
+
Tim Waugh e7e163
+	      in += c;
Tim Waugh e7e163
+
Tim Waugh e7e163
+	      if (this_wc == L'\n')
Tim Waugh e7e163
+		break;
Tim Waugh e7e163
+
Tim Waugh e7e163
+	      out++;
Tim Waugh e7e163
+	    }
Tim Waugh e7e163
+
Tim Waugh e7e163
+	  if (f_i_multibyte == -1)
Tim Waugh e7e163
+	    break;
Tim Waugh e7e163
+
Tim Waugh e7e163
+	  /* Nul-terminate it. */
Tim Waugh e7e163
+	  if (out == f_this_pattern_allocated)
Tim Waugh e7e163
+	    {
Tim Waugh e7e163
+	      f_this_pattern_allocated++;
Tim Waugh e7e163
+	      f_this_pattern = xrealloc (f_this_pattern,
Tim Waugh e7e163
+					 f_this_pattern_allocated);
Tim Waugh e7e163
+	    }
Tim Waugh e7e163
+
Tim Waugh e7e163
+	  f_this_pattern[out] = L'\0';
Tim Waugh e7e163
+	  f_pattern[f_pattern_count - 1] = f_this_pattern;
Tim Waugh e7e163
+	  f_i_multibyte = 1;
Tim Waugh e7e163
+	}
Tim Waugh e7e163
+    }
Tim Waugh e7e163
+#endif /* MBS_SUPPORT */
Tim Waugh e7e163
+
Tim Waugh e7e163
+
Tim Waugh c3fd6f
   kwsinit ();
Tim Waugh c3fd6f
   beg = pattern;
Tim Waugh c3fd6f
   do
Tim Waugh e7e163
@@ -523,6 +656,87 @@
Tim Waugh e7e163
 }
Tim Waugh e7e163
 
Tim Waugh e7e163
 static size_t
Tim Waugh e7e163
+Fimbexec (const char *buf, size_t size, size_t *plen)
Tim Waugh e7e163
+{
Tim Waugh e7e163
+  char const *beg;
Tim Waugh e7e163
+  size_t len;
Tim Waugh e7e163
+  mbstate_t mbs;
Tim Waugh e7e163
+
Tim Waugh e7e163
+  assert (match_icase && f_i_multibyte == 1);
Tim Waugh e7e163
+  assert (MB_CUR_MAX > 1);
Tim Waugh e7e163
+
Tim Waugh e7e163
+  memset (&mbs, '\0', sizeof (mbs));
Tim Waugh e7e163
+  beg = buf;
Tim Waugh e7e163
+  len = 0;
Tim Waugh e7e163
+  while (beg < buf + size)
Tim Waugh e7e163
+    {
Tim Waugh e7e163
+      wchar_t wc;
Tim Waugh e7e163
+      char const *p;
Tim Waugh e7e163
+      char const *next_char;
Tim Waugh e7e163
+      unsigned char match[f_pattern_count];
Tim Waugh e7e163
+      size_t i, letter;
Tim Waugh e7e163
+      int patterns_left;
Tim Waugh e7e163
+
Tim Waugh e7e163
+      for (p = beg;
Tim Waugh e7e163
+	   (p < buf + size) && !strchr (f_initial_byte, *p);
Tim Waugh e7e163
+	   p++)
Tim Waugh e7e163
+	;
Tim Waugh e7e163
+
Tim Waugh e7e163
+      if (p == NULL || p == buf + size)
Tim Waugh e7e163
+	break;
Tim Waugh e7e163
+
Tim Waugh e7e163
+      /* First byte matches, now check the rest */
Tim Waugh e7e163
+      beg = p;
Tim Waugh e7e163
+      letter = len = 0;
Tim Waugh e7e163
+      memset (match, '\1', f_pattern_count);
Tim Waugh e7e163
+      patterns_left = 1;
Tim Waugh e7e163
+      while (patterns_left)
Tim Waugh e7e163
+	{
Tim Waugh e7e163
+	  size_t c;
Tim Waugh e7e163
+
Tim Waugh e7e163
+	  patterns_left = 0;
Tim Waugh e7e163
+
Tim Waugh e7e163
+	  c = mbrtowc (&wc, beg + len, size - (beg - buf) - len, &mbs);
Tim Waugh e7e163
+	  if (c < 1)
Tim Waugh e7e163
+	    {
Tim Waugh e7e163
+	      memset (&mbs, '\0', sizeof (mbs));
Tim Waugh e7e163
+	      next_char = beg + 1;
Tim Waugh e7e163
+	      break;
Tim Waugh e7e163
+	    }
Tim Waugh e7e163
+
Tim Waugh e7e163
+	  if (!len)
Tim Waugh e7e163
+	    next_char = beg + c;
Tim Waugh e7e163
+
Tim Waugh e7e163
+	  wc = towlower (wc);
Tim Waugh e7e163
+	  for (i = 0; i < f_pattern_count; i++)
Tim Waugh e7e163
+	    {
Tim Waugh e7e163
+	      if (match[i])
Tim Waugh e7e163
+		{
Tim Waugh e7e163
+		  if (f_pattern[i][letter] == L'\0')
Tim Waugh e7e163
+		    {
Tim Waugh e7e163
+		      /* Found a match. */
Tim Waugh e7e163
+		      *plen = len;
Tim Waugh e7e163
+		      return beg - buf;
Tim Waugh e7e163
+		    }
Tim Waugh e7e163
+
Tim Waugh e7e163
+		  if (f_pattern[i][letter] == wc)
Tim Waugh e7e163
+		    patterns_left = 1;
Tim Waugh e7e163
+		  else
Tim Waugh e7e163
+		    match[i] = '\0';
Tim Waugh e7e163
+		}
Tim Waugh e7e163
+	    }
Tim Waugh e7e163
+
Tim Waugh e7e163
+	  len += c;
Tim Waugh e7e163
+	  letter++;
Tim Waugh e7e163
+	}
Tim Waugh e7e163
+
Tim Waugh e7e163
+      beg = next_char;
Tim Waugh e7e163
+    }
Tim Waugh e7e163
+      
Tim Waugh e7e163
+  return -1;
Tim Waugh e7e163
+}
Tim Waugh e7e163
+
Tim Waugh e7e163
+static size_t
Tim Waugh e7e163
 Fexecute (char const *buf, size_t size, size_t *match_size, int exact)
Tim Waugh e7e163
 {
Tim Waugh e7e163
   register char const *beg, *try, *end;
Tim Waugh e7e163
@@ -531,27 +745,50 @@
Tim Waugh e1ca94
   struct kwsmatch kwsmatch;
Tim Waugh e1ca94
   size_t ret_val;
Tim Waugh e1ca94
 #ifdef MBS_SUPPORT
Tim Waugh e1ca94
-  char *mb_properties = NULL;
Tim Waugh e1ca94
-  if (MB_CUR_MAX > 1)
Tim Waugh e1ca94
-    {
Tim Waugh e1ca94
-      if (match_icase)
Tim Waugh e1ca94
-        {
Tim Waugh e1ca94
-          char *case_buf = xmalloc(size);
Tim Waugh e1ca94
-          memcpy(case_buf, buf, size);
Tim Waugh e1ca94
-          buf = case_buf;
Tim Waugh e1ca94
-        }
Tim Waugh e1ca94
-      mb_properties = check_multibyte_string(buf, size);
Tim Waugh e1ca94
-    }
Tim Waugh e1ca94
+  mbstate_t mbs;
Tim Waugh e1ca94
+  memset (&mbs, '\0', sizeof (mbstate_t));
Tim Waugh e1ca94
 #endif /* MBS_SUPPORT */
Tim Waugh e1ca94
 
Tim Waugh e1ca94
   for (beg = buf; beg <= buf + size; ++beg)
Tim Waugh e7e163
     {
Tim Waugh e7e163
-      size_t offset = kwsexec (kwset, beg, buf + size - beg, &kwsmatch);
Tim Waugh e7e163
+      size_t offset;
Tim Waugh e7e163
+#ifdef MBS_SUPPORT
Tim Waugh e7e163
+      if (match_icase && f_i_multibyte == 1)
Tim Waugh e7e163
+	offset = Fimbexec (beg, buf + size - beg, &kwsmatch.size[0]);
Tim Waugh e7e163
+      else
Tim Waugh e7e163
+#endif /* MBS_SUPPORT */
Tim Waugh e7e163
+      offset = kwsexec (kwset, beg, buf + size - beg, &kwsmatch);
Tim Waugh e7e163
+
Tim Waugh e1ca94
       if (offset == (size_t) -1)
Tim Waugh e1ca94
 	goto failure;
Tim Waugh e1ca94
 #ifdef MBS_SUPPORT
Tim Waugh e1ca94
-      if (MB_CUR_MAX > 1 && mb_properties[offset+beg-buf] == 0)
Tim Waugh e1ca94
-	continue; /* It is a part of multibyte character.  */
Tim Waugh c3fd6f
+      if (MB_CUR_MAX > 1 && !using_utf8)
Tim Waugh e1ca94
+	{
Tim Waugh 9fa0ec
+	  size_t bytes_left = offset;
Tim Waugh 9fa0ec
+	  while (bytes_left)
Tim Waugh e1ca94
+	    {
Tim Waugh 9fa0ec
+	      size_t len = mbrlen (beg, bytes_left, &mbs);
Tim Waugh 2bb286
+	      if (len == (size_t) -1 || len == 0)
Tim Waugh 6f2e16
+		{
Tim Waugh 2bb286
+		  /* Incomplete character: treat as single-byte. */
Tim Waugh 6f2e16
+		  memset (&mbs, '\0', sizeof (mbstate_t));
Tim Waugh 2bb286
+		  beg++;
Tim Waugh 2bb286
+		  bytes_left--;
Tim Waugh 2bb286
+		  continue;
Tim Waugh 6f2e16
+		}
Tim Waugh 6f2e16
+
Tim Waugh 2bb286
+	      if (len == (size_t) -2)
Tim Waugh 2bb286
+		/* Offset points inside multibyte character: no good. */
Tim Waugh 2bb286
+		break;
Tim Waugh 2bb286
+
Tim Waugh 6f2e16
+	      beg += len;
Tim Waugh 9fa0ec
+	      bytes_left -= len;
Tim Waugh e1ca94
+	    }
Tim Waugh e1ca94
+
Tim Waugh 9fa0ec
+	  if (bytes_left)
Tim Waugh 6f2e16
+	    continue;
Tim Waugh e1ca94
+	}
Tim Waugh 6f2e16
+      else
Tim Waugh 6f2e16
 #endif /* MBS_SUPPORT */
Tim Waugh e1ca94
       beg += offset;
Tim Waugh e1ca94
       len = kwsmatch.size[0];
Tim Waugh e7e163
@@ -583,10 +820,46 @@
Tim Waugh e7e163
 	        {
Tim Waugh e7e163
 	          /* Try a shorter length anchored at the same place. */
Tim Waugh e7e163
 	          --len;
Tim Waugh e7e163
+#ifdef MBS_SUPPORT
Tim Waugh e7e163
+		  if (match_icase && f_i_multibyte == 1)
Tim Waugh e7e163
+		    offset = Fimbexec (beg, len, &kwsmatch.size[0]);
Tim Waugh e7e163
+		  else
Tim Waugh e7e163
+#endif /* MBS_SUPPORT */
Tim Waugh e7e163
 	          offset = kwsexec (kwset, beg, len, &kwsmatch);
Tim Waugh e7e163
+
Tim Waugh e1ca94
 	          if (offset == -1) {
Tim Waugh e1ca94
 	            break; /* Try a different anchor. */
Tim Waugh e1ca94
 	          }
Tim Waugh e1ca94
+#ifdef MBS_SUPPORT
Tim Waugh c3fd6f
+		  if (MB_CUR_MAX > 1 && !using_utf8)
Tim Waugh e1ca94
+		    {
Tim Waugh 9fa0ec
+		      size_t bytes_left = offset;
Tim Waugh 9fa0ec
+		      while (bytes_left)
Tim Waugh e1ca94
+			{
Tim Waugh 9fa0ec
+			  size_t len = mbrlen (beg, bytes_left, &mbs);
Tim Waugh 2bb286
+			  if (len == (size_t) -1 || len == 0)
Tim Waugh 6f2e16
+			    {
Tim Waugh 2bb286
+			      /* Incomplete character: treat as single-byte. */
Tim Waugh 6f2e16
+			      memset (&mbs, '\0', sizeof (mbstate_t));
Tim Waugh 2bb286
+			      beg++;
Tim Waugh 2bb286
+			      bytes_left--;
Tim Waugh 2bb286
+			      continue;
Tim Waugh 6f2e16
+			    }
Tim Waugh 6f2e16
+
Tim Waugh 2bb286
+			  if (len == (size_t) -2)
Tim Waugh 2bb286
+			    /* Offset points inside multibyte character:
Tim Waugh 2bb286
+			     * no good. */
Tim Waugh 2bb286
+			    break;
Tim Waugh 2bb286
+
Tim Waugh 6f2e16
+			  beg += len;
Tim Waugh 9fa0ec
+			  bytes_left -= len;
Tim Waugh e1ca94
+			}
Tim Waugh e1ca94
+
Tim Waugh 9fa0ec
+		      if (bytes_left)
Tim Waugh 6f2e16
+			break; /* Try a different anchor. */
Tim Waugh e1ca94
+		    }
Tim Waugh 6f2e16
+		  else
Tim Waugh e1ca94
+#endif /* MBS_SUPPORT */
Tim Waugh 6f2e16
 	          beg += offset;
Tim Waugh e1ca94
 	          len = kwsmatch.size[0];
Tim Waugh e1ca94
 	        }
Tim Waugh e7e163
@@ -597,19 +870,31 @@
Tim Waugh e1ca94
     }
Tim Waugh e1ca94
 
Tim Waugh e1ca94
  failure:
Tim Waugh c3fd6f
+  return -1;
Tim Waugh c3fd6f
+
Tim Waugh c3fd6f
+ success:
Tim Waugh c3fd6f
 #ifdef MBS_SUPPORT
Tim Waugh e1ca94
-  if (MB_CUR_MAX > 1)
Tim Waugh c3fd6f
+  if (MB_CUR_MAX > 1 && !using_utf8)
Tim Waugh c3fd6f
     {
Tim Waugh e1ca94
-      if (match_icase)
Tim Waugh e1ca94
-        free((char *) buf);
Tim Waugh e1ca94
-      if (mb_properties)
Tim Waugh e1ca94
-        free(mb_properties);
Tim Waugh c3fd6f
+      end = beg + len;
Tim Waugh c3fd6f
+      while (end < buf + size)
Tim Waugh e1ca94
+	{
Tim Waugh c3fd6f
+	  size_t len = mbrlen (end, buf + size - end, &mbs);
Tim Waugh c3fd6f
+	  if (len == (size_t) -1 || len == (size_t) -2 || len == 0)
Tim Waugh c3fd6f
+	    {
Tim Waugh c3fd6f
+	      memset (&mbs, '\0', sizeof (mbstate_t));
Tim Waugh c3fd6f
+	      len = 1;
Tim Waugh c3fd6f
+	    }
Tim Waugh c3fd6f
+	  if (len == 1 && *end == eol)
Tim Waugh c3fd6f
+	    break;
Tim Waugh e1ca94
+
Tim Waugh c3fd6f
+	  end += len;
Tim Waugh c3fd6f
+	}
Tim Waugh c3fd6f
     }
Tim Waugh c3fd6f
+  else
Tim Waugh c3fd6f
 #endif /* MBS_SUPPORT */
Tim Waugh c3fd6f
-  return -1;
Tim Waugh c3fd6f
-
Tim Waugh c3fd6f
- success:
Tim Waugh e1ca94
   end = memchr (beg + len, eol, (buf + size) - (beg + len));
Tim Waugh c3fd6f
+
Tim Waugh e1ca94
   end++;
Tim Waugh e1ca94
   while (buf < beg && beg[-1] != eol)
Tim Waugh e1ca94
     --beg;
Tim Waugh e7e163
@@ -618,15 +903,6 @@
Tim Waugh e1ca94
 
Tim Waugh e1ca94
  success_in_beg_and_len:
Tim Waugh e1ca94
   *match_size = len;
Tim Waugh e1ca94
-#ifdef MBS_SUPPORT
Tim Waugh e1ca94
-  if (MB_CUR_MAX > 1)
Tim Waugh e1ca94
-    {
Tim Waugh e1ca94
-      if (mb_properties)
Tim Waugh e1ca94
-	free (mb_properties);
Tim Waugh e1ca94
-      if (match_icase)
Tim Waugh e1ca94
-	free ((char *) buf);
Tim Waugh e1ca94
-    }
Tim Waugh e1ca94
-#endif /* MBS_SUPPORT */
Tim Waugh e1ca94
   return beg - buf;
Tim Waugh e1ca94
 }
Tim Waugh e1ca94