2017-12-17 13:20:03 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2021-06-04 10:51:38 +08:00
|
|
|
|
|
|
|
"src.elv.sh/pkg/eval/errs"
|
2017-12-17 13:20:03 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Command and process control.
|
|
|
|
|
2020-08-17 01:46:29 +08:00
|
|
|
// TODO(xiaq): Document "fg".
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
addBuiltinFns(map[string]interface{}{
|
|
|
|
// Command resolution
|
|
|
|
"external": external,
|
|
|
|
"has-external": hasExternal,
|
|
|
|
"search-external": searchExternal,
|
|
|
|
|
|
|
|
// Process control
|
|
|
|
"fg": fg,
|
|
|
|
"exec": execFn,
|
|
|
|
"exit": exit,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-18 21:12:50 +08:00
|
|
|
//elvdoc:fn external
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// external $program
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Construct a callable value for the external program `$program`. Example:
|
|
|
|
//
|
|
|
|
// ```elvish-transcript
|
|
|
|
// ~> x = (external man)
|
|
|
|
// ~> $x ls # opens the manpage for ls
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// @cf has-external search-external
|
|
|
|
|
2021-01-05 12:07:35 +08:00
|
|
|
func external(cmd string) Callable {
|
|
|
|
return NewExternalCmd(cmd)
|
2020-08-17 01:46:29 +08:00
|
|
|
}
|
|
|
|
|
2020-01-18 21:12:50 +08:00
|
|
|
//elvdoc:fn has-external
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// has-external $command
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Test whether `$command` names a valid external command. Examples (your output
|
|
|
|
// might differ):
|
|
|
|
//
|
|
|
|
// ```elvish-transcript
|
|
|
|
// ~> has-external cat
|
|
|
|
// ▶ $true
|
|
|
|
// ~> has-external lalala
|
|
|
|
// ▶ $false
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// @cf external search-external
|
|
|
|
|
2020-08-17 01:46:29 +08:00
|
|
|
func hasExternal(cmd string) bool {
|
|
|
|
_, err := exec.LookPath(cmd)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2020-01-18 21:12:50 +08:00
|
|
|
//elvdoc:fn search-external
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// search-external $command
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Output the full path of the external `$command`. Throws an exception when not
|
|
|
|
// found. Example (your output might vary):
|
|
|
|
//
|
|
|
|
// ```elvish-transcript
|
|
|
|
// ~> search-external cat
|
|
|
|
// ▶ /bin/cat
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// @cf external has-external
|
|
|
|
|
2020-08-17 01:46:29 +08:00
|
|
|
func searchExternal(cmd string) (string, error) {
|
|
|
|
return exec.LookPath(cmd)
|
|
|
|
}
|
2020-01-18 21:12:50 +08:00
|
|
|
|
|
|
|
//elvdoc:fn exit
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// exit $status?
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Exit the Elvish process with `$status` (defaulting to 0).
|
|
|
|
|
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:
|
2021-06-04 10:51:38 +08:00
|
|
|
return errs.ArityMismatch{What: "arguments", ValidLow: 0, ValidHigh: 1, Actual: len(codes)}
|
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
|
|
|
}
|
|
|
|
|
2018-03-01 10:17:56 +08:00
|
|
|
func preExit(fm *Frame) {
|
2021-01-05 07:54:13 +08:00
|
|
|
daemon := fm.Evaler.DaemonClient()
|
|
|
|
if daemon != nil {
|
|
|
|
err := daemon.Close()
|
2020-05-29 07:20:12 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
2017-12-17 13:20:03 +08:00
|
|
|
}
|
|
|
|
}
|