Escaping for cmd.exe: always use double quotes

This commit is contained in:
Junegunn Choi 2024-04-28 23:29:29 +09:00
parent 8d74446bef
commit 194a763c46
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627

View File

@ -7,7 +7,6 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"regexp"
"strings" "strings"
"sync/atomic" "sync/atomic"
"syscall" "syscall"
@ -26,7 +25,6 @@ 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 {
@ -38,7 +36,6 @@ 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 {
@ -46,14 +43,13 @@ 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, r: r} return &Executor{shell: shell, shellType: shellType, args: args}
} }
// ExecCommand executes the given command with $SHELL // ExecCommand executes the given command with $SHELL
@ -112,6 +108,34 @@ func (x *Executor) Become(stdin *os.File, environ []string, command string) {
Exit(0) Exit(0)
} }
func escapeArg(s string) string {
b := make([]byte, 0, len(s)+2)
b = append(b, '"')
slashes := 0
for i := 0; i < len(s); i++ {
c := s[i]
switch c {
default:
slashes = 0
case '\\':
slashes++
case '&', '|', '<', '>', '(', ')', '@', '^', '%', '!':
b = append(b, '^')
case '"':
for ; slashes > 0; slashes-- {
b = append(b, '\\')
}
b = append(b, '\\')
}
b = append(b, c)
}
for ; slashes > 0; slashes-- {
b = append(b, '\\')
}
b = append(b, '"')
return string(b)
}
func (x *Executor) QuoteEntry(entry string) string { func (x *Executor) QuoteEntry(entry string) string {
switch x.shellType { switch x.shellType {
case shellTypeCmd: case shellTypeCmd:
@ -122,16 +146,13 @@ func (x *Executor) QuoteEntry(entry string) string {
echo .git\refs\\| fzf --preview "dir {}" echo .git\refs\\| fzf --preview "dir {}"
echo .git\refs\\\| fzf --preview "dir {}" echo .git\refs\\\| fzf --preview "dir {}"
reg query HKCU | fzf --reverse --bind "enter:reload(reg query {})" reg query HKCU | fzf --reverse --bind "enter:reload(reg query {})"
fzf --disabled --preview "echo {q}" --query "&|<>()@^^%!" fzf --disabled --preview "echo {q} {n} {}" --query "&|<>()@^%!"
fd -H --no-ignore -td -d 4 | fzf --preview "dir {}" 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 {}" --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 --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" 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 escapeArg(entry)
return x.r.ReplaceAllStringFunc(escaped, func(match string) string {
return "^" + match
})
case shellTypePowerShell: case shellTypePowerShell:
escaped := strings.Replace(entry, `"`, `\"`, -1) escaped := strings.Replace(entry, `"`, `\"`, -1)
return "'" + strings.Replace(escaped, "'", "''", -1) + "'" return "'" + strings.Replace(escaped, "'", "''", -1) + "'"