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