rcolebaugh / rpms / bash

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