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 supports more than 256 recursive
28# function+command substitution calls.
29#
30# This was reported as CR #6769332 ('Recursive function+command
31# substitutions terminate shell after 257 iterations'):
32# ------------ snip ------------
33# Recursive function+command substitutions
34# (e.g. func1() { x=$( func2 ) ; } ; x=$( func1 ) ) terminate the
35# ksh93 shell after 257 iterations with a exit code of "0" (it
36# seems the shell just "quits" after the last "return 0" statement
37# in the function).
38# Running the attached testcase terminates the shell after 257
39# iterations (g=257 in the script) while 256 iterations (replace
40# "257" with "256" in the script) just works fine.
41# The same testcase works Ok in ksh88 (=/usr/bin/ksh in
42# Solaris 10U5)
43#
44# Expected Result
45#    The script should output "done" and return the exit code 0.
46#
47# Actual Result
48#    No messsge. Exit code "0".
49#
50# Error Message(s)
51#    None (exit code is "0").
52#
53# Test Case
54#    f1()
55# {
56#         h=$1
57#         (( h=h-1 ))
58#         (( h <= 0 )) && return 0
59#         x=$(f1 "$h" "$l" "$g" d e "$l") || print -u2 "$g/$h: fail"
60#         return 0
61# }
62# l=""
63# g=257
64# i=0
65# while (( i < $g )) ; do
66#         l="${l}x"
67#         (( i=i+1 ))
68# done
69# f1 "$g" "$l" "$g" d e "$l" || print -u2 "$g: fail0"
70# print "done"
71# exit 0
72#
73# Workaround
74#    -
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# test1: Testcase from CR #6769332
93#
94(
95cat <<EOF
96# make sure we have enougth stack (needed for 64bit SPARC and SystemZ)
97ulimit -s 65536
98
99f1()
100{
101        h=\$1
102        (( h=h-1 ))
103        (( h <= 0 )) && return 0
104        x=\$(f1 "\$h" "\$l" "\$g" d e "\$l") || print -u2 "\$g/\$h: fail"
105        return 0
106}
107l=""
108g=257
109i=0
110while (( i < \$g )) ; do
111        l="\${l}x"
112        (( i=i+1 ))
113done
114f1 "\$g" "\$l" "\$g" d e "\$l" || print -u2 "\$g: fail0"
115print "done"
116EOF
117) | out="$( ${SHELL} 2>&1 ; )" || err_exit "Shell returned non-zero exit code $?."
118
119[[ "${out}" == "done" ]] || err_exit "Output expected to be 'done', got '${out}'."
120
121
122# tests done
123exit $((Errors))
124