Skip to content
Myr

Escaping for cmd.exe: always use double quotes

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Junegunn Choi · committed by Junegunn Choi · parent 8d74446 · Browse files · 1 file+31 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+31 added−10 removed

  • other:+31 added−10 removed

1 file+31 added−10 removed

modified:src/util/util_windows.go+31 added-10 removed
5 unmodified lines
6 "fmt"7 "os"8 "os/exec"9 "path/filepath"10 "regexp"10 "strings"11 "sync/atomic"12 "syscall"13)
10 unmodified lines
24 shell string25 shellType shellType26 args []string27 shellPath atomic.Value29 r *regexp.Regexp28}2930func NewExecutor(withShell string) *Executor {31 shell := os.Getenv("SHELL")
3 unmodified lines
35 } else if len(shell) == 0 {36 shell = "cmd"37 }3841 var r *regexp.Regexp39 shellType := shellTypeUnknown40 basename := filepath.Base(shell)41 if len(args) > 0 {42 args = args[1:]43 } else if strings.HasPrefix(basename, "cmd") {44 shellType = shellTypeCmd45 args = []string{"/v:on/s/c"}49 r = regexp.MustCompile(`[&|<>()@^%!]`)46 } else if strings.HasPrefix(basename, "pwsh") || strings.HasPrefix(basename, "powershell") {47 shellType = shellTypePowerShell48 args = []string{"-NoProfile", "-Command"}49 } else {50 args = []string{"-c"}51 }56 return &Executor{shell: shell, shellType: shellType, args: args, r: r}52 return &Executor{shell: shell, shellType: shellType, args: args}53}5455// ExecCommand executes the given command with $SHELL56// FIXME: setpgid is unused. We set it in the Unix implementation so that we
50 unmodified lines
107 }108 Exit(0)109}110111func escapeArg(s string) string {112 b := make([]byte, 0, len(s)+2)113 b = append(b, '"')114 slashes := 0115 for i := 0; i < len(s); i++ {116 c := s[i]117 switch c {118 default:119 slashes = 0120 case '\\':121 slashes++122 case '&', '|', '<', '>', '(', ')', '@', '^', '%', '!':123 b = append(b, '^')124 case '"':125 for ; slashes > 0; slashes-- {126 b = append(b, '\\')127 }128 b = append(b, '\\')129 }130 b = append(b, c)131 }132 for ; slashes > 0; slashes-- {133 b = append(b, '\\')134 }135 b = append(b, '"')136 return string(b)137}138139func (x *Executor) QuoteEntry(entry string) string {140 switch x.shellType {141 case shellTypeCmd:142 /* Manually tested with the following commands:
2 unmodified lines
145 echo .git\refs\| fzf --preview "dir {}"146 echo .git\refs\\| fzf --preview "dir {}"147 echo .git\refs\\\| fzf --preview "dir {}"148 reg query HKCU | fzf --reverse --bind "enter:reload(reg query {})"125 fzf --disabled --preview "echo {q}" --query "&|<>()@^^%!"149 fzf --disabled --preview "echo {q} {n} {}" --query "&|<>()@^%!"150 fd -H --no-ignore -td -d 4 | fzf --preview "dir {}"151 fd -H --no-ignore -td -d 4 | fzf --preview "eza {}" --preview-window up152 fd -H --no-ignore -td -d 4 | fzf --preview "eza --color=always --tree --level=3 --icons=always {}"153 fd -H --no-ignore -td -d 4 | fzf --preview ".\eza.exe --color=always --tree --level=3 --icons=always {}" --with-shell "powershell -NoProfile -Command"154 */131 escaped := syscall.EscapeArg(entry)132 return x.r.ReplaceAllStringFunc(escaped, func(match string) string {133 return "^" + match134 })155 return escapeArg(entry)156 case shellTypePowerShell:157 escaped := strings.Replace(entry, `"`, `\"`, -1)158 return "'" + strings.Replace(escaped, "'", "''", -1) + "'"159 default:
27 unmodified lines