1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24#
25
26#
27# This test checks whether ksh93's builtin "cat" command properly
28# supports the "-n" option.
29#
30# This was reported as CR #6835835 ('ksh93 "cat" builtin does not handle "-n" correctly'):
31# ------------ snip ------------
32# [Originally reported in
33# http://mail.opensolaris.org/pipermail/ksh93-integration-discuss/2009-February/007050.html
34# by Casper Dik]
35# -- snip --
36# I just noticed this in ksh93:
37#  ksh93 -c 'yes "" | head -5|cat -n'
38#     1
39#     2
40#     3
41#     4
42# (I used this for older shells when I want to a list of all integers from 1
43# to a particular number)
44# -- snip --
45# Frequency
46#   Always
47# Regression
48#   No
49# Steps to Reproduce
50#   Execute $ ksh93 -c 'yes "" | head -5|cat -n' #
51# Expected Result
52#     1
53#     2
54#     3
55#     4
56#     5
57# Actual Result
58#
59#
60#     1
61#     2
62#
63#     3
64#
65#     4
66# Error Message(s)
67#   None.
68# Test Case
69#   See description.
70# Workaround
71#   Disable ksh93's builtin "cat" command either via using an absolute path
72#   to the "cat" command (POSIX-style workaround) or using ksh93's
73#   "builtin" command to remove "cat" from the list of builtin
74#   commands (e.g. $ builtin -d /bin/cat /usr/bin/cat #).
75# ------------ snip ------------
76#
77
78# test setup
79function err_exit
80{
81	print -u2 -n "\t"
82	print -u2 -r ${Command}[$1]: "${@:2}"
83	(( Errors < 127 && Errors++ ))
84}
85alias err_exit='err_exit $LINENO'
86
87set -o nounset
88Command=${0##*/}
89integer Errors=0
90
91#
92# test 1: Compare output of various "cat -n" combinations
93#
94integer i
95typeset expected_output
96typeset out
97
98expected_output=$( ${SHELL} -c 'for ((i=1 ; i <= 12 ; i++ )) ; do printf "%6d\t\n" i ; done' )
99
100compound -a testcases=(
101	# note: we have to add an extra /usr/bin/cat at the end of the pipe to make
102	# sure the "cat" builtin uses the correct buffering mode to trigger
103	# the error and a "true" to make sure the "cat" command isn't the last command
104	# of the shell
105	( name="test1a" cmd='integer i ; builtin cat ; for ((i=1 ; i <= 12 ; i++ )) ; do print ; done | cat -n          | /usr/bin/cat ; true' )
106	# same as "test1a" but uses external "cat" command
107	( name="test1b" cmd='integer i ;               for ((i=1 ; i <= 12 ; i++ )) ; do print ; done | /usr/bin/cat -n | /usr/bin/cat ; true' )
108
109	# same as "test1a" but without the last /usr/bin/cat in the pipe
110	( name="test1c" cmd='integer i ; builtin cat ; for ((i=1 ; i <= 12 ; i++ )) ; do print ; done | cat -n ; true' )
111	# same as "test1b" but without the last /usr/bin/cat in the pipe
112	( name="test1d" cmd='integer i ;               for ((i=1 ; i <= 12 ; i++ )) ; do print ; done | /usr/bin/cat -n ; true' )
113)
114
115for testid in "${!testcases[@]}" ; do
116	nameref tc=testcases[${testid}]
117
118	out="$( ${SHELL} -o errexit -c "${tc.cmd}" )" || err_exit "${tc.name}: Shell failed"
119	[[ "${expected_output}" == "${out}" ]] || err_exit "${tc.name}: Builtin output does not match expected output"
120
121	out="$( ${SHELL} +o errexit -c "${tc.cmd}" )" || err_exit "${tc.name}: Shell failed"
122	[[ "${expected_output}" == "${out}" ]] || err_exit "${tc.name}: Builtin output does not match expected output"
123done
124
125
126#
127# test 2: Casper Dik's original testcase
128# from http://mail.opensolaris.org/pipermail/ksh93-integration-discuss/2009-February/007050.html
129#
130
131cmp -s \
132	<( ${SHELL} -c 'yes "" | head -5 | cat -n' ) \
133	<( for ((i=1 ; i <= 5 ; i++ )) ; do printf "%6d\t\n" i ; done ) \
134	|| err_exit 'yes "" | head -5 | cat -n does not match expected output.'
135
136
137# tests done
138exit $((Errors))
139