2022年3月15日(火) 4:06 Chet Ramey <chet.ra...@case.edu>:
> > Description:
> >
> >    The escaping of the invisible characters in the directory names in
> >    the prompt escapes \w, \W and the path in \s has been implemented in
> >    the function `sh_backslash_quote_for_double_quotes'
> >    (lib/sh/shquote.c:323) in commit b4e5e550.  The primary purpose of
> >    the function `sh_backslash_quote_for_double_quotes' is to quote
> >    special characters in double quotes with a backslash.  When the
> >    escaping of the invisible characters are turned on by the argument
> >    FLAG, a backslash in the visible representation of $'\034', '^\', it
> >    not properly escaped by another backslash.  This may break the
> >    escaping of the succeeding special character.
>
> I did it this way precisely to avoid two passes through the string. If
> that's not an issue, you can just call sh_strvis and then
> sh_backslash_quote_for_double_quotes on the result.

Thank you for the suggestion. You are right. I have updated patch A
[see attached 0001-fix-prompt-charvis-A.v2.patch]. Or maybe another
option might be to just drop the argument FLAGS from
`sh_backslash_quote_for_double_quotes' and call `sh_strvis' at the
caller side [see another patch C attached as
0001-fix-prompt-charvis-C.patch].

--
Koichi
From 78e6234fb3bf0d1112f26735a0a594a32190b96e Mon Sep 17 00:00:00 2001
From: Koichi Murase <myoga.murase@gmail.com>
Date: Sat, 12 Mar 2022 15:52:57 +0900
Subject: [PATCH 1/4] fix backslash quoting of invisible character ^\

---
 lib/sh/shquote.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/lib/sh/shquote.c b/lib/sh/shquote.c
index 622fcbb0..63d891dc 100644
--- a/lib/sh/shquote.c
+++ b/lib/sh/shquote.c
@@ -39,7 +39,7 @@
 extern char *ansic_quote PARAMS((char *, int, int *));
 extern int ansic_shouldquote PARAMS((const char *));
 
-extern int sh_charvis PARAMS((const char *, size_t *, size_t, char *, size_t *));
+extern char *sh_strvis PARAMS((const char *));
 
 /* Default set of characters that should be backslash-quoted in strings */
 static const char bstab[256] =
@@ -316,7 +316,7 @@ sh_backslash_quote (string, table, flags)
 #if defined (PROMPT_STRING_DECODE) || defined (TRANSLATABLE_STRINGS)
 /* Quote characters that get special treatment when in double quotes in STRING
    using backslashes. If FLAGS == 1, also make `unsafe' characters visible by
-   translating them to a standard ^X/M-X representation by calling sh_charvis,
+   translating them to a standard ^X/M-X representation by calling sh_strvis,
    which handles multibyte characters as well.
    Return a new string. */
 char *
@@ -329,12 +329,15 @@ sh_backslash_quote_for_double_quotes (string, flags)
   size_t slen, sind, rind;
   int mb_cur_max;
   DECLARE_MBSTATE;
- 
+
+  if (flags & 1)
+    string = sh_strvis (string);
+
   slen = strlen (string);
   send = string + slen;
   mb_cur_max = MB_CUR_MAX;
-  /* Max is 4*string length (backslash + three-character visible representation) */
-  result = (char *)xmalloc (4 * slen + 1);
+  /* Max is 2*string length (backslash) */
+  result = (char *)xmalloc (2 * slen + 1);
 
   for (rind = sind = 0; c = string[sind]; sind++)
     {
@@ -343,13 +346,6 @@ sh_backslash_quote_for_double_quotes (string, flags)
       if ((sh_syntaxtab[c] & CBSDQUOTE) && c != '\n')
 	result[rind++] = '\\';
 
-      if (flags & 1)
-	{
-	  sh_charvis (string, &sind, slen, result, &rind);
-	  sind--;		/* sh_charvis consumes an extra character */
-	  continue;
-	}
-
       /* I should probably use the CSPECL flag for these in sh_syntaxtab[] */
       else if (c == CTLESC || c == CTLNUL)
 	result[rind++] = CTLESC;		/* could be '\\'? */
@@ -368,6 +364,10 @@ sh_backslash_quote_for_double_quotes (string, flags)
     }
 
   result[rind] = '\0';
+
+  if (flags & 1)
+    free(string);
+
   return (result);
 }
 #endif /* PROMPT_STRING_DECODE */
-- 
2.35.1

Attachment: 0001-fix-prompt-charvis-C.patch
Description: Binary data

Reply via email to