########################################################################
# #
# This software is part of the ast package #
# Copyright (c) 1982-2010 AT&T Intellectual Property #
# and is licensed under the #
# Common Public License, Version 1.0 #
# by AT&T Intellectual Property #
# #
# A copy of the License is available at #
# (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) #
# #
# 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
x;i=\\$c;typeset -A a; a[\$i]=foo;typeset -n x=a[\$i]; print "\$x"
++EOF++
) != foo ]] && err_exit 'nameref x=[$c] '"not working for c=$c"
done
unset -n foo x
unset foo x
typeset -A foo
nameref x=foo[xyz]
foo[xyz]=ok
function function2
{
nameref v=$1
}
function function1
{
typeset compound_var=()
function2 compound_var
printf "x=%d, y=%d\n" compound_var.x compound_var.y
}
x="$(function1)"
[[ "$x" != 'x=19, y=20' ]] && err_exit "expected 'x=19, y=20', got '${x}'"
typeset +n bar
unset foo bar
[[ $(function a
{
for i in foo bar
do typeset -n v=$i
print $v
done | cat
}
function a
{
typeset -n v=vars.data._1
}
vars=(data=())
vars.data._1.a=a.1
vars.data._1.b=b.1
typeset +n bam zip foo
unset bam zip foo
typeset -A foo
foo[2]=bar
typeset -n bam=foo[2]
typeset -n zip=bam
unset ref x
typeset -n ref
x=3
function foobar
{
typeset xxx=3
ref=xxx
return 0
}
ref=x
[[ $ref == 3 ]] || err_exit "\$ref is $ref, it should be 3"
function foobar
{
typeset fvar=()
typeset -n ref=fvar.foo
ref=ok
print -r $ref
}
[[ $(foobar) == ok ]] 2> /dev/null || err_exit 'nameref in function not creating variable in proper scope'
function foobar
{
nameref doc=docs
nameref bar=doc.num
}
docs=(num=2)
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'
typeset _n sp
nameref sp=addrsp
sp[14]=( size=1 )
exit $((Errors))