Skip to content
Myr

Fix escaping for cmd.exe

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

Close #3651 Close #2609

authored by Junegunn Choi · committed by Junegunn Choi · parent 7ed6c79 · Browse files · 2 files+22 added−11 removed
This commit

State

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

Diff stats

2 files+22 added−11 removed

  • other:+22 added−11 removed

2 files+22 added−11 removed

modified:src/util/util_windows.go+19 added-8 removed
24 unmodified lines
25 shell string26 shellType shellType27 args []string28 shellPath atomic.Value29 r *regexp.Regexp30}3132func NewExecutor(withShell string) *Executor {33 shell := os.Getenv("SHELL")
3 unmodified lines
37 } else if len(shell) == 0 {38 shell = "cmd"39 }4041 var r *regexp.Regexp42 shellType := shellTypeUnknown43 basename := filepath.Base(shell)44 if len(args) > 0 {45 args = args[1:]46 } else if strings.HasPrefix(basename, "cmd") {47 shellType = shellTypeCmd48 args = []string{"/v:on/s/c"}49 r = regexp.MustCompile(`[&|<>()@^%!]`)50 } else if strings.HasPrefix(basename, "pwsh") || strings.HasPrefix(basename, "powershell") {51 shellType = shellTypePowerShell52 args = []string{"-NoProfile", "-Command"}53 } else {54 args = []string{"-c"}55 }53 return &Executor{shell: shell, shellType: shellType, args: args}56 return &Executor{shell: shell, shellType: shellType, args: args, r: r}57}5859// ExecCommand executes the given command with $SHELL60// FIXME: setpgid is unused. We set it in the Unix implementation so that we
53 unmodified lines
114115func (x *Executor) QuoteEntry(entry string) string {116 switch x.shellType {117 case shellTypeCmd:115 // backslash escaping is done here for applications116 // (see ripgrep test case in terminal_test.go#TestWindowsCommands)117 escaped := strings.Replace(entry, `\`, `\\`, -1)118 escaped = `"` + strings.Replace(escaped, `"`, `\"`, -1) + `"`119 // caret is the escape character for cmd shell120 r, _ := regexp.Compile(`[&|<>()@^%!"]`)121 return r.ReplaceAllStringFunc(escaped, func(match string) string {118 /* Manually tested with the following commands:119 fzf --preview "echo {}"120 fzf --preview "type {}"121 echo .git\refs\| fzf --preview "dir {}"122 echo .git\refs\\| fzf --preview "dir {}"123 echo .git\refs\\\| fzf --preview "dir {}"124 reg query HKCU | fzf --reverse --bind "enter:reload(reg query {})"125 fzf --disabled --preview "echo {q}" --query "&|<>()@^^%!"126 fd -H --no-ignore -td -d 4 | fzf --preview "dir {}"127 fd -H --no-ignore -td -d 4 | fzf --preview "eza {}" --preview-window up128 fd -H --no-ignore -td -d 4 | fzf --preview "eza --color=always --tree --level=3 --icons=always {}"129 fd -H --no-ignore -td -d 4 | fzf --preview ".\eza.exe --color=always --tree --level=3 --icons=always {}" --with-shell "powershell -NoProfile -Command"130 */131 escaped := syscall.EscapeArg(entry)132 return x.r.ReplaceAllStringFunc(escaped, func(match string) string {133 return "^" + match134 })135 case shellTypePowerShell:136 escaped := strings.Replace(entry, `"`, `\"`, -1)
29 unmodified lines
modified:src/terminal_test.go+3 added-3 removed
317 unmodified lines
318}319320// purpose of this test is to demonstrate some shortcomings of fzf's templating system on Windows321func TestWindowsCommands(t *testing.T) {322 if !util.IsWindows() {323 t.SkipNow()324 }322 // XXX Deprecated323 t.SkipNow()324325 tests := []testCase{326 // reference: give{template, query, items}, want{output OR match}327328 // 1) working examples
330 unmodified lines