xref: /titanic_51/usr/src/lib/libshell/common/tests/attributes.sh (revision f657cd55755084dade71fb0c1d9f51994c226a70)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2008 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
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
126print 'print VAR=$VAR' > /tmp/ksh$$
127typeset -L70 VAR=var
128/tmp/ksh$$ > /tmp/ksh$$.1
129[[ $(< /tmp/ksh$$.1) == VAR= ]] || err_exit 'typeset -L should not be inherited'
130typeset -Z  LAST=00
131unset -f foo
132function foo
133{
134        if [[ $1 ]]
135        then    LAST=$1
136        else    ((LAST++))
137        fi
138}
139foo 1
140if	(( ${#LAST} != 2 ))
141then	err_exit 'LAST!=2'
142fi
143foo
144if	(( ${#LAST} != 2 ))
145then	err_exit 'LAST!=2'
146fi
147[[ $(set | grep LAST) == LAST=02 ]] || err_exit "LAST not correct in set list"
148rm -rf /tmp/ksh$$*
149set -a
150unset foo
151foo=bar
152if	[[ $(export | grep ^foo=) != 'foo=bar' ]]
153then	err_exit 'all export not working'
154fi
155unset foo
156read foo <<!
157bar
158!
159if	[[ $(export | grep ^foo=) != 'foo=bar' ]]
160then	err_exit 'all export not working with read'
161fi
162if	[[ $(typeset | grep PS2) == PS2 ]]
163then	err_exit 'typeset without arguments outputs names without attributes'
164fi
165unset a z q x
166w1=hello
167w2=world
168t1="$w1 $w2"
169if	(( 'a' == 97 ))
170then	b1=aGVsbG8gd29ybGQ=
171	b2=aGVsbG8gd29ybGRoZWxsbyB3b3JsZA==
172else	b1=iIWTk5ZAppaZk4Q=
173	b2=iIWTk5ZAppaZk4SIhZOTlkCmlpmThA==
174fi
175z=$b1
176typeset -b x=$b1
177[[ $x == "$z" ]] || print -u2 'binary variable not expanding correctly'
178[[  $(printf "%B" x) == $t1 ]] || err_exit 'typeset -b not working'
179typeset -b -Z5 a=$b1
180[[  $(printf "%B" a) == $w1 ]] || err_exit 'typeset -b -Z5 not working'
181typeset -b q=$x$x
182[[ $q == $b2 ]] || err_exit 'typeset -b not working with concatination'
183[[  $(printf "%B" q) == $t1$t1 ]] || err_exit 'typeset -b concatination not working'
184x+=$b1
185[[ $x == $b2 ]] || err_exit 'typeset -b not working with append'
186[[  $(printf "%B" x) == $t1$t1 ]] || err_exit 'typeset -b append not working'
187typeset -b -Z20 z=$b1
188(( $(printf "%B" z | wc -c) == 20 )) || err_exit 'typeset -b -Z20 not storing 20 bytes'
189{
190	typeset -b v1 v2
191	read -N11 v1
192	read -N22 v2
193} << !
194hello worldhello worldhello world
195!
196[[ $v1 == "$b1" ]] || err_exit "v1=$v1 should be $b1"
197[[ $v2 == "$x" ]] || err_exit "v1=$v2 should be $x"
198[[ $(env - '!=1' $SHELL -c 'echo ok' 2>/dev/null) == ok ]] || err_exit 'malformed environment terminates shell'
199unset var
200typeset -b var
201printf '12%Z34' | read -r -N 5 var
202[[ $var == MTIAMzQ= ]] || err_exit 'binary files with zeros not working'
203unset var
204if	command typeset -usi var=0xfffff 2> /dev/null
205then	(( $var == 0xffff )) || err_exit 'unsigned short integers not working'
206else	err_exit 'typeset -usi cannot be used for unsigned short'
207fi
208[[ $($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'
209[[ $($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'
210unset foo bar
211unset -f fun
212function fun
213{
214	export foo=hello
215	typeset -x  bar=world
216	[[ $foo == hello ]] || err_exit 'export scoping problem in function'
217}
218fun
219[[ $(export | grep foo) == 'foo=hello' ]] || err_exit 'export not working in functions'
220[[ $(export | grep bar) ]] && err_exit 'typeset -x not local'
221[[ $($SHELL -c 'typeset -r IFS=;print -r $(pwd)' 2> /dev/null) == "$(pwd)" ]] || err_exit 'readonly IFS causes command substitution to fail'
222fred[66]=88
223[[ $(typeset -pa) == *fred* ]] || err_exit 'typeset -pa not working'
224unset x y z
225typeset -LZ3 x=abcd y z=00abcd
226y=03
227[[ $y == "3  " ]] || err_exit '-LZ3 not working for value 03'
228[[ $x == "abc" ]] || err_exit '-LZ3 not working for value abcd'
229[[ $x == "abc" ]] || err_exit '-LZ3 not working for value 00abcd'
230unset x z
231set +a
232[[ $(typeset -p z) ]] && err_exit "typeset -p for z undefined failed"
233unset z
234x='typeset -i z=45'
235eval "$x"
236[[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed"
237[[ $(typeset +p z) == "${x%=*}" ]] || err_exit "typeset +p for '$x' failed"
238unset z
239x='typeset -a z=(a b c)'
240eval "$x"
241[[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed"
242[[ $(typeset +p z) == "${x%=*}" ]] || err_exit "typeset +p for '$x' failed"
243unset z
244x='typeset -C z=(
245	foo=bar
246	xxx=bam
247)'
248eval "$x"
249x=${x//$'\t'}
250x=${x//$'(\n'/'('}
251x=${x//$'\n'/';'}
252[[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed"
253[[ $(typeset +p z) == "${x%%=*}" ]] || err_exit "typeset +p for '$x' failed"
254unset z
255x='typeset -A z=([bar]=bam [xyz]=bar)'
256eval "$x"
257[[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed"
258[[ $(typeset +p z) == "${x%%=*}" ]] || err_exit "typeset +p for '$x' failed"
259unset z
260foo=abc
261x='typeset -n z=foo'
262eval "$x"
263[[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed"
264[[ $(typeset +p z) == "${x%%=*}" ]] || err_exit "typeset +p for '$x' failed"
265typeset +n z
266unset foo z
267typeset -T Pt_t=(
268	float x=1 y=2
269)
270Pt_t z
271x=${z//$'\t'}
272x=${x//$'(\n'/'('}
273x=${x//$'\n'/';'}
274[[ $(typeset -p z) == "Pt_t z=$x" ]] || err_exit "typeset -p for type failed"
275[[ $(typeset +p z) == "Pt_t z" ]] || err_exit "typeset +p for type failed"
276unset z
277function foo
278{
279	typeset -p bar
280}
281bar=xxx
282[[ $(foo) == bar=xxx ]] || err_exit 'typeset -p not working inside a function'
283exit	$((Errors))
284