#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# posix rules for assignments preceding export/readonly in functions, using
# local and global vars; default rules for assignments preceding declare/local
# in functions, always using local vars.
#
# changes to local variables should never propagate upward from the function
# to its caller, even in posix mode

x=1
x=4 declare -r x
declare -p x
y=2
y=5 readonly y
declare -p y
# can't use x and y from here on

f() { local -r a=3; echo f:$a; }
f1() { declare -r b=3; echo f1:$b; }

a=4 f
b=4 f1
echo global1:$a $b

set -o posix
a=4 f
b=4 f1
echo global2:$a $b
set +o posix

unset -f f f1

f() { local a=3; readonly a; echo f:$a; }
f1() { local b=3; declare -r b; echo f1:$b; }

a=4 f
b=4 f1
echo global:$a $b

set -o posix
a=4 f
b=4 f1
echo global:$a $b
set +o posix

unset -f f f1

f() { local a; a=3 readonly a; echo f:$a; }

a=4 f
echo $a
set -o posix
a=4 f
echo $a
set +o posix

f1() { a=3 readonly a; echo f1:$a; }

a=7 f1
echo global: $a

set -o posix
a=7 f1
echo global: $a
set +o posix

unset -f f f1
# can't use a from here on

c=7 b=8
f() { c=3 readonly c; echo f: $c; }
f1() { b=4 declare -r b; echo f1: $b; }

f
echo global: $(declare -p c)

f1
echo global: $(declare -p b)

unset -f f f1

# can't use c from here on


