function-local variables in ltmain

From:

Ralf Wildenhues

Subject:

function-local variables in ltmain

Date:

Sat, 13 Aug 2005 10:40:01 +0200

User-agent:

Mutt/1.5.9i

Gary once mentioned a plan to emulate local variables in m4sh.
(Or was it return values? I can't find it now).
I have two ideas:
1) Since we do assume the shell has functions, can we maybe also assume
the shell has /some/ way of declaring local variables?
If so, the CONFIG_COMMANDS to generate `libtool' could put it in:
local or
typeset
Issue may arise if different semantics are employed by different
shells, e.g.: are local visible to functions called, how or whether
to access the global shadowing the local.
Implicitly, branch-2-0 and HEAD already assume that the positional
parameters are function-local and restored after returning.
2) This looks somewhat ugly in the output, but to a certain extent, it
is possible to emulate local variables with m4sh: see below.
The idea is to save and restores the globals. I tried renaming the
locals, but that would not work within an eval, for example oldlib in
old_archive_cmds.
One disadvantage of this is that it destroys the difference between
empty and unset variables. I still need to audit ltmain for places
where this might be important.
It's a bummer the m4sh -> sh transition isn't done after configuration.
Then we could just replace with the local-variable mechanism of the
shell, if any..
What do you think?
Regards,
Ralf
# func(name)
# ----------
# Define function func_name.
m4_define([func],
[m4_pushdef([lt_fname],[$1])dnl
m4_define([lt_fcleanup])dnl
[func_]$1[]dnl
])
# endfunc
# -------
m4_define([endfunc],
[lt_fcleanup[]dnl
m4_popdef([lt_fname])dnl
])
# local(variable, [value])
# ------------------------
# Define VARIABLE to be local to the current shell function.
m4_define([local],
[[s_]]lt_fname[[_$1=$][$1]dnl
m4_if([$#], [2], [
$1=$2])[]dnl
m4_append([lt_fcleanup], [
$1=[$]s_]lt_fname[_$1])dnl
])
# start examples
func(foo) ()
{
local(inherited)
local(newly_set, newvalue)
local(empty_set, [])
# function body
endfunc
}
func(bar) ()
{
local(inh)
local(new, ["a,b,c"])
local(emp, [])
# function body
endfunc
}
name is lt_fname
func(barbaz) ()
{
# ..
endfunc
}