elvish/pkg/eval/external_cmd.go

107 lines
2.5 KiB
Go
Raw Normal View History

2016-02-19 05:52:05 +08:00
package eval
import (
"errors"
"os"
"os/exec"
2016-02-19 05:52:05 +08:00
"syscall"
2017-08-31 02:52:27 +08:00
"github.com/xiaq/persistent/hash"
"src.elv.sh/pkg/eval/vals"
"src.elv.sh/pkg/fsutil"
"src.elv.sh/pkg/parse"
2016-02-19 05:52:05 +08:00
)
var (
2019-04-19 05:15:34 +08:00
// ErrExternalCmdOpts is thrown when an external command is passed Elvish
// options.
//
// TODO: Catch this kind of errors at compilation time.
ErrExternalCmdOpts = errors.New("external commands don't accept elvish options")
2019-04-19 05:15:34 +08:00
// ErrImplicitCdNoArg is thrown when an implicit cd form is passed arguments.
ErrImplicitCdNoArg = errors.New("implicit cd accepts no arguments")
)
2016-02-19 05:52:05 +08:00
// externalCmd is an external command.
type externalCmd struct {
2016-02-19 05:52:05 +08:00
Name string
}
// NewExternalCmd returns a callable that executes the named external command.
//
// An external command converts all arguments to strings, and does not accept
// any option.
func NewExternalCmd(name string) Callable {
return externalCmd{name}
}
func (e externalCmd) Kind() string {
2016-02-19 05:52:05 +08:00
return "fn"
}
func (e externalCmd) Equal(a interface{}) bool {
return e == a
}
func (e externalCmd) Hash() uint32 {
2017-08-31 02:52:27 +08:00
return hash.String(e.Name)
}
func (e externalCmd) Repr(int) string {
return "<external " + parse.Quote(e.Name) + ">"
2016-02-19 05:52:05 +08:00
}
// Call calls an external command.
func (e externalCmd) Call(fm *Frame, argVals []interface{}, opts map[string]interface{}) error {
if len(opts) > 0 {
return ErrExternalCmdOpts
}
if fsutil.DontSearch(e.Name) {
2016-02-19 05:52:05 +08:00
stat, err := os.Stat(e.Name)
if err == nil && stat.IsDir() {
// implicit cd
if len(argVals) > 0 {
2019-04-19 05:15:34 +08:00
return ErrImplicitCdNoArg
2016-02-19 05:52:05 +08:00
}
return fm.Evaler.Chdir(e.Name)
2016-02-19 05:52:05 +08:00
}
}
files := make([]*os.File, len(fm.ports))
for i, port := range fm.ports {
if port != nil {
files[i] = port.File
}
2016-02-19 05:52:05 +08:00
}
args := make([]string, len(argVals)+1)
for i, a := range argVals {
// TODO: Maybe we should enforce string arguments instead of coercing
// all args to strings.
2018-02-15 17:14:05 +08:00
args[i+1] = vals.ToString(a)
2016-02-19 05:52:05 +08:00
}
path, err := exec.LookPath(e.Name)
2016-02-19 05:52:05 +08:00
if err != nil {
return err
2016-02-19 05:52:05 +08:00
}
args[0] = path
sys := makeSysProcAttr(fm.background)
proc, err := os.StartProcess(path, args, &os.ProcAttr{Files: files, Sys: sys})
2016-02-19 05:52:05 +08:00
if err != nil {
return err
2016-02-19 05:52:05 +08:00
}
state, err := proc.Wait()
2016-02-19 05:52:05 +08:00
if err != nil {
// This should be a can't happen situation. Nonetheless, treat it as a
2020-10-06 02:35:52 +08:00
// soft error rather than panicking since the Go documentation is not
// explicit that this can only happen if we make a mistake. Such as
// calling `Wait` twice on a particular process object.
return err
2016-02-19 05:52:05 +08:00
}
return NewExternalCmdExit(e.Name, state.Sys().(syscall.WaitStatus), proc.Pid)
2016-02-19 05:52:05 +08:00
}