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
Provenance
- Commit
8d74446- Tree
74317c8- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
7ed6c79
2 files changed+22 added−11 removed
modified:src/util/util_windows.go+19 added-8 removed
24 unmodified lines25 shell string26 shellType shellType27 args []string28 shellPath atomic.Value29+ r *regexp.Regexp30 }31 32 func NewExecutor(withShell string) *Executor {33 shell := os.Getenv("SHELL")3 unmodified lines37 } else if len(shell) == 0 {38 shell = "cmd"39 }40 41+ 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 }58 59 // ExecCommand executes the given command with $SHELL60 // FIXME: setpgid is unused. We set it in the Unix implementation so that we53 unmodified lines114 115 func (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 linesmodified:src/terminal_test.go+3 added-3 removed
317 unmodified lines318 }319 320 // purpose of this test is to demonstrate some shortcomings of fzf's templating system on Windows321 func TestWindowsCommands(t *testing.T) {322- if !util.IsWindows() {323- t.SkipNow()324- }322+ // XXX Deprecated323+ t.SkipNow()324+325 tests := []testCase{326 // reference: give{template, query, items}, want{output OR match}327 328 // 1) working examples330 unmodified lines