xref: /titanic_50/usr/src/lib/libshell/common/tests/attributes.sh (revision f8919bdadda3ebb97bd55cc14a16e0271ed57615)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#           Copyright (c) 1982-2007 AT&T Knowledge Ventures            #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                      by AT&T Knowledge Ventures                      #
8#                                                                      #
9#                A copy of the License is available at                 #
10#            http://www.opensource.org/licenses/cpl1.0.txt             #
11#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12#                                                                      #
13#              Information and Software Systems Research               #
14#                            AT&T Research                             #
15#                           Florham Park NJ                            #
16#                                                                      #
17#                  David Korn <dgk@research.att.com>                   #
18#                                                                      #
19########################################################################
20function err_exit
21{
22	print -u2 -n "\t"
23	print -u2 -r ${Command}[$1]: "${@:2}"
24	let Errors+=1
25}
26alias err_exit='err_exit $LINENO'
27
28Command=${0##*/}
29integer Errors=0
30r=readonly u=Uppercase l=Lowercase i=22 i8=10 L=abc L5=def uL5=abcdef xi=20
31x=export t=tagged H=hostname LZ5=026 RZ5=026 Z5=123 lR5=ABcdef R5=def n=l
32for option in u l i i8 L L5 LZ5 RZ5 Z5 r x H t R5 uL5 lR5 xi n
33do	typeset -$option $option
34done
35(r=newval) 2> /dev/null && err_exit readonly attribute fails
36i=i+5
37if	((i != 27))
38then	err_exit integer attributes fails
39fi
40if	[[ $i8 != 8#12 ]]
41then	err_exit integer base 8 fails
42fi
43if	[[ $u != UPPERCASE ]]
44then	err_exit uppercase fails
45fi
46if	[[ $l != lowercase ]]
47then	err_exit lowercase fails
48fi
49if	[[ $n != lowercase ]]
50then	err_exit reference variables fail
51fi
52if	[[ t=tagged != $(typeset -t) ]]
53then	err_exit tagged fails
54fi
55if	[[ t != $(typeset +t) ]]
56then	err_exit tagged fails
57fi
58if	[[ $Z5 != 00123 ]]
59then	err_exit zerofill fails
60fi
61if	[[ $RZ5 != 00026 ]]
62then	err_exit right zerofill fails
63fi
64L=12345
65if	[[ $L != 123 ]]
66then	err_exit leftjust fails
67fi
68if	[[ $L5 != "def  " ]]
69then	err_exit leftjust fails
70fi
71if	[[ $uL5 != ABCDE ]]
72then	err_exit leftjust uppercase fails
73fi
74if	[[ $lR5 != bcdef ]]
75then	err_exit rightjust fails
76fi
77if	[[ $R5 != "  def" ]]
78then	err_exit rightjust fails
79fi
80if	[[ $($SHELL -c 'echo $x') != export ]]
81then	err_exit export fails
82fi
83if	[[ $($SHELL -c 'xi=xi+4;echo $xi') != 24 ]]
84then	err_exit export attributes fails
85fi
86x=$(foo=abc $SHELL <<!
87	foo=bar
88	$SHELL -c  'print \$foo'
89!
90)
91if	[[ $x != bar ]]
92then	err_exit 'environment variables require re-export'
93fi
94(typeset + ) > /dev/null 2>&1 || err_exit 'typeset + not working'
95(typeset -L-5 buf="A" 2>/dev/null)
96if [[ $? == 0 ]]
97then	err_exit 'typeset allows negative field for left/right adjust'
98fi
99a=b
100readonly $a=foo
101if	[[ $b != foo ]]
102then	err_exit 'readonly $a=b not working'
103fi
104if	[[ $(export | grep '^PATH=') != PATH=* ]]
105then	err_exit 'export not working'
106fi
107picture=(
108	bitmap=/fruit
109	size=(typeset -E x=2.5)
110)
111string="$(print $picture)"
112if [[ "${string}" != *'size=( typeset -E'* ]]
113then	err_exit 'print of compound exponential variable not working'
114fi
115sz=(typeset -E y=2.2)
116string="$(print $sz)"
117if [[ "${sz}" == *'typeset -E -F'* ]]
118then 	err_exit 'print of exponential shows both -E and -F attributes'
119fi
120print 'typeset -i m=48/4+1;print -- $m' > /tmp/ksh$$
121chmod +x /tmp/ksh$$
122typeset -Z2 m
123if	[[ $(/tmp/ksh$$) != 13 ]]
124then	err_exit 'attributes not cleared for script execution'
125fi
126typeset -Z  LAST=00
127unset -f foo
128function foo
129{
130        if [[ $1 ]]
131        then    LAST=$1
132        else    ((LAST++))
133        fi
134}
135foo 1
136if	(( ${#LAST} != 2 ))
137then	err_exit 'LAST!=2'
138fi
139foo
140if	(( ${#LAST} != 2 ))
141then	err_exit 'LAST!=2'
142fi
143rm -rf /tmp/ksh$$
144set -a
145unset foo
146foo=bar
147if	[[ $(export | grep ^foo=) != 'foo=bar' ]]
148then	err_exit 'all export not working'
149fi
150unset foo
151read foo <<!
152bar
153!
154if	[[ $(export | grep ^foo=) != 'foo=bar' ]]
155then	err_exit 'all export not working with read'
156fi
157if	[[ $(typeset | grep PS2) == PS2 ]]
158then	err_exit 'typeset without arguments outputs names without attributes'
159fi
160unset a z q x
161w1=hello
162w2=world
163t1="$w1 $w2"
164if	(( 'a' == 97 ))
165then	b1=aGVsbG8gd29ybGQ=
166	b2=aGVsbG8gd29ybGRoZWxsbyB3b3JsZA==
167else	b1=iIWTk5ZAppaZk4Q=
168	b2=iIWTk5ZAppaZk4SIhZOTlkCmlpmThA==
169fi
170z=$b1
171typeset -b x=$b1
172[[ $x == "$z" ]] || print -u2 'binary variable not expanding correctly'
173[[  $(printf "%B" x) == $t1 ]] || err_exit 'typeset -b not working'
174typeset -b -Z5 a=$b1
175[[  $(printf "%B" a) == $w1 ]] || err_exit 'typeset -b -Z5 not working'
176typeset -b q=$x$x
177[[ $q == $b2 ]] || err_exit 'typeset -b not working with concatination'
178[[  $(printf "%B" q) == $t1$t1 ]] || err_exit 'typeset -b concatination not working'
179x+=$b1
180[[ $x == $b2 ]] || err_exit 'typeset -b not working with append'
181[[  $(printf "%B" x) == $t1$t1 ]] || err_exit 'typeset -b append not working'
182typeset -b -Z20 z=$b1
183(( $(printf "%B" z | wc -c) == 20 )) || err_exit 'typeset -b -Z20 not storing 20 bytes'
184{
185	typeset -b v1 v2
186	read -N11 v1
187	read -N22 v2
188} << !
189hello worldhello worldhello world
190!
191[[ $v1 == "$b1" ]] || err_exit "v1=$v1 should be $b1"
192[[ $v2 == "$x" ]] || err_exit "v1=$v2 should be $x"
193[[ $(env '!=1' $SHELL -c 'echo ok' 2>/dev/null) == ok ]] || err_exit 'malformed environment terminates shell'
194unset var
195typeset -b var
196printf '12%Z34' | read -r -N 5 var
197[[ $var == MTIAMzQ= ]] || err_exit 'binary files with zeros not working'
198unset var
199if	command typeset -usi var=0xfffff 2> /dev/null
200then	(( $var == 0xffff )) || err_exit 'unsigned short integers not working'
201else	err_exit 'typeset -usi cannot be used for unsigned short'
202fi
203[[ $($SHELL -c 'unset foo;typeset -Z2 foo; print ${foo:-3}' 2> /dev/null) == 3 ]]  || err_exit  '${foo:-3} not 3 when typeset -Z2 field undefined'
204[[ $($SHELL -c 'unset foo;typeset -Z2 foo; print ${foo:=3}' 2> /dev/null) == 03 ]]  || err_exit  '${foo:=-3} not 3 when typeset -Z2 foo undefined'
205unset foo bar
206unset -f fun
207function fun
208{
209	export foo=hello
210	typeset -x  bar=world
211	[[ $foo == hello ]] || err_exit 'export scoping problem in function'
212}
213fun
214[[ $(export | grep foo) == 'foo=hello' ]] || err_exit 'export not working in functions'
215[[ $(export | grep bar) ]] && err_exit 'typeset -x not local'
216exit	$((Errors))
217