-
Notifications
You must be signed in to change notification settings - Fork 4
/
for.go
177 lines (155 loc) · 3.75 KB
/
for.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
// Copyright © 2021 Platina Systems, Inc. All rights reserved.
// Use of this source code is governed by the GPL-2 license described in the
// LICENSE file.
package forcmd
import (
"errors"
"fmt"
"io"
"github.com/platinasystems/goes"
"github.com/platinasystems/goes/internal/shellutils"
"github.com/platinasystems/goes/lang"
)
type Command struct{}
func (Command) String() string { return "for" }
func (Command) Usage() string {
return "for VAR in ARGS... ; do COMMAND $VAR; done"
}
func (Command) Apropos() lang.Alt {
return lang.Alt{
lang.EnUS: "loop over a set of arguments and run a command",
}
}
func (Command) Man() lang.Alt {
return lang.Alt{
lang.EnUS: `
DESCRIPTION
Iterate over a series of words for a set of commands.`,
}
}
func (c Command) Block(g *goes.Goes, ls shellutils.List) (*shellutils.List, func(stdin io.Reader, stdout io.Writer, stderr io.Writer) error, error) {
var doList []func(stdin io.Reader, stdout io.Writer, stderr io.Writer) error
cl := ls.Cmds[0]
// for <var> in <commands>
if len(cl.Cmds) > 1 {
cl.Cmds = cl.Cmds[1:]
ls.Cmds[0] = cl
} else {
return nil, nil, errors.New("Unexpected `newline'")
}
varName := cl.Cmds[0].String()
if varName == "" {
return nil, nil, errors.New("Malformed loop variable")
}
cl.Cmds = cl.Cmds[1:]
if len(cl.Cmds) > 1 {
ls.Cmds[0] = cl
} else {
ls.Cmds = ls.Cmds[1:]
}
foundIn := false
foundDo := false
var wordList []shellutils.Word
for {
if len(cl.Cmds) == 0 {
for len(ls.Cmds) == 0 {
newls, err := shellutils.Parse("for>", g.Catline)
if err != nil {
return nil, nil, err
}
ls = *newls
}
cl = ls.Cmds[0]
}
name := cl.Cmds[0].String()
if !foundIn {
if name != "in" {
return nil, nil, fmt.Errorf("Expected `in' found `%s'",
name)
}
foundIn = true
cl.Cmds = cl.Cmds[1:]
if len(cl.Cmds) > 0 {
ls.Cmds[0] = cl
} else {
ls.Cmds = ls.Cmds[1:]
}
continue
}
if wordList == nil {
wordList = cl.Cmds
cl.Cmds = nil
ls.Cmds = ls.Cmds[1:]
continue
}
if !foundDo {
if name != "do" {
return nil, nil, fmt.Errorf("Looking for `do' got `%s'",
name)
}
foundDo = true
if len(cl.Cmds) > 1 {
cl.Cmds = cl.Cmds[1:]
ls.Cmds[0] = cl
} else {
ls.Cmds = ls.Cmds[1:]
}
continue
}
if name == "done" {
if len(cl.Cmds) > 1 {
return nil, nil, errors.New("unexpected text after `done'")
}
break
}
nextls, _, runfun, err := g.ProcessList(ls)
if err != nil {
return nil, nil, err
}
doList = append(doList, runfun)
cl.Cmds = nil
ls = *nextls
if len(ls.Cmds) != 0 {
cl = ls.Cmds[0]
}
}
blockfun, err := makeBlockFunc(g, varName, wordList, doList)
return &ls, blockfun, err
}
func runList(pipeline []func(stdin io.Reader, stdout io.Writer, stderr io.Writer) error, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
for _, runent := range pipeline {
err := runent(stdin, stdout, stderr)
if err != nil {
return err
}
}
return nil
}
func makeBlockFunc(g *goes.Goes, varName string,
wordList []shellutils.Word,
doList []func(stdin io.Reader, stdout io.Writer, stderr io.Writer) error) (func(stdin io.Reader, stdout io.Writer, stderr io.Writer) error, error) {
if g.EnvMap == nil {
g.EnvMap = make(map[string]string)
}
runfun := func(stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
for _, word := range wordList {
for _, str := range word.Expand() {
g.EnvMap[varName] = str
err := runList(doList, stdin, stdout, stderr)
if err != nil {
fmt.Fprintln(stderr, err)
}
if g.Status != nil {
if g.Status.Error() == "signal: interrupt" {
return g.Status
}
}
}
}
return nil
}
return runfun, nil
}
func (Command) Main(args ...string) error {
return errors.New("internal error")
}