1 # $NetBSD: t_builtin.sh,v 1.5 2019/07/21 15:00:18 kre Exp $ 2 # 3 # Copyright (c) 2018 The NetBSD Foundation, Inc. 4 # All rights reserved. 5 # 6 # Redistribution and use in source and binary forms, with or without 7 # modification, are permitted provided that the following conditions 8 # are met: 9 # 1. Redistributions of source code must retain the above copyright 10 # notice, this list of conditions and the following disclaimer. 11 # 2. Redistributions in binary form must reproduce the above copyright 12 # notice, this list of conditions and the following disclaimer in the 13 # documentation and/or other materials provided with the distribution. 14 # 15 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 # POSSIBILITY OF SUCH DAMAGE. 26 # 27 28 # The shell to use for tests of the builtin printf (or at least try) 29 : ${TEST_SH:=/bin/sh} 30 31 # This tests the builtin printf command in ${TEST_SH} 32 33 # For the actual code/tests see printf.sh 34 # (shared with tests for the external (filesystem) command printf 35 36 # These tests are designed to be able to be run by ATF, or standalone 37 # 38 # That is, either 39 # atf_run t_builtin | atf-report (or whatever is needed) 40 # or 41 # sh t_builtin [sub_test]... (default is to run all sub_tests) 42 # 43 # nb: for standalone runs, do not attempt ./t_builtin as the #! line 44 # added will force ATF which will complain about the test being run 45 # in the wrong way. Instead use some Bourne shell compatible shell 46 # (any will do, caveat any bugs it might have, it need not be TEST_SH, 47 # but it can be) and run the script explicitly. 48 49 do_printf() 50 { 51 $Running_under_ATF && atf_require_prog "${TEST_SH%% *}" 52 53 unset LANG LC_ALL LC_NUMERIC LC_CTYPE LC_MESSAGES 54 55 case "$*" in 56 *\'*) 57 $Running_under_ATF && atf_require_prog printf 58 $Running_under_ATF && atf_require_prog sed 59 ;; 60 esac 61 62 COMMAND=printf 63 for ARG 64 do 65 case "${ARG}" in 66 (';') # Allow multiple commands 67 COMMAND="${COMMAND} ; printf" 68 ;; 69 (*\'*) 70 # This is kind of odd, we need a working 71 # printf in order to test printf ... 72 # nb: do not use echo here, an arg might be "-n'x" 73 COMMAND="${COMMAND} '$( 74 printf '%s\n' "${ARG}" | 75 sed "s/'/'\\\\''/g" 76 )'" 77 ;; 78 (*) 79 COMMAND="${COMMAND} '${ARG}'" 80 ;; 81 esac 82 done 83 ${TEST_SH} -c "${COMMAND}" 84 } 85 86 Not_builtin() 87 { 88 if $Running_under_ATF 89 then 90 atf_skip "${TEST_SH%% *} does not have printf built in" 91 else 92 echo >&2 "No builtin printf in ${TEST_SH}" 93 exit 1 94 fi 95 } 96 97 # See if we have a builtin "printf" command to test 98 99 setup() 100 { 101 # If the shell being used for its printf supports "type -t", use it 102 if B=$( ${TEST_SH} -c 'type -t printf' 2>/dev/null ) 103 then 104 case "$B" in 105 ( builtin ) return 0;; 106 esac 107 else 108 # We get here if type -t is not supported, or it is, 109 # but printf is completely unknown. No harm trying again. 110 111 case "$( unset LANG LC_ALL LC_NUMERIC LC_CTYPE LC_MESSAGES 112 ${TEST_SH} -c 'type printf' 2>&1 )" in 113 ( *[Bb]uiltin* | *[Bb]uilt[-\ ][Ii]n* ) return 0;; 114 esac 115 fi 116 117 Tests= 118 define Not_builtin 'Dummy test to skip when no printf builtin' 119 return 0 120 } 121 122 setmsg() 123 { 124 MSG="${TEST_SH%% *} builtin printf" 125 CurrentTest="$1" 126 RVAL=0 127 } 128 129 BUILTIN_TEST=true 130 131 # All the code to actually run the test comes from printf.sh ... 132 133