Add detection of "special parameters" for substitution

Detect Special parameters as defined in
http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_05_02

Treat these as parameters that are not set, instead of
producing an error that a modifier is missing.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2018-05-25 17:59:32 +02:00
parent 8687a3f4b8
commit 9654e9b6f8
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
3 changed files with 51 additions and 6 deletions

View file

@ -174,3 +174,38 @@ A|$9 |
A|${9} |
A|${9:+bbb} |
A|${9:-bbb} | bbb
# Special parameters won't be set in the Dockerfile:
# http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_05_02
A|$@ |
A|${@} |
A|${@:+bbb} |
A|${@:-bbb} | bbb
A|$* |
A|${*} |
A|${*:+bbb} |
A|${*:-bbb} | bbb
A|$# |
A|${#} |
A|${#:+bbb} |
A|${#:-bbb} | bbb
A|$? |
A|${?} |
A|${?:+bbb} |
A|${?:-bbb} | bbb
A|$- |
A|${-} |
A|${-:+bbb} |
A|${-:-bbb} | bbb
A|$$ |
A|${$} |
A|${$:+bbb} |
A|${$:-bbb} | bbb
A|$! |
A|${!} |
A|${!:+bbb} |
A|${!:-bbb} | bbb
A|$0 |
A|${0} |
A|${0:+bbb} |
A|${0:-bbb} | bbb

View file

@ -318,7 +318,7 @@ func (sw *shellWord) processName() string {
for sw.scanner.Peek() != scanner.EOF {
ch := sw.scanner.Peek()
if name.Len() == 0 && unicode.IsDigit(ch) {
if name.Len() == 0 && (unicode.IsDigit(ch) || isSpecialParam(ch)) {
ch = sw.scanner.Next()
return string(ch)
}
@ -332,6 +332,18 @@ func (sw *shellWord) processName() string {
return name.String()
}
// isSpecialParam checks if the provided character is a special parameters,
// as defined in http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_05_02
func isSpecialParam(char rune) bool {
switch char {
case '@', '*', '#', '?', '-', '$', '!', '0':
// Special parameters
// http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_05_02
return true
}
return false
}
func (sw *shellWord) getEnv(name string) string {
for _, env := range sw.envs {
i := strings.Index(env, "=")

View file

@ -26,13 +26,11 @@ func TestShellParser4EnvVars(t *testing.T) {
line := scanner.Text()
lineCount++
// Trim comments and blank lines
i := strings.Index(line, "#")
if i >= 0 {
line = line[:i]
// Skip comments and blank lines
if strings.HasPrefix(line, "#") {
continue
}
line = strings.TrimSpace(line)
if line == "" {
continue
}