diff options
author | ridiculousfish <corydoras@ridiculousfish.com> | 2015-08-15 13:37:17 -0700 |
---|---|---|
committer | ridiculousfish <corydoras@ridiculousfish.com> | 2015-08-15 13:37:17 -0700 |
commit | 93d57bd73a885df46f45a1e771a8f9568144fd6a (patch) | |
tree | 8a08e090c232692f7cdc7b31d87f976f7efad07e /src/function.cpp | |
parent | 60798798ef37fc0dcee3edece62ca11fb2d1ced8 (diff) |
Factor function environment preparation into its own function
Diffstat (limited to 'src/function.cpp')
-rw-r--r-- | src/function.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/function.cpp b/src/function.cpp index 2a8c20be..a06e2993 100644 --- a/src/function.cpp +++ b/src/function.cpp @@ -387,3 +387,32 @@ int function_get_definition_offset(const wcstring &name) const function_info_t *func = function_get(name); return func ? func->definition_offset : -1; } + +void function_prepare_environment(const wcstring &name, const wchar_t * const * argv, const std::map<wcstring, env_var_t> &inherited_vars) +{ + /* Three components of the environment: + 1. argv + 2. named arguments + 3. inherited variables + */ + env_set_argv(argv); + + const wcstring_list_t named_arguments = function_get_named_arguments(name); + if (! named_arguments.empty()) + { + const wchar_t * const *arg; + size_t i; + for (i=0, arg=argv; i < named_arguments.size(); i++) + { + env_set(named_arguments.at(i).c_str(), *arg, ENV_LOCAL | ENV_USER); + + if (*arg) + arg++; + } + } + + for (std::map<wcstring,env_var_t>::const_iterator it = inherited_vars.begin(), end = inherited_vars.end(); it != end; ++it) + { + env_set(it->first, it->second.missing() ? NULL : it->second.c_str(), ENV_LOCAL | ENV_USER); + } +} |