xref: /titanic_50/usr/src/lib/libshell/common/tests/vartree1.sh (revision 3e14f97f673e8a630f076077de35afdd43dc1587)
17c2fbfb3SApril Chin########################################################################
27c2fbfb3SApril Chin#                                                                      #
37c2fbfb3SApril Chin#               This software is part of the ast package               #
4*3e14f97fSRoger A. Faulkner#          Copyright (c) 1982-2010 AT&T Intellectual Property          #
57c2fbfb3SApril Chin#                      and is licensed under the                       #
67c2fbfb3SApril Chin#                  Common Public License, Version 1.0                  #
77c2fbfb3SApril Chin#                    by AT&T Intellectual Property                     #
87c2fbfb3SApril Chin#                                                                      #
97c2fbfb3SApril Chin#                A copy of the License is available at                 #
107c2fbfb3SApril Chin#            http://www.opensource.org/licenses/cpl1.0.txt             #
117c2fbfb3SApril Chin#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
127c2fbfb3SApril Chin#                                                                      #
137c2fbfb3SApril Chin#              Information and Software Systems Research               #
147c2fbfb3SApril Chin#                            AT&T Research                             #
157c2fbfb3SApril Chin#                           Florham Park NJ                            #
167c2fbfb3SApril Chin#                                                                      #
177c2fbfb3SApril Chin#                  David Korn <dgk@research.att.com>                   #
187c2fbfb3SApril Chin#                                                                      #
197c2fbfb3SApril Chin########################################################################
207c2fbfb3SApril Chin#
217c2fbfb3SApril Chin# variable tree test #001
227c2fbfb3SApril Chin# Propose of this test is whether ksh93 handles global variable trees
237c2fbfb3SApril Chin# and function-local variable trees the same way, including "nameref"
247c2fbfb3SApril Chin# and "unset" handling.
257c2fbfb3SApril Chin#
267c2fbfb3SApril Chin
277c2fbfb3SApril Chinfunction err_exit
287c2fbfb3SApril Chin{
297c2fbfb3SApril Chin	print -u2 -n "\t"
307c2fbfb3SApril Chin	print -u2 -r ${Command}[$1]: "${@:2}"
317c2fbfb3SApril Chin	(( Errors+=1 ))
327c2fbfb3SApril Chin}
337c2fbfb3SApril Chin
347c2fbfb3SApril Chinalias err_exit='err_exit $LINENO'
357c2fbfb3SApril Chin
367c2fbfb3SApril Chinfunction build_tree
377c2fbfb3SApril Chin{
387c2fbfb3SApril Chin#set -o errexit -o xtrace
397c2fbfb3SApril Chin	typeset index
407c2fbfb3SApril Chin	typeset s
417c2fbfb3SApril Chin	typeset i
427c2fbfb3SApril Chin	typeset dummy
437c2fbfb3SApril Chin	typeset a b c d e f
447c2fbfb3SApril Chin
457c2fbfb3SApril Chin	nameref dest_tree="$1" # destination tree
467c2fbfb3SApril Chin	nameref srcdata="$2"   # source data
477c2fbfb3SApril Chin	typeset tree_mode="$3" # mode to define the type of leads
487c2fbfb3SApril Chin
497c2fbfb3SApril Chin	typeset -A dest_tree.l1
507c2fbfb3SApril Chin
517c2fbfb3SApril Chin	for index in "${!srcdata.hashnodes[@]}" ; do
527c2fbfb3SApril Chin		nameref node=srcdata.hashnodes["${index}"]
537c2fbfb3SApril Chin
547c2fbfb3SApril Chin		for i in "${node.xlfd[@]}" ; do
557c2fbfb3SApril Chin			IFS='-' read dummy a b c d e f <<<"$i"
567c2fbfb3SApril Chin
577c2fbfb3SApril Chin			if [[ "$a" == "" ]] ; then
587c2fbfb3SApril Chin				a="$dummy"
597c2fbfb3SApril Chin			fi
607c2fbfb3SApril Chin
617c2fbfb3SApril Chin			[[ "$a" == "" ]] && a='-'
627c2fbfb3SApril Chin			[[ "$b" == "" ]] && b='-'
637c2fbfb3SApril Chin			[[ "$c" == "" ]] && c='-'
647c2fbfb3SApril Chin
657c2fbfb3SApril Chin			if [[ "${dest_tree.l1["$a"]}" == "" ]] ; then
667c2fbfb3SApril Chin			#if ! (unset dest_tree.l1["$a"]) ; then
677c2fbfb3SApril Chin				typeset -A dest_tree.l1["$a"].l2
687c2fbfb3SApril Chin			fi
697c2fbfb3SApril Chin
707c2fbfb3SApril Chin			if [[ "${dest_tree.l1["$a"].l2["$b"]}" == "" ]] ; then
717c2fbfb3SApril Chin			#if ! (unset dest_tree.l1["$a"].l2["$b"]) ; then
727c2fbfb3SApril Chin				typeset -A dest_tree.l1["$a"].l2["$b"].l3
737c2fbfb3SApril Chin			fi
747c2fbfb3SApril Chin
757c2fbfb3SApril Chin			if [[ "${!dest_tree.l1["$a"].l2["$b"].l3["$c"].entries[*]}" == "" ]] ; then
767c2fbfb3SApril Chin				typeset -A dest_tree.l1["$a"].l2["$b"].l3["$c"].entries
777c2fbfb3SApril Chin			fi
787c2fbfb3SApril Chin
797c2fbfb3SApril Chin			#dest_tree.l1["$a"].l2["$b"].l3["$c"].entries+=( "$index" )
807c2fbfb3SApril Chin			typeset new_index
817c2fbfb3SApril Chin			if [[ "${tree_mode}" == "leaf_name" ]] ; then
827c2fbfb3SApril Chin				new_index=$(( ${#dest_tree.l1["$a"].l2["$b"].l3["$c"].entries[@]}+1 ))
837c2fbfb3SApril Chin			else
847c2fbfb3SApril Chin				new_index="${node.name}"
857c2fbfb3SApril Chin
867c2fbfb3SApril Chin				# skip if the leaf node already exists
877c2fbfb3SApril Chin				if [[ "${dest_tree.l1["$a"].l2["$b"].l3["$c"].entries[${new_index}]}" != "" ]] ; then
887c2fbfb3SApril Chin					continue
897c2fbfb3SApril Chin				fi
907c2fbfb3SApril Chin			fi
917c2fbfb3SApril Chin
927c2fbfb3SApril Chin			add_tree_leaf dest_tree.l1["$a"].l2["$b"].l3["$c"].entries[${new_index}] "${index}" "${tree_mode}"
937c2fbfb3SApril Chin		done
947c2fbfb3SApril Chin	done
957c2fbfb3SApril Chin
967c2fbfb3SApril Chin	return 0
977c2fbfb3SApril Chin}
987c2fbfb3SApril Chin
997c2fbfb3SApril Chinfunction add_tree_leaf
1007c2fbfb3SApril Chin{
1017c2fbfb3SApril Chin	nameref tree_leafnode="$1"
1027c2fbfb3SApril Chin	nameref data_node=srcdata.hashnodes["$2"]
1037c2fbfb3SApril Chin	typeset add_mode="$3"
1047c2fbfb3SApril Chin
1057c2fbfb3SApril Chin	case "${add_mode}" in
1067c2fbfb3SApril Chin		"leaf_name")
1077c2fbfb3SApril Chin			tree_leafnode="${data_node.name}"
1087c2fbfb3SApril Chin			return 0
1097c2fbfb3SApril Chin			;;
1107c2fbfb3SApril Chin		"leaf_compound")
1117c2fbfb3SApril Chin			tree_leafnode=(
1127c2fbfb3SApril Chin				typeset name="${data_node.name}"
1137c2fbfb3SApril Chin				typeset -a filenames=( "${data_node.filenames[@]}" )
1147c2fbfb3SApril Chin				typeset -a comments=( "${data_node.comments[@]}" )
1157c2fbfb3SApril Chin				typeset -a xlfd=( "${data_node.xlfd[@]}" )
1167c2fbfb3SApril Chin			)
1177c2fbfb3SApril Chin			return 0
1187c2fbfb3SApril Chin			;;
1197c2fbfb3SApril Chin		*)
1207c2fbfb3SApril Chin			print -u2 -f "ERROR: Unknown mode %s in add_tree_leaf\n" "${add_mode}"
1217c2fbfb3SApril Chin			return 1
1227c2fbfb3SApril Chin			;;
1237c2fbfb3SApril Chin	esac
1247c2fbfb3SApril Chin
1257c2fbfb3SApril Chin	# not reached
1267c2fbfb3SApril Chin	return 1
1277c2fbfb3SApril Chin}
1287c2fbfb3SApril Chin
1297c2fbfb3SApril Chin# "mysrcdata_local" and "mysrcdata_global" must be identical
1307c2fbfb3SApril Chintypeset mysrcdata_global=(
1317c2fbfb3SApril Chin	typeset -A hashnodes=(
1327c2fbfb3SApril Chin		[abcd]=(
1337c2fbfb3SApril Chin			name='abcd'
1347c2fbfb3SApril Chin			typeset -a xlfd=(
1357c2fbfb3SApril Chin				'-urw-itc zapfchancery-medium-i-normal--0-0-0-0-p-0-iso8859-1'
1367c2fbfb3SApril Chin				'-urw-itc zapfdingbats-medium-r-normal--0-0-0-0-p-0-adobe-fontspecific'
1377c2fbfb3SApril Chin				'-urw-itc zapfdingbats-medium-r-normal--0-0-0-0-p-0-sun-fontspecific'
1387c2fbfb3SApril Chin			)
1397c2fbfb3SApril Chin			typeset -a comments=(
1407c2fbfb3SApril Chin				'comment 1'
1417c2fbfb3SApril Chin				'comment 2'
1427c2fbfb3SApril Chin				'comment 3'
1437c2fbfb3SApril Chin			)
1447c2fbfb3SApril Chin			typeset -a filenames=(
1457c2fbfb3SApril Chin				'/home/foo/abcd_1'
1467c2fbfb3SApril Chin				'/home/foo/abcd_2'
1477c2fbfb3SApril Chin				'/home/foo/abcd_3'
1487c2fbfb3SApril Chin			)
1497c2fbfb3SApril Chin		)
1507c2fbfb3SApril Chin	)
1517c2fbfb3SApril Chin)
1527c2fbfb3SApril Chin
1537c2fbfb3SApril Chinmytree_global=()
1547c2fbfb3SApril Chin
1557c2fbfb3SApril Chinfunction main
1567c2fbfb3SApril Chin{
1577c2fbfb3SApril Chin	# "mysrcdata_local" and "mysrcdata_global" must be identical
1587c2fbfb3SApril Chin	typeset mysrcdata_local=(
1597c2fbfb3SApril Chin		typeset -A hashnodes=(
1607c2fbfb3SApril Chin			[abcd]=(
1617c2fbfb3SApril Chin				name='abcd'
1627c2fbfb3SApril Chin				typeset -a xlfd=(
1637c2fbfb3SApril Chin					'-urw-itc zapfchancery-medium-i-normal--0-0-0-0-p-0-iso8859-1'
1647c2fbfb3SApril Chin					'-urw-itc zapfdingbats-medium-r-normal--0-0-0-0-p-0-adobe-fontspecific'
1657c2fbfb3SApril Chin					'-urw-itc zapfdingbats-medium-r-normal--0-0-0-0-p-0-sun-fontspecific'
1667c2fbfb3SApril Chin				)
1677c2fbfb3SApril Chin				typeset -a comments=(
1687c2fbfb3SApril Chin					'comment 1'
1697c2fbfb3SApril Chin					'comment 2'
1707c2fbfb3SApril Chin					'comment 3'
1717c2fbfb3SApril Chin				)
1727c2fbfb3SApril Chin				typeset -a filenames=(
1737c2fbfb3SApril Chin					'/home/foo/abcd_1'
1747c2fbfb3SApril Chin					'/home/foo/abcd_2'
1757c2fbfb3SApril Chin					'/home/foo/abcd_3'
1767c2fbfb3SApril Chin				)
1777c2fbfb3SApril Chin			)
1787c2fbfb3SApril Chin		)
1797c2fbfb3SApril Chin	)
1807c2fbfb3SApril Chin
1817c2fbfb3SApril Chin	# build tree using global tree variables
1827c2fbfb3SApril Chin	build_tree mytree_global mysrcdata_global leaf_compound || \
1837c2fbfb3SApril Chin		err_exit 'build_tree mytree_global mysrcdata_global leaf_compound returned an error'
1847c2fbfb3SApril Chin
18534f9b3eeSRoland Mainz	(( $(print -r -- "${mytree_global}" | wc -l) > 10 )) || err_exit "compound tree 'mytree_global' too small"
1867c2fbfb3SApril Chin
1877c2fbfb3SApril Chin	# build tree using local tree variables
1887c2fbfb3SApril Chin	mytree_local=()
1897c2fbfb3SApril Chin	build_tree mytree_local mysrcdata_local leaf_compound || \
1907c2fbfb3SApril Chin		err_exit 'build_tree mytree_local mysrcdata_local leaf_compound returned an error'
1917c2fbfb3SApril Chin
19234f9b3eeSRoland Mainz	(( $(print -r -- "${mytree_local}" | wc -l) > 10 )) || err_exit "compound tree 'mytree_local' too small"
1937c2fbfb3SApril Chin
1947c2fbfb3SApril Chin	# Compare trees
1957c2fbfb3SApril Chin	if [[ "${mytree_global}" != "${mytree_local}" ]] ; then
19634f9b3eeSRoland Mainz		err_exit "compound trees 'mytree_local' and 'mytree_global' not identical"
1977c2fbfb3SApril Chin	fi
1987c2fbfb3SApril Chin
1997c2fbfb3SApril Chin	unset 'mytree_global.l1[urw].l2[itc zapfdingbats].l3[medium].entries[abcd].filenames[0]' ||
20034f9b3eeSRoland Mainz		err_exit "variable 'mytree_global.l1[urw].l2[itc zapfdingbats].l3[medium].entries[abcd].filenames[0]' not found"
2017c2fbfb3SApril Chin
2027c2fbfb3SApril Chin	[[ "${mytree_global}" != "${mytree_local}" ]] || err_exit "mytree_global and mytree_local should differ"
2037c2fbfb3SApril Chin
2047c2fbfb3SApril Chin	unset 'mytree_local.l1[urw].l2[itc zapfdingbats].l3[medium].entries[abcd].filenames[0]' ||
20534f9b3eeSRoland Mainz		err_exit "variable 'mytree_local.l1[urw].l2[itc zapfdingbats].l3[medium].entries[abcd].filenames[0]' not found"
2067c2fbfb3SApril Chin
2077c2fbfb3SApril Chin	# Compare trees (after "unset")
2087c2fbfb3SApril Chin	if [[ "${mytree_global}" != "${mytree_local}" ]] ; then
20934f9b3eeSRoland Mainz		err_exit "compound trees 'mytree_local' and 'mytree_global' not identical after unset"
2107c2fbfb3SApril Chin	fi
2117c2fbfb3SApril Chin}
2127c2fbfb3SApril Chin
2137c2fbfb3SApril Chinmain
2147c2fbfb3SApril Chinexit $((Errors))
215