| 1 |
//go:build !windows |
| 2 |
|
| 3 |
package util |
| 4 |
|
| 5 |
import ( |
| 6 |
"fmt" |
| 7 |
"os" |
| 8 |
"os/exec" |
| 9 |
"strings" |
| 10 |
"syscall" |
| 11 |
|
| 12 |
"github.com/junegunn/go-shellwords" |
| 13 |
"golang.org/x/sys/unix" |
| 14 |
) |
| 15 |
|
| 16 |
type Executor struct { |
| 17 |
shell string |
| 18 |
args []string |
| 19 |
escaper *strings.Replacer |
| 20 |
} |
| 21 |
|
| 22 |
func NewExecutor(withShell string) *Executor { |
| 23 |
shell := os.Getenv("SHELL") |
| 24 |
args, err := shellwords.Parse(withShell) |
| 25 |
if err == nil && len(args) > 0 { |
| 26 |
shell = args[0] |
| 27 |
args = args[1:] |
| 28 |
} else { |
| 29 |
if len(shell) == 0 { |
| 30 |
shell = "sh" |
| 31 |
} |
| 32 |
args = []string{"-c"} |
| 33 |
} |
| 34 |
|
| 35 |
var escaper *strings.Replacer |
| 36 |
tokens := strings.Split(shell, "/") |
| 37 |
if tokens[len(tokens)-1] == "fish" { |
| 38 |
// https://fishshell.com/docs/current/language.html#quotes |
| 39 |
// > The only meaningful escape sequences in single quotes are \', which |
| 40 |
// > escapes a single quote and \\, which escapes the backslash symbol. |
| 41 |
escaper = strings.NewReplacer("\\", "\\\\", "'", "\\'") |
| 42 |
} else { |
| 43 |
escaper = strings.NewReplacer("'", "'\\''") |
| 44 |
} |
| 45 |
return &Executor{shell, args, escaper} |
| 46 |
} |
| 47 |
|
| 48 |
// ExecCommand executes the given command with $SHELL |
| 49 |
func (x *Executor) ExecCommand(command string, setpgid bool) *exec.Cmd { |
| 50 |
cmd := exec.Command(x.shell, append(x.args, command)...) |
| 51 |
if setpgid { |
| 52 |
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} |
| 53 |
} |
| 54 |
return cmd |
| 55 |
} |
| 56 |
|
| 57 |
func (x *Executor) QuoteEntry(entry string) string { |
| 58 |
return "'" + x.escaper.Replace(entry) + "'" |
| 59 |
} |
| 60 |
|
| 61 |
func (x *Executor) Become(stdin *os.File, environ []string, command string) { |
| 62 |
shellPath, err := exec.LookPath(x.shell) |
| 63 |
if err != nil { |
| 64 |
fmt.Fprintf(os.Stderr, "fzf (become): %s\n", err.Error()) |
| 65 |
os.Exit(127) |
| 66 |
} |
| 67 |
args := append([]string{shellPath}, append(x.args, command)...) |
| 68 |
SetStdin(stdin) |
| 69 |
syscall.Exec(shellPath, args, environ) |
| 70 |
} |
| 71 |
|
| 72 |
// KillCommand kills the process for the given command |
| 73 |
func KillCommand(cmd *exec.Cmd) error { |
| 74 |
return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) |
| 75 |
} |
| 76 |
|
| 77 |
// IsWindows returns true on Windows |
| 78 |
func IsWindows() bool { |
| 79 |
return false |
| 80 |
} |
| 81 |
|
| 82 |
// SetNonblock executes syscall.SetNonblock on file descriptor |
| 83 |
func SetNonblock(file *os.File, nonblock bool) { |
| 84 |
syscall.SetNonblock(int(file.Fd()), nonblock) |
| 85 |
} |
| 86 |
|
| 87 |
// Read executes syscall.Read on file descriptor |
| 88 |
func Read(fd int, b []byte) (int, error) { |
| 89 |
return syscall.Read(int(fd), b) |
| 90 |
} |
| 91 |
|
| 92 |
func SetStdin(file *os.File) { |
| 93 |
unix.Dup2(int(file.Fd()), 0) |
| 94 |
} |