xref: /titanic_50/usr/src/lib/libshell/common/tests/sun_solaris_vartree001.sh (revision 3e14f97f673e8a630f076077de35afdd43dc1587)
1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24#
25
26#
27# variable tree test #001
28# Propose of this test is whether ksh93 crashes or not - ast-ksh.2008-05-14
29# crashes like this when running this test:
30#
31# program terminated by signal ILL (illegal opcode)
32# 0xffffffffffffffff:     <bad address 0xffffffffffffffff>
33# Current function is nv_diropen
34#   123                   dp->hp = (Namval_t*)dtprev(dp->root,&fake);
35# (dbx) where
36#   [1] 0x100381e80(0x100381e80, 0xffffffff7fffe690, 0x10, 0x61, 0x0, 0x100381ec9), at 0x100381e80
37# =>[2] nv_diropen(np = (nil), name = 0x100381ebc "mysrcdata"), line 123 in "nvtree.c"
38#   [3] walk_tree(np = 0x1003809e0, dlete = 524289), line 743 in "nvtree.c"
39#   [4] put_tree(np = 0x1003809e0, val = (nil), flags = 524289, fp = 0x100381db0), line 814 in "nvtree.c"
40#   [5] nv_putv(np = 0x1003809e0, value = (nil), flags = 524289, nfp = 0x100381db0), line 141 in "nvdisc.c"
41#   [6] _nv_unset(np = 0x1003809e0, flags = 524289), line 1976 in "name.c"
42#   [7] table_unset(shp = 0x10033e900, root = 0x100380900, flags = 524289, oroot = 0x100360980), line 1902 in "name.c"
43#   [8] sh_unscope(shp = 0x10033e900), line 2711 in "name.c"
44#   [9] sh_funscope(argn = 1, argv = 0x10035e680, fun = (nil), arg = 0xffffffff7ffff118, execflg = 4), line 2470 in "xec.c"
45#   [10] sh_funct(np = 0x100380860, argn = 1, argv = 0x10035e680, envlist = (nil), execflg = 4), line 2528 in "xec.c"
46#   [11] sh_exec(t = 0x10035e620, flags = 4), line 1032 in "xec.c"
47#   [12] exfile(shp = 0x10033e900, iop = 0x100379a20, fno = 10), line 589 in "main.c"
48#   [13] sh_main(ac = 2, av = 0xffffffff7ffffa08, userinit = (nil)), line 364 in "main.c"
49#   [14] main(argc = 2, argv = 0xffffffff7ffffa08), line 46 in "pmain.c"
50#
51
52# test setup
53function err_exit
54{
55	print -u2 -n "\t"
56	print -u2 -r ${Command}[$1]: "${@:2}"
57	(( Errors < 127 && Errors++ ))
58}
59alias err_exit='err_exit $LINENO'
60
61# the test cannot use "nounset"
62Command=${0##*/}
63integer Errors=0
64
65
66function build_tree
67{
68#set -o errexit -o xtrace
69	typeset index
70	typeset s
71	typeset i
72	typeset dummy
73	typeset a b c d e f
74
75	nameref dest_tree="$1" # destination tree
76	nameref srcdata="$2"   # source data
77	typeset tree_mode="$3" # mode to define the type of leads
78
79	typeset -A dest_tree.l1
80
81	for index in "${!srcdata.hashnodes[@]}" ; do
82		nameref node=srcdata.hashnodes["${index}"]
83
84		for i in "${node.xlfd[@]}" ; do
85			IFS='-' read dummy a b c d e f <<<"$i"
86
87			if [[ "$a" == "" ]] ; then
88				a="$dummy"
89			fi
90
91			[[ "$a" == "" ]] && a='-'
92			[[ "$b" == "" ]] && b='-'
93			[[ "$c" == "" ]] && c='-'
94
95			if [[ "${dest_tree.l1["$a"]}" == "" ]] ; then
96			#if ! (unset dest_tree.l1["$a"]) ; then
97				typeset -A dest_tree.l1["$a"].l2
98			fi
99
100			if [[ "${dest_tree.l1["$a"].l2["$b"]}" == "" ]] ; then
101			#if ! (unset dest_tree.l1["$a"].l2["$b"]) ; then
102				typeset -A dest_tree.l1["$a"].l2["$b"].l3
103			fi
104
105			if [[ "${!dest_tree.l1["$a"].l2["$b"].l3["$c"].entries[*]}" == "" ]] ; then
106				typeset -A dest_tree.l1["$a"].l2["$b"].l3["$c"].entries
107			fi
108
109			#dest_tree.l1["$a"].l2["$b"].l3["$c"].entries+=( "$index" )
110			typeset new_index
111			if [[ "${tree_mode}" == "leaf_name" ]] ; then
112				new_index=$(( ${#dest_tree.l1["$a"].l2["$b"].l3["$c"].entries[@]}+1 ))
113			else
114				new_index="${node.name}"
115
116				# skip if the leaf node already exists
117				if [[ "${dest_tree.l1["$a"].l2["$b"].l3["$c"].entries[${new_index}]}" != "" ]] ; then
118					continue
119				fi
120			fi
121
122			add_tree_leaf dest_tree.l1["$a"].l2["$b"].l3["$c"].entries[${new_index}] "${index}" "${tree_mode}"
123		done
124	done
125
126	return 0
127}
128
129function add_tree_leaf
130{
131	nameref tree_leafnode="$1"
132	nameref data_node=srcdata.hashnodes["$2"]
133	typeset add_mode="$3"
134
135	case "${add_mode}" in
136		"leaf_name")
137			tree_leafnode="${data_node.name}"
138			return 0
139			;;
140		"leaf_compound")
141			tree_leafnode=(
142				typeset name="${data_node.name}"
143				typeset -a filenames=( "${data_node.filenames[@]}" )
144				typeset -a comments=( "${data_node.comments[@]}" )
145				typeset -a xlfd=( "${data_node.xlfd[@]}" )
146			)
147			return 0
148			;;
149		*)
150			print -u2 -f "ERROR: Unknown mode %s in add_tree_leaf\n" "${add_mode}"
151			return 1
152			;;
153	esac
154
155	# not reached
156	return 1
157}
158
159function main
160{
161	typeset mysrcdata=(
162		typeset -A hashnodes=(
163			[abcd]=(
164				name='abcd'
165				typeset -a xlfd=(
166					'-urw-itc zapfchancery-medium-i-normal--0-0-0-0-p-0-iso8859-1'
167					'-urw-itc zapfdingbats-medium-r-normal--0-0-0-0-p-0-adobe-fontspecific'
168					'-urw-itc zapfdingbats-medium-r-normal--0-0-0-0-p-0-sun-fontspecific'
169				)
170				typeset -a comments=(
171					'comment 1'
172					'comment 2'
173					'comment 3'
174				)
175				typeset -a filenames=(
176					'/home/foo/abcd_1'
177					'/home/foo/abcd_2'
178					'/home/foo/abcd_3'
179				)
180			)
181		)
182	)
183
184	mytree=()
185	build_tree mytree mysrcdata leaf_compound
186#	(( $(print -r -- "$mytree" | wc -l) > 10 )) || err_exit "Compound tree too small."
187}
188
189main
190
191# tests done
192exit $((Errors))
193