From 26fc705c07c4530d38bed3180c1c8883e7d5e0c7 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Mon, 10 Sep 2018 21:27:25 -0700 Subject: [PATCH] Instance env_set_empty --- src/builtin_read.cpp | 3 ++- src/env.cpp | 19 +++++++------------ src/env.h | 3 --- src/function.cpp | 2 +- src/parse_execution.cpp | 2 +- 5 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/builtin_read.cpp b/src/builtin_read.cpp index 0ce1e3be0..4494ae886 100644 --- a/src/builtin_read.cpp +++ b/src/builtin_read.cpp @@ -26,6 +26,7 @@ #include "highlight.h" #include "history.h" #include "io.h" +#include "parser.h" #include "proc.h" #include "reader.h" #include "wcstringutil.h" @@ -452,7 +453,7 @@ int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv) { auto vars_left = [&] () { return argv + argc - var_ptr; }; auto clear_remaining_vars = [&] () { while (vars_left()) { - env_set_empty(*var_ptr, opts.place); + parser.vars().set_empty(*var_ptr, opts.place); // env_set_one(*var_ptr, opts.place, L""); ++var_ptr; } diff --git a/src/env.cpp b/src/env.cpp index 6258c845a..4d7305a19 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -852,6 +852,7 @@ static void setup_var_dispatch_table() { void env_init(const struct config_paths_t *paths /* or NULL */) { setup_var_dispatch_table(); + env_stack_t &vars = env_stack_t::principal(); // Now the environment variable handling is set up, the next step is to insert valid data. // Import environment variables. Walk backwards so that the first one out of any duplicates wins @@ -866,7 +867,7 @@ void env_init(const struct config_paths_t *paths /* or NULL */) { if (eql == wcstring::npos) { // No equal-sign found so treat it as a defined var that has no value(s). if (is_read_only(key_and_val) || is_electric(key_and_val)) continue; - env_set_empty(key_and_val, ENV_EXPORT | ENV_GLOBAL); + vars.set_empty(key_and_val, ENV_EXPORT | ENV_GLOBAL); } else { key.assign(key_and_val, 0, eql); val.assign(key_and_val, eql+1, wcstring::npos); @@ -965,16 +966,15 @@ void env_init(const struct config_paths_t *paths /* or NULL */) { } else { // We cannot get $HOME. This triggers warnings for history and config.fish already, // so it isn't necessary to warn here as well. - env_set_empty(L"HOME", ENV_GLOBAL | ENV_EXPORT); + vars.set_empty(L"HOME", ENV_GLOBAL | ENV_EXPORT); } free(unam_narrow); } else { // If $USER is empty as well (which we tried to set above), we can't get $HOME. - env_set_empty(L"HOME", ENV_GLOBAL | ENV_EXPORT); + vars.set_empty(L"HOME", ENV_GLOBAL | ENV_EXPORT); } } - env_stack_t &vars = env_stack_t::principal(); // initialize the PWD variable if necessary // Note we may inherit a virtual PWD that doesn't match what getcwd would return; respect that. if (vars.get(L"PWD").missing_or_empty()) { @@ -1431,10 +1431,6 @@ int env_set_one(const wcstring &key, env_mode_flags_t mode, wcstring val) { return env_stack_t::principal().set_one(key, mode, std::move(val)); } -int env_set_empty(const wcstring &key, env_mode_flags_t mode) { - return env_stack_t::principal().set_empty(key, mode); -} - void env_universal_barrier() { env_stack_t::principal().universal_barrier(); } wcstring env_get_pwd_slash() { return env_stack_t::principal().get_pwd_slash(); } @@ -1599,12 +1595,11 @@ void env_stack_t::set_argv(const wchar_t *const *argv) { if (argv && *argv) { wcstring_list_t list; for (auto arg = argv; *arg; arg++) { - list.push_back(*arg); + list.emplace_back(*arg); } - - env_set(L"argv", ENV_LOCAL, list); + set(L"argv", ENV_LOCAL, std::move(list)); } else { - env_set_empty(L"argv", ENV_LOCAL); + set_empty(L"argv", ENV_LOCAL); } } diff --git a/src/env.h b/src/env.h index a10880d3b..918df75f1 100644 --- a/src/env.h +++ b/src/env.h @@ -152,9 +152,6 @@ int env_set(const wcstring &key, env_mode_flags_t mode, wcstring_list_t vals); /// Sets the variable with the specified name to a single value. int env_set_one(const wcstring &key, env_mode_flags_t mode, wcstring val); -/// Sets the variable with the specified name to no values. -int env_set_empty(const wcstring &key, env_mode_flags_t mode); - /// Synchronizes all universal variable changes: writes everything out, reads stuff in. void env_universal_barrier(); diff --git a/src/function.cpp b/src/function.cpp index c5438bf36..e8a424414 100644 --- a/src/function.cpp +++ b/src/function.cpp @@ -356,7 +356,7 @@ void function_prepare_environment(env_stack_t &vars, const wcstring &name, env_set_one(named_arg, ENV_LOCAL | ENV_USER, *arg); arg++; } else { - env_set_empty(named_arg, ENV_LOCAL | ENV_USER); + vars.set_empty(named_arg, ENV_LOCAL | ENV_USER); } } } diff --git a/src/parse_execution.cpp b/src/parse_execution.cpp index 09b7f7d9d..a56fe4815 100644 --- a/src/parse_execution.cpp +++ b/src/parse_execution.cpp @@ -387,7 +387,7 @@ parse_execution_result_t parse_execution_context_t::run_for_statement( auto var = env_get(for_var_name, ENV_LOCAL); if (!var && !is_function_context()) var = env_get(for_var_name, ENV_DEFAULT); if (!var || var->read_only()) { - int retval = env_set_empty(for_var_name, ENV_LOCAL | ENV_USER); + int retval = parser->vars().set_empty(for_var_name, ENV_LOCAL | ENV_USER); if (retval != ENV_OK) { report_error(var_name_node, L"You cannot use read-only variable '%ls' in a for loop", for_var_name.c_str());