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
|
2018-02-04 12:52:54 +08:00
|
|
|
"external": external,
|
|
|
|
"has-external": hasExternal,
|
|
|
|
"search-external": searchExternal,
|
2017-12-17 13:20:03 +08:00
|
|
|
|
|
|
|
// Process control
|
2018-02-04 12:52:54 +08:00
|
|
|
"fg": fg,
|
|
|
|
"exec": execFn,
|
|
|
|
"exit": exit,
|
2017-12-17 13:20:03 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-04 12:52:54 +08:00
|
|
|
func external(cmd string) ExternalCmd {
|
|
|
|
return ExternalCmd{cmd}
|
2018-01-03 07:26:28 +08:00
|
|
|
}
|
|
|
|
|
2018-02-04 12:52:54 +08:00
|
|
|
func hasExternal(cmd string) bool {
|
2018-01-25 09:40:15 +08:00
|
|
|
_, err := exec.LookPath(cmd)
|
2018-02-04 12:52:54 +08:00
|
|
|
return err == nil
|
2017-12-17 13:20:03 +08:00
|
|
|
}
|
|
|
|
|
2018-02-04 12:52:54 +08:00
|
|
|
func searchExternal(cmd string) (string, error) {
|
|
|
|
return exec.LookPath(cmd)
|
2017-12-17 13:20:03 +08:00
|
|
|
}
|
|
|
|
|
2018-02-04 12:52:54 +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:
|
2018-02-04 12:52:54 +08:00
|
|
|
code = codes[0]
|
2017-12-17 13:20:03 +08:00
|
|
|
default:
|
2018-02-04 12:52:54 +08:00
|
|
|
return ErrArgs
|
2017-12-17 13:20:03 +08:00
|
|
|
}
|
2018-02-04 12:52:54 +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) {
|
2017-12-23 07:45:47 +08:00
|
|
|
err := ec.DaemonClient.Close()
|
2017-12-17 13:20:03 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
|
|
|
}
|
2018-01-01 04:31:45 +08:00
|
|
|
|
|
|
|
var errNotSupportedOnWindows = errors.New("not supported on Windows")
|
|
|
|
|
2018-02-04 12:52:54 +08:00
|
|
|
func notSupportedOnWindows() error {
|
|
|
|
return errNotSupportedOnWindows
|
2018-01-01 04:31:45 +08:00
|
|
|
}
|