#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# change behavior of shell builtins to extend assoc_expand_once to indexed
# arrays

export subscript='$(echo INJECTION! >&2 ; echo 0)'
shopt -s array_expand_once

printf -v a["$subscript"] %s hi
declare -p a
unset a

printf -v "a[$subscript]" %s hi
declare -p a

a[0]=hi

unset a["$subscript"]
declare -p a

unset a

unset a["$subscript"]
declare -p a

unset -v a
read a["$subscript"] <<<hi
declare -p a

declare -a a
read a["$subscript"] <<<hi
declare -p a

unset -v a

declare -a a

{ sleep 1; exit 12; } & bgpid=$!
wait -n -p a["$subscript"] $bgpid

declare -p a

unset -v a
declare -a a

declare -i a["$subscript"]=42
declare -p a

# this still won't work because the quotes prevent it from being recognized as
# an assignment statement
#declare -i "a[$subscript]"=42
#declare -p a

test -v a["$subscript"] && echo set
[ -v a["$subscript"] ] && echo set

let a["$subscript"]+=1
unset -v a

# these are all already arithmetic expression errors

declare -a a

(( a[$subscript]++ ))
declare -p a
: $(( a[$subscript]++ ))
declare -p a

a[$subscript]=hi
declare -p a

# length shortcuts for unset variables, so give it a value
a[0]=zero
echo ${#a[$subscript]}

unset -v a

# compound assignments should not perform double expansion

a=( [$subscript]=hi )
declare -p a

declare -a a
a=( [$subscript]=hi )
declare -p a

unset -v a
