mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-05 03:17:50 +08:00
6e8ecc8bd5
- Move NewEnvListVar to pkg/eval/vars. - De-export GlobPattern, GlobFlag and ExternalCmd. - Merge editor.go and chdir.go into eval.go, value_helper.go into compile_value.go. - Remove eval_internal_test.go and replace it with a new test for $pid.
107 lines
2.5 KiB
Go
107 lines
2.5 KiB
Go
package eval
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"os/exec"
|
|
"syscall"
|
|
|
|
"github.com/elves/elvish/pkg/eval/vals"
|
|
"github.com/elves/elvish/pkg/fsutil"
|
|
"github.com/elves/elvish/pkg/parse"
|
|
"github.com/xiaq/persistent/hash"
|
|
)
|
|
|
|
var (
|
|
// 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")
|
|
// ErrImplicitCdNoArg is thrown when an implicit cd form is passed arguments.
|
|
ErrImplicitCdNoArg = errors.New("implicit cd accepts no arguments")
|
|
)
|
|
|
|
// externalCmd is an external command.
|
|
type externalCmd struct {
|
|
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 {
|
|
return "fn"
|
|
}
|
|
|
|
func (e externalCmd) Equal(a interface{}) bool {
|
|
return e == a
|
|
}
|
|
|
|
func (e externalCmd) Hash() uint32 {
|
|
return hash.String(e.Name)
|
|
}
|
|
|
|
func (e externalCmd) Repr(int) string {
|
|
return "<external " + parse.Quote(e.Name) + ">"
|
|
}
|
|
|
|
// 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) {
|
|
stat, err := os.Stat(e.Name)
|
|
if err == nil && stat.IsDir() {
|
|
// implicit cd
|
|
if len(argVals) > 0 {
|
|
return ErrImplicitCdNoArg
|
|
}
|
|
return fm.Evaler.Chdir(e.Name)
|
|
}
|
|
}
|
|
|
|
files := make([]*os.File, len(fm.ports))
|
|
for i, port := range fm.ports {
|
|
if port != nil {
|
|
files[i] = port.File
|
|
}
|
|
}
|
|
|
|
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.
|
|
args[i+1] = vals.ToString(a)
|
|
}
|
|
|
|
path, err := exec.LookPath(e.Name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
args[0] = path
|
|
|
|
sys := makeSysProcAttr(fm.background)
|
|
proc, err := os.StartProcess(path, args, &os.ProcAttr{Files: files, Sys: sys})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
state, err := proc.Wait()
|
|
if err != nil {
|
|
// This should be a can't happen situation. Nonetheless, treat it as a
|
|
// 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
|
|
}
|
|
return NewExternalCmdExit(e.Name, state.Sys().(syscall.WaitStatus), proc.Pid)
|
|
}
|