comvar.sh revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
########################################################################
# #
# 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'
#test for compound variables
integer Errors=0
Point=(
float x=1. y=0.
)
eval p="$Point"
then err_exit 'compound variable not working'
fi
nameref foo=p
then err_exit 'reference to compound object not working'
fi
unset foo
rec=(
name='Joe Blow'
born=(
integer day=16
year=1980
)
)
if [[ ${newrec.name} != "${rec.name}" ]]
then err_exit 'copying a compound object not working'
fi
then err_exit 'copying integer field of compound object not working'
fi
p_t=(
integer z=0
typeset -A tokens
)
unset x
typeset -A x
if [[ ${x[@]} != bar ]]
then err_exit 'compound assignemnt of associative arrays not working'
fi
unset -n foo x
unset foo x
foo=( x=3)
nameref x=foo
if [[ ${!x.@} != foo.x ]]
then err_exit 'name references not expanded on prefix matching'
fi
unset x
unset -n x
(
x=()
)
foo=(integer x=3)
if [[ ${foo} != *x=3* ]]
then err_exit "compound variable with integer subvariable not working"
fi
err_exit '[[ ... ]] not working after compound assignment'
unset foo
suitable=(
label="Table Viewer"
langs="ksh"
groups="default"
default=(
label="Table Viewer Preferences"
entrylist=" \
vieworigin viewsize viewcolor viewfontname viewfontsize \
showheader header showfooter footer showtitle title showlegends \
class_td_lg1_style class_tr_tr1_style \
class_th_th1_style class_td_td1_style \
fields fieldorder \
"
entries=(
)
viewsize=(
)
label="Window Colors"
)
type=fontname var=viewfontname val="Times-Roman"
label="Window Font Name"
)
type=fontsize var=viewfontsize val=14 label="Window Font Size"
)
)
header=(
)
)
footer=(
)
)
title=(
)
)
type=style var=class_td_lg1_style
val="color: black; font-family: Times-Roman; font-size: 14pt"
label="Legend 1 Style"
)
type=style var=class_tr_tr1_style val="background: black"
label="Table Row 1 Style"
)
type=style var=class_th_th1_style
val="color: black; font-family: Times-Roman; font-size: 14pt; text-align: left"
label="Table Header 1 Style"
)
type=style var=class_td_td1_style
val="color: black; font-family: Times-Roman; font-size: 14pt; text-align: left"
label="Table Cell 1 Style"
)
fields=(
)
type=text var=fieldorder val= label="Order of Fields"
)
)
)
)
function localvar
{
typeset point=(typeset -i x=3 y=4)
}
point=(integer x=6 y=8)
(( (point.x*point.x + point.y*point.y) == 100 )) || err_exit "global compound variable not preserved"
[[ $($SHELL -c 'foo=();foo.[x]=(y z); print ${foo.x[@]}') == 'y z' ]] 2> /dev/null || err_exit 'foo=( [x]=(y z) not working'
function staticvar
{
if [[ $1 ]]
then print -r -- "$point"
return
fi
typeset -S point=(typeset -i x=3 y=4)
point.y=5
}
(( (point.x*point.x + point.y*point.y) == 100 )) || err_exit "global compound variable not preserved"
[[ $(staticvar x) == $'(\n\ttypeset -i x=3\n\ttypeset -i y=5\n\tz=foobar\n)' ]] || err_exit 'static variables in function not working'
integer x=3
unset z
stack=()
typeset -a stack.items=([0]=foo [1]=bar)
$SHELL -c 'typeset -a info=( [1]=( passwd=( since=2005-07-20) ))' || err_exit 'problem with embedded index array in compound variable'
unset z
z=1
function foo
{
z=3
print hi
}
function a.b.get
{
}
function dcl1
{
eval 'a=1
function a.set
}
function dcl2
{
eval 'b=(typeset x=0; typeset y=0 )
function b.x.set
}
eval 'b=(typeset x=0; typeset y=0 )
function b.x.set
unset a b
function func
{
typeset X
X=( bar=2 )
}
X=( foo=1 )
unset foo
[[ $({ $SHELL -c 'x=(); typeset -a x.foo; x.foo=bar; print -r -- "$x"' ;} 2> /dev/null) == $'(\n\ttypeset -a foo=bar\n)' ]] || err_exit 'indexed array in compound variable with only element 0 defined fails'
unset foo
[[ $foo == *'typeset -a bar'* ]] || err_exit 'array attribute -a not preserved in compound variable'
unset s
[[ ${s[@]} == *z=*z=* ]] || err_exit 'missing elements in compound associative array'
unset nodes
typeset -A nodes
unset foo
typeset -C foo
foo=(
)
{
read -ru3
} 3<<- ++++
"$foo"
"$foo2"
++++
eval "${y/#x/y}"
unset x y z
typeset -C y=x
unset y
y=()
y=x
function foobar
{
typeset -C z
z=x
y=z
}
y+=z
unset y.foo
unset x y
unset x
x=()
unset z
z=()
exp='(
typeset -a bar=(
[0]=hello
[2]=world
[1]=(
x=4
y=5
)
)
typeset -A foo=(
[one]=hello
[three]=hi
[two]=(
x=3
y=4
)
)
)'
got=$z
}
typeset -A record
record[a]=(
typeset -a x=(
[1]=(
X=1
)
)
)
exp=$'(\n\ttypeset -a x=(\n\t\t[1]=(\n\t\t\tX=1\n\t\t)\n\t)\n)'
}
unset r
r=(
typeset -a x=(
[1]=(
X=1
)
)
)
exp=$'(\n\ttypeset -a x=(\n\t\t[1]=(\n\t\t\tX=1\n\t\t)\n\t)\n)'
got=$r
}
# array of compund variables
typeset -C data=(
typeset -a samples
)
data.samples+=(
type1="greeting1"
timestamp1="now1"
command1="grrrr1"
)
data.samples+=(
type2="greeting2"
timestamp2="now2"
command2="grrrr2"
)
[[ $data == %(()) ]] || err_exit "unbalanced parenthesis with compound variable containing array of compound variables"
[[ $(typeset -p hello) == 'typeset -C -A hello=([foo]=bar)' ]] || err_exit 'typeset -A -C with intial assignment not working'
# this caused a core dump before ksh93t+
[[ $($SHELL -c 'foo=(x=3 y=4);function bar { typeset z=4;: $z;};bar;print ${!foo.@}') == 'foo.x foo.y' ]] 2> /dev/null || err_exit '${!foo.@} after function not working'
function foo
{
typeset tmp
read -C tmp
read -C tmp
}
(
typeset -A myarray3=(
)
)
typeset -C -a mica01
typeset -C more_content=(
some_stuff="hello"
)
expected=$'typeset -C -a mica01=([4]=(a_string=\'foo bar\';some_stuff=hello))'
[[ $(typeset -p mica01) == "$expected" ]] || err_exit 'appened to indexed array compound variable not working'
unset x
compound x=( integer x ; )
expected=$'(\n\ttypeset -l -i x=0\n)'
typeset -C -A hello19=(
[19]=(
one="xone 19"
two="xtwo 19"
)
[23]=(
one="xone 23"
two="xtwo 23"
)
)
expected="typeset -C -A hello19=([19]=(one='xone 19';two='xtwo 19') [23]=(one='xone 23';two='xtwo 23'))"
expected=$'(\n\tone=\'xone 19\'\n\ttwo=\'xtwo 19\'\n) (\n\tone=\'xone 23\'\n\ttwo=\'xtwo 23\'\n)'
function add_one
{
nameref left_op=$1
typeset -C info
info.hello="world"
nameref x=info
left_op+=x
}
nameref node1="foo1[1234]"
add_one "node1"
add_one "foo2[1234]"
[[ "${foo1[1234]}" == "${foo2[1234]}" ]] || err_exit "test failed\n$(diff -u <( print -r -- "${foo1[1234]}") <(print -r -- "${foo2[1234]}"))."
typeset -C tree
function f1
{
nameref tr=$1
typeset -A tr.subtree
typeset -C node
node.one="hello"
node.two="world"
# move local note into the array
}
expected=$'(\n\ttypeset -A subtree=(\n\t\t[a_node]=(\n\t\t\tone=hello\n\t\t\ttwo=world\n\t\t)\n\t)\n)'
[[ $tree == "$expected" ]] || err_exit 'move of compound local variable to global variable not working'
typeset -C -A array
expected='typeset -C -A array=([12]=(typeset -l -E amount=2.9))'
[[ $(typeset -p array) == "$expected" ]] || err_exit 'typeset with compound variable with compound variable array not working'
typeset -T foo_t=(
function diff
{
print 1.0
return 0
}
)
compound output=(
integer one=1
float end=.314
)
compound cpv1=( integer f=2 )
compound x=(
integer a=1
compound b=cpv1
)
unset x
compound x=(
compound -a nodes=(
[4]=( )
)
)
expected='typeset -C x=(typeset -C -a nodes=([4]=());)'
[[ $(typeset -p x) == "$expected" ]] || err_exit 'typeset -p with nested compound index array not working'
unset v
compound v=(
integer -A ar=(
)
)
expected='typeset -C v=(typeset -A -l -i ar=([aa]=4 [bb]=9);)'
[[ $(typeset -p v) == "$expected" ]] || err_exit 'attributes for associative arrays embedded in compound variables not working'
unset x
compound -a x
x[1]=( a=1 b=2 )
[[ $(print -v x[1]) == "${x[1]}" ]] || err_exit 'print -v x[1] not working for index array of compound variables'
unset x
z='typeset -a x=(hello (x=12;y=5) world)'
expected='typeset -C -A l=([4]=(typeset -a ar=(1 2 3);b=1))'
typeset -A -C l
[[ $(typeset -p l) == "$expected" ]] || err_exit 'read -C for associative array of compound variables not working'
unset x
compound x=( z="a=b c")
exp=$'typeset -C x=(z=a\\=\'b c\')'
got=$(typeset -p x)
x=(typeset -C -a y;float z=2)
got=$(print -C x)
expected='(typeset -C -a y;typeset -l -E z=2)'
compound vx=(
compound -a va=(
[3][17]=(
)
)
)
unset x
typeset -C -A x=( [0]=(a=1) [1]=(b=2) )
expected=$'(\n\t[0]=(\n\t\ta=1\n\t)\n\t[1]=(\n\t\tb=2\n\t)\n)'
compound -a x=( [0]=(a=1) [1]=(b=2) )
typeset -m "z=x[1]"
[[ $(typeset -p z 2>/dev/null) == 'typeset -C z=(b=2)' ]] || err_exit 'typeset -m not working with commpound -a variable'
unset x z
compound -A x=( [0]=(a=1) [1]=(b=2) )
typeset -m "z=x[1]"
[[ $(typeset -p z 2>/dev/null) == 'typeset -C z=(b=2)' ]] || err_exit 'typeset -m not working with commpound -a variable'
typeset -m "x[1]=x[0]"
typeset -m "x[0]=z"
exp='([0]=(b=2) [1]=(a=1))'
unset z r
z=(a b c)
r=(x=3 y=4)
typeset -m z[1]=r
exp='typeset -a z=(a (x=3;y=4) c)'
unset c
compound c
exp='typeset -C c=(typeset -C -a a;typeset -C -a b=( [2]=(bb=2;)[9]=(aa=1));)'
[[ $(typeset -p c) == "$exp" ]] || err_exit 'moving compound indexed array element to another index fails'
unset c
compound c
exp='typeset -C c=(typeset -C -a a;typeset -C -A b=( [2]=(bb=2;)[9]=(aa=1));)'
[[ $(typeset -p c) == "$exp" ]] || err_exit 'moving compound indexed array element to a compound associative array element fails'
zzz=(
foo=(
bar=4
)
)
typeset -A stats
(( ${#stats[@]} == 2 )) || err_exit "stats[1] should contain 2 element not ${#stats[@]}"
integer i=1
foo[i++]=(x=3 y=4)
# ${!x.} caused core dump in ks93u and earlier
compound -a a
a[4]=c
[[ ${a[4]} == $'(\n\tname=container1\n)' ]] || err_exit 'assignment of compound variable to compound array element not working'
unset c
compound c
compound -a c.board
for ((i=2; i < 4; i++))
done
exp=$'(\n\ttypeset -C -a board=(\n\t\t[1]=(\n\t\t\t[2]=(\n\t\t\t\tfoo=bar\n\t\t\t)\n\t\t\t[3]=(\n\t\t\t\tfoo=bar\n\t\t\t)\n\t\t)\n\t)\n)'
[[ "$(print -v c)" == "$exp" ]] || err_exit 'compound variable assignment to two dimensional array not working'
unset zz
zz=()
exp='typeset -C zz=([2]=def;foo=abc)'
[[ $(typeset -p zz) == "$exp" ]] || err_exit 'expansion of compound variables with non-identifiers not working'
(
typeset -i zz.[3]=123
exec 2>& 3-
exp='typeset -C zz=([2]=def;typeset -i [3]=123;foo=abc)'
[[ $(typeset -p zz) == "$exp" ]] || err_exit 'expansion of compound variables with non-identifiers not working in subshells'
) 3>&2 2> /dev/null || err_exit 'syntax errors expansion of compound variables with non-identifiers'
unset xx
xx=()