2016-02-19 05:52:05 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-12-18 23:22:59 +08:00
|
|
|
"io/ioutil"
|
2016-02-19 05:52:05 +08:00
|
|
|
"os"
|
2017-12-05 07:48:38 +08:00
|
|
|
"os/exec"
|
2016-02-19 05:52:05 +08:00
|
|
|
"syscall"
|
2016-02-21 21:32:13 +08:00
|
|
|
|
2019-12-24 04:00:59 +08:00
|
|
|
"github.com/elves/elvish/pkg/eval/vals"
|
|
|
|
"github.com/elves/elvish/pkg/parse"
|
|
|
|
"github.com/elves/elvish/pkg/util"
|
2017-08-31 02:52:27 +08:00
|
|
|
"github.com/xiaq/persistent/hash"
|
2016-02-19 05:52:05 +08:00
|
|
|
)
|
|
|
|
|
2016-09-15 05:34:10 +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.
|
2016-09-15 05:34:10 +08:00
|
|
|
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-09-15 05:34:10 +08:00
|
|
|
)
|
2016-02-19 05:52:05 +08:00
|
|
|
|
|
|
|
// ExternalCmd is an external command.
|
|
|
|
type ExternalCmd struct {
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ExternalCmd) Kind() string {
|
|
|
|
return "fn"
|
|
|
|
}
|
|
|
|
|
2017-08-31 01:47:50 +08:00
|
|
|
func (e ExternalCmd) Equal(a interface{}) bool {
|
2017-07-14 08:15:14 +08:00
|
|
|
return e == a
|
|
|
|
}
|
|
|
|
|
2017-08-31 02:52:27 +08:00
|
|
|
func (e ExternalCmd) Hash() uint32 {
|
|
|
|
return hash.String(e.Name)
|
|
|
|
}
|
|
|
|
|
2016-02-20 03:11:31 +08:00
|
|
|
func (e ExternalCmd) Repr(int) string {
|
2016-04-05 12:20:18 +08:00
|
|
|
return "<external " + parse.Quote(e.Name) + ">"
|
2016-02-19 05:52:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call calls an external command.
|
2018-03-01 10:17:56 +08:00
|
|
|
func (e ExternalCmd) Call(fm *Frame, argVals []interface{}, opts map[string]interface{}) error {
|
2016-09-15 05:34:10 +08:00
|
|
|
if len(opts) > 0 {
|
2018-01-21 19:05:36 +08:00
|
|
|
return ErrExternalCmdOpts
|
2016-09-15 05:34:10 +08:00
|
|
|
}
|
2016-02-21 21:32:13 +08:00
|
|
|
if util.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
|
|
|
}
|
2018-03-22 04:51:08 +08:00
|
|
|
return fm.Chdir(e.Name)
|
2016-02-19 05:52:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 10:17:56 +08:00
|
|
|
files := make([]*os.File, len(fm.ports))
|
|
|
|
for i, port := range fm.ports {
|
2020-01-12 20:32:25 +08:00
|
|
|
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 {
|
|
|
|
// NOTE Maybe we should enfore string arguments instead of coercing all
|
|
|
|
// args into string
|
2018-02-15 17:14:05 +08:00
|
|
|
args[i+1] = vals.ToString(a)
|
2016-02-19 05:52:05 +08:00
|
|
|
}
|
|
|
|
|
2017-12-05 07:48:38 +08:00
|
|
|
path, err := exec.LookPath(e.Name)
|
2016-02-19 05:52:05 +08:00
|
|
|
if err != nil {
|
2018-01-21 19:05:36 +08:00
|
|
|
return err
|
2016-02-19 05:52:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
args[0] = path
|
2017-12-04 04:46:08 +08:00
|
|
|
|
2018-03-01 10:17:56 +08:00
|
|
|
sys := makeSysProcAttr(fm.background)
|
2017-12-04 04:46:08 +08:00
|
|
|
proc, err := os.StartProcess(path, args, &os.ProcAttr{Files: files, Sys: sys})
|
|
|
|
|
2016-02-19 05:52:05 +08:00
|
|
|
if err != nil {
|
2018-01-21 19:05:36 +08:00
|
|
|
return err
|
2016-02-19 05:52:05 +08:00
|
|
|
}
|
|
|
|
|
2017-12-04 04:46:08 +08:00
|
|
|
state, err := proc.Wait()
|
2016-03-17 19:32:48 +08:00
|
|
|
|
2016-02-19 05:52:05 +08:00
|
|
|
if err != nil {
|
2018-01-21 19:05:36 +08:00
|
|
|
return err
|
2016-02-19 05:52:05 +08:00
|
|
|
}
|
2018-01-21 19:05:36 +08:00
|
|
|
return NewExternalCmdExit(e.Name, state.Sys().(syscall.WaitStatus), proc.Pid)
|
2016-02-19 05:52:05 +08:00
|
|
|
}
|
2017-12-18 23:22:59 +08:00
|
|
|
|
|
|
|
// EachExternal calls f for each name that can resolve to an external
|
|
|
|
// command.
|
|
|
|
// TODO(xiaq): Windows support
|
|
|
|
func EachExternal(f func(string)) {
|
|
|
|
for _, dir := range searchPaths() {
|
2020-08-09 11:42:32 +08:00
|
|
|
// TODO(xiaq): Ignore error.
|
2017-12-18 23:22:59 +08:00
|
|
|
infos, _ := ioutil.ReadDir(dir)
|
|
|
|
for _, info := range infos {
|
|
|
|
if !info.IsDir() && (info.Mode()&0111 != 0) {
|
|
|
|
f(info.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|