xref: /titanic_41/usr/src/lib/libshell/common/tests/quoting.sh (revision 0bb073995ac5a95bd35f2dd790df1ea3d8c2d507)
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	let Errors+=1
25}
26alias err_exit='err_exit $LINENO'
27
28Command=${0##*/}
29integer Errors=0
30if	[[ 'hi there' != "hi there" ]]
31then	err_exit "single quotes not the same as double quotes"
32fi
33x='hi there'
34if	[[ $x != 'hi there' ]]
35then	err_exit "$x not the same as 'hi there'"
36fi
37if	[[ $x != "hi there" ]]
38then	err_exit "$x not the same as \"hi there \""
39fi
40if	[[ \a\b\c\*\|\"\ \\ != 'abc*|" \' ]]
41then	err_exit " \\ differs from '' "
42fi
43if	[[ "ab\'\"\$(" != 'ab\'\''"$(' ]]
44then	err_exit " \"\" differs from '' "
45fi
46if	[[ $(print -r - 'abc*|" \') !=  'abc*|" \' ]]
47then	err_exit "\$(print -r - '') differs from ''"
48fi
49if	[[ $(print -r - "abc*|\" \\") !=  'abc*|" \' ]]
50then	err_exit "\$(print -r - '') differs from ''"
51fi
52if	[[ "$(print -r - 'abc*|" \')" !=  'abc*|" \' ]]
53then	err_exit "\"\$(print -r - '')\" differs from ''"
54fi
55if	[[ "$(print -r - "abc*|\" \\")" !=  'abc*|" \' ]]
56then	err_exit "\"\$(print -r - "")\" differs from ''"
57fi
58if	[[ $(print -r - $(print -r - 'abc*|" \')) !=  'abc*|" \' ]]
59then	err_exit "nested \$(print -r - '') differs from ''"
60fi
61if	[[ "$(print -r - $(print -r - 'abc*|" \'))" !=  'abc*|" \' ]]
62then	err_exit "\"nested \$(print -r - '')\" differs from ''"
63fi
64if	[[ $(print -r - "$(print -r - 'abc*|" \')") !=  'abc*|" \' ]]
65then	err_exit "nested \"\$(print -r - '')\" differs from ''"
66fi
67unset x
68if	[[ ${x-$(print -r - "abc*|\" \\")} !=  'abc*|" \' ]]
69then	err_exit "\${x-\$(print -r - '')} differs from ''"
70fi
71if	[[ ${x-$(print -r - "a}c*|\" \\")} !=  'a}c*|" \' ]]
72then	err_exit "\${x-\$(print -r - '}')} differs from ''"
73fi
74x=$((echo foo)|(cat))
75if	[[ $x != foo  ]]
76then	err_exit "((cmd)|(cmd)) failed"
77fi
78x=$(print -r -- "\"$HOME\"")
79if	[[ $x != '"'$HOME'"' ]]
80then	err_exit "nested double quotes failed"
81fi
82: ${z="a{b}c"}
83if	[[ $z != 'a{b}c' ]]
84then	err_exit '${z="a{b}c"} not correct'
85fi
86unset z
87: "${z="a{b}c"}"
88if	[[ $z != 'a{b}c' ]]
89then	err_exit '"${z="a{b}c"}" not correct'
90fi
91if	[[ $(print -r -- "a\*b") !=  'a\*b' ]]
92then	err_exit '$(print -r -- "a\*b") differs from  a\*b'
93fi
94unset x
95if	[[ $(print -r -- "a\*b$x") !=  'a\*b' ]]
96then	err_exit '$(print -r -- "a\*b$x") differs from  a\*b'
97fi
98x=hello
99set -- ${x+foo bar bam}
100if	(( $# !=3 ))
101then	err_exit '${x+foo bar bam} does not yield three arguments'
102fi
103set -- ${x+foo "bar bam"}
104if	(( $# !=2 ))
105then	err_exit '${x+foo "bar bam"} does not yield two arguments'
106fi
107set -- ${x+foo 'bar bam'}
108if	(( $# !=2 ))
109then	err_exit '${x+foo '\''bar bam'\''} does not yield two arguments'
110fi
111set -- ${x+foo $x bam}
112if	(( $# !=3 ))
113then	err_exit '${x+foo $x bam} does not yield three arguments'
114fi
115set -- ${x+foo "$x" bam}
116if	(( $# !=3 ))
117then	err_exit '${x+foo "$x" bam} does not yield three arguments'
118fi
119set -- ${x+"foo $x bam"}
120if	(( $# !=1 ))
121then	err_exit '${x+"foo $x bam"} does not yield one argument'
122fi
123set -- "${x+foo $x bam}"
124if	(( $# !=1 ))
125then	err_exit '"${x+foo $x bam}" does not yield one argument'
126fi
127set -- ${x+foo "$x "bam}
128if	(( $# !=2 ))
129then	err_exit '${x+foo "$x "bam} does not yield two arguments'
130fi
131x="ab$'cd"
132if	[[ $x != 'ab$'"'cd" ]]
133then	err_exit '$'"' inside double quotes not working"
134fi
135x=`print 'ab$'`
136if	[[ $x != 'ab$' ]]
137then	err_exit '$'"' inside `` quotes not working"
138fi
139unset a
140x=$(print -r -- "'\
141\
142")
143if	[[ $x != "'" ]]
144then	err_exit 'line continuation in double strings not working'
145fi
146x=$(print -r -- "'\
147$a\
148")
149if	[[ $x != "'" ]]
150then	err_exit 'line continuation in expanded double strings not working'
151fi
152x='\*'
153if	[[ $(print -r -- $x) != '\*' ]]
154then	err_exit 'x="\\*";$x != \*'
155fi
156x='   hello    world    '
157set -- $x
158if	(( $# != 2 ))
159then	err_exit 'field splitting error'
160fi
161x=$(print -r -- '1234567890123456789012345678901234567890123456789012345678901234567890 \
1621234567890123456789012345678901234567890123456789012345678901234567890 \
1631234567890123456789012345678901234567890123456789012345678901234567890 \
1641234567890123456789012345678901234567890123456789012345678901234567890 \
1651234567890123456789012345678901234567890123456789012345678901234567890 \
1661234567890123456789012345678901234567890123456789012345678901234567890 \
1671234567890123456789012345678901234567890123456789012345678901234567890 \
1681234567890123456789012345678901234567890123456789012345678901234567890 \
1691234567890123456789012345678901234567890123456789012345678901234567890 \
1701234567890123456789012345678901234567890123456789012345678901234567890 \
1711234567890123456789012345678901234567890123456789012345678901234567890 \
1721234567890123456789012345678901234567890123456789012345678901234567890 \
1731234567890123456789012345678901234567890123456789012345678901234567890 \
1741234567890123456789012345678901234567890123456789012345678901234567890 \
1751234567890123456789012345678901234567890123456789012345678901234567890')
176if	(( ${#x} != (15*73-3) ))
177then	err_exit "length of x, ${#x}, is incorrect should be $((15*73-3))"
178fi
179x='$hi'
180if	[[ $x\$ != '$hi$' ]]
181then	err_exit ' $x\$, with x=$hi, does not expand to $hi$'
182fi
183if	[[ $x$ != '$hi$' ]]
184then	err_exit ' $x$, with x=$hi, does not expand to $hi$'
185fi
186set -- $(/bin/echo foo;sleep 1;/bin/echo bar)
187if	[[ $# != 2 ]]
188then	err_exit 'word splitting after command substitution not working'
189fi
190unset q
191if	[[ "${q:+'}q${q:+'}" != q ]]
192then	err_exit 'expansion of "{q:+'\''}" not correct when q unset'
193fi
194q=1
195if	[[ "${q:+'}q${q:+'}" != "'q'" ]]
196then	err_exit 'expansion of "{q:+'\''}" not correct when q set'
197fi
198x=$'x\' #y'
199if	[[ $x != "x' #y" ]]
200then	err_exit "$'x\' #y'" not working
201fi
202x=$q$'x\' #y'
203if	[[ $x != "1x' #y" ]]
204then	err_exit "$q$'x\' #y'" not working
205fi
206IFS=,
207x='a,b\,c,d'
208set -- $x
209if	[[ $2 != 'b\' ]]
210then	err_exit "field splitting of $x with IFS=$IFS not working"
211fi
212foo=bar
213bar=$(print -r -- ${foo+\\n\ })
214if	[[ $bar != '\n ' ]]
215then	err_exit '${foo+\\n\ } expansion error'
216fi
217unset bar
218bar=$(print -r -- ${foo+\\n\ $bar})
219if	[[ $bar != '\n ' ]]
220then	err_exit '${foo+\\n\ $bar} expansion error with bar unset'
221fi
222x='\\(..\\)|&\|\|\\&\\|'
223if	[[ $(print -r -- $x) != "$x" ]]
224then	err_exit '$x, where x=\\(..\\)|&\|\|\\&\\| not working'
225fi
226x='\\('
227if	[[ $(print -r -- a${x}b) != a"${x}"b ]]
228then	err_exit 'a${x}b, where x=\\( not working'
229fi
230x=
231if	[[ $(print -r -- $x'\\1') != '\\1' ]]
232then	err_exit 'backreference inside single quotes broken'
233fi
234set -- ''
235set -- "$@"
236if	(( $# != 1 ))
237then	err_exit '"$@" not preserving nulls'
238fi
239x=
240if	[[ $(print -r s"!\2${x}\1\a!") != 's!\2\1\a!' ]]
241then	err_exit  'print -r s"!\2${x}\1\a!" not equal s!\2\1\a!'
242fi
243if	[[ $(print -r $'foo\n\n\n') != foo ]]
244then	err_exit 'trailing newlines on comsubstitution not removed'
245fi
246unset x
247if	[[ ${x:='//'} != '//' ]]
248then	err_exit '${x:='//'} != "//"'
249fi
250if	[[ $(print -r "\"hi$\"")	!= '"hi$"' ]]
251then	err_exit '$\ not correct inside ""'
252fi
253unset x
254if	[[ "${x-a\}b}" != 'a}b' ]]
255then	err_exit '"${x-a\}b}" !=  "a}b"'
256fi
257if	[[ "\}\]$x\*\{\[\\" != '\}\]\*\{\[\' ]]
258then	err_exit '"\}\]$x\*\{\[\\" !=  "\}\]\*\{\[\"'
259fi
260foo=yes
261if	[[ $(print -r -- {\$foo}) != '{$foo}' ]]
262then	err_exit '{\$foo}' not expanded correctly
263fi
264[[ foo == $(
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###########################################################
303print foo) ]] || err_exit "command subsitution with long comments broken"
304subject='some/other/words'
305re='(?*)/(?*)/(?*)'
306[[ ${subject/${re}/\3} != words ]] && err_exit 'string replacement with \3 not working'
307[[ ${subject/${re}/'\3'} != '\3' ]] && err_exit 'string replacement with '"'\3'"' not working'
308[[ ${subject/${re}/"\\3"} != '\3' ]] && err_exit 'string replacement with "\\3" not working'
309[[ ${subject/${re}/"\3"} != '\3' ]] && err_exit 'string replacement with "\3" not working'
310string='\3'
311[[ ${subject/${re}/${string}} != words ]] && err_exit 'string replacement with $string not working with string=\3'
312[[ $(print -r "${subject/${re}/${string}}") != words ]] && err_exit 'string replacement with $string not working with string=\3 using print'
313[[ ${subject/${re}/"${string}"} != '\3' ]] && err_exit 'string replacement with "$string" not working with  string=\3'
314[[ $(print -r "${subject/${re}/"${string}"}") != '\3' ]] && err_exit 'string replacement with "$string" not working with  string=\3 using print'
315string='\\3'
316[[ ${subject/${re}/${string}} != '\3' ]] && err_exit 'string replacement with $string not working with string=\\3'
317[[ ${subject/${re}/"${string}"} != '\\3' ]] && err_exit 'string replacement with "$string" not working with  string=\\3'
318[[ ${subject/${re}/\4} != '\4' ]] && err_exit 'string replacement with \4 not working'
319[[ ${subject/${re}/'\4'} != '\4' ]] && err_exit 'string replacement with '\4' not working'
320string='\4'
321[[ ${subject/${re}/${string}} != '\4' ]] && err_exit 'string replacement with $string not working with string=\4'
322[[ ${subject/${re}/"${string}"} != '\4' ]] && err_exit 'string replacement with "$string" not working with  string=\4'
323string='&foo'
324[[ ${subject/${re}/${string}} != '&foo' ]] && err_exit 'string replacement with $string not working with string=&foo'
325[[ ${subject/${re}/"${string}"} != '&foo' ]] && err_exit 'string replacement with "$string" not working with  string=&foo'
326{
327x=x
328x=${x:-`id | sed 's/^[^(]*(\([^)]*\)).*/\1/'`}
329} 2> /dev/null || err_exit 'skipping over `` failed'
330[[ $x == x ]] || err_exit 'assignment ${x:=`...`} failed'
331exit $((Errors))
332