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

@@ -150,12 +150,23 @@ void function_init()
VOMIT_ON_FAILURE(pthread_mutexattr_destroy(&a));
}
static std::map<wcstring,env_var_t> snapshot_vars(const wcstring_list_t &vars)
{
std::map<wcstring,env_var_t> result;
for (wcstring_list_t::const_iterator it = vars.begin(), end = vars.end(); it != end; ++it)
{
result.insert(std::make_pair(*it, env_get_string(*it)));
}
return result;
}
function_info_t::function_info_t(const function_data_t &data, const wchar_t *filename, int def_offset, bool autoload) :
definition(data.definition),
description(data.description),
definition_file(intern(filename)),
definition_offset(def_offset),
named_arguments(data.named_arguments),
inherit_vars(snapshot_vars(data.inherit_vars)),
is_autoload(autoload),
shadows(data.shadows)
{
@@ -167,6 +178,7 @@ function_info_t::function_info_t(const function_info_t &data, const wchar_t *fil
definition_file(intern(filename)),
definition_offset(def_offset),
named_arguments(data.named_arguments),
inherit_vars(data.inherit_vars),
is_autoload(autoload),
shadows(data.shadows)
{
@@ -268,6 +280,13 @@ wcstring_list_t function_get_named_arguments(const wcstring &name)
return func ? func->named_arguments : wcstring_list_t();
}
std::map<wcstring,env_var_t> function_get_inherit_vars(const wcstring &name)
{
scoped_lock lock(functions_lock);
const function_info_t *func = function_get(name);
return func ? func->inherit_vars : std::map<wcstring,env_var_t>();
}
int function_get_shadows(const wcstring &name)
{
scoped_lock lock(functions_lock);