elvish/eval/builtin_fn_cmd.go

69 lines
1.1 KiB
Go
Raw Normal View History

2017-12-17 13:20:03 +08:00
package eval
import (
"errors"
"fmt"
"os"
"os/exec"
)
// Command and process control.
var ErrNotInSameGroup = errors.New("not in the same process group")
func init() {
2018-02-06 16:02:27 +08:00
addToBuiltinFns(map[string]interface{}{
2017-12-17 13:20:03 +08:00
// Command resolution
"external": external,
"has-external": hasExternal,
"search-external": searchExternal,
2017-12-17 13:20:03 +08:00
// Process control
"fg": fg,
"exec": execFn,
"exit": exit,
2017-12-17 13:20:03 +08:00
})
}
func external(cmd string) ExternalCmd {
return ExternalCmd{cmd}
2018-01-03 07:26:28 +08:00
}
func hasExternal(cmd string) bool {
_, err := exec.LookPath(cmd)
return err == nil
2017-12-17 13:20:03 +08:00
}
func searchExternal(cmd string) (string, error) {
return exec.LookPath(cmd)
2017-12-17 13:20:03 +08:00
}
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 ErrArgs
2017-12-17 13:20:03 +08:00
}
preExit(fm)
os.Exit(code)
// Does not return
panic("os.Exit returned")
2017-12-17 13:20:03 +08:00
}
2017-12-28 03:59:42 +08:00
func preExit(ec *Frame) {
err := ec.DaemonClient.Close()
2017-12-17 13:20:03 +08:00
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
var errNotSupportedOnWindows = errors.New("not supported on Windows")
func notSupportedOnWindows() error {
return errNotSupportedOnWindows
}