forked from u-root/u-root
/
rush.go
227 lines (212 loc) · 4.79 KB
/
rush.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright 2012-2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Rush is an interactive shell similar to sh.
//
// Description:
// Prompt is '% '.
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"syscall"
)
type builtin func(c *Command) error
// TODO: probably have one builtin map and use it for both types?
var (
urpath = "/go/bin:/ubin:/buildbin:/bbin:/bin:/usr/local/bin:"
builtins = make(map[string]builtin)
// the environment dir is INTENDED to be per-user and bound in
// a private name space at /env.
envDir = "/env"
)
func addBuiltIn(name string, f builtin) error {
if _, ok := builtins[name]; ok {
return fmt.Errorf("%v already a builtin", name)
}
builtins[name] = f
return nil
}
func wire(cmds []*Command) error {
for i, c := range cmds {
// IO defaults.
var err error
if c.Stdin == nil {
if c.Stdin, err = openRead(c, os.Stdin, 0); err != nil {
return err
}
}
if c.link != "|" {
if c.Stdout, err = openWrite(c, os.Stdout, 1); err != nil {
return err
}
}
if c.Stderr, err = openWrite(c, os.Stderr, 2); err != nil {
return err
}
// The validation is such that "|" is not set on the last one.
// Also, there won't be redirects and "|" inappropriately.
if c.link != "|" {
continue
}
w, err := cmds[i+1].StdinPipe()
if err != nil {
return err
}
r, err := cmds[i].StdoutPipe()
if err != nil {
return err
}
// Oh, yuck.
// There seems to be no way to do the classic
// inherited pipes thing in Go. Hard to believe.
go func() {
io.Copy(w, r)
w.Close()
}()
}
return nil
}
func runit(c *Command) error {
defer func() {
for fd, f := range c.files {
f.Close()
delete(c.files, fd)
}
}()
if b, ok := builtins[c.cmd]; ok {
if err := b(c); err != nil {
return err
}
} else {
c.Cmd.SysProcAttr = &syscall.SysProcAttr{}
if c.bg {
c.Cmd.SysProcAttr.Setpgid = true
} else {
c.Cmd.SysProcAttr.Foreground = true
c.Cmd.SysProcAttr.Ctty = int(ttyf.Fd())
}
if err := c.Start(); err != nil {
return fmt.Errorf("%v: Path %v", err, os.Getenv("PATH"))
}
if err := c.Wait(); err != nil {
return fmt.Errorf("wait: %v", err)
}
}
return nil
}
func openRead(c *Command, r io.Reader, fd int) (io.Reader, error) {
if c.fdmap[fd] != "" {
f, err := os.Open(c.fdmap[fd])
c.files[fd] = f
return f, err
}
return r, nil
}
func openWrite(c *Command, w io.Writer, fd int) (io.Writer, error) {
if c.fdmap[fd] != "" {
f, err := os.Create(c.fdmap[fd])
c.files[fd] = f
return f, err
}
return w, nil
}
func doArgs(cmds []*Command) error {
for _, c := range cmds {
globargv := []string{}
for _, v := range c.args {
if v.mod == "ENV" {
e := v.val
if !path.IsAbs(v.val) {
e = filepath.Join(envDir, e)
}
b, err := ioutil.ReadFile(e)
if err != nil {
return err
}
// It goes in as one argument. Not sure if this is what we want
// but it gets very weird to start splitting it on spaces. Or maybe not?
globargv = append(globargv, string(b))
} else if globs, err := filepath.Glob(v.val); err == nil && len(globs) > 0 {
globargv = append(globargv, globs...)
} else {
globargv = append(globargv, v.val)
}
}
c.cmd = globargv[0]
c.argv = globargv[1:]
}
return nil
}
// There seems to be no harm in creating a Cmd struct
// even for builtins, so for now, we do.
// It will, however, do a path lookup, which we really don't need,
// and we may change it later.
func commands(cmds []*Command) error {
for _, c := range cmds {
c.Cmd = exec.Command(c.cmd, c.argv[:]...)
// this is a Very Special Case related to a Go issue.
// we're not able to unshare correctly in builtin.
// Not sure of the issue but this hack will have to do until
// we understand it. Barf.
if c.cmd == "builtin" {
c.Cmd.SysProcAttr.Cloneflags |= syscall.CLONE_NEWNS
}
}
return nil
}
func command(c *Command) error {
// for now, bg will just happen in background.
if c.bg {
go func() {
if err := runit(c); err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
}
}()
} else {
err := runit(c)
return err
}
return nil
}
func rush(b *bufio.Reader) error {
foreground()
cmds, _, err := getCommand(b)
if err != nil {
return err
}
if err := doArgs(cmds); err != nil {
return err
}
if err := commands(cmds); err != nil {
return err
}
if err := wire(cmds); err != nil {
return err
}
for _, c := range cmds {
if err := command(c); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
if c.link == "||" {
continue
}
// yes, not needed, but useful so you know
// what goes on here.
if c.link == "&&" {
break
}
break
} else {
if c.link == "||" {
break
}
}
}
return nil
}