-
Notifications
You must be signed in to change notification settings - Fork 68
/
shell_parser.go
456 lines (410 loc) · 10.6 KB
/
shell_parser.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package imagebuilder
// This will take a single word and an array of env variables and
// process all quotes (" and ') as well as $xxx and ${xxx} env variable
// tokens. Tries to mimic bash shell process.
// It doesn't support all flavors of ${xx:...} formats but new ones can
// be added by adding code to the "special ${} format processing" section
import (
"errors"
"fmt"
"path"
"strings"
"text/scanner"
"unicode"
)
type shellWord struct {
word string
scanner scanner.Scanner
envs []string
pos int
}
// ProcessWord will use the 'env' list of environment variables,
// and replace any env var references in 'word'.
func ProcessWord(word string, env []string) (string, error) {
sw := &shellWord{
word: word,
envs: env,
pos: 0,
}
sw.scanner.Init(strings.NewReader(word))
word, _, err := sw.process()
return word, err
}
// ProcessWords will use the 'env' list of environment variables,
// and replace any env var references in 'word' then it will also
// return a slice of strings which represents the 'word'
// split up based on spaces - taking into account quotes. Note that
// this splitting is done **after** the env var substitutions are done.
// Note, each one is trimmed to remove leading and trailing spaces (unless
// they are quoted", but ProcessWord retains spaces between words.
func ProcessWords(word string, env []string) ([]string, error) {
sw := &shellWord{
word: word,
envs: env,
pos: 0,
}
sw.scanner.Init(strings.NewReader(word))
_, words, err := sw.process()
return words, err
}
func (sw *shellWord) process() (string, []string, error) {
return sw.processStopOn(scanner.EOF)
}
type wordsStruct struct {
word string
words []string
inWord bool
}
func (w *wordsStruct) addChar(ch rune) {
if unicode.IsSpace(ch) && w.inWord {
if len(w.word) != 0 {
w.words = append(w.words, w.word)
w.word = ""
w.inWord = false
}
} else if !unicode.IsSpace(ch) {
w.addRawChar(ch)
}
}
func (w *wordsStruct) addRawChar(ch rune) {
w.word += string(ch)
w.inWord = true
}
func (w *wordsStruct) addString(str string) {
var scan scanner.Scanner
scan.Init(strings.NewReader(str))
for scan.Peek() != scanner.EOF {
w.addChar(scan.Next())
}
}
func (w *wordsStruct) addRawString(str string) {
w.word += str
w.inWord = true
}
func (w *wordsStruct) getWords() []string {
if len(w.word) > 0 {
w.words = append(w.words, w.word)
// Just in case we're called again by mistake
w.word = ""
w.inWord = false
}
return w.words
}
func (sw *shellWord) processStopOn(stopChar rune) (string, []string, error) {
_, result, words, err := sw.processStopOnAny([]rune{stopChar})
return result, words, err
}
// Process the word, starting at 'pos', and stop when we get to the
// end of the word or the 'stopChar' character
func (sw *shellWord) processStopOnAny(stopChars []rune) (rune, string, []string, error) {
var result string
var words wordsStruct
var charFuncMapping = map[rune]func() (string, error){
'\'': sw.processSingleQuote,
'"': sw.processDoubleQuote,
'$': sw.processDollar,
}
sliceContains := func(slice []rune, value rune) bool {
for _, r := range slice {
if r == value {
return true
}
}
return false
}
for sw.scanner.Peek() != scanner.EOF {
ch := sw.scanner.Peek()
if sliceContains(stopChars, ch) {
sw.scanner.Next() // skip over ch
return ch, result, words.getWords(), nil
}
if fn, ok := charFuncMapping[ch]; ok {
// Call special processing func for certain chars
tmp, err := fn()
if err != nil {
return ch, "", []string{}, err
}
result += tmp
if ch == rune('$') {
words.addString(tmp)
} else {
words.addRawString(tmp)
}
} else {
// Not special, just add it to the result
ch = sw.scanner.Next()
if ch == '\\' {
// '\' escapes, except end of line
ch = sw.scanner.Next()
if ch == scanner.EOF {
break
}
words.addRawChar(ch)
} else {
words.addChar(ch)
}
result += string(ch)
}
}
if !sliceContains(stopChars, scanner.EOF) {
return scanner.EOF, "", []string{}, fmt.Errorf("unexpected end of statement while looking for matching %s", string(stopChars))
}
return scanner.EOF, result, words.getWords(), nil
}
func (sw *shellWord) processSingleQuote() (string, error) {
// All chars between single quotes are taken as-is
// Note, you can't escape '
var result string
sw.scanner.Next()
for {
ch := sw.scanner.Next()
if ch == '\'' {
break
}
if ch == scanner.EOF {
return "", errors.New("unexpected end of statement while looking for matching single-quote")
}
result += string(ch)
}
return result, nil
}
func (sw *shellWord) processDoubleQuote() (string, error) {
// All chars up to the next " are taken as-is, even ', except any $ chars
// But you can escape " with a \
var result string
sw.scanner.Next()
for {
ch := sw.scanner.Peek()
if ch == '"' {
sw.scanner.Next()
break
}
if ch == scanner.EOF {
return "", errors.New("unexpected end of statement while looking for matching double-quote")
}
if ch == '$' {
tmp, err := sw.processDollar()
if err != nil {
return "", err
}
result += tmp
} else {
ch = sw.scanner.Next()
if ch == '\\' {
chNext := sw.scanner.Peek()
if chNext == scanner.EOF {
// Ignore \ at end of word
continue
}
if chNext == '"' || chNext == '$' || chNext == '\\' {
// \" and \$ and \\ can be escaped, all other \'s are left as-is
ch = sw.scanner.Next()
}
}
result += string(ch)
}
}
return result, nil
}
func (sw *shellWord) processDollar() (string, error) {
sw.scanner.Next()
ch := sw.scanner.Peek()
if ch == '{' {
sw.scanner.Next()
name := sw.processName()
ch = sw.scanner.Peek()
if ch == '}' {
// Normal ${xx} case
sw.scanner.Next()
return sw.getEnv(name), nil
}
if ch == ':' {
// Special ${xx:...} format processing
// Yes it allows for recursive $'s in the ... spot
sw.scanner.Next() // skip over :
modifier := sw.scanner.Next()
word, _, err := sw.processStopOn('}')
if err != nil {
return "", err
}
// Grab the current value of the variable in question so we
// can use it to determine what to do based on the modifier
newValue := sw.getEnv(name)
switch modifier {
case '+':
if newValue != "" {
newValue = word
}
return newValue, nil
case '-':
if newValue == "" {
newValue = word
}
return newValue, nil
case '?':
if newValue == "" {
newValue = word
}
if newValue == "" {
return "", fmt.Errorf("Failed to process `%s`: %s is not allowed to be unset", sw.word, name)
}
return newValue, nil
default:
return "", fmt.Errorf("Unsupported modifier (%c) in substitution: %s", modifier, sw.word)
}
}
if ch == '#' || ch == '%' { // strip a prefix or suffix
sw.scanner.Next() // skip over # or %
greedy := false
if sw.scanner.Peek() == ch {
sw.scanner.Next() // skip over second # or %
greedy = true
}
word, _, err := sw.processStopOn('}')
if err != nil {
return "", err
}
value := sw.getEnv(name)
switch ch {
case '#': // strip a prefix
if word == "" {
return "", fmt.Errorf("%s#: no prefix to remove", name)
}
if greedy {
for i := len(value) - 1; i >= 0; i-- {
if matches, err := path.Match(word, value[:i]); err == nil && matches {
return value[i:], nil
}
}
} else {
for i := 0; i < len(value)-1; i++ {
if matches, err := path.Match(word, value[:i]); err == nil && matches {
return value[i:], nil
}
}
}
return value, nil
case '%': // strip a suffix
if word == "" {
return "", fmt.Errorf("%s%%: no suffix to remove", name)
}
if greedy {
for i := 0; i < len(value)-1; i++ {
if matches, err := path.Match(word, value[i:]); err == nil && matches {
return value[:i], nil
}
}
} else {
for i := len(value) - 1; i >= 0; i-- {
if matches, err := path.Match(word, value[i:]); err == nil && matches {
return value[:i], nil
}
}
}
return value, nil
}
}
if ch == '/' { // perform substitution
sw.scanner.Next() // skip over /
all, begin, end := false, false, false
switch sw.scanner.Peek() {
case ch:
sw.scanner.Next() // skip over second /
all = true // replace all instances
case '#':
sw.scanner.Next() // skip over #
begin = true // replace only an prefix instance
case '%':
sw.scanner.Next() // skip over %
end = true // replace only a fuffix instance
}
// the '/', and the replacement pattern that follows
// it, can be omitted if the replacement pattern is "",
// so the pattern-to-replace can end at either a '/' or
// a '}'
ch, pattern, _, err := sw.processStopOnAny([]rune{'/', '}'})
if err != nil {
return "", err
}
if pattern == "" { // pattern to replace needs to not be empty
return "", fmt.Errorf("%s/: no pattern to replace", name)
}
var replacement string
if ch == '/' { // patter to replace it with was specified
replacement, _, err = sw.processStopOn('}')
if err != nil {
return "", err
}
}
value := sw.getEnv(name)
i := 0
for {
if i >= len(value) {
break
}
for j := len(value); j > i; j-- {
if begin && i != 0 {
continue
}
if end && j != len(value) {
continue
}
matches, err := path.Match(pattern, value[i:j])
if err == nil && matches {
value = value[:i] + replacement + value[j:]
if !all {
return value, nil
}
i += (len(replacement) - 1)
break
}
}
i++
}
return value, nil
}
return "", fmt.Errorf("Missing ':' or '#' or '%%' or '/' in substitution: %s", sw.word)
}
// $xxx case
name := sw.processName()
if name == "" {
return "$", nil
}
return sw.getEnv(name), nil
}
func (sw *shellWord) processName() string {
// Read in a name (alphanumeric or _)
// If it starts with a numeric then just return $#
var name string
for sw.scanner.Peek() != scanner.EOF {
ch := sw.scanner.Peek()
if len(name) == 0 && unicode.IsDigit(ch) {
ch = sw.scanner.Next()
return string(ch)
}
if !unicode.IsLetter(ch) && !unicode.IsDigit(ch) && ch != '_' {
break
}
ch = sw.scanner.Next()
name += string(ch)
}
return name
}
func (sw *shellWord) getEnv(name string) string {
for _, env := range sw.envs {
i := strings.Index(env, "=")
if i < 0 {
if name == env {
// Should probably never get here, but just in case treat
// it like "var" and "var=" are the same
return ""
}
continue
}
if name != env[:i] {
continue
}
return env[i+1:]
}
return ""
}