summaryrefslogtreecommitdiff
path: root/src/cmd/ksh93/tests/quoting.sh
blob: 01aae0dc1510c037d43562fb5fb85e79f993377b (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
########################################################################
#                                                                      #
#               This software is part of the ast package               #
#          Copyright (c) 1982-2011 AT&T Intellectual Property          #
#                      and is licensed under the                       #
#                 Eclipse Public License, Version 1.0                  #
#                    by AT&T Intellectual Property                     #
#                                                                      #
#                A copy of the License is available at                 #
#          http://www.eclipse.org/org/documents/epl-v10.html           #
#         (with md5 checksum b35adb5213ca9657e911e9befb180842)         #
#                                                                      #
#              Information and Software Systems Research               #
#                            AT&T Research                             #
#                           Florham Park NJ                            #
#                                                                      #
#                  David Korn <dgk@research.att.com>                   #
#                                                                      #
########################################################################
function err_exit
{
	print -u2 -n "\t"
	print -u2 -r ${Command}[$1]: "${@:2}"
	(( Errors++ ))
}
alias err_exit='err_exit $LINENO'

Command=${0##*/}
integer Errors=0
if	[[ 'hi there' != "hi there" ]]
then	err_exit "single quotes not the same as double quotes"
fi
x='hi there'
if	[[ $x != 'hi there' ]]
then	err_exit "$x not the same as 'hi there'"
fi
if	[[ $x != "hi there" ]]
then	err_exit "$x not the same as \"hi there \""
fi
if	[[ \a\b\c\*\|\"\ \\ != 'abc*|" \' ]]
then	err_exit " \\ differs from '' "
fi
if	[[ "ab\'\"\$(" != 'ab\'\''"$(' ]]
then	err_exit " \"\" differs from '' "
fi
if	[[ $(print -r - 'abc*|" \') !=  'abc*|" \' ]]
then	err_exit "\$(print -r - '') differs from ''"
fi
if	[[ $(print -r - "abc*|\" \\") !=  'abc*|" \' ]]
then	err_exit "\$(print -r - '') differs from ''"
fi
if	[[ "$(print -r - 'abc*|" \')" !=  'abc*|" \' ]]
then	err_exit "\"\$(print -r - '')\" differs from ''"
fi
if	[[ "$(print -r - "abc*|\" \\")" !=  'abc*|" \' ]]
then	err_exit "\"\$(print -r - "")\" differs from ''"
fi
if	[[ $(print -r - $(print -r - 'abc*|" \')) !=  'abc*|" \' ]]
then	err_exit "nested \$(print -r - '') differs from ''"
fi
if	[[ "$(print -r - $(print -r - 'abc*|" \'))" !=  'abc*|" \' ]]
then	err_exit "\"nested \$(print -r - '')\" differs from ''"
fi
if	[[ $(print -r - "$(print -r - 'abc*|" \')") !=  'abc*|" \' ]]
then	err_exit "nested \"\$(print -r - '')\" differs from ''"
fi
unset x
if	[[ ${x-$(print -r - "abc*|\" \\")} !=  'abc*|" \' ]]
then	err_exit "\${x-\$(print -r - '')} differs from ''"
fi
if	[[ ${x-$(print -r - "a}c*|\" \\")} !=  'a}c*|" \' ]]
then	err_exit "\${x-\$(print -r - '}')} differs from ''"
fi
x=$((echo foo)|(cat))
if	[[ $x != foo  ]]
then	err_exit "((cmd)|(cmd)) failed"
fi
x=$(print -r -- "\"$HOME\"")
if	[[ $x != '"'$HOME'"' ]]
then	err_exit "nested double quotes failed"
fi
unset z
: ${z="a{b}c"}
if	[[ $z != 'a{b}c' ]]
then	err_exit '${z="a{b}c"} not correct'
fi
unset z
: "${z="a{b}c"}"
if	[[ $z != 'a{b}c' ]]
then	err_exit '"${z="a{b}c"}" not correct'
fi
if	[[ $(print -r -- "a\*b") !=  'a\*b' ]]
then	err_exit '$(print -r -- "a\*b") differs from  a\*b'
fi
unset x
if	[[ $(print -r -- "a\*b$x") !=  'a\*b' ]]
then	err_exit '$(print -r -- "a\*b$x") differs from  a\*b'
fi
x=hello
set -- ${x+foo bar bam}
if	(( $# !=3 ))
then	err_exit '${x+foo bar bam} does not yield three arguments'
fi
set -- ${x+foo "bar bam"}
if	(( $# !=2 ))
then	err_exit '${x+foo "bar bam"} does not yield two arguments'
fi
set -- ${x+foo 'bar bam'}
if	(( $# !=2 ))
then	err_exit '${x+foo '\''bar bam'\''} does not yield two arguments'
fi
set -- ${x+foo $x bam}
if	(( $# !=3 ))
then	err_exit '${x+foo $x bam} does not yield three arguments'
fi
set -- ${x+foo "$x" bam}
if	(( $# !=3 ))
then	err_exit '${x+foo "$x" bam} does not yield three arguments'
fi
set -- ${x+"foo $x bam"}
if	(( $# !=1 ))
then	err_exit '${x+"foo $x bam"} does not yield one argument'
fi
set -- "${x+foo $x bam}"
if	(( $# !=1 ))
then	err_exit '"${x+foo $x bam}" does not yield one argument'
fi
set -- ${x+foo "$x "bam}
if	(( $# !=2 ))
then	err_exit '${x+foo "$x "bam} does not yield two arguments'
fi
x="ab$'cd"
if	[[ $x != 'ab$'"'cd" ]]
then	err_exit '$'"' inside double quotes not working"
fi
x=`print 'ab$'`
if	[[ $x != 'ab$' ]]
then	err_exit '$'"' inside `` quotes not working"
fi
unset a
x=$(print -r -- "'\
\
")
if	[[ $x != "'" ]]
then	err_exit 'line continuation in double strings not working'
fi
x=$(print -r -- "'\
$a\
")
if	[[ $x != "'" ]]
then	err_exit 'line continuation in expanded double strings not working'
fi
x='\*'
if	[[ $(print -r -- $x) != '\*' ]]
then	err_exit 'x="\\*";$x != \*'
fi
x='   hello    world    '
set -- $x
if	(( $# != 2 ))
then	err_exit 'field splitting error'
fi
x=$(print -r -- '1234567890123456789012345678901234567890123456789012345678901234567890 \
1234567890123456789012345678901234567890123456789012345678901234567890 \
1234567890123456789012345678901234567890123456789012345678901234567890 \
1234567890123456789012345678901234567890123456789012345678901234567890 \
1234567890123456789012345678901234567890123456789012345678901234567890 \
1234567890123456789012345678901234567890123456789012345678901234567890 \
1234567890123456789012345678901234567890123456789012345678901234567890 \
1234567890123456789012345678901234567890123456789012345678901234567890 \
1234567890123456789012345678901234567890123456789012345678901234567890 \
1234567890123456789012345678901234567890123456789012345678901234567890 \
1234567890123456789012345678901234567890123456789012345678901234567890 \
1234567890123456789012345678901234567890123456789012345678901234567890 \
1234567890123456789012345678901234567890123456789012345678901234567890 \
1234567890123456789012345678901234567890123456789012345678901234567890 \
1234567890123456789012345678901234567890123456789012345678901234567890')
if	(( ${#x} != (15*73-3) ))
then	err_exit "length of x, ${#x}, is incorrect should be $((15*73-3))"
fi
x='$hi'
if	[[ $x\$ != '$hi$' ]]
then	err_exit ' $x\$, with x=$hi, does not expand to $hi$'
fi
if	[[ $x$ != '$hi$' ]]
then	err_exit ' $x$, with x=$hi, does not expand to $hi$'
fi
set -- $(/bin/echo foo;sleep 1;/bin/echo bar)
if	[[ $# != 2 ]]
then	err_exit 'word splitting after command substitution not working'
fi
unset q
if	[[ "${q:+'}q${q:+'}" != q ]]
then	err_exit 'expansion of "{q:+'\''}" not correct when q unset'
fi
q=1
if	[[ "${q:+'}q${q:+'}" != "'q'" ]]
then	err_exit 'expansion of "{q:+'\''}" not correct when q set'
fi
x=$'x\' #y'
if	[[ $x != "x' #y" ]]
then	err_exit "$'x\' #y'" not working
fi
x=$q$'x\' #y'
if	[[ $x != "1x' #y" ]]
then	err_exit "$q$'x\' #y'" not working
fi
IFS=,
x='a,b\,c,d'
set -- $x
if	[[ $2 != 'b\' ]]
then	err_exit "field splitting of $x with IFS=$IFS not working"
fi
foo=bar
bar=$(print -r -- ${foo+\\n\ })
if	[[ $bar != '\n ' ]]
then	err_exit '${foo+\\n\ } expansion error'
fi
unset bar
bar=$(print -r -- ${foo+\\n\ $bar})
if	[[ $bar != '\n ' ]]
then	err_exit '${foo+\\n\ $bar} expansion error with bar unset'
fi
x='\\(..\\)|&\|\|\\&\\|'
if	[[ $(print -r -- $x) != "$x" ]]
then	err_exit '$x, where x=\\(..\\)|&\|\|\\&\\| not working'
fi
x='\\('
if	[[ $(print -r -- a${x}b) != a"${x}"b ]]
then	err_exit 'a${x}b, where x=\\( not working'
fi
x=
if	[[ $(print -r -- $x'\\1') != '\\1' ]]
then	err_exit 'backreference inside single quotes broken'
fi
set -- ''
set -- "$@"
if	(( $# != 1 ))
then	err_exit '"$@" not preserving nulls'
fi
x=
if	[[ $(print -r s"!\2${x}\1\a!") != 's!\2\1\a!' ]]
then	err_exit  'print -r s"!\2${x}\1\a!" not equal s!\2\1\a!'
fi
if	[[ $(print -r $'foo\n\n\n') != foo ]]
then	err_exit 'trailing newlines on comsubstitution not removed'
fi
unset x
if	[[ ${x:='//'} != '//' ]]
then	err_exit '${x:='//'} != "//"'
fi
if	[[ $(print -r "\"hi$\"")	!= '"hi$"' ]]
then	err_exit '$\ not correct inside ""'
fi
unset x
if	[[ "${x-a\}b}" != 'a}b' ]]
then	err_exit '"${x-a\}b}" !=  "a}b"'
fi
if	[[ "\}\]$x\*\{\[\\" != '\}\]\*\{\[\' ]]
then	err_exit '"\}\]$x\*\{\[\\" !=  "\}\]\*\{\[\"'
fi
foo=yes
if	[[ $(print -r -- {\$foo}) != '{$foo}' ]]
then	err_exit '{\$foo}' not expanded correctly
fi
[[ foo == $(
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
###########################################################
print foo) ]] || err_exit "command subsitution with long comments broken"
subject='some/other/words'
re='@(?*)/@(?*)/@(?*)'
[[ ${subject/${re}/\3} != words ]] && err_exit 'string replacement with \3 not working'
[[ ${subject/${re}/'\3'} != '\3' ]] && err_exit 'string replacement with '"'\3'"' not working'
[[ ${subject/${re}/"\\3"} != '\3' ]] && err_exit 'string replacement with "\\3" not working'
[[ ${subject/${re}/"\3"} != '\3' ]] && err_exit 'string replacement with "\3" not working'
string='\3'
[[ ${subject/${re}/${string}} != words ]] && err_exit 'string replacement with $string not working with string=\3'
[[ $(print -r "${subject/${re}/${string}}") != words ]] && err_exit 'string replacement with $string not working with string=\3 using print'
[[ ${subject/${re}/"${string}"} != '\3' ]] && err_exit 'string replacement with "$string" not working with  string=\3'
[[ $(print -r "${subject/${re}/"${string}"}") != '\3' ]] && err_exit 'string replacement with "$string" not working with  string=\3 using print'
string='\\3'
[[ ${subject/${re}/${string}} != '\3' ]] && err_exit 'string replacement with $string not working with string=\\3'
[[ ${subject/${re}/"${string}"} != '\\3' ]] && err_exit 'string replacement with "$string" not working with  string=\\3'
[[ ${subject/${re}/\4} != '\4' ]] && err_exit 'string replacement with \4 not working'
[[ ${subject/${re}/'\4'} != '\4' ]] && err_exit 'string replacement with '\4' not working'
string='\4'
[[ ${subject/${re}/${string}} != '\4' ]] && err_exit 'string replacement with $string not working with string=\4'
[[ ${subject/${re}/"${string}"} != '\4' ]] && err_exit 'string replacement with "$string" not working with  string=\4'
string='&foo'
[[ ${subject/${re}/${string}} != '&foo' ]] && err_exit 'string replacement with $string not working with string=&foo'
[[ ${subject/${re}/"${string}"} != '&foo' ]] && err_exit 'string replacement with "$string" not working with  string=&foo'
{
x=x
x=${x:-`id | sed 's/^[^(]*(\([^)]*\)).*/\1/'`}
} 2> /dev/null || err_exit 'skipping over `` failed'
[[ $x == x ]] || err_exit 'assignment ${x:=`...`} failed'
[[ $($SHELL -c 'print a[') == 'a[' ]] || err_exit "unbalanced '[' in command arg fails"
$SHELL -c $'false && (( `wc -l /dev/null | nawk \'{print $1}\'` > 2 )) && true;:' 2> /dev/null ||  err_exit 'syntax error with ` in arithmetic expression'
{ $SHELL  -c '((  1`: "{ }"` ))' ;} 2> /dev/null || err_exit 'problem with ` inside (())'
varname=foobarx
x=`print '"\$'${varname}'"'`
[[ $x == '"$foobarx"' ]] ||  err_exit $'\\$\' not handled correctly inside ``'

copy1=5 copynum=1
foo="`eval echo "$"{copy$copynum"-0}"`"
[[ $foo == "$copy1" ]] || err_exit '$"..." not being ignored inside ``'

[[ $($SHELL -c 'set --  ${1+"$@"}; print $#' cmd '') == 1 ]] || err_exit '${1+"$@"} with one empty argument fails'
[[ $($SHELL -c 'set --  ${1+"$@"}; print $#' cmd foo '') == 2 ]] || err_exit '${1+"$@"} with one non-empty and on empty argument fails'
[[ $($SHELL -c 'set --  ${1+"$@"}; print $#' cmd "" '') == 2 ]] || err_exit '${1+"$@"} with two empty arguments fails'
[[ $($SHELL -c 'set --  ${1+"$@"}; print $#' cmd "" '' '') == 3 ]] || err_exit '${1+"$@"} with three empty arguments fails'
[[ $($SHELL -c 'set --  "$@"; print $#' cmd '') == 1 ]] || err_exit '"$@" with one empty argument fails'
[[ $($SHELL -c 'set --  "${@:2}"; print $#' cmd '') == 0 ]] || err_exit '"$@" with one empty argument fails'
[[ $($SHELL -c 'set --  "$@"; print $#' cmd foo '') == 2 ]] || err_exit '"$@" with one non-empty and on empty argument fails'
[[ $($SHELL -c 'set --  "$@"; print $#' cmd "" '') == 2 ]] || err_exit '"$@" with two empty arguments fails'
[[ $($SHELL -c 'set --  "$@"; print $#' cmd "" '' '') == 3 ]] || err_exit '"$@" with three empty arguments fails'
args=('')
set -- "${args[@]}"
[[ $# == 1 ]] || err_exit '"${args[@]}"} with one empty argument fails'
set -- ${1+"${args[@]}"}
[[ $# == 1 ]] || err_exit '${1+"${args[@]}"} with one empty argument fails'
args=(foo '')
set -- "${args[@]}"
[[ $# == 2 ]] || err_exit '"${args[@]}"} with one non-empty and one empty argument fails'
set -- ${1+"${args[@]}"}
[[ $# == 2 ]] || err_exit '${1+"${args[@]}"} with one non-empty and one empty argument fails'

unset ARGS
set --
ARGS=("$@")
set -- "${ARGS[@]}"
(( $# )) &&  err_exit 'set -- "${ARGS[@]}" for empty array should not produce arguments'

exit $((Errors<125?Errors:125))