nameref.sh revision da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968
########################################################################
# #
# This software is part of the ast package #
# Copyright (c) 1982-2007 AT&T Knowledge Ventures #
# and is licensed under the #
# Common Public License, Version 1.0 #
# by AT&T Knowledge Ventures #
# #
# 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
function setval # name value
{
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'