1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-01 04:24:19 +00:00
arikawa/bot/extras/shellwords/shellwords.go

124 lines
1.8 KiB
Go
Raw Normal View History

package shellwords
import (
"errors"
)
2020-01-26 07:17:18 +00:00
type ErrParse struct {
Line string
Position string
}
2020-01-26 07:17:18 +00:00
func Parse(line string) ([]string, error) {
var args []string
buf := ""
2020-01-26 07:17:18 +00:00
var escaped, doubleQuoted, singleQuoted bool
backtick := ""
got := false
2020-01-26 07:17:18 +00:00
cursor := 0
2020-01-26 07:17:18 +00:00
runes := []rune(line)
for _, r := range runes {
if escaped {
buf += string(r)
escaped = false
continue
}
if r == '\\' {
if singleQuoted {
buf += string(r)
} else {
escaped = true
}
continue
}
if isSpace(r) {
switch {
2020-01-26 07:17:18 +00:00
case singleQuoted, doubleQuoted:
buf += string(r)
backtick += string(r)
case got:
2020-01-26 07:17:18 +00:00
cursor += len(buf)
args = append(args, buf)
buf = ""
got = false
}
continue
}
switch r {
case '"':
if !singleQuoted {
if doubleQuoted {
got = true
}
doubleQuoted = !doubleQuoted
continue
}
case '\'':
if !doubleQuoted {
if singleQuoted {
got = true
}
singleQuoted = !singleQuoted
continue
}
}
got = true
buf += string(r)
}
if got {
args = append(args, buf)
}
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))])
part = ""
)
for i := pos - 1; i >= 0 && i < len(runes) && i < pos+2; i++ {
if runes[i] == '\\' {
part += "\\"
}
part += string(runes[i])
}
2020-01-26 07:17:18 +00:00
return nil, errors.New(
"Unexpected quote or escape: " + start + "__" + part + "__" + end)
}
return args, nil
}
2020-01-26 07:17:18 +00:00
func isSpace(r rune) bool {
switch r {
case ' ', '\t', '\r', '\n':
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
}