########################################################################
#                                                                      #
#               This software is part of the ast package               #
#          Copyright (c) 1982-2009 AT&T Intellectual Property          #
#                      and is licensed under the                       #
#                  Common Public License, Version 1.0                  #
#                    by AT&T Intellectual Property                     #
#                                                                      #
#                A copy of the License is available at                 #
#            http://www.opensource.org/licenses/cpl1.0.txt             #
#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
#                                                                      #
#              Information and Software Systems Research               #
#                            AT&T Research                             #
#                           Florham Park NJ                            #
#                                                                      #
#                  David Korn <dgk@research.att.com>                   #
#                                                                      #
########################################################################
function err_exit
{
	print -u2 -n "\t"
	print -u2 -r ${Command}[$1]: "${@:2}"
	let Errors+=1
}
alias err_exit='err_exit $LINENO'

integer Errors=0
Command=${0##*/}

ulimit -c 0

tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
trap "cd /; rm -rf $tmp" EXIT

integer foo=33
bar=bye
# check for global variables and $0
function foobar
{
	case $1 in
	1) 	print -r - "$foo" "$bar";;
	2)	print -r - "$0";;
	3)	typeset foo=foo
		integer bar=10
	 	print -r - "$foo" "$bar";;
	4)	trap 'foo=36' EXIT
		typeset foo=20;;
	esac
}
function print
{
	command print hi
}
if	[[ $(print) != hi ]]
then	err_exit "command print not working inside print function"
fi
unset -f print

if	[[ $(foobar 1) != '33 bye' ]]
then	err_exit 'global variables not correct'
fi

if	[[ $(foobar 2) != 'foobar' ]]
then	err_exit '$0  not correct'
fi

if	[[ $(bar=foo foobar 1) != '33 foo' ]]
then	err_exit 'environment override not correct'
fi
if	[[ $bar == foo ]]
then	err_exit 'scoping error'
fi

if	[[ $(foobar 3) != 'foo 10' ]]
then	err_exit non-local variables
fi

foobar 4
if	[[ $foo != 36 ]]
then	err_exit EXIT trap in wrong scope
fi
unset -f foobar || err_exit "cannot unset function foobar"
typeset -f foobar>/dev/null  && err_exit "typeset -f has incorrect exit status"

function foobar
{
	(return 0)
}
> $tmp/test1
{
foobar
if	[ -r $tmp/test1 ]
then	rm -r $tmp/test1
else	err_exit 'return within subshell inside function error'
fi
}
abc() print hi
if	[[ $(abc) != hi ]]
then	err_exit 'abc() print hi not working'
fi
( unset -f abc )
if	[[ $(abc 2>/dev/null) != hi ]]
then	err_exit 'abc() print hi not working after subshell unset'
fi
(
	function f
	{
		exit 1
	}
	f
	err_exit 'exit from function not working'
)
unset -f foo
function foo
{
	x=2
	(
		x=3
		cd $tmp
		print bar
	)
	if	[[ $x != 2 ]]
	then	err_exit 'value of x not restored after subshell inside function'
	fi
}
x=1
dir=$PWD
if	[[ $(foo) != bar ]]
then	err_exit 'cd inside nested subshell not working'
fi
if	[[ $PWD != "$dir" ]]
then	err_exit 'cd inside nested subshell changes $PWD'
fi
fun() /bin/echo hello
if	[[ $(fun) != hello ]]
then	err_exit one line functions not working
fi
cat > $tmp/script <<-\!
	print -r -- "$1"
!
chmod +x $tmp/script
function passargs
{
	$tmp/script "$@"
}
if	[[ $(passargs one) != one ]]
then	err_exit 'passing args from functions to scripts not working'
fi
cat > $tmp/script <<-\!
	trap 'exit 0' EXIT
	function foo
	{
		/tmp > /dev/null 2>&1
	}
	foo
!
if	! $tmp/script
then	err_exit 'exit trap incorrectly triggered'
fi
if	! $SHELL -c $tmp/script
then	err_exit 'exit trap incorrectly triggered when invoked with -c'
fi
$SHELL -c "trap 'rm $tmp/script' EXIT"
if	[[ -f $tmp/script ]]
then	err_exit 'exit trap not triggered when invoked with -c'
fi
cat > $tmp/script <<- \EOF
	foobar()
	{
		return
	}
	shift
	foobar
	print -r -- "$1"
EOF
chmod +x $tmp/script
if	[[ $( $SHELL $tmp/script arg1 arg2) != arg2 ]]
then	err_exit 'arguments not restored by posix functions'
fi
function foo
{
	print hello
}
(
	function foo
	{
		print bar
	}
	if [[ $(foo) != bar ]]
	then	err_exit 'function definitions inside subshells not working'
	fi
)
if [[ $(foo) != hello ]]
then	err_exit 'function definitions inside subshells not restored'
fi
unset -f foo bar
function bar
{
        print "$y"
}

function foo
{
        typeset x=3
        y=$x bar
}
x=1
if	[[ $(foo) != 3 ]]
then	err_exit 'variable assignment list not using parent scope'
fi
unset -f foobar
cat > $tmp/foobar <<!
function foobar
{
	print foo
}
!
chmod +x $tmp/foobar
FPATH=$tmp
autoload foobar
if	[[ $(foobar 2>/dev/null) != foo ]]
then	err_exit 'autoload not working'
fi
unset -f foobar
function foobar
{
	typeset -r x=3
	return 0
}
( foobar ) 2> /dev/null || err_exit "cannot unset readonly variable in function"
if	$SHELL -n 2> /dev/null <<-!
	abc()
	!
then	err_exit 'abc() without a function body is not a syntax error'
fi
function winpath
{
	usage='q pathname ...'
	typeset var format=s
	while   getopts  "$usage" var
	do      case $var in
		q)      format=q;;
	        esac
	done
	print done
}
if	[[ $( (winpath --man 2>/dev/null); print ok) != ok ]]
then	err_exit 'getopts --man in functions not working'
fi
if	[[ $( (winpath -z 2>/dev/null); print ok) != ok ]]
then	err_exit 'getopts with bad option in functions not working'
fi
unset -f x
function x
{
        print "$@"
}
typeset -ft x
if      [[ $(x x=y 2>/dev/null) != x=y ]]
then    err_exit 'name=value pair args not passed to traced functions'
fi
function bad
{
	false
}
trap 'val=false' ERR
val=true
bad
if	[[ $val != false ]]
then	err_exit 'set -e not working for functions'
fi
function bad
{
	false
	return 0
}
val=true
bad
if	[[ $val != true ]]
then	err_exit 'set -e not disabled for functions'
fi
bad()
{
	false
	return 0
}
val=true
bad
if	[[ $val != false ]]
then	err_exit 'set -e not inherited for posix functions'
fi
trap - ERR

function myexport
{
	nameref var=$1
	if	(( $# > 1 ))
	then	export	$1=$2
	fi
	if	(( $# > 2 ))
	then	print $(myexport "$1" "$3" )
		return
	fi
	typeset val
	val=$(export | grep "^$1=")
	print ${val#"$1="}
}
export dgk=base
val=$(myexport dgk fun)
if	[[ $val != fun ]]
then	err_exit "export inside function not working -- expected 'fun', got '$val'"
fi
val=$(export | sed -e '/^dgk=/!d' -e 's/^dgk=//')
if	[[ $val != base ]]
then	err_exit "export not restored after function call -- expected 'base', got '$val'"
fi
val=$(myexport dgk fun fun2)
if	[[ $val != fun2 ]]
then	err_exit "export inside function not working with recursive function -- expected 'fun2', got '$val'"
fi
val=$(export | sed -e '/^dgk=/!d' -e 's/^dgk=//')
if	[[ $val != base ]]
then	err_exit "export not restored after recursive function call -- expected 'base', got '$val'"
fi
val=$(dgk=try3 myexport dgk)
if	[[ $val != try3 ]]
then	err_exit "name=value not added to export list with function call -- expected 'try3', got '$val'"
fi
val=$(export | sed -e '/^dgk=/!d' -e 's/^dgk=//')
if	[[ $val != base ]]
then	err_exit "export not restored name=value function call -- expected 'base', got '$val'"
fi
unset zzz
val=$(myexport zzz fun)
if	[[ $val != fun ]]
then	err_exit "export inside function not working -- expected 'fun', got '$val'"
fi
val=$(export | sed -e '/^zzz=/!d' -e 's/^zzz=//')
if	[[ $val ]]
then	err_exit "unset varaible exported after function call -- expected '', got '$val'"
fi

unset zzz
typeset -u zzz
function foo
{
	zzz=abc
	print $zzz
}
if	[[ $(foo)$(foo) != ABCABC ]]
then	err_exit 'attributes on unset variables not saved/restored'
fi
function xpd {
	typeset i j=$1
                for i
                        do print i=$i j=$j
                        [[ $i == a ]] && xpd b
                        done
                }
if	[[ $(xpd a c) != $'i=a j=a\ni=b j=b\ni=c j=a' ]]
then	err_exit 'for loop function optimization error'
fi

typeset -A visited
integer level=0
function closure
{
	(( $# > 5 )) && return 1
	((level < 2)) && ((level++))
	typeset tmp r=0
	visited[$1]=1

	for tmp in $level _$level
	do
		[[ ${visited[$tmp]} == 1 ]] && continue
		closure $tmp $* || r=1
	done
	return $r
}
closure 0 || err_exit -u2 'for loop function optimization bug2'
dir=$tmp/dir
mkdir $dir
cd $dir || { err_exit "cd $dir failed"; exit 1; }
print 'false' > try
chmod +x try
cat > tst <<- EOF
	function ignore
	{
		./try
		return 0
	}
	trap "print error; exit 1" ERR
	ignore
EOF
if	[[ $($SHELL < tst)  == error ]]
then	err_exit 'ERR trap not cleared'
fi
FPATH=$dir
print ': This does nothing' > foobar
chmod +x foobar
unset -f foobar
{ foobar; } 2>/dev/null
got=$?
exp=126
if	[[ $got != $exp ]]
then	err_exit "function file without function definition processes wrong error -- expected '$exp', got '$got'"
fi
print 'set a b c' > dotscript
[[ $(PATH=$PATH: $SHELL -c '. dotscript;print $#') == 3 ]] || err_exit 'positional parameters not preserved with . script without arguments'
cd ~- || err_exit "cd back failed"
function errcheck
{
	trap 'print ERR; return 1' ERR
	false
	print ok
}
err=$(errcheck)
[[ $err == ERR ]] || err_exit 'trap on ERR not working in a function'
x="$(
	function foobar
	{
		print ok
	}
	typeset -f foobar
)"
eval "$x"  || err_exit 'typeset -f generates syntax error'
[[ $(foobar) != ok ]] && err_exit 'typeset -f not generating function'
unset -f a b c
a()
{
        b
        b
        print ${.sh.fun}
}
b() { : ;}
[[ $(a) == a ]] || err_exit '.sh.fun not set correctly in a function'
print $'a(){\ndate\n}'  | $SHELL 2> /dev/null || err_exit 'parser error in a(){;date;}'
cat > $tmp/data1 << '++EOF'
     1  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
     2  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
     3  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
     4  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
     5  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
     6  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
     7  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
     8  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

f()
{
cat <<\M
++EOF
cat $tmp/data1 >> $tmp/script
printf 'M\n}\n\nf\n\n' >> $tmp/script
$SHELL -c $tmp/script  > $tmp/data2
cmp -s $tmp/data[12] || err_exit 'error with long functions'
v=1
function f
{
	typeset i
	for i in 0 1
	do	typeset v
		v=$i
		[[ $v == $i ]] || return 1
	done
}
f || err_exit "typeset optimization bug"
function f
{
	print -r -- "$foo$bar"
}
function g
{
	print -r -- $(bar=bam f)
}
unset foo bar
[[ $(foo=hello g) == hellobam ]] || err_exit 'function exports not passed on'
[[ $(bar=hello g) == bam ]] || err_exit 'function exports not overridden'
unset -f foo
function foo
{
	typeset line=$1
	set +n
	while	[[ $line ]]
	do	if	[[ ! $varname ]]
		then	varname=${line%% *}
			line=${line##"$varname"?( )}
			[[ $line ]] && continue
		else	print ok
			return
		fi
		varname=
	done
}
[[ $(foo 'NUMBERED RECORDSIZE') == ok ]] || err_exit 'optimization error with undefined variable'
unset x
x=$(
	set -e
	integer count=0
	function err_f
	{
		if	((count++==3))
		then	print failed
		else	false
		fi
	}
	trap 'err_f' ERR
	false
)
[[ $x == failed ]] && err_exit 'ERR trap executed multiple times'
export environment
typeset global
function f
{
	typeset i t local

	for i
	do	case $i in
		[-+]*)	set "$@"
			continue
			;;
		local)	local=f
			t=$(typeset +f $local)
			;;
		global)	global=f
			t=$(typeset +f $global)
			;;
		environment)
			environment=f
			t=$(typeset +f $environment)
			;;
		literal)t=$(typeset +f f)
			;;
		positional)
			set -- f
			t=$(typeset +f $1)
			;;
		esac
		[[ $t ]] || err_exit "typeset +f \$$i failed"
	done
}
f local global environment literal positional
$SHELL -c '
	print exit 0 > '$tmp'/script
	chmod +x '$tmp'/script
	unset var
	var=( ident=1 )
	function fun
	{
		PATH='$tmp' script
	}
	fun
' || err_exit "compound variable cleanup before script exec failed"
( $SHELL << \++EOF++
function main
{
 	typeset key
 	typeset -A entry
 	entry[a]=( value=aaa )
}
main
++EOF++
) 2> /dev/null || err_exit 'function main fails'
optind=$OPTIND
sub()
{
        (
                OPTIND=1
                while getopts :abc OPTION "$@"
                do      print OPTIND=$OPTIND
                done
        )
}
[[ $(sub -a) == OPTIND=2 ]] || err_exit 'OPTIND should be 2'
[[ $(sub -a) == OPTIND=2 ]] || err_exit 'OPTIND should be 2 again'
[[ $OPTIND == "$optind" ]] || err_exit 'OPTIND should be 1'

function bar
{
	[[ -o nounset ]] && err_exit  'nounset option should not be inherited'
}
function foo
{
	set -o nounset
	bar
}
set +o nounset
foo
function red
{
	integer -S d=0
	printf 'red_one %d\n' d
	(( d++ ))
	return 0
}
[[ ${ red } != 'red_one 0' ]] && err_exit 'expected red_one 0'
[[ ${ red } != 'red_one 1' ]] && err_exit 'expected red_one 1'
xyz=$0
function traceback
{
	integer .level=.sh.level
	while((--.level>=0))
	do
		((.sh.level = .level))
		[[ $xyz == "$0" ]] || err_exit "\$xyz=$xyz does not match $0 on level ${.level}"
		[[ ${.sh.lineno} == "$1" ]] || err_exit "\${.sh.lineno}=${.sh.lineno} does not match $1 on level ${.level}"
	done
}

function foo
{
	typeset xyz=foo
	set -- $((LINENO+1))
	bar $LINENO "$1"
}

function bar
{
    	typeset xyz=bar
	set -- $((LINENO+2))
	trap 'traceback $LINENO' DEBUG
	: $LINENO "$1"
}

set -- $((LINENO+1))
foo $LINENO
function .sh.fun.set
{
	print -r -- "${.sh.value}"
}
function abc
{
	:
}
def()
{
	:
}
[[ $(abc) == abc ]] || err_exit '.sh.fun.set not capturing function name'
[[ $(def) == def ]] || err_exit '.sh.fun.set not capturing name()'
unset -f .sh.fun.set

# tests for debug functions
basefile=${.sh.file}
integer baseline
cat > $tmp/debug << \+++
	: line 1

	: line 3
+++
# Print one line in a call stack
function _Dbg_print_frame
{
	typeset -i pos=$1
	typeset fn=$2
	typeset filename="$3"
	typeset -i line=$4
	typeset  arg=$5
	shift 5
	if	((pos==0))
	then	[[ $filename == "$basefile" ]] || err_exit "filename for level 0 is $filename not $basename"
		[[ $arg == DEBUG  ]] && ((baseline++))
		[[ $line == "$baseline" ]] || err_exit "line number for level 0 is $line not $baseline"
	elif	((pos==1))
	then	[[ $filename == "$tmp/debug" ]] ||  err_exit "filename for level 1 is $filename not $tmp/debug"
		[[ $* == 'foo bar' ]] || err_exit "args are '$*', not 'foo bar'"
		[[ $line == $arg ]] || err_exit "line number for level 1 is $line not $arg"
	else	err_exit "level should be 0 or 1 but is $pos"
	fi
}

function _Dbg_debug_trap_handler
{

	integer .level=.sh.level .max=.sh.level-1
	while((--.level>=0))
	do
		((.sh.level = .level))
      		_Dbg_print_frame  "${.level}" "$0" "${.sh.file}" "${.sh.lineno}" "${.sh.command##* }" "$@"
	done
}

((baseline=LINENO+2))
trap '_Dbg_debug_trap_handler' DEBUG
.  $tmp/debug foo bar
trap '' DEBUG

caller() {
  integer .level=.sh.level .max=.sh.level-1
  while((--.level>=0))
  do
      ((.sh.level = .level))
      print -r -- "${.sh.lineno}"
  done
}
bar() { caller;}
set -- $(bar)
[[ $1 == $2 ]] && err_exit ".sh.inline optimization bug"
( $SHELL  -c ' function foo { typeset x=$1;print $1;};z=();z=($(foo bar)) ') 2> /dev/null ||  err_exit 'using a function to set an array in a command sub  fails'

{
got=$(
s=$(ulimit -s)
if	[[ $s == +([[:digit:]]) ]] && (( s < 16384 ))
then	ulimit -s 16384 2>/dev/null
fi
$SHELL << \+++
f()
{
	if	(($1>1))
	then	x=$(f $(($1-1))) || exit 1
	fi
	return 0
}
f 257 && print ok
+++
)
} 2>/dev/null
[[ $got == ok ]] || err_exit 'cannot handle comsub depth > 256 in function'

tmp1=$tmp/job.1
tmp2=$tmp/job.2
cat > $tmp1 << +++
#! $SHELL
print \$\$
+++
chmod +x $tmp1
function foo
{
	typeset pid
	$tmp1 > $tmp2 & pid=$!
	wait $!
	[[ $(< $tmp2) == $pid ]] || err_exit 'wrong pid for & job in function'
}
foo
# make sure compiled functions work
[[ $(tmp=$tmp $SHELL <<- \++++
	cat > $tmp/functions <<- \EOF
	 	function bar
	 	{
	 		print foo
	 	}
	 	function foobar
	 	{
	 		bar
	 	}
	EOF
	${SHCOMP:-${SHELL%/*}/shcomp} $tmp/functions > $tmp/foobar
	rm -f "$tmp/functions"
	chmod +x $tmp/foobar
	rm $tmp/!(dir|foobar)
	FPATH=$tmp
	PATH=$FPATH:$PATH
	foobar
++++
) == foo ]] > /dev/null  || err_exit 'functions compiled with shcomp not working'
exit $((Errors))
