/
command.go
135 lines (122 loc) · 2.55 KB
/
command.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
package main
import (
"bufio"
"fmt"
"io"
"os"
)
func ScanArgs(data []byte, atEOF bool) (advance int, token []byte, err error) {
isSpace := func(c byte) bool { return c == ' ' || c == '\t' || c == '\r' || c == '\n' }
skip := 0
for ; skip < len(data) && isSpace(data[skip]); skip++ {
}
data = data[skip:]
if len(data) == 0 {
advance = skip
return
}
var new_token []byte
in_quote := false
in_dquote := false
in_escape := false
oct := 0
in_oct := 0
for here, c := range data {
if in_escape {
switch c {
case 'b':
new_token = append(new_token, '\b')
case 't':
new_token = append(new_token, '\t')
case 'n':
new_token = append(new_token, '\n')
case 'r':
new_token = append(new_token, '\r')
case '"':
new_token = append(new_token, '"')
case '\'':
new_token = append(new_token, '\'')
case ' ':
new_token = append(new_token, ' ')
case '\\':
new_token = append(new_token, '\\')
case 'e':
new_token = append(new_token, '\033')
case '0', '1', '2', '3', '4', '5', '6', '7':
oct = (oct * 8) + int(c-'0')
in_oct++
if in_oct == 3 {
new_token = append(new_token, byte(oct))
oct = 0
in_oct = 0
}
default:
if in_oct > 0 {
new_token = append(new_token, byte(oct))
oct = 0
in_oct = 0
}
new_token = append(new_token, c)
}
in_escape = (in_oct != 0)
} else {
switch c {
case '"':
if !in_quote {
in_dquote = !in_dquote
} else {
new_token = append(new_token, c)
}
case '\'':
if !in_dquote {
in_quote = !in_quote
} else {
new_token = append(new_token, c)
}
case '\\':
in_escape = true
case ' ', '\t', '\r', '\n':
if !in_dquote && !in_quote {
advance = skip + here
token = new_token
return
} else {
new_token = append(new_token, c)
}
default:
new_token = append(new_token, c)
}
}
}
if in_escape && in_oct > 0 {
new_token = append(new_token, byte(oct))
in_escape = false
}
if !in_dquote && !in_escape && !in_quote {
advance = skip + len(data)
if len(new_token) > 0 {
token = new_token
}
return
}
if atEOF {
err = fmt.Errorf("incomplete line: '%v' => '%v'", string(data), string(new_token))
}
return
}
func CommandExpand(reader io.Reader) (out []string, err error) {
var lines []string
scanner := bufio.NewScanner(reader)
scanner.Split(ScanArgs)
for scanner.Scan() {
text := scanner.Text()
expanded := os.ExpandEnv(text)
lines = append(lines, expanded)
}
err = scanner.Err()
if err != nil {
return
}
out = lines
return
}