Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/ksh93/tests/glob.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 -r $'\t'"${Command}[$1] ${@:2}"
23
((Errors++))
24
}
25
alias err_exit='err_exit $LINENO'
26
27
Command=${0##*/}
28
integer aware=0 contrary=0 Errors=0 ignorant=0
29
30
tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
31
trap "cd /; rm -rf $tmp" EXIT
32
33
function test_glob
34
{
35
typeset lineno expected drop arg got sep op val add del
36
lineno=$1
37
shift
38
if [[ $1 == --* ]]
39
then del=${1#--}
40
shift
41
fi
42
if [[ $1 == ++* ]]
43
then add=${1#++}
44
shift
45
fi
46
expected=$1
47
shift
48
if (( contrary ))
49
then if [[ $expected == "<Beware> "* ]]
50
then expected=${expected#"<Beware> "}
51
expected="$expected <Beware>"
52
fi
53
if [[ $expected == *"<aXb> <abd>"* ]]
54
then expected=${expected/"<aXb> <abd>"/"<abd> <aXb>"}
55
fi
56
fi
57
for arg
58
do got="$got$sep<$arg>"
59
sep=" "
60
done
61
if (( ignorant && aware ))
62
then if [[ $del ]]
63
then got="<$del> $got"
64
fi
65
if [[ $add ]]
66
then expected="<$add> $expected"
67
fi
68
fi
69
if [[ $got != "$expected" ]]
70
then 'err_exit' $lineno "glob -- expected '$expected', got '$got'"
71
fi
72
}
73
alias test_glob='test_glob $LINENO'
74
75
function test_case
76
{
77
typeset lineno expected subject pattern got
78
lineno=$1 expected=$2 subject=$3 pattern=$4
79
eval "
80
case $subject in
81
$pattern) got='<match>' ;;
82
*) got='<nomatch>' ;;
83
esac
84
"
85
if [[ $got != "$expected" ]]
86
then 'err_exit' $lineno "case $subject in $pattern) -- expected '$expected', got '$got'"
87
fi
88
}
89
alias test_case='test_case $LINENO'
90
91
unset undefined
92
93
cd $tmp || { err_exit "cd $tmp failed"; exit 1; }
94
95
export LC_COLLATE=C
96
touch B b
97
set -- *
98
case $* in
99
'b B') contrary=1 ;;
100
b|B) ignorant=1 ;;
101
esac
102
set -- $(LC_ALL=C /bin/sh -c 'echo [a-c]')
103
case $* in
104
B) aware=1 ;;
105
esac
106
rm -rf *
107
108
touch a b c d abc abd abe bb bcd ca cb dd de Beware
109
mkdir bdir
110
111
test_glob '<a> <abc> <abd> <abe> <X*>' a* X*
112
test_glob '<a> <abc> <abd> <abe>' \a*
113
114
if ( set --nullglob ) 2>/dev/null
115
then
116
set --nullglob
117
118
test_glob '<a> <abc> <abd> <abe>' a* X*
119
120
set --nonullglob
121
fi
122
123
if ( set --failglob ) 2>/dev/null
124
then
125
set --failglob
126
mkdir tmp
127
touch tmp/l1 tmp/l2 tmp/l3
128
129
test_glob '' tmp/l[12] tmp/*4 tmp/*3
130
test_glob '' tmp/l[12] tmp/*4 tmp/*3
131
132
rm -r tmp
133
set --nofailglob
134
fi
135
136
test_glob '<bdir/>' b*/
137
test_glob '<*>' \*
138
test_glob '<a*>' 'a*'
139
test_glob '<a*>' a\*
140
test_glob '<c> <ca> <cb> <a*> <*q*>' c* a\* *q*
141
test_glob '<**>' "*"*
142
test_glob '<**>' \**
143
test_glob '<\.\./*/>' "\.\./*/"
144
test_glob '<s/\..*//>' 's/\..*//'
145
test_glob '</^root:/{s/^[!:]*:[!:]*:\([!:]*\).*$/\1/>' "/^root:/{s/^[!:]*:[!:]*:\([!:]*\).*"'$'"/\1/"
146
test_glob '<abc> <abd> <abe> <bb> <cb>' [a-c]b*
147
test_glob ++Beware '<abd> <abe> <bb> <bcd> <bdir> <ca> <cb> <dd> <de>' [a-y]*[!c]
148
test_glob '<abd> <abe>' a*[!c]
149
150
touch a-b aXb
151
152
test_glob '<a-b> <aXb>' a[X-]b
153
154
touch .x .y
155
156
test_glob --Beware '<Beware> <d> <dd> <de>' [!a-c]*
157
158
if mkdir a\*b 2>/dev/null
159
then
160
touch a\*b/ooo
161
162
test_glob '<a*b/ooo>' a\*b/*
163
test_glob '<a*b/ooo>' a\*?/*
164
test_case '<match>' '!7' '*\!*'
165
test_case '<match>' 'r.*' '*.\*'
166
test_glob '<abc>' a[b]c
167
test_glob '<abc>' a["b"]c
168
test_glob '<abc>' a[\b]c
169
test_glob '<abc>' a?c
170
test_case '<match>' 'abc' 'a"b"c'
171
test_case '<match>' 'abc' 'a*c'
172
test_case '<nomatch>' 'abc' '"a?c"'
173
test_case '<nomatch>' 'abc' 'a\*c'
174
test_case '<nomatch>' 'abc' 'a\[b]c'
175
test_case '<match>' '"$undefined"' '""'
176
test_case '<match>' 'abc' 'a["\b"]c'
177
178
rm -rf mkdir a\*b
179
fi
180
181
mkdir man
182
mkdir man/man1
183
touch man/man1/sh.1
184
185
test_glob '<man/man1/sh.1>' */man*/sh.*
186
test_glob '<man/man1/sh.1>' $(echo */man*/sh.*)
187
test_glob '<man/man1/sh.1>' "$(echo */man*/sh.*)"
188
189
test_case '<match>' 'abc' 'a***c'
190
test_case '<match>' 'abc' 'a*****?c'
191
test_case '<match>' 'abc' '?*****??'
192
test_case '<match>' 'abc' '*****??'
193
test_case '<match>' 'abc' '*****??c'
194
test_case '<match>' 'abc' '?*****?c'
195
test_case '<match>' 'abc' '?***?****c'
196
test_case '<match>' 'abc' '?***?****?'
197
test_case '<match>' 'abc' '?***?****'
198
test_case '<match>' 'abc' '*******c'
199
test_case '<match>' 'abc' '*******?'
200
test_case '<match>' 'abcdecdhjk' 'a*cd**?**??k'
201
test_case '<match>' 'abcdecdhjk' 'a**?**cd**?**??k'
202
test_case '<match>' 'abcdecdhjk' 'a**?**cd**?**??k***'
203
test_case '<match>' 'abcdecdhjk' 'a**?**cd**?**??***k'
204
test_case '<match>' 'abcdecdhjk' 'a**?**cd**?**??***k**'
205
test_case '<match>' 'abcdecdhjk' 'a****c**?**??*****'
206
test_case '<match>' "'-'" '[-abc]'
207
test_case '<match>' "'-'" '[abc-]'
208
test_case '<match>' "'\\'" '\\'
209
test_case '<match>' "'\\'" '[\\]'
210
test_case '<match>' "'\\'" "'\\'"
211
test_case '<match>' "'['" '[[]'
212
test_case '<match>' '[' '[[]'
213
test_case '<match>' "'['" '['
214
test_case '<match>' '[' '['
215
test_case '<match>' "'[abc'" "'['*"
216
test_case '<nomatch>' "'[abc'" '[*'
217
test_case '<match>' '[abc' "'['*"
218
test_case '<nomatch>' '[abc' '[*'
219
test_case '<match>' 'abd' "a[b/c]d"
220
test_case '<match>' 'a/d' "a[b/c]d"
221
test_case '<match>' 'acd' "a[b/c]d"
222
test_case '<match>' "']'" '[]]'
223
test_case '<match>' "'-'" '[]-]'
224
test_case '<match>' 'p' '[a-\z]'
225
test_case '<match>' '"/tmp"' '[/\\]*'
226
test_case '<nomatch>' 'abc' '??**********?****?'
227
test_case '<nomatch>' 'abc' '??**********?****c'
228
test_case '<nomatch>' 'abc' '?************c****?****'
229
test_case '<nomatch>' 'abc' '*c*?**'
230
test_case '<nomatch>' 'abc' 'a*****c*?**'
231
test_case '<nomatch>' 'abc' 'a********???*******'
232
test_case '<nomatch>' "'a'" '[]'
233
test_case '<nomatch>' 'a' '[]'
234
test_case '<nomatch>' "'['" '[abc'
235
test_case '<nomatch>' '[' '[abc'
236
237
test_glob ++Beware '<b> <bb> <bcd> <bdir>' b*
238
test_glob '<Beware> <b> <bb> <bcd> <bdir>' [bB]*
239
240
if ( set --nocaseglob ) 2>/dev/null
241
then
242
set --nocaseglob
243
244
test_glob '<Beware> <b> <bb> <bcd> <bdir>' b*
245
test_glob '<Beware> <b> <bb> <bcd> <bdir>' [b]*
246
test_glob '<Beware> <b> <bb> <bcd> <bdir>' [bB]*
247
248
set --nonocaseglob
249
fi
250
251
if ( set -f ) 2>/dev/null
252
then
253
set -f
254
255
test_glob '<*>' *
256
257
set +f
258
fi
259
260
if ( set --noglob ) 2>/dev/null
261
then
262
set --noglob
263
264
test_glob '<*>' *
265
266
set --glob
267
fi
268
269
FIGNORE='@(.*|*)'
270
test_glob '<*>' *
271
272
FIGNORE='@(.*|*c|*e|?)'
273
test_glob '<a-b> <aXb> <abd> <bb> <bcd> <bdir> <ca> <cb> <dd> <man>' *
274
275
FIGNORE='@(.*|*b|*d|?)'
276
test_glob '<Beware> <abc> <abe> <bdir> <ca> <de> <man>' *
277
278
FIGNORE=
279
test_glob '<man/man1/sh.1>' */man*/sh.*
280
281
unset FIGNORE
282
test_glob '<bb> <ca> <cb> <dd> <de>' ??
283
test_glob '<man/man1/sh.1>' */man*/sh.*
284
285
GLOBIGNORE='.*:*'
286
set -- *
287
if [[ $1 == '*' ]]
288
then
289
GLOBIGNORE='.*:*c:*e:?'
290
test_glob '<>' *
291
292
GLOBIGNORE='.*:*b:*d:?'
293
test_glob '<>' *
294
295
unset GLOBIGNORE
296
test_glob '<>' *
297
test_glob '<man/man1/sh.1>' */man*/sh.*
298
299
GLOBIGNORE=
300
test_glob '<man/man1/sh.1>' */man*/sh.*
301
fi
302
unset GLOBIGNORE
303
304
function test_sub
305
{
306
x='${subject'$2'}'
307
eval g=$x
308
if [[ "$g" != "$3" ]]
309
then 'err_exit' $1 subject="'$subject' $x failed, expected '$3', got '$g'"
310
fi
311
}
312
alias test_sub='test_sub $LINENO'
313
314
set --noglob --nobraceexpand
315
316
subject='A regular expressions test'
317
318
test_sub '/e/#' 'A r#gular expressions test'
319
test_sub '//e/#' 'A r#gular #xpr#ssions t#st'
320
test_sub '/[^e]/#' '# regular expressions test'
321
test_sub '//[^e]/#' '###e######e###e########e##'
322
test_sub '/+(e)/#' 'A r#gular expressions test'
323
test_sub '//+(e)/#' 'A r#gular #xpr#ssions t#st'
324
test_sub '/@-(e)/#' 'A r#gular expressions test'
325
test_sub '//@-(e)/#' 'A r#gular #xpr#ssions t#st'
326
test_sub '/?(e)/#' '#A regular expressions test'
327
test_sub '//?(e)/#' '#A# #r#g#u#l#a#r# #x#p#r#s#s#i#o#n#s# #t#s#t#'
328
test_sub '/*(e)/#' '#A regular expressions test'
329
test_sub '//*(e)/#' '#A# #r#g#u#l#a#r# #x#p#r#s#s#i#o#n#s# #t#s#t#'
330
test_sub '//@(e)/[\1]' 'A r[e]gular [e]xpr[e]ssions t[e]st'
331
test_sub '//@-(e)/[\1]' 'A r[e]gular [e]xpr[e]ssions t[e]st'
332
test_sub '//+(e)/[\1]' 'A r[e]gular [e]xpr[e]ssions t[e]st'
333
test_sub '//+-(e)/[\1]' 'A r[e]gular [e]xpr[e]ssions t[e]st'
334
test_sub '//@(+(e))/[\1]' 'A r[e]gular [e]xpr[e]ssions t[e]st'
335
test_sub '//@(+-(e))/[\1]' 'A r[e]gular [e]xpr[e]ssions t[e]st'
336
test_sub '//-(e)/#' 'A regular expressions test'
337
test_sub '//--(e)/#' 'A regular expressions test'
338
test_sub '//?(e)/[\1]' '[]A[] []r[e]g[]u[]l[]a[]r[] [e]x[]p[]r[e]s[]s[]i[]o[]n[]s[] []t[e]s[]t[]'
339
test_sub '//{0,1}(e)/[\1]' '[]A[] []r[e]g[]u[]l[]a[]r[] [e]x[]p[]r[e]s[]s[]i[]o[]n[]s[] []t[e]s[]t[]'
340
test_sub '//*(e)/[\1]' '[]A[] []r[e]g[]u[]l[]a[]r[] [e]x[]p[]r[e]s[]s[]i[]o[]n[]s[] []t[e]s[]t[]'
341
test_sub '//{0,}(e)/[\1]' '[]A[] []r[e]g[]u[]l[]a[]r[] [e]x[]p[]r[e]s[]s[]i[]o[]n[]s[] []t[e]s[]t[]'
342
test_sub '//@(?(e))/[\1]' '[]A[] []r[e]g[]u[]l[]a[]r[] [e]x[]p[]r[e]s[]s[]i[]o[]n[]s[] []t[e]s[]t[]'
343
test_sub '//@({0,1}(e))/[\1]' '[]A[] []r[e]g[]u[]l[]a[]r[] [e]x[]p[]r[e]s[]s[]i[]o[]n[]s[] []t[e]s[]t[]'
344
test_sub '//@(*(e))/[\1]' '[]A[] []r[e]g[]u[]l[]a[]r[] [e]x[]p[]r[e]s[]s[]i[]o[]n[]s[] []t[e]s[]t[]'
345
test_sub '//@({0,}(e))/[\1]' '[]A[] []r[e]g[]u[]l[]a[]r[] [e]x[]p[]r[e]s[]s[]i[]o[]n[]s[] []t[e]s[]t[]'
346
test_sub '/?-(e)/#' '#A regular expressions test'
347
test_sub '/@(?-(e))/[\1]' '[]A regular expressions test'
348
test_sub '/!(e)/#' '#'
349
test_sub '//!(e)/#' '#'
350
test_sub '/@(!(e))/[\1]' '[A regular expressions test]'
351
test_sub '//@(!(e))/[\1]' '[A regular expressions test]'
352
353
subject='e'
354
355
test_sub '/!(e)/#' '#e'
356
test_sub '//!(e)/#' '#e#'
357
test_sub '/!(e)/[\1]' '[]e'
358
test_sub '//!(e)/[\1]' '[]e[]'
359
test_sub '/@(!(e))/[\1]' '[]e'
360
test_sub '//@(!(e))/[\1]' '[]e[]'
361
362
subject='a'
363
364
test_sub '/@(!(a))/[\1]' '[]a'
365
test_sub '//@(!(a))/[\1]' '[]a[]'
366
367
subject='aha'
368
369
test_sub '/@(!(a))/[\1]' '[aha]'
370
test_sub '//@(!(a))/[\1]' '[aha]'
371
test_sub '/@(!(aha))/[\1]' '[ah]a'
372
test_sub '//@(!(aha))/[\1]' '[ah][a]'
373
374
exit $((Errors<125?Errors:125))
375
376