2022-03-29 12:20:33 +00:00
|
|
|
//go:build windows
|
2016-10-24 00:44:56 +00:00
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2017-10-15 09:32:59 +00:00
|
|
|
"fmt"
|
2016-10-24 00:44:56 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2024-04-27 09:36:37 +00:00
|
|
|
"regexp"
|
2021-10-22 16:09:47 +00:00
|
|
|
"strings"
|
2021-10-25 09:46:59 +00:00
|
|
|
"sync/atomic"
|
2017-01-07 16:30:31 +00:00
|
|
|
"syscall"
|
2016-10-24 00:44:56 +00:00
|
|
|
)
|
|
|
|
|
2024-04-27 09:36:37 +00:00
|
|
|
type Executor struct {
|
|
|
|
shell string
|
|
|
|
args []string
|
|
|
|
shellPath atomic.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewExecutor(withShell string) *Executor {
|
|
|
|
shell := os.Getenv("SHELL")
|
|
|
|
args := strings.Fields(withShell)
|
|
|
|
if len(args) > 0 {
|
|
|
|
shell = args[0]
|
|
|
|
} else if len(shell) == 0 {
|
|
|
|
shell = "cmd"
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) > 0 {
|
|
|
|
args = args[1:]
|
|
|
|
} else if strings.Contains(shell, "cmd") {
|
|
|
|
args = []string{"/v:on/s/c"}
|
|
|
|
} else if strings.Contains(shell, "pwsh") || strings.Contains(shell, "powershell") {
|
|
|
|
args = []string{"-NoProfile", "-Command"}
|
|
|
|
} else {
|
|
|
|
args = []string{"-c"}
|
|
|
|
}
|
|
|
|
return &Executor{shell: shell, args: args}
|
|
|
|
}
|
2021-10-25 09:46:59 +00:00
|
|
|
|
2021-10-22 16:09:47 +00:00
|
|
|
// ExecCommand executes the given command with $SHELL
|
2024-04-27 09:36:37 +00:00
|
|
|
// FIXME: setpgid is unused. We set it in the Unix implementation so that we
|
|
|
|
// can kill preview process with its child processes at once.
|
|
|
|
// NOTE: For "powershell", we should ideally set output encoding to UTF8,
|
|
|
|
// but it is left as is now because no adverse effect has been observed.
|
|
|
|
func (x *Executor) ExecCommand(command string, setpgid bool) *exec.Cmd {
|
|
|
|
shell := x.shell
|
|
|
|
if cached := x.shellPath.Load(); cached != nil {
|
2021-10-25 09:46:59 +00:00
|
|
|
shell = cached.(string)
|
|
|
|
} else {
|
2024-04-27 09:36:37 +00:00
|
|
|
if strings.Contains(shell, "/") {
|
2021-10-25 09:46:59 +00:00
|
|
|
out, err := exec.Command("cygpath", "-w", shell).Output()
|
|
|
|
if err == nil {
|
|
|
|
shell = strings.Trim(string(out), "\n")
|
|
|
|
}
|
2021-10-22 16:09:47 +00:00
|
|
|
}
|
2024-04-27 09:36:37 +00:00
|
|
|
x.shellPath.Store(shell)
|
2021-10-22 16:09:47 +00:00
|
|
|
}
|
2024-04-27 09:36:37 +00:00
|
|
|
cmd := exec.Command(shell, append(x.args, command)...)
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
|
|
|
HideWindow: false,
|
|
|
|
CreationFlags: 0,
|
|
|
|
}
|
|
|
|
return cmd
|
2017-09-28 14:05:02 +00:00
|
|
|
}
|
|
|
|
|
2024-04-27 09:36:37 +00:00
|
|
|
func (x *Executor) Become(stdin *os.File, environ []string, command string) {
|
|
|
|
cmd := x.ExecCommand(command, false)
|
|
|
|
cmd.Stdin = stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
cmd.Env = environ
|
|
|
|
err := cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "fzf (become): %s\n", err.Error())
|
|
|
|
Exit(127)
|
|
|
|
}
|
|
|
|
err = cmd.Wait()
|
|
|
|
if err != nil {
|
|
|
|
if exitError, ok := err.(*exec.ExitError); ok {
|
|
|
|
Exit(exitError.ExitCode())
|
2021-10-22 16:09:47 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-27 09:36:37 +00:00
|
|
|
Exit(0)
|
|
|
|
}
|
2021-10-27 07:50:40 +00:00
|
|
|
|
2024-04-27 09:36:37 +00:00
|
|
|
func (x *Executor) QuoteEntry(entry string) string {
|
|
|
|
if strings.Contains(x.shell, "cmd") {
|
|
|
|
// backslash escaping is done here for applications
|
|
|
|
// (see ripgrep test case in terminal_test.go#TestWindowsCommands)
|
|
|
|
escaped := strings.Replace(entry, `\`, `\\`, -1)
|
|
|
|
escaped = `"` + strings.Replace(escaped, `"`, `\"`, -1) + `"`
|
|
|
|
// caret is the escape character for cmd shell
|
|
|
|
r, _ := regexp.Compile(`[&|<>()@^%!"]`)
|
|
|
|
return r.ReplaceAllStringFunc(escaped, func(match string) string {
|
|
|
|
return "^" + match
|
|
|
|
})
|
|
|
|
} else if strings.Contains(x.shell, "pwsh") || strings.Contains(x.shell, "powershell") {
|
|
|
|
escaped := strings.Replace(entry, `"`, `\"`, -1)
|
|
|
|
return "'" + strings.Replace(escaped, "'", "''", -1) + "'"
|
2021-10-27 07:50:40 +00:00
|
|
|
} else {
|
2024-04-27 09:36:37 +00:00
|
|
|
return "'" + strings.Replace(entry, "'", "'\\''", -1) + "'"
|
2017-10-02 15:36:19 +00:00
|
|
|
}
|
2016-11-06 17:15:34 +00:00
|
|
|
}
|
|
|
|
|
2018-09-27 06:27:08 +00:00
|
|
|
// KillCommand kills the process for the given command
|
|
|
|
func KillCommand(cmd *exec.Cmd) error {
|
|
|
|
return cmd.Process.Kill()
|
|
|
|
}
|
|
|
|
|
2016-11-06 17:15:34 +00:00
|
|
|
// IsWindows returns true on Windows
|
|
|
|
func IsWindows() bool {
|
|
|
|
return true
|
2016-10-24 00:44:56 +00:00
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
|
2018-09-27 06:27:08 +00:00
|
|
|
// SetNonblock executes syscall.SetNonblock on file descriptor
|
2017-01-07 16:30:31 +00:00
|
|
|
func SetNonblock(file *os.File, nonblock bool) {
|
|
|
|
syscall.SetNonblock(syscall.Handle(file.Fd()), nonblock)
|
|
|
|
}
|
2017-05-24 16:36:59 +00:00
|
|
|
|
|
|
|
// Read executes syscall.Read on file descriptor
|
|
|
|
func Read(fd int, b []byte) (int, error) {
|
|
|
|
return syscall.Read(syscall.Handle(fd), b)
|
|
|
|
}
|
2023-02-14 14:21:34 +00:00
|
|
|
|
|
|
|
func SetStdin(file *os.File) {
|
|
|
|
// No-op
|
|
|
|
}
|