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