Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/ksh93/tests/attributes.sh
1810 views
1
########################################################################
2
# #
3
# This software is part of the ast package #
4
# Copyright (c) 1982-2012 AT&T Intellectual Property #
5
# and is licensed under the #
6
# Eclipse Public License, Version 1.0 #
7
# by AT&T Intellectual Property #
8
# #
9
# A copy of the License is available at #
10
# http://www.eclipse.org/org/documents/epl-v10.html #
11
# (with md5 checksum b35adb5213ca9657e911e9befb180842) #
12
# #
13
# Information and Software Systems Research #
14
# AT&T Research #
15
# Florham Park NJ #
16
# #
17
# David Korn <[email protected]> #
18
# #
19
########################################################################
20
function err_exit
21
{
22
print -u2 -n "\t"
23
print -u2 -r ${Command}[$1]: "${@:2}"
24
let Errors+=1
25
}
26
alias err_exit='err_exit $LINENO'
27
28
Command=${0##*/}
29
integer Errors=0
30
31
tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
32
trap "cd /; rm -rf $tmp" EXIT
33
34
r=readonly u=Uppercase l=Lowercase i=22 i8=10 L=abc L5=def uL5=abcdef xi=20
35
x=export t=tagged H=hostname LZ5=026 RZ5=026 Z5=123 lR5=ABcdef R5=def n=l
36
for option in u l i i8 L L5 LZ5 RZ5 Z5 r x H t R5 uL5 lR5 xi n
37
do typeset -$option $option
38
done
39
(r=newval) 2> /dev/null && err_exit readonly attribute fails
40
i=i+5
41
if ((i != 27))
42
then err_exit integer attributes fails
43
fi
44
if [[ $i8 != 8#12 ]]
45
then err_exit integer base 8 fails
46
fi
47
if [[ $u != UPPERCASE ]]
48
then err_exit uppercase fails
49
fi
50
if [[ $l != lowercase ]]
51
then err_exit lowercase fails
52
fi
53
if [[ $n != lowercase ]]
54
then err_exit reference variables fail
55
fi
56
if [[ t=tagged != $(typeset -t) ]]
57
then err_exit tagged fails
58
fi
59
if [[ t != $(typeset +t) ]]
60
then err_exit tagged fails
61
fi
62
if [[ $Z5 != 00123 ]]
63
then err_exit zerofill fails
64
fi
65
if [[ $RZ5 != 00026 ]]
66
then err_exit right zerofill fails
67
fi
68
L=12345
69
if [[ $L != 123 ]]
70
then err_exit leftjust fails
71
fi
72
if [[ $L5 != "def " ]]
73
then err_exit leftjust fails
74
fi
75
if [[ $uL5 != ABCDE ]]
76
then err_exit leftjust uppercase fails
77
fi
78
if [[ $lR5 != bcdef ]]
79
then err_exit rightjust fails
80
fi
81
if [[ $R5 != " def" ]]
82
then err_exit rightjust fails
83
fi
84
if [[ $($SHELL -c 'echo $x') != export ]]
85
then err_exit export fails
86
fi
87
if [[ $($SHELL -c 'xi=xi+4;echo $xi') != 24 ]]
88
then err_exit export attributes fails
89
fi
90
x=$(foo=abc $SHELL <<!
91
foo=bar
92
$SHELL -c 'print \$foo'
93
!
94
)
95
if [[ $x != bar ]]
96
then err_exit 'environment variables require re-export'
97
fi
98
(typeset + ) > /dev/null 2>&1 || err_exit 'typeset + not working'
99
(typeset -L-5 buf="A" 2>/dev/null)
100
if [[ $? == 0 ]]
101
then err_exit 'typeset allows negative field for left/right adjust'
102
fi
103
a=b
104
readonly $a=foo
105
if [[ $b != foo ]]
106
then err_exit 'readonly $a=b not working'
107
fi
108
if [[ $(export | grep '^PATH=') != PATH=* ]]
109
then err_exit 'export not working'
110
fi
111
picture=(
112
bitmap=/fruit
113
size=(typeset -E x=2.5)
114
)
115
string="$(print $picture)"
116
if [[ "${string}" != *'size=( typeset -E'* ]]
117
then err_exit 'print of compound exponential variable not working'
118
fi
119
sz=(typeset -E y=2.2)
120
string="$(print $sz)"
121
if [[ "${sz}" == *'typeset -E -F'* ]]
122
then err_exit 'print of exponential shows both -E and -F attributes'
123
fi
124
print 'typeset -i m=48/4+1;print -- $m' > $tmp/script
125
chmod +x $tmp/script
126
typeset -Z2 m
127
if [[ $($tmp/script) != 13 ]]
128
then err_exit 'attributes not cleared for script execution'
129
fi
130
print 'print VAR=$VAR' > $tmp/script
131
typeset -L70 VAR=var
132
$tmp/script > $tmp/script.1
133
[[ $(< $tmp/script.1) == VAR= ]] || err_exit 'typeset -L should not be inherited'
134
typeset -Z LAST=00
135
unset -f foo
136
function foo
137
{
138
if [[ $1 ]]
139
then LAST=$1
140
else ((LAST++))
141
fi
142
}
143
foo 1
144
if (( ${#LAST} != 2 ))
145
then err_exit 'LAST!=2'
146
fi
147
foo
148
if (( ${#LAST} != 2 ))
149
then err_exit 'LAST!=2'
150
fi
151
[[ $(set | grep LAST) == LAST=02 ]] || err_exit "LAST not correct in set list"
152
set -a
153
unset foo
154
foo=bar
155
if [[ $(export | grep ^foo=) != 'foo=bar' ]]
156
then err_exit 'all export not working'
157
fi
158
unset foo
159
read foo <<!
160
bar
161
!
162
if [[ $(export | grep ^foo=) != 'foo=bar' ]]
163
then err_exit 'all export not working with read'
164
fi
165
if [[ $(typeset | grep PS2) == PS2 ]]
166
then err_exit 'typeset without arguments outputs names without attributes'
167
fi
168
unset a z q x
169
w1=hello
170
w2=world
171
t1="$w1 $w2"
172
if (( 'a' == 97 ))
173
then b1=aGVsbG8gd29ybGQ=
174
b2=aGVsbG8gd29ybGRoZWxsbyB3b3JsZA==
175
else b1=iIWTk5ZAppaZk4Q=
176
b2=iIWTk5ZAppaZk4SIhZOTlkCmlpmThA==
177
fi
178
z=$b1
179
typeset -b x=$b1
180
[[ $x == "$z" ]] || print -u2 'binary variable not expanding correctly'
181
[[ $(printf "%B" x) == $t1 ]] || err_exit 'typeset -b not working'
182
typeset -b -Z5 a=$b1
183
[[ $(printf "%B" a) == $w1 ]] || err_exit 'typeset -b -Z5 not working'
184
typeset -b q=$x$x
185
[[ $q == $b2 ]] || err_exit 'typeset -b not working with concatination'
186
[[ $(printf "%B" q) == $t1$t1 ]] || err_exit 'typeset -b concatination not working'
187
x+=$b1
188
[[ $x == $b2 ]] || err_exit 'typeset -b not working with append'
189
[[ $(printf "%B" x) == $t1$t1 ]] || err_exit 'typeset -b append not working'
190
typeset -b -Z20 z=$b1
191
(( $(printf "%B" z | wc -c) == 20 )) || err_exit 'typeset -b -Z20 not storing 20 bytes'
192
{
193
typeset -b v1 v2
194
read -N11 v1
195
read -N22 v2
196
} << !
197
hello worldhello worldhello world
198
!
199
[[ $v1 == "$b1" ]] || err_exit "v1=$v1 should be $b1"
200
[[ $v2 == "$x" ]] || err_exit "v1=$v2 should be $x"
201
if env '!=1' >/dev/null 2>&1
202
then [[ $(env '!=1' $SHELL -c 'echo ok' 2>/dev/null) == ok ]] || err_exit 'malformed environment terminates shell'
203
fi
204
unset var
205
typeset -b var
206
printf '12%Z34' | read -r -N 5 var
207
[[ $var == MTIAMzQ= ]] || err_exit 'binary files with zeros not working'
208
unset var
209
if command typeset -usi var=0xfffff 2> /dev/null
210
then (( $var == 0xffff )) || err_exit 'unsigned short integers not working'
211
else err_exit 'typeset -usi cannot be used for unsigned short'
212
fi
213
[[ $($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'
214
[[ $($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'
215
unset foo bar
216
unset -f fun
217
function fun
218
{
219
export foo=hello
220
typeset -x bar=world
221
[[ $foo == hello ]] || err_exit 'export scoping problem in function'
222
}
223
fun
224
[[ $(export | grep foo) == 'foo=hello' ]] || err_exit 'export not working in functions'
225
[[ $(export | grep bar) ]] && err_exit 'typeset -x not local'
226
[[ $($SHELL -c 'typeset -r IFS=;print -r $(pwd)' 2> /dev/null) == "$(pwd)" ]] || err_exit 'readonly IFS causes command substitution to fail'
227
fred[66]=88
228
[[ $(typeset -pa) == *fred* ]] || err_exit 'typeset -pa not working'
229
unset x y z
230
typeset -LZ3 x=abcd y z=00abcd
231
y=03
232
[[ $y == "3 " ]] || err_exit '-LZ3 not working for value 03'
233
[[ $x == "abc" ]] || err_exit '-LZ3 not working for value abcd'
234
[[ $x == "abc" ]] || err_exit '-LZ3 not working for value 00abcd'
235
unset x z
236
set +a
237
[[ $(typeset -p z) ]] && err_exit "typeset -p for z undefined failed"
238
unset z
239
x='typeset -i z=45'
240
eval "$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"
243
unset z
244
x='typeset -a z=(a b c)'
245
eval "$x"
246
[[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed"
247
[[ $(typeset +p z) == "${x%=*}" ]] || err_exit "typeset +p for '$x' failed"
248
unset z
249
x='typeset -C z=(
250
foo=bar
251
xxx=bam
252
)'
253
eval "$x"
254
x=${x//$'\t'}
255
x=${x//$'(\n'/'('}
256
x=${x//$'\n'/';'}
257
x=${x%';)'}')'
258
[[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed"
259
[[ $(typeset +p z) == "${x%%=*}" ]] || err_exit "typeset +p for '$x' failed"
260
unset z
261
x='typeset -A z=([bar]=bam [xyz]=bar)'
262
eval "$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"
265
unset z
266
foo=abc
267
x='typeset -n z=foo'
268
eval "$x"
269
[[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed"
270
[[ $(typeset +p z) == "${x%%=*}" ]] || err_exit "typeset +p for '$x' failed"
271
typeset +n z
272
unset foo z
273
typeset -T Pt_t=(
274
float x=1 y=2
275
)
276
Pt_t z
277
x=${z//$'\t'}
278
x=${x//$'(\n'/'('}
279
x=${x//$'\n'/';'}
280
x=${x%';)'}')'
281
[[ $(typeset -p z) == "Pt_t z=$x" ]] || err_exit "typeset -p for type failed"
282
[[ $(typeset +p z) == "Pt_t z" ]] || err_exit "typeset +p for type failed"
283
unset z
284
function foo
285
{
286
typeset -p bar
287
}
288
bar=xxx
289
[[ $(foo) == bar=xxx ]] || err_exit 'typeset -p not working inside a function'
290
unset foo
291
typeset -L5 foo
292
[[ $(typeset -p foo) == 'typeset -L 5 foo' ]] || err_exit 'typeset -p not working for variables with attributes but without a value'
293
{ $SHELL <<- EOF
294
typeset -L3 foo=aaa
295
typeset -L6 foo=bbbbbb
296
[[ \$foo == bbbbbb ]]
297
EOF
298
} || err_exit 'typeset -L should not preserve old attributes'
299
{ $SHELL <<- EOF
300
typeset -R3 foo=aaa
301
typeset -R6 foo=bbbbbb
302
[[ \$foo == bbbbbb ]]
303
EOF
304
} 2> /dev/null || err_exit 'typeset -R should not preserve old attributes'
305
306
expected='YWJjZGVmZ2hpag=='
307
unset foo
308
typeset -b -Z10 foo
309
read foo <<< 'abcdefghijklmnop'
310
[[ $foo == "$expected" ]] || err_exit 'read foo, where foo is "typeset -b -Z10" not working'
311
unset foo
312
typeset -b -Z10 foo
313
read -N10 foo <<< 'abcdefghijklmnop'
314
[[ $foo == "$expected" ]] || err_exit 'read -N10 foo, where foo is "typeset -b -Z10" not working'
315
unset foo
316
typeset -b -A foo
317
read -N10 foo[4] <<< 'abcdefghijklmnop'
318
[[ ${foo[4]} == "$expected" ]] || err_exit 'read -N10 foo, where foo is "typeset -b -A" foo not working'
319
unset foo
320
typeset -b -a foo
321
read -N10 foo[4] <<< 'abcdefghijklmnop'
322
[[ ${foo[4]} == "$expected" ]] || err_exit 'read -N10 foo, where foo is "typeset -b -a" foo not working'
323
[[ $(printf %B foo[4]) == abcdefghij ]] || err_exit 'printf %B for binary associative array element not working'
324
[[ $(printf %B foo[4]) == abcdefghij ]] || err_exit 'printf %B for binary indexed array element not working'
325
unset foo
326
327
$SHELL 2> /dev/null -c 'export foo=(bar=3)' && err_exit 'compound variables cannot be exported'
328
329
$SHELL -c 'builtin date' >/dev/null 2>&1 &&
330
{
331
332
# check env var changes against a builtin that uses the env var
333
334
SEC=1234252800
335
ETZ=EST5EDT
336
EDT=03
337
PTZ=PST8PDT
338
PDT=00
339
340
CMD="date -f%H \\#$SEC"
341
342
export TZ=$ETZ
343
344
set -- \
345
"$EDT $PDT $EDT" "" "TZ=$PTZ" "" \
346
"$EDT $PDT $EDT" "" "TZ=$PTZ" "TZ=$ETZ" \
347
"$EDT $PDT $EDT" "TZ=$ETZ" "TZ=$PTZ" "TZ=$ETZ" \
348
"$PDT $EDT $PDT" "TZ=$PTZ" "" "TZ=$PTZ" \
349
"$PDT $EDT $PDT" "TZ=$PTZ" "TZ=$ETZ" "TZ=$PTZ" \
350
"$EDT $PDT $EDT" "foo=bar" "TZ=$PTZ" "TZ=$ETZ" \
351
352
while (( $# >= 4 ))
353
do exp=$1
354
got=$(print $($SHELL -c "builtin date; $2 $CMD; $3 $CMD; $4 $CMD"))
355
[[ $got == $exp ]] || err_exit "[ '$2' '$3' '$4' ] env sequence failed -- expected '$exp', got '$got'"
356
shift 4
357
done
358
359
}
360
361
unset v
362
typeset -H v=/dev/null
363
[[ $v == *nul* ]] || err_exit 'typeset -H for /dev/null not working'
364
365
unset x
366
(typeset +C x) 2> /dev/null && err_exit 'typeset +C should be an error'
367
(typeset +A x) 2> /dev/null && err_exit 'typeset +A should be an error'
368
(typeset +a x) 2> /dev/null && err_exit 'typeset +a should be an error'
369
370
unset x
371
{
372
x=$($SHELL -c 'integer -s x=5;print -r -- $x')
373
} 2> /dev/null
374
[[ $x == 5 ]] || err_exit 'integer -s not working'
375
376
[[ $(typeset -l) == *namespace*.sh* ]] && err_exit 'typeset -l should not contain namespace .sh'
377
378
unset got
379
typeset -u got
380
exp=100
381
((got=$exp))
382
[[ $got == $exp ]] || err_exit "typeset -l fails on numeric value -- expected '$exp', got '$got'"
383
384
unset s
385
typeset -a -u s=( hello world chicken )
386
[[ ${s[2]} == CHICKEN ]] || err_exit 'typeset -u not working with indexed arrays'
387
unset s
388
typeset -A -u s=( [1]=hello [0]=world [2]=chicken )
389
[[ ${s[2]} == CHICKEN ]] || err_exit 'typeset -u not working with associative arrays'
390
expected=$'(\n\t[0]=WORLD\n\t[1]=HELLO\n\t[2]=CHICKEN\n)'
391
[[ $(print -v s) == "$expected" ]] || err_exit 'typeset -u for associative array does not display correctly'
392
393
unset s
394
if command typeset -M totitle s 2> /dev/null
395
then [[ $(typeset +p s) == 'typeset -M totitle s' ]] || err_exit 'typeset -M totitle does not display correctly with typeset -p'
396
fi
397
398
{ $SHELL <<- \EOF
399
compound -a a1
400
for ((i=1 ; i < 100 ; i++ ))
401
do [[ "$( typeset + a1[$i] )" == '' ]] && a1[$i].text='hello'
402
done
403
[[ ${a1[70].text} == hello ]]
404
EOF
405
} 2> /dev/null
406
(( $? )) && err_exit 'typeset + a[i] not working'
407
408
typeset groupDB="" userDB=""
409
typeset -l -L1 DBPick=""
410
[[ -n "$groupDB" ]] && err_exit 'typeset -l -L1 causes unwanted side effect'
411
412
HISTFILE=foo
413
typeset -u PS1='hello --- '
414
HISTFILE=foo
415
[[ $HISTFILE == foo ]] || err_exit 'typeset -u PS1 affects HISTFILE'
416
417
typeset -a a=( aA= ZQ= bA= bA= bw= Cg= )
418
typeset -b x
419
for (( i=0 ; i < ${#a[@]} ; i++ ))
420
do x+="${a[i]}"
421
done
422
[[ $(printf "%B" x) == hello ]] || err_exit "append for typeset -b not working: got '$(printf "%B" x)' should get hello"
423
424
(
425
trap 'exit $?' EXIT
426
$SHELL -c 'typeset v=foo; [[ $(typeset -p v[0]) == v=foo ]]'
427
) 2> /dev/null || err_exit 'typeset -p v[0] not working for simple variable v'
428
429
unset x
430
expected='typeset -a x=(a\=3 b\=4)'
431
typeset -a x=( a=3 b=4)
432
[[ $(typeset -p x) == "$expected" ]] || err_exit 'assignment elements in typeset -a assignment not working'
433
434
unset z
435
z='typeset -a q=(a b c)'
436
$SHELL -c "$z; [[ \$(typeset -pa) == '$z' ]]" || err_exit 'typeset -pa does not list only index arrays'
437
z='typeset -C z=(foo=bar)'
438
$SHELL -c "$z; [[ \$(typeset -pC) == '$z' ]]" || err_exit 'typeset -pC does not list only compound variables'
439
unset y
440
z='typeset -A y=([a]=foo)'
441
$SHELL -c "$z; [[ \$(typeset -pA) == '$z' ]]" || err_exit 'typeset -pA does not list only associative arrays'
442
443
$SHELL 2> /dev/null -c 'typeset -C arr=( aa bb cc dd )' && err_exit 'invalid compound variable assignment not reported'
444
445
unset x
446
typeset -l x=
447
[[ ${x:=foo} == foo ]] || err_exit '${x:=foo} with x unset, not foo when x is a lowercase variable'
448
449
unset x
450
typeset -L4 x=$'\001abcdef'
451
[[ ${#x} == 5 ]] || err_exit "width of character '\01' is not zero"
452
453
unset x
454
typeset -L x=-1
455
command typeset -F x=0-1 2> /dev/null || err_exit 'typeset -F after typeset -L fails'
456
457
unset val
458
typeset -i val=10#0-3
459
typeset -Z val=0-1
460
[[ $val == 0-1 ]] || err_exit 'integer attribute not cleared for subsequent typeset'
461
462
unset x
463
typeset -L -Z x=foo
464
[[ $(typeset -p x) == 'typeset -Z 3 -L 3 x=foo' ]] || err_exit '-LRZ without [n] not defaulting to width of variable'
465
466
unset foo
467
typeset -Z2 foo=3
468
[[ $(typeset -p foo) == 'typeset -Z 2 -R 2 foo=03' ]] || err_exit '-Z2 not working'
469
export foo
470
[[ $(typeset -p foo) == 'typeset -x -Z 2 -R 2 foo=03' ]] || err_exit '-Z2 not working after export'
471
472
exit $((Errors<125?Errors:125))
473
474