1#! /bin/sh
2#
3# @(#)glob.sh	1.9 19/10/15 Copyright 2016-2019 J. Schilling
4#
5
6# Read test core functions
7. ../../common/test-common
8
9remove d/*
10remove dir/*
11rmdir d dir 2>/dev/null
12
13#
14# Basic tests to check whether globbing works
15#
16docommand gl00 "$SHELL -c 'echo gl?b.sh'" 0 "glob.sh\n" ""
17docommand gl01 "$SHELL -c 'echo gl*b.sh'" 0 "glob.sh\n" ""
18docommand gl02 "$SHELL -c 'echo g*b.sh'" 0 "glob.sh\n" ""
19docommand gl03 "$SHELL -c 'echo gl[abco]b.sh'" 0 "glob.sh\n" ""
20docommand gl04 "$SHELL -c 'echo gl[a-o]b.sh'" 0 "glob.sh\n" ""
21docommand gl05 "$SHELL -c 'echo gl\ob.sh'" 0 "glob.sh\n" ""
22
23docommand gl10 "$SHELL -c 'echo gl[[:alpha:]]b.sh'"  0 "glob.sh\n" ""
24docommand gl11 "$SHELL -c 'echo gl[![:alpha:]]b.sh'" 0 "gl[![:alpha:]]b.sh\n" ""
25docommand gl12 "$SHELL -c 'echo gl[[:digit:]]b.sh'"  0 "gl[[:digit:]]b.sh\n" ""
26docommand gl13 "$SHELL -c 'echo gl[![:digit:]]b.sh'" 0 "glob.sh\n" ""
27
28#
29# Basic tests to check whether globbing with syntax error does not expand
30#
31docommand gl50 "$SHELL -c 'echo ![*[*'" 0 "![*[*\n" ""
32
33$SRCROOT/conf/mkdir-sh -p '[x'
34: > '[x/foo'
35docommand gl51 "$SHELL -c 'echo [*'" 0 "[*\n" ""
36docommand gl52 "$SHELL -c 'echo *[x'" 0 "*[x\n" ""
37docommand gl53 "$SHELL -c 'echo [x/*'" 0 "[x/foo\n" ""
38remove '[x/foo'
39rmdir '[x'
40
41symlink="ln -s"
42MKLINKS_TEST=${MKLINKS_TEST-:}
43rm -f xxzzy.123 xxzzy.345
44echo test > xxzzy.123
45$symlink xxzzy.123 xxzzy.345
46test $? = 0 || symlink=cp
47test -r xxzzy.345 || symlink=cp
48${MKLINKS_TEST} -h xxzzy.345 || symlink=cp
49rm -f xxzzy.123 xxzzy.345
50
51if [ "$symlink" != cp ]; then
52$SRCROOT/conf/mkdir-sh -p 'dir'
53$symlink non-existent dir/abc
54
55docommand gl60 "$SHELL -c 'echo d*/*'" 0 "dir/abc\n" ""
56docommand gl61 "$SHELL -c 'echo d*/abc'" 0 "dir/abc\n" ""
57
58remove dir/abc
59rmdir dir
60fi
61
62docommand gl100 "$SHELL -c 'case foo in f*) echo OK;; *) echo FAIL;; esac'" 0 "OK\n" ""
63docommand gl101 "$SHELL -c 'case foo in bla|f*) echo OK;; *) echo FAIL;; esac'" 0 "OK\n" ""
64docommand gl102 "$SHELL -c 'case f\\* in f*) echo OK;; *) echo FAIL;; esac'" 0 "OK\n" ""
65docommand gl103 "$SHELL -c 'case f\\* in f\*) echo OK;; *) echo FAIL;; esac'" 0 "OK\n" ""
66docommand gl104 "$SHELL -c 'case f\\* in f'*') echo OK;; *) echo FAIL;; esac'" 0 "OK\n" ""
67docommand gl105 "$SHELL -c 'case f\\* in 'f*') echo OK;; *) echo FAIL;; esac'" 0 "OK\n" ""
68
69docommand gl120 "$SHELL -c 'case ] in *[ab\]cd]*) echo OK;; *) echo FAIL;; esac'" 0 "OK\n" ""
70docommand gl121 "$SHELL -c 'case e in *[!ab\]cd]*) echo OK;; *) echo FAIL;; esac'" 0 "OK\n" ""
71
72docommand gl150 "$SHELL -c 'case bla in [[:alpha:]]la) echo OK;; *) echo FAIL;; esac'" 0 "OK\n" ""
73docommand gl151 "$SHELL -c 'case bla in [![:digit:]]la) echo OK;; *) echo FAIL;; esac'" 0 "OK\n" ""
74docommand gl152 "$SHELL -c 'case 1la in [[:alpha:]]la) echo FAIL;; *) echo OK;; esac'" 0 "OK\n" ""
75
76
77#
78# The ideas from the following tests have been taken from the "mksh" test suite
79# Thanks to Thorsten Glaser
80#
81
82$SRCROOT/conf/mkdir-sh -p 'd'
83:> "d/.bc"
84:> "d/abc"
85:> "d/bbc"
86:> "d/cbc"
87:> "d/-bc"
88#
89# Note that we need to escape ^ as the Bourne Shell implements ^ as
90# an alias for |
91#
92# glob-range-1
93# Test range matching
94#
95cat > x <<"XEOF"
96cd d
97echo [ab-]*
98echo [-ab]*
99echo [!-ab]*
100echo [!ab]*
101echo []ab]*
102:>'./!bc'
103:>'./^bc'
104echo [\^ab]*
105echo [!ab]*
106XEOF
107docommand glob00 "$SHELL ./x" 0 "\
108-bc abc bbc
109-bc abc bbc
110cbc
111-bc cbc
112abc bbc
113^bc abc bbc
114!bc -bc ^bc cbc
115" ""
116remove x
117remove "d/.bc" "d/abc" "d/bbc" "d/cbc" "d/-bc" "d/!bc" "d/^bc"
118rmdir d
119
120:>abc
121#
122# glob01 is from mksh but fails to fail as usual gmatch() or
123# fnmatch implementations check for:
124#	(c == sub || (c <= sub && sub <= d))
125# so they match if the character from the filename matches the
126# first character in the range.
127#
128#docommand glob01 "$SHELL -c 'echo [a--]*'" 0 "OK\n" ""
129
130#
131# Z is after - in ascii, so this is an illegal glob.
132#
133docommand glob02 "$SHELL -c 'echo [Z--]*'" 0 "[Z--]*\n" ""
134remove abc
135
136#
137# glob-range-3
138# ISO-8859-1 matching
139# Will not pass in Linux or Apple systems
140#:> "a�c"
141#docommand glob03 "$SHELL -c 'echo a[�-�]*'" 0 "a�c\n" ""
142#remove "a�c"
143
144:>.bc
145docommand glob04 "$SHELL -c 'echo [a.]*'" 0 "[a.]*\n" ""
146remove .bc
147
148$SRCROOT/conf/mkdir-sh -p 'd'
149:> "d/abc"
150:> "d/bbc"
151:> "d/cbc"
152:> "d/dbc"
153:> "d/ebc"
154:> "d/-bc"
155#
156# glob-range-5
157#	Results unspecified according to POSIX
158#	(AT&T gmatch() from libgen treats this like [a-cc-e]*)
159#	Our gmatch.c treats this like [a-c] + [-e], so we
160#	cannot use this as long as we may use the one or the other
161#
162cat > x <<"XEOF"
163cd d
164echo [a-c-e]*
165XEOF
166#docommand glob05 "$SHELL ./x" 0 "-bc abc bbc cbc ebc\n" ""
167remove x
168remove "d/abc" "d/bbc" "d/cbc" "d/dbc" "d/ebc" "d/-bc"
169rmdir d
170
171#
172# glob-trim-1
173# Check against a regression from fixing IFS-subst-2
174#
175cat > x <<"XEOF"
176x='#foo'
177printf "before='%s'\n" "$x"
178x=${x%%#*}
179printf "after='%s'\n" "$x"
180XEOF
181docommand glob06 "$SHELL ./x" 0 "\
182before='#foo'
183after=''
184" ""
185remove x
186
187cat > x <<"XEOF"
188XEOF
189#docommand glob01 "$SHELL ./x" 0 "" ""
190remove x
191
192success
193