1*11be35a1SLionel Sambuc# $NetBSD: t_fsplit.sh,v 1.1 2012/03/17 16:33:11 jruoho Exp $ 2*11be35a1SLionel Sambuc# 3*11be35a1SLionel Sambuc# Copyright (c) 2007 The NetBSD Foundation, Inc. 4*11be35a1SLionel Sambuc# All rights reserved. 5*11be35a1SLionel Sambuc# 6*11be35a1SLionel Sambuc# Redistribution and use in source and binary forms, with or without 7*11be35a1SLionel Sambuc# modification, are permitted provided that the following conditions 8*11be35a1SLionel Sambuc# are met: 9*11be35a1SLionel Sambuc# 1. Redistributions of source code must retain the above copyright 10*11be35a1SLionel Sambuc# notice, this list of conditions and the following disclaimer. 11*11be35a1SLionel Sambuc# 2. Redistributions in binary form must reproduce the above copyright 12*11be35a1SLionel Sambuc# notice, this list of conditions and the following disclaimer in the 13*11be35a1SLionel Sambuc# documentation and/or other materials provided with the distribution. 14*11be35a1SLionel Sambuc# 15*11be35a1SLionel Sambuc# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16*11be35a1SLionel Sambuc# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17*11be35a1SLionel Sambuc# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18*11be35a1SLionel Sambuc# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19*11be35a1SLionel Sambuc# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20*11be35a1SLionel Sambuc# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21*11be35a1SLionel Sambuc# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22*11be35a1SLionel Sambuc# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23*11be35a1SLionel Sambuc# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24*11be35a1SLionel Sambuc# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25*11be35a1SLionel Sambuc# POSSIBILITY OF SUCH DAMAGE. 26*11be35a1SLionel Sambuc# 27*11be35a1SLionel Sambuc 28*11be35a1SLionel Sambuc# The standard 29*11be35a1SLionel Sambuc# http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html 30*11be35a1SLionel Sambuc# explains (section 2.6) that Field splitting should be performed on the 31*11be35a1SLionel Sambuc# result of variable expansions. 32*11be35a1SLionel Sambuc# In particular this means that in ${x-word}, 'word' must be expanded as if 33*11be35a1SLionel Sambuc# the "${x-" and "}" were absent from the input line. 34*11be35a1SLionel Sambuc# 35*11be35a1SLionel Sambuc# So: sh -c 'set ${x-a b c}; echo $#' should give 3. 36*11be35a1SLionel Sambuc# 37*11be35a1SLionel Sambuc 38*11be35a1SLionel Sambucnl=' 39*11be35a1SLionel Sambuc' 40*11be35a1SLionel Sambuc 41*11be35a1SLionel Sambuccheck() 42*11be35a1SLionel Sambuc{ 43*11be35a1SLionel Sambuc result="$(eval $1)" 44*11be35a1SLionel Sambuc # Remove newlines 45*11be35a1SLionel Sambuc oifs="$IFS" 46*11be35a1SLionel Sambuc IFS="$nl" 47*11be35a1SLionel Sambuc result="$(echo $result)" 48*11be35a1SLionel Sambuc IFS="$oifs" 49*11be35a1SLionel Sambuc if [ "$2" != "$result" ] 50*11be35a1SLionel Sambuc then 51*11be35a1SLionel Sambuc atf_fail "expected [$2], found [$result]" 52*11be35a1SLionel Sambuc fi 53*11be35a1SLionel Sambuc} 54*11be35a1SLionel Sambuc 55*11be35a1SLionel Sambucatf_test_case for 56*11be35a1SLionel Sambucfor_head() { 57*11be35a1SLionel Sambuc atf_set "descr" "Checks field splitting in for loops" 58*11be35a1SLionel Sambuc} 59*11be35a1SLionel Sambucfor_body() { 60*11be35a1SLionel Sambuc unset x 61*11be35a1SLionel Sambuc 62*11be35a1SLionel Sambuc # Since I managed to break this, leave the test in 63*11be35a1SLionel Sambuc check 'for f in $x; do echo x${f}y; done' '' 64*11be35a1SLionel Sambuc} 65*11be35a1SLionel Sambuc 66*11be35a1SLionel Sambucatf_test_case default_val 67*11be35a1SLionel Sambucdefault_val_head() { 68*11be35a1SLionel Sambuc atf_set "descr" "Checks field splitting in variable default values" 69*11be35a1SLionel Sambuc} 70*11be35a1SLionel Sambucdefault_val_body() { 71*11be35a1SLionel Sambuc unset x 72*11be35a1SLionel Sambuc 73*11be35a1SLionel Sambuc # Check that IFS is applied to text from ${x-...} unless it is inside 74*11be35a1SLionel Sambuc # any set of "..." 75*11be35a1SLionel Sambuc check 'set ${x-a b c}; echo $#' 3 76*11be35a1SLionel Sambuc check 'for i in ${x-a b c}; do echo "z${i}z"; done' 'zaz zbz zcz' 77*11be35a1SLionel Sambuc check 'for i in ${x-"a b" c}; do echo "z${i}z"; done' 'za bz zcz' 78*11be35a1SLionel Sambuc check 'for i in ${x-"a ${x-b c}" d}; do echo "z${i}z"; done' 'za b cz zdz' 79*11be35a1SLionel Sambuc check 'for i in ${x-"a ${x-"b c"}" d}; do echo "z${i}z"; done' 'za b cz zdz' 80*11be35a1SLionel Sambuc check 'for i in ${x-a ${x-"b c"} d}; do echo "z${i}z"; done' 'zaz zb cz zdz' 81*11be35a1SLionel Sambuc check 'for i in ${x-a ${x-b c} d}; do echo "z${i}z"; done' 'zaz zbz zcz zdz' 82*11be35a1SLionel Sambuc} 83*11be35a1SLionel Sambuc 84*11be35a1SLionel Sambucatf_test_case ifs_alpha 85*11be35a1SLionel Sambucifs_alpha_head() { 86*11be35a1SLionel Sambuc atf_set "descr" "Checks that field splitting works with alphabetic" \ 87*11be35a1SLionel Sambuc "characters" 88*11be35a1SLionel Sambuc} 89*11be35a1SLionel Sambucifs_alpha_body() { 90*11be35a1SLionel Sambuc unset x 91*11be35a1SLionel Sambuc 92*11be35a1SLionel Sambuc # repeat with an alphabetic in IFS 93*11be35a1SLionel Sambuc check 'IFS=q; set ${x-aqbqc}; echo $#' 3 94*11be35a1SLionel Sambuc check 'IFS=q; for i in ${x-aqbqc}; do echo "z${i}z"; done' 'zaz zbz zcz' 95*11be35a1SLionel Sambuc check 'IFS=q; for i in ${x-"aqb"qc}; do echo "z${i}z"; done' 'zaqbz zcz' 96*11be35a1SLionel Sambuc check 'IFS=q; for i in ${x-"aq${x-bqc}"qd}; do echo "z${i}z"; done' 'zaqbqcz zdz' 97*11be35a1SLionel Sambuc check 'IFS=q; for i in ${x-"aq${x-"bqc"}"qd}; do echo "z${i}z"; done' 'zaqbqcz zdz' 98*11be35a1SLionel Sambuc check 'IFS=q; for i in ${x-aq${x-"bqc"}qd}; do echo "z${i}z"; done' 'zaz zbqcz zdz' 99*11be35a1SLionel Sambuc} 100*11be35a1SLionel Sambuc 101*11be35a1SLionel Sambucatf_test_case quote 102*11be35a1SLionel Sambucquote_head() { 103*11be35a1SLionel Sambuc atf_set "descr" "Checks that field splitting works with multi-word" \ 104*11be35a1SLionel Sambuc "fields" 105*11be35a1SLionel Sambuc} 106*11be35a1SLionel Sambucquote_body() { 107*11be35a1SLionel Sambuc unset x 108*11be35a1SLionel Sambuc 109*11be35a1SLionel Sambuc # Some quote propagation checks 110*11be35a1SLionel Sambuc check 'set "${x-a b c}"; echo $#' 1 111*11be35a1SLionel Sambuc check 'set "${x-"a b" c}"; echo $1' 'a b c' 112*11be35a1SLionel Sambuc check 'for i in "${x-a b c}"; do echo "z${i}z"; done' 'za b cz' 113*11be35a1SLionel Sambuc} 114*11be35a1SLionel Sambuc 115*11be35a1SLionel Sambucatf_test_case dollar_at 116*11be35a1SLionel Sambucdollar_at_head() { 117*11be35a1SLionel Sambuc atf_set "descr" "Checks that field splitting works when expanding" \ 118*11be35a1SLionel Sambuc "\$@" 119*11be35a1SLionel Sambuc} 120*11be35a1SLionel Sambucdollar_at_body() { 121*11be35a1SLionel Sambuc unset x 122*11be35a1SLionel Sambuc 123*11be35a1SLionel Sambuc # Check we get "$@" right 124*11be35a1SLionel Sambuc check 'set ""; for i; do echo "z${i}z"; done' 'zz' 125*11be35a1SLionel Sambuc check 'set ""; for i in "$@"; do echo "z${i}z"; done' 'zz' 126*11be35a1SLionel Sambuc check 'set "" ""; for i; do echo "z${i}z"; done' 'zz zz' 127*11be35a1SLionel Sambuc check 'set "" ""; for i in "$@"; do echo "z${i}z"; done' 'zz zz' 128*11be35a1SLionel Sambuc check 'set "" ""; for i in $@; do echo "z${i}z"; done' '' 129*11be35a1SLionel Sambuc check 'set "a b" c; for i; do echo "z${i}z"; done' 'za bz zcz' 130*11be35a1SLionel Sambuc check 'set "a b" c; for i in "$@"; do echo "z${i}z"; done' 'za bz zcz' 131*11be35a1SLionel Sambuc check 'set "a b" c; for i in $@; do echo "z${i}z"; done' 'zaz zbz zcz' 132*11be35a1SLionel Sambuc check 'set " a b " c; for i in "$@"; do echo "z${i}z"; done' 'z a b z zcz' 133*11be35a1SLionel Sambuc check 'set --; for i in x"$@"x; do echo "z${i}z"; done' 'zxxz' 134*11be35a1SLionel Sambuc check 'set a; for i in x"$@"x; do echo "z${i}z"; done' 'zxaxz' 135*11be35a1SLionel Sambuc check 'set a b; for i in x"$@"x; do echo "z${i}z"; done' 'zxaz zbxz' 136*11be35a1SLionel Sambuc} 137*11be35a1SLionel Sambuc 138*11be35a1SLionel Sambucatf_test_case ifs 139*11be35a1SLionel Sambucifs_head() { 140*11be35a1SLionel Sambuc atf_set "descr" "Checks that IFS correctly configures field" \ 141*11be35a1SLionel Sambuc "splitting behavior" 142*11be35a1SLionel Sambuc} 143*11be35a1SLionel Sambucifs_body() { 144*11be35a1SLionel Sambuc unset x 145*11be35a1SLionel Sambuc 146*11be35a1SLionel Sambuc # Some IFS tests 147*11be35a1SLionel Sambuc check 't="-- "; IFS=" "; set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '0' 148*11be35a1SLionel Sambuc check 't=" x"; IFS=" x"; set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '1' 149*11be35a1SLionel Sambuc check 't=" x "; IFS=" x"; set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '1' 150*11be35a1SLionel Sambuc check 't=axb; IFS="x"; set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '2 a:b' 151*11be35a1SLionel Sambuc check 't="a x b"; IFS="x"; set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '2 a : b' 152*11be35a1SLionel Sambuc check 't="a xx b"; IFS="x"; set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '3 a :: b' 153*11be35a1SLionel Sambuc check 't="a xx b"; IFS="x "; set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '3 a::b' 154*11be35a1SLionel Sambuc # A recent 'clarification' means that a single trailing IFS non-whitespace 155*11be35a1SLionel Sambuc # doesn't generate an empty parameter 156*11be35a1SLionel Sambuc check 't="xax"; IFS="x"; set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '2 :a' 157*11be35a1SLionel Sambuc check 't="xax "; IFS="x "; set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '2 :a' 158*11be35a1SLionel Sambuc # Verify that IFS isn't being applied where it shouldn't be. 159*11be35a1SLionel Sambuc check 'IFS="x"; set axb; IFS=":"; r="$*"; IFS=; echo $# $r' '1 axb' 160*11be35a1SLionel Sambuc} 161*11be35a1SLionel Sambuc 162*11be35a1SLionel Sambucatf_test_case var_length 163*11be35a1SLionel Sambucvar_length_head() { 164*11be35a1SLionel Sambuc atf_set "descr" "Checks that field splitting works when expanding" \ 165*11be35a1SLionel Sambuc "a variable's length" 166*11be35a1SLionel Sambuc} 167*11be35a1SLionel Sambucvar_length_body() { 168*11be35a1SLionel Sambuc unset x 169*11be35a1SLionel Sambuc 170*11be35a1SLionel Sambuc # Check that we apply IFS to ${#var} 171*11be35a1SLionel Sambuc long=12345678123456781234567812345678 172*11be35a1SLionel Sambuc long=$long$long$long$long 173*11be35a1SLionel Sambuc check 'echo ${#long}; IFS=2; echo ${#long}; set 1 ${#long};echo $#' '128 1 8 3' 174*11be35a1SLionel Sambuc check 'IFS=2; set ${x-${#long}}; IFS=" "; echo $* $#' '1 8 2' 175*11be35a1SLionel Sambuc check 'IFS=2; set ${x-"${#long}"}; IFS=" "; echo $* $#' '128 1' 176*11be35a1SLionel Sambuc} 177*11be35a1SLionel Sambuc 178*11be35a1SLionel Sambucatf_init_test_cases() { 179*11be35a1SLionel Sambuc atf_add_test_case for 180*11be35a1SLionel Sambuc atf_add_test_case default_val 181*11be35a1SLionel Sambuc atf_add_test_case ifs_alpha 182*11be35a1SLionel Sambuc atf_add_test_case quote 183*11be35a1SLionel Sambuc atf_add_test_case dollar_at 184*11be35a1SLionel Sambuc atf_add_test_case ifs 185*11be35a1SLionel Sambuc atf_add_test_case var_length 186*11be35a1SLionel Sambuc} 187