2015-09-05 15:49:06 -04:00
|
|
|
package dockerfile
|
2015-01-28 21:28:48 -05:00
|
|
|
|
|
|
|
import (
|
2017-04-13 11:56:37 -04:00
|
|
|
"bytes"
|
2015-01-28 21:28:48 -05:00
|
|
|
"strings"
|
2015-10-18 23:55:53 -04:00
|
|
|
"text/scanner"
|
2015-01-28 21:28:48 -05:00
|
|
|
"unicode"
|
2017-04-13 11:56:37 -04:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2015-01-28 21:28:48 -05:00
|
|
|
)
|
|
|
|
|
2017-04-26 18:24:41 -04:00
|
|
|
// ShellLex performs shell word splitting and variable expansion.
|
|
|
|
//
|
|
|
|
// ShellLex takes a string and an array of env variables and
|
|
|
|
// process all quotes (" and ') as well as $xxx and ${xxx} env variable
|
|
|
|
// tokens. Tries to mimic bash shell process.
|
|
|
|
// It doesn't support all flavors of ${xx:...} formats but new ones can
|
|
|
|
// be added by adding code to the "special ${} format processing" section
|
|
|
|
type ShellLex struct {
|
2016-10-21 14:55:39 -04:00
|
|
|
escapeToken rune
|
2015-01-28 21:28:48 -05:00
|
|
|
}
|
|
|
|
|
2017-04-26 18:24:41 -04:00
|
|
|
// NewShellLex creates a new ShellLex which uses escapeToken to escape quotes.
|
|
|
|
func NewShellLex(escapeToken rune) *ShellLex {
|
|
|
|
return &ShellLex{escapeToken: escapeToken}
|
|
|
|
}
|
|
|
|
|
2015-07-22 01:29:03 -04:00
|
|
|
// ProcessWord will use the 'env' list of environment variables,
|
|
|
|
// and replace any env var references in 'word'.
|
2017-04-26 18:24:41 -04:00
|
|
|
func (s *ShellLex) ProcessWord(word string, env []string) (string, error) {
|
|
|
|
word, _, err := s.process(word, env)
|
2017-04-04 13:40:37 -04:00
|
|
|
return word, err
|
2015-01-28 21:28:48 -05:00
|
|
|
}
|
|
|
|
|
2015-11-07 15:05:55 -05:00
|
|
|
// ProcessWords will use the 'env' list of environment variables,
|
|
|
|
// and replace any env var references in 'word' then it will also
|
|
|
|
// return a slice of strings which represents the 'word'
|
|
|
|
// split up based on spaces - taking into account quotes. Note that
|
|
|
|
// this splitting is done **after** the env var substitutions are done.
|
|
|
|
// Note, each one is trimmed to remove leading and trailing spaces (unless
|
|
|
|
// they are quoted", but ProcessWord retains spaces between words.
|
2017-04-26 18:24:41 -04:00
|
|
|
func (s *ShellLex) ProcessWords(word string, env []string) ([]string, error) {
|
|
|
|
_, words, err := s.process(word, env)
|
2017-03-13 17:25:37 -04:00
|
|
|
return words, err
|
|
|
|
}
|
|
|
|
|
2017-04-26 18:24:41 -04:00
|
|
|
func (s *ShellLex) process(word string, env []string) (string, []string, error) {
|
2015-11-07 15:05:55 -05:00
|
|
|
sw := &shellWord{
|
2016-10-21 14:55:39 -04:00
|
|
|
envs: env,
|
2017-04-26 18:24:41 -04:00
|
|
|
escapeToken: s.escapeToken,
|
2015-11-07 15:05:55 -05:00
|
|
|
}
|
|
|
|
sw.scanner.Init(strings.NewReader(word))
|
2017-04-26 18:24:41 -04:00
|
|
|
return sw.process(word)
|
|
|
|
}
|
|
|
|
|
|
|
|
type shellWord struct {
|
|
|
|
scanner scanner.Scanner
|
|
|
|
envs []string
|
|
|
|
escapeToken rune
|
2015-11-07 15:05:55 -05:00
|
|
|
}
|
|
|
|
|
2017-04-26 18:24:41 -04:00
|
|
|
func (sw *shellWord) process(source string) (string, []string, error) {
|
|
|
|
word, words, err := sw.processStopOn(scanner.EOF)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrapf(err, "failed to process %q", source)
|
|
|
|
}
|
|
|
|
return word, words, err
|
2015-01-28 21:28:48 -05:00
|
|
|
}
|
|
|
|
|
2015-11-07 15:05:55 -05:00
|
|
|
type wordsStruct struct {
|
|
|
|
word string
|
|
|
|
words []string
|
|
|
|
inWord bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wordsStruct) addChar(ch rune) {
|
|
|
|
if unicode.IsSpace(ch) && w.inWord {
|
|
|
|
if len(w.word) != 0 {
|
|
|
|
w.words = append(w.words, w.word)
|
|
|
|
w.word = ""
|
|
|
|
w.inWord = false
|
|
|
|
}
|
|
|
|
} else if !unicode.IsSpace(ch) {
|
|
|
|
w.addRawChar(ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wordsStruct) addRawChar(ch rune) {
|
|
|
|
w.word += string(ch)
|
|
|
|
w.inWord = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wordsStruct) addString(str string) {
|
|
|
|
var scan scanner.Scanner
|
|
|
|
scan.Init(strings.NewReader(str))
|
|
|
|
for scan.Peek() != scanner.EOF {
|
|
|
|
w.addChar(scan.Next())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wordsStruct) addRawString(str string) {
|
|
|
|
w.word += str
|
|
|
|
w.inWord = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wordsStruct) getWords() []string {
|
|
|
|
if len(w.word) > 0 {
|
|
|
|
w.words = append(w.words, w.word)
|
|
|
|
|
|
|
|
// Just in case we're called again by mistake
|
|
|
|
w.word = ""
|
|
|
|
w.inWord = false
|
|
|
|
}
|
|
|
|
return w.words
|
|
|
|
}
|
|
|
|
|
2015-01-28 21:28:48 -05:00
|
|
|
// Process the word, starting at 'pos', and stop when we get to the
|
|
|
|
// end of the word or the 'stopChar' character
|
2015-11-07 15:05:55 -05:00
|
|
|
func (sw *shellWord) processStopOn(stopChar rune) (string, []string, error) {
|
2017-04-13 11:56:37 -04:00
|
|
|
var result bytes.Buffer
|
2015-11-07 15:05:55 -05:00
|
|
|
var words wordsStruct
|
|
|
|
|
2015-01-28 21:28:48 -05:00
|
|
|
var charFuncMapping = map[rune]func() (string, error){
|
|
|
|
'\'': sw.processSingleQuote,
|
|
|
|
'"': sw.processDoubleQuote,
|
|
|
|
'$': sw.processDollar,
|
|
|
|
}
|
|
|
|
|
2015-10-18 23:55:53 -04:00
|
|
|
for sw.scanner.Peek() != scanner.EOF {
|
|
|
|
ch := sw.scanner.Peek()
|
|
|
|
|
|
|
|
if stopChar != scanner.EOF && ch == stopChar {
|
|
|
|
sw.scanner.Next()
|
2015-01-28 21:28:48 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if fn, ok := charFuncMapping[ch]; ok {
|
|
|
|
// Call special processing func for certain chars
|
|
|
|
tmp, err := fn()
|
|
|
|
if err != nil {
|
2015-11-07 15:05:55 -05:00
|
|
|
return "", []string{}, err
|
2015-01-28 21:28:48 -05:00
|
|
|
}
|
2017-04-13 11:56:37 -04:00
|
|
|
result.WriteString(tmp)
|
2015-11-07 15:05:55 -05:00
|
|
|
|
|
|
|
if ch == rune('$') {
|
|
|
|
words.addString(tmp)
|
|
|
|
} else {
|
|
|
|
words.addRawString(tmp)
|
|
|
|
}
|
2015-01-28 21:28:48 -05:00
|
|
|
} else {
|
|
|
|
// Not special, just add it to the result
|
2015-10-18 23:55:53 -04:00
|
|
|
ch = sw.scanner.Next()
|
|
|
|
|
2016-10-21 14:55:39 -04:00
|
|
|
if ch == sw.escapeToken {
|
|
|
|
// '\' (default escape token, but ` allowed) escapes, except end of line
|
2015-10-18 23:55:53 -04:00
|
|
|
ch = sw.scanner.Next()
|
|
|
|
|
|
|
|
if ch == scanner.EOF {
|
|
|
|
break
|
2015-01-28 21:28:48 -05:00
|
|
|
}
|
2015-10-18 23:55:53 -04:00
|
|
|
|
2015-11-07 15:05:55 -05:00
|
|
|
words.addRawChar(ch)
|
|
|
|
} else {
|
|
|
|
words.addChar(ch)
|
2015-01-28 21:28:48 -05:00
|
|
|
}
|
2015-10-18 23:55:53 -04:00
|
|
|
|
2017-04-13 11:56:37 -04:00
|
|
|
result.WriteRune(ch)
|
2015-01-28 21:28:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 11:56:37 -04:00
|
|
|
return result.String(), words.getWords(), nil
|
2015-01-28 21:28:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sw *shellWord) processSingleQuote() (string, error) {
|
|
|
|
// All chars between single quotes are taken as-is
|
|
|
|
// Note, you can't escape '
|
2017-04-03 13:45:39 -04:00
|
|
|
//
|
|
|
|
// From the "sh" man page:
|
|
|
|
// Single Quotes
|
|
|
|
// Enclosing characters in single quotes preserves the literal meaning of
|
|
|
|
// all the characters (except single quotes, making it impossible to put
|
|
|
|
// single-quotes in a single-quoted string).
|
|
|
|
|
2017-04-13 11:56:37 -04:00
|
|
|
var result bytes.Buffer
|
2015-01-28 21:28:48 -05:00
|
|
|
|
2015-10-18 23:55:53 -04:00
|
|
|
sw.scanner.Next()
|
2015-01-28 21:28:48 -05:00
|
|
|
|
|
|
|
for {
|
2015-10-18 23:55:53 -04:00
|
|
|
ch := sw.scanner.Next()
|
2017-04-13 11:56:37 -04:00
|
|
|
switch ch {
|
|
|
|
case scanner.EOF:
|
|
|
|
return "", errors.New("unexpected end of statement while looking for matching single-quote")
|
|
|
|
case '\'':
|
|
|
|
return result.String(), nil
|
2015-01-28 21:28:48 -05:00
|
|
|
}
|
2017-04-13 11:56:37 -04:00
|
|
|
result.WriteRune(ch)
|
2015-01-28 21:28:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sw *shellWord) processDoubleQuote() (string, error) {
|
|
|
|
// All chars up to the next " are taken as-is, even ', except any $ chars
|
2016-10-21 14:55:39 -04:00
|
|
|
// But you can escape " with a \ (or ` if escape token set accordingly)
|
2017-04-03 13:45:39 -04:00
|
|
|
//
|
|
|
|
// From the "sh" man page:
|
|
|
|
// Double Quotes
|
|
|
|
// Enclosing characters within double quotes preserves the literal meaning
|
|
|
|
// of all characters except dollarsign ($), backquote (`), and backslash
|
|
|
|
// (\). The backslash inside double quotes is historically weird, and
|
|
|
|
// serves to quote only the following characters:
|
|
|
|
// $ ` " \ <newline>.
|
|
|
|
// Otherwise it remains literal.
|
|
|
|
|
2017-04-13 11:56:37 -04:00
|
|
|
var result bytes.Buffer
|
2015-01-28 21:28:48 -05:00
|
|
|
|
2015-10-18 23:55:53 -04:00
|
|
|
sw.scanner.Next()
|
2015-01-28 21:28:48 -05:00
|
|
|
|
2017-04-03 13:45:39 -04:00
|
|
|
for {
|
2017-04-13 11:56:37 -04:00
|
|
|
switch sw.scanner.Peek() {
|
|
|
|
case scanner.EOF:
|
|
|
|
return "", errors.New("unexpected end of statement while looking for matching double-quote")
|
|
|
|
case '"':
|
2015-10-18 23:55:53 -04:00
|
|
|
sw.scanner.Next()
|
2017-04-13 11:56:37 -04:00
|
|
|
return result.String(), nil
|
|
|
|
case '$':
|
|
|
|
value, err := sw.processDollar()
|
2015-01-28 21:28:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-04-13 11:56:37 -04:00
|
|
|
result.WriteString(value)
|
|
|
|
default:
|
|
|
|
ch := sw.scanner.Next()
|
2016-10-21 14:55:39 -04:00
|
|
|
if ch == sw.escapeToken {
|
2017-04-13 11:56:37 -04:00
|
|
|
switch sw.scanner.Peek() {
|
|
|
|
case scanner.EOF:
|
2015-01-28 21:28:48 -05:00
|
|
|
// Ignore \ at end of word
|
|
|
|
continue
|
2017-04-13 11:56:37 -04:00
|
|
|
case '"', '$', sw.escapeToken:
|
2017-04-03 13:45:39 -04:00
|
|
|
// These chars can be escaped, all other \'s are left as-is
|
2017-04-13 11:56:37 -04:00
|
|
|
// Note: for now don't do anything special with ` chars.
|
|
|
|
// Not sure what to do with them anyway since we're not going
|
|
|
|
// to execute the text in there (not now anyway).
|
2015-10-18 23:55:53 -04:00
|
|
|
ch = sw.scanner.Next()
|
2015-01-28 21:28:48 -05:00
|
|
|
}
|
|
|
|
}
|
2017-04-13 11:56:37 -04:00
|
|
|
result.WriteRune(ch)
|
2015-01-28 21:28:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sw *shellWord) processDollar() (string, error) {
|
2015-10-18 23:55:53 -04:00
|
|
|
sw.scanner.Next()
|
2017-04-13 11:56:37 -04:00
|
|
|
|
|
|
|
// $xxx case
|
|
|
|
if sw.scanner.Peek() != '{' {
|
2015-01-28 21:28:48 -05:00
|
|
|
name := sw.processName()
|
2017-04-13 11:56:37 -04:00
|
|
|
if name == "" {
|
|
|
|
return "$", nil
|
2015-01-28 21:28:48 -05:00
|
|
|
}
|
2017-04-13 11:56:37 -04:00
|
|
|
return sw.getEnv(name), nil
|
|
|
|
}
|
2015-01-28 21:28:48 -05:00
|
|
|
|
2017-04-13 11:56:37 -04:00
|
|
|
sw.scanner.Next()
|
|
|
|
name := sw.processName()
|
|
|
|
ch := sw.scanner.Peek()
|
|
|
|
if ch == '}' {
|
|
|
|
// Normal ${xx} case
|
|
|
|
sw.scanner.Next()
|
|
|
|
return sw.getEnv(name), nil
|
|
|
|
}
|
|
|
|
if ch == ':' {
|
|
|
|
// Special ${xx:...} format processing
|
|
|
|
// Yes it allows for recursive $'s in the ... spot
|
2015-01-28 21:28:48 -05:00
|
|
|
|
2017-04-13 11:56:37 -04:00
|
|
|
sw.scanner.Next() // skip over :
|
|
|
|
modifier := sw.scanner.Next()
|
2015-01-28 21:28:48 -05:00
|
|
|
|
2017-04-13 11:56:37 -04:00
|
|
|
word, _, err := sw.processStopOn('}')
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-01-28 21:28:48 -05:00
|
|
|
|
2017-04-13 11:56:37 -04:00
|
|
|
// Grab the current value of the variable in question so we
|
|
|
|
// can use to to determine what to do based on the modifier
|
|
|
|
newValue := sw.getEnv(name)
|
2015-01-28 21:28:48 -05:00
|
|
|
|
2017-04-13 11:56:37 -04:00
|
|
|
switch modifier {
|
|
|
|
case '+':
|
|
|
|
if newValue != "" {
|
|
|
|
newValue = word
|
|
|
|
}
|
|
|
|
return newValue, nil
|
2015-01-28 21:28:48 -05:00
|
|
|
|
2017-04-13 11:56:37 -04:00
|
|
|
case '-':
|
|
|
|
if newValue == "" {
|
|
|
|
newValue = word
|
2015-01-28 21:28:48 -05:00
|
|
|
}
|
2017-04-13 11:56:37 -04:00
|
|
|
return newValue, nil
|
|
|
|
|
|
|
|
default:
|
2017-04-26 18:24:41 -04:00
|
|
|
return "", errors.Errorf("unsupported modifier (%c) in substitution", modifier)
|
2015-01-28 21:28:48 -05:00
|
|
|
}
|
2015-03-23 15:21:37 -04:00
|
|
|
}
|
2017-04-26 18:24:41 -04:00
|
|
|
return "", errors.Errorf("missing ':' in substitution")
|
2015-01-28 21:28:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sw *shellWord) processName() string {
|
|
|
|
// Read in a name (alphanumeric or _)
|
|
|
|
// If it starts with a numeric then just return $#
|
2017-04-13 11:56:37 -04:00
|
|
|
var name bytes.Buffer
|
2015-01-28 21:28:48 -05:00
|
|
|
|
2015-10-18 23:55:53 -04:00
|
|
|
for sw.scanner.Peek() != scanner.EOF {
|
|
|
|
ch := sw.scanner.Peek()
|
2017-04-13 11:56:37 -04:00
|
|
|
if name.Len() == 0 && unicode.IsDigit(ch) {
|
2015-10-18 23:55:53 -04:00
|
|
|
ch = sw.scanner.Next()
|
2015-01-28 21:28:48 -05:00
|
|
|
return string(ch)
|
|
|
|
}
|
|
|
|
if !unicode.IsLetter(ch) && !unicode.IsDigit(ch) && ch != '_' {
|
|
|
|
break
|
|
|
|
}
|
2015-10-18 23:55:53 -04:00
|
|
|
ch = sw.scanner.Next()
|
2017-04-13 11:56:37 -04:00
|
|
|
name.WriteRune(ch)
|
2015-01-28 21:28:48 -05:00
|
|
|
}
|
|
|
|
|
2017-04-13 11:56:37 -04:00
|
|
|
return name.String()
|
2015-01-28 21:28:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sw *shellWord) getEnv(name string) string {
|
|
|
|
for _, env := range sw.envs {
|
|
|
|
i := strings.Index(env, "=")
|
|
|
|
if i < 0 {
|
2017-04-04 12:28:59 -04:00
|
|
|
if equalEnvKeys(name, env) {
|
2015-01-28 21:28:48 -05:00
|
|
|
// Should probably never get here, but just in case treat
|
|
|
|
// it like "var" and "var=" are the same
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2016-11-22 14:26:02 -05:00
|
|
|
compareName := env[:i]
|
2017-04-04 12:28:59 -04:00
|
|
|
if !equalEnvKeys(name, compareName) {
|
2015-01-28 21:28:48 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return env[i+1:]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|