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