2016-10-03 20:50:56 +00:00
|
|
|
package shellwords
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ParseEnv bool = false
|
|
|
|
ParseBacktick bool = false
|
|
|
|
)
|
|
|
|
|
|
|
|
var envRe = regexp.MustCompile(`\$({[a-zA-Z0-9_]+}|[a-zA-Z0-9_]+)`)
|
|
|
|
|
|
|
|
func isSpace(r rune) bool {
|
|
|
|
switch r {
|
|
|
|
case ' ', '\t', '\r', '\n':
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-30 14:29:47 +00:00
|
|
|
func replaceEnv(getenv func(string) string, s string) string {
|
|
|
|
if getenv == nil {
|
|
|
|
getenv = os.Getenv
|
|
|
|
}
|
|
|
|
|
2016-10-03 20:50:56 +00:00
|
|
|
return envRe.ReplaceAllStringFunc(s, func(s string) string {
|
|
|
|
s = s[1:]
|
|
|
|
if s[0] == '{' {
|
|
|
|
s = s[1 : len(s)-1]
|
|
|
|
}
|
2020-05-30 14:29:47 +00:00
|
|
|
return getenv(s)
|
2016-10-03 20:50:56 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type Parser struct {
|
|
|
|
ParseEnv bool
|
|
|
|
ParseBacktick bool
|
2020-05-30 14:29:47 +00:00
|
|
|
Position int
|
|
|
|
|
|
|
|
// If ParseEnv is true, use this for getenv.
|
|
|
|
// If nil, use os.Getenv.
|
|
|
|
Getenv func(string) string
|
2016-10-03 20:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewParser() *Parser {
|
2020-05-30 14:29:47 +00:00
|
|
|
return &Parser{
|
|
|
|
ParseEnv: ParseEnv,
|
|
|
|
ParseBacktick: ParseBacktick,
|
|
|
|
Position: 0,
|
|
|
|
}
|
2016-10-03 20:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) Parse(line string) ([]string, error) {
|
|
|
|
args := []string{}
|
|
|
|
buf := ""
|
2020-05-30 14:29:47 +00:00
|
|
|
var escaped, doubleQuoted, singleQuoted, backQuote, dollarQuote bool
|
2016-10-03 20:50:56 +00:00
|
|
|
backtick := ""
|
|
|
|
|
2020-05-30 14:29:47 +00:00
|
|
|
pos := -1
|
|
|
|
got := false
|
|
|
|
|
|
|
|
loop:
|
|
|
|
for i, r := range line {
|
2016-10-03 20:50:56 +00:00
|
|
|
if escaped {
|
|
|
|
buf += string(r)
|
|
|
|
escaped = false
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if r == '\\' {
|
|
|
|
if singleQuoted {
|
|
|
|
buf += string(r)
|
|
|
|
} else {
|
|
|
|
escaped = true
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if isSpace(r) {
|
2020-05-30 14:29:47 +00:00
|
|
|
if singleQuoted || doubleQuoted || backQuote || dollarQuote {
|
2016-10-03 20:50:56 +00:00
|
|
|
buf += string(r)
|
|
|
|
backtick += string(r)
|
2020-05-30 14:29:47 +00:00
|
|
|
} else if got {
|
2016-10-03 20:50:56 +00:00
|
|
|
if p.ParseEnv {
|
2020-05-30 14:29:47 +00:00
|
|
|
buf = replaceEnv(p.Getenv, buf)
|
2016-10-03 20:50:56 +00:00
|
|
|
}
|
|
|
|
args = append(args, buf)
|
|
|
|
buf = ""
|
2020-05-30 14:29:47 +00:00
|
|
|
got = false
|
2016-10-03 20:50:56 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r {
|
|
|
|
case '`':
|
2020-05-30 14:29:47 +00:00
|
|
|
if !singleQuoted && !doubleQuoted && !dollarQuote {
|
2016-10-03 20:50:56 +00:00
|
|
|
if p.ParseBacktick {
|
|
|
|
if backQuote {
|
|
|
|
out, err := shellRun(backtick)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
buf = out
|
|
|
|
}
|
|
|
|
backtick = ""
|
|
|
|
backQuote = !backQuote
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
backtick = ""
|
|
|
|
backQuote = !backQuote
|
|
|
|
}
|
2020-05-30 14:29:47 +00:00
|
|
|
case ')':
|
|
|
|
if !singleQuoted && !doubleQuoted && !backQuote {
|
|
|
|
if p.ParseBacktick {
|
|
|
|
if dollarQuote {
|
|
|
|
out, err := shellRun(backtick)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if r == ')' {
|
|
|
|
buf = buf[:len(buf)-len(backtick)-2] + out
|
|
|
|
} else {
|
|
|
|
buf = buf[:len(buf)-len(backtick)-1] + out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
backtick = ""
|
|
|
|
dollarQuote = !dollarQuote
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
backtick = ""
|
|
|
|
dollarQuote = !dollarQuote
|
|
|
|
}
|
|
|
|
case '(':
|
|
|
|
if !singleQuoted && !doubleQuoted && !backQuote {
|
|
|
|
if !dollarQuote && strings.HasSuffix(buf, "$") {
|
|
|
|
dollarQuote = true
|
|
|
|
buf += "("
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("invalid command line string")
|
|
|
|
}
|
|
|
|
}
|
2016-10-03 20:50:56 +00:00
|
|
|
case '"':
|
2020-05-30 14:29:47 +00:00
|
|
|
if !singleQuoted && !dollarQuote {
|
2016-10-03 20:50:56 +00:00
|
|
|
doubleQuoted = !doubleQuoted
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case '\'':
|
2020-05-30 14:29:47 +00:00
|
|
|
if !doubleQuoted && !dollarQuote {
|
2016-10-03 20:50:56 +00:00
|
|
|
singleQuoted = !singleQuoted
|
|
|
|
continue
|
|
|
|
}
|
2020-05-30 14:29:47 +00:00
|
|
|
case ';', '&', '|', '<', '>':
|
|
|
|
if !(escaped || singleQuoted || doubleQuoted || backQuote) {
|
|
|
|
if r == '>' && len(buf) > 0 {
|
|
|
|
if c := buf[0]; '0' <= c && c <= '9' {
|
|
|
|
i -= 1
|
|
|
|
got = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pos = i
|
|
|
|
break loop
|
|
|
|
}
|
2016-10-03 20:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-05-30 14:29:47 +00:00
|
|
|
got = true
|
2016-10-03 20:50:56 +00:00
|
|
|
buf += string(r)
|
2020-05-30 14:29:47 +00:00
|
|
|
if backQuote || dollarQuote {
|
2016-10-03 20:50:56 +00:00
|
|
|
backtick += string(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 14:29:47 +00:00
|
|
|
if got {
|
2016-10-03 20:50:56 +00:00
|
|
|
if p.ParseEnv {
|
2020-05-30 14:29:47 +00:00
|
|
|
buf = replaceEnv(p.Getenv, buf)
|
2016-10-03 20:50:56 +00:00
|
|
|
}
|
|
|
|
args = append(args, buf)
|
|
|
|
}
|
|
|
|
|
2020-05-30 14:29:47 +00:00
|
|
|
if escaped || singleQuoted || doubleQuoted || backQuote || dollarQuote {
|
2016-10-03 20:50:56 +00:00
|
|
|
return nil, errors.New("invalid command line string")
|
|
|
|
}
|
|
|
|
|
2020-05-30 14:29:47 +00:00
|
|
|
p.Position = pos
|
|
|
|
|
2016-10-03 20:50:56 +00:00
|
|
|
return args, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Parse(line string) ([]string, error) {
|
|
|
|
return NewParser().Parse(line)
|
|
|
|
}
|