xref: /titanic_41/usr/src/lib/libshell/common/tests/glob.sh (revision 0f1702c5201310f0529cd5abb77652e5e9b241b6)
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	((errors++))
25}
26alias err_exit='err_exit $LINENO'
27
28integer aware=0 contrary=0 ignorant=0
29
30function test_glob
31{
32	typeset lineno expected drop arg got sep op val add del
33	if	[[ $1 == --* ]]
34	then	del=${1#--}
35		shift
36	fi
37	if	[[ $1 == ++* ]]
38	then	add=${1#++}
39		shift
40	fi
41	lineno=$1 expected=$2
42	shift 2
43	if	(( contrary ))
44	then	if	[[ $expected == "<Beware> "* ]]
45		then	expected=${expected#"<Beware> "}
46			expected="$expected <Beware>"
47		fi
48		if	[[ $expected == *"<aXb> <abd>"* ]]
49		then	expected=${expected/"<aXb> <abd>"/"<abd> <aXb>"}
50		fi
51	fi
52	for arg
53	do	got="$got$sep<$arg>"
54		sep=" "
55	done
56	if	(( ignorant && aware ))
57	then	if	[[ $del ]]
58		then	got="<$del> $got"
59		fi
60		if	[[ $add ]]
61		then	expected="<$add> $expected"
62		fi
63	fi
64	if	[[ $got != "$expected" ]]
65	then	err_exit $lineno "glob: got '$got' expected '$expected'"
66	fi
67}
68
69function test_case
70{
71	typeset lineno expected subject pattern got
72	lineno=$1 expected=$2 subject=$3 pattern=$4
73	eval "
74		case $subject in
75		$pattern)	got='<match>' ;;
76		*)		got='<nomatch>' ;;
77		esac
78	"
79	if	[[ $got != "$expected" ]]
80	then	err_exit $lineno "case $subject in $pattern) got '$got' expected '$expected'"
81	fi
82}
83
84Command=${0##*/}
85tmp=/tmp/ksh$$
86integer errors=0
87unset undefined
88
89export LC_COLLATE=C
90
91mkdir $tmp || err_exit $LINENO "mkdir $tmp failed"
92trap "cd /; rm -rf $tmp" EXIT
93cd $tmp || err_exit $LINENO "cd $tmp failed"
94rm -rf *
95
96touch B b
97set -- *
98case $* in
99'b B')	contrary=1 ;;
100b|B)	ignorant=1 ;;
101esac
102set -- $(/bin/sh -c 'echo [a-c]')
103case $* in
104B)	aware=1 ;;
105esac
106rm -rf *
107
108touch a b c d abc abd abe bb bcd ca cb dd de Beware
109mkdir bdir
110
111test_glob $LINENO '<a> <abc> <abd> <abe> <X*>' a* X*
112test_glob $LINENO '<a> <abc> <abd> <abe>' \a*
113
114if	( set --nullglob ) 2>/dev/null
115then
116	set --nullglob
117
118	test_glob $LINENO '<a> <abc> <abd> <abe>' a* X*
119
120	set --nonullglob
121fi
122
123if	( set --failglob ) 2>/dev/null
124then
125	set --failglob
126	mkdir tmp
127	touch tmp/l1 tmp/l2 tmp/l3
128
129	test_glob $LINENO '' tmp/l[12] tmp/*4 tmp/*3
130	test_glob $LINENO '' tmp/l[12] tmp/*4 tmp/*3
131
132	rm -r tmp
133	set --nofailglob
134fi
135
136test_glob $LINENO '<bdir/>' b*/
137test_glob $LINENO '<*>' \*
138test_glob $LINENO '<a*>' 'a*'
139test_glob $LINENO '<a*>' a\*
140test_glob $LINENO '<c> <ca> <cb> <a*> <*q*>' c* a\* *q*
141test_glob $LINENO '<**>' "*"*
142test_glob $LINENO '<**>' \**
143test_glob $LINENO '<\.\./*/>' "\.\./*/"
144test_glob $LINENO '<s/\..*//>' 's/\..*//'
145test_glob $LINENO '</^root:/{s/^[!:]*:[!:]*:\([!:]*\).*$/\1/>' "/^root:/{s/^[!:]*:[!:]*:\([!:]*\).*"'$'"/\1/"
146test_glob $LINENO '<abc> <abd> <abe> <bb> <cb>' [a-c]b*
147test_glob ++Beware $LINENO '<abd> <abe> <bb> <bcd> <bdir> <ca> <cb> <dd> <de>' [a-y]*[!c]
148test_glob $LINENO '<abd> <abe>' a*[!c]
149
150touch a-b aXb
151
152test_glob $LINENO '<a-b> <aXb>' a[X-]b
153
154touch .x .y
155
156test_glob --Beware $LINENO '<Beware> <d> <dd> <de>' [!a-c]*
157
158if	mkdir a\*b 2>/dev/null
159then
160	touch a\*b/ooo
161
162	test_glob $LINENO '<a*b/ooo>' a\*b/*
163	test_glob $LINENO '<a*b/ooo>' a\*?/*
164	test_case $LINENO '<match>' '!7' '*\!*'
165	test_case $LINENO '<match>' 'r.*' '*.\*'
166	test_glob $LINENO '<abc>' a[b]c
167	test_glob $LINENO '<abc>' a["b"]c
168	test_glob $LINENO '<abc>' a[\b]c
169	test_glob $LINENO '<abc>' a?c
170	test_case $LINENO '<match>' 'abc' 'a"b"c'
171	test_case $LINENO '<match>' 'abc' 'a*c'
172	test_case $LINENO '<nomatch>' 'abc' '"a?c"'
173	test_case $LINENO '<nomatch>' 'abc' 'a\*c'
174	test_case $LINENO '<nomatch>' 'abc' 'a\[b]c'
175	test_case $LINENO '<match>' '"$undefined"' '""'
176	test_case $LINENO '<match>' 'abc' 'a["\b"]c'
177
178	rm -rf mkdir a\*b
179fi
180
181mkdir man
182mkdir man/man1
183touch man/man1/sh.1
184
185test_glob $LINENO '<man/man1/sh.1>' */man*/sh.*
186test_glob $LINENO '<man/man1/sh.1>' $(echo */man*/sh.*)
187test_glob $LINENO '<man/man1/sh.1>' "$(echo */man*/sh.*)"
188
189test_case $LINENO '<match>' 'abc' 'a***c'
190test_case $LINENO '<match>' 'abc' 'a*****?c'
191test_case $LINENO '<match>' 'abc' '?*****??'
192test_case $LINENO '<match>' 'abc' '*****??'
193test_case $LINENO '<match>' 'abc' '*****??c'
194test_case $LINENO '<match>' 'abc' '?*****?c'
195test_case $LINENO '<match>' 'abc' '?***?****c'
196test_case $LINENO '<match>' 'abc' '?***?****?'
197test_case $LINENO '<match>' 'abc' '?***?****'
198test_case $LINENO '<match>' 'abc' '*******c'
199test_case $LINENO '<match>' 'abc' '*******?'
200test_case $LINENO '<match>' 'abcdecdhjk' 'a*cd**?**??k'
201test_case $LINENO '<match>' 'abcdecdhjk' 'a**?**cd**?**??k'
202test_case $LINENO '<match>' 'abcdecdhjk' 'a**?**cd**?**??k***'
203test_case $LINENO '<match>' 'abcdecdhjk' 'a**?**cd**?**??***k'
204test_case $LINENO '<match>' 'abcdecdhjk' 'a**?**cd**?**??***k**'
205test_case $LINENO '<match>' 'abcdecdhjk' 'a****c**?**??*****'
206test_case $LINENO '<match>' "'-'" '[-abc]'
207test_case $LINENO '<match>' "'-'" '[abc-]'
208test_case $LINENO '<match>' "'\\'" '\\'
209test_case $LINENO '<match>' "'\\'" '[\\]'
210test_case $LINENO '<match>' "'\\'" "'\\'"
211test_case $LINENO '<match>' "'['" '[[]'
212test_case $LINENO '<match>' '[' '[[]'
213test_case $LINENO '<match>' "'['" '['
214test_case $LINENO '<match>' '[' '['
215test_case $LINENO '<match>' "'[abc'" "'['*"
216test_case $LINENO '<nomatch>' "'[abc'" '[*'
217test_case $LINENO '<match>' '[abc' "'['*"
218test_case $LINENO '<nomatch>' '[abc' '[*'
219test_case $LINENO '<match>' 'abd' "a[b/c]d"
220test_case $LINENO '<match>' 'a/d' "a[b/c]d"
221test_case $LINENO '<match>' 'acd' "a[b/c]d"
222test_case $LINENO '<match>' "']'" '[]]'
223test_case $LINENO '<match>' "'-'" '[]-]'
224test_case $LINENO '<match>' 'p' '[a-\z]'
225test_case $LINENO '<match>' '"/tmp"' '[/\\]*'
226test_case $LINENO '<nomatch>' 'abc' '??**********?****?'
227test_case $LINENO '<nomatch>' 'abc' '??**********?****c'
228test_case $LINENO '<nomatch>' 'abc' '?************c****?****'
229test_case $LINENO '<nomatch>' 'abc' '*c*?**'
230test_case $LINENO '<nomatch>' 'abc' 'a*****c*?**'
231test_case $LINENO '<nomatch>' 'abc' 'a********???*******'
232test_case $LINENO '<nomatch>' "'a'" '[]'
233test_case $LINENO '<nomatch>' 'a' '[]'
234test_case $LINENO '<nomatch>' "'['" '[abc'
235test_case $LINENO '<nomatch>' '[' '[abc'
236
237test_glob ++Beware $LINENO '<b> <bb> <bcd> <bdir>' b*
238test_glob $LINENO '<Beware> <b> <bb> <bcd> <bdir>' [bB]*
239
240if	( set --nocaseglob ) 2>/dev/null
241then
242	set --nocaseglob
243
244	test_glob $LINENO '<Beware> <b> <bb> <bcd> <bdir>' b*
245	test_glob $LINENO '<Beware> <b> <bb> <bcd> <bdir>' [b]*
246	test_glob $LINENO '<Beware> <b> <bb> <bcd> <bdir>' [bB]*
247
248	set --nonocaseglob
249fi
250
251if	( set -f ) 2>/dev/null
252then
253	set -f
254
255	test_glob $LINENO '<*>' *
256
257	set +f
258fi
259
260if	( set --noglob ) 2>/dev/null
261then
262	set --noglob
263
264	test_glob $LINENO '<*>' *
265
266	set --glob
267fi
268
269FIGNORE='.*|*'
270test_glob $LINENO '<*>' *
271
272FIGNORE='.*|*c|*e|?'
273test_glob $LINENO '<a-b> <aXb> <abd> <bb> <bcd> <bdir> <ca> <cb> <dd> <man>' *
274
275FIGNORE='.*|*b|*d|?'
276test_glob $LINENO '<Beware> <abc> <abe> <bdir> <ca> <de> <man>' *
277
278FIGNORE=
279test_glob $LINENO '<man/man1/sh.1>' */man*/sh.*
280
281unset FIGNORE
282test_glob $LINENO '<bb> <ca> <cb> <dd> <de>' ??
283test_glob $LINENO '<man/man1/sh.1>' */man*/sh.*
284
285GLOBIGNORE='.*:*'
286set -- *
287if	[[ $1 == '*' ]]
288then
289	GLOBIGNORE='.*:*c:*e:?'
290	test_glob $LINENO '<>' *
291
292	GLOBIGNORE='.*:*b:*d:?'
293	test_glob $LINENO '<>' *
294
295	unset GLOBIGNORE
296	test_glob $LINENO '<>' *
297	test_glob $LINENO '<man/man1/sh.1>' */man*/sh.*
298
299	GLOBIGNORE=
300	test_glob $LINENO '<man/man1/sh.1>' */man*/sh.*
301fi
302
303exit $errors
304