/
nodes.go
393 lines (317 loc) · 8.35 KB
/
nodes.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package main
import (
"bytes"
"io"
"os/exec"
"os/user"
"strings"
"gopkg.in/logex.v1"
"github.com/danwakefield/fnmatch"
"github.com/danwakefield/gosh/T"
"github.com/danwakefield/gosh/builtins"
"github.com/danwakefield/gosh/variables"
)
type Arg struct {
Raw string
Quoted bool
Subs []Substitution
}
type ExpandFlag int
const (
NoExpandSubstitutions ExpandFlag = iota
NoExpandTilde
NoExpandWordSplit
NoExpandGlob
)
func (a Arg) Expand(scp *variables.Scope, flags ...ExpandFlag) (returnString string) {
flagSet := func(e ExpandFlag) bool {
for _, v := range flags {
if v == e {
return true
}
}
return false
}
logex.Debugf("Expand '%s'", a.Raw)
defer func() {
logex.Debugf("Returned '%s'", returnString)
}()
expString := a.Raw
if !flagSet(NoExpandTilde) {
expString = a.expandTilde(scp, expString)
}
if !flagSet(NoExpandSubstitutions) {
expString = a.expandSubstitutions(scp, expString)
}
if !flagSet(NoExpandGlob) {
}
// XXX: Do FNmatch pathname expansion here.
// see `man 7 glob` for details. Key point is that is the expansion
// has no files it should be returned as is.
return expString
}
func (a Arg) expandSubstitutions(scp *variables.Scope, s string) string {
if !strings.ContainsRune(s, SentinalSubstitution) {
return s
}
buf := bytes.Buffer{}
subCounter := 0
for _, r := range a.Raw {
if r == SentinalSubstitution {
buf.WriteString(a.Subs[subCounter].Sub(scp))
subCounter++
continue
}
buf.WriteRune(r)
}
return buf.String()
}
func (a Arg) expandTilde(scp *variables.Scope, s string) string {
if strings.HasPrefix(s, "~") && !a.Quoted {
u, err := user.Current()
if err != nil {
return s
}
return u.HomeDir + s[1:]
} else {
return s
}
}
type Node interface {
Eval(*variables.Scope, *T.IOContainer) T.ExitStatus
}
type NodeNoop struct{}
func (NodeNoop) Eval(*variables.Scope, *T.IOContainer) T.ExitStatus { return T.ExitSuccess }
// NodeEOF is end of file sentinal node.
type NodeEOF struct {
NodeNoop
}
type NodeList []Node
// Eval calls Eval on the Nodes contained in the list and returns the
// T.ExitStatus of the last command.
func (n NodeList) Eval(scp *variables.Scope, ioc *T.IOContainer) T.ExitStatus {
returnExit := T.ExitSuccess
for _, x := range n {
returnExit = x.Eval(scp, ioc)
}
return returnExit
}
// NodeBinary is used to chain nodes conditionally
type NodeBinary struct {
Left, Right Node
IsAnd bool
}
func (n NodeBinary) Eval(scp *variables.Scope, ioc *T.IOContainer) T.ExitStatus {
var runRight bool
leftExit := n.Left.Eval(scp, ioc)
if n.IsAnd {
runRight = leftExit == T.ExitSuccess
} else { // OR
runRight = leftExit != T.ExitSuccess
}
if runRight {
return n.Right.Eval(scp, ioc)
}
return leftExit
}
// NodeNegate is used to flip the T.ExitStatus of the contained Node
type NodeNegate struct {
N Node
}
func (n NodeNegate) Eval(scp *variables.Scope, ioc *T.IOContainer) T.ExitStatus {
ex := n.N.Eval(scp, ioc)
// Any Non-zero T.ExitStatus is a failure so we only check for success
if ex == T.ExitSuccess {
return T.ExitFailure
}
return T.ExitSuccess
}
type NodeLoop struct {
IsWhile bool
Condition Node
Body Node
}
func (n NodeLoop) Eval(scp *variables.Scope, ioc *T.IOContainer) T.ExitStatus {
var runBody bool
returnExit := T.ExitSuccess
for {
condExit := n.Condition.Eval(scp, ioc)
if n.IsWhile {
runBody = condExit == T.ExitSuccess
} else { // Until
runBody = condExit != T.ExitSuccess
}
if runBody {
returnExit = n.Body.Eval(scp, ioc)
} else {
break
}
}
return returnExit
}
type NodeFor struct {
LoopVar string
Args []Arg
Body Node
}
func (n NodeFor) Eval(scp *variables.Scope, ioc *T.IOContainer) T.ExitStatus {
returnExit := T.ExitSuccess
expandedArgs := make([]string, len(n.Args))
for i, arg := range n.Args {
// This will need to be changed when IFS splitting is coded.
// Append each split as a seperate item
expandedArgs[i] = arg.Expand(scp)
}
for _, arg := range expandedArgs {
scp.Set(n.LoopVar, arg)
returnExit = n.Body.Eval(scp, ioc)
}
return returnExit
}
// NodeIf is the structure that is used for 'if', 'elif' and 'else'
// as an 'if' or 'elif' Condition is required and Else is optionally nil to
// indicate the end of the if chain.
// as an 'else' Condition is required to be nil.
type NodeIf struct {
Condition Node
Else *NodeIf
Body Node
}
func (n NodeIf) Eval(scp *variables.Scope, ioc *T.IOContainer) T.ExitStatus {
runBody := n.Condition.Eval(scp, ioc)
if runBody == T.ExitSuccess {
return n.Body.Eval(scp, ioc)
}
if n.Else != nil {
return n.Else.Eval(scp, ioc)
}
return T.ExitSuccess
}
type NodeCommand struct {
Assign map[string]Arg
Args []Arg
LineNo int
}
func (n NodeCommand) execExternal(scp *variables.Scope, ioc *T.IOContainer, args []string) T.ExitStatus {
// This is needed so that pipes will terminate
if pw, isPipeWriter := ioc.Out.(*io.PipeWriter); isPipeWriter {
defer func() {
if err := pw.Close(); err != nil {
panic(err) // XXX: Print error to stdout and continue?
}
}()
}
cmd := exec.Command(args[0], args[1:]...)
cmd.Env = scp.Environ()
cmd.Stdin = ioc.In
cmd.Stderr = ioc.Err
cmd.Stdout = ioc.Out
err := cmd.Run()
if err == nil {
return T.ExitSuccess
}
return T.ExitFailure
}
func (n NodeCommand) execFunction(scp *variables.Scope, ioc *T.IOContainer, args []string) T.ExitStatus {
return T.ExitSuccess
}
func (n NodeCommand) Eval(scp *variables.Scope, ioc *T.IOContainer) T.ExitStatus {
// A line with only assignments applies them to the Root Scope
// We check this first to avoid unnecessary scope Push/Pop's
if len(n.Args) == 0 {
for k, v := range n.Assign {
scp.Set(k, v.Expand(scp))
}
return T.ExitSuccess
}
// Minimum of len(n.Args) after expansions, Likely
// that it will be more after globbing though
expandedArgs := []string{}
for _, arg := range n.Args {
expandedArgs = append(expandedArgs, arg.Expand(scp))
}
// Order of precedence:
// Relative command > Builtin > User Function > Other external command
command := expandedArgs[0]
builtinFunc, builtinFound := builtins.All[command]
userFunc, userFuncFound := scp.Functions[command]
if strings.ContainsRune(command, '/') || (!builtinFound && !userFuncFound) {
scp.Push()
defer scp.Pop()
for k, v := range n.Assign {
scp.Set(k, v.Expand(scp), variables.LocalScope)
}
return n.execExternal(scp, ioc, expandedArgs)
}
if builtinFound {
return builtinFunc(scp, ioc, expandedArgs[1:])
}
if userFuncFound {
x := userFunc.(NodeFunction)
return x.EvalFunc(scp, ioc, expandedArgs[1:])
}
return T.ExitUnknownCommand
}
type NodeCaseList struct {
Patterns []Arg
Body Node
}
func (n NodeCaseList) Eval(scp *variables.Scope, ioc *T.IOContainer) T.ExitStatus {
return n.Body.Eval(scp, ioc)
}
func (n NodeCaseList) Matches(s string, scp *variables.Scope) bool {
for _, p := range n.Patterns {
expandedPat := p.Expand(scp)
if fnmatch.Match(expandedPat, s, 0) {
return true
}
}
return false
}
type NodeCase struct {
Expr Arg
Cases []NodeCaseList
}
func (n NodeCase) Eval(scp *variables.Scope, ioc *T.IOContainer) T.ExitStatus {
expandedExpr := n.Expr.Expand(scp)
for _, c := range n.Cases {
if c.Matches(expandedExpr, scp) {
return c.Eval(scp, ioc)
}
}
return T.ExitSuccess
}
type NodePipe struct {
Background bool
Commands NodeList
}
func (n NodePipe) Eval(scp *variables.Scope, ioc *T.IOContainer) T.ExitStatus {
lastPipeReader, pipeWriter := io.Pipe()
scp = scp.Copy()
cmd := n.Commands[0]
go cmd.Eval(scp, &T.IOContainer{In: &bytes.Buffer{}, Out: pipeWriter, Err: ioc.Err})
for _, cmd = range n.Commands[1 : len(n.Commands)-1] {
pipeReader, pipeWriter := io.Pipe()
go cmd.Eval(scp, &T.IOContainer{In: lastPipeReader, Out: pipeWriter, Err: ioc.Err})
lastPipeReader = pipeReader
}
cmd = n.Commands[len(n.Commands)-1]
if !n.Background {
return cmd.Eval(scp, &T.IOContainer{In: lastPipeReader, Out: ioc.Out, Err: ioc.Err})
}
go cmd.Eval(scp, &T.IOContainer{In: lastPipeReader, Out: ioc.Out, Err: ioc.Err})
return T.ExitSuccess
}
type NodeFunction struct {
Body Node
Name string
}
func (n NodeFunction) Eval(scp *variables.Scope, ioc *T.IOContainer) T.ExitStatus {
scp.Functions[n.Name] = n
return T.ExitSuccess
}
func (n NodeFunction) EvalFunc(scp *variables.Scope, ioc *T.IOContainer, args []string) T.ExitStatus {
scp.PushFunction(args)
defer scp.Pop()
return n.Body.Eval(scp, ioc)
}