nalika / rpms / bash

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