aboutsummaryrefslogtreecommitdiffhomepage
path: root/exec.cpp
diff options
context:
space:
mode:
authorGravatar Kevin Ballard <kevin@sb.org>2014-10-02 15:59:24 -0700
committerGravatar Kevin Ballard <kevin@sb.org>2014-10-02 18:41:39 -0700
commitcfc06203e7ad7707acadd160292d47b25d6daba6 (patch)
tree387a4aa8149477309f9c545dfb9c6ccf1ea24ac1 /exec.cpp
parent6d7a7b00d77098c93aa2b6c0deba4c18029b5a32 (diff)
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`.
Diffstat (limited to 'exec.cpp')
-rw-r--r--exec.cpp7
1 files changed, 6 insertions, 1 deletions
diff --git a/exec.cpp b/exec.cpp
index 10baa76b..10e746ff 100644
--- a/exec.cpp
+++ b/exec.cpp
@@ -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());