Add new functions flag -V/--inherit-variable

--inherit-variable takes a variable name and snapshots its current
value. When the function is executed, it will have a local variable with
this value already defined. Printing the function source will include
synthesized `set -l` lines for the values.

This is primarily useful for functions that are created on the fly, such
as in `psub`.
This commit is contained in:
Kevin Ballard
2014-10-02 15:59:24 -07:00
parent 6d7a7b00d7
commit cfc06203e7
10 changed files with 152 additions and 7 deletions

View File

@@ -856,6 +856,7 @@ void exec_job(parser_t &parser, job_t *j)
wcstring_list_t named_arguments = function_get_named_arguments(p->argv0());
bool shadows = function_get_shadows(p->argv0());
std::map<wcstring,env_var_t> inherit_vars = function_get_inherit_vars(p->argv0());
signal_block();
@@ -868,12 +869,16 @@ void exec_job(parser_t &parser, job_t *j)
parser.push_block(newv);
/*
set_argv might trigger an event
setting variables might trigger an event
handler, hence we need to unblock
signals.
*/
signal_unblock();
parse_util_set_argv(p->get_argv()+1, named_arguments);
for (std::map<wcstring,env_var_t>::const_iterator it = inherit_vars.begin(), end = inherit_vars.end(); it != end; ++it)
{
env_set(it->first, it->second.missing() ? NULL : it->second.c_str(), ENV_LOCAL | ENV_USER);
}
signal_block();
parser.forbid_function(p->argv0());