]> granicus.if.org Git - vim/commitdiff
patch 8.2.3385: escaping for fish shell does not work properly v8.2.3385
authorJason Cox <dev@jasoncarloscox.com>
Sun, 29 Aug 2021 10:36:49 +0000 (12:36 +0200)
committerBram Moolenaar <Bram@vim.org>
Sun, 29 Aug 2021 10:36:49 +0000 (12:36 +0200)
Problem:    Escaping for fish shell does not work properly.
Solution:   Insert a backslash before a backslash. (Jason Cox, closes #8810)

runtime/doc/eval.txt
src/strings.c
src/testdir/test_shell.vim
src/version.c

index db74f9c10b6e8afba6abeca2a4fdb59c94b3137f..93ff4169f941fff72b8c238d0881b4a42b5a2f2f 100644 (file)
@@ -10111,6 +10111,10 @@ shellescape({string} [, {special}])                    *shellescape()*
                escaped.  When 'shell' containing "csh" in the tail it's
                escaped a second time.
 
+               The "\" character will be escaped when 'shell' contains "fish"
+               in the tail. That is because for fish "\" is used as an escape
+               character inside single quotes.
+
                Example of use with a |:!| command: >
                    :exe '!dir ' . shellescape(expand('<cfile>'), 1)
 <              This results in a directory listing for the file under the
index ef0282a69b829f189552185fbd7b3acb32a80ecd..566f4dab4348d5957eedd08a84e961492f15ccb3 100644 (file)
@@ -124,6 +124,15 @@ csh_like_shell(void)
     return (strstr((char *)gettail(p_sh), "csh") != NULL);
 }
 
+/*
+ * Return TRUE when 'shell' has "fish" in the tail.
+ */
+    int
+fish_like_shell(void)
+{
+    return (strstr((char *)gettail(p_sh), "fish") != NULL);
+}
+
 /*
  * Escape "string" for use as a shell argument with system().
  * This uses single quotes, except when we know we need to use double quotes
@@ -145,6 +154,7 @@ vim_strsave_shellescape(char_u *string, int do_special, int do_newline)
     char_u     *escaped_string;
     int                l;
     int                csh_like;
+    int                fish_like;
     char_u     *shname;
     int                powershell;
 # ifdef MSWIN
@@ -157,6 +167,10 @@ vim_strsave_shellescape(char_u *string, int do_special, int do_newline)
     // Csh also needs to have "\n" escaped twice when do_special is set.
     csh_like = csh_like_shell();
 
+    // Fish shell uses '\' as an escape character within single quotes, so '\'
+    // itself must be escaped to get a literal '\'.
+    fish_like = fish_like_shell();
+
     // PowerShell uses it's own version for quoting single quotes
     shname = gettail(p_sh);
     powershell = strstr((char *)shname, "pwsh") != NULL;
@@ -197,6 +211,8 @@ vim_strsave_shellescape(char_u *string, int do_special, int do_newline)
            ++length;                   // insert backslash
            p += l - 1;
        }
+       if (*p == '\\' && fish_like)
+           ++length;                   // insert backslash
     }
 
     // Allocate memory for the result and fill it.
@@ -261,6 +277,11 @@ vim_strsave_shellescape(char_u *string, int do_special, int do_newline)
                    *d++ = *p++;
                continue;
            }
+           if (*p == '\\' && fish_like)
+           {
+               *d++ = '\\';
+               *d++ = *p++;
+           }
 
            MB_COPY_CHAR(p, d);
        }
index bdc66317778998bda7516ed9b193a5369a1107b8..fa11f08d726883e41aa390a2b7ed912f1415dc5b 100644 (file)
@@ -61,18 +61,21 @@ func Test_shell_options()
   for e in shells
     exe 'set shell=' .. e[0]
     if e[0] =~# '.*csh$' || e[0] =~# '.*csh.exe$'
-      let str1 = "'cmd \"arg1\" '\\''arg2'\\'' \\!%#'"
-      let str2 = "'cmd \"arg1\" '\\''arg2'\\'' \\\\!\\%\\#'"
+      let str1 = "'cmd \"arg1\" '\\''arg2'\\'' \\!%# \\'"
+      let str2 = "'cmd \"arg1\" '\\''arg2'\\'' \\\\!\\%\\# \\'"
     elseif e[0] =~# '.*powershell$' || e[0] =~# '.*powershell.exe$'
           \ || e[0] =~# '.*pwsh$' || e[0] =~# '.*pwsh.exe$'
-      let str1 = "'cmd \"arg1\" ''arg2'' !%#'"
-      let str2 = "'cmd \"arg1\" ''arg2'' \\!\\%\\#'"
+      let str1 = "'cmd \"arg1\" ''arg2'' !%# \\'"
+      let str2 = "'cmd \"arg1\" ''arg2'' \\!\\%\\# \\'"
+    elseif e[0] =~# '.*fish$' || e[0] =~# '.*fish.exe$'
+      let str1 = "'cmd \"arg1\" '\\''arg2'\\'' !%# \\\\'"
+      let str2 = "'cmd \"arg1\" '\\''arg2'\\'' \\!\\%\\# \\\\'"
     else
-      let str1 = "'cmd \"arg1\" '\\''arg2'\\'' !%#'"
-      let str2 = "'cmd \"arg1\" '\\''arg2'\\'' \\!\\%\\#'"
+      let str1 = "'cmd \"arg1\" '\\''arg2'\\'' !%# \\'"
+      let str2 = "'cmd \"arg1\" '\\''arg2'\\'' \\!\\%\\# \\'"
     endif
-    call assert_equal(str1, shellescape("cmd \"arg1\" 'arg2' !%#"), e[0])
-    call assert_equal(str2, shellescape("cmd \"arg1\" 'arg2' !%#", 1), e[0])
+    call assert_equal(str1, shellescape("cmd \"arg1\" 'arg2' !%# \\"), e[0])
+    call assert_equal(str2, shellescape("cmd \"arg1\" 'arg2' !%# \\", 1), e[0])
 
     " Try running an external command with the shell.
     if executable(e[0])
index d50d09829dc012f9e9d59aee8fd681d79b68750c..5f427a8774be52f091c1471f162ffef68b4d31c1 100644 (file)
@@ -755,6 +755,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    3385,
 /**/
     3384,
 /**/