134f9b3eeSRoland Mainz#
234f9b3eeSRoland Mainz# CDDL HEADER START
334f9b3eeSRoland Mainz#
434f9b3eeSRoland Mainz# The contents of this file are subject to the terms of the
534f9b3eeSRoland Mainz# Common Development and Distribution License (the "License").
634f9b3eeSRoland Mainz# You may not use this file except in compliance with the License.
734f9b3eeSRoland Mainz#
834f9b3eeSRoland Mainz# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
934f9b3eeSRoland Mainz# or http://www.opensolaris.org/os/licensing.
1034f9b3eeSRoland Mainz# See the License for the specific language governing permissions
1134f9b3eeSRoland Mainz# and limitations under the License.
1234f9b3eeSRoland Mainz#
1334f9b3eeSRoland Mainz# When distributing Covered Code, include this CDDL HEADER in each
1434f9b3eeSRoland Mainz# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1534f9b3eeSRoland Mainz# If applicable, add the following below this CDDL HEADER, with the
1634f9b3eeSRoland Mainz# fields enclosed by brackets "[]" replaced with your own identifying
1734f9b3eeSRoland Mainz# information: Portions Copyright [yyyy] [name of copyright owner]
1834f9b3eeSRoland Mainz#
1934f9b3eeSRoland Mainz# CDDL HEADER END
2034f9b3eeSRoland Mainz#
2134f9b3eeSRoland Mainz
2234f9b3eeSRoland Mainz#
233e14f97fSRoger A. Faulkner# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
2434f9b3eeSRoland Mainz#
2534f9b3eeSRoland Mainz
2634f9b3eeSRoland Mainz#
2734f9b3eeSRoland Mainz# This test checks whether ksh93's builtin "cat" command properly
2834f9b3eeSRoland Mainz# supports the "-n" option.
2934f9b3eeSRoland Mainz#
3034f9b3eeSRoland Mainz# This was reported as CR #6835835 ('ksh93 "cat" builtin does not handle "-n" correctly'):
3134f9b3eeSRoland Mainz# ------------ snip ------------
3234f9b3eeSRoland Mainz# [Originally reported in
3334f9b3eeSRoland Mainz# http://mail.opensolaris.org/pipermail/ksh93-integration-discuss/2009-February/007050.html
3434f9b3eeSRoland Mainz# by Casper Dik]
3534f9b3eeSRoland Mainz# -- snip --
3634f9b3eeSRoland Mainz# I just noticed this in ksh93:
3734f9b3eeSRoland Mainz#  ksh93 -c 'yes "" | head -5|cat -n'
3834f9b3eeSRoland Mainz#     1
3934f9b3eeSRoland Mainz#     2
4034f9b3eeSRoland Mainz#     3
4134f9b3eeSRoland Mainz#     4
4234f9b3eeSRoland Mainz# (I used this for older shells when I want to a list of all integers from 1
4334f9b3eeSRoland Mainz# to a particular number)
4434f9b3eeSRoland Mainz# -- snip --
4534f9b3eeSRoland Mainz# Frequency
4634f9b3eeSRoland Mainz#   Always
4734f9b3eeSRoland Mainz# Regression
4834f9b3eeSRoland Mainz#   No
4934f9b3eeSRoland Mainz# Steps to Reproduce
5034f9b3eeSRoland Mainz#   Execute $ ksh93 -c 'yes "" | head -5|cat -n' #
5134f9b3eeSRoland Mainz# Expected Result
5234f9b3eeSRoland Mainz#     1
5334f9b3eeSRoland Mainz#     2
5434f9b3eeSRoland Mainz#     3
5534f9b3eeSRoland Mainz#     4
5634f9b3eeSRoland Mainz#     5
5734f9b3eeSRoland Mainz# Actual Result
5834f9b3eeSRoland Mainz#
5934f9b3eeSRoland Mainz#
6034f9b3eeSRoland Mainz#     1
6134f9b3eeSRoland Mainz#     2
6234f9b3eeSRoland Mainz#
6334f9b3eeSRoland Mainz#     3
6434f9b3eeSRoland Mainz#
6534f9b3eeSRoland Mainz#     4
6634f9b3eeSRoland Mainz# Error Message(s)
6734f9b3eeSRoland Mainz#   None.
6834f9b3eeSRoland Mainz# Test Case
6934f9b3eeSRoland Mainz#   See description.
7034f9b3eeSRoland Mainz# Workaround
7134f9b3eeSRoland Mainz#   Disable ksh93's builtin "cat" command either via using an absolute path
7234f9b3eeSRoland Mainz#   to the "cat" command (POSIX-style workaround) or using ksh93's
7334f9b3eeSRoland Mainz#   "builtin" command to remove "cat" from the list of builtin
7434f9b3eeSRoland Mainz#   commands (e.g. $ builtin -d /bin/cat /usr/bin/cat #).
7534f9b3eeSRoland Mainz# ------------ snip ------------
7634f9b3eeSRoland Mainz#
7734f9b3eeSRoland Mainz
7834f9b3eeSRoland Mainz# test setup
7934f9b3eeSRoland Mainzfunction err_exit
8034f9b3eeSRoland Mainz{
8134f9b3eeSRoland Mainz	print -u2 -n "\t"
8234f9b3eeSRoland Mainz	print -u2 -r ${Command}[$1]: "${@:2}"
833e14f97fSRoger A. Faulkner	(( Errors < 127 && Errors++ ))
8434f9b3eeSRoland Mainz}
8534f9b3eeSRoland Mainzalias err_exit='err_exit $LINENO'
8634f9b3eeSRoland Mainz
8734f9b3eeSRoland Mainzset -o nounset
8834f9b3eeSRoland MainzCommand=${0##*/}
8934f9b3eeSRoland Mainzinteger Errors=0
9034f9b3eeSRoland Mainz
9134f9b3eeSRoland Mainz#
9234f9b3eeSRoland Mainz# test 1: Compare output of various "cat -n" combinations
9334f9b3eeSRoland Mainz#
9434f9b3eeSRoland Mainzinteger i
9534f9b3eeSRoland Mainztypeset expected_output
9634f9b3eeSRoland Mainztypeset out
9734f9b3eeSRoland Mainz
9834f9b3eeSRoland Mainzexpected_output=$( ${SHELL} -c 'for ((i=1 ; i <= 12 ; i++ )) ; do printf "%6d\t\n" i ; done' )
9934f9b3eeSRoland Mainz
10034f9b3eeSRoland Mainzcompound -a testcases=(
10134f9b3eeSRoland Mainz	# note: we have to add an extra /usr/bin/cat at the end of the pipe to make
10234f9b3eeSRoland Mainz	# sure the "cat" builtin uses the correct buffering mode to trigger
10334f9b3eeSRoland Mainz	# the error and a "true" to make sure the "cat" command isn't the last command
10434f9b3eeSRoland Mainz	# of the shell
10534f9b3eeSRoland Mainz	( name="test1a" cmd='integer i ; builtin cat ; for ((i=1 ; i <= 12 ; i++ )) ; do print ; done | cat -n          | /usr/bin/cat ; true' )
10634f9b3eeSRoland Mainz	# same as "test1a" but uses external "cat" command
10734f9b3eeSRoland Mainz	( name="test1b" cmd='integer i ;               for ((i=1 ; i <= 12 ; i++ )) ; do print ; done | /usr/bin/cat -n | /usr/bin/cat ; true' )
10834f9b3eeSRoland Mainz
10934f9b3eeSRoland Mainz	# same as "test1a" but without the last /usr/bin/cat in the pipe
11034f9b3eeSRoland Mainz	( name="test1c" cmd='integer i ; builtin cat ; for ((i=1 ; i <= 12 ; i++ )) ; do print ; done | cat -n ; true' )
11134f9b3eeSRoland Mainz	# same as "test1b" but without the last /usr/bin/cat in the pipe
11234f9b3eeSRoland Mainz	( name="test1d" cmd='integer i ;               for ((i=1 ; i <= 12 ; i++ )) ; do print ; done | /usr/bin/cat -n ; true' )
11334f9b3eeSRoland Mainz)
11434f9b3eeSRoland Mainz
11534f9b3eeSRoland Mainzfor testid in "${!testcases[@]}" ; do
11634f9b3eeSRoland Mainz	nameref tc=testcases[${testid}]
11734f9b3eeSRoland Mainz
11834f9b3eeSRoland Mainz	out="$( ${SHELL} -o errexit -c "${tc.cmd}" )" || err_exit "${tc.name}: Shell failed"
11934f9b3eeSRoland Mainz	[[ "${expected_output}" == "${out}" ]] || err_exit "${tc.name}: Builtin output does not match expected output"
12034f9b3eeSRoland Mainz
12134f9b3eeSRoland Mainz	out="$( ${SHELL} +o errexit -c "${tc.cmd}" )" || err_exit "${tc.name}: Shell failed"
12234f9b3eeSRoland Mainz	[[ "${expected_output}" == "${out}" ]] || err_exit "${tc.name}: Builtin output does not match expected output"
12334f9b3eeSRoland Mainzdone
12434f9b3eeSRoland Mainz
12534f9b3eeSRoland Mainz
12634f9b3eeSRoland Mainz#
12734f9b3eeSRoland Mainz# test 2: Casper Dik's original testcase
12834f9b3eeSRoland Mainz# from http://mail.opensolaris.org/pipermail/ksh93-integration-discuss/2009-February/007050.html
12934f9b3eeSRoland Mainz#
13034f9b3eeSRoland Mainz
13134f9b3eeSRoland Mainzcmp -s \
13234f9b3eeSRoland Mainz	<( ${SHELL} -c 'yes "" | head -5 | cat -n' ) \
13334f9b3eeSRoland Mainz	<( for ((i=1 ; i <= 5 ; i++ )) ; do printf "%6d\t\n" i ; done ) \
13434f9b3eeSRoland Mainz	|| err_exit 'yes "" | head -5 | cat -n does not match expected output.'
13534f9b3eeSRoland Mainz
13634f9b3eeSRoland Mainz
13734f9b3eeSRoland Mainz# tests done
13834f9b3eeSRoland Mainzexit $((Errors))
139