elvish/pkg/eval/builtin_fn_cmd.go

58 lines
941 B
Go
Raw Normal View History

2017-12-17 13:20:03 +08:00
package eval
import (
"os"
"os/exec"
"src.elv.sh/pkg/eval/errs"
2017-12-17 13:20:03 +08:00
)
// Command and process control.
// TODO(xiaq): Document "fg".
func init() {
addBuiltinFns(map[string]any{
// Command resolution
"external": external,
"has-external": hasExternal,
"search-external": searchExternal,
// Process control
"fg": fg,
"exec": execFn,
"exit": exit,
})
}
func external(cmd string) Callable {
return NewExternalCmd(cmd)
}
func hasExternal(cmd string) bool {
_, err := exec.LookPath(cmd)
return err == nil
}
func searchExternal(cmd string) (string, error) {
return exec.LookPath(cmd)
}
// Can be overridden in tests.
var osExit = os.Exit
func exit(fm *Frame, codes ...int) error {
code := 0
2017-12-17 13:20:03 +08:00
switch len(codes) {
case 0:
case 1:
code = codes[0]
2017-12-17 13:20:03 +08:00
default:
return errs.ArityMismatch{What: "arguments", ValidLow: 0, ValidHigh: 1, Actual: len(codes)}
2017-12-17 13:20:03 +08:00
}
fm.Evaler.PreExit()
osExit(code)
return nil
2017-12-17 13:20:03 +08:00
}