Fix escaping for cmd.exe

Close #3651
Close #2609
pull/3189/merge
Junegunn Choi 1 month ago
parent 7ed6c7905c
commit 8d74446bef
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627

@ -319,9 +319,9 @@ func TestUnixCommands(t *testing.T) {
// purpose of this test is to demonstrate some shortcomings of fzf's templating system on Windows // purpose of this test is to demonstrate some shortcomings of fzf's templating system on Windows
func TestWindowsCommands(t *testing.T) { func TestWindowsCommands(t *testing.T) {
if !util.IsWindows() { // XXX Deprecated
t.SkipNow() t.SkipNow()
}
tests := []testCase{ tests := []testCase{
// reference: give{template, query, items}, want{output OR match} // reference: give{template, query, items}, want{output OR match}

@ -26,6 +26,7 @@ type Executor struct {
shellType shellType shellType shellType
args []string args []string
shellPath atomic.Value shellPath atomic.Value
r *regexp.Regexp
} }
func NewExecutor(withShell string) *Executor { func NewExecutor(withShell string) *Executor {
@ -37,6 +38,7 @@ func NewExecutor(withShell string) *Executor {
shell = "cmd" shell = "cmd"
} }
var r *regexp.Regexp
shellType := shellTypeUnknown shellType := shellTypeUnknown
basename := filepath.Base(shell) basename := filepath.Base(shell)
if len(args) > 0 { if len(args) > 0 {
@ -44,13 +46,14 @@ func NewExecutor(withShell string) *Executor {
} else if strings.HasPrefix(basename, "cmd") { } else if strings.HasPrefix(basename, "cmd") {
shellType = shellTypeCmd shellType = shellTypeCmd
args = []string{"/v:on/s/c"} args = []string{"/v:on/s/c"}
r = regexp.MustCompile(`[&|<>()@^%!]`)
} else if strings.HasPrefix(basename, "pwsh") || strings.HasPrefix(basename, "powershell") { } else if strings.HasPrefix(basename, "pwsh") || strings.HasPrefix(basename, "powershell") {
shellType = shellTypePowerShell shellType = shellTypePowerShell
args = []string{"-NoProfile", "-Command"} args = []string{"-NoProfile", "-Command"}
} else { } else {
args = []string{"-c"} args = []string{"-c"}
} }
return &Executor{shell: shell, shellType: shellType, args: args} return &Executor{shell: shell, shellType: shellType, args: args, r: r}
} }
// ExecCommand executes the given command with $SHELL // ExecCommand executes the given command with $SHELL
@ -112,13 +115,21 @@ func (x *Executor) Become(stdin *os.File, environ []string, command string) {
func (x *Executor) QuoteEntry(entry string) string { func (x *Executor) QuoteEntry(entry string) string {
switch x.shellType { switch x.shellType {
case shellTypeCmd: case shellTypeCmd:
// backslash escaping is done here for applications /* Manually tested with the following commands:
// (see ripgrep test case in terminal_test.go#TestWindowsCommands) fzf --preview "echo {}"
escaped := strings.Replace(entry, `\`, `\\`, -1) fzf --preview "type {}"
escaped = `"` + strings.Replace(escaped, `"`, `\"`, -1) + `"` echo .git\refs\| fzf --preview "dir {}"
// caret is the escape character for cmd shell echo .git\refs\\| fzf --preview "dir {}"
r, _ := regexp.Compile(`[&|<>()@^%!"]`) echo .git\refs\\\| fzf --preview "dir {}"
return r.ReplaceAllStringFunc(escaped, func(match string) string { reg query HKCU | fzf --reverse --bind "enter:reload(reg query {})"
fzf --disabled --preview "echo {q}" --query "&|<>()@^^%!"
fd -H --no-ignore -td -d 4 | fzf --preview "dir {}"
fd -H --no-ignore -td -d 4 | fzf --preview "eza {}" --preview-window up
fd -H --no-ignore -td -d 4 | fzf --preview "eza --color=always --tree --level=3 --icons=always {}"
fd -H --no-ignore -td -d 4 | fzf --preview ".\eza.exe --color=always --tree --level=3 --icons=always {}" --with-shell "powershell -NoProfile -Command"
*/
escaped := syscall.EscapeArg(entry)
return x.r.ReplaceAllStringFunc(escaped, func(match string) string {
return "^" + match return "^" + match
}) })
case shellTypePowerShell: case shellTypePowerShell:

Loading…
Cancel
Save