-
Notifications
You must be signed in to change notification settings - Fork 3
/
parse.go
169 lines (144 loc) · 2.83 KB
/
parse.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
package shellcmd
import (
"fmt"
"strings"
)
type (
cmdParser struct {
args []string
current strings.Builder
quote quoteType
started bool
escaped bool
}
quoteType int
)
const (
notQuoted quoteType = iota // intentionally 0 to be default value
singleQuoted
doubleQuoted
)
func (p *cmdParser) parse(cmd string) ([]string, error) {
// Clear out in case there was a previous run
p.args = nil
p.current = strings.Builder{}
p.quote = notQuoted
p.started = false
p.escaped = false
for _, r := range cmd {
switch r {
case '"':
p.handleDoubleQuote(r)
case '\'':
p.handleSingleQuote(r)
case ' ', '\t', '\n', '\r':
p.handleWhitespace(r)
case '\\':
p.handleBackslash(r)
default:
p.handleNormal(r)
}
}
// If we're still in a quote, then we have an unterminated quote
if p.quote != notQuoted {
return nil, fmt.Errorf("shellcmd: unterminated quote in command: %s", cmd)
}
// If there is still an escape, we need to write it (should this be an error?)
if p.escaped {
p.current.WriteRune('\\')
p.started = true
}
// If we still have a started literal or there are no args, we need to
// fill in the last arg
if p.started || len(p.args) == 0 {
p.args = append(p.args, p.current.String())
}
return p.args, nil
}
func (p *cmdParser) handleSingleQuote(r rune) {
switch p.quote {
case notQuoted:
if p.escaped {
p.current.WriteRune(r)
} else {
p.quote = singleQuoted
}
case singleQuoted:
if p.escaped {
p.current.WriteRune(r)
} else {
p.quote = notQuoted
}
case doubleQuoted:
if p.escaped {
p.current.WriteRune('\\')
}
p.current.WriteRune(r)
}
p.started = true
p.escaped = false
}
func (p *cmdParser) handleDoubleQuote(r rune) {
switch p.quote {
case notQuoted:
if p.escaped {
p.current.WriteRune(r)
} else {
p.quote = doubleQuoted
}
case singleQuoted:
if p.escaped {
p.current.WriteRune('\\')
}
p.current.WriteRune(r)
case doubleQuoted:
if p.escaped {
p.current.WriteRune(r)
} else {
p.quote = notQuoted
}
}
p.started = true
p.escaped = false
}
func (p *cmdParser) handleWhitespace(r rune) {
switch p.quote {
case notQuoted:
if p.escaped {
p.current.WriteRune(r)
p.started = true
break
}
// End the segment
if p.started {
p.args = append(p.args, p.current.String())
p.current = strings.Builder{}
p.quote = notQuoted
p.started = false
}
case singleQuoted, doubleQuoted:
if p.escaped {
p.current.WriteRune('\\')
}
p.current.WriteRune(r)
p.started = true
}
p.escaped = false
}
func (p *cmdParser) handleBackslash(r rune) {
if p.escaped {
p.current.WriteRune(r)
p.escaped = false
} else {
p.escaped = true
}
p.started = true
}
func (p *cmdParser) handleNormal(r rune) {
if p.escaped {
p.current.WriteRune('\\')
}
p.current.WriteRune(r)
p.escaped = false
p.started = true
}