-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
builtin_fn_cmd.go
68 lines (55 loc) · 1.1 KB
/
builtin_fn_cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package eval
import (
"errors"
"fmt"
"os"
"os/exec"
)
// Command and process control.
var ErrNotInSameGroup = errors.New("not in the same process group")
func init() {
addBuiltinFns(map[string]interface{}{
// Command resolution
"external": external,
"has-external": hasExternal,
"search-external": searchExternal,
// Process control
"fg": fg,
"exec": execFn,
"exit": exit,
})
}
func external(cmd string) ExternalCmd {
return ExternalCmd{cmd}
}
func hasExternal(cmd string) bool {
_, err := exec.LookPath(cmd)
return err == nil
}
func searchExternal(cmd string) (string, error) {
return exec.LookPath(cmd)
}
func exit(fm *Frame, codes ...int) error {
code := 0
switch len(codes) {
case 0:
case 1:
code = codes[0]
default:
return ErrArgs
}
preExit(fm)
os.Exit(code)
// Does not return
panic("os.Exit returned")
}
func preExit(fm *Frame) {
err := fm.DaemonClient.Close()
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
var errNotSupportedOnWindows = errors.New("not supported on Windows")
func notSupportedOnWindows() error {
return errNotSupportedOnWindows
}