2016-04-29 01:26:46 +00:00
|
|
|
// Functions for setting and getting environment variables.
|
2016-05-18 22:30:21 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2019-10-13 22:50:48 +00:00
|
|
|
#include "env.h"
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
#include <errno.h>
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <pwd.h>
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <stddef.h>
|
2017-02-16 04:09:26 +00:00
|
|
|
#include <stdio.h>
|
2016-04-29 01:26:46 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2016-12-23 21:08:45 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
#include <algorithm>
|
2018-01-30 21:22:36 +00:00
|
|
|
#include <iterator>
|
2019-04-13 21:27:03 +00:00
|
|
|
#include <mutex>
|
2016-04-29 01:26:46 +00:00
|
|
|
#include <set>
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2021-11-10 01:07:15 +00:00
|
|
|
#include "builtins/bind.h"
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "common.h"
|
2019-03-26 03:18:00 +00:00
|
|
|
#include "env_dispatch.h"
|
2014-06-16 00:30:50 +00:00
|
|
|
#include "env_universal_common.h"
|
2005-10-05 22:37:08 +00:00
|
|
|
#include "event.h"
|
2016-04-29 01:26:46 +00:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2014-05-01 07:46:27 +00:00
|
|
|
#include "fish_version.h"
|
2019-05-27 22:56:53 +00:00
|
|
|
#include "flog.h"
|
2019-04-28 23:34:54 +00:00
|
|
|
#include "global_safety.h"
|
2016-04-29 01:26:46 +00:00
|
|
|
#include "history.h"
|
|
|
|
#include "input.h"
|
2021-05-10 02:48:43 +00:00
|
|
|
#include "kill.h"
|
2016-04-29 01:26:46 +00:00
|
|
|
#include "path.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "reader.h"
|
2020-06-07 23:05:52 +00:00
|
|
|
#include "termsize.h"
|
2020-07-29 23:37:23 +00:00
|
|
|
#include "wcstringutil.h"
|
2016-04-29 01:26:46 +00:00
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Some configuration path environment variables.
|
2018-03-12 13:34:20 +00:00
|
|
|
#define FISH_DATADIR_VAR L"__fish_data_dir"
|
|
|
|
#define FISH_SYSCONFDIR_VAR L"__fish_sysconf_dir"
|
2012-07-18 17:50:38 +00:00
|
|
|
#define FISH_HELPDIR_VAR L"__fish_help_dir"
|
|
|
|
#define FISH_BIN_DIR L"__fish_bin_dir"
|
2018-12-15 04:09:29 +00:00
|
|
|
#define FISH_CONFIG_DIR L"__fish_config_dir"
|
|
|
|
#define FISH_USER_DATA_DIR L"__fish_user_data_dir"
|
2012-07-18 17:50:38 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// At init, we read all the environment variables from this array.
|
2005-09-20 13:26:39 +00:00
|
|
|
extern char **environ;
|
|
|
|
|
2018-09-10 01:32:15 +00:00
|
|
|
/// The character used to delimit path and non-path variables in exporting and in string expansion.
|
2020-06-07 22:46:54 +00:00
|
|
|
static constexpr wchar_t PATH_ARRAY_SEP = L':';
|
|
|
|
static constexpr wchar_t NONPATH_ARRAY_SEP = L' ';
|
|
|
|
|
2017-02-16 04:09:26 +00:00
|
|
|
bool curses_initialized = false;
|
2012-03-06 23:51:48 +00:00
|
|
|
|
2016-12-23 21:08:45 +00:00
|
|
|
/// Does the terminal have the "eat_newline_glitch".
|
|
|
|
bool term_has_xn = false;
|
|
|
|
|
2021-05-10 21:06:17 +00:00
|
|
|
/// Getter for universal variables.
|
|
|
|
/// This is typically initialized in env_init(), and is considered empty before then.
|
2021-05-10 02:48:43 +00:00
|
|
|
static acquired_lock<env_universal_t> uvars() {
|
2021-05-10 21:06:17 +00:00
|
|
|
// Leaked to avoid shutdown dtor registration.
|
2021-10-28 08:46:29 +00:00
|
|
|
static auto const s_universal_variables = new owning_lock<env_universal_t>();
|
2021-05-10 02:48:43 +00:00
|
|
|
return s_universal_variables->acquire();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Whether we were launched with no_config; in this case setting a uvar instead sets a global.
|
|
|
|
static relaxed_atomic_bool_t s_uvar_scope_is_global{false};
|
2019-04-14 21:01:23 +00:00
|
|
|
|
2019-04-29 05:05:03 +00:00
|
|
|
bool env_universal_barrier() { return env_stack_t::principal().universal_barrier(); }
|
2019-04-14 21:01:23 +00:00
|
|
|
|
2021-09-28 19:50:27 +00:00
|
|
|
namespace {
|
2019-05-12 02:09:22 +00:00
|
|
|
struct electric_var_t {
|
|
|
|
enum {
|
|
|
|
freadonly = 1 << 0, // May not be modified by the user.
|
|
|
|
fcomputed = 1 << 1, // Value is dynamically computed.
|
|
|
|
fexports = 1 << 2, // Exported to child processes.
|
|
|
|
};
|
|
|
|
const wchar_t *name;
|
|
|
|
uint32_t flags;
|
|
|
|
|
|
|
|
bool readonly() const { return flags & freadonly; }
|
|
|
|
|
|
|
|
bool computed() const { return flags & fcomputed; }
|
|
|
|
|
|
|
|
bool exports() const { return flags & fexports; }
|
|
|
|
|
2021-04-20 02:05:41 +00:00
|
|
|
static const electric_var_t *for_name(const wchar_t *name);
|
2019-05-12 02:09:22 +00:00
|
|
|
static const electric_var_t *for_name(const wcstring &name);
|
|
|
|
};
|
|
|
|
|
2020-06-25 03:46:02 +00:00
|
|
|
// Keep sorted alphabetically
|
2021-02-08 21:31:49 +00:00
|
|
|
static constexpr const electric_var_t electric_variables[] = {
|
2020-06-25 03:46:02 +00:00
|
|
|
{L"FISH_VERSION", electric_var_t::freadonly},
|
2019-05-12 02:09:22 +00:00
|
|
|
{L"PWD", electric_var_t::freadonly | electric_var_t::fcomputed | electric_var_t::fexports},
|
|
|
|
{L"SHLVL", electric_var_t::freadonly | electric_var_t::fexports},
|
2020-06-25 03:46:02 +00:00
|
|
|
{L"_", electric_var_t::freadonly},
|
|
|
|
{L"fish_kill_signal", electric_var_t::freadonly | electric_var_t::fcomputed},
|
2021-04-16 18:23:14 +00:00
|
|
|
{L"fish_killring", electric_var_t::freadonly | electric_var_t::fcomputed},
|
2020-06-25 03:46:02 +00:00
|
|
|
{L"fish_pid", electric_var_t::freadonly},
|
2019-05-12 02:09:22 +00:00
|
|
|
{L"history", electric_var_t::freadonly | electric_var_t::fcomputed},
|
2020-06-25 03:46:02 +00:00
|
|
|
{L"hostname", electric_var_t::freadonly},
|
2019-05-12 02:09:22 +00:00
|
|
|
{L"pipestatus", electric_var_t::freadonly | electric_var_t::fcomputed},
|
|
|
|
{L"status", electric_var_t::freadonly | electric_var_t::fcomputed},
|
2020-08-02 20:37:19 +00:00
|
|
|
{L"status_generation", electric_var_t::freadonly | electric_var_t::fcomputed},
|
2019-05-12 02:09:22 +00:00
|
|
|
{L"umask", electric_var_t::fcomputed},
|
2020-06-25 03:46:02 +00:00
|
|
|
{L"version", electric_var_t::freadonly},
|
2019-05-12 02:09:22 +00:00
|
|
|
};
|
2021-07-15 20:15:24 +00:00
|
|
|
ASSERT_SORTED_BY_NAME(electric_variables);
|
2019-05-12 02:09:22 +00:00
|
|
|
|
2021-04-20 02:05:41 +00:00
|
|
|
const electric_var_t *electric_var_t::for_name(const wchar_t *name) {
|
2021-08-26 20:40:02 +00:00
|
|
|
return get_by_sorted_name(name, electric_variables);
|
2019-05-12 02:09:22 +00:00
|
|
|
}
|
2019-04-14 21:01:23 +00:00
|
|
|
|
2021-04-20 02:05:41 +00:00
|
|
|
const electric_var_t *electric_var_t::for_name(const wcstring &name) {
|
|
|
|
return electric_var_t::for_name(name.c_str());
|
|
|
|
}
|
2021-09-28 19:50:27 +00:00
|
|
|
} // namespace
|
2021-04-20 02:05:41 +00:00
|
|
|
|
2019-04-14 21:01:23 +00:00
|
|
|
/// Check if a variable may not be set using the set command.
|
2021-04-20 02:05:41 +00:00
|
|
|
static bool is_read_only(const wchar_t *key) {
|
2020-04-02 23:04:04 +00:00
|
|
|
if (auto ev = electric_var_t::for_name(key)) {
|
2019-05-12 02:09:22 +00:00
|
|
|
return ev->readonly();
|
|
|
|
}
|
2021-01-01 21:57:45 +00:00
|
|
|
return false;
|
2019-04-14 21:01:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 02:05:41 +00:00
|
|
|
static bool is_read_only(const wcstring &key) { return is_read_only(key.c_str()); }
|
|
|
|
|
2019-04-14 21:01:23 +00:00
|
|
|
/// Return true if a variable should become a path variable by default. See #436.
|
|
|
|
static bool variable_should_auto_pathvar(const wcstring &name) {
|
|
|
|
return string_suffixes_string(L"PATH", name);
|
|
|
|
}
|
2019-05-10 01:38:18 +00:00
|
|
|
// This is a big dorky lock we take around everything that might read from or modify an env_node_t.
|
|
|
|
// Fine grained locking is annoying here because env_nodes may be shared between env_stacks, so each
|
|
|
|
// node would need its own lock.
|
|
|
|
static std::mutex env_lock;
|
|
|
|
|
2019-06-09 20:25:30 +00:00
|
|
|
/// We cache our null-terminated export list. However an exported variable may change for lots of
|
|
|
|
/// reasons: popping a scope, a modified universal variable, etc. We thus have a monotone counter.
|
|
|
|
/// Every time an exported variable changes in a node, it acquires the next generation. 0 is a
|
|
|
|
/// sentinel that indicates that the node contains no exported variables.
|
|
|
|
using export_generation_t = uint64_t;
|
|
|
|
static export_generation_t next_export_generation() {
|
|
|
|
static owning_lock<export_generation_t> s_gen;
|
|
|
|
auto val = s_gen.acquire();
|
|
|
|
return ++*val;
|
|
|
|
}
|
|
|
|
|
2019-04-14 21:01:23 +00:00
|
|
|
const wcstring_list_t &env_var_t::as_list() const { return *vals_; }
|
|
|
|
|
|
|
|
wchar_t env_var_t::get_delimiter() const {
|
|
|
|
return is_pathvar() ? PATH_ARRAY_SEP : NONPATH_ARRAY_SEP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a string representation of the var.
|
|
|
|
wcstring env_var_t::as_string() const { return join_strings(*vals_, get_delimiter()); }
|
|
|
|
|
|
|
|
void env_var_t::to_list(wcstring_list_t &out) const { out = *vals_; }
|
|
|
|
|
|
|
|
env_var_t::env_var_flags_t env_var_t::flags_for(const wchar_t *name) {
|
|
|
|
env_var_flags_t result = 0;
|
|
|
|
if (is_read_only(name)) result |= flag_read_only;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \return a singleton empty list, to avoid unnecessary allocations in env_var_t.
|
|
|
|
std::shared_ptr<const wcstring_list_t> env_var_t::empty_list() {
|
2019-04-29 01:13:55 +00:00
|
|
|
static const auto s_empty_result = std::make_shared<const wcstring_list_t>();
|
|
|
|
return s_empty_result;
|
2019-04-14 21:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
environment_t::~environment_t() = default;
|
|
|
|
|
|
|
|
wcstring environment_t::get_pwd_slash() const {
|
|
|
|
// Return "/" if PWD is missing.
|
|
|
|
// See https://github.com/fish-shell/fish-shell/issues/5080
|
|
|
|
auto pwd_var = get(L"PWD");
|
|
|
|
wcstring pwd;
|
|
|
|
if (!pwd_var.missing_or_empty()) {
|
|
|
|
pwd = pwd_var->as_string();
|
|
|
|
}
|
|
|
|
if (!string_suffixes_string(L"/", pwd)) {
|
|
|
|
pwd.push_back(L'/');
|
|
|
|
}
|
|
|
|
return pwd;
|
|
|
|
}
|
|
|
|
|
|
|
|
null_environment_t::~null_environment_t() = default;
|
|
|
|
maybe_t<env_var_t> null_environment_t::get(const wcstring &key, env_mode_flags_t mode) const {
|
|
|
|
UNUSED(key);
|
|
|
|
UNUSED(mode);
|
|
|
|
return none();
|
|
|
|
}
|
|
|
|
wcstring_list_t null_environment_t::get_names(int flags) const {
|
|
|
|
UNUSED(flags);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-05-02 15:15:52 +00:00
|
|
|
/// Set up the USER and HOME variable.
|
|
|
|
static void setup_user() {
|
2018-09-18 04:26:21 +00:00
|
|
|
auto &vars = env_stack_t::globals();
|
2022-05-02 15:15:52 +00:00
|
|
|
auto uid = geteuid();
|
|
|
|
auto user_var = vars.get(L"USER");
|
|
|
|
struct passwd userinfo;
|
|
|
|
struct passwd *result;
|
|
|
|
char buf[8192];
|
|
|
|
|
|
|
|
// If we have a $USER, we try to get the passwd entry for the name.
|
|
|
|
// If that has the same UID that we use, we assume the data is correct.
|
|
|
|
if (!user_var.missing_or_empty()) {
|
|
|
|
std::string unam_narrow = wcs2string(user_var->as_string());
|
|
|
|
int retval = getpwnam_r(unam_narrow.c_str(), &userinfo, buf, sizeof(buf), &result);
|
2017-05-18 06:07:47 +00:00
|
|
|
if (!retval && result) {
|
2022-05-02 15:15:52 +00:00
|
|
|
if (result->pw_uid == uid) {
|
|
|
|
// The uid matches but we still might need to set $HOME.
|
|
|
|
if (vars.get(L"HOME").missing_or_empty()) {
|
|
|
|
if (userinfo.pw_dir) {
|
|
|
|
const wcstring dir = str2wcstring(userinfo.pw_dir);
|
|
|
|
vars.set_one(L"HOME", ENV_GLOBAL | ENV_EXPORT, dir);
|
|
|
|
} else {
|
|
|
|
vars.set_empty(L"HOME", ENV_GLOBAL | ENV_EXPORT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Either we didn't have a $USER or it had a different uid.
|
|
|
|
// We need to get the data *again* via the uid.
|
|
|
|
int retval = getpwuid_r(uid, &userinfo, buf, sizeof(buf), &result);
|
|
|
|
if (!retval && result) {
|
|
|
|
const wcstring uname = str2wcstring(userinfo.pw_name);
|
|
|
|
vars.set_one(L"USER", ENV_GLOBAL | ENV_EXPORT, uname);
|
|
|
|
// Only change $HOME if it's empty, so we allow e.g. `HOME=(mktemp -d)`.
|
|
|
|
// This is okay with common `su` and `sudo` because they set $HOME.
|
|
|
|
if (vars.get(L"HOME").missing_or_empty()) {
|
|
|
|
if (userinfo.pw_dir) {
|
|
|
|
const wcstring dir = str2wcstring(userinfo.pw_dir);
|
|
|
|
vars.set_one(L"HOME", ENV_GLOBAL | ENV_EXPORT, dir);
|
|
|
|
} else {
|
|
|
|
// We cannot get $HOME. This triggers warnings for history and config.fish already,
|
|
|
|
// so it isn't necessary to warn here as well.
|
|
|
|
vars.set_empty(L"HOME", ENV_GLOBAL | ENV_EXPORT);
|
|
|
|
}
|
2016-11-28 17:24:49 +00:00
|
|
|
}
|
2022-05-02 15:15:52 +00:00
|
|
|
} else if (vars.get(L"HOME").missing_or_empty()) {
|
|
|
|
// If $USER is empty as well (which we tried to set above), we can't get $HOME.
|
|
|
|
vars.set_empty(L"HOME", ENV_GLOBAL | ENV_EXPORT);
|
2016-11-28 17:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 04:09:26 +00:00
|
|
|
/// Various things we need to initialize at run-time that don't really fit any of the other init
|
|
|
|
/// routines.
|
|
|
|
void misc_init() {
|
|
|
|
// 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.
|
|
|
|
if (isatty(STDOUT_FILENO)) {
|
|
|
|
fflush(stdout);
|
2019-11-19 02:34:50 +00:00
|
|
|
setvbuf(stdout, nullptr, _IONBF, 0);
|
2017-02-16 04:09:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-14 21:01:23 +00:00
|
|
|
/// Make sure the PATH variable contains something.
|
|
|
|
static void setup_path() {
|
|
|
|
auto &vars = env_stack_t::globals();
|
|
|
|
const auto path = vars.get(L"PATH");
|
|
|
|
if (path.missing_or_empty()) {
|
|
|
|
#if defined(_CS_PATH)
|
|
|
|
// _CS_PATH: colon-separated paths to find POSIX utilities
|
|
|
|
std::string cspath;
|
|
|
|
cspath.resize(confstr(_CS_PATH, nullptr, 0));
|
2021-05-24 18:19:01 +00:00
|
|
|
if (cspath.length() > 0) {
|
|
|
|
confstr(_CS_PATH, &cspath[0], cspath.length());
|
|
|
|
// remove the trailing null-terminator
|
|
|
|
cspath.resize(cspath.length() - 1);
|
|
|
|
}
|
2019-04-14 21:01:23 +00:00
|
|
|
#else
|
|
|
|
std::string cspath = "/usr/bin:/bin"; // I doubt this is even necessary
|
|
|
|
#endif
|
|
|
|
vars.set_one(L"PATH", ENV_GLOBAL | ENV_EXPORT, str2wcstring(cspath));
|
|
|
|
}
|
|
|
|
}
|
2017-08-19 00:26:45 +00:00
|
|
|
|
2021-05-01 17:43:31 +00:00
|
|
|
void env_init(const struct config_paths_t *paths, bool do_uvars, bool default_paths) {
|
2019-05-10 01:38:18 +00:00
|
|
|
env_stack_t &vars = env_stack_t::principal();
|
2016-04-29 01:26:46 +00:00
|
|
|
// Import environment variables. Walk backwards so that the first one out of any duplicates wins
|
2017-04-04 06:16:11 +00:00
|
|
|
// (See issue #2784).
|
2016-03-06 03:07:00 +00:00
|
|
|
wcstring key, val;
|
2016-05-01 00:46:14 +00:00
|
|
|
const char *const *envp = environ;
|
2021-10-31 10:50:22 +00:00
|
|
|
int i = 0;
|
2017-08-05 22:08:39 +00:00
|
|
|
while (envp && envp[i]) i++;
|
2016-04-29 01:26:46 +00:00
|
|
|
while (i--) {
|
|
|
|
const wcstring key_and_val = str2wcstring(envp[i]); // like foo=bar
|
2012-12-19 21:31:06 +00:00
|
|
|
size_t eql = key_and_val.find(L'=');
|
2016-04-29 01:26:46 +00:00
|
|
|
if (eql == wcstring::npos) {
|
2017-08-05 22:08:39 +00:00
|
|
|
// No equal-sign found so treat it as a defined var that has no value(s).
|
2019-05-12 02:09:22 +00:00
|
|
|
if (!electric_var_t::for_name(key_and_val)) {
|
|
|
|
vars.set_empty(key_and_val, ENV_EXPORT | ENV_GLOBAL);
|
|
|
|
}
|
2016-04-29 01:26:46 +00:00
|
|
|
} else {
|
2016-03-06 03:07:00 +00:00
|
|
|
key.assign(key_and_val, 0, eql);
|
2019-05-05 10:09:25 +00:00
|
|
|
val.assign(key_and_val, eql + 1, wcstring::npos);
|
2019-05-12 02:09:22 +00:00
|
|
|
if (!electric_var_t::for_name(key)) {
|
2020-10-26 02:45:45 +00:00
|
|
|
// fish_user_paths should not be exported; attempting to re-import it from
|
|
|
|
// a value we previously (due to user error) exported will cause impossibly
|
|
|
|
// difficult to debug PATH problems.
|
|
|
|
if (key != L"fish_user_paths") {
|
|
|
|
vars.set(key, ENV_EXPORT | ENV_GLOBAL, {val});
|
|
|
|
}
|
2019-05-12 02:09:22 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
// Set the given paths in the environment, if we have any.
|
2019-11-19 02:34:50 +00:00
|
|
|
if (paths != nullptr) {
|
2018-09-14 07:36:26 +00:00
|
|
|
vars.set_one(FISH_DATADIR_VAR, ENV_GLOBAL, paths->data);
|
|
|
|
vars.set_one(FISH_SYSCONFDIR_VAR, ENV_GLOBAL, paths->sysconf);
|
|
|
|
vars.set_one(FISH_HELPDIR_VAR, ENV_GLOBAL, paths->doc);
|
|
|
|
vars.set_one(FISH_BIN_DIR, ENV_GLOBAL, paths->bin);
|
2021-05-01 17:43:31 +00:00
|
|
|
if (default_paths) {
|
2021-05-10 02:48:43 +00:00
|
|
|
wcstring scstr = paths->data;
|
2021-05-01 17:43:31 +00:00
|
|
|
scstr.append(L"/functions");
|
|
|
|
vars.set_one(L"fish_function_path", ENV_GLOBAL, scstr);
|
|
|
|
}
|
2012-07-18 17:50:38 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2022-05-02 15:15:52 +00:00
|
|
|
// Set $USER, $HOME and $EUID
|
|
|
|
// This involves going to passwd and stuff.
|
2022-04-15 13:57:57 +00:00
|
|
|
vars.set_one(L"EUID", ENV_GLOBAL, to_string(static_cast<unsigned long long>(geteuid())));
|
2022-05-02 15:15:52 +00:00
|
|
|
setup_user();
|
2014-07-25 17:42:22 +00:00
|
|
|
|
2021-01-11 17:49:15 +00:00
|
|
|
wcstring user_config_dir;
|
|
|
|
path_get_config(user_config_dir);
|
|
|
|
vars.set_one(FISH_CONFIG_DIR, ENV_GLOBAL, user_config_dir);
|
|
|
|
|
|
|
|
wcstring user_data_dir;
|
|
|
|
path_get_data(user_data_dir);
|
|
|
|
vars.set_one(FISH_USER_DATA_DIR, ENV_GLOBAL, user_data_dir);
|
|
|
|
|
|
|
|
// Set up a default PATH
|
|
|
|
setup_path();
|
|
|
|
|
|
|
|
// Set up $IFS - this used to be in share/config.fish, but really breaks if it isn't done.
|
|
|
|
vars.set_one(L"IFS", ENV_GLOBAL, L"\n \t");
|
|
|
|
|
|
|
|
// Set up the version variable.
|
|
|
|
wcstring version = str2wcstring(get_fish_version());
|
|
|
|
vars.set_one(L"version", ENV_GLOBAL, version);
|
|
|
|
vars.set_one(L"FISH_VERSION", ENV_GLOBAL, version);
|
|
|
|
|
|
|
|
// Set the $fish_pid variable.
|
|
|
|
vars.set_one(L"fish_pid", ENV_GLOBAL, to_string(getpid()));
|
|
|
|
|
|
|
|
// Set the $hostname variable
|
|
|
|
wcstring hostname = L"fish";
|
|
|
|
get_hostname_identifier(hostname);
|
|
|
|
vars.set_one(L"hostname", ENV_GLOBAL, hostname);
|
|
|
|
|
|
|
|
// Set up SHLVL variable. Not we can't use vars.get() because SHLVL is read-only, and therefore
|
|
|
|
// was not inherited from the environment.
|
Don't touch $SHLVL if not interactive
It's not super clear what $SHLVL is useful for, but the current
definition is essentially
"number of shells in the parent processes + 1"
which isn't *super useful*?
Bash's behavior here is a bit weird in that it increments $SHLVL
basically always, but since it auto-execs the last process it will
decrement it again, so in practice it's often not incremented.
E.g.
```
> echo $SHLVL
1
> bash -c 'echo $SHLVL; bash'
2
>> echo $SHLVL
2
```
Both bashes here end up having the same $SHLVL because this is
equivalent to `echo $SHLVL; exec bash`. Running `echo $SHLVL` and then
`bash -c 'echo $SHLVL'` in an interactive bash will have a different
result (1 and 2) because that doesn't *exec* the inner bash.
That's not something we want to get into, so what we do is increment
$SHLVL in every interactive fish. Non-interactive fish will simply
import the existing value.
That means if you had e.g. a bash that runs a fish script that ends up
opening a new fish session, you would have a $SHLVL of *2* - one for the
bash, and one for the inner fish.
We key this off is_interactive_session() (which can also be enabled
via `fish -i`) because it's easy and because `fish -i` is asking for
fish to be, in some form, "interactive".
That means most of the time $SHLVL will be "how many shells am I deep,
how often do I have to `exit`", except for when you specifically asked
for a fish to be "interactive". If that's a problem, we can rethink it.
Fixes #7864.
2021-03-29 15:35:55 +00:00
|
|
|
if (is_interactive_session()) {
|
|
|
|
wcstring nshlvl_str = L"1";
|
|
|
|
if (const char *shlvl_var = getenv("SHLVL")) {
|
|
|
|
const wchar_t *end;
|
2021-04-21 20:31:58 +00:00
|
|
|
// TODO: Figure out how to handle invalid numbers better. Shouldn't we issue a
|
|
|
|
// diagnostic?
|
Don't touch $SHLVL if not interactive
It's not super clear what $SHLVL is useful for, but the current
definition is essentially
"number of shells in the parent processes + 1"
which isn't *super useful*?
Bash's behavior here is a bit weird in that it increments $SHLVL
basically always, but since it auto-execs the last process it will
decrement it again, so in practice it's often not incremented.
E.g.
```
> echo $SHLVL
1
> bash -c 'echo $SHLVL; bash'
2
>> echo $SHLVL
2
```
Both bashes here end up having the same $SHLVL because this is
equivalent to `echo $SHLVL; exec bash`. Running `echo $SHLVL` and then
`bash -c 'echo $SHLVL'` in an interactive bash will have a different
result (1 and 2) because that doesn't *exec* the inner bash.
That's not something we want to get into, so what we do is increment
$SHLVL in every interactive fish. Non-interactive fish will simply
import the existing value.
That means if you had e.g. a bash that runs a fish script that ends up
opening a new fish session, you would have a $SHLVL of *2* - one for the
bash, and one for the inner fish.
We key this off is_interactive_session() (which can also be enabled
via `fish -i`) because it's easy and because `fish -i` is asking for
fish to be, in some form, "interactive".
That means most of the time $SHLVL will be "how many shells am I deep,
how often do I have to `exit`", except for when you specifically asked
for a fish to be "interactive". If that's a problem, we can rethink it.
Fixes #7864.
2021-03-29 15:35:55 +00:00
|
|
|
long shlvl_i = fish_wcstol(str2wcstring(shlvl_var).c_str(), &end);
|
|
|
|
if (!errno && shlvl_i >= 0) {
|
|
|
|
nshlvl_str = to_string(shlvl_i + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vars.set_one(L"SHLVL", ENV_GLOBAL | ENV_EXPORT, nshlvl_str);
|
|
|
|
} else {
|
|
|
|
// If we're not interactive, simply pass the value along.
|
|
|
|
if (const char *shlvl_var = getenv("SHLVL")) {
|
|
|
|
vars.set_one(L"SHLVL", ENV_GLOBAL | ENV_EXPORT, str2wcstring(shlvl_var));
|
2021-01-11 17:49:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 01:03:13 +00:00
|
|
|
// initialize the PWD variable if necessary
|
2019-02-18 21:11:01 +00:00
|
|
|
// Note we may inherit a virtual PWD that doesn't match what getcwd would return; respect that
|
|
|
|
// if and only if it matches getcwd (#5647). Note we treat PWD as read-only so it was not set in
|
|
|
|
// vars.
|
2021-01-17 22:03:15 +00:00
|
|
|
//
|
|
|
|
// Also reject all paths that don't start with "/", this includes windows paths like "F:\foo".
|
|
|
|
// (see #7636)
|
2019-02-18 21:11:01 +00:00
|
|
|
const char *incoming_pwd_cstr = getenv("PWD");
|
|
|
|
wcstring incoming_pwd = incoming_pwd_cstr ? str2wcstring(incoming_pwd_cstr) : wcstring{};
|
2021-04-21 20:31:58 +00:00
|
|
|
if (!incoming_pwd.empty() && incoming_pwd.front() == L'/' &&
|
|
|
|
paths_are_same_file(incoming_pwd, L".")) {
|
2019-02-18 21:11:01 +00:00
|
|
|
vars.set_one(L"PWD", ENV_EXPORT | ENV_GLOBAL, incoming_pwd);
|
2019-01-22 21:07:55 +00:00
|
|
|
} else {
|
2018-09-11 02:17:44 +00:00
|
|
|
vars.set_pwd_from_getcwd();
|
2018-09-17 01:03:13 +00:00
|
|
|
}
|
2020-06-07 22:46:54 +00:00
|
|
|
|
|
|
|
// Initialize termsize variables.
|
2020-06-07 23:05:52 +00:00
|
|
|
auto termsize = termsize_container_t::shared().initialize(vars);
|
2020-06-07 22:46:54 +00:00
|
|
|
if (vars.get(L"COLUMNS").missing_or_empty())
|
2020-06-07 23:05:52 +00:00
|
|
|
vars.set_one(L"COLUMNS", ENV_GLOBAL, to_string(termsize.width));
|
2020-06-07 22:46:54 +00:00
|
|
|
if (vars.get(L"LINES").missing_or_empty())
|
2020-06-07 23:05:52 +00:00
|
|
|
vars.set_one(L"LINES", ENV_GLOBAL, to_string(termsize.height));
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
// Set fish_bind_mode to "default".
|
2018-09-11 02:17:44 +00:00
|
|
|
vars.set_one(FISH_BIND_MODE_VAR, ENV_GLOBAL, DEFAULT_BIND_MODE);
|
2014-07-13 20:21:06 +00:00
|
|
|
|
2019-03-26 03:18:00 +00:00
|
|
|
// Allow changes to variables to produce events.
|
2019-04-01 02:51:08 +00:00
|
|
|
env_dispatch_init(vars);
|
2017-07-14 17:45:31 +00:00
|
|
|
|
2019-04-08 20:32:25 +00:00
|
|
|
init_input();
|
|
|
|
|
2019-05-02 00:31:22 +00:00
|
|
|
// Complain about invalid config paths.
|
2021-07-26 19:28:37 +00:00
|
|
|
// HACK: Assume the defaults are correct (in practice this is only --no-config anyway).
|
|
|
|
if (!default_paths) {
|
|
|
|
path_emit_config_directory_messages(vars);
|
|
|
|
}
|
2019-05-02 00:31:22 +00:00
|
|
|
|
2021-05-10 21:06:17 +00:00
|
|
|
// Initialize our uvars if requested.
|
2021-05-10 02:48:43 +00:00
|
|
|
if (!do_uvars) {
|
|
|
|
s_uvar_scope_is_global = true;
|
|
|
|
} else {
|
2021-05-10 00:40:02 +00:00
|
|
|
// Set up universal variables using the default path.
|
2021-04-11 09:08:21 +00:00
|
|
|
callback_data_list_t callbacks;
|
2021-05-10 21:06:17 +00:00
|
|
|
uvars()->initialize(callbacks);
|
2021-04-11 09:08:21 +00:00
|
|
|
env_universal_callbacks(&vars, callbacks);
|
|
|
|
|
|
|
|
// Do not import variables that have the same name and value as
|
|
|
|
// an exported universal variable. See issues #5258 and #5348.
|
2021-05-10 21:06:17 +00:00
|
|
|
var_table_t table = uvars()->get_table();
|
|
|
|
for (const auto &kv : table) {
|
2021-04-11 09:08:21 +00:00
|
|
|
const wcstring &name = kv.first;
|
|
|
|
const env_var_t &uvar = kv.second;
|
|
|
|
if (!uvar.exports()) continue;
|
|
|
|
// Look for a global exported variable with the same name.
|
|
|
|
maybe_t<env_var_t> global = vars.globals().get(name, ENV_GLOBAL | ENV_EXPORT);
|
|
|
|
if (global && uvar.as_string() == global->as_string()) {
|
|
|
|
vars.globals().remove(name, ENV_GLOBAL | ENV_EXPORT);
|
|
|
|
}
|
2019-10-17 10:41:13 +00:00
|
|
|
}
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2017-08-28 09:51:34 +00:00
|
|
|
static int set_umask(const wcstring_list_t &list_val) {
|
2017-08-19 22:45:46 +00:00
|
|
|
long mask = -1;
|
2017-08-28 09:51:34 +00:00
|
|
|
if (list_val.size() == 1 && !list_val.front().empty()) {
|
2019-11-19 02:34:50 +00:00
|
|
|
mask = fish_wcstol(list_val.front().c_str(), nullptr, 8);
|
2017-08-19 22:45:46 +00:00
|
|
|
}
|
2017-08-05 22:08:39 +00:00
|
|
|
|
2017-08-19 22:45:46 +00:00
|
|
|
if (errno || mask > 0777 || mask < 0) return ENV_INVALID;
|
2018-09-25 04:17:05 +00:00
|
|
|
// Do not actually create a umask variable. On env_stack_t::get() it will be calculated.
|
2017-08-19 22:45:46 +00:00
|
|
|
umask(mask);
|
|
|
|
return ENV_OK;
|
2017-08-05 22:08:39 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
namespace {
|
|
|
|
struct query_t {
|
|
|
|
// Whether any scopes were specified.
|
|
|
|
bool has_scope;
|
2018-10-22 07:49:09 +00:00
|
|
|
|
Add `set --function` (#8145)
* Add `set --function`
This makes the function's scope available, even inside of blocks. Outside of blocks it's the toplevel local scope.
This removes the need to declare variables locally before use, and will probably end up being the main way variables get set.
E.g.:
```fish
set -l thing
if condition
set thing one
else
set thing two
end
```
could be written as
```fish
if condition
set -f thing one
else
set -f thing two
end
```
Note: Many scripts shipped with fish use workarounds like `and`/`or`
instead of `if`, so it isn't easy to find good examples.
Also, if there isn't an else-branch in that above, just with
```fish
if condition
set -f thing one
end
```
that means something different from setting it before! Now, if
`condition` isn't true, it would use a global (or universal) variable of
te same name!
Some more interesting parts:
Because it *is* a local scope, setting a variable `-f` and
`-l` in the toplevel of a function ends up the same:
```fish
function foo2
set -l foo bar
set -f foo baz # modifies the *same* variable!
end
```
but setting it locally inside a block creates a new local variable
that shadows the function-scoped variable:
```fish
function foo3
set -f foo bar
begin
set -l foo banana
# $foo is banana
end
# $foo is bar again
end
```
This is how local variables already work. "Local" is actually "block-scoped".
Also `set --show` will only show the closest local scope, so it won't
show a shadowed function-level variable. Again, this is how local
variables already work, and could be done as a separate change.
As a fun tidbit, functions with --no-scope-shadowing can now use this to set variables in the calling function. That's probably okay given that it's already an escape hatch (but to be clear: if it turns out to problematic I reserve the right to remove it).
Fixes #565
2021-08-01 18:08:12 +00:00
|
|
|
// Whether to search local, function, global, universal scopes.
|
2019-05-10 16:10:43 +00:00
|
|
|
bool local;
|
Add `set --function` (#8145)
* Add `set --function`
This makes the function's scope available, even inside of blocks. Outside of blocks it's the toplevel local scope.
This removes the need to declare variables locally before use, and will probably end up being the main way variables get set.
E.g.:
```fish
set -l thing
if condition
set thing one
else
set thing two
end
```
could be written as
```fish
if condition
set -f thing one
else
set -f thing two
end
```
Note: Many scripts shipped with fish use workarounds like `and`/`or`
instead of `if`, so it isn't easy to find good examples.
Also, if there isn't an else-branch in that above, just with
```fish
if condition
set -f thing one
end
```
that means something different from setting it before! Now, if
`condition` isn't true, it would use a global (or universal) variable of
te same name!
Some more interesting parts:
Because it *is* a local scope, setting a variable `-f` and
`-l` in the toplevel of a function ends up the same:
```fish
function foo2
set -l foo bar
set -f foo baz # modifies the *same* variable!
end
```
but setting it locally inside a block creates a new local variable
that shadows the function-scoped variable:
```fish
function foo3
set -f foo bar
begin
set -l foo banana
# $foo is banana
end
# $foo is bar again
end
```
This is how local variables already work. "Local" is actually "block-scoped".
Also `set --show` will only show the closest local scope, so it won't
show a shadowed function-level variable. Again, this is how local
variables already work, and could be done as a separate change.
As a fun tidbit, functions with --no-scope-shadowing can now use this to set variables in the calling function. That's probably okay given that it's already an escape hatch (but to be clear: if it turns out to problematic I reserve the right to remove it).
Fixes #565
2021-08-01 18:08:12 +00:00
|
|
|
bool function;
|
2019-05-10 16:10:43 +00:00
|
|
|
bool global;
|
|
|
|
bool universal;
|
2018-10-27 22:20:32 +00:00
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
// Whether export or unexport was specified.
|
|
|
|
bool has_export_unexport;
|
2018-10-22 07:49:09 +00:00
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
// Whether to search exported and unexported variables.
|
|
|
|
bool exports;
|
|
|
|
bool unexports;
|
2018-10-22 07:49:09 +00:00
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
// Whether pathvar or unpathvar was set.
|
|
|
|
bool has_pathvar_unpathvar;
|
|
|
|
bool pathvar;
|
|
|
|
bool unpathvar;
|
2019-05-10 01:38:18 +00:00
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
// Whether this is a "user" set.
|
|
|
|
bool user;
|
|
|
|
|
|
|
|
explicit query_t(env_mode_flags_t mode) {
|
Add `set --function` (#8145)
* Add `set --function`
This makes the function's scope available, even inside of blocks. Outside of blocks it's the toplevel local scope.
This removes the need to declare variables locally before use, and will probably end up being the main way variables get set.
E.g.:
```fish
set -l thing
if condition
set thing one
else
set thing two
end
```
could be written as
```fish
if condition
set -f thing one
else
set -f thing two
end
```
Note: Many scripts shipped with fish use workarounds like `and`/`or`
instead of `if`, so it isn't easy to find good examples.
Also, if there isn't an else-branch in that above, just with
```fish
if condition
set -f thing one
end
```
that means something different from setting it before! Now, if
`condition` isn't true, it would use a global (or universal) variable of
te same name!
Some more interesting parts:
Because it *is* a local scope, setting a variable `-f` and
`-l` in the toplevel of a function ends up the same:
```fish
function foo2
set -l foo bar
set -f foo baz # modifies the *same* variable!
end
```
but setting it locally inside a block creates a new local variable
that shadows the function-scoped variable:
```fish
function foo3
set -f foo bar
begin
set -l foo banana
# $foo is banana
end
# $foo is bar again
end
```
This is how local variables already work. "Local" is actually "block-scoped".
Also `set --show` will only show the closest local scope, so it won't
show a shadowed function-level variable. Again, this is how local
variables already work, and could be done as a separate change.
As a fun tidbit, functions with --no-scope-shadowing can now use this to set variables in the calling function. That's probably okay given that it's already an escape hatch (but to be clear: if it turns out to problematic I reserve the right to remove it).
Fixes #565
2021-08-01 18:08:12 +00:00
|
|
|
has_scope = mode & (ENV_LOCAL | ENV_FUNCTION | ENV_GLOBAL | ENV_UNIVERSAL);
|
2019-05-10 16:10:43 +00:00
|
|
|
local = !has_scope || (mode & ENV_LOCAL);
|
Add `set --function` (#8145)
* Add `set --function`
This makes the function's scope available, even inside of blocks. Outside of blocks it's the toplevel local scope.
This removes the need to declare variables locally before use, and will probably end up being the main way variables get set.
E.g.:
```fish
set -l thing
if condition
set thing one
else
set thing two
end
```
could be written as
```fish
if condition
set -f thing one
else
set -f thing two
end
```
Note: Many scripts shipped with fish use workarounds like `and`/`or`
instead of `if`, so it isn't easy to find good examples.
Also, if there isn't an else-branch in that above, just with
```fish
if condition
set -f thing one
end
```
that means something different from setting it before! Now, if
`condition` isn't true, it would use a global (or universal) variable of
te same name!
Some more interesting parts:
Because it *is* a local scope, setting a variable `-f` and
`-l` in the toplevel of a function ends up the same:
```fish
function foo2
set -l foo bar
set -f foo baz # modifies the *same* variable!
end
```
but setting it locally inside a block creates a new local variable
that shadows the function-scoped variable:
```fish
function foo3
set -f foo bar
begin
set -l foo banana
# $foo is banana
end
# $foo is bar again
end
```
This is how local variables already work. "Local" is actually "block-scoped".
Also `set --show` will only show the closest local scope, so it won't
show a shadowed function-level variable. Again, this is how local
variables already work, and could be done as a separate change.
As a fun tidbit, functions with --no-scope-shadowing can now use this to set variables in the calling function. That's probably okay given that it's already an escape hatch (but to be clear: if it turns out to problematic I reserve the right to remove it).
Fixes #565
2021-08-01 18:08:12 +00:00
|
|
|
function = !has_scope || (mode & ENV_FUNCTION);
|
2019-05-10 16:10:43 +00:00
|
|
|
global = !has_scope || (mode & ENV_GLOBAL);
|
|
|
|
universal = !has_scope || (mode & ENV_UNIVERSAL);
|
|
|
|
|
|
|
|
has_export_unexport = mode & (ENV_EXPORT | ENV_UNEXPORT);
|
|
|
|
exports = !has_export_unexport || (mode & ENV_EXPORT);
|
|
|
|
unexports = !has_export_unexport || (mode & ENV_UNEXPORT);
|
|
|
|
|
|
|
|
// note we don't use pathvar for searches, so these don't default to true if unspecified.
|
|
|
|
has_pathvar_unpathvar = mode & (ENV_PATHVAR | ENV_UNPATHVAR);
|
|
|
|
pathvar = mode & ENV_PATHVAR;
|
|
|
|
unpathvar = mode & ENV_UNPATHVAR;
|
|
|
|
|
|
|
|
user = mode & ENV_USER;
|
2019-05-10 01:04:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
bool export_matches(const env_var_t &var) const {
|
|
|
|
if (has_export_unexport) {
|
|
|
|
return var.exports() ? exports : unexports;
|
2019-05-10 01:04:30 +00:00
|
|
|
} else {
|
2019-05-10 16:10:43 +00:00
|
|
|
return true;
|
2019-05-10 01:04:30 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-26 17:29:10 +00:00
|
|
|
|
|
|
|
bool pathvar_matches(const env_var_t &var) const {
|
|
|
|
if (has_pathvar_unpathvar) {
|
|
|
|
return var.is_pathvar() ? pathvar : unpathvar;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-05-10 16:10:43 +00:00
|
|
|
};
|
2019-05-10 01:04:30 +00:00
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
// Struct representing one level in the function variable stack.
|
|
|
|
class env_node_t {
|
|
|
|
public:
|
|
|
|
/// Variable table.
|
|
|
|
var_table_t env;
|
|
|
|
/// Does this node imply a new variable scope? If yes, all non-global variables below this one
|
|
|
|
/// in the stack are invisible. If new_scope is set for the global variable node, the universe
|
|
|
|
/// will explode.
|
|
|
|
const bool new_scope;
|
2019-06-09 20:25:30 +00:00
|
|
|
/// The export generation. If this is nonzero, then we contain a variable that is exported to
|
|
|
|
/// subshells, or redefines a variable to not be exported.
|
|
|
|
export_generation_t export_gen = 0;
|
2019-05-10 16:10:43 +00:00
|
|
|
/// Pointer to next level.
|
|
|
|
const std::shared_ptr<env_node_t> next;
|
2019-05-10 01:04:30 +00:00
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
env_node_t(bool is_new_scope, std::shared_ptr<env_node_t> next_scope)
|
|
|
|
: new_scope(is_new_scope), next(std::move(next_scope)) {}
|
2019-05-10 01:04:30 +00:00
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
maybe_t<env_var_t> find_entry(const wcstring &key) {
|
|
|
|
auto it = env.find(key);
|
|
|
|
if (it != env.end()) return it->second;
|
|
|
|
return none();
|
|
|
|
}
|
2019-06-09 20:25:30 +00:00
|
|
|
|
|
|
|
bool exports() const { return export_gen > 0; }
|
|
|
|
|
|
|
|
void changed_exported() { export_gen = next_export_generation(); }
|
2019-05-10 16:10:43 +00:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
using env_node_ref_t = std::shared_ptr<env_node_t>;
|
2021-07-22 17:43:25 +00:00
|
|
|
class env_scoped_impl_t : public environment_t, noncopyable_t {
|
2019-05-10 16:10:43 +00:00
|
|
|
/// A struct wrapping up parser-local variables. These are conceptually variables that differ in
|
|
|
|
/// different fish internal processes.
|
|
|
|
struct perproc_data_t {
|
2019-05-12 21:00:44 +00:00
|
|
|
wcstring pwd{};
|
|
|
|
statuses_t statuses{statuses_t::just(0)};
|
2019-05-10 16:10:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
env_scoped_impl_t(env_node_ref_t locals, env_node_ref_t globals)
|
|
|
|
: locals_(std::move(locals)), globals_(std::move(globals)) {
|
|
|
|
assert(locals_ && globals_ && "Nodes cannot be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
maybe_t<env_var_t> get(const wcstring &key, env_mode_flags_t mode = ENV_DEFAULT) const override;
|
|
|
|
wcstring_list_t get_names(int flags) const override;
|
|
|
|
|
|
|
|
perproc_data_t &perproc_data() { return perproc_data_; }
|
|
|
|
const perproc_data_t &perproc_data() const { return perproc_data_; }
|
|
|
|
|
|
|
|
std::shared_ptr<environment_t> snapshot() const;
|
|
|
|
|
2019-11-20 06:17:30 +00:00
|
|
|
~env_scoped_impl_t() override = default;
|
2019-05-10 16:10:43 +00:00
|
|
|
|
2021-02-14 21:15:29 +00:00
|
|
|
std::shared_ptr<owning_null_terminated_array_t> export_array();
|
2019-05-10 16:10:43 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// A linked list of scopes.
|
|
|
|
env_node_ref_t locals_{};
|
|
|
|
|
|
|
|
// Global scopes. There is no parent here.
|
|
|
|
env_node_ref_t globals_{};
|
|
|
|
|
|
|
|
// Per process data.
|
|
|
|
perproc_data_t perproc_data_{};
|
|
|
|
|
|
|
|
// Exported variable array used by execv.
|
2021-02-14 21:15:29 +00:00
|
|
|
std::shared_ptr<owning_null_terminated_array_t> export_array_{};
|
2019-05-10 16:10:43 +00:00
|
|
|
|
2019-06-09 20:25:30 +00:00
|
|
|
// Cached list of export generations corresponding to the above export_array_.
|
|
|
|
// If this differs from the current export generations then we need to regenerate the array.
|
|
|
|
std::vector<export_generation_t> export_array_generations_{};
|
2019-05-10 16:10:43 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// These "try" methods return true on success, false on failure. On a true return, \p result is
|
|
|
|
// populated. A maybe_t<maybe_t<...>> is a bridge too far.
|
|
|
|
// These may populate result with none() if a variable is present which does not match the
|
|
|
|
// query.
|
2019-05-13 01:23:00 +00:00
|
|
|
maybe_t<env_var_t> try_get_computed(const wcstring &key) const;
|
|
|
|
maybe_t<env_var_t> try_get_local(const wcstring &key) const;
|
2022-03-10 17:26:45 +00:00
|
|
|
maybe_t<env_var_t> try_get_function(const wcstring &key) const;
|
2019-05-13 01:23:00 +00:00
|
|
|
maybe_t<env_var_t> try_get_global(const wcstring &key) const;
|
|
|
|
maybe_t<env_var_t> try_get_universal(const wcstring &key) const;
|
2019-05-10 16:10:43 +00:00
|
|
|
|
2019-06-09 20:25:30 +00:00
|
|
|
/// Invoke a function on the current (nonzero) export generations, in order.
|
|
|
|
template <typename Func>
|
|
|
|
void enumerate_generations(const Func &func) const {
|
2019-06-09 20:48:07 +00:00
|
|
|
// Our uvars generation count doesn't come from next_export_generation(), so always supply
|
|
|
|
// it even if it's 0.
|
2021-05-10 02:48:43 +00:00
|
|
|
func(uvars()->get_export_generation());
|
2019-06-09 20:25:30 +00:00
|
|
|
if (globals_->exports()) func(globals_->export_gen);
|
|
|
|
for (auto node = locals_; node; node = node->next) {
|
|
|
|
if (node->exports()) func(node->export_gen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \return whether the current export array is empty or out-of-date.
|
|
|
|
bool export_array_needs_regeneration() const;
|
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
/// \return a newly allocated export array.
|
2021-02-14 21:15:29 +00:00
|
|
|
std::shared_ptr<owning_null_terminated_array_t> create_export_array() const;
|
2019-05-10 16:10:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Get the exported variables into a variable table.
|
|
|
|
static void get_exported(const env_node_ref_t &n, var_table_t &table) {
|
|
|
|
if (!n) return;
|
2019-05-10 01:04:30 +00:00
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
// Allow parent scopes to populate first, since we may want to overwrite those results.
|
|
|
|
get_exported(n->next, table);
|
2019-05-10 01:04:30 +00:00
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
for (const auto &kv : n->env) {
|
|
|
|
const wcstring &key = kv.first;
|
|
|
|
const env_var_t &var = kv.second;
|
|
|
|
if (var.exports()) {
|
|
|
|
// Export the variable. Don't use std::map::insert here, since we need to overwrite
|
|
|
|
// existing values from previous scopes.
|
|
|
|
table[key] = var;
|
2019-05-10 01:04:30 +00:00
|
|
|
} else {
|
2019-05-10 16:10:43 +00:00
|
|
|
// We need to erase from the map if we are not exporting, since a lower scope may have
|
|
|
|
// exported. See #2132.
|
|
|
|
table.erase(key);
|
2019-05-10 01:04:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-09 20:25:30 +00:00
|
|
|
bool env_scoped_impl_t::export_array_needs_regeneration() const {
|
|
|
|
// Check if our export array is stale. If we don't have one, it's obviously stale. Otherwise,
|
|
|
|
// compare our cached generations with the current generations. If they don't match exactly then
|
|
|
|
// our generation list is stale.
|
|
|
|
if (!export_array_) return true;
|
|
|
|
|
|
|
|
bool mismatch = false;
|
|
|
|
auto cursor = export_array_generations_.begin();
|
|
|
|
auto end = export_array_generations_.end();
|
|
|
|
enumerate_generations([&](export_generation_t gen) {
|
|
|
|
if (cursor != end && *cursor == gen) {
|
|
|
|
++cursor;
|
|
|
|
} else {
|
|
|
|
mismatch = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (cursor != end) {
|
|
|
|
mismatch = true;
|
|
|
|
}
|
|
|
|
return mismatch;
|
|
|
|
}
|
|
|
|
|
2021-02-14 21:15:29 +00:00
|
|
|
std::shared_ptr<owning_null_terminated_array_t> env_scoped_impl_t::create_export_array() const {
|
2019-06-09 20:21:58 +00:00
|
|
|
FLOG(env_export, L"create_export_array() recalc");
|
2019-05-10 16:10:43 +00:00
|
|
|
var_table_t vals;
|
|
|
|
get_exported(this->globals_, vals);
|
|
|
|
get_exported(this->locals_, vals);
|
|
|
|
|
2021-05-10 02:48:43 +00:00
|
|
|
const wcstring_list_t uni = uvars()->get_names(true, false);
|
|
|
|
for (const wcstring &key : uni) {
|
|
|
|
auto var = uvars()->get(key);
|
|
|
|
assert(var && "Variable should be present in uvars");
|
|
|
|
// Note that std::map::insert does NOT overwrite a value already in the map,
|
|
|
|
// which we depend on here.
|
2021-08-05 04:19:38 +00:00
|
|
|
// Note: Using std::move around emplace prevents the compiler from implementing
|
2021-05-10 02:48:43 +00:00
|
|
|
// copy elision.
|
2021-08-05 04:19:38 +00:00
|
|
|
vals.emplace(key, std::move(*var));
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2013-01-21 02:34:18 +00:00
|
|
|
|
2019-05-12 02:09:22 +00:00
|
|
|
// Dorky way to add our single exported computed variable.
|
2019-05-10 16:10:43 +00:00
|
|
|
vals[L"PWD"] = env_var_t(L"PWD", perproc_data().pwd);
|
|
|
|
|
|
|
|
// Construct the export list: a list of strings of the form key=value.
|
|
|
|
std::vector<std::string> export_list;
|
|
|
|
export_list.reserve(vals.size());
|
|
|
|
for (const auto &kv : vals) {
|
|
|
|
std::string str = wcs2string(kv.first);
|
|
|
|
str.push_back('=');
|
|
|
|
str.append(wcs2string(kv.second.as_string()));
|
|
|
|
export_list.push_back(std::move(str));
|
2014-07-12 21:05:42 +00:00
|
|
|
}
|
2021-02-14 21:15:29 +00:00
|
|
|
return std::make_shared<owning_null_terminated_array_t>(std::move(export_list));
|
2019-05-10 16:10:43 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 21:15:29 +00:00
|
|
|
std::shared_ptr<owning_null_terminated_array_t> env_scoped_impl_t::export_array() {
|
2019-05-10 16:10:43 +00:00
|
|
|
ASSERT_IS_NOT_FORKED_CHILD();
|
2019-06-09 20:25:30 +00:00
|
|
|
if (export_array_needs_regeneration()) {
|
2019-05-10 16:10:43 +00:00
|
|
|
export_array_ = create_export_array();
|
2019-06-09 20:25:30 +00:00
|
|
|
|
|
|
|
// Update our export array generations.
|
|
|
|
export_array_generations_.clear();
|
|
|
|
enumerate_generations(
|
|
|
|
[this](export_generation_t gen) { export_array_generations_.push_back(gen); });
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2019-05-10 16:10:43 +00:00
|
|
|
return export_array_;
|
|
|
|
}
|
2013-01-21 02:34:18 +00:00
|
|
|
|
2019-05-13 01:23:00 +00:00
|
|
|
maybe_t<env_var_t> env_scoped_impl_t::try_get_computed(const wcstring &key) const {
|
2019-05-12 02:09:22 +00:00
|
|
|
const electric_var_t *ev = electric_var_t::for_name(key);
|
|
|
|
if (!(ev && ev->computed())) {
|
2019-05-10 16:10:43 +00:00
|
|
|
return none();
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2019-05-10 16:10:43 +00:00
|
|
|
if (key == L"PWD") {
|
|
|
|
return env_var_t(perproc_data().pwd, env_var_t::flag_export);
|
|
|
|
} else if (key == L"history") {
|
|
|
|
// 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).
|
|
|
|
if (!is_main_thread()) {
|
|
|
|
return none();
|
|
|
|
}
|
2013-01-21 02:34:18 +00:00
|
|
|
|
2021-07-20 20:18:03 +00:00
|
|
|
std::shared_ptr<history_t> history = commandline_get_state().history;
|
2019-05-10 16:10:43 +00:00
|
|
|
if (!history) {
|
2021-01-10 00:22:32 +00:00
|
|
|
history = history_t::with_name(history_session_id(*this));
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2019-05-10 16:10:43 +00:00
|
|
|
wcstring_list_t result;
|
|
|
|
if (history) history->get_history(result);
|
|
|
|
return env_var_t(L"history", std::move(result));
|
2021-04-16 18:23:14 +00:00
|
|
|
} else if (key == L"fish_killring") {
|
|
|
|
return env_var_t(L"fish_killring", kill_entries());
|
2019-05-10 16:10:43 +00:00
|
|
|
} else if (key == L"pipestatus") {
|
2019-05-12 21:00:44 +00:00
|
|
|
const auto &js = perproc_data().statuses;
|
2019-05-10 16:10:43 +00:00
|
|
|
wcstring_list_t result;
|
|
|
|
result.reserve(js.pipestatus.size());
|
|
|
|
for (int i : js.pipestatus) {
|
|
|
|
result.push_back(to_string(i));
|
|
|
|
}
|
|
|
|
return env_var_t(L"pipestatus", std::move(result));
|
|
|
|
} else if (key == L"status") {
|
2019-05-12 21:00:44 +00:00
|
|
|
const auto &js = perproc_data().statuses;
|
|
|
|
return env_var_t(L"status", to_string(js.status));
|
2020-08-02 20:37:19 +00:00
|
|
|
} else if (key == L"status_generation") {
|
|
|
|
auto status_generation = reader_status_count();
|
|
|
|
return env_var_t(L"status_generation", to_string(status_generation));
|
2020-03-31 18:27:17 +00:00
|
|
|
} else if (key == L"fish_kill_signal") {
|
|
|
|
const auto &js = perproc_data().statuses;
|
|
|
|
return env_var_t(L"fish_kill_signal", to_string(js.kill_signal));
|
2019-05-10 16:10:43 +00:00
|
|
|
} else if (key == L"umask") {
|
|
|
|
// note umask() is an absurd API: you call it to set the value and it returns the old
|
|
|
|
// value. Thus we have to call it twice, to reset the value. The env_lock protects
|
|
|
|
// against races. Guess what the umask is; if we guess right we don't need to reset it.
|
2019-05-29 18:24:40 +00:00
|
|
|
mode_t guess = 022;
|
2019-05-10 16:10:43 +00:00
|
|
|
mode_t res = umask(guess);
|
|
|
|
if (res != guess) umask(res);
|
|
|
|
return env_var_t(L"umask", format_string(L"0%0.3o", res));
|
2012-02-17 20:23:30 +00:00
|
|
|
}
|
2019-05-10 16:10:43 +00:00
|
|
|
// We should never get here unless the electric var list is out of sync with the above code.
|
2019-05-12 02:09:22 +00:00
|
|
|
DIE("unrecognized computed var name");
|
2019-05-10 16:10:43 +00:00
|
|
|
}
|
2013-01-21 02:34:18 +00:00
|
|
|
|
2019-05-13 01:23:00 +00:00
|
|
|
maybe_t<env_var_t> env_scoped_impl_t::try_get_local(const wcstring &key) const {
|
2021-12-25 02:39:32 +00:00
|
|
|
maybe_t<env_var_t> entry;
|
2022-04-16 19:22:44 +00:00
|
|
|
for (auto cur = locals_; cur; cur = cur->next) {
|
2021-12-25 02:39:32 +00:00
|
|
|
if ((entry = cur->find_entry(key))) break;
|
2019-05-10 16:10:43 +00:00
|
|
|
}
|
2022-04-16 19:22:44 +00:00
|
|
|
return entry; // this is either the entry or none() from find_entry
|
2012-02-17 20:23:30 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2022-03-10 17:26:45 +00:00
|
|
|
maybe_t<env_var_t> env_scoped_impl_t::try_get_function(const wcstring &key) const {
|
|
|
|
maybe_t<env_var_t> entry;
|
|
|
|
auto node = locals_;
|
|
|
|
while (node->next) {
|
|
|
|
node = node->next;
|
|
|
|
// The first node that introduces a new scope is ours.
|
|
|
|
// If this doesn't happen, we go on until we've reached the
|
|
|
|
// topmost local scope.
|
|
|
|
if (node->new_scope) break;
|
|
|
|
}
|
2022-04-16 19:22:44 +00:00
|
|
|
for (auto cur = node; cur; cur = cur->next) {
|
2022-03-10 17:26:45 +00:00
|
|
|
if ((entry = cur->find_entry(key))) break;
|
|
|
|
}
|
2022-04-16 19:22:44 +00:00
|
|
|
return entry; // this is either the entry or none() from find_entry
|
2022-03-10 17:26:45 +00:00
|
|
|
}
|
|
|
|
|
2019-05-13 01:23:00 +00:00
|
|
|
maybe_t<env_var_t> env_scoped_impl_t::try_get_global(const wcstring &key) const {
|
2021-12-25 02:39:32 +00:00
|
|
|
return globals_->find_entry(key);
|
2017-08-19 22:45:46 +00:00
|
|
|
}
|
|
|
|
|
2019-05-13 01:23:00 +00:00
|
|
|
maybe_t<env_var_t> env_scoped_impl_t::try_get_universal(const wcstring &key) const {
|
2021-05-10 02:48:43 +00:00
|
|
|
return uvars()->get(key);
|
2017-08-19 22:45:46 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
maybe_t<env_var_t> env_scoped_impl_t::get(const wcstring &key, env_mode_flags_t mode) const {
|
|
|
|
const query_t query(mode);
|
|
|
|
|
2020-05-27 17:56:47 +00:00
|
|
|
maybe_t<env_var_t> result;
|
|
|
|
// Computed variables are effectively global and can't be shadowed.
|
|
|
|
if (query.global) {
|
|
|
|
result = try_get_computed(key);
|
|
|
|
}
|
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
if (!result && query.local) {
|
2019-05-13 01:23:00 +00:00
|
|
|
result = try_get_local(key);
|
2019-05-10 16:10:43 +00:00
|
|
|
}
|
2022-03-10 17:26:45 +00:00
|
|
|
if (!result && query.function) {
|
|
|
|
result = try_get_function(key);
|
|
|
|
}
|
2019-05-10 16:10:43 +00:00
|
|
|
if (!result && query.global) {
|
2019-05-13 01:23:00 +00:00
|
|
|
result = try_get_global(key);
|
2019-05-10 16:10:43 +00:00
|
|
|
}
|
|
|
|
if (!result && query.universal) {
|
2019-05-13 01:23:00 +00:00
|
|
|
result = try_get_universal(key);
|
2019-05-10 16:10:43 +00:00
|
|
|
}
|
|
|
|
// If the user requested only exported or unexported variables, enforce that here.
|
|
|
|
if (result && !query.export_matches(*result)) {
|
|
|
|
result = none();
|
|
|
|
}
|
2021-11-26 17:29:10 +00:00
|
|
|
// Same for pathvars
|
|
|
|
if (result && !query.pathvar_matches(*result)) {
|
|
|
|
result = none();
|
|
|
|
}
|
2019-05-10 16:10:43 +00:00
|
|
|
return result;
|
2017-08-19 22:45:46 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
wcstring_list_t env_scoped_impl_t::get_names(int flags) const {
|
|
|
|
const query_t query(flags);
|
|
|
|
std::set<wcstring> names;
|
|
|
|
|
|
|
|
// Helper to add the names of variables from \p envs to names, respecting show_exported and
|
|
|
|
// show_unexported.
|
|
|
|
auto add_keys = [&](const var_table_t &envs) {
|
|
|
|
for (const auto &kv : envs) {
|
|
|
|
if (query.export_matches(kv.second)) {
|
|
|
|
names.insert(kv.first);
|
|
|
|
}
|
2019-05-05 23:19:36 +00:00
|
|
|
}
|
2019-05-10 16:10:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (query.local) {
|
|
|
|
for (auto cursor = locals_; cursor != nullptr; cursor = cursor->next) {
|
|
|
|
add_keys(cursor->env);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query.global) {
|
|
|
|
add_keys(globals_->env);
|
|
|
|
// Add electrics.
|
2019-05-12 02:09:22 +00:00
|
|
|
for (const auto &ev : electric_variables) {
|
|
|
|
if (ev.exports() ? query.exports : query.unexports) {
|
|
|
|
names.insert(ev.name);
|
2019-05-05 23:19:36 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-10 16:10:43 +00:00
|
|
|
|
2021-05-10 02:48:43 +00:00
|
|
|
if (query.universal) {
|
2019-05-10 16:10:43 +00:00
|
|
|
const wcstring_list_t uni_list = uvars()->get_names(query.exports, query.unexports);
|
|
|
|
names.insert(uni_list.begin(), uni_list.end());
|
|
|
|
}
|
|
|
|
|
2021-10-28 08:46:29 +00:00
|
|
|
return {names.begin(), names.end()};
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
/// Recursive helper to snapshot a series of nodes.
|
|
|
|
static env_node_ref_t copy_node_chain(const env_node_ref_t &node) {
|
|
|
|
if (node == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
auto next = copy_node_chain(node->next);
|
|
|
|
auto result = std::make_shared<env_node_t>(node->new_scope, next);
|
|
|
|
// Copy over variables.
|
|
|
|
// Note assigning env is a potentially big copy.
|
2019-06-09 20:25:30 +00:00
|
|
|
result->export_gen = node->export_gen;
|
2019-05-10 16:10:43 +00:00
|
|
|
result->env = node->env;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<environment_t> env_scoped_impl_t::snapshot() const {
|
|
|
|
auto ret = std::make_shared<env_scoped_impl_t>(copy_node_chain(locals_), globals_);
|
|
|
|
ret->perproc_data_ = this->perproc_data_;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-11-08 06:24:13 +00:00
|
|
|
// A struct that wraps up the result of setting or removing a variable.
|
2021-09-28 19:50:27 +00:00
|
|
|
namespace {
|
2019-11-08 06:24:13 +00:00
|
|
|
struct mod_result_t {
|
|
|
|
// The publicly visible status of the set call.
|
|
|
|
int status{ENV_OK};
|
|
|
|
|
|
|
|
// Whether we modified the global scope.
|
|
|
|
bool global_modified{false};
|
|
|
|
|
|
|
|
// Whether we modified universal variables.
|
|
|
|
bool uvar_modified{false};
|
|
|
|
|
|
|
|
explicit mod_result_t(int status) : status(status) {}
|
|
|
|
};
|
2021-09-28 19:50:27 +00:00
|
|
|
} // namespace
|
2019-11-08 06:24:13 +00:00
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
/// A mutable subclass of env_scoped_impl_t.
|
|
|
|
class env_stack_impl_t final : public env_scoped_impl_t {
|
|
|
|
public:
|
|
|
|
using env_scoped_impl_t::env_scoped_impl_t;
|
|
|
|
|
|
|
|
/// Set a variable under the name \p key, using the given \p mode, setting its value to \p val.
|
2019-11-08 06:24:13 +00:00
|
|
|
mod_result_t set(const wcstring &key, env_mode_flags_t mode, wcstring_list_t val);
|
2019-05-10 16:10:43 +00:00
|
|
|
|
|
|
|
/// Remove a variable under the name \p key.
|
2019-11-08 06:24:13 +00:00
|
|
|
mod_result_t remove(const wcstring &key, int var_mode);
|
2019-05-10 16:10:43 +00:00
|
|
|
|
|
|
|
/// Push a new shadowing local scope.
|
|
|
|
void push_shadowing();
|
|
|
|
|
|
|
|
/// Push a new non-shadowing (inner) local scope.
|
|
|
|
void push_nonshadowing();
|
|
|
|
|
|
|
|
/// Pop the variable stack.
|
|
|
|
/// \return the popped node.
|
|
|
|
env_node_ref_t pop();
|
|
|
|
|
|
|
|
/// \return a new impl representing global variables, with a single local scope.
|
|
|
|
static std::unique_ptr<env_stack_impl_t> create() {
|
|
|
|
static const auto s_global_node = std::make_shared<env_node_t>(false, nullptr);
|
|
|
|
auto local = std::make_shared<env_node_t>(false, nullptr);
|
|
|
|
return make_unique<env_stack_impl_t>(std::move(local), s_global_node);
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2019-12-21 21:47:58 +00:00
|
|
|
~env_stack_impl_t() override = default;
|
2019-05-10 16:10:43 +00:00
|
|
|
|
|
|
|
private:
|
2019-10-03 10:26:51 +00:00
|
|
|
/// The scopes of caller functions, which are currently shadowed.
|
2019-05-10 16:10:43 +00:00
|
|
|
std::vector<env_node_ref_t> shadowed_locals_;
|
|
|
|
|
|
|
|
/// A restricted set of variable flags.
|
|
|
|
struct var_flags_t {
|
|
|
|
// if set, whether we should become a path variable; otherwise guess based on the name.
|
|
|
|
maybe_t<bool> pathvar{};
|
|
|
|
|
|
|
|
// if set, the new export value; otherwise inherit any existing export value.
|
|
|
|
maybe_t<bool> exports{};
|
|
|
|
|
|
|
|
// whether the variable is exported by some parent.
|
|
|
|
bool parent_exports{};
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Find the first node in the chain starting at \p node which contains the given key \p key.
|
|
|
|
static env_node_ref_t find_in_chain(const env_node_ref_t &node, const wcstring &key) {
|
|
|
|
for (auto cursor = node; cursor; cursor = cursor->next) {
|
|
|
|
if (cursor->env.count(key)) {
|
|
|
|
return cursor;
|
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2019-05-10 16:10:43 +00:00
|
|
|
return nullptr;
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 20:25:30 +00:00
|
|
|
/// Remove a variable from the chain \p node.
|
2019-05-10 16:10:43 +00:00
|
|
|
/// \return true if the variable was found and removed.
|
2019-12-27 05:54:21 +00:00
|
|
|
bool remove_from_chain(const env_node_ref_t &node, const wcstring &key) const {
|
2019-05-10 16:10:43 +00:00
|
|
|
for (auto cursor = node; cursor; cursor = cursor->next) {
|
|
|
|
auto iter = cursor->env.find(key);
|
|
|
|
if (iter != cursor->env.end()) {
|
2019-06-28 18:21:59 +00:00
|
|
|
if (iter->second.exports()) {
|
|
|
|
node->changed_exported();
|
|
|
|
}
|
2019-05-10 16:10:43 +00:00
|
|
|
cursor->env.erase(iter);
|
|
|
|
return true;
|
2018-10-22 07:49:09 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-10 16:10:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Try setting\p key as an electric or readonly variable.
|
|
|
|
/// \return an error code, or none() if not an electric or readonly variable.
|
|
|
|
/// \p val will not be modified upon a none() return.
|
|
|
|
maybe_t<int> try_set_electric(const wcstring &key, const query_t &query, wcstring_list_t &val);
|
|
|
|
|
|
|
|
/// Set a universal value.
|
|
|
|
void set_universal(const wcstring &key, wcstring_list_t val, const query_t &query);
|
|
|
|
|
|
|
|
/// Set a variable in a given node \p node.
|
2019-12-27 05:54:21 +00:00
|
|
|
void set_in_node(const env_node_ref_t &node, const wcstring &key, wcstring_list_t &&val,
|
2019-05-10 16:10:43 +00:00
|
|
|
const var_flags_t &flags);
|
|
|
|
|
|
|
|
// Implement the default behavior of 'set' by finding the node for an unspecified scope.
|
|
|
|
env_node_ref_t resolve_unspecified_scope() {
|
|
|
|
for (auto cursor = locals_; cursor; cursor = cursor->next) {
|
|
|
|
if (cursor->new_scope) return cursor;
|
2014-07-07 01:04:30 +00:00
|
|
|
}
|
2019-05-10 16:10:43 +00:00
|
|
|
return globals_;
|
|
|
|
}
|
2016-04-29 01:26:46 +00:00
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
/// Get a pointer to an existing variable, or nullptr.
|
|
|
|
/// This is used for inheriting pathvar and export status.
|
|
|
|
const env_var_t *find_variable(const wcstring &key) const {
|
|
|
|
env_node_ref_t node = find_in_chain(locals_, key);
|
|
|
|
if (!node) node = find_in_chain(globals_, key);
|
|
|
|
if (node) {
|
|
|
|
auto iter = node->env.find(key);
|
|
|
|
assert(iter != node->env.end() && "Node should contain key");
|
|
|
|
return &iter->second;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void env_stack_impl_t::push_nonshadowing() {
|
|
|
|
locals_ = std::make_shared<env_node_t>(false, locals_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void env_stack_impl_t::push_shadowing() {
|
|
|
|
// Propagate local exported variables.
|
|
|
|
auto node = std::make_shared<env_node_t>(true, nullptr);
|
2019-10-03 10:26:51 +00:00
|
|
|
for (auto cursor = locals_; cursor; cursor = cursor->next) {
|
|
|
|
for (const auto &var : cursor->env) {
|
|
|
|
if (var.second.exports()) {
|
|
|
|
node->env.insert(var);
|
|
|
|
node->changed_exported();
|
|
|
|
}
|
2019-05-10 16:10:43 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2019-05-10 16:10:43 +00:00
|
|
|
this->shadowed_locals_.push_back(std::move(locals_));
|
|
|
|
this->locals_ = std::move(node);
|
|
|
|
}
|
2006-01-08 23:00:49 +00:00
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
env_node_ref_t env_stack_impl_t::pop() {
|
|
|
|
auto popped = std::move(locals_);
|
|
|
|
if (popped->next) {
|
|
|
|
// Pop the inner scope.
|
|
|
|
locals_ = popped->next;
|
|
|
|
} else {
|
|
|
|
// Exhausted the inner scopes, put back a shadowing scope.
|
|
|
|
assert(!shadowed_locals_.empty() && "Attempt to pop last local scope");
|
|
|
|
locals_ = std::move(shadowed_locals_.back());
|
|
|
|
shadowed_locals_.pop_back();
|
|
|
|
}
|
|
|
|
assert(locals_ && "Attempt to pop first local scope");
|
|
|
|
return popped;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
/// Apply the pathvar behavior, splitting about colons.
|
|
|
|
static wcstring_list_t colon_split(const wcstring_list_t &val) {
|
|
|
|
wcstring_list_t split_val;
|
|
|
|
split_val.reserve(val.size());
|
|
|
|
for (const wcstring &str : val) {
|
|
|
|
vec_append(split_val, split_string(str, PATH_ARRAY_SEP));
|
|
|
|
}
|
|
|
|
return split_val;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 05:54:21 +00:00
|
|
|
void env_stack_impl_t::set_in_node(const env_node_ref_t &node, const wcstring &key,
|
|
|
|
wcstring_list_t &&val, const var_flags_t &flags) {
|
2019-05-10 16:10:43 +00:00
|
|
|
env_var_t &var = node->env[key];
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
// Use an explicit exports, or inherit from the existing variable.
|
|
|
|
bool res_exports = flags.exports.has_value() ? *flags.exports : var.exports();
|
2019-04-08 19:32:14 +00:00
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
// Pathvar is inferred from the name. If set, split our entry about colons.
|
|
|
|
bool res_pathvar =
|
|
|
|
flags.pathvar.has_value() ? *flags.pathvar : variable_should_auto_pathvar(key);
|
|
|
|
if (res_pathvar) {
|
|
|
|
val = colon_split(val);
|
2019-04-08 19:32:14 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 19:36:01 +00:00
|
|
|
var =
|
|
|
|
var.setting_vals(std::move(val)).setting_exports(res_exports).setting_pathvar(res_pathvar);
|
2019-05-10 16:10:43 +00:00
|
|
|
|
|
|
|
// Perhaps mark that this node contains an exported variable, or shadows an exported variable.
|
|
|
|
// If so regenerate the export list.
|
|
|
|
if (res_exports || flags.parent_exports) {
|
2019-06-09 20:25:30 +00:00
|
|
|
node->changed_exported();
|
2019-04-08 19:32:14 +00:00
|
|
|
}
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
maybe_t<int> env_stack_impl_t::try_set_electric(const wcstring &key, const query_t &query,
|
|
|
|
wcstring_list_t &val) {
|
2019-05-12 02:09:22 +00:00
|
|
|
const electric_var_t *ev = electric_var_t::for_name(key);
|
|
|
|
if (!ev) {
|
|
|
|
return none();
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-05-12 02:09:22 +00:00
|
|
|
// If a variable is electric, it may only be set in the global scope.
|
|
|
|
if (query.has_scope && !query.global) {
|
|
|
|
return ENV_SCOPE;
|
2019-05-10 16:10:43 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
// If the variable is read-only, the user may not set it.
|
2019-05-12 02:09:22 +00:00
|
|
|
if (query.user && ev->readonly()) {
|
2019-05-10 16:10:43 +00:00
|
|
|
return ENV_PERM;
|
|
|
|
}
|
|
|
|
|
2019-05-12 02:09:22 +00:00
|
|
|
// Be picky about exporting.
|
|
|
|
if (query.has_export_unexport) {
|
|
|
|
if (ev->exports() ? query.unexports : query.exports) {
|
2019-05-10 16:10:43 +00:00
|
|
|
return ENV_SCOPE;
|
2019-04-14 21:01:23 +00:00
|
|
|
}
|
2019-05-10 16:10:43 +00:00
|
|
|
}
|
2019-04-14 21:01:23 +00:00
|
|
|
|
2019-05-12 02:09:22 +00:00
|
|
|
// Handle computed mutable electric variables.
|
2019-05-10 16:10:43 +00:00
|
|
|
if (key == L"umask") {
|
|
|
|
return set_umask(val);
|
|
|
|
} else if (key == L"PWD") {
|
|
|
|
assert(val.size() == 1 && "Should have exactly one element in PWD");
|
|
|
|
wcstring &pwd = val.front();
|
|
|
|
if (pwd != perproc_data().pwd) {
|
|
|
|
perproc_data().pwd = std::move(pwd);
|
2019-06-09 20:25:30 +00:00
|
|
|
globals_->changed_exported();
|
2019-05-10 16:10:43 +00:00
|
|
|
}
|
|
|
|
return ENV_OK;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
// Decide on the mode and set it in the global scope.
|
|
|
|
var_flags_t flags{};
|
2019-05-12 02:09:22 +00:00
|
|
|
flags.exports = ev->exports();
|
|
|
|
flags.parent_exports = ev->exports();
|
2019-05-10 16:10:43 +00:00
|
|
|
flags.pathvar = false;
|
|
|
|
set_in_node(globals_, key, std::move(val), flags);
|
|
|
|
return ENV_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set a universal variable, inheriting as applicable from the given old variable.
|
|
|
|
void env_stack_impl_t::set_universal(const wcstring &key, wcstring_list_t val,
|
|
|
|
const query_t &query) {
|
|
|
|
ASSERT_IS_MAIN_THREAD();
|
|
|
|
auto oldvar = uvars()->get(key);
|
|
|
|
// Resolve whether or not to export.
|
|
|
|
bool exports = false;
|
|
|
|
if (query.has_export_unexport) {
|
|
|
|
exports = query.exports;
|
|
|
|
} else if (oldvar) {
|
|
|
|
exports = oldvar->exports();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve whether to be a path variable.
|
|
|
|
// Here we fall back to the auto-pathvar behavior.
|
|
|
|
bool pathvar = false;
|
|
|
|
if (query.has_pathvar_unpathvar) {
|
|
|
|
pathvar = query.pathvar;
|
|
|
|
} else if (oldvar) {
|
|
|
|
pathvar = oldvar->is_pathvar();
|
|
|
|
} else {
|
|
|
|
pathvar = variable_should_auto_pathvar(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split about ':' if it's a path variable.
|
|
|
|
if (pathvar) {
|
|
|
|
wcstring_list_t split_val;
|
|
|
|
for (const wcstring &str : val) {
|
|
|
|
vec_append(split_val, split_string(str, PATH_ARRAY_SEP));
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2019-05-10 16:10:43 +00:00
|
|
|
val = std::move(split_val);
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
// Construct and set the new variable.
|
|
|
|
env_var_t::env_var_flags_t varflags = 0;
|
|
|
|
if (exports) varflags |= env_var_t::flag_export;
|
|
|
|
if (pathvar) varflags |= env_var_t::flag_pathvar;
|
|
|
|
env_var_t new_var{val, varflags};
|
|
|
|
|
|
|
|
uvars()->set(key, new_var);
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-11-08 06:24:13 +00:00
|
|
|
mod_result_t env_stack_impl_t::set(const wcstring &key, env_mode_flags_t mode,
|
|
|
|
wcstring_list_t val) {
|
2019-05-10 16:10:43 +00:00
|
|
|
const query_t query(mode);
|
|
|
|
// Handle electric and read-only variables.
|
|
|
|
if (auto ret = try_set_electric(key, query, val)) {
|
2019-11-08 06:24:13 +00:00
|
|
|
return mod_result_t{*ret};
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
// Resolve as much of our flags as we can. Note these contain maybes, and we may defer the final
|
|
|
|
// decision until the set_in_node call. Also note that we only inherit pathvar, not export. For
|
|
|
|
// example, if you have a global exported variable, a local variable with the same name will not
|
|
|
|
// automatically be exported. But if you have a global pathvar, a local variable with the same
|
|
|
|
// name will be a pathvar. This is historical.
|
|
|
|
var_flags_t flags{};
|
|
|
|
if (const env_var_t *existing = find_variable(key)) {
|
|
|
|
flags.pathvar = existing->is_pathvar();
|
|
|
|
flags.parent_exports = existing->exports();
|
|
|
|
}
|
|
|
|
if (query.has_export_unexport) {
|
|
|
|
flags.exports = query.exports;
|
|
|
|
}
|
|
|
|
if (query.has_pathvar_unpathvar) {
|
|
|
|
flags.pathvar = query.pathvar;
|
|
|
|
}
|
|
|
|
|
2019-11-08 06:24:13 +00:00
|
|
|
mod_result_t result{ENV_OK};
|
2019-05-10 16:10:43 +00:00
|
|
|
if (query.has_scope) {
|
|
|
|
// The user requested a particular scope.
|
2021-04-11 09:07:44 +00:00
|
|
|
//
|
|
|
|
// If we don't have uvars, fall back to using globals
|
2021-05-10 02:48:43 +00:00
|
|
|
if (query.universal && !s_uvar_scope_is_global) {
|
2019-05-10 16:10:43 +00:00
|
|
|
set_universal(key, std::move(val), query);
|
2019-11-08 06:24:13 +00:00
|
|
|
result.uvar_modified = true;
|
2021-05-10 02:48:43 +00:00
|
|
|
} else if (query.global || (query.universal && s_uvar_scope_is_global)) {
|
2019-05-10 16:10:43 +00:00
|
|
|
set_in_node(globals_, key, std::move(val), flags);
|
2019-11-08 06:24:13 +00:00
|
|
|
result.global_modified = true;
|
2019-05-10 16:10:43 +00:00
|
|
|
} else if (query.local) {
|
2019-11-08 06:24:13 +00:00
|
|
|
assert(locals_ != globals_ && "Locals should not be globals");
|
2019-05-10 16:10:43 +00:00
|
|
|
set_in_node(locals_, key, std::move(val), flags);
|
Add `set --function` (#8145)
* Add `set --function`
This makes the function's scope available, even inside of blocks. Outside of blocks it's the toplevel local scope.
This removes the need to declare variables locally before use, and will probably end up being the main way variables get set.
E.g.:
```fish
set -l thing
if condition
set thing one
else
set thing two
end
```
could be written as
```fish
if condition
set -f thing one
else
set -f thing two
end
```
Note: Many scripts shipped with fish use workarounds like `and`/`or`
instead of `if`, so it isn't easy to find good examples.
Also, if there isn't an else-branch in that above, just with
```fish
if condition
set -f thing one
end
```
that means something different from setting it before! Now, if
`condition` isn't true, it would use a global (or universal) variable of
te same name!
Some more interesting parts:
Because it *is* a local scope, setting a variable `-f` and
`-l` in the toplevel of a function ends up the same:
```fish
function foo2
set -l foo bar
set -f foo baz # modifies the *same* variable!
end
```
but setting it locally inside a block creates a new local variable
that shadows the function-scoped variable:
```fish
function foo3
set -f foo bar
begin
set -l foo banana
# $foo is banana
end
# $foo is bar again
end
```
This is how local variables already work. "Local" is actually "block-scoped".
Also `set --show` will only show the closest local scope, so it won't
show a shadowed function-level variable. Again, this is how local
variables already work, and could be done as a separate change.
As a fun tidbit, functions with --no-scope-shadowing can now use this to set variables in the calling function. That's probably okay given that it's already an escape hatch (but to be clear: if it turns out to problematic I reserve the right to remove it).
Fixes #565
2021-08-01 18:08:12 +00:00
|
|
|
} else if (query.function) {
|
|
|
|
// "Function" scope is:
|
|
|
|
// Either the topmost local scope of the nearest function,
|
|
|
|
// or the top-level local scope if no function exists.
|
|
|
|
//
|
|
|
|
// This is distinct from the unspecified scope,
|
|
|
|
// which is the global scope if no function exists.
|
|
|
|
auto node = locals_;
|
|
|
|
while (node->next) {
|
|
|
|
node = node->next;
|
|
|
|
// The first node that introduces a new scope is ours.
|
|
|
|
// If this doesn't happen, we go on until we've reached the
|
|
|
|
// topmost local scope.
|
|
|
|
if (node->new_scope) break;
|
|
|
|
}
|
|
|
|
set_in_node(node, key, std::move(val), flags);
|
2019-05-10 16:10:43 +00:00
|
|
|
} else {
|
|
|
|
DIE("Unknown scope");
|
|
|
|
}
|
2019-05-26 00:15:50 +00:00
|
|
|
} else if (env_node_ref_t node = find_in_chain(locals_, key)) {
|
|
|
|
// Existing local variable.
|
|
|
|
set_in_node(node, key, std::move(val), flags);
|
|
|
|
} else if (env_node_ref_t node = find_in_chain(globals_, key)) {
|
|
|
|
// Existing global variable.
|
|
|
|
set_in_node(node, key, std::move(val), flags);
|
2019-11-08 06:24:13 +00:00
|
|
|
result.global_modified = true;
|
2021-05-10 21:06:17 +00:00
|
|
|
} else if (uvars()->get(key)) {
|
2019-05-26 00:15:50 +00:00
|
|
|
// Existing universal variable.
|
2019-05-10 16:10:43 +00:00
|
|
|
set_universal(key, std::move(val), query);
|
2019-11-08 06:24:13 +00:00
|
|
|
result.uvar_modified = true;
|
2019-05-10 16:10:43 +00:00
|
|
|
} else {
|
2019-05-26 00:15:50 +00:00
|
|
|
// Unspecified scope with no existing variables.
|
2021-10-31 10:50:22 +00:00
|
|
|
node = resolve_unspecified_scope();
|
2019-05-10 16:10:43 +00:00
|
|
|
assert(node && "Should always resolve some scope");
|
|
|
|
set_in_node(node, key, std::move(val), flags);
|
2019-11-08 06:24:13 +00:00
|
|
|
result.global_modified = (node == globals_);
|
2019-05-10 16:10:43 +00:00
|
|
|
}
|
2019-11-08 06:24:13 +00:00
|
|
|
return result;
|
2019-05-10 16:10:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 06:24:13 +00:00
|
|
|
mod_result_t env_stack_impl_t::remove(const wcstring &key, int mode) {
|
2019-05-10 16:10:43 +00:00
|
|
|
const query_t query(mode);
|
|
|
|
|
|
|
|
// Users can't remove read-only keys.
|
|
|
|
if (query.user && is_read_only(key)) {
|
2019-11-08 06:24:13 +00:00
|
|
|
return mod_result_t{ENV_SCOPE};
|
2019-05-10 16:10:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 06:24:13 +00:00
|
|
|
mod_result_t result{ENV_OK};
|
2019-05-10 16:10:43 +00:00
|
|
|
if (query.has_scope) {
|
|
|
|
// The user requested erasing from a particular scope.
|
|
|
|
if (query.universal) {
|
2021-05-10 21:06:17 +00:00
|
|
|
result.status = uvars()->remove(key) ? ENV_OK : ENV_NOT_FOUND;
|
2019-11-08 06:24:13 +00:00
|
|
|
result.uvar_modified = true;
|
2019-05-10 16:10:43 +00:00
|
|
|
} else if (query.global) {
|
2019-11-08 06:24:13 +00:00
|
|
|
result.status = remove_from_chain(globals_, key) ? ENV_OK : ENV_NOT_FOUND;
|
|
|
|
result.global_modified = true;
|
2019-05-10 16:10:43 +00:00
|
|
|
} else if (query.local) {
|
2019-11-08 06:24:13 +00:00
|
|
|
result.status = remove_from_chain(locals_, key) ? ENV_OK : ENV_NOT_FOUND;
|
2021-08-04 15:54:58 +00:00
|
|
|
} else if (query.function) {
|
|
|
|
auto node = locals_;
|
|
|
|
while (node->next) {
|
|
|
|
node = node->next;
|
|
|
|
if (node->new_scope) break;
|
|
|
|
}
|
|
|
|
result.status = remove_from_chain(node, key) ? ENV_OK : ENV_NOT_FOUND;
|
2019-05-10 16:10:43 +00:00
|
|
|
} else {
|
|
|
|
DIE("Unknown scope");
|
|
|
|
}
|
|
|
|
} else if (remove_from_chain(locals_, key)) {
|
2019-11-08 06:24:13 +00:00
|
|
|
// pass
|
2019-05-10 16:10:43 +00:00
|
|
|
} else if (remove_from_chain(globals_, key)) {
|
2019-11-08 06:24:13 +00:00
|
|
|
result.global_modified = true;
|
2021-05-10 21:06:17 +00:00
|
|
|
} else if (uvars()->remove(key)) {
|
2019-11-08 06:24:13 +00:00
|
|
|
result.uvar_modified = true;
|
2019-05-10 16:10:43 +00:00
|
|
|
} else {
|
2019-11-08 06:24:13 +00:00
|
|
|
result.status = ENV_NOT_FOUND;
|
2019-05-10 16:10:43 +00:00
|
|
|
}
|
2019-11-08 06:24:13 +00:00
|
|
|
return result;
|
2012-01-10 20:51:09 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
bool env_stack_t::universal_barrier() {
|
2020-07-16 23:16:03 +00:00
|
|
|
// Only perform universal barriers for the principal env stack.
|
|
|
|
// This means that changes from other fish processes will only be visible when the "main thread
|
|
|
|
// runs."
|
|
|
|
if (!is_principal()) return false;
|
|
|
|
|
2012-02-28 23:11:46 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2021-05-10 02:48:43 +00:00
|
|
|
if (s_uvar_scope_is_global) return false;
|
2019-05-10 16:10:43 +00:00
|
|
|
|
|
|
|
callback_data_list_t callbacks;
|
|
|
|
bool changed = uvars()->sync(callbacks);
|
|
|
|
if (changed) {
|
|
|
|
universal_notifier_t::default_notifier().post_notification();
|
|
|
|
}
|
|
|
|
|
|
|
|
env_universal_callbacks(this, callbacks);
|
|
|
|
return changed || !callbacks.empty();
|
|
|
|
}
|
|
|
|
|
2019-05-12 21:00:44 +00:00
|
|
|
statuses_t env_stack_t::get_last_statuses() const {
|
|
|
|
return acquire_impl()->perproc_data().statuses;
|
|
|
|
}
|
|
|
|
|
|
|
|
int env_stack_t::get_last_status() const { return acquire_impl()->perproc_data().statuses.status; }
|
|
|
|
|
|
|
|
void env_stack_t::set_last_statuses(statuses_t s) {
|
|
|
|
acquire_impl()->perproc_data().statuses = std::move(s);
|
|
|
|
}
|
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
/// Update the PWD variable directory from the result of getcwd().
|
|
|
|
void env_stack_t::set_pwd_from_getcwd() {
|
|
|
|
wcstring cwd = wgetcwd();
|
|
|
|
if (cwd.empty()) {
|
2019-05-28 13:06:42 +00:00
|
|
|
FLOG(error,
|
2019-06-04 03:30:48 +00:00
|
|
|
_(L"Could not determine current working directory. Is your locale set correctly?"));
|
2019-05-10 16:10:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
set_one(L"PWD", ENV_EXPORT | ENV_GLOBAL, cwd);
|
|
|
|
}
|
|
|
|
|
|
|
|
env_stack_t::env_stack_t(std::unique_ptr<env_stack_impl_t> impl) : impl_(std::move(impl)) {}
|
|
|
|
|
|
|
|
acquired_lock<env_stack_impl_t> env_stack_t::acquire_impl() {
|
|
|
|
return acquired_lock<env_stack_impl_t>::from_global(env_lock, impl_.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
acquired_lock<const env_stack_impl_t> env_stack_t::acquire_impl() const {
|
|
|
|
return acquired_lock<const env_stack_impl_t>::from_global(env_lock, impl_.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
maybe_t<env_var_t> env_stack_t::get(const wcstring &key, env_mode_flags_t mode) const {
|
|
|
|
return acquire_impl()->get(key, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
wcstring_list_t env_stack_t::get_names(int flags) const { return acquire_impl()->get_names(flags); }
|
|
|
|
|
2021-10-26 15:32:13 +00:00
|
|
|
int env_stack_t::set(const wcstring &key, env_mode_flags_t mode, wcstring_list_t vals) {
|
2019-05-10 16:10:43 +00:00
|
|
|
// Historical behavior.
|
|
|
|
if (vals.size() == 1 && (key == L"PWD" || key == L"HOME")) {
|
|
|
|
path_make_canonical(vals.front());
|
|
|
|
}
|
|
|
|
|
2019-11-02 23:46:33 +00:00
|
|
|
// Hacky stuff around PATH and CDPATH: #3914.
|
|
|
|
// Not MANPATH; see #4158.
|
|
|
|
// Replace empties with dot. Note we ignore pathvar here.
|
|
|
|
if (key == L"PATH" || key == L"CDPATH") {
|
|
|
|
auto munged_vals = colon_split(vals);
|
|
|
|
std::replace(munged_vals.begin(), munged_vals.end(), wcstring(L""), wcstring(L"."));
|
|
|
|
vals = std::move(munged_vals);
|
|
|
|
}
|
|
|
|
|
2019-11-08 06:24:13 +00:00
|
|
|
mod_result_t ret = acquire_impl()->set(key, mode, std::move(vals));
|
|
|
|
if (ret.status == ENV_OK) {
|
2019-11-08 07:15:26 +00:00
|
|
|
// If we modified the global state, or we are principal, then dispatch changes.
|
2019-05-10 16:10:43 +00:00
|
|
|
// Important to not hold the lock here.
|
2019-11-08 07:15:26 +00:00
|
|
|
if (ret.global_modified || is_principal()) {
|
|
|
|
env_dispatch_var_change(key, *this);
|
|
|
|
}
|
2019-05-10 16:10:43 +00:00
|
|
|
}
|
2019-11-08 07:15:26 +00:00
|
|
|
// If the principal stack modified universal variables, then post a barrier.
|
|
|
|
if (ret.uvar_modified && is_principal()) {
|
2019-05-10 16:10:43 +00:00
|
|
|
universal_barrier();
|
|
|
|
}
|
2019-11-08 06:24:13 +00:00
|
|
|
return ret.status;
|
2019-05-10 16:10:43 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 15:32:13 +00:00
|
|
|
int env_stack_t::set_one(const wcstring &key, env_mode_flags_t mode, wcstring val) {
|
2019-05-10 16:10:43 +00:00
|
|
|
wcstring_list_t vals;
|
|
|
|
vals.push_back(std::move(val));
|
2021-10-26 15:32:13 +00:00
|
|
|
return set(key, mode, std::move(vals));
|
2019-05-10 16:10:43 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 15:32:13 +00:00
|
|
|
int env_stack_t::set_empty(const wcstring &key, env_mode_flags_t mode) {
|
|
|
|
return set(key, mode, {});
|
2012-02-28 23:11:46 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 15:32:13 +00:00
|
|
|
int env_stack_t::remove(const wcstring &key, int mode) {
|
2019-11-08 06:24:13 +00:00
|
|
|
mod_result_t ret = acquire_impl()->remove(key, mode);
|
|
|
|
if (ret.status == ENV_OK) {
|
2019-11-08 07:15:26 +00:00
|
|
|
if (ret.global_modified || is_principal()) {
|
|
|
|
// Important to not hold the lock here.
|
|
|
|
env_dispatch_var_change(key, *this);
|
|
|
|
}
|
2019-05-10 16:10:43 +00:00
|
|
|
}
|
2019-11-08 07:15:26 +00:00
|
|
|
if (ret.uvar_modified && is_principal()) {
|
2019-05-10 16:10:43 +00:00
|
|
|
universal_barrier();
|
|
|
|
}
|
2019-11-08 06:24:13 +00:00
|
|
|
return ret.status;
|
2019-05-10 16:10:43 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 21:15:29 +00:00
|
|
|
std::shared_ptr<owning_null_terminated_array_t> env_stack_t::export_arr() {
|
2019-05-10 16:10:43 +00:00
|
|
|
return acquire_impl()->export_array();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<environment_t> env_stack_t::snapshot() const { return acquire_impl()->snapshot(); }
|
|
|
|
|
2019-05-19 03:58:45 +00:00
|
|
|
void env_stack_t::set_argv(wcstring_list_t argv) { set(L"argv", ENV_LOCAL, std::move(argv)); }
|
2015-08-15 20:37:17 +00:00
|
|
|
|
2019-06-10 17:26:21 +00:00
|
|
|
wcstring env_stack_t::get_pwd_slash() const {
|
|
|
|
wcstring pwd = acquire_impl()->perproc_data().pwd;
|
|
|
|
if (!string_suffixes_string(L"/", pwd)) {
|
|
|
|
pwd.push_back(L'/');
|
|
|
|
}
|
|
|
|
return pwd;
|
|
|
|
}
|
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
void env_stack_t::push(bool new_scope) {
|
|
|
|
auto impl = acquire_impl();
|
|
|
|
if (new_scope) {
|
|
|
|
impl->push_shadowing();
|
|
|
|
} else {
|
|
|
|
impl->push_nonshadowing();
|
|
|
|
}
|
|
|
|
}
|
2018-09-09 19:17:31 +00:00
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
void env_stack_t::pop() {
|
|
|
|
auto popped = acquire_impl()->pop();
|
2019-11-08 07:15:26 +00:00
|
|
|
// Only dispatch variable changes if we are the principal environment.
|
|
|
|
if (this == principal_ref().get()) {
|
|
|
|
// TODO: we would like to coalesce locale / curses changes, so that we only re-initialize
|
|
|
|
// once.
|
|
|
|
for (const auto &kv : popped->env) {
|
|
|
|
env_dispatch_var_change(kv.first, *this);
|
|
|
|
}
|
2019-05-10 16:10:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
env_stack_t &env_stack_t::globals() {
|
|
|
|
static env_stack_t s_globals(env_stack_impl_t::create());
|
|
|
|
return s_globals;
|
2018-09-14 07:36:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-20 16:27:46 +00:00
|
|
|
const std::shared_ptr<env_stack_t> &env_stack_t::principal_ref() {
|
|
|
|
static const std::shared_ptr<env_stack_t> s_principal{
|
|
|
|
new env_stack_t(env_stack_impl_t::create())};
|
2018-09-09 19:17:31 +00:00
|
|
|
return s_principal;
|
2018-09-14 07:36:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
env_stack_t::~env_stack_t() = default;
|
2018-09-09 19:17:31 +00:00
|
|
|
|
2018-09-28 16:49:06 +00:00
|
|
|
#if defined(__APPLE__) || defined(__CYGWIN__)
|
|
|
|
static int check_runtime_path(const char *path) {
|
2019-01-13 23:03:19 +00:00
|
|
|
UNUSED(path);
|
2018-09-28 16:49:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
2018-09-28 15:14:27 +00:00
|
|
|
/// Check, and create if necessary, a secure runtime path. Derived from tmux.c in tmux
|
|
|
|
/// (http://tmux.sourceforge.net/).
|
|
|
|
static int check_runtime_path(const char *path) {
|
|
|
|
// Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
// IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
struct stat statpath;
|
|
|
|
uid_t uid = geteuid();
|
|
|
|
|
|
|
|
if (mkdir(path, S_IRWXU) != 0 && errno != EEXIST) return errno;
|
|
|
|
if (lstat(path, &statpath) != 0) return errno;
|
|
|
|
if (!S_ISDIR(statpath.st_mode) || statpath.st_uid != uid ||
|
|
|
|
(statpath.st_mode & (S_IRWXG | S_IRWXO)) != 0)
|
|
|
|
return EACCES;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/// Return the path of an appropriate runtime data directory.
|
|
|
|
wcstring env_get_runtime_path() {
|
|
|
|
wcstring result;
|
|
|
|
const char *dir = getenv("XDG_RUNTIME_DIR");
|
|
|
|
|
|
|
|
// Check that the path is actually usable. Technically this is guaranteed by the fdo spec but in
|
|
|
|
// practice it is not always the case: see #1828 and #2222.
|
2020-09-17 10:30:16 +00:00
|
|
|
if (dir != nullptr && check_runtime_path(dir) == 0) {
|
2018-09-28 15:14:27 +00:00
|
|
|
result = str2wcstring(dir);
|
|
|
|
} else {
|
|
|
|
// Don't rely on $USER being set, as setup_user() has not yet been called.
|
|
|
|
// See https://github.com/fish-shell/fish-shell/issues/5180
|
2019-01-22 16:14:33 +00:00
|
|
|
// getpeuid() can't fail, but getpwuid sure can.
|
|
|
|
auto pwuid = getpwuid(geteuid());
|
2019-11-19 02:34:50 +00:00
|
|
|
const char *uname = pwuid ? pwuid->pw_name : nullptr;
|
2018-09-28 15:14:27 +00:00
|
|
|
// /tmp/fish.user
|
2019-02-06 04:36:38 +00:00
|
|
|
std::string tmpdir = get_path_to_tmp_dir() + "/fish.";
|
2019-01-22 16:14:33 +00:00
|
|
|
if (uname) {
|
|
|
|
tmpdir.append(uname);
|
|
|
|
}
|
2018-09-28 15:14:27 +00:00
|
|
|
|
2019-01-22 16:14:33 +00:00
|
|
|
if (!uname || check_runtime_path(tmpdir.c_str()) != 0) {
|
2019-05-27 22:56:53 +00:00
|
|
|
FLOG(error, L"Runtime path not available.");
|
2019-05-30 09:54:09 +00:00
|
|
|
FLOGF(error, L"Try deleting the directory %s and restarting fish.", tmpdir.c_str());
|
2018-09-28 15:14:27 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = str2wcstring(tmpdir);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2019-05-22 23:09:59 +00:00
|
|
|
|
|
|
|
static std::mutex s_setenv_lock{};
|
|
|
|
|
|
|
|
void setenv_lock(const char *name, const char *value, int overwrite) {
|
|
|
|
scoped_lock locker(s_setenv_lock);
|
|
|
|
setenv(name, value, overwrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
void unsetenv_lock(const char *name) {
|
|
|
|
scoped_lock locker(s_setenv_lock);
|
|
|
|
unsetenv(name);
|
|
|
|
}
|