-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.go
More file actions
213 lines (188 loc) · 4.42 KB
/
split.go
File metadata and controls
213 lines (188 loc) · 4.42 KB
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
package shellshape
import "sort"
type segment struct {
text string
sep string
}
var splitOps = []string{"&&", "||", "|&", "|", ";", "\r\n", "\n"}
// Shell keywords that open compound commands.
var compoundOpeners = []string{"for", "while", "until", "if", "case"}
// Shell keywords that close compound commands. Each closer is paired
// with one or more openers — we only track aggregate depth here.
var compoundClosers = []string{"done", "fi", "esac"}
// splitTopLevel splits a command string on shell operators at the top level only
// (outside quotes, backticks, subshells, and compound commands).
// Returns segments with text and separator.
// Newline operators are canonicalized to ";" in the sep field.
func splitTopLevel(command string, operators []string) []segment {
// Sort operators longest-first
sorted := make([]string, len(operators))
copy(sorted, operators)
sort.Slice(sorted, func(i, j int) bool {
return len(sorted[i]) > len(sorted[j])
})
var segments []segment
var cur []byte
inSingle := false
inDouble := false
inBacktick := false
subshellDepth := 0
compoundDepth := 0
i := 0
for i < len(command) {
ch := command[i]
if inSingle {
cur = append(cur, ch)
if ch == '\'' {
inSingle = false
}
i++
continue
}
if inBacktick {
cur = append(cur, ch)
if ch == '`' {
inBacktick = false
}
i++
continue
}
if inDouble {
cur = append(cur, ch)
if ch == '"' {
inDouble = false
}
i++
continue
}
if ch == '\'' {
inSingle = true
cur = append(cur, ch)
i++
continue
}
if ch == '"' {
inDouble = true
cur = append(cur, ch)
i++
continue
}
if ch == '`' {
inBacktick = true
cur = append(cur, ch)
i++
continue
}
// Outside quotes, a backslash escapes the next character so it is
// never interpreted as an operator (e.g. `find ... -exec cmd {} \;`
// where `\;` is the argument terminator for -exec, not a real ;).
if ch == '\\' && i+1 < len(command) {
cur = append(cur, ch, command[i+1])
i += 2
continue
}
// Track $( for subshell
if ch == '$' && i+1 < len(command) && command[i+1] == '(' {
subshellDepth++
cur = append(cur, ch, command[i+1])
i += 2
continue
}
if ch == '(' && subshellDepth > 0 {
subshellDepth++
cur = append(cur, ch)
i++
continue
}
if ch == ')' && subshellDepth > 0 {
subshellDepth--
cur = append(cur, ch)
i++
continue
}
if subshellDepth > 0 {
cur = append(cur, ch)
i++
continue
}
// Check for compound keywords at word boundaries.
if compoundDepth > 0 {
for _, kw := range compoundClosers {
if matchKeywordAt(command, i, kw) {
compoundDepth--
break
}
}
// Also check for nested openers inside compound.
for _, kw := range compoundOpeners {
if matchKeywordAt(command, i, kw) {
compoundDepth++
break
}
}
} else {
for _, kw := range compoundOpeners {
if matchKeywordAt(command, i, kw) {
compoundDepth++
break
}
}
}
// Inside a compound command: don't split on operators.
if compoundDepth > 0 {
cur = append(cur, ch)
i++
continue
}
// Try to match an operator
matched := false
for _, op := range sorted {
if i+len(op) <= len(command) && command[i:i+len(op)] == op {
sep := op
if sep == "\n" || sep == "\r\n" {
sep = ";"
}
segments = append(segments, segment{text: string(cur), sep: sep})
cur = cur[:0]
i += len(op)
matched = true
break
}
}
if matched {
continue
}
cur = append(cur, ch)
i++
}
// Final segment
segments = append(segments, segment{text: string(cur), sep: ""})
return segments
}
// matchKeywordAt checks whether the shell keyword kw starts at position pos
// in s, with word boundaries on both sides (not inside a longer word).
func matchKeywordAt(s string, pos int, kw string) bool {
end := pos + len(kw)
if end > len(s) {
return false
}
if s[pos:end] != kw {
return false
}
// Word boundary before: start of string, or preceded by whitespace/semicolon.
if pos > 0 {
prev := s[pos-1]
if prev != ' ' && prev != '\t' && prev != ';' && prev != '\n' && prev != '\r' {
return false
}
}
// Word boundary after: end of string, or followed by whitespace/semicolon/operator char.
if end < len(s) {
next := s[end]
if next != ' ' && next != '\t' && next != ';' && next != '\n' && next != '\r' &&
next != '&' && next != '|' && next != '(' {
return false
}
}
return true
}