From ede66ccaacad3c9161693c36c04d437f357c7e4d Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Mon, 10 Sep 2018 19:17:44 -0700 Subject: [PATCH] Instance env_set_argv and env_set_pwd --- src/builtin_cd.cpp | 2 +- src/builtin_source.cpp | 2 +- src/env.cpp | 23 ++++++++++------------- src/env.h | 9 ++++++--- src/exec.cpp | 2 +- src/function.cpp | 5 +++-- src/function.h | 3 ++- 7 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/builtin_cd.cpp b/src/builtin_cd.cpp index 10362f444..1ab7a2601 100644 --- a/src/builtin_cd.cpp +++ b/src/builtin_cd.cpp @@ -86,6 +86,6 @@ int builtin_cd(parser_t &parser, io_streams_t &streams, wchar_t **argv) { return STATUS_CMD_ERROR; } - env_set_one(L"PWD", ENV_EXPORT | ENV_GLOBAL, std::move(norm_dir)); + parser.vars().set_one(L"PWD", ENV_EXPORT | ENV_GLOBAL, std::move(norm_dir)); return STATUS_CMD_OK; } diff --git a/src/builtin_source.cpp b/src/builtin_source.cpp index 1e09b702e..8784b0603 100644 --- a/src/builtin_source.cpp +++ b/src/builtin_source.cpp @@ -78,7 +78,7 @@ int builtin_source(parser_t &parser, io_streams_t &streams, wchar_t **argv) { // This is slightly subtle. If this is a bare `source` with no args then `argv + optind` already // points to the end of argv. Otherwise we want to skip the file name to get to the args if any. - env_set_argv(argv + optind + (argc == optind ? 0 : 1)); + parser.vars().set_argv(argv + optind + (argc == optind ? 0 : 1)); retval = reader_read(fd, streams.io_chain ? *streams.io_chain : io_chain_t()); diff --git a/src/env.cpp b/src/env.cpp index df7282a72..c147e22d7 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -642,11 +642,11 @@ static void setup_path() { /// If they don't already exist initialize the `COLUMNS` and `LINES` env vars to reasonable /// defaults. They will be updated later by the `get_current_winsize()` function if they need to be /// adjusted. -static void env_set_termsize() { - auto cols = env_get(L"COLUMNS"); +void env_stack_t::set_termsize() { + auto cols = get(L"COLUMNS"); if (cols.missing_or_empty()) env_set_one(L"COLUMNS", ENV_GLOBAL, DFLT_TERM_COL_STR); - auto rows = env_get(L"LINES"); + auto rows = get(L"LINES"); if (rows.missing_or_empty()) env_set_one(L"LINES", ENV_GLOBAL, DFLT_TERM_ROW_STR); } @@ -708,8 +708,6 @@ static void setup_user(bool force) { /// Various things we need to initialize at run-time that don't really fit any of the other init /// routines. void misc_init() { - env_set_read_limit(); - // If stdout is open on a tty ensure stdio is unbuffered. That's because those functions might // be intermixed with `write()` calls and we need to ensure the writes are not reordered. See // issue #3748. @@ -976,21 +974,22 @@ void env_init(const struct config_paths_t *paths /* or NULL */) { } } + 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 (env_get(L"PWD").missing_or_empty()) { - env_stack_t::principal().set_pwd_from_getcwd(); + if (vars.get(L"PWD").missing_or_empty()) { + vars.set_pwd_from_getcwd(); } - env_set_termsize(); // initialize the terminal size variables - env_set_read_limit(); // initialize the read_byte_limit + vars.set_termsize(); // initialize the terminal size variables + vars.set_read_limit(); // initialize the read_byte_limit // Set g_use_posix_spawn. Default to true. - auto use_posix_spawn = env_get(L"fish_use_posix_spawn"); + auto use_posix_spawn = vars.get(L"fish_use_posix_spawn"); g_use_posix_spawn = use_posix_spawn.missing_or_empty() ? true : from_string(use_posix_spawn->as_string()); // Set fish_bind_mode to "default". - env_set_one(FISH_BIND_MODE_VAR, ENV_GLOBAL, DEFAULT_BIND_MODE); + vars.set_one(FISH_BIND_MODE_VAR, ENV_GLOBAL, DEFAULT_BIND_MODE); // This is somewhat subtle. At this point we consider our environment to be sufficiently // initialized that we can react to changes to variables. Prior to doing this we expect that the @@ -1440,8 +1439,6 @@ int env_remove(const wcstring &key, int mode) { return env_stack_t::principal(). void env_universal_barrier() { env_stack_t::principal().universal_barrier(); } -void env_set_argv(const wchar_t *const *argv) { return env_stack_t::principal().set_argv(argv); } - wcstring env_get_pwd_slash() { return env_stack_t::principal().get_pwd_slash(); } void env_set_read_limit() { return env_stack_t::principal().set_read_limit(); } diff --git a/src/env.h b/src/env.h index 22606c45c..83e6434e4 100644 --- a/src/env.h +++ b/src/env.h @@ -168,9 +168,6 @@ int env_remove(const wcstring &key, int mode); /// Synchronizes all universal variable changes: writes everything out, reads stuff in. void env_universal_barrier(); -/// Sets up argv as the given null terminated array of strings. -void env_set_argv(const wchar_t *const *argv); - /// Returns the PWD with a terminating slash. wcstring env_get_pwd_slash(); @@ -236,6 +233,12 @@ class env_stack_t : public environment_t { /// Returns all variable names. wcstring_list_t get_names(int flags) const override; + /// Update the termsize variable. + void set_termsize(); + + /// Update the PWD variable directory. + bool set_pwd(); + /// Sets up argv as the given null terminated array of strings. void set_argv(const wchar_t *const *argv); diff --git a/src/exec.cpp b/src/exec.cpp index 5b13d7b23..3e7af698a 100644 --- a/src/exec.cpp +++ b/src/exec.cpp @@ -787,7 +787,7 @@ static bool exec_block_or_func_process(parser_t &parser, std::shared_ptr function_block_t *fb = parser.push_block(p, func_name, props->shadow_scope); - function_prepare_environment(func_name, p->get_argv() + 1, inherit_vars); + function_prepare_environment(parser.vars(), func_name, p->get_argv() + 1, inherit_vars); parser.forbid_function(func_name); internal_exec_helper(parser, props->parsed_source, props->body_node, io_chain, j); diff --git a/src/function.cpp b/src/function.cpp index f709b08f0..c5438bf36 100644 --- a/src/function.cpp +++ b/src/function.cpp @@ -344,9 +344,10 @@ void function_invalidate_path() { function_autoloader.invalidate(); } // 1. argv // 2. named arguments // 3. inherited variables -void function_prepare_environment(const wcstring &name, const wchar_t *const *argv, +void function_prepare_environment(env_stack_t &vars, const wcstring &name, + const wchar_t *const *argv, const std::map &inherited_vars) { - env_set_argv(argv); + vars.set_argv(argv); auto props = function_get_properties(name); if (props && !props->named_arguments.empty()) { const wchar_t *const *arg = argv; diff --git a/src/function.h b/src/function.h index d823bdeeb..9ee7df6eb 100644 --- a/src/function.h +++ b/src/function.h @@ -109,7 +109,8 @@ std::map function_get_inherit_vars(const wcstring &name); bool function_copy(const wcstring &name, const wcstring &new_name); /// Prepares the environment for executing a function. -void function_prepare_environment(const wcstring &name, const wchar_t *const *argv, +void function_prepare_environment(env_stack_t &vars, const wcstring &name, + const wchar_t *const *argv, const std::map &inherited_vars); /// Observes that fish_function_path has changed.