462fb2
diff --git a/lib/sh/strtrans.c b/lib/sh/strtrans.c
462fb2
--- a/lib/sh/strtrans.c
462fb2
+++ b/lib/sh/strtrans.c
462fb2
@@ -30,6 +30,9 @@
462fb2
 
462fb2
 #include "shell.h"
462fb2
 
462fb2
+#include "shmbchar.h"
462fb2
+#include "shmbutil.h"
462fb2
+
462fb2
 #ifdef ESC
462fb2
 #undef ESC
462fb2
 #endif
462fb2
@@ -74,7 +77,7 @@ ansicstr (string, len, flags, sawc, rlen)
462fb2
 	    case 'a': c = '\a'; break;
462fb2
 	    case 'v': c = '\v'; break;
462fb2
 #else
462fb2
-	    case 'a': c = '\007'; break;
462fb2
+	    case 'a': c = (int) 0x07; break;
462fb2
 	    case 'v': c = (int) 0x0B; break;
462fb2
 #endif
462fb2
 	    case 'b': c = '\b'; break;
462fb2
@@ -208,6 +211,11 @@ ansic_quote (str, flags, rlen)
462fb2
   char *r, *ret, *s;
462fb2
   int l, rsize;
462fb2
   unsigned char c;
462fb2
+  size_t clen;
462fb2
+  int b;
462fb2
+#if defined (HANDLE_MULTIBYTE)
462fb2
+  wchar_t wc;
462fb2
+#endif
462fb2
 
462fb2
   if (str == 0 || *str == 0)
462fb2
     return ((char *)0);
462fb2
@@ -219,10 +227,11 @@ ansic_quote (str, flags, rlen)
462fb2
   *r++ = '$';
462fb2
   *r++ = '\'';
462fb2
 
462fb2
-  for (s = str, l = 0; *s; s++)
462fb2
+  for (s = str; c = *s; s++)
462fb2
     {
462fb2
-      c = *s;
462fb2
-      l = 1;		/* 1 == add backslash; 0 == no backslash */
462fb2
+      b = l = 1;		/* 1 == add backslash; 0 == no backslash */
462fb2
+      clen = 1;
462fb2
+
462fb2
       switch (c)
462fb2
 	{
462fb2
 	case ESC: c = 'E'; break;
462fb2
@@ -230,7 +239,7 @@ ansic_quote (str, flags, rlen)
462fb2
 	case '\a': c = 'a'; break;
462fb2
 	case '\v': c = 'v'; break;
462fb2
 #else
462fb2
-	case '\007': c = 'a'; break;
462fb2
+	case 0x07: c = 'a'; break;
462fb2
 	case 0x0b: c = 'v'; break;
462fb2
 #endif
462fb2
 
462fb2
@@ -243,7 +252,13 @@ ansic_quote (str, flags, rlen)
462fb2
 	case '\'':
462fb2
 	  break;
462fb2
 	default:
462fb2
+#if defined (HANDLE_MULTIBYTE)
462fb2
+	  b = is_basic (c);
462fb2
+	  if ((b == 0 && ((clen = mbrtowc (&wc, s, MB_CUR_MAX, 0)) < 0 || MB_INVALIDCH (clen) || iswprint (wc) == 0)) ||
462fb2
+	      (b == 1 && ISPRINT (c) == 0))
462fb2
+#else
462fb2
 	  if (ISPRINT (c) == 0)
462fb2
+#endif
462fb2
 	    {
462fb2
 	      *r++ = '\\';
462fb2
 	      *r++ = TOCHAR ((c >> 6) & 07);
462fb2
@@ -254,9 +269,20 @@ ansic_quote (str, flags, rlen)
462fb2
 	  l = 0;
462fb2
 	  break;
462fb2
 	}
462fb2
+      if (b == 0 && clen == 0)
462fb2
+	break;
462fb2
+
462fb2
       if (l)
462fb2
 	*r++ = '\\';
462fb2
-      *r++ = c;
462fb2
+
462fb2
+      if (clen == 1)
462fb2
+	*r++ = c;
462fb2
+      else
462fb2
+	{
462fb2
+	  for (b = 0; b < (int)clen; b++)
462fb2
+	    *r++ = (unsigned char)s[b];
462fb2
+	  s += clen - 1;	/* -1 because of the increment above */
462fb2
+	}
462fb2
     }
462fb2
 
462fb2
   *r++ = '\'';
462fb2
@@ -266,6 +292,37 @@ ansic_quote (str, flags, rlen)
462fb2
   return ret;
462fb2
 }
462fb2
 
462fb2
+#if defined (HANDLE_MULTIBYTE)
462fb2
+int
462fb2
+ansic_wshouldquote (string)
462fb2
+     const char *string;
462fb2
+{
462fb2
+  const wchar_t *wcs;
462fb2
+  wchar_t wcc;
462fb2
+
462fb2
+  wchar_t *wcstr = NULL;
462fb2
+  size_t slen;
462fb2
+
462fb2
+
462fb2
+  slen = mbstowcs (wcstr, string, 0);
462fb2
+
462fb2
+  if (slen == -1)
462fb2
+    slen = 0;
462fb2
+  wcstr = (wchar_t *)xmalloc (sizeof (wchar_t) * (slen + 1));
462fb2
+  mbstowcs (wcstr, string, slen + 1);
462fb2
+
462fb2
+  for (wcs = wcstr; wcc = *wcs; wcs++)
462fb2
+    if (iswprint(wcc) == 0)
462fb2
+      {
462fb2
+	free (wcstr);
462fb2
+	return 1;
462fb2
+      }
462fb2
+
462fb2
+  free (wcstr);
462fb2
+  return 0;	
462fb2
+}
462fb2
+#endif
462fb2
+
462fb2
 /* return 1 if we need to quote with $'...' because of non-printing chars. */
462fb2
 int
462fb2
 ansic_shouldquote (string)
462fb2
@@ -278,8 +335,14 @@ ansic_shouldquote (string)
462fb2
     return 0;
462fb2
 
462fb2
   for (s = string; c = *s; s++)
462fb2
-    if (ISPRINT (c) == 0)
462fb2
-      return 1;
462fb2
+    {
462fb2
+#if defined (HANDLE_MULTIBYTE)
462fb2
+      if (is_basic (c) == 0)
462fb2
+	return (ansic_wshouldquote (s));
462fb2
+#endif
462fb2
+      if (ISPRINT (c) == 0)
462fb2
+	return 1;
462fb2
+    }
462fb2
 
462fb2
   return 0;
462fb2
 }