########################################################################
# #
# This software is part of the ast package #
# Copyright (c) 1982-2012 AT&T Intellectual Property #
# and is licensed under the #
# Eclipse Public License, Version 1.0 #
# by AT&T Intellectual Property #
# #
# A copy of the License is available at #
# (with md5 checksum b35adb5213ca9657e911e9befb180842) #
# #
# Information and Software Systems Research #
# AT&T Research #
# Florham Park NJ #
# #
# David Korn <dgk@research.att.com> #
# #
########################################################################
function err_exit
{
print -u2 -n "\t"
let Errors+=1
}
alias err_exit='err_exit $LINENO'
integer Errors=0
function checkref
{
fi
fi
fi
}
.foo.bar=next
fi
if [[ ${.foo.bar.child} != child ]]
then err_exit ".foo.bar.child=${.foo.bar.child} != child"
fi
function func1
{
nameref color=$1
}
function func2
{
nameref color=$1
set -s -- ${!color[@]}
print -r -- "$@"
}
typeset -A color
then err_exit "nameref or nameref not working"
fi
nameref x=.foo.bar
if [[ ${!x} != .foo.bar ]]
then err_exit "${!x} not working"
fi
typeset +n x $(typeset +n)
unset x
nameref x=.foo.bar
function x.set
{
}
if [[ $(.foo.bar.set) != $(x.set) ]]
then err_exit "function references not working"
fi
if [[ $(typeset +n) != x ]]
then err_exit "typeset +n doesn't list names of reference variables"
fi
if [[ $(typeset -n) != x=.foo.bar ]]
then err_exit "typeset +n doesn't list values of reference variables"
fi
fi
print -r -- $foo
!
y=$( $file)
if [[ $y != '' ]]
then err_exit "reference variable not cleared"
fi
{
set foo
nameref bar=$1
then err_exit 'nameref of positional paramters outside of function not working'
fi
bar=123
function foobar
{
}
(
short=( A=a B=b )
if [[ ${verylong.A} != a ]]
then err_exit 'nameref short to longname compound assignment error'
fi
unset x
do nameref x=$i
print $x
done) != $'1\n2' ]]
then err_exit 'for loop nameref optimization error'
fi
do
typeset -n array=$i
print ${!array[*]}
done) != $'sub1\nsub2' ]]
then err_exit 'for loop nameref optimization test2 error'
fi
then err_exit 'for loop optimization with namerefs not working'
fi
if [[ $(
p=(x=(r=3) y=(r=4))
for i in x y
do nameref x=p.$i
print ${x.r}
done
) != $'3\n4' ]]
then err_exit 'nameref optimization error'
fi
[[ $(
unset x y var
do typeset -n x=$i
if [[ ${!x.@} ]]
then print ok
fi
typeset +n x
{
nameref arg=$1
var=$2
}
function selfref
{
typeset -n ps=$1
print -r -- "${ps}"
}
ps=(a=1 b=2)
[[ $(selfref ps) == *a=1* ]] || err_exit 'local nameref cannot reference global variable of the same name'
function subref
{
typeset -n foo=$1
print -r -- ${foo.a}
}
function local
{
typeset ps=(typeset -i a=3 b=4)
}
local
unset -f local
function local
{
qs=(integer a=3; integer b=4)
}
unset fun i
function fun
{
nameref i=$1
print -r -- "${i.x}"
}
i=foo
[[ $(fun $i) == hi ]] || err_exit 'nameref for compound variable with in function name of caller fails'
typeset -A foo
typeset +n bar
$SHELL -c 'function bar { nameref x=foo[++];};typeset -A foo;bar' 2> /dev/null ||err_exit 'nameref of associative array tries to evaluate subscript'
[[ $i == 4 ]] || err_exit 'creating reference from subscripted variable whose name is a reference failed'
function bar
{
nameref x=$1
print -r -- "$x"
}
function foo
{
}
+++EOF
) == *foo=hello* ]] || err_exit 'unable to display compound variable from name reference of local variable'
#set -x
i=\\$c;typeset -A a; a[\$i]=foo;typeset -n x=a[\$i]; print "\$x"
++EOF++
) != foo ]] && err_exit 'nameref x=a[$c] '"not working for c=$c"
done
for c in '=' '[' ']' '\' "'" '"' '<' '=' '('
i=\\$c;typeset -A a; a[\$i]=foo;b=a[\$i];typeset -n x=\$b; print "\$x"
++EOF++
done
unset -n foo x
unset foo x
typeset -A foo
function function2
{
nameref v=$1
}
function function1
{
typeset compound_var=()
}
typeset +n bar
[[ $(function a
{
do typeset -n v=$i
print $v
done | cat
}
function a
{
}
vars.data._1.a=a.1
vars.data._1.b=b.1
typeset -A foo
[[ -R 123 ]] && err_exit '[[ -R 123 ]] should detect that 123 is not a reference'
[[ -v 123 ]] && err_exit '[[ -v 123 ]] should detect that 123 is not set'
unset ref x
typeset -n ref
x=3
function foobar
{
typeset xxx=3
return 0
}
ref=x
function foobar
{
typeset fvar=()
print -r $ref
}
[[ $(foobar) == ok ]] 2> /dev/null || err_exit 'nameref in function not creating variable in proper scope'
function foobar
{
}
typeset +n x y
unset x y
typeset -A x
x[a]=(b=c)
typeset -n y=x[a]
typeset +n v
v=()
k=a.b.c/d
command typeset -n n=v.${k//['./']/_} 2> /dev/null || err_exit 'patterns with quotes not handled correctly with name reference assignment'
function fun2
{
nameref var=$1
}
function fun1
{
compound -S container
[[ $container == *foo=bar* ]] || err_exit 'name references to static compound variables in parent scope not working'
}
function fun2
{
nameref var=$1
}
typeset -T container_t=(
typeset foo
)
function fun1
{
[[ $container == *foo=bar* ]] || err_exit 'name references to static type variables in parent scope not working'
}
function fun2
{
nameref var=$1
}
function fun3
{
}
compound container
[[ $container == *foo=bar* ]] || err_exit 'name reference to a name reference variable in a function not working'
typeset -A x=( [a]=1 )
nameref c=x[h]
[[ -v x[h] ]] && err_exit 'creating reference to non-existant associative array element causes element to get added'
unset a
function x
{
nameref a=a
(( $# > 0 )) && typeset -A a
a[a b]=${1-99} # this was cauing a syntax on the second call
}
x 7
[[ ${a[a b]} == 99 ]] || err_exit 'nameref not handling subscript correctly'
typeset -A -i sizes
$SHELL 2> /dev/null -c 'nameref foo=bar; typeset -A foo; (( (x=foo[a])==0 ))' || err_exit 'references inside arithmetic expressions not working'
:
unset ar z
integer -a ar
nameref z=ar[0]
(( z[2]=3))
unset c x
typeset +n c x
compound c=( typeset -a x )
nameref x=c.x
x[4]=1
typeset -T xxx_t=(
float x=1 y=2
)
xxx_t x
nameref r=x.y
[[ $r == 2 ]] || exit 1
unset x
[[ ${!r} == .deleted ]] || exit 2
exitval=$?
then print -u2 'name reference to unset type instance causes segmentation violation'
then print -u2 'name reference to unset type instance not redirected to .deleted'
fi
fi
typeset +n nr
unset c nr
compound c
compound -A c.a
[[ ${!nr} == "c.a[hello]" ]] || err_exit 'name reference nr to unset associative array instance does not expand ${!nr} correctly.'
typeset +n nr
compound -a c.b
[[ ${!nr} == "c.b[2]" ]] || err_exit 'name reference nr to unset indexed array instance does not expand ${!nr} correctly.'
typeset +n a b
unset a b
read line << \!
3 4
!
[[ $a == 3 ]] || err_exit 'name reference to ls[0] when ls is not an array fails'
set -o errexit
function bf {
nameref treename=$1
nameref x="$nodepath"
compound -A x.nodes
nameref node=treename.nodes[4]
node=()
typeset +p node.elements
}
compound c
bf c
function add_compound
{
nameref arr=$1
}
compound -a rootcpv
compound -a mycpv.myindexedcompoundarray
function add_file_to_tree
{
nameref node=$1
compound -A node.elements
node.elements[/]=(filepath=foobar)
}
function main
{
compound filetree
}
[[ $? == 0 ]] || err_exit 'nameref binding to calling function compound variable failed'
unset l
typeset -a -C l
exp=$(print -v l)
unset l
typeset -a -C l
nameref l4=l[4]
unset l
typeset -a -C l
nameref l46=l[4][6]
exp=$'(\n\t[4]=(\n\t\ttypeset -a ar=(\n\t\t\t1\n\t\t\t2\n\t\t)\n\t\tb=1\n\t)\n)'
unset l
typeset +n l4
typeset -a -C l
nameref l4=l[4]
unset l
typeset +n l4
typeset -A -C l
nameref l4=l[4]
exp=$'(\n\t[9]=(\n\t\tfish=4\n\t)\n)'
function add_eval
{
nameref pos=$1
}
function do_local_plain
{
compound -A local_tree
add_eval local_tree[9].fish "typeset -i addvar=4"
}
function do_global_throughnameref
{
nameref tr=global_tree
}
function do_local_throughnameref
{
compound -A local_tree
nameref tr=local_tree
}
compound -A global_tree
unset ar
compound -a ar
function read_c
{
nameref v=$1
read -C v
}
exp=$'(\n\t[5]=(\n\t\t[9]=(\n\t\t\t[2]=(\n\t\t\t\ttypeset -i x=36\n\t\t\t)\n\t\t)\n\t)\n)'
[[ $(print -v ar) == "$exp" ]] || err_exit 'read into nameref of global array instance from within a function fails'
function read_c
{
nameref v=$1
read -C v
}
function main
{
compound -a ar
exp=$'(\n\t[5]=(\n\t\t[9]=(\n\t\t\t[2]=(\n\t\t\t\ttypeset -i x=36\n\t\t\t)\n\t\t)\n\t)\n)'
}
function rf2
{
nameref val=$1
read -C val
}
function rf
{
nameref val=$1
}
function main
{
compound c
typeset -A -C c.l
exp=$'(\n\ttypeset -C -A l=(\n\t\t[4]=(\n\t\t\ttypeset -a ar=(\n\t\t\t\t1\n\t\t\t\t2\n\t\t\t\t3\n\t\t\t)\n\t\t\tb=1\n\t\t)\n\t)\n)'
}
# bug reported by ek
function a
{
typeset -n y=$1
print -- ${y.type}
}
function b
{
a $1
}
function yy
{
nameref n=$1
n=( z=4 )
}
unset foo
unset arr
function fun2
{
var=$2
}
function fun1
{
xxx=$1
}
typeset +n ref
typeset -a arr=( 1 2 3 )
typeset -n ref='arr[2]'
[[ $(typeset -p ref) == *'arr[2]'* ]] || err_exit 'typeset -p ref when ref is a reference to an index array element is wrong'
$SHELL 2> /dev/null -c 'function x { nameref lv=gg ; compound -A lv.c=( [4]=( x=1 )) ; } ; compound gg ; x' || err_exit 'compound array assignment with nameref in a function failed'