Factor out fetching electric variables into a separate function

This factors env_stack_t::get() a little better
This commit is contained in:
ridiculousfish 2019-04-10 16:02:49 -07:00
parent f35f2fe110
commit 341799194e

View file

@ -836,19 +836,8 @@ std::shared_ptr<const wcstring_list_t> env_var_t::empty_list() {
return result; return result;
} }
maybe_t<env_var_t> env_stack_t::get(const wcstring &key, env_mode_flags_t mode) const { /// \return a the value of a variable for \p key, which must be electric (computed).
const bool has_scope = mode & (ENV_LOCAL | ENV_GLOBAL | ENV_UNIVERSAL); static maybe_t<env_var_t> get_electric(const wcstring &key, const environment_t &vars) {
const bool search_local = !has_scope || (mode & ENV_LOCAL);
const bool search_global = !has_scope || (mode & ENV_GLOBAL);
const bool search_universal = !has_scope || (mode & ENV_UNIVERSAL);
const bool search_exported = (mode & ENV_EXPORT) || !(mode & ENV_UNEXPORT);
const bool search_unexported = (mode & ENV_UNEXPORT) || !(mode & ENV_EXPORT);
// Make the assumption that electric keys can't be shadowed elsewhere, since we currently block
// that in env_stack_t::set().
if (is_electric(key)) {
if (!search_global) return none();
if (key == L"history") { if (key == L"history") {
// Big hack. We only allow getting the history on the main thread. Note that history_t // Big hack. We only allow getting the history on the main thread. Note that history_t
// may ask for an environment variable, so don't take the lock here (we don't need it). // may ask for an environment variable, so don't take the lock here (we don't need it).
@ -858,7 +847,7 @@ maybe_t<env_var_t> env_stack_t::get(const wcstring &key, env_mode_flags_t mode)
history_t *history = reader_get_history(); history_t *history = reader_get_history();
if (!history) { if (!history) {
history = &history_t::history_with_name(history_session_id(*this)); history = &history_t::history_with_name(history_session_id(vars));
} }
wcstring_list_t result; wcstring_list_t result;
if (history) history->get_history(result); if (history) history->get_history(result);
@ -877,7 +866,23 @@ maybe_t<env_var_t> env_stack_t::get(const wcstring &key, env_mode_flags_t mode)
return env_var_t(L"umask", format_string(L"0%0.3o", get_umask())); return env_var_t(L"umask", format_string(L"0%0.3o", get_umask()));
} }
// We should never get here unless the electric var list is out of sync with the above code. // We should never get here unless the electric var list is out of sync with the above code.
DIE("unerecognized electric var name"); DIE("unrecognized electric var name");
}
maybe_t<env_var_t> env_stack_t::get(const wcstring &key, env_mode_flags_t mode) const {
const bool has_scope = mode & (ENV_LOCAL | ENV_GLOBAL | ENV_UNIVERSAL);
const bool search_local = !has_scope || (mode & ENV_LOCAL);
const bool search_global = !has_scope || (mode & ENV_GLOBAL);
const bool search_universal = !has_scope || (mode & ENV_UNIVERSAL);
const bool search_exported = (mode & ENV_EXPORT) || !(mode & ENV_UNEXPORT);
const bool search_unexported = (mode & ENV_UNEXPORT) || !(mode & ENV_EXPORT);
// Make the assumption that electric keys can't be shadowed elsewhere, since we currently block
// that in env_stack_t::set().
if (is_electric(key)) {
if (!search_global) return none();
return get_electric(key, *this);
} }
if (search_local || search_global) { if (search_local || search_global) {