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-08-30 05:09:52 +00:00
|
|
|
|
// WordOffset is the offset from the position cursor to print on the error.
|
|
|
|
|
const WordOffset = 7
|
|
|
|
|
|
|
|
|
|
var escaper = strings.NewReplacer(
|
|
|
|
|
"`", "\\`",
|
|
|
|
|
"@", "\\@",
|
|
|
|
|
"\\", "\\\\",
|
|
|
|
|
)
|
|
|
|
|
|
2020-01-26 07:17:18 +00:00
|
|
|
|
type ErrParse struct {
|
2020-08-11 20:44:10 +00:00
|
|
|
|
Position int
|
2020-08-30 05:09:52 +00:00
|
|
|
|
Words string // joined
|
2020-01-26 06:15:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-11 20:44:10 +00:00
|
|
|
|
func (e ErrParse) Error() string {
|
2020-08-30 05:09:52 +00:00
|
|
|
|
// Magic number 5.
|
|
|
|
|
var a = max(0, e.Position-WordOffset)
|
|
|
|
|
var b = min(len(e.Words), e.Position+WordOffset)
|
|
|
|
|
var word = e.Words[a:b]
|
|
|
|
|
var uidx = e.Position - a
|
|
|
|
|
|
|
|
|
|
errstr := strings.Builder{}
|
|
|
|
|
errstr.WriteString("Unexpected quote or escape")
|
|
|
|
|
|
|
|
|
|
// Do a bound check.
|
|
|
|
|
if uidx+1 > len(word) {
|
|
|
|
|
// Invalid.
|
|
|
|
|
errstr.WriteString(".")
|
|
|
|
|
return errstr.String()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write the pre-underline part.
|
|
|
|
|
fmt.Fprintf(
|
|
|
|
|
&errstr, ": %s__%s__",
|
|
|
|
|
escaper.Replace(word[:uidx]),
|
|
|
|
|
escaper.Replace(string(word[uidx:])),
|
2020-08-11 20:44:10 +00:00
|
|
|
|
)
|
2020-08-30 05:09:52 +00:00
|
|
|
|
|
|
|
|
|
return errstr.String()
|
2020-08-11 20:44:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
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 {
|
2020-08-11 20:44:10 +00:00
|
|
|
|
return args, &ErrParse{
|
2020-08-30 05:09:52 +00:00
|
|
|
|
Position: cursor + buf.Len(),
|
|
|
|
|
Words: strings.Join(args, " "),
|
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
|
|
|
|
}
|