Tim Waugh 8443e1
--- grep-2.5.1/src/search.c	2004-12-14 15:55:21.257729918 +0000
Tim Waugh 8443e1
+++ grep-2.5.1/src/search.c	2004-12-16 16:53:01.110921088 +0000
Tim Waugh c3fd6f
@@ -39,6 +39,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 c3fd6f
@@ -70,9 +73,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 c3fd6f
@@ -84,6 +88,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 c3fd6f
@@ -141,47 +154,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 c3fd6f
@@ -190,6 +162,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 c3fd6f
@@ -266,6 +239,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 c3fd6f
@@ -350,18 +324,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 8443e1
@@ -373,18 +337,56 @@
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
+	      /* kwsexec doesn't work with match_icase and multibyte input. */
Tim Waugh 8443e1
+#ifdef MBS_SUPPORT
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 e1ca94
 	      if (kwsm.index < kwset_exact_matches)
Tim Waugh 8443e1
@@ -395,13 +397,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 8443e1
@@ -469,15 +505,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 8443e1
@@ -486,15 +513,6 @@
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 8443e1
@@ -504,6 +522,7 @@
Tim Waugh c3fd6f
 {
Tim Waugh c3fd6f
   char const *beg, *lim, *err;
Tim Waugh c3fd6f
 
Tim Waugh c3fd6f
+  check_utf8 ();
Tim Waugh c3fd6f
   kwsinit ();
Tim Waugh c3fd6f
   beg = pattern;
Tim Waugh c3fd6f
   do
Tim Waugh 8443e1
@@ -531,17 +550,8 @@
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 8443e1
@@ -550,8 +560,33 @@
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 8443e1
@@ -587,6 +622,36 @@
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 8443e1
@@ -597,19 +662,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 8443e1
@@ -618,15 +695,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