Skip to content

Commit

Permalink
syntax: forbid ${!foo*} and ${!foo@} in mksh mode
Browse files Browse the repository at this point in the history
These forms exist in bash, but not mksh.
  • Loading branch information
mvdan committed Oct 16, 2022
1 parent c780bb4 commit f67ab4b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion syntax/filetests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2608,7 +2608,7 @@ var fileTests = []testCase{
},
{
Strs: []string{`${!foo*} ${!bar@}`},
bsmk: call(
bash: call(
word(&ParamExp{
Excl: true,
Param: lit("foo"),
Expand Down
9 changes: 6 additions & 3 deletions syntax/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1264,9 +1264,6 @@ func (p *Parser) paramExp() *ParamExp {
}
case exclMark:
if paramNameOp(p.r) {
if p.lang == LangPOSIX {
p.langErr(p.pos, "${!foo}", LangBash, LangMirBSDKorn)
}
pe.Excl = true
p.next()
}
Expand Down Expand Up @@ -1298,6 +1295,9 @@ func (p *Parser) paramExp() *ParamExp {
case _Lit, _LitWord:
p.curErr("%s cannot be followed by a word", op)
case rightBrace:
if pe.Excl && p.lang == LangPOSIX {
p.posErr(pe.Pos(), `"${!foo}" is a bash/mksh feature`)
}
pe.Rbrace = p.pos
p.quote = old
p.next()
Expand Down Expand Up @@ -1368,6 +1368,9 @@ func (p *Parser) paramExp() *ParamExp {
case p.tok == star && !pe.Excl:
p.curErr("not a valid parameter expansion operator: %v", p.tok)
case pe.Excl && p.r == '}':
if !p.lang.isBash() {
p.posErr(pe.Pos(), `"${!foo`+p.tok.String()+`}" is a bash feature`)
}
pe.Names = ParNamesOperator(p.tok)
p.next()
default:
Expand Down
16 changes: 15 additions & 1 deletion syntax/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1928,7 +1928,21 @@ var shellTests = []errorCase{
},
{
in: "echo ${!foo}",
posix: `1:8: ${!foo} is a bash/mksh feature`,
posix: `1:6: "${!foo}" is a bash/mksh feature`,
},
{
in: "echo ${!foo*}",
posix: `1:6: "${!foo*}" is a bash feature`,
mksh: `1:6: "${!foo*}" is a bash feature`,
},
{
in: "echo ${!foo@}",
posix: `1:12: this expansion operator is a bash/mksh feature`,
mksh: `1:6: "${!foo@}" is a bash feature`,
},
{
in: "echo ${!foo[@]}",
posix: `1:12: arrays are a bash/mksh feature`,
},
{
in: "echo ${foo[1]}",
Expand Down

0 comments on commit f67ab4b

Please sign in to comment.