1 ########################################################################
3 # This software is part of the ast package #
4 # Copyright (c) 1982-2010 AT&T Intellectual Property #
5 # and is licensed under the #
6 # Common Public License, Version 1.0 #
7 # by AT&T Intellectual Property #
9 # A copy of the License is available at #
10 # http://www.opensource.org/licenses/cpl1.0.txt #
11 # (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) #
13 # Information and Software Systems Research #
17 # David Korn <dgk@research.att.com> #
19 ########################################################################
21 # variable tree test #001
22 # Propose of this test is whether ksh93 handles global variable trees
23 # and function-local variable trees the same way, including "nameref"
24 # and "unset" handling.
30 print
-u2 -r ${Command}[$1]: "${@:2}"
34 alias err_exit
='err_exit $LINENO'
38 #set -o errexit -o xtrace
45 nameref dest_tree
="$1" # destination tree
46 nameref srcdata
="$2" # source data
47 typeset tree_mode
="$3" # mode to define the type of leads
49 typeset
-A dest_tree.l1
51 for index
in "${!srcdata.hashnodes[@]}" ; do
52 nameref node
=srcdata.hashnodes
["${index}"]
54 for i
in "${node.xlfd[@]}" ; do
55 IFS
='-' read dummy a b c d e f
<<<"$i"
57 if [[ "$a" == "" ]] ; then
61 [[ "$a" == "" ]] && a
='-'
62 [[ "$b" == "" ]] && b
='-'
63 [[ "$c" == "" ]] && c
='-'
65 if [[ "${dest_tree.l1["$a"]}" == "" ]] ; then
66 #if ! (unset dest_tree.l1["$a"]) ; then
67 typeset
-A dest_tree.l1
["$a"].l2
70 if [[ "${dest_tree.l1["$a"].l2["$b"]}" == "" ]] ; then
71 #if ! (unset dest_tree.l1["$a"].l2["$b"]) ; then
72 typeset
-A dest_tree.l1
["$a"].l2
["$b"].l3
75 if [[ "${!dest_tree.l1["$a"].l2["$b"].l3["$c"].entries[*]}" == "" ]] ; then
76 typeset
-A dest_tree.l1
["$a"].l2
["$b"].l3
["$c"].entries
79 #dest_tree.l1["$a"].l2["$b"].l3["$c"].entries+=( "$index" )
81 if [[ "${tree_mode}" == "leaf_name" ]] ; then
82 new_index
=$
(( ${#dest_tree.l1["$a"].l2["$b"].l3["$c"].entries[@]}+1 ))
84 new_index
="${node.name}"
86 # skip if the leaf node already exists
87 if [[ "${dest_tree.l1["$a"].l2["$b"].l3["$c"].entries[${new_index}]}" != "" ]] ; then
92 add_tree_leaf dest_tree.l1
["$a"].l2
["$b"].l3
["$c"].entries
[${new_index}] "${index}" "${tree_mode}"
99 function add_tree_leaf
101 nameref tree_leafnode="$1"
102 nameref data_node=srcdata.hashnodes["$2"]
103 typeset add_mode="$3"
105 case "${add_mode}" in
107 tree_leafnode="${data_node.name}"
112 typeset name="${data_node.name}"
113 typeset -a filenames=( "${data_node.filenames[@]}" )
114 typeset -a comments=( "${data_node.comments[@]}" )
115 typeset -a xlfd=( "${data_node.xlfd[@]}" )
120 print -u2 -f "ERROR
: Unknown mode
%s
in add_tree_leaf
\n" "${add_mode}"
129 # "mysrcdata_local
" and "mysrcdata_global
" must be identical
130 typeset mysrcdata_global=(
131 typeset -A hashnodes=(
135 '-urw-itc zapfchancery-medium-i-normal--0-0-0-0-p-0-iso8859-1'
136 '-urw-itc zapfdingbats-medium-r-normal--0-0-0-0-p-0-adobe-fontspecific'
137 '-urw-itc zapfdingbats-medium-r-normal--0-0-0-0-p-0-sun-fontspecific'
139 typeset -a comments=(
144 typeset -a filenames=(
157 # "mysrcdata_local
" and "mysrcdata_global
" must be identical
158 typeset mysrcdata_local=(
159 typeset -A hashnodes=(
163 '-urw-itc zapfchancery-medium-i-normal--0-0-0-0-p-0-iso8859-1'
164 '-urw-itc zapfdingbats-medium-r-normal--0-0-0-0-p-0-adobe-fontspecific'
165 '-urw-itc zapfdingbats-medium-r-normal--0-0-0-0-p-0-sun-fontspecific'
167 typeset -a comments=(
172 typeset -a filenames=(
181 # build tree using global tree variables
182 build_tree mytree_global mysrcdata_global leaf_compound || \
183 err_exit 'build_tree mytree_global mysrcdata_global leaf_compound returned an error'
185 (( $(print -r -- "${mytree_global}" | wc -l) > 10 )) || err_exit "compound tree
'mytree_global' too small
"
187 # build tree using local tree variables
189 build_tree mytree_local mysrcdata_local leaf_compound || \
190 err_exit 'build_tree mytree_local mysrcdata_local leaf_compound returned an error'
192 (( $(print -r -- "${mytree_local}" | wc -l) > 10 )) || err_exit "compound tree
'mytree_local' too small
"
195 if [[ "${mytree_global}" != "${mytree_local}" ]] ; then
196 err_exit "compound trees
'mytree_local' and
'mytree_global' not identical
"
199 unset 'mytree_global.l1[urw].l2[itc zapfdingbats].l3[medium].entries[abcd].filenames[0]' ||
200 err_exit "variable
'mytree_global.l1[urw].l2[itc zapfdingbats].l3[medium].entries[abcd].filenames[0]' not found
"
202 [[ "${mytree_global}" != "${mytree_local}" ]] || err_exit "mytree_global and mytree_local should differ
"
204 unset 'mytree_local.l1[urw].l2[itc zapfdingbats].l3[medium].entries[abcd].filenames[0]' ||
205 err_exit "variable
'mytree_local.l1[urw].l2[itc zapfdingbats].l3[medium].entries[abcd].filenames[0]' not found
"
207 # Compare trees (after "unset")
208 if [[ "${mytree_global}" != "${mytree_local}" ]] ; then
209 err_exit "compound trees
'mytree_local' and
'mytree_global' not identical after
unset"