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