Skip to content
Myr

Fix ExecCommandWith for cmd.exe in Windows (#1072)

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

Close #1018 Run the command as is in cmd.exe with no parsing and escaping. Explicity set cmd.SysProcAttr so execCommand does not escape the command. Technically, the command should be escaped with ^ for special characters, including ". This allows cmd.exe commands to be chained together. See https://github.com/neovim/neovim/pull/7343#issuecomment-333350201 This commit also updates quoteEntry to use strings.Replace instead of strconv.Quote which escapes more than \ and ".

authored by Jan Edmund Lazo · committed by Junegunn Choi · parent 0580fe9 · Browse files · 3 files+36 added−8 removed
This commit

State

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

Diff stats

3 files+36 added−8 removed

  • other:+36 added−8 removed

3 files+36 added−8 removed

modified:src/util/util_windows.go+7 added-7 removed
4 unmodified lines
5import (6 "os"7 "os/exec"8 "syscall"910 "github.com/mattn/go-shellwords"9)1011// ExecCommand executes the given command with cmd12func ExecCommand(command string) *exec.Cmd {
2 unmodified lines
1516// ExecCommandWith executes the given command with cmd. _shell parameter is17// ignored on Windows.18func ExecCommandWith(_shell string, command string) *exec.Cmd {21 args, _ := shellwords.Parse(command)22 allArgs := make([]string, len(args)+1)23 allArgs[0] = "/c"24 copy(allArgs[1:], args)25 return exec.Command("cmd", allArgs...)19 cmd := exec.Command("cmd")20 cmd.SysProcAttr = &syscall.SysProcAttr{21 HideWindow: false,22 CmdLine: fmt.Sprintf(` /s /c "%s"`, command),23 CreationFlags: 0,24 }25 return cmd26}2728// IsWindows returns true on Windows29func IsWindows() bool {
12 unmodified lines
modified:src/terminal_test.go+19 added
89 unmodified lines
90 // foo'bar baz91 result = replacePlaceholder("echo {}/{1}/{3}/{2..3}", true, Delimiter{regex: regex}, false, "query", items1)92 check("echo ' foo'\\''bar baz'/'f'/'r b'/''\\''bar b'")93}9495func TestQuoteEntryCmd(t *testing.T) {96 tests := map[string]string{97 `"`: `^"\^"^"`,98 `\`: `^"\\^"`,99 `\"`: `^"\\\^"^"`,100 `"\\\"`: `^"\^"\\\\\\\^"^"`,101 `&|<>()@^%!`: `^"^&^|^<^>^(^)^@^^^%^!^"`,102 `%USERPROFILE%`: `^"^%USERPROFILE^%^"`,103 `C:\Program Files (x86)\`: `^"C:\\Program Files ^(x86^)\\^"`,104 }105106 for input, expected := range tests {107 escaped := quoteEntryCmd(input)108 if escaped != expected {109 t.Errorf("Input: %s, expected: %s, actual %s", input, expected, escaped)110 }111 }112}
modified:src/terminal.go+10 added-1 removed
1101 unmodified lines
1102 event.Type == tui.Rune && int(event.Char) == key-tui.AltZ ||1103 event.Type == tui.Mouse && key == tui.DoubleClick && event.MouseEvent.Double1104}11051106func quoteEntryCmd(entry string) string {1107 escaped := strings.Replace(entry, `\`, `\\`, -1)1108 escaped = `"` + strings.Replace(escaped, `"`, `\"`, -1) + `"`1109 r, _ := regexp.Compile(`[&|<>()@^%!"]`)1110 return r.ReplaceAllStringFunc(escaped, func(match string) string {1111 return "^" + match1112 })1113}11141115func quoteEntry(entry string) string {1116 if util.IsWindows() {1108 return strconv.Quote(strings.Replace(entry, "\"", "\\\"", -1))1117 return quoteEntryCmd(entry)1118 }1119 return "'" + strings.Replace(entry, "'", "'\\''", -1) + "'"1120}1121
739 unmodified lines