cvsdist 51c509
--- grep-2.5.1/src/dfa.c	2004-02-26 13:09:54.000000000 +0000
cvsdist 51c509
+++ grep-2.5.1/src/dfa.c	2004-05-18 16:43:31.189200479 +0100
cvsdist 732ac9
@@ -414,7 +414,7 @@
cvsdist 732ac9
 
cvsdist 732ac9
 /* This function fetch a wide character, and update cur_mb_len,
cvsdist 732ac9
    used only if the current locale is a multibyte environment.  */
cvsdist 732ac9
-static wchar_t
cvsdist 732ac9
+static wint_t
cvsdist 732ac9
 fetch_wc (char const *eoferr)
cvsdist 732ac9
 {
cvsdist 732ac9
   wchar_t wc;
cvsdist 732ac9
@@ -423,7 +423,7 @@
cvsdist 732ac9
       if (eoferr != 0)
cvsdist 732ac9
 	dfaerror (eoferr);
cvsdist 732ac9
       else
cvsdist 732ac9
-	return -1;
cvsdist 732ac9
+	return WEOF;
cvsdist 732ac9
     }
cvsdist 732ac9
 
cvsdist 732ac9
   cur_mb_len = mbrtowc(&wc, lexptr, lexleft, &mbs);
cvsdist 732ac9
@@ -459,7 +459,7 @@
cvsdist 732ac9
 static void
cvsdist 732ac9
 parse_bracket_exp_mb ()
cvsdist 732ac9
 {
cvsdist 732ac9
-  wchar_t wc, wc1, wc2;
cvsdist 732ac9
+  wint_t wc, wc1, wc2;
cvsdist 732ac9
 
cvsdist 732ac9
   /* Work area to build a mb_char_classes.  */
cvsdist 732ac9
   struct mb_char_classes *work_mbc;
cvsdist 732ac9
@@ -496,7 +496,7 @@
cvsdist 732ac9
     work_mbc->invert = 0;
cvsdist 732ac9
   do
cvsdist 732ac9
     {
cvsdist 732ac9
-      wc1 = -1; /* mark wc1 is not initialized".  */
cvsdist 732ac9
+      wc1 = WEOF; /* mark wc1 is not initialized".  */
cvsdist 732ac9
 
cvsdist 732ac9
       /* Note that if we're looking at some other [:...:] construct,
cvsdist 732ac9
 	 we just treat it as a bunch of ordinary characters.  We can do
cvsdist 732ac9
@@ -586,7 +586,7 @@
cvsdist 732ac9
 		      work_mbc->coll_elems[work_mbc->ncoll_elems++] = elem;
cvsdist 732ac9
 		    }
cvsdist 732ac9
  		}
cvsdist 51c509
-	      wc1 = wc = -1;
cvsdist 51c509
+	      wc1 = wc = WEOF;
cvsdist 732ac9
 	    }
cvsdist 732ac9
 	  else
cvsdist 732ac9
 	    /* We treat '[' as a normal character here.  */
cvsdist 732ac9
@@ -600,7 +600,7 @@
cvsdist 732ac9
 	    wc = fetch_wc(("Unbalanced ["));
cvsdist 732ac9
 	}
cvsdist 732ac9
 
cvsdist 732ac9
-      if (wc1 == -1)
cvsdist 732ac9
+      if (wc1 == WEOF)
cvsdist 732ac9
 	wc1 = fetch_wc(_("Unbalanced ["));
cvsdist 732ac9
 
cvsdist 732ac9
       if (wc1 == L'-')
cvsdist 732ac9
@@ -630,17 +630,17 @@
cvsdist 732ac9
 	    }
cvsdist 732ac9
 	  REALLOC_IF_NECESSARY(work_mbc->range_sts, wchar_t,
cvsdist 732ac9
 			       range_sts_al, work_mbc->nranges + 1);
cvsdist 732ac9
-	  work_mbc->range_sts[work_mbc->nranges] = wc;
cvsdist 732ac9
+	  work_mbc->range_sts[work_mbc->nranges] = (wchar_t)wc;
cvsdist 732ac9
 	  REALLOC_IF_NECESSARY(work_mbc->range_ends, wchar_t,
cvsdist 732ac9
 			       range_ends_al, work_mbc->nranges + 1);
cvsdist 732ac9
-	  work_mbc->range_ends[work_mbc->nranges++] = wc2;
cvsdist 732ac9
+	  work_mbc->range_ends[work_mbc->nranges++] = (wchar_t)wc2;
cvsdist 732ac9
 	}
cvsdist 732ac9
-      else if (wc != -1)
cvsdist 732ac9
+      else if (wc != WEOF)
cvsdist 732ac9
 	/* build normal characters.  */
cvsdist 732ac9
 	{
cvsdist 732ac9
 	  REALLOC_IF_NECESSARY(work_mbc->chars, wchar_t, chars_al,
cvsdist 732ac9
 			       work_mbc->nchars + 1);
cvsdist 732ac9
-	  work_mbc->chars[work_mbc->nchars++] = wc;
cvsdist 732ac9
+	  work_mbc->chars[work_mbc->nchars++] = (wchar_t)wc;
cvsdist 732ac9
 	}
cvsdist 732ac9
     }
cvsdist 732ac9
   while ((wc = wc1) != L']');
cvsdist 51c509
@@ -2552,6 +2552,8 @@
cvsdist 51c509
     }
cvsdist 51c509
 
cvsdist 51c509
   /* match with a character?  */
cvsdist 51c509
+  if (case_fold)
cvsdist 51c509
+    wc = towlower (wc);
cvsdist 51c509
   for (i = 0; i<work_mbc->nchars; i++)
cvsdist 51c509
     {
cvsdist 51c509
       if (wc == work_mbc->chars[i])
cvsdist ac51d8
--- grep-2.5.1/src/grep.c.i18n	2002-03-26 15:54:12.000000000 +0000
cvsdist ac51d8
+++ grep-2.5.1/src/grep.c	2004-02-26 13:09:54.000000000 +0000
cvsdist 732ac9
@@ -30,6 +30,12 @@
cvsdist 732ac9
 # include <sys/time.h>
cvsdist 732ac9
 # include <sys/resource.h>
cvsdist 732ac9
 #endif
cvsdist 732ac9
+#if defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H && defined HAVE_MBRTOWC
cvsdist 732ac9
+/* We can handle multibyte string.  */
cvsdist 732ac9
+# define MBS_SUPPORT
cvsdist 732ac9
+# include <wchar.h>
cvsdist 732ac9
+# include <wctype.h>
cvsdist 732ac9
+#endif
cvsdist 732ac9
 #include <stdio.h>
cvsdist 732ac9
 #include "system.h"
cvsdist 732ac9
 #include "getopt.h"
cvsdist 732ac9
@@ -1697,6 +1703,37 @@
cvsdist 732ac9
   if (!install_matcher (matcher) && !install_matcher ("default"))
cvsdist 732ac9
     abort ();
cvsdist 732ac9
 
cvsdist 732ac9
+#ifdef MBS_SUPPORT
cvsdist 732ac9
+  if (MB_CUR_MAX != 1 && match_icase)
cvsdist 732ac9
+    {
cvsdist 732ac9
+      wchar_t wc;
cvsdist 732ac9
+      mbstate_t cur_state, prev_state;
cvsdist 732ac9
+      int i, len = strlen(keys);
cvsdist 732ac9
+
cvsdist 732ac9
+      memset(&cur_state, 0, sizeof(mbstate_t));
cvsdist 732ac9
+      for (i = 0; i <= len ;)
cvsdist 732ac9
+	{
cvsdist 732ac9
+	  size_t mbclen;
cvsdist 732ac9
+	  mbclen = mbrtowc(&wc, keys + i, len - i, &cur_state);
cvsdist 732ac9
+	  if (mbclen == (size_t) -1 || mbclen == (size_t) -2 || mbclen == 0)
cvsdist 732ac9
+	    {
cvsdist 732ac9
+	      /* An invalid sequence, or a truncated multibyte character.
cvsdist 732ac9
+		 We treat it as a singlebyte character.  */
cvsdist 732ac9
+	      mbclen = 1;
cvsdist 732ac9
+	    }
cvsdist 732ac9
+	  else
cvsdist 732ac9
+	    {
cvsdist 732ac9
+	      if (iswupper((wint_t)wc))
cvsdist 732ac9
+		{
cvsdist 732ac9
+		  wc = towlower((wint_t)wc);
cvsdist 732ac9
+		  wcrtomb(keys + i, wc, &cur_state);
cvsdist 732ac9
+		}
cvsdist 732ac9
+	    }
cvsdist 732ac9
+	  i += mbclen;
cvsdist 732ac9
+	}
cvsdist 732ac9
+    }
cvsdist 732ac9
+#endif /* MBS_SUPPORT */
cvsdist 732ac9
+
cvsdist 732ac9
   (*compile)(keys, keycc);
cvsdist 732ac9
 
cvsdist 732ac9
   if ((argc - optind > 1 && !no_filenames) || with_filenames)
cvsdist ac51d8
--- grep-2.5.1/src/search.c.i18n	2004-02-26 13:09:54.000000000 +0000
cvsdist ac51d8
+++ grep-2.5.1/src/search.c	2004-02-26 13:17:12.000000000 +0000
cvsdist 732ac9
@@ -149,15 +149,16 @@
cvsdist 732ac9
 static char*
cvsdist 732ac9
 check_multibyte_string(char const *buf, size_t size)
cvsdist 732ac9
 {
cvsdist 732ac9
-  char *mb_properties = malloc(size);
cvsdist 732ac9
+  char *mb_properties = xmalloc(size);
cvsdist 732ac9
   mbstate_t cur_state;
cvsdist 732ac9
+  wchar_t wc;
cvsdist 732ac9
   int i;
cvsdist 732ac9
   memset(&cur_state, 0, sizeof(mbstate_t));
cvsdist 732ac9
   memset(mb_properties, 0, sizeof(char)*size);
cvsdist 732ac9
   for (i = 0; i < size ;)
cvsdist 732ac9
     {
cvsdist 732ac9
       size_t mbclen;
cvsdist 732ac9
-      mbclen = mbrlen(buf + i, size - i, &cur_state);
cvsdist 732ac9
+      mbclen = mbrtowc(&wc, buf + i, size - i, &cur_state);
cvsdist 732ac9
 
cvsdist 732ac9
       if (mbclen == (size_t) -1 || mbclen == (size_t) -2 || mbclen == 0)
cvsdist 732ac9
 	{
cvsdist 732ac9
@@ -165,6 +166,14 @@
cvsdist 732ac9
 	     We treat it as a singlebyte character.  */
cvsdist 732ac9
 	  mbclen = 1;
cvsdist 732ac9
 	}
cvsdist 732ac9
+      else if (match_icase)
cvsdist 732ac9
+	{
cvsdist 732ac9
+	  if (iswupper((wint_t)wc))
cvsdist 732ac9
+	    {
cvsdist 732ac9
+	      wc = towlower((wint_t)wc);
cvsdist 732ac9
+	      wcrtomb(buf + i, wc, &cur_state);
cvsdist 732ac9
+	    }
cvsdist 732ac9
+	}
cvsdist 732ac9
       mb_properties[i] = mbclen;
cvsdist 732ac9
       i += mbclen;
cvsdist 732ac9
     }
cvsdist 732ac9
@@ -233,7 +242,7 @@
cvsdist 732ac9
       static char const line_end[] = "\\)$";
cvsdist 732ac9
       static char const word_beg[] = "\\(^\\|[^[:alnum:]_]\\)\\(";
cvsdist 732ac9
       static char const word_end[] = "\\)\\([^[:alnum:]_]\\|$\\)";
cvsdist 732ac9
-      char *n = malloc (sizeof word_beg - 1 + size + sizeof word_end);
cvsdist 732ac9
+      char *n = xmalloc (sizeof word_beg - 1 + size + sizeof word_end);
cvsdist 732ac9
       size_t i;
cvsdist 732ac9
       strcpy (n, match_lines ? line_beg : word_beg);
cvsdist 732ac9
       i = strlen (n);
cvsdist 732ac9
@@ -316,7 +325,7 @@
cvsdist 732ac9
       static char const line_end[] = ")$";
cvsdist 732ac9
       static char const word_beg[] = "(^|[^[:alnum:]_])(";
cvsdist 732ac9
       static char const word_end[] = ")([^[:alnum:]_]|$)";
cvsdist 732ac9
-      char *n = malloc (sizeof word_beg - 1 + size + sizeof word_end);
cvsdist 732ac9
+      char *n = xmalloc (sizeof word_beg - 1 + size + sizeof word_end);
cvsdist 732ac9
       size_t i;
cvsdist 732ac9
       strcpy (n, match_lines ? line_beg : word_beg);
cvsdist 732ac9
       i = strlen(n);
cvsdist 732ac9
@@ -339,14 +348,20 @@
cvsdist 732ac9
   char eol = eolbyte;
cvsdist 732ac9
   int backref, start, len;
cvsdist 732ac9
   struct kwsmatch kwsm;
cvsdist 732ac9
-  size_t i;
cvsdist 732ac9
+  size_t i, ret_val;
cvsdist 732ac9
 #ifdef MBS_SUPPORT
cvsdist 732ac9
   char *mb_properties = NULL;
cvsdist 732ac9
-#endif /* MBS_SUPPORT */
cvsdist 732ac9
-
cvsdist 732ac9
-#ifdef MBS_SUPPORT
cvsdist 732ac9
-  if (MB_CUR_MAX > 1 && kwset)
cvsdist 732ac9
-    mb_properties = check_multibyte_string(buf, size);
cvsdist 732ac9
+  if (MB_CUR_MAX > 1)
cvsdist 732ac9
+    {
cvsdist 732ac9
+      if (match_icase)
cvsdist 732ac9
+        {
cvsdist 732ac9
+          char *case_buf = xmalloc(size);
cvsdist 732ac9
+          memcpy(case_buf, buf, size);
cvsdist 732ac9
+          buf = case_buf;
cvsdist 732ac9
+        }
cvsdist 732ac9
+      if (kwset)
cvsdist 732ac9
+        mb_properties = check_multibyte_string(buf, size);
cvsdist 732ac9
+    }
cvsdist 732ac9
 #endif /* MBS_SUPPORT */
cvsdist 732ac9
 
cvsdist 732ac9
   buflim = buf + size;
cvsdist ac51d8
@@ -455,8 +470,13 @@
cvsdist ac51d8
 
cvsdist ac51d8
  failure:
cvsdist 732ac9
 #ifdef MBS_SUPPORT
cvsdist 732ac9
-  if (MB_CUR_MAX > 1 && mb_properties)
cvsdist 732ac9
-    free (mb_properties);
cvsdist 732ac9
+  if (MB_CUR_MAX > 1)
cvsdist 732ac9
+    {
cvsdist 732ac9
+      if (mb_properties)
cvsdist ac51d8
+	free (mb_properties);
cvsdist ac51d8
+      if (match_icase)
cvsdist ac51d8
+	free ((char *) buf);
cvsdist 732ac9
+    }
cvsdist 732ac9
 #endif /* MBS_SUPPORT */
cvsdist 732ac9
   return (size_t) -1;
cvsdist 732ac9
 
cvsdist ac51d8
@@ -467,8 +487,13 @@
cvsdist ac51d8
 
cvsdist ac51d8
  success_in_start_and_len:
cvsdist 732ac9
 #ifdef MBS_SUPPORT
cvsdist 732ac9
-  if (MB_CUR_MAX > 1 && mb_properties)
cvsdist 732ac9
-    free (mb_properties);
cvsdist 732ac9
+  if (MB_CUR_MAX > 1)
cvsdist 732ac9
+    {
cvsdist 732ac9
+      if (mb_properties)
cvsdist ac51d8
+	free (mb_properties);
cvsdist ac51d8
+      if (match_icase)
cvsdist ac51d8
+	free ((char *) buf);
cvsdist 732ac9
+    }
cvsdist 732ac9
 #endif /* MBS_SUPPORT */
cvsdist ac51d8
   *match_size = len;
cvsdist ac51d8
   return start;
cvsdist ac51d8
@@ -504,10 +529,19 @@
cvsdist 732ac9
   register size_t len;
cvsdist 732ac9
   char eol = eolbyte;
cvsdist 732ac9
   struct kwsmatch kwsmatch;
cvsdist 732ac9
+  size_t ret_val;
cvsdist 732ac9
 #ifdef MBS_SUPPORT
cvsdist 732ac9
-  char *mb_properties;
cvsdist 732ac9
+  char *mb_properties = NULL;
cvsdist 732ac9
   if (MB_CUR_MAX > 1)
cvsdist 732ac9
-    mb_properties = check_multibyte_string (buf, size);
cvsdist 732ac9
+    {
cvsdist 732ac9
+      if (match_icase)
cvsdist 732ac9
+        {
cvsdist 732ac9
+          char *case_buf = xmalloc(size);
cvsdist 732ac9
+          memcpy(case_buf, buf, size);
cvsdist 732ac9
+          buf = case_buf;
cvsdist 732ac9
+        }
cvsdist 732ac9
+      mb_properties = check_multibyte_string(buf, size);
cvsdist 732ac9
+    }
cvsdist 732ac9
 #endif /* MBS_SUPPORT */
cvsdist 732ac9
 
cvsdist 732ac9
   for (beg = buf; beg <= buf + size; ++beg)
cvsdist ac51d8
@@ -565,7 +599,12 @@
cvsdist ac51d8
  failure:
cvsdist 732ac9
 #ifdef MBS_SUPPORT
cvsdist 732ac9
   if (MB_CUR_MAX > 1)
cvsdist 732ac9
-    free (mb_properties);
cvsdist 732ac9
+    {
cvsdist 732ac9
+      if (match_icase)
cvsdist ac51d8
+        free((char *) buf);
cvsdist 732ac9
+      if (mb_properties)
cvsdist 732ac9
+        free(mb_properties);
cvsdist 732ac9
+    }
cvsdist 732ac9
 #endif /* MBS_SUPPORT */
cvsdist 732ac9
   return -1;
cvsdist 732ac9
 
cvsdist ac51d8
@@ -581,7 +620,12 @@
cvsdist ac51d8
   *match_size = len;
cvsdist 732ac9
 #ifdef MBS_SUPPORT
cvsdist 732ac9
   if (MB_CUR_MAX > 1)
cvsdist 732ac9
-    free (mb_properties);
cvsdist 732ac9
+    {
cvsdist 732ac9
+      if (mb_properties)
cvsdist ac51d8
+	free (mb_properties);
cvsdist ac51d8
+      if (match_icase)
cvsdist ac51d8
+	free ((char *) buf);
cvsdist 732ac9
+    }
cvsdist 732ac9
 #endif /* MBS_SUPPORT */
cvsdist ac51d8
   return beg - buf;
cvsdist 732ac9
 }