Instance env_set_empty

This commit is contained in:
ridiculousfish 2018-09-10 21:27:25 -07:00
parent a00de96a57
commit 26fc705c07
5 changed files with 11 additions and 18 deletions

View file

@ -26,6 +26,7 @@
#include "highlight.h" #include "highlight.h"
#include "history.h" #include "history.h"
#include "io.h" #include "io.h"
#include "parser.h"
#include "proc.h" #include "proc.h"
#include "reader.h" #include "reader.h"
#include "wcstringutil.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 vars_left = [&] () { return argv + argc - var_ptr; };
auto clear_remaining_vars = [&] () { auto clear_remaining_vars = [&] () {
while (vars_left()) { 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""); // env_set_one(*var_ptr, opts.place, L"");
++var_ptr; ++var_ptr;
} }

View file

@ -852,6 +852,7 @@ static void setup_var_dispatch_table() {
void env_init(const struct config_paths_t *paths /* or NULL */) { void env_init(const struct config_paths_t *paths /* or NULL */) {
setup_var_dispatch_table(); 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. // 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 // 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) { if (eql == wcstring::npos) {
// No equal-sign found so treat it as a defined var that has no value(s). // 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; 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 { } else {
key.assign(key_and_val, 0, eql); key.assign(key_and_val, 0, eql);
val.assign(key_and_val, eql+1, wcstring::npos); 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 { } else {
// We cannot get $HOME. This triggers warnings for history and config.fish already, // We cannot get $HOME. This triggers warnings for history and config.fish already,
// so it isn't necessary to warn here as well. // 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); free(unam_narrow);
} else { } else {
// If $USER is empty as well (which we tried to set above), we can't get $HOME. // 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 // initialize the PWD variable if necessary
// Note we may inherit a virtual PWD that doesn't match what getcwd would return; respect that. // 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()) { 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)); 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(); } void env_universal_barrier() { env_stack_t::principal().universal_barrier(); }
wcstring env_get_pwd_slash() { return env_stack_t::principal().get_pwd_slash(); } 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) { if (argv && *argv) {
wcstring_list_t list; wcstring_list_t list;
for (auto arg = argv; *arg; arg++) { for (auto arg = argv; *arg; arg++) {
list.push_back(*arg); list.emplace_back(*arg);
} }
set(L"argv", ENV_LOCAL, std::move(list));
env_set(L"argv", ENV_LOCAL, list);
} else { } else {
env_set_empty(L"argv", ENV_LOCAL); set_empty(L"argv", ENV_LOCAL);
} }
} }

View file

@ -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. /// 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); 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. /// Synchronizes all universal variable changes: writes everything out, reads stuff in.
void env_universal_barrier(); void env_universal_barrier();

View file

@ -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); env_set_one(named_arg, ENV_LOCAL | ENV_USER, *arg);
arg++; arg++;
} else { } else {
env_set_empty(named_arg, ENV_LOCAL | ENV_USER); vars.set_empty(named_arg, ENV_LOCAL | ENV_USER);
} }
} }
} }

View file

@ -387,7 +387,7 @@ parse_execution_result_t parse_execution_context_t::run_for_statement(
auto var = env_get(for_var_name, ENV_LOCAL); auto var = env_get(for_var_name, ENV_LOCAL);
if (!var && !is_function_context()) var = env_get(for_var_name, ENV_DEFAULT); if (!var && !is_function_context()) var = env_get(for_var_name, ENV_DEFAULT);
if (!var || var->read_only()) { 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) { if (retval != ENV_OK) {
report_error(var_name_node, L"You cannot use read-only variable '%ls' in a for loop", report_error(var_name_node, L"You cannot use read-only variable '%ls' in a for loop",
for_var_name.c_str()); for_var_name.c_str());