-
Notifications
You must be signed in to change notification settings - Fork 6
/
line.go
280 lines (247 loc) · 6.2 KB
/
line.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
package console
import (
"bytes"
"errors"
"strings"
"unicode/utf8"
"github.com/kballard/go-shellquote"
"mvdan.cc/sh/v3/syntax"
)
var (
splitChars = " \n\t"
singleChar = '\''
doubleChar = '"'
escapeChar = '\\'
doubleEscapeChars = "$`\"\n\\"
)
var (
errUnterminatedSingleQuote = errors.New("unterminated single-quoted string")
errUnterminatedDoubleQuote = errors.New("unterminated double-quoted string")
errUnterminatedEscape = errors.New("unterminated backslash-escape")
)
// parse is in charge of removing all comments from the input line
// before execution, and if successfully parsed, split into words.
func (c *Console) parse(line string) (args []string, err error) {
lineReader := strings.NewReader(line)
parser := syntax.NewParser(syntax.KeepComments(false))
// Parse the shell string a syntax, removing all comments.
stmts, err := parser.Parse(lineReader, "")
if err != nil {
return nil, err
}
var parsedLine bytes.Buffer
err = syntax.NewPrinter().Print(&parsedLine, stmts)
if err != nil {
return nil, err
}
// Split the line into shell words.
return shellquote.Split(parsedLine.String())
}
// acceptMultiline determines if the line just accepted is complete (in which case
// we should execute it), or incomplete (in which case we must read in multiline).
func (c *Console) acceptMultiline(line []rune) (accept bool) {
// Errors are either: unterminated quotes, or unterminated escapes.
_, _, err := split(string(line), false)
if err == nil {
return true
}
// Currently, unterminated quotes are obvious to treat: keep reading.
switch err {
case errUnterminatedDoubleQuote, errUnterminatedSingleQuote:
return false
case errUnterminatedEscape:
if len(line) > 0 && line[len(line)-1] == '\\' {
return false
}
return true
}
return true
}
// split has been copied from go-shellquote and slightly modified so as to also
// return the remainder when the parsing failed because of an unterminated quote.
func split(input string, hl bool) (words []string, remainder string, err error) {
var buf bytes.Buffer
words = make([]string, 0)
for len(input) > 0 {
// skip any splitChars at the start
c, l := utf8.DecodeRuneInString(input)
if strings.ContainsRune(splitChars, c) {
// Keep these characters in the result when higlighting the line.
if hl {
if len(words) == 0 {
words = append(words, string(c))
} else {
words[len(words)-1] += string(c)
}
}
input = input[l:]
continue
} else if c == escapeChar {
// Look ahead for escaped newline so we can skip over it
next := input[l:]
if len(next) == 0 {
if hl {
remainder = string(escapeChar)
}
err = errUnterminatedEscape
return
}
c2, l2 := utf8.DecodeRuneInString(next)
if c2 == '\n' {
if hl {
if len(words) == 0 {
words = append(words, string(c)+string(c2))
} else {
words[len(words)-1] += string(c) + string(c2)
}
}
input = next[l2:]
continue
}
}
var word string
word, input, err = splitWord(input, &buf, hl)
if err != nil {
remainder = input
return
}
words = append(words, word)
}
return
}
// splitWord has been modified to return the remainder of the input (the part that has not been
// added to the buffer) even when an error is returned.
func splitWord(input string, buf *bytes.Buffer, hl bool) (word string, remainder string, err error) {
buf.Reset()
raw:
{
cur := input
for len(cur) > 0 {
c, l := utf8.DecodeRuneInString(cur)
cur = cur[l:]
if c == singleChar {
buf.WriteString(input[0 : len(input)-len(cur)-l])
input = cur
goto single
} else if c == doubleChar {
buf.WriteString(input[0 : len(input)-len(cur)-l])
input = cur
goto double
} else if c == escapeChar {
buf.WriteString(input[0 : len(input)-len(cur)-l])
if hl {
buf.WriteRune(c)
}
input = cur
goto escape
} else if strings.ContainsRune(splitChars, c) {
buf.WriteString(input[0 : len(input)-len(cur)-l])
if hl {
buf.WriteRune(c)
}
return buf.String(), cur, nil
}
}
if len(input) > 0 {
buf.WriteString(input)
input = ""
}
goto done
}
escape:
{
if len(input) == 0 {
if hl {
input = buf.String() + input
}
return "", input, errUnterminatedEscape
}
c, l := utf8.DecodeRuneInString(input)
if c == '\n' {
// a backslash-escaped newline is elided from the output entirely
} else {
buf.WriteString(input[:l])
}
input = input[l:]
}
goto raw
single:
{
i := strings.IndexRune(input, singleChar)
if i == -1 {
if hl {
input = buf.String() + seqFgYellow + string(singleChar) + input
}
return "", input, errUnterminatedSingleQuote
}
// Catch up opening quote
if hl {
buf.WriteString(seqFgYellow)
buf.WriteRune(singleChar)
}
buf.WriteString(input[0:i])
input = input[i+1:]
if hl {
buf.WriteRune(singleChar)
buf.WriteString(seqFgReset)
}
goto raw
}
double:
{
cur := input
for len(cur) > 0 {
c, l := utf8.DecodeRuneInString(cur)
cur = cur[l:]
if c == doubleChar {
// Catch up opening quote
if hl {
buf.WriteString(seqFgYellow)
buf.WriteRune(c)
}
buf.WriteString(input[0 : len(input)-len(cur)-l])
if hl {
buf.WriteRune(c)
buf.WriteString(seqFgReset)
}
input = cur
goto raw
} else if c == escapeChar && !hl {
// bash only supports certain escapes in double-quoted strings
c2, l2 := utf8.DecodeRuneInString(cur)
cur = cur[l2:]
if strings.ContainsRune(doubleEscapeChars, c2) {
buf.WriteString(input[0 : len(input)-len(cur)-l-l2])
if c2 == '\n' {
// newline is special, skip the backslash entirely
} else {
buf.WriteRune(c2)
}
input = cur
}
}
}
if hl {
input = buf.String() + seqFgYellow + string(doubleChar) + input
}
return "", input, errUnterminatedDoubleQuote
}
done:
return buf.String(), input, nil
}
func trimSpacesMatch(remain []string) (trimmed []string) {
for _, word := range remain {
trimmed = append(trimmed, strings.TrimSpace(word))
}
return
}
func (c *Console) lineEmpty(line string) bool {
empty := true
for _, r := range line {
if !strings.ContainsRune(string(c.EmptyChars), r) {
empty = false
break
}
}
return empty
}