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