2020-01-26 06:15:39 +00:00
|
|
|
|
package shellwords
|
|
|
|
|
|
|
|
|
|
import (
|
2020-08-11 20:44:10 +00:00
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
2020-01-26 06:15:39 +00:00
|
|
|
|
)
|
|
|
|
|
|
2020-01-26 07:17:18 +00:00
|
|
|
|
type ErrParse struct {
|
2020-08-11 20:44:10 +00:00
|
|
|
|
Position int
|
|
|
|
|
ErrorStart,
|
|
|
|
|
ErrorPart,
|
|
|
|
|
ErrorEnd string
|
2020-01-26 06:15:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-11 20:44:10 +00:00
|
|
|
|
func (e ErrParse) Error() string {
|
|
|
|
|
return fmt.Sprintf(
|
|
|
|
|
"Unexpected quote or escape: %s__%s__%s",
|
|
|
|
|
e.ErrorStart, e.ErrorPart, e.ErrorEnd,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse parses the given text to a slice of words.
|
2020-01-26 07:17:18 +00:00
|
|
|
|
func Parse(line string) ([]string, error) {
|
2020-07-29 23:29:01 +00:00
|
|
|
|
var args []string
|
2020-01-26 07:17:18 +00:00
|
|
|
|
var escaped, doubleQuoted, singleQuoted bool
|
2020-08-11 20:44:10 +00:00
|
|
|
|
|
|
|
|
|
var buf strings.Builder
|
|
|
|
|
buf.Grow(len(line))
|
2020-01-26 06:15:39 +00:00
|
|
|
|
|
|
|
|
|
got := false
|
2020-01-26 07:17:18 +00:00
|
|
|
|
cursor := 0
|
2020-01-26 06:15:39 +00:00
|
|
|
|
|
2020-01-26 07:17:18 +00:00
|
|
|
|
runes := []rune(line)
|
|
|
|
|
|
|
|
|
|
for _, r := range runes {
|
2020-01-26 06:15:39 +00:00
|
|
|
|
if escaped {
|
2020-08-11 20:44:10 +00:00
|
|
|
|
buf.WriteRune(r)
|
2020-01-26 06:15:39 +00:00
|
|
|
|
escaped = false
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if r == '\\' {
|
|
|
|
|
if singleQuoted {
|
2020-08-11 20:44:10 +00:00
|
|
|
|
buf.WriteRune(r)
|
2020-01-26 06:15:39 +00:00
|
|
|
|
} else {
|
|
|
|
|
escaped = true
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if isSpace(r) {
|
|
|
|
|
switch {
|
2020-01-26 07:17:18 +00:00
|
|
|
|
case singleQuoted, doubleQuoted:
|
2020-08-11 20:44:10 +00:00
|
|
|
|
buf.WriteRune(r)
|
2020-01-26 06:15:39 +00:00
|
|
|
|
case got:
|
2020-08-11 20:44:10 +00:00
|
|
|
|
cursor += buf.Len()
|
|
|
|
|
args = append(args, buf.String())
|
|
|
|
|
buf.Reset()
|
2020-01-26 06:15:39 +00:00
|
|
|
|
got = false
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch r {
|
|
|
|
|
case '"':
|
|
|
|
|
if !singleQuoted {
|
|
|
|
|
if doubleQuoted {
|
|
|
|
|
got = true
|
|
|
|
|
}
|
|
|
|
|
doubleQuoted = !doubleQuoted
|
|
|
|
|
continue
|
|
|
|
|
}
|
2020-08-11 20:44:10 +00:00
|
|
|
|
case '\'', '`':
|
2020-01-26 06:15:39 +00:00
|
|
|
|
if !doubleQuoted {
|
|
|
|
|
if singleQuoted {
|
|
|
|
|
got = true
|
|
|
|
|
}
|
2020-08-11 20:44:10 +00:00
|
|
|
|
|
|
|
|
|
// // If this is a backtick, then write it.
|
|
|
|
|
// if r == '`' {
|
|
|
|
|
// buf.WriteByte('`')
|
|
|
|
|
// }
|
|
|
|
|
|
2020-01-26 06:15:39 +00:00
|
|
|
|
singleQuoted = !singleQuoted
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
got = true
|
2020-08-11 20:44:10 +00:00
|
|
|
|
buf.WriteRune(r)
|
2020-01-26 06:15:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if got {
|
2020-08-11 20:44:10 +00:00
|
|
|
|
args = append(args, buf.String())
|
2020-01-26 06:15:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-26 07:17:18 +00:00
|
|
|
|
if escaped || singleQuoted || doubleQuoted {
|
|
|
|
|
// the number of characters to highlight
|
|
|
|
|
var (
|
|
|
|
|
pos = cursor + 5
|
|
|
|
|
start = string(runes[max(cursor-100, 0) : pos-1])
|
|
|
|
|
end = string(runes[pos+1 : min(cursor+100, len(runes))])
|
2020-08-11 20:44:10 +00:00
|
|
|
|
part = string(runes[max(pos-1, 0):min(len(runes), pos+2)])
|
2020-01-26 07:17:18 +00:00
|
|
|
|
)
|
|
|
|
|
|
2020-08-11 20:44:10 +00:00
|
|
|
|
return args, &ErrParse{
|
|
|
|
|
Position: cursor,
|
|
|
|
|
ErrorStart: start,
|
|
|
|
|
ErrorPart: part,
|
|
|
|
|
ErrorEnd: end,
|
2020-01-26 07:17:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-26 06:15:39 +00:00
|
|
|
|
|
|
|
|
|
return args, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-26 07:17:18 +00:00
|
|
|
|
func isSpace(r rune) bool {
|
|
|
|
|
switch r {
|
2020-08-11 20:44:10 +00:00
|
|
|
|
case ' ', '\t', '\r', '\n', ' ':
|
2020-01-26 07:17:18 +00:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func min(i, j int) int {
|
|
|
|
|
if i < j {
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
return j
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func max(i, j int) int {
|
|
|
|
|
if i < j {
|
|
|
|
|
return j
|
|
|
|
|
}
|
|
|
|
|
return i
|
2020-01-26 06:15:39 +00:00
|
|
|
|
}
|