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
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
#include <errno.h>
|
2017-05-10 04:46:18 +00:00
|
|
|
#include <limits.h>
|
2016-04-29 01:26:46 +00:00
|
|
|
#include <locale.h>
|
2011-12-27 03:18:46 +00:00
|
|
|
#include <pthread.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>
|
2016-06-04 02:05:13 +00:00
|
|
|
#include <string.h>
|
2016-04-29 01:26:46 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2016-06-29 01:06:39 +00:00
|
|
|
#include <time.h>
|
2016-04-29 01:26:46 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <wchar.h>
|
2016-12-23 21:08:45 +00:00
|
|
|
|
2018-02-04 08:59:37 +00:00
|
|
|
#if HAVE_CURSES_H
|
|
|
|
#include <curses.h>
|
|
|
|
#elif HAVE_NCURSES_H
|
2017-02-16 04:09:26 +00:00
|
|
|
#include <ncurses.h>
|
|
|
|
#elif HAVE_NCURSES_CURSES_H
|
|
|
|
#include <ncurses/curses.h>
|
|
|
|
#endif
|
2016-12-23 21:08:45 +00:00
|
|
|
#if HAVE_TERM_H
|
|
|
|
#include <term.h>
|
|
|
|
#elif HAVE_NCURSES_TERM_H
|
|
|
|
#include <ncurses/term.h>
|
|
|
|
#endif
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
#include <algorithm>
|
2018-01-30 21:22:36 +00:00
|
|
|
#include <iterator>
|
2016-04-29 01:26:46 +00:00
|
|
|
#include <set>
|
2017-02-11 02:47:02 +00:00
|
|
|
#include <type_traits>
|
2017-08-19 16:55:06 +00:00
|
|
|
#include <unordered_map>
|
2017-08-19 20:29:52 +00:00
|
|
|
#include <unordered_set>
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2017-06-13 00:19:13 +00:00
|
|
|
#include "builtin_bind.h"
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "common.h"
|
2018-02-16 05:58:02 +00:00
|
|
|
#include "complete.h"
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "env.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"
|
2017-05-22 05:22:55 +00:00
|
|
|
#include "expand.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"
|
2018-02-16 05:58:02 +00:00
|
|
|
#include "function.h"
|
2016-04-29 01:26:46 +00:00
|
|
|
#include "history.h"
|
|
|
|
#include "input.h"
|
2016-04-21 06:00:54 +00:00
|
|
|
#include "input_common.h"
|
2017-02-16 04:09:26 +00:00
|
|
|
#include "output.h"
|
2016-04-29 01:26:46 +00:00
|
|
|
#include "path.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "reader.h"
|
|
|
|
#include "sanity.h"
|
2017-02-04 03:20:21 +00:00
|
|
|
#include "screen.h"
|
2016-04-29 01:26:46 +00:00
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2017-02-16 04:09:26 +00:00
|
|
|
#define DEFAULT_TERM1 "ansi"
|
|
|
|
#define DEFAULT_TERM2 "dumb"
|
2005-09-26 14:47:03 +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;
|
|
|
|
|
2017-02-08 01:21:35 +00:00
|
|
|
// Limit `read` to 10 MiB (bytes not wide chars) by default. This can be overridden by the
|
2017-10-14 15:22:57 +00:00
|
|
|
// fish_read_limit variable.
|
2017-02-08 01:21:35 +00:00
|
|
|
#define READ_BYTE_LIMIT 10 * 1024 * 1024
|
|
|
|
size_t read_byte_limit = READ_BYTE_LIMIT;
|
|
|
|
|
2018-09-10 01:32:15 +00:00
|
|
|
/// The character used to delimit path and non-path variables in exporting and in string expansion.
|
|
|
|
static const wchar_t PATH_ARRAY_SEP = L':';
|
|
|
|
static const wchar_t NONPATH_ARRAY_SEP = L' ';
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
bool g_use_posix_spawn = false; // will usually be set to true
|
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;
|
|
|
|
|
2017-02-16 04:09:26 +00:00
|
|
|
/// This is used to ensure that we don't perform any callbacks from `react_to_variable_change()`
|
|
|
|
/// when we're importing environment variables in `env_init()`. That's because we don't have any
|
|
|
|
/// control over the order in which the vars are imported and some of them work in combination.
|
|
|
|
/// For example, `TERMINFO_DIRS` and `TERM`. If the user has set `TERM` to a custom value that is
|
|
|
|
/// found in `TERMINFO_DIRS` we don't to call `handle_curses()` before we've imported the latter.
|
|
|
|
static bool env_initialized = false;
|
|
|
|
|
2018-09-14 07:36:26 +00:00
|
|
|
typedef std::unordered_map<wcstring, void (*)(const wcstring &, const wcstring &, env_stack_t &)>
|
2017-12-23 01:31:25 +00:00
|
|
|
var_dispatch_table_t;
|
|
|
|
static var_dispatch_table_t var_dispatch_table;
|
2017-08-19 00:26:45 +00:00
|
|
|
|
2017-02-16 04:09:26 +00:00
|
|
|
/// List of all locale environment variable names that might trigger (re)initializing the locale
|
|
|
|
/// subsystem.
|
2017-04-05 04:28:57 +00:00
|
|
|
static const wcstring_list_t locale_variables({L"LANG", L"LANGUAGE", L"LC_ALL", L"LC_ADDRESS",
|
|
|
|
L"LC_COLLATE", L"LC_CTYPE", L"LC_IDENTIFICATION",
|
|
|
|
L"LC_MEASUREMENT", L"LC_MESSAGES", L"LC_MONETARY",
|
|
|
|
L"LC_NAME", L"LC_NUMERIC", L"LC_PAPER",
|
|
|
|
L"LC_TELEPHONE", L"LC_TIME"});
|
2017-01-26 19:20:09 +00:00
|
|
|
|
2017-02-16 04:09:26 +00:00
|
|
|
/// List of all curses environment variable names that might trigger (re)initializing the curses
|
|
|
|
/// subsystem.
|
2017-04-05 04:28:57 +00:00
|
|
|
static const wcstring_list_t curses_variables({L"TERM", L"TERMINFO", L"TERMINFO_DIRS"});
|
2017-01-26 19:20:09 +00:00
|
|
|
|
2017-02-16 04:09:26 +00:00
|
|
|
// Some forward declarations to make it easy to logically group the code.
|
2018-09-18 04:26:21 +00:00
|
|
|
static void init_locale(const environment_t &vars);
|
|
|
|
static void init_curses(const environment_t &vars);
|
2017-02-16 04:09:26 +00:00
|
|
|
|
2017-01-26 19:06:03 +00:00
|
|
|
// Struct representing one level in the function variable stack.
|
2017-01-26 19:20:09 +00:00
|
|
|
// Only our variable stack should create and destroy these
|
2017-01-26 19:06:03 +00:00
|
|
|
class env_node_t {
|
2017-01-27 04:00:43 +00:00
|
|
|
public:
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Variable table.
|
2012-11-19 00:30:30 +00:00
|
|
|
var_table_t env;
|
2016-04-29 01:26:46 +00:00
|
|
|
/// 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.
|
2013-01-19 21:21:55 +00:00
|
|
|
bool new_scope;
|
2017-06-21 14:48:48 +00:00
|
|
|
/// Does this node contain any variables which are exported to subshells
|
|
|
|
/// or does it redefine any variables to not be exported?
|
2017-01-26 19:06:03 +00:00
|
|
|
bool exportv = false;
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Pointer to next level.
|
2018-09-10 08:17:37 +00:00
|
|
|
std::shared_ptr<env_node_t> next;
|
|
|
|
|
|
|
|
env_node_t(bool is_new_scope) : new_scope(is_new_scope) {}
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2017-08-28 07:25:41 +00:00
|
|
|
maybe_t<env_var_t> find_entry(const wcstring &key);
|
2017-12-23 00:27:47 +00:00
|
|
|
|
|
|
|
bool contains_any_of(const wcstring_list_t &vars) const;
|
2012-02-26 02:54:49 +00:00
|
|
|
};
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2018-09-10 08:17:37 +00:00
|
|
|
using env_node_ref_t = std::shared_ptr<env_node_t>;
|
|
|
|
|
2018-12-31 02:15:49 +00:00
|
|
|
static std::mutex env_lock;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2017-01-26 18:38:55 +00:00
|
|
|
// A class wrapping up a variable stack
|
|
|
|
// Currently there is only one variable stack in fish,
|
|
|
|
// but we can imagine having separate (linked) stacks
|
|
|
|
// if we introduce multiple threads of execution
|
|
|
|
struct var_stack_t {
|
2018-09-14 07:36:26 +00:00
|
|
|
var_stack_t(var_stack_t &&) = default;
|
|
|
|
|
2017-01-26 18:38:55 +00:00
|
|
|
// Top node on the function stack.
|
2018-09-10 08:17:37 +00:00
|
|
|
env_node_ref_t top;
|
2017-01-26 18:38:55 +00:00
|
|
|
|
2018-09-10 08:17:37 +00:00
|
|
|
// Bottom node on the function stack.
|
|
|
|
env_node_ref_t global_env;
|
2017-01-26 19:06:03 +00:00
|
|
|
|
2017-01-26 20:03:14 +00:00
|
|
|
// Exported variable array used by execv.
|
2018-09-14 07:36:26 +00:00
|
|
|
maybe_t<null_terminated_array_t<char>> export_array;
|
2017-01-26 20:03:14 +00:00
|
|
|
|
|
|
|
/// Flag for checking if we need to regenerate the exported variable array.
|
2018-09-14 07:36:26 +00:00
|
|
|
void mark_changed_exported() { export_array.reset(); }
|
|
|
|
|
|
|
|
bool has_changed_exported() const { return !export_array; }
|
|
|
|
|
2017-01-26 20:03:14 +00:00
|
|
|
void update_export_array_if_necessary();
|
|
|
|
|
2018-09-11 01:59:57 +00:00
|
|
|
var_stack_t() : top(globals()), global_env(globals()) {
|
|
|
|
// Add a toplevel local scope on top of the global scope. This local scope will persist
|
|
|
|
// throughout the lifetime of the fish process, and it will ensure that `set -l` commands
|
|
|
|
// run at the command-line don't affect the global scope.
|
|
|
|
push(false);
|
|
|
|
}
|
2017-01-26 19:06:03 +00:00
|
|
|
|
|
|
|
// Pushes a new node onto our stack
|
|
|
|
// Optionally creates a new scope for the node
|
2017-01-26 19:20:09 +00:00
|
|
|
void push(bool new_scope);
|
|
|
|
|
|
|
|
// Pops the top node if it's not global
|
|
|
|
void pop();
|
2017-01-26 19:32:45 +00:00
|
|
|
|
2018-09-10 08:17:37 +00:00
|
|
|
// Returns the next scope to search for a given node, respecting the new_scope flag.
|
|
|
|
// Returns an empty pointer if we're done.
|
|
|
|
env_node_ref_t next_scope_to_search(const env_node_ref_t &node) const;
|
2017-01-26 20:11:32 +00:00
|
|
|
|
2017-08-09 18:11:58 +00:00
|
|
|
// Returns the scope used for unspecified scopes. An unspecified scope is either the topmost
|
|
|
|
// shadowing scope, or the global scope if none. This implements the default behavior of `set`.
|
2018-09-10 08:17:37 +00:00
|
|
|
env_node_ref_t resolve_unspecified_scope();
|
2018-09-09 19:17:31 +00:00
|
|
|
|
2018-09-14 07:36:26 +00:00
|
|
|
/// Copy this vars_stack.
|
|
|
|
var_stack_t clone() const {
|
|
|
|
return var_stack_t(*this);
|
|
|
|
}
|
|
|
|
|
2018-09-09 19:17:31 +00:00
|
|
|
private:
|
2018-09-14 07:36:26 +00:00
|
|
|
/// Copy constructor. This does not copy the export array; it just allows it to be regenerated.
|
|
|
|
var_stack_t(const var_stack_t &rhs) : top(rhs.top), global_env(rhs.global_env) {}
|
|
|
|
|
2018-09-10 08:17:37 +00:00
|
|
|
bool local_scope_exports(const env_node_ref_t &n) const;
|
2018-09-09 19:17:31 +00:00
|
|
|
void get_exported(const env_node_t *n, var_table_t &h) const;
|
2018-09-10 08:17:37 +00:00
|
|
|
|
|
|
|
/// Returns the global variable set.
|
|
|
|
static env_node_ref_t globals();
|
2017-01-26 19:20:09 +00:00
|
|
|
};
|
|
|
|
|
2018-09-10 08:17:37 +00:00
|
|
|
env_node_ref_t var_stack_t::globals() {
|
|
|
|
static env_node_ref_t s_globals{std::make_shared<env_node_t>(false)};
|
|
|
|
return s_globals;
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:20:09 +00:00
|
|
|
void var_stack_t::push(bool new_scope) {
|
2018-09-10 08:17:37 +00:00
|
|
|
auto node = std::make_shared<env_node_t>(new_scope);
|
Copy local-exported variables
When executing a function, local-exported (`set -lx`) variables
previously were not accessible at all. This is weird e.g. in case of
aliases, since
```fish
set -lx PAGER cat
git something # which will call $PAGER
```
would not work if `git` were a function, even if that ends up calling
`command git`.
Now, we copy these variables, so functions get a local-exported copy.
```fish
function x
echo $var
set var wurst
echo $var
end
set -lx var banana
x # prints "banana" and "wurst"
echo $var # prints "banana"
```
One weirdness here is that, if a variable is both local and global,
the local-copy takes precedence:
```fish
set -gx var banana
set -lx var pineapple
echo $var # prints "pineapple"
x # from above, prints "pineapple" and "wurst"
echo $var # still prints "pineapple"
set -el var # deletes local version
echo $var # "banana" again
```
I don't think there is any more consistent way to handle this - the
local version is the one that is accessed first, so it should also be
written to first.
Global-exported variables are _not_ copied, instead they still offer
full read-write access.
2017-06-20 22:47:11 +00:00
|
|
|
|
2017-08-09 18:11:58 +00:00
|
|
|
// Copy local-exported variables.
|
2018-09-10 08:17:37 +00:00
|
|
|
auto top_node = top;
|
2017-08-09 18:11:58 +00:00
|
|
|
// Only if we introduce a new shadowing scope; i.e. not if it's just `begin; end` or
|
|
|
|
// "--no-scope-shadowing".
|
|
|
|
if (new_scope && top_node != this->global_env) {
|
2017-12-23 01:38:25 +00:00
|
|
|
for (const auto &var : top_node->env) {
|
2018-01-30 20:36:50 +00:00
|
|
|
if (var.second.exports()) node->env.insert(var);
|
Copy local-exported variables
When executing a function, local-exported (`set -lx`) variables
previously were not accessible at all. This is weird e.g. in case of
aliases, since
```fish
set -lx PAGER cat
git something # which will call $PAGER
```
would not work if `git` were a function, even if that ends up calling
`command git`.
Now, we copy these variables, so functions get a local-exported copy.
```fish
function x
echo $var
set var wurst
echo $var
end
set -lx var banana
x # prints "banana" and "wurst"
echo $var # prints "banana"
```
One weirdness here is that, if a variable is both local and global,
the local-copy takes precedence:
```fish
set -gx var banana
set -lx var pineapple
echo $var # prints "pineapple"
x # from above, prints "pineapple" and "wurst"
echo $var # still prints "pineapple"
set -el var # deletes local version
echo $var # "banana" again
```
I don't think there is any more consistent way to handle this - the
local version is the one that is accessed first, so it should also be
written to first.
Global-exported variables are _not_ copied, instead they still offer
full read-write access.
2017-06-20 22:47:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 08:17:37 +00:00
|
|
|
node->next = this->top;
|
|
|
|
this->top = node;
|
|
|
|
if (new_scope && local_scope_exports(this->top)) {
|
2017-01-26 20:03:14 +00:00
|
|
|
this->mark_changed_exported();
|
2017-01-26 19:20:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-23 00:27:47 +00:00
|
|
|
/// Return true if if the node contains any of the entries in the vars list.
|
|
|
|
bool env_node_t::contains_any_of(const wcstring_list_t &vars) const {
|
|
|
|
for (const auto &v : vars) {
|
|
|
|
if (env.count(v)) return true;
|
2017-02-16 04:09:26 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:20:09 +00:00
|
|
|
void var_stack_t::pop() {
|
2017-02-16 04:09:26 +00:00
|
|
|
// Don't pop the top-most, global, level.
|
2018-09-10 08:17:37 +00:00
|
|
|
if (top == this->global_env) {
|
2017-01-26 19:20:09 +00:00
|
|
|
debug(0, _(L"Tried to pop empty environment stack."));
|
|
|
|
sanity_lose();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-23 00:27:47 +00:00
|
|
|
bool locale_changed = top->contains_any_of(locale_variables);
|
|
|
|
bool curses_changed = top->contains_any_of(curses_variables);
|
2017-01-26 19:20:09 +00:00
|
|
|
|
2017-01-26 20:40:55 +00:00
|
|
|
if (top->new_scope) { //!OCLINT(collapsible if statements)
|
2018-09-10 08:17:37 +00:00
|
|
|
if (top->exportv || local_scope_exports(top->next)) {
|
2017-01-26 20:03:14 +00:00
|
|
|
this->mark_changed_exported();
|
2017-01-26 19:06:03 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-26 20:40:55 +00:00
|
|
|
|
2018-09-10 08:17:37 +00:00
|
|
|
// Actually do the pop!
|
|
|
|
env_node_ref_t old_top = this->top;
|
|
|
|
this->top = old_top->next;
|
2017-01-26 20:40:55 +00:00
|
|
|
for (const auto &entry_pair : old_top->env) {
|
2017-08-09 18:11:58 +00:00
|
|
|
const env_var_t &var = entry_pair.second;
|
2018-01-30 20:36:50 +00:00
|
|
|
if (var.exports()) {
|
2017-01-26 20:03:14 +00:00
|
|
|
this->mark_changed_exported();
|
2017-01-26 19:20:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-02-16 04:09:26 +00:00
|
|
|
|
2018-09-18 04:26:21 +00:00
|
|
|
// TODO: instantize this locale and curses
|
|
|
|
const auto &vars = env_stack_t::principal();
|
|
|
|
if (locale_changed) init_locale(vars);
|
|
|
|
if (curses_changed) init_curses(vars);
|
2017-01-26 19:20:09 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 08:17:37 +00:00
|
|
|
env_node_ref_t var_stack_t::next_scope_to_search(const env_node_ref_t &node) const {
|
2017-01-26 19:32:45 +00:00
|
|
|
assert(node != NULL);
|
|
|
|
if (node == this->global_env) {
|
2018-09-10 08:17:37 +00:00
|
|
|
return nullptr;
|
2017-01-26 19:32:45 +00:00
|
|
|
}
|
2018-09-10 08:17:37 +00:00
|
|
|
return node->new_scope ? this->global_env : node->next;
|
2017-01-26 19:32:45 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 08:17:37 +00:00
|
|
|
env_node_ref_t var_stack_t::resolve_unspecified_scope() {
|
|
|
|
env_node_ref_t node = this->top;
|
2017-01-26 20:11:32 +00:00
|
|
|
while (node && !node->new_scope) {
|
2018-09-10 08:17:37 +00:00
|
|
|
node = node->next;
|
2017-01-26 20:11:32 +00:00
|
|
|
}
|
|
|
|
return node ? node : this->global_env;
|
|
|
|
}
|
2017-01-26 19:06:03 +00:00
|
|
|
|
2018-09-09 19:17:31 +00:00
|
|
|
env_stack_t::env_stack_t() : vars_(make_unique<var_stack_t>()) {}
|
2018-09-14 07:36:26 +00:00
|
|
|
env_stack_t::env_stack_t(std::unique_ptr<var_stack_t> vars) : vars_(std::move(vars)) {}
|
2018-09-09 19:17:31 +00:00
|
|
|
|
|
|
|
// Get the variable stack
|
|
|
|
var_stack_t &env_stack_t::vars_stack() { return *vars_; }
|
|
|
|
|
|
|
|
const var_stack_t &env_stack_t::vars_stack() const { return *vars_; }
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Universal variables global instance. Initialized in env_init.
|
2014-06-16 00:30:50 +00:00
|
|
|
static env_universal_t *s_universal_variables = NULL;
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Getter for universal variables.
|
|
|
|
static env_universal_t *uvars() { return s_universal_variables; }
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2018-01-30 21:22:36 +00:00
|
|
|
// A typedef for a set of constant strings. Note our sets are typically on the order of 6 elements,
|
|
|
|
// so we don't bother to sort them.
|
|
|
|
using string_set_t = const wchar_t *const[];
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2018-11-18 21:22:15 +00:00
|
|
|
template <typename T>
|
|
|
|
bool string_set_contains(const T &set, const wchar_t *val) {
|
|
|
|
for (const wchar_t *entry : set) {
|
|
|
|
if (!wcscmp(val, entry)) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-30 21:22:36 +00:00
|
|
|
/// Check if a variable may not be set using the set command.
|
|
|
|
static bool is_read_only(const wchar_t *val) {
|
2018-11-22 00:42:34 +00:00
|
|
|
const string_set_t env_read_only = {
|
2019-02-10 21:46:58 +00:00
|
|
|
L"PWD", L"SHLVL", L"history", L"status", L"version",
|
|
|
|
L"FISH_VERSION", L"fish_pid", L"hostname", L"_", L"fish_private_mode"};
|
2018-11-18 21:22:15 +00:00
|
|
|
return string_set_contains(env_read_only, val) ||
|
2018-09-19 23:56:08 +00:00
|
|
|
(in_private_mode() && wcscmp(L"fish_history", val) == 0);
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2018-01-30 21:22:36 +00:00
|
|
|
static bool is_read_only(const wcstring &val) { return is_read_only(val.c_str()); }
|
|
|
|
|
2018-09-10 01:32:15 +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);
|
2018-01-30 21:31:36 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Table of variables whose value is dynamically calculated, such as umask, status, etc.
|
2019-02-10 21:46:58 +00:00
|
|
|
static const string_set_t env_electric = {L"history", L"status", L"umask"};
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2018-09-29 04:58:44 +00:00
|
|
|
static bool is_electric(const wcstring &key) { return contains(env_electric, key); }
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2017-08-28 07:25:41 +00:00
|
|
|
maybe_t<env_var_t> env_node_t::find_entry(const wcstring &key) {
|
2017-08-09 18:11:58 +00:00
|
|
|
var_table_t::const_iterator entry = env.find(key);
|
|
|
|
if (entry != env.end()) return entry->second;
|
2017-08-28 07:25:41 +00:00
|
|
|
return none();
|
2013-02-20 01:48:51 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Return the current umask value.
|
|
|
|
static mode_t get_umask() {
|
2012-11-19 00:30:30 +00:00
|
|
|
mode_t res;
|
|
|
|
res = umask(0);
|
|
|
|
umask(res);
|
|
|
|
return res;
|
2005-10-23 12:14:29 +00:00
|
|
|
}
|
|
|
|
|
2016-06-29 04:25:25 +00:00
|
|
|
/// Properly sets all timezone information.
|
2018-09-18 04:26:21 +00:00
|
|
|
static void handle_timezone(const wchar_t *env_var_name, const environment_t &vars) {
|
|
|
|
const auto var = vars.get(env_var_name, ENV_DEFAULT);
|
2017-08-05 22:08:39 +00:00
|
|
|
debug(2, L"handle_timezone() current timezone var: |%ls| => |%ls|", env_var_name,
|
2017-08-28 07:25:41 +00:00
|
|
|
!var ? L"MISSING" : var->as_string().c_str());
|
2016-06-29 01:06:39 +00:00
|
|
|
const std::string &name = wcs2string(env_var_name);
|
2017-08-06 01:22:49 +00:00
|
|
|
if (var.missing_or_empty()) {
|
2016-06-29 01:06:39 +00:00
|
|
|
unsetenv(name.c_str());
|
|
|
|
} else {
|
2017-08-28 07:25:41 +00:00
|
|
|
const std::string value = wcs2string(var->as_string());
|
2016-06-29 01:06:39 +00:00
|
|
|
setenv(name.c_str(), value.c_str(), 1);
|
|
|
|
}
|
|
|
|
tzset();
|
|
|
|
}
|
|
|
|
|
2017-04-04 06:16:11 +00:00
|
|
|
/// Some env vars contain a list of paths where an empty path element is equivalent to ".".
|
|
|
|
/// Unfortunately that convention causes problems for fish scripts. So this function replaces the
|
|
|
|
/// empty path element with an explicit ".". See issue #3914.
|
2018-09-14 07:36:26 +00:00
|
|
|
static void fix_colon_delimited_var(const wcstring &var_name, env_stack_t &vars) {
|
|
|
|
const auto paths = vars.get(var_name);
|
2017-04-04 06:16:11 +00:00
|
|
|
if (paths.missing_or_empty()) return;
|
|
|
|
|
2017-12-23 01:31:25 +00:00
|
|
|
// See if there's any empties.
|
|
|
|
const wcstring empty = wcstring();
|
|
|
|
const wcstring_list_t &strs = paths->as_list();
|
2018-08-12 02:55:06 +00:00
|
|
|
if (contains(strs, empty)) {
|
2017-12-23 01:31:25 +00:00
|
|
|
// Copy the list and replace empties with L"."
|
|
|
|
wcstring_list_t newstrs = strs;
|
|
|
|
std::replace(newstrs.begin(), newstrs.end(), empty, wcstring(L"."));
|
2018-09-14 07:36:26 +00:00
|
|
|
int retval = vars.set(var_name, ENV_DEFAULT | ENV_USER, std::move(newstrs));
|
2017-12-23 01:31:25 +00:00
|
|
|
if (retval != ENV_OK) {
|
|
|
|
debug(0, L"fix_colon_delimited_var failed unexpectedly with retval %d", retval);
|
2017-04-04 06:16:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 04:09:26 +00:00
|
|
|
/// Initialize the locale subsystem.
|
2018-09-18 04:26:21 +00:00
|
|
|
static void init_locale(const environment_t &vars) {
|
2016-10-16 19:43:48 +00:00
|
|
|
// We have to make a copy because the subsequent setlocale() call to change the locale will
|
|
|
|
// invalidate the pointer from the this setlocale() call.
|
|
|
|
char *old_msg_locale = strdup(setlocale(LC_MESSAGES, NULL));
|
2017-02-16 04:09:26 +00:00
|
|
|
|
2017-12-23 01:31:25 +00:00
|
|
|
for (const auto &var_name : locale_variables) {
|
2018-09-18 04:26:21 +00:00
|
|
|
const auto var = vars.get(var_name, ENV_EXPORT);
|
2017-04-05 04:28:57 +00:00
|
|
|
const std::string &name = wcs2string(var_name);
|
2017-08-06 01:22:49 +00:00
|
|
|
if (var.missing_or_empty()) {
|
2018-10-02 16:19:56 +00:00
|
|
|
debug(5, L"locale var %s missing or empty", name.c_str());
|
2017-02-16 04:09:26 +00:00
|
|
|
unsetenv(name.c_str());
|
|
|
|
} else {
|
2017-08-28 07:25:41 +00:00
|
|
|
const std::string value = wcs2string(var->as_string());
|
2018-10-02 16:19:56 +00:00
|
|
|
debug(5, L"locale var %s='%s'", name.c_str(), value.c_str());
|
2017-02-16 04:09:26 +00:00
|
|
|
setenv(name.c_str(), value.c_str(), 1);
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2016-06-04 02:05:13 +00:00
|
|
|
char *locale = setlocale(LC_ALL, "");
|
|
|
|
fish_setlocale();
|
2018-10-02 16:19:56 +00:00
|
|
|
debug(5, L"init_locale() setlocale(): '%s'", locale);
|
2016-06-04 02:05:13 +00:00
|
|
|
|
|
|
|
const char *new_msg_locale = setlocale(LC_MESSAGES, NULL);
|
2018-10-02 16:19:56 +00:00
|
|
|
debug(5, L"old LC_MESSAGES locale: '%s'", old_msg_locale);
|
|
|
|
debug(5, L"new LC_MESSAGES locale: '%s'", new_msg_locale);
|
2016-06-06 01:52:19 +00:00
|
|
|
#ifdef HAVE__NL_MSG_CAT_CNTR
|
2016-06-04 02:05:13 +00:00
|
|
|
if (strcmp(old_msg_locale, new_msg_locale)) {
|
2016-06-06 01:52:19 +00:00
|
|
|
// Make change known to GNU gettext.
|
2012-11-19 00:30:30 +00:00
|
|
|
extern int _nl_msg_cat_cntr;
|
|
|
|
_nl_msg_cat_cntr++;
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2016-06-06 01:52:19 +00:00
|
|
|
#endif
|
2016-10-16 19:43:48 +00:00
|
|
|
free(old_msg_locale);
|
2006-01-08 23:00:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-10 02:35:37 +00:00
|
|
|
/// True if we think we can set the terminal title else false.
|
|
|
|
static bool can_set_term_title = false;
|
|
|
|
|
|
|
|
/// Returns true if we think the terminal supports setting its title.
|
2017-01-27 04:00:43 +00:00
|
|
|
bool term_supports_setting_title() { return can_set_term_title; }
|
2017-01-10 02:35:37 +00:00
|
|
|
|
|
|
|
/// This is a pretty lame heuristic for detecting terminals that do not support setting the
|
|
|
|
/// title. If we recognise the terminal name as that of a virtual terminal, we assume it supports
|
|
|
|
/// setting the title. If we recognise it as that of a console, we assume it does not support
|
|
|
|
/// setting the title. Otherwise we check the ttyname and see if we believe it is a virtual
|
|
|
|
/// terminal.
|
|
|
|
///
|
|
|
|
/// One situation in which this breaks down is with screen, since screen supports setting the
|
|
|
|
/// terminal title if the underlying terminal does so, but will print garbage on terminals that
|
|
|
|
/// don't. Since we can't see the underlying terminal below screen there is no way to fix this.
|
2018-09-30 23:57:05 +00:00
|
|
|
static const wchar_t *const title_terms[] = {L"xterm", L"screen", L"tmux", L"nxterm", L"rxvt"};
|
2018-09-18 04:26:21 +00:00
|
|
|
static bool does_term_support_setting_title(const environment_t &vars) {
|
|
|
|
const auto term_var = vars.get(L"TERM");
|
2017-08-06 01:22:49 +00:00
|
|
|
if (term_var.missing_or_empty()) return false;
|
2017-01-10 02:35:37 +00:00
|
|
|
|
2017-08-28 07:25:41 +00:00
|
|
|
const wcstring term_str = term_var->as_string();
|
2017-08-27 00:24:05 +00:00
|
|
|
const wchar_t *term = term_str.c_str();
|
2017-08-28 07:25:41 +00:00
|
|
|
bool recognized = contains(title_terms, term_var->as_string());
|
2017-01-10 02:35:37 +00:00
|
|
|
if (!recognized) recognized = !wcsncmp(term, L"xterm-", wcslen(L"xterm-"));
|
|
|
|
if (!recognized) recognized = !wcsncmp(term, L"screen-", wcslen(L"screen-"));
|
|
|
|
if (!recognized) recognized = !wcsncmp(term, L"tmux-", wcslen(L"tmux-"));
|
|
|
|
if (!recognized) {
|
2017-08-06 01:22:49 +00:00
|
|
|
if (wcscmp(term, L"linux") == 0) return false;
|
|
|
|
if (wcscmp(term, L"dumb") == 0) return false;
|
2018-12-12 12:10:34 +00:00
|
|
|
// NetBSD
|
|
|
|
if (wcscmp(term, L"vt100") == 0) return false;
|
|
|
|
if (wcscmp(term, L"wsvt25") == 0) return false;
|
2017-01-10 02:35:37 +00:00
|
|
|
|
2017-05-10 04:46:18 +00:00
|
|
|
char buf[PATH_MAX];
|
|
|
|
int retval = ttyname_r(STDIN_FILENO, buf, PATH_MAX);
|
|
|
|
if (retval != 0 || strstr(buf, "tty") || strstr(buf, "/vc/")) return false;
|
2017-01-10 02:35:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-16 04:09:26 +00:00
|
|
|
/// Updates our idea of whether we support term256 and term24bit (see issue #10222).
|
2018-09-18 04:26:21 +00:00
|
|
|
static void update_fish_color_support(const environment_t &vars) {
|
2017-02-16 04:09:26 +00:00
|
|
|
// Detect or infer term256 support. If fish_term256 is set, we respect it;
|
|
|
|
// otherwise infer it from the TERM variable or use terminfo.
|
2019-02-12 09:32:06 +00:00
|
|
|
wcstring term;
|
|
|
|
bool support_term256 = false;
|
|
|
|
bool support_term24bit = false;
|
|
|
|
|
|
|
|
if (auto term_var = vars.get(L"TERM")) term = term_var->as_string();
|
|
|
|
|
|
|
|
if (auto fish_term256 = vars.get(L"fish_term256")) {
|
|
|
|
// $fish_term256
|
2019-02-04 00:06:10 +00:00
|
|
|
support_term256 = bool_from_string(fish_term256->as_string());
|
2019-02-12 09:32:06 +00:00
|
|
|
debug(2, L"256 color support determined by '$fish_term256'");
|
2017-02-16 04:09:26 +00:00
|
|
|
} else if (term.find(L"256color") != wcstring::npos) {
|
2019-02-12 09:32:06 +00:00
|
|
|
// TERM is *256color*: 256 colors explicitly supported
|
2017-02-16 04:09:26 +00:00
|
|
|
support_term256 = true;
|
2019-02-12 09:32:06 +00:00
|
|
|
debug(2, L"256 color support enabled for TERM=%ls", term.c_str());
|
2017-02-16 04:09:26 +00:00
|
|
|
} else if (term.find(L"xterm") != wcstring::npos) {
|
2019-02-12 09:32:06 +00:00
|
|
|
// Assume that all 'xterm's can handle 256, except for Terminal.app from Snow Leopard
|
|
|
|
wcstring term_program, term_version;
|
|
|
|
if (auto tp = vars.get(L"TERM_PROGRAM")) term_program = tp->as_string();
|
|
|
|
if (auto tpv = vars.get(L"TERM_PROGRAM_VERSION")) {
|
|
|
|
if (term_program == L"Apple_Terminal" &&
|
|
|
|
fish_wcstod(tpv->as_string().c_str(), NULL) > 299) {
|
|
|
|
// OS X Lion is version 299+, it has 256 color support (see github Wiki)
|
|
|
|
support_term256 = true;
|
|
|
|
debug(2, L"256 color support enabled for TERM=%ls on Terminal.app", term.c_str());
|
|
|
|
} else {
|
2017-02-16 04:09:26 +00:00
|
|
|
support_term256 = true;
|
2019-02-12 09:32:06 +00:00
|
|
|
debug(2, L"256 color support enabled for TERM=%ls", term.c_str());
|
2017-02-16 04:09:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (cur_term != NULL) {
|
|
|
|
// See if terminfo happens to identify 256 colors
|
|
|
|
support_term256 = (max_colors >= 256);
|
2019-02-12 09:32:06 +00:00
|
|
|
debug(2, L"256 color support: %d colors per terminfo entry for %ls", max_colors, term.c_str());
|
2017-02-16 04:09:26 +00:00
|
|
|
}
|
2017-01-10 02:35:37 +00:00
|
|
|
|
2019-02-12 09:32:06 +00:00
|
|
|
// Handle $fish_term24bit
|
|
|
|
if (auto fish_term24bit = vars.get(L"fish_term24bit")) {
|
2019-02-04 00:06:10 +00:00
|
|
|
support_term24bit = bool_from_string(fish_term24bit->as_string());
|
2017-02-16 04:09:26 +00:00
|
|
|
debug(2, L"'fish_term24bit' preference: 24-bit color %s",
|
|
|
|
support_term24bit ? L"enabled" : L"disabled");
|
2016-06-14 02:00:30 +00:00
|
|
|
} else {
|
2017-02-16 04:09:26 +00:00
|
|
|
// We don't attempt to infer term24 bit support yet.
|
2019-02-12 09:32:06 +00:00
|
|
|
// XXX: actually, we do, in config.fish.
|
|
|
|
// So we actually change the color mode shortly after startup
|
2017-02-16 04:09:26 +00:00
|
|
|
}
|
|
|
|
color_support_t support = (support_term256 ? color_support_term256 : 0) |
|
|
|
|
(support_term24bit ? color_support_term24bit : 0);
|
|
|
|
output_set_color_support(support);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to initialize the terminfo/curses subsystem using our fallback terminal name. Do not set
|
|
|
|
// `TERM` to our fallback. We're only doing this in the hope of getting a minimally functional
|
|
|
|
// shell. If we launch an external command that uses TERM it should get the same value we were
|
|
|
|
// given, if any.
|
|
|
|
static bool initialize_curses_using_fallback(const char *term) {
|
|
|
|
// If $TERM is already set to the fallback name we're about to use there isn't any point in
|
|
|
|
// seeing if the fallback name can be used.
|
2018-09-18 04:26:21 +00:00
|
|
|
auto &vars = env_stack_t::globals();
|
|
|
|
auto term_var = vars.get(L"TERM");
|
2017-08-06 01:22:49 +00:00
|
|
|
if (term_var.missing_or_empty()) return false;
|
|
|
|
|
2018-02-08 23:16:56 +00:00
|
|
|
auto term_env = wcs2string(term_var->as_string());
|
2018-02-08 23:36:29 +00:00
|
|
|
if (term_env == DEFAULT_TERM1 || term_env == DEFAULT_TERM2) return false;
|
2017-02-16 04:09:26 +00:00
|
|
|
|
2017-08-06 01:22:49 +00:00
|
|
|
if (is_interactive_session) debug(1, _(L"Using fallback terminal type '%s'."), term);
|
|
|
|
|
2017-02-16 04:09:26 +00:00
|
|
|
int err_ret;
|
|
|
|
if (setupterm((char *)term, STDOUT_FILENO, &err_ret) == OK) return true;
|
|
|
|
if (is_interactive_session) {
|
|
|
|
debug(1, _(L"Could not set up terminal using the fallback terminal type '%s'."), term);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-04 06:16:11 +00:00
|
|
|
/// Ensure the content of the magic path env vars is reasonable. Specifically, that empty path
|
|
|
|
/// elements are converted to explicit "." to make the vars easier to use in fish scripts.
|
|
|
|
static void init_path_vars() {
|
2018-09-10 01:32:15 +00:00
|
|
|
// Do not replace empties in MATHPATH - see #4158.
|
2018-09-14 07:36:26 +00:00
|
|
|
fix_colon_delimited_var(L"PATH", env_stack_t::globals());
|
|
|
|
fix_colon_delimited_var(L"CDPATH", env_stack_t::globals());
|
2017-04-04 06:16:11 +00:00
|
|
|
}
|
|
|
|
|
2018-02-26 01:43:29 +00:00
|
|
|
/// Update the value of g_guessed_fish_emoji_width
|
|
|
|
static void guess_emoji_width() {
|
|
|
|
wcstring term;
|
2018-09-18 04:26:21 +00:00
|
|
|
auto &vars = env_stack_t::globals();
|
|
|
|
if (auto term_var = vars.get(L"TERM_PROGRAM")) {
|
2018-02-26 01:43:29 +00:00
|
|
|
term = term_var->as_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
double version = 0;
|
2018-09-18 04:26:21 +00:00
|
|
|
if (auto version_var = vars.get(L"TERM_PROGRAM_VERSION")) {
|
2018-02-26 01:43:29 +00:00
|
|
|
std::string narrow_version = wcs2string(version_var->as_string());
|
|
|
|
version = strtod(narrow_version.c_str(), NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// iTerm2 defaults to Unicode 8 sizes.
|
|
|
|
// See https://gitlab.com/gnachman/iterm2/wikis/unicodeversionswitching
|
|
|
|
|
|
|
|
if (term == L"Apple_Terminal" && version >= 400) {
|
|
|
|
// Apple Terminal on High Sierra
|
|
|
|
g_guessed_fish_emoji_width = 2;
|
2019-01-25 21:34:40 +00:00
|
|
|
debug(2, "default emoji width: 2 for %ls", term.c_str());
|
2018-02-26 01:43:29 +00:00
|
|
|
} else {
|
|
|
|
g_guessed_fish_emoji_width = 1;
|
2019-01-25 21:34:40 +00:00
|
|
|
debug(2, "default emoji width: 1");
|
2018-02-26 01:43:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 04:09:26 +00:00
|
|
|
/// Initialize the curses subsystem.
|
2018-09-18 04:26:21 +00:00
|
|
|
static void init_curses(const environment_t &vars) {
|
2017-12-23 01:38:25 +00:00
|
|
|
for (const auto &var_name : curses_variables) {
|
|
|
|
std::string name = wcs2string(var_name);
|
2018-09-18 04:26:21 +00:00
|
|
|
const auto var = vars.get(var_name, ENV_EXPORT);
|
2017-08-06 01:22:49 +00:00
|
|
|
if (var.missing_or_empty()) {
|
|
|
|
debug(2, L"curses var %s missing or empty", name.c_str());
|
2017-02-16 04:09:26 +00:00
|
|
|
unsetenv(name.c_str());
|
|
|
|
} else {
|
2017-12-23 01:38:25 +00:00
|
|
|
std::string value = wcs2string(var->as_string());
|
2017-08-06 01:22:49 +00:00
|
|
|
debug(2, L"curses var %s='%s'", name.c_str(), value.c_str());
|
2017-02-16 04:09:26 +00:00
|
|
|
setenv(name.c_str(), value.c_str(), 1);
|
|
|
|
}
|
2016-06-02 03:03:50 +00:00
|
|
|
}
|
2017-02-04 03:20:21 +00:00
|
|
|
|
2017-02-16 04:09:26 +00:00
|
|
|
int err_ret;
|
|
|
|
if (setupterm(NULL, STDOUT_FILENO, &err_ret) == ERR) {
|
2018-09-18 04:26:21 +00:00
|
|
|
auto term = vars.get(L"TERM");
|
2017-02-16 04:09:26 +00:00
|
|
|
if (is_interactive_session) {
|
|
|
|
debug(1, _(L"Could not set up terminal."));
|
|
|
|
if (term.missing_or_empty()) {
|
|
|
|
debug(1, _(L"TERM environment variable not set."));
|
|
|
|
} else {
|
2017-08-28 07:25:41 +00:00
|
|
|
debug(1, _(L"TERM environment variable set to '%ls'."), term->as_string().c_str());
|
2017-02-16 04:09:26 +00:00
|
|
|
debug(1, _(L"Check that this terminal type is supported on this system."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!initialize_curses_using_fallback(DEFAULT_TERM1)) {
|
|
|
|
initialize_curses_using_fallback(DEFAULT_TERM2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 04:26:21 +00:00
|
|
|
can_set_term_title = does_term_support_setting_title(vars);
|
2017-02-16 04:09:26 +00:00
|
|
|
term_has_xn = tigetflag((char *)"xenl") == 1; // does terminal have the eat_newline_glitch
|
2018-09-18 04:26:21 +00:00
|
|
|
update_fish_color_support(vars);
|
2017-02-04 03:20:21 +00:00
|
|
|
// Invalidate the cached escape sequences since they may no longer be valid.
|
2018-02-04 23:03:50 +00:00
|
|
|
cached_layouts.clear();
|
2017-02-16 04:09:26 +00:00
|
|
|
curses_initialized = true;
|
2016-06-02 03:03:50 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// React to modifying the given variable.
|
2018-09-14 07:36:26 +00:00
|
|
|
static void react_to_variable_change(const wchar_t *op, const wcstring &key, env_stack_t &vars) {
|
2017-02-16 04:09:26 +00:00
|
|
|
// Don't do any of this until `env_init()` has run. We only want to do this in response to
|
2017-07-14 17:45:31 +00:00
|
|
|
// variables set by the user; e.g., in a script like *config.fish* or interactively or as part
|
2017-08-19 00:26:45 +00:00
|
|
|
// of loading the universal variables for the first time. Variables we import from the
|
|
|
|
// environment or that are otherwise set by fish before this gets called have to explicitly
|
|
|
|
// call the appropriate functions to put the value of the var into effect.
|
2017-02-16 04:09:26 +00:00
|
|
|
if (!env_initialized) return;
|
|
|
|
|
2017-08-19 00:26:45 +00:00
|
|
|
auto dispatch = var_dispatch_table.find(key);
|
|
|
|
if (dispatch != var_dispatch_table.end()) {
|
2018-09-14 07:36:26 +00:00
|
|
|
(*dispatch->second)(op, key, vars);
|
2016-04-29 01:26:46 +00:00
|
|
|
} else if (string_prefixes_string(L"fish_color_", key)) {
|
2012-03-25 10:00:38 +00:00
|
|
|
reader_react_to_color_change();
|
2016-01-15 05:46:53 +00:00
|
|
|
}
|
2012-03-05 22:18:16 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Universal variable callback function. This function makes sure the proper events are triggered
|
|
|
|
/// when an event occurs.
|
2018-09-09 19:17:31 +00:00
|
|
|
static void universal_callback(env_stack_t *stack, const callback_data_t &cb) {
|
2018-10-20 20:16:14 +00:00
|
|
|
const wchar_t *op = cb.is_erase() ? L"ERASE" : L"SET";
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2018-09-14 07:36:26 +00:00
|
|
|
react_to_variable_change(op, cb.key, *stack);
|
2018-09-09 19:17:31 +00:00
|
|
|
stack->mark_changed_exported();
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2018-10-20 20:16:14 +00:00
|
|
|
event_t ev = event_t::variable_event(cb.key);
|
2017-05-22 05:22:55 +00:00
|
|
|
ev.arguments.push_back(L"VARIABLE");
|
|
|
|
ev.arguments.push_back(op);
|
2018-10-20 20:16:14 +00:00
|
|
|
ev.arguments.push_back(cb.key);
|
2017-05-22 05:22:55 +00:00
|
|
|
event_fire(&ev);
|
2005-10-11 19:23:43 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Make sure the PATH variable contains something.
|
|
|
|
static void setup_path() {
|
2018-09-14 07:36:26 +00:00
|
|
|
auto &vars = env_stack_t::globals();
|
|
|
|
const auto path = vars.get(L"PATH");
|
2016-04-29 01:26:46 +00:00
|
|
|
if (path.missing_or_empty()) {
|
2019-01-24 18:42:56 +00:00
|
|
|
#if defined(_CS_PATH)
|
|
|
|
// _CS_PATH: colon-separated paths to find POSIX utilities
|
|
|
|
std::string cspath;
|
|
|
|
cspath.resize(confstr(_CS_PATH, nullptr, 0));
|
|
|
|
confstr(_CS_PATH, &cspath[0], cspath.length());
|
|
|
|
#else
|
2019-01-29 03:27:16 +00:00
|
|
|
std::string cspath = "/usr/bin:/bin"; // I doubt this is even necessary
|
2019-01-24 18:42:56 +00:00
|
|
|
#endif
|
|
|
|
vars.set_one(L"PATH", ENV_GLOBAL | ENV_EXPORT, str2wcstring(cspath));
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2006-01-21 20:42:17 +00:00
|
|
|
}
|
|
|
|
|
2017-02-16 04:09:26 +00:00
|
|
|
/// If they don't already exist initialize the `COLUMNS` and `LINES` env vars to reasonable
|
2017-01-18 21:54:54 +00:00
|
|
|
/// defaults. They will be updated later by the `get_current_winsize()` function if they need to be
|
|
|
|
/// adjusted.
|
2018-09-11 02:17:44 +00:00
|
|
|
void env_stack_t::set_termsize() {
|
2018-09-14 07:36:26 +00:00
|
|
|
auto &vars = env_stack_t::globals();
|
2018-09-11 02:17:44 +00:00
|
|
|
auto cols = get(L"COLUMNS");
|
2018-09-14 07:36:26 +00:00
|
|
|
if (cols.missing_or_empty()) vars.set_one(L"COLUMNS", ENV_GLOBAL, DFLT_TERM_COL_STR);
|
2017-02-10 23:20:09 +00:00
|
|
|
|
2018-09-11 02:17:44 +00:00
|
|
|
auto rows = get(L"LINES");
|
2018-09-14 07:36:26 +00:00
|
|
|
if (rows.missing_or_empty()) vars.set_one(L"LINES", ENV_GLOBAL, DFLT_TERM_ROW_STR);
|
2017-01-18 21:54:54 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 01:03:13 +00:00
|
|
|
/// Update the PWD variable directory from the result of getcwd().
|
2018-09-09 19:17:31 +00:00
|
|
|
void env_stack_t::set_pwd_from_getcwd() {
|
2017-08-05 22:08:39 +00:00
|
|
|
wcstring cwd = wgetcwd();
|
|
|
|
if (cwd.empty()) {
|
2016-04-29 01:26:46 +00:00
|
|
|
debug(0,
|
|
|
|
_(L"Could not determine current working directory. Is your locale set correctly?"));
|
2018-09-17 01:03:13 +00:00
|
|
|
return;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2018-09-14 07:36:26 +00:00
|
|
|
set_one(L"PWD", ENV_EXPORT | ENV_GLOBAL, cwd);
|
2008-01-16 22:07:38 +00:00
|
|
|
}
|
|
|
|
|
2017-02-08 01:21:35 +00:00
|
|
|
/// Allow the user to override the limit on how much data the `read` command will process.
|
|
|
|
/// This is primarily for testing but could be used by users in special situations.
|
2018-09-09 19:17:31 +00:00
|
|
|
void env_stack_t::set_read_limit() {
|
2018-09-18 04:26:21 +00:00
|
|
|
auto read_byte_limit_var = this->get(L"fish_read_limit");
|
2017-02-08 01:21:35 +00:00
|
|
|
if (!read_byte_limit_var.missing_or_empty()) {
|
2017-08-28 07:25:41 +00:00
|
|
|
size_t limit = fish_wcstoull(read_byte_limit_var->as_string().c_str());
|
2017-02-08 01:21:35 +00:00
|
|
|
if (errno) {
|
2017-10-14 15:22:57 +00:00
|
|
|
debug(1, "Ignoring fish_read_limit since it is not valid");
|
2017-02-08 01:21:35 +00:00
|
|
|
} else {
|
|
|
|
read_byte_limit = limit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-09 19:17:31 +00:00
|
|
|
void env_stack_t::mark_changed_exported() { vars_stack().mark_changed_exported(); }
|
|
|
|
|
2018-09-11 05:29:52 +00:00
|
|
|
wcstring environment_t::get_pwd_slash() const {
|
2018-09-29 00:09:45 +00:00
|
|
|
// Return "/" if PWD is missing.
|
|
|
|
// See https://github.com/fish-shell/fish-shell/issues/5080
|
2018-09-25 04:17:05 +00:00
|
|
|
auto pwd_var = get(L"PWD");
|
2018-09-19 23:10:38 +00:00
|
|
|
wcstring pwd;
|
|
|
|
if (!pwd_var.missing_or_empty()) {
|
|
|
|
pwd = pwd_var->as_string();
|
|
|
|
}
|
2016-04-29 01:26:46 +00:00
|
|
|
if (!string_suffixes_string(L"/", pwd)) {
|
2013-04-27 07:45:38 +00:00
|
|
|
pwd.push_back(L'/');
|
|
|
|
}
|
|
|
|
return pwd;
|
|
|
|
}
|
|
|
|
|
2016-11-28 17:24:49 +00:00
|
|
|
/// Set up the USER variable.
|
2016-11-27 00:35:48 +00:00
|
|
|
static void setup_user(bool force) {
|
2018-09-18 04:26:21 +00:00
|
|
|
auto &vars = env_stack_t::globals();
|
|
|
|
if (force || vars.get(L"USER").missing_or_empty()) {
|
2017-05-18 06:07:47 +00:00
|
|
|
struct passwd userinfo;
|
|
|
|
struct passwd *result;
|
|
|
|
char buf[8192];
|
|
|
|
int retval = getpwuid_r(getuid(), &userinfo, buf, sizeof(buf), &result);
|
|
|
|
if (!retval && result) {
|
2017-05-11 04:08:36 +00:00
|
|
|
const wcstring uname = str2wcstring(userinfo.pw_name);
|
2018-09-18 04:26:21 +00:00
|
|
|
vars.set_one(L"USER", ENV_GLOBAL | ENV_EXPORT, uname);
|
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);
|
|
|
|
setvbuf(stdout, NULL, _IONBF, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-09 19:17:31 +00:00
|
|
|
static void env_universal_callbacks(env_stack_t *stack, const callback_data_list_t &callbacks) {
|
2018-10-20 20:16:14 +00:00
|
|
|
for (const callback_data_t &cb : callbacks) {
|
2018-09-09 19:17:31 +00:00
|
|
|
universal_callback(stack, cb);
|
2017-07-14 17:45:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-09 19:17:31 +00:00
|
|
|
void env_stack_t::universal_barrier() {
|
2017-07-14 17:45:31 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
|
|
|
if (!uvars()) return;
|
|
|
|
|
|
|
|
callback_data_list_t callbacks;
|
|
|
|
bool changed = uvars()->sync(callbacks);
|
|
|
|
if (changed) {
|
|
|
|
universal_notifier_t::default_notifier().post_notification();
|
|
|
|
}
|
|
|
|
|
2018-09-09 19:17:31 +00:00
|
|
|
env_universal_callbacks(this, callbacks);
|
2017-07-14 17:45:31 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 07:36:26 +00:00
|
|
|
static void handle_fish_term_change(const wcstring &op, const wcstring &var_name,
|
|
|
|
env_stack_t &vars) {
|
2017-08-19 00:26:45 +00:00
|
|
|
UNUSED(op);
|
|
|
|
UNUSED(var_name);
|
2018-09-18 04:26:21 +00:00
|
|
|
update_fish_color_support(vars);
|
2017-08-19 00:26:45 +00:00
|
|
|
reader_react_to_color_change();
|
|
|
|
}
|
|
|
|
|
2018-09-14 07:36:26 +00:00
|
|
|
static void handle_escape_delay_change(const wcstring &op, const wcstring &var_name,
|
|
|
|
env_stack_t &vars) {
|
2017-08-19 00:26:45 +00:00
|
|
|
UNUSED(op);
|
|
|
|
UNUSED(var_name);
|
2018-09-18 04:26:21 +00:00
|
|
|
update_wait_on_escape_ms(vars);
|
2017-08-19 00:26:45 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 07:36:26 +00:00
|
|
|
static void handle_change_emoji_width(const wcstring &op, const wcstring &var_name,
|
|
|
|
env_stack_t &vars) {
|
2018-04-01 00:06:13 +00:00
|
|
|
(void)op;
|
|
|
|
(void)var_name;
|
2018-02-26 01:43:29 +00:00
|
|
|
int new_width = 0;
|
2018-09-18 04:26:21 +00:00
|
|
|
if (auto width_str = vars.get(L"fish_emoji_width")) {
|
2018-02-26 01:43:29 +00:00
|
|
|
new_width = fish_wcstol(width_str->as_string().c_str());
|
|
|
|
}
|
|
|
|
g_fish_emoji_width = std::max(0, new_width);
|
2019-01-25 21:34:40 +00:00
|
|
|
debug(2, "'fish_emoji_width' preference: %d, overwriting default", g_fish_emoji_width);
|
2018-02-26 01:43:29 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 07:36:26 +00:00
|
|
|
static void handle_change_ambiguous_width(const wcstring &op, const wcstring &var_name,
|
|
|
|
env_stack_t &vars) {
|
2018-08-18 22:38:05 +00:00
|
|
|
(void)op;
|
|
|
|
(void)var_name;
|
|
|
|
int new_width = 1;
|
2018-09-18 04:26:21 +00:00
|
|
|
if (auto width_str = vars.get(L"fish_ambiguous_width")) {
|
2018-08-18 22:38:05 +00:00
|
|
|
new_width = fish_wcstol(width_str->as_string().c_str());
|
|
|
|
}
|
|
|
|
g_fish_ambiguous_width = std::max(0, new_width);
|
|
|
|
}
|
|
|
|
|
2018-09-14 07:36:26 +00:00
|
|
|
static void handle_term_size_change(const wcstring &op, const wcstring &var_name,
|
|
|
|
env_stack_t &vars) {
|
2017-08-19 00:26:45 +00:00
|
|
|
UNUSED(op);
|
|
|
|
UNUSED(var_name);
|
2019-01-13 23:03:19 +00:00
|
|
|
UNUSED(vars);
|
2017-08-19 00:26:45 +00:00
|
|
|
invalidate_termsize(true); // force fish to update its idea of the terminal size plus vars
|
|
|
|
}
|
|
|
|
|
2018-09-14 07:36:26 +00:00
|
|
|
static void handle_read_limit_change(const wcstring &op, const wcstring &var_name,
|
|
|
|
env_stack_t &vars) {
|
2017-08-19 00:26:45 +00:00
|
|
|
UNUSED(op);
|
|
|
|
UNUSED(var_name);
|
2018-09-25 03:59:55 +00:00
|
|
|
vars.set_read_limit();
|
2017-08-19 00:26:45 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 07:36:26 +00:00
|
|
|
static void handle_fish_history_change(const wcstring &op, const wcstring &var_name,
|
|
|
|
env_stack_t &vars) {
|
2017-08-19 00:26:45 +00:00
|
|
|
UNUSED(op);
|
|
|
|
UNUSED(var_name);
|
2018-09-18 16:18:47 +00:00
|
|
|
reader_change_history(history_session_id(vars).c_str());
|
2017-08-19 00:26:45 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 07:36:26 +00:00
|
|
|
static void handle_function_path_change(const wcstring &op, const wcstring &var_name,
|
|
|
|
env_stack_t &vars) {
|
2018-02-16 05:58:02 +00:00
|
|
|
UNUSED(op);
|
|
|
|
UNUSED(var_name);
|
2019-01-13 23:03:19 +00:00
|
|
|
UNUSED(vars);
|
2018-02-16 05:58:02 +00:00
|
|
|
function_invalidate_path();
|
|
|
|
}
|
|
|
|
|
2018-09-14 07:36:26 +00:00
|
|
|
static void handle_complete_path_change(const wcstring &op, const wcstring &var_name,
|
|
|
|
env_stack_t &vars) {
|
2018-02-16 05:58:02 +00:00
|
|
|
UNUSED(op);
|
|
|
|
UNUSED(var_name);
|
2019-01-13 23:03:19 +00:00
|
|
|
UNUSED(vars);
|
2018-02-16 05:58:02 +00:00
|
|
|
complete_invalidate_path();
|
|
|
|
}
|
|
|
|
|
2018-09-14 07:36:26 +00:00
|
|
|
static void handle_tz_change(const wcstring &op, const wcstring &var_name, env_stack_t &vars) {
|
2017-08-19 00:26:45 +00:00
|
|
|
UNUSED(op);
|
2018-09-18 04:26:21 +00:00
|
|
|
handle_timezone(var_name.c_str(), vars);
|
2017-08-19 00:26:45 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 07:36:26 +00:00
|
|
|
static void handle_magic_colon_var_change(const wcstring &op, const wcstring &var_name,
|
|
|
|
env_stack_t &vars) {
|
2017-08-19 00:26:45 +00:00
|
|
|
UNUSED(op);
|
2018-09-14 07:36:26 +00:00
|
|
|
fix_colon_delimited_var(var_name, vars);
|
2017-08-19 00:26:45 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 07:36:26 +00:00
|
|
|
static void handle_locale_change(const wcstring &op, const wcstring &var_name, env_stack_t &vars) {
|
2017-08-19 00:26:45 +00:00
|
|
|
UNUSED(op);
|
|
|
|
UNUSED(var_name);
|
2018-09-18 04:26:21 +00:00
|
|
|
init_locale(vars);
|
2017-08-19 00:26:45 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 07:36:26 +00:00
|
|
|
static void handle_curses_change(const wcstring &op, const wcstring &var_name, env_stack_t &vars) {
|
2017-08-19 00:26:45 +00:00
|
|
|
UNUSED(op);
|
|
|
|
UNUSED(var_name);
|
2018-02-26 01:43:29 +00:00
|
|
|
guess_emoji_width();
|
2018-09-18 04:26:21 +00:00
|
|
|
init_curses(vars);
|
2017-08-19 00:26:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Populate the dispatch table used by `react_to_variable_change()` to efficiently call the
|
|
|
|
/// appropriate function to handle a change to a variable.
|
|
|
|
static void setup_var_dispatch_table() {
|
2017-12-23 01:31:25 +00:00
|
|
|
for (const auto &var_name : locale_variables) {
|
2017-08-19 00:26:45 +00:00
|
|
|
var_dispatch_table.emplace(var_name, handle_locale_change);
|
|
|
|
}
|
|
|
|
|
2017-12-23 01:31:25 +00:00
|
|
|
for (const auto &var_name : curses_variables) {
|
2017-08-19 00:26:45 +00:00
|
|
|
var_dispatch_table.emplace(var_name, handle_curses_change);
|
|
|
|
}
|
|
|
|
|
2018-09-10 01:32:15 +00:00
|
|
|
var_dispatch_table.emplace(L"PATH", handle_magic_colon_var_change);
|
|
|
|
var_dispatch_table.emplace(L"CDPATH", handle_magic_colon_var_change);
|
2017-08-19 00:26:45 +00:00
|
|
|
var_dispatch_table.emplace(L"fish_term256", handle_fish_term_change);
|
|
|
|
var_dispatch_table.emplace(L"fish_term24bit", handle_fish_term_change);
|
|
|
|
var_dispatch_table.emplace(L"fish_escape_delay_ms", handle_escape_delay_change);
|
2018-02-26 01:43:29 +00:00
|
|
|
var_dispatch_table.emplace(L"fish_emoji_width", handle_change_emoji_width);
|
2018-08-18 22:38:05 +00:00
|
|
|
var_dispatch_table.emplace(L"fish_ambiguous_width", handle_change_ambiguous_width);
|
2017-08-19 00:26:45 +00:00
|
|
|
var_dispatch_table.emplace(L"LINES", handle_term_size_change);
|
|
|
|
var_dispatch_table.emplace(L"COLUMNS", handle_term_size_change);
|
2018-02-16 05:58:02 +00:00
|
|
|
var_dispatch_table.emplace(L"fish_complete_path", handle_complete_path_change);
|
|
|
|
var_dispatch_table.emplace(L"fish_function_path", handle_function_path_change);
|
2017-10-14 15:22:57 +00:00
|
|
|
var_dispatch_table.emplace(L"fish_read_limit", handle_read_limit_change);
|
2017-09-24 06:07:45 +00:00
|
|
|
var_dispatch_table.emplace(L"fish_history", handle_fish_history_change);
|
2017-08-19 00:26:45 +00:00
|
|
|
var_dispatch_table.emplace(L"TZ", handle_tz_change);
|
|
|
|
}
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
void env_init(const struct config_paths_t *paths /* or NULL */) {
|
2017-08-19 00:26:45 +00:00
|
|
|
setup_var_dispatch_table();
|
|
|
|
|
2018-09-14 07:36:26 +00:00
|
|
|
env_stack_t &vars = env_stack_t::globals();
|
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;
|
2016-03-06 03:02:50 +00:00
|
|
|
size_t 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).
|
2014-07-12 07:53:23 +00:00
|
|
|
if (is_read_only(key_and_val) || is_electric(key_and_val)) continue;
|
2018-09-11 04:27:25 +00:00
|
|
|
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);
|
2018-09-10 01:32:15 +00:00
|
|
|
val.assign(key_and_val, eql+1, wcstring::npos);
|
2014-07-12 07:53:23 +00:00
|
|
|
if (is_read_only(key) || is_electric(key)) continue;
|
2018-09-14 07:36:26 +00:00
|
|
|
vars.set(key, ENV_EXPORT | ENV_GLOBAL, {val});
|
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.
|
|
|
|
if (paths != NULL) {
|
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);
|
2012-07-18 17:50:38 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2018-12-15 04:09:29 +00:00
|
|
|
wcstring user_config_dir;
|
|
|
|
path_get_config(user_config_dir);
|
2018-09-14 07:36:26 +00:00
|
|
|
vars.set_one(FISH_CONFIG_DIR, ENV_GLOBAL, user_config_dir);
|
2018-12-15 04:09:29 +00:00
|
|
|
|
|
|
|
wcstring user_data_dir;
|
|
|
|
path_get_data(user_data_dir);
|
2018-09-14 07:36:26 +00:00
|
|
|
vars.set_one(FISH_USER_DATA_DIR, ENV_GLOBAL, user_data_dir);
|
2018-12-15 04:09:29 +00:00
|
|
|
|
2018-09-18 04:26:21 +00:00
|
|
|
init_locale(vars);
|
|
|
|
init_curses(vars);
|
2017-02-16 04:09:26 +00:00
|
|
|
init_input();
|
2017-04-04 06:16:11 +00:00
|
|
|
init_path_vars();
|
2018-02-26 01:43:29 +00:00
|
|
|
guess_emoji_width();
|
2017-02-16 04:09:26 +00:00
|
|
|
|
2016-11-28 17:24:49 +00:00
|
|
|
// Set up the USER and PATH variables
|
2012-11-19 00:30:30 +00:00
|
|
|
setup_path();
|
2017-04-07 13:06:54 +00:00
|
|
|
|
|
|
|
// Some `su`s keep $USER when changing to root.
|
|
|
|
// This leads to issues later on (and e.g. in prompts),
|
|
|
|
// so we work around it by resetting $USER.
|
2017-05-02 04:44:30 +00:00
|
|
|
// TODO: Figure out if that su actually checks if username == "root"(as the man page says) or
|
|
|
|
// UID == 0.
|
2017-04-07 13:06:54 +00:00
|
|
|
uid_t uid = getuid();
|
|
|
|
setup_user(uid == 0);
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2018-05-03 10:49:21 +00:00
|
|
|
// Set up $IFS - this used to be in share/config.fish, but really breaks if it isn't done.
|
2018-09-14 07:36:26 +00:00
|
|
|
vars.set_one(L"IFS", ENV_GLOBAL, L"\n \t");
|
2018-05-03 10:49:21 +00:00
|
|
|
|
2016-05-26 14:11:26 +00:00
|
|
|
// Set up the version variable.
|
2014-05-01 07:46:27 +00:00
|
|
|
wcstring version = str2wcstring(get_fish_version());
|
2018-09-14 07:36:26 +00:00
|
|
|
vars.set_one(L"version", ENV_GLOBAL, version);
|
|
|
|
vars.set_one(L"FISH_VERSION", ENV_GLOBAL, version);
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2018-10-10 21:26:29 +00:00
|
|
|
// Set the $fish_pid variable.
|
2019-02-04 00:06:10 +00:00
|
|
|
vars.set_one(L"fish_pid", ENV_GLOBAL, to_string(getpid()));
|
2018-03-09 09:47:28 +00:00
|
|
|
|
2018-03-09 21:02:32 +00:00
|
|
|
// Set the $hostname variable
|
|
|
|
wcstring hostname = L"fish";
|
|
|
|
get_hostname_identifier(hostname);
|
2018-09-14 07:36:26 +00:00
|
|
|
vars.set_one(L"hostname", ENV_GLOBAL, hostname);
|
2018-03-09 21:02:32 +00:00
|
|
|
|
2018-09-25 04:17:05 +00:00
|
|
|
// Set up SHLVL variable. Not we can't use vars.get() because SHLVL is read-only, and therefore
|
|
|
|
// was not inherited from the environment.
|
2012-05-09 10:23:31 +00:00
|
|
|
wcstring nshlvl_str = L"1";
|
2018-01-30 21:22:36 +00:00
|
|
|
if (const char *shlvl_var = getenv("SHLVL")) {
|
2016-11-23 04:24:03 +00:00
|
|
|
const wchar_t *end;
|
|
|
|
// TODO: Figure out how to handle invalid numbers better. Shouldn't we issue a diagnostic?
|
2018-01-30 21:22:36 +00:00
|
|
|
long shlvl_i = fish_wcstol(str2wcstring(shlvl_var).c_str(), &end);
|
2016-11-23 04:24:03 +00:00
|
|
|
if (!errno && shlvl_i >= 0) {
|
2019-02-04 00:06:10 +00:00
|
|
|
nshlvl_str = to_string(shlvl_i + 1);
|
2012-05-09 10:23:31 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2018-09-14 07:36:26 +00:00
|
|
|
vars.set_one(L"SHLVL", ENV_GLOBAL | ENV_EXPORT, nshlvl_str);
|
2010-10-08 00:35:22 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
// Set up the HOME variable.
|
2017-04-07 13:06:54 +00:00
|
|
|
// Unlike $USER, it doesn't seem that `su`s pass this along
|
|
|
|
// if the target user is root, unless "--preserve-environment" is used.
|
|
|
|
// Since that is an explicit choice, we should allow it to enable e.g.
|
|
|
|
// env HOME=(mktemp -d) su --preserve-environment fish
|
2018-09-18 04:26:21 +00:00
|
|
|
if (vars.get(L"HOME").missing_or_empty()) {
|
|
|
|
auto user_var = vars.get(L"USER");
|
2017-08-06 01:22:49 +00:00
|
|
|
if (!user_var.missing_or_empty()) {
|
2017-08-28 07:25:41 +00:00
|
|
|
char *unam_narrow = wcs2str(user_var->as_string());
|
2017-07-20 17:45:03 +00:00
|
|
|
struct passwd userinfo;
|
|
|
|
struct passwd *result;
|
|
|
|
char buf[8192];
|
|
|
|
int retval = getpwnam_r(unam_narrow, &userinfo, buf, sizeof(buf), &result);
|
|
|
|
if (retval || !result) {
|
|
|
|
// Maybe USER is set but it's bogus. Reset USER from the db and try again.
|
|
|
|
setup_user(true);
|
2018-09-18 04:26:21 +00:00
|
|
|
user_var = vars.get(L"USER");
|
2017-08-06 01:22:49 +00:00
|
|
|
if (!user_var.missing_or_empty()) {
|
2017-08-28 07:25:41 +00:00
|
|
|
unam_narrow = wcs2str(user_var->as_string());
|
2017-08-06 01:22:49 +00:00
|
|
|
retval = getpwnam_r(unam_narrow, &userinfo, buf, sizeof(buf), &result);
|
|
|
|
}
|
2017-07-20 17:45:03 +00:00
|
|
|
}
|
|
|
|
if (!retval && result && userinfo.pw_dir) {
|
|
|
|
const wcstring dir = str2wcstring(userinfo.pw_dir);
|
2018-09-14 07:36:26 +00:00
|
|
|
vars.set_one(L"HOME", ENV_GLOBAL | ENV_EXPORT, dir);
|
2017-07-20 17:45:03 +00:00
|
|
|
} else {
|
2017-08-05 22:08:39 +00:00
|
|
|
// We cannot get $HOME. This triggers warnings for history and config.fish already,
|
2017-07-20 17:45:03 +00:00
|
|
|
// so it isn't necessary to warn here as well.
|
2018-09-11 04:27:25 +00:00
|
|
|
vars.set_empty(L"HOME", ENV_GLOBAL | ENV_EXPORT);
|
2017-07-20 17:45:03 +00:00
|
|
|
}
|
|
|
|
free(unam_narrow);
|
2017-07-20 14:06:01 +00:00
|
|
|
} else {
|
2017-08-05 22:08:39 +00:00
|
|
|
// If $USER is empty as well (which we tried to set above), we can't get $HOME.
|
2018-09-11 04:27:25 +00:00
|
|
|
vars.set_empty(L"HOME", ENV_GLOBAL | ENV_EXPORT);
|
2014-07-25 17:42:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 01:03:13 +00:00
|
|
|
// initialize the PWD variable if necessary
|
|
|
|
// Note we may inherit a virtual PWD that doesn't match what getcwd would return; respect that.
|
2019-01-22 21:07:55 +00:00
|
|
|
// Note we treat PWD as read-only so it was not set in vars.
|
|
|
|
const char *incoming_pwd = getenv("PWD");
|
|
|
|
if (incoming_pwd && incoming_pwd[0]) {
|
|
|
|
vars.set_one(L"PWD", ENV_EXPORT | ENV_GLOBAL, str2wcstring(incoming_pwd));
|
|
|
|
} else {
|
2018-09-11 02:17:44 +00:00
|
|
|
vars.set_pwd_from_getcwd();
|
2018-09-17 01:03:13 +00:00
|
|
|
}
|
2018-09-11 02:17:44 +00:00
|
|
|
vars.set_termsize(); // initialize the terminal size variables
|
|
|
|
vars.set_read_limit(); // initialize the read_byte_limit
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
// Set g_use_posix_spawn. Default to true.
|
2018-09-11 02:17:44 +00:00
|
|
|
auto use_posix_spawn = vars.get(L"fish_use_posix_spawn");
|
2017-08-06 23:05:51 +00:00
|
|
|
g_use_posix_spawn =
|
2019-02-04 00:06:10 +00:00
|
|
|
use_posix_spawn.missing_or_empty() ? true : bool_from_string(use_posix_spawn->as_string());
|
2014-01-19 09:27:39 +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
|
|
|
|
2017-07-14 17:45:31 +00:00
|
|
|
// 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
|
|
|
|
// code for setting vars that might have side-effects will do whatever
|
|
|
|
// `react_to_variable_change()` would do for that var.
|
|
|
|
env_initialized = true;
|
|
|
|
|
|
|
|
// Set up universal variables. The empty string means to use the default path.
|
|
|
|
assert(s_universal_variables == NULL);
|
|
|
|
s_universal_variables = new env_universal_t(L"");
|
|
|
|
callback_data_list_t callbacks;
|
2018-04-02 00:43:12 +00:00
|
|
|
s_universal_variables->initialize(callbacks);
|
2018-09-09 19:17:31 +00:00
|
|
|
env_universal_callbacks(&env_stack_t::principal(), callbacks);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Search all visible scopes in order for the specified key. Return the first scope in which it was
|
|
|
|
/// found.
|
2018-09-10 08:17:37 +00:00
|
|
|
env_node_ref_t env_stack_t::get_node(const wcstring &key) {
|
|
|
|
env_node_ref_t env = vars_stack().top;
|
|
|
|
while (env) {
|
2017-08-28 07:25:41 +00:00
|
|
|
if (env->find_entry(key)) break;
|
2017-01-26 19:32:45 +00:00
|
|
|
env = vars_stack().next_scope_to_search(env);
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
return env;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2006-01-08 23:00:49 +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()) {
|
|
|
|
mask = fish_wcstol(list_val.front().c_str(), NULL, 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
|
|
|
}
|
|
|
|
|
2018-10-22 07:49:09 +00:00
|
|
|
/// Set a universal variable, inheriting as applicable from the given old variable.
|
|
|
|
static void env_set_internal_universal(const wcstring &key, wcstring_list_t val,
|
2018-09-09 19:17:31 +00:00
|
|
|
env_mode_flags_t input_var_mode, env_stack_t *stack) {
|
2018-10-22 07:49:09 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
|
|
|
if (!uvars()) return;
|
|
|
|
env_mode_flags_t var_mode = input_var_mode;
|
|
|
|
auto oldvar = uvars()->get(key);
|
|
|
|
// Resolve whether or not to export.
|
|
|
|
if ((var_mode & (ENV_EXPORT | ENV_UNEXPORT)) == 0) {
|
|
|
|
bool doexport = oldvar ? oldvar->exports() : false;
|
|
|
|
var_mode |= (doexport ? ENV_EXPORT : ENV_UNEXPORT);
|
|
|
|
}
|
|
|
|
// Resolve whether to be a path variable.
|
|
|
|
// Here we fall back to the auto-pathvar behavior.
|
|
|
|
if ((var_mode & (ENV_PATHVAR | ENV_UNPATHVAR)) == 0) {
|
|
|
|
bool dopathvar = oldvar ? oldvar->is_pathvar() : variable_should_auto_pathvar(key);
|
|
|
|
var_mode |= (dopathvar ? ENV_PATHVAR : ENV_UNPATHVAR);
|
|
|
|
}
|
|
|
|
|
2018-10-27 22:20:32 +00:00
|
|
|
// Split about ':' if it's a path variable.
|
|
|
|
if (var_mode & ENV_PATHVAR) {
|
|
|
|
wcstring_list_t split_val;
|
|
|
|
for (const wcstring &str : val) {
|
|
|
|
vec_append(split_val, split_string(str, PATH_ARRAY_SEP));
|
|
|
|
}
|
|
|
|
val = std::move(split_val);
|
|
|
|
}
|
|
|
|
|
2018-10-22 07:49:09 +00:00
|
|
|
// Construct and set the new variable.
|
|
|
|
env_var_t::env_var_flags_t varflags = 0;
|
|
|
|
if (var_mode & ENV_EXPORT) varflags |= env_var_t::flag_export;
|
|
|
|
if (var_mode & ENV_PATHVAR) varflags |= env_var_t::flag_pathvar;
|
|
|
|
env_var_t new_var{val, varflags};
|
|
|
|
|
|
|
|
uvars()->set(key, new_var);
|
|
|
|
env_universal_barrier();
|
|
|
|
if (new_var.exports() || (oldvar && oldvar->exports())) {
|
2018-09-09 19:17:31 +00:00
|
|
|
stack->mark_changed_exported();
|
2018-10-22 07:49:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-30 00:25:48 +00:00
|
|
|
/// Set the value of the environment variable whose name matches key to val.
|
|
|
|
///
|
|
|
|
/// \param key The key
|
2017-08-28 09:51:34 +00:00
|
|
|
/// \param val The value to set.
|
2016-11-05 01:11:37 +00:00
|
|
|
/// \param var_mode The type of the variable. Can be any combination of ENV_GLOBAL, ENV_LOCAL,
|
2017-08-05 22:08:39 +00:00
|
|
|
/// ENV_EXPORT and ENV_USER. If mode is ENV_DEFAULT, the current variable space is searched and the
|
|
|
|
/// current mode is used. If no current variable with the same name is found, ENV_LOCAL is assumed.
|
2016-10-30 00:25:48 +00:00
|
|
|
///
|
|
|
|
/// Returns:
|
|
|
|
///
|
|
|
|
/// * ENV_OK on success.
|
|
|
|
/// * ENV_PERM, can only be returned when setting as a user, e.g. ENV_USER is set. This means that
|
|
|
|
/// the user tried to change a read-only variable.
|
|
|
|
/// * ENV_SCOPE, the variable cannot be set in the given scope. This applies to readonly/electric
|
|
|
|
/// variables set from the local or universal scopes, or set as exported.
|
|
|
|
/// * ENV_INVALID, the variable value was invalid. This applies only to special variables.
|
2018-09-09 19:17:31 +00:00
|
|
|
int env_stack_t::set_internal(const wcstring &key, env_mode_flags_t input_var_mode, wcstring_list_t val) {
|
2012-03-31 22:33:34 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2018-09-10 01:32:15 +00:00
|
|
|
env_mode_flags_t var_mode = input_var_mode;
|
2018-09-14 07:36:26 +00:00
|
|
|
bool has_changed_old = vars_stack().has_changed_exported();
|
2016-04-29 01:26:46 +00:00
|
|
|
int done = 0;
|
2013-01-21 02:34:18 +00:00
|
|
|
|
2017-08-28 09:51:34 +00:00
|
|
|
if (val.size() == 1 && (key == L"PWD" || key == L"HOME")) {
|
2016-04-29 01:26:46 +00:00
|
|
|
// Canonicalize our path; if it changes, recurse and try again.
|
2017-08-28 09:51:34 +00:00
|
|
|
wcstring val_canonical = val.front();
|
2012-02-08 05:23:12 +00:00
|
|
|
path_make_canonical(val_canonical);
|
2017-08-28 09:51:34 +00:00
|
|
|
if (val.front() != val_canonical) {
|
2018-09-09 19:17:31 +00:00
|
|
|
return set_internal(key, var_mode, {val_canonical});
|
2012-02-08 05:23:12 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2013-01-21 02:34:18 +00:00
|
|
|
|
2017-08-05 22:08:39 +00:00
|
|
|
if ((var_mode & ENV_LOCAL || var_mode & ENV_UNIVERSAL) &&
|
|
|
|
(is_read_only(key) || is_electric(key))) {
|
2014-07-12 21:05:42 +00:00
|
|
|
return ENV_SCOPE;
|
|
|
|
}
|
2016-04-29 01:26:46 +00:00
|
|
|
if ((var_mode & ENV_EXPORT) && is_electric(key)) {
|
2014-07-12 21:05:42 +00:00
|
|
|
return ENV_SCOPE;
|
|
|
|
}
|
2016-04-29 01:26:46 +00:00
|
|
|
if ((var_mode & ENV_USER) && is_read_only(key)) {
|
2012-11-19 00:30:30 +00:00
|
|
|
return ENV_PERM;
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2013-01-21 02:34:18 +00:00
|
|
|
|
2017-08-19 22:45:46 +00:00
|
|
|
if (key == L"umask") { // set new umask
|
2017-08-28 09:51:34 +00:00
|
|
|
return set_umask(val);
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2013-01-21 02:34:18 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
if (var_mode & ENV_UNIVERSAL) {
|
|
|
|
if (uvars()) {
|
2018-09-09 19:17:31 +00:00
|
|
|
env_set_internal_universal(key, std::move(val), var_mode, this);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-04-29 01:26:46 +00:00
|
|
|
} else {
|
|
|
|
// Determine the node.
|
2016-02-19 23:45:12 +00:00
|
|
|
bool has_changed_new = false;
|
2018-09-10 08:17:37 +00:00
|
|
|
env_node_ref_t preexisting_node = get_node(key);
|
2018-09-10 01:32:15 +00:00
|
|
|
maybe_t<env_var_t::env_var_flags_t> preexisting_flags{};
|
2016-04-29 01:26:46 +00:00
|
|
|
if (preexisting_node != NULL) {
|
2013-01-19 21:16:21 +00:00
|
|
|
var_table_t::const_iterator result = preexisting_node->env.find(key);
|
|
|
|
assert(result != preexisting_node->env.end());
|
2018-09-10 01:32:15 +00:00
|
|
|
preexisting_flags = result->second.get_flags();
|
|
|
|
if (*preexisting_flags & env_var_t::flag_export) {
|
2012-11-19 00:30:30 +00:00
|
|
|
has_changed_new = true;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2013-01-21 02:34:18 +00:00
|
|
|
|
2018-09-10 08:17:37 +00:00
|
|
|
env_node_ref_t node = nullptr;
|
2016-04-29 01:26:46 +00:00
|
|
|
if (var_mode & ENV_GLOBAL) {
|
2017-01-26 19:06:03 +00:00
|
|
|
node = vars_stack().global_env;
|
2016-04-29 01:26:46 +00:00
|
|
|
} else if (var_mode & ENV_LOCAL) {
|
2018-09-10 08:17:37 +00:00
|
|
|
node = vars_stack().top;
|
2016-04-29 01:26:46 +00:00
|
|
|
} else if (preexisting_node != NULL) {
|
2013-01-19 21:16:21 +00:00
|
|
|
node = preexisting_node;
|
2016-04-29 01:26:46 +00:00
|
|
|
if ((var_mode & (ENV_EXPORT | ENV_UNEXPORT)) == 0) {
|
2017-08-09 18:11:58 +00:00
|
|
|
// Use existing entry's exportv status.
|
2018-09-10 01:32:15 +00:00
|
|
|
if (preexisting_flags && (*preexisting_flags & env_var_t::flag_export)) {
|
|
|
|
var_mode |= ENV_EXPORT;
|
|
|
|
}
|
2013-01-19 21:16:21 +00:00
|
|
|
}
|
2016-04-29 01:26:46 +00:00
|
|
|
} else {
|
|
|
|
if (!get_proc_had_barrier()) {
|
2013-01-19 21:16:21 +00:00
|
|
|
set_proc_had_barrier(true);
|
|
|
|
env_universal_barrier();
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2017-08-28 07:25:41 +00:00
|
|
|
if (uvars() && uvars()->get(key)) {
|
2018-10-22 07:49:09 +00:00
|
|
|
// Modifying an existing universal variable.
|
2018-09-09 19:17:31 +00:00
|
|
|
env_set_internal_universal(key, std::move(val), var_mode, this);
|
2018-10-22 07:49:09 +00:00
|
|
|
done = true;
|
2016-04-29 01:26:46 +00:00
|
|
|
} else {
|
2017-01-26 20:11:32 +00:00
|
|
|
// New variable with unspecified scope
|
|
|
|
node = vars_stack().resolve_unspecified_scope();
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2013-01-21 02:34:18 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
if (!done) {
|
2018-09-10 01:32:15 +00:00
|
|
|
// Resolve if we should mark ourselves as a path variable or not.
|
|
|
|
// If there's an existing variable, use its path flag; otherwise infer it.
|
|
|
|
if ((var_mode & (ENV_PATHVAR | ENV_UNPATHVAR)) == 0) {
|
|
|
|
bool should_pathvar = false;
|
2018-11-04 16:02:04 +00:00
|
|
|
if (preexisting_flags) {
|
|
|
|
should_pathvar = *preexisting_flags & env_var_t::flag_pathvar;
|
2018-09-10 01:32:15 +00:00
|
|
|
} else {
|
|
|
|
should_pathvar = variable_should_auto_pathvar(key);
|
|
|
|
}
|
|
|
|
var_mode |= should_pathvar ? ENV_PATHVAR : ENV_UNPATHVAR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split about ':' if it's a path variable.
|
|
|
|
if (var_mode & ENV_PATHVAR) {
|
|
|
|
wcstring_list_t split_val;
|
|
|
|
for (const wcstring &str : val) {
|
|
|
|
vec_append(split_val, split_string(str, PATH_ARRAY_SEP));
|
|
|
|
}
|
|
|
|
val = std::move(split_val);
|
|
|
|
}
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
// Set the entry in the node. Note that operator[] accesses the existing entry, or
|
|
|
|
// creates a new one.
|
2017-08-09 18:11:58 +00:00
|
|
|
env_var_t &var = node->env[key];
|
2018-01-30 20:36:50 +00:00
|
|
|
if (var.exports()) {
|
2016-04-29 01:26:46 +00:00
|
|
|
// This variable already existed, and was exported.
|
2013-01-19 21:16:21 +00:00
|
|
|
has_changed_new = true;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2017-08-09 18:11:58 +00:00
|
|
|
|
2017-08-28 09:51:34 +00:00
|
|
|
var.set_vals(std::move(val));
|
2018-09-10 01:32:15 +00:00
|
|
|
var.set_pathvar(var_mode & ENV_PATHVAR);
|
2019-01-18 18:24:49 +00:00
|
|
|
var.set_read_only(is_read_only(key));
|
2017-08-28 09:51:34 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
if (var_mode & ENV_EXPORT) {
|
|
|
|
// The new variable is exported.
|
2018-01-30 20:36:50 +00:00
|
|
|
var.set_exports(true);
|
2013-01-19 21:16:21 +00:00
|
|
|
node->exportv = true;
|
|
|
|
has_changed_new = true;
|
2016-04-29 01:26:46 +00:00
|
|
|
} else {
|
2018-01-30 20:36:50 +00:00
|
|
|
var.set_exports(false);
|
2017-08-09 18:11:58 +00:00
|
|
|
// Set the node's exported when it changes something about exports
|
2017-06-21 14:48:48 +00:00
|
|
|
// (also when it redefines a variable to not be exported).
|
|
|
|
node->exportv = has_changed_old != has_changed_new;
|
2012-02-17 20:23:30 +00:00
|
|
|
}
|
2013-01-21 02:34:18 +00:00
|
|
|
|
2017-01-26 20:03:14 +00:00
|
|
|
if (has_changed_old || has_changed_new) vars_stack().mark_changed_exported();
|
2012-02-17 20:23:30 +00:00
|
|
|
}
|
|
|
|
}
|
2013-01-21 02:34:18 +00:00
|
|
|
|
2015-02-04 00:13:02 +00:00
|
|
|
event_t ev = event_t::variable_event(key);
|
|
|
|
ev.arguments.reserve(3);
|
|
|
|
ev.arguments.push_back(L"VARIABLE");
|
|
|
|
ev.arguments.push_back(L"SET");
|
|
|
|
ev.arguments.push_back(key);
|
|
|
|
event_fire(&ev);
|
2018-09-14 07:36:26 +00:00
|
|
|
react_to_variable_change(L"SET", key, *this);
|
2016-10-30 00:25:48 +00:00
|
|
|
return ENV_OK;
|
2012-02-17 20:23:30 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2017-08-28 09:51:34 +00:00
|
|
|
/// Sets the variable with the specified name to the given values.
|
2018-09-09 19:17:31 +00:00
|
|
|
int env_stack_t::set(const wcstring &key, env_mode_flags_t mode, wcstring_list_t vals) {
|
|
|
|
return set_internal(key, mode, std::move(vals));
|
2017-08-19 22:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the variable with the specified name to a single value.
|
2018-09-09 19:17:31 +00:00
|
|
|
int env_stack_t::set_one(const wcstring &key, env_mode_flags_t mode, wcstring val) {
|
2017-08-28 09:51:34 +00:00
|
|
|
wcstring_list_t vals;
|
|
|
|
vals.push_back(std::move(val));
|
2018-09-09 19:17:31 +00:00
|
|
|
return set_internal(key, mode, std::move(vals));
|
2017-08-19 22:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the variable with the specified name without any (i.e., zero) values.
|
2018-09-09 19:17:31 +00:00
|
|
|
int env_stack_t::set_empty(const wcstring &key, env_mode_flags_t mode) {
|
|
|
|
return set_internal(key, mode, {});
|
2017-08-19 22:45:46 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Attempt to remove/free the specified key/value pair from the specified map.
|
|
|
|
///
|
|
|
|
/// \return zero if the variable was not found, non-zero otherwise
|
2018-09-10 08:17:37 +00:00
|
|
|
bool env_stack_t::try_remove(env_node_ref_t n, const wchar_t *key, int var_mode) {
|
|
|
|
if (n == nullptr) {
|
2013-01-19 21:16:21 +00:00
|
|
|
return false;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
var_table_t::iterator result = n->env.find(key);
|
2016-04-29 01:26:46 +00:00
|
|
|
if (result != n->env.end()) {
|
2018-01-30 20:36:50 +00:00
|
|
|
if (result->second.exports()) {
|
2018-09-09 19:17:31 +00:00
|
|
|
mark_changed_exported();
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
n->env.erase(result);
|
2013-01-19 21:16:21 +00:00
|
|
|
return true;
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
if (var_mode & ENV_LOCAL) {
|
2013-01-19 21:16:21 +00:00
|
|
|
return false;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
if (n->new_scope) {
|
2017-01-26 19:06:03 +00:00
|
|
|
return try_remove(vars_stack().global_env, key, var_mode);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2018-09-10 08:17:37 +00:00
|
|
|
return try_remove(n->next, key, var_mode);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 19:17:31 +00:00
|
|
|
int env_stack_t::remove(const wcstring &key, int var_mode) {
|
2012-03-05 22:18:16 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2018-09-10 08:17:37 +00:00
|
|
|
env_node_ref_t first_node{};
|
2012-11-19 00:30:30 +00:00
|
|
|
int erased = 0;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
if ((var_mode & ENV_USER) && is_read_only(key)) {
|
2018-04-01 02:44:54 +00:00
|
|
|
return ENV_SCOPE;
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 08:17:37 +00:00
|
|
|
first_node = vars_stack().top;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
if (!(var_mode & ENV_UNIVERSAL)) {
|
|
|
|
if (var_mode & ENV_GLOBAL) {
|
2017-01-26 19:06:03 +00:00
|
|
|
first_node = vars_stack().global_env;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
if (try_remove(first_node, key.c_str(), var_mode)) {
|
2012-11-19 00:30:30 +00:00
|
|
|
event_t ev = event_t::variable_event(key);
|
2012-12-20 09:52:44 +00:00
|
|
|
ev.arguments.push_back(L"VARIABLE");
|
|
|
|
ev.arguments.push_back(L"ERASE");
|
|
|
|
ev.arguments.push_back(key);
|
2012-11-19 00:30:30 +00:00
|
|
|
event_fire(&ev);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
erased = 1;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
if (!erased && !(var_mode & ENV_GLOBAL) && !(var_mode & ENV_LOCAL)) {
|
2018-10-22 07:49:09 +00:00
|
|
|
bool is_exported = false;
|
|
|
|
if (uvars()) {
|
|
|
|
if (auto old_flags = uvars()->get_flags(key)) {
|
|
|
|
is_exported = *old_flags & env_var_t::flag_export;
|
|
|
|
}
|
|
|
|
erased = uvars()->remove(key);
|
|
|
|
}
|
2016-04-29 01:26:46 +00:00
|
|
|
if (erased) {
|
2014-07-07 01:04:30 +00:00
|
|
|
env_universal_barrier();
|
2015-02-04 00:13:02 +00:00
|
|
|
event_t ev = event_t::variable_event(key);
|
|
|
|
ev.arguments.push_back(L"VARIABLE");
|
|
|
|
ev.arguments.push_back(L"ERASE");
|
|
|
|
ev.arguments.push_back(key);
|
|
|
|
event_fire(&ev);
|
2014-07-07 01:04:30 +00:00
|
|
|
}
|
2016-04-29 01:26:46 +00:00
|
|
|
|
2017-01-26 20:03:14 +00:00
|
|
|
if (is_exported) vars_stack().mark_changed_exported();
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2006-01-08 23:00:49 +00:00
|
|
|
|
2018-09-14 07:36:26 +00:00
|
|
|
react_to_variable_change(L"ERASE", key, *this);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2018-04-01 03:12:52 +00:00
|
|
|
return erased ? ENV_OK : ENV_NOT_FOUND;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2017-08-28 09:51:34 +00:00
|
|
|
const wcstring_list_t &env_var_t::as_list() const { return vals; }
|
2017-08-05 22:08:39 +00:00
|
|
|
|
2018-09-30 23:34:01 +00:00
|
|
|
wchar_t env_var_t::get_delimiter() const {
|
|
|
|
return is_pathvar() ? PATH_ARRAY_SEP : NONPATH_ARRAY_SEP;
|
|
|
|
}
|
|
|
|
|
2018-09-10 01:32:15 +00:00
|
|
|
/// Return a string representation of the var.
|
2018-02-19 02:33:04 +00:00
|
|
|
wcstring env_var_t::as_string() const {
|
2018-09-30 23:34:01 +00:00
|
|
|
return join_strings(vals, get_delimiter());
|
2017-08-06 01:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void env_var_t::to_list(wcstring_list_t &out) const {
|
2017-08-05 22:08:39 +00:00
|
|
|
out = vals;
|
2012-01-14 09:06:47 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 20:36:50 +00:00
|
|
|
env_var_t::env_var_flags_t env_var_t::flags_for(const wchar_t *name) {
|
|
|
|
env_var_flags_t result = 0;
|
2018-01-30 21:22:36 +00:00
|
|
|
if (is_read_only(name)) result |= flag_read_only;
|
2018-01-30 20:36:50 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-09-09 19:17:31 +00:00
|
|
|
maybe_t<env_var_t> env_stack_t::get(const wcstring &key, env_mode_flags_t mode) const {
|
2014-07-12 21:40:46 +00:00
|
|
|
const bool has_scope = mode & (ENV_LOCAL | ENV_GLOBAL | ENV_UNIVERSAL);
|
|
|
|
const bool search_local = !has_scope || (mode & ENV_LOCAL);
|
|
|
|
const bool search_global = !has_scope || (mode & ENV_GLOBAL);
|
|
|
|
const bool search_universal = !has_scope || (mode & ENV_UNIVERSAL);
|
|
|
|
|
|
|
|
const bool search_exported = (mode & ENV_EXPORT) || !(mode & ENV_UNEXPORT);
|
|
|
|
const bool search_unexported = (mode & ENV_UNEXPORT) || !(mode & ENV_EXPORT);
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
// Make the assumption that electric keys can't be shadowed elsewhere, since we currently block
|
2018-09-25 03:59:55 +00:00
|
|
|
// that in env_stack_t::set().
|
2016-04-29 01:26:46 +00:00
|
|
|
if (is_electric(key)) {
|
2017-08-28 07:25:41 +00:00
|
|
|
if (!search_global) return none();
|
2016-11-27 00:35:48 +00:00
|
|
|
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()) {
|
2017-08-28 07:25:41 +00:00
|
|
|
return none();
|
2016-11-27 00:35:48 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2014-07-12 21:40:46 +00:00
|
|
|
history_t *history = reader_get_history();
|
2016-04-29 01:26:46 +00:00
|
|
|
if (!history) {
|
2018-09-18 16:18:47 +00:00
|
|
|
history = &history_t::history_with_name(history_session_id(*this));
|
2014-07-12 21:40:46 +00:00
|
|
|
}
|
2017-08-25 01:59:50 +00:00
|
|
|
wcstring_list_t result;
|
|
|
|
if (history) history->get_history(result);
|
2017-08-05 22:08:39 +00:00
|
|
|
return env_var_t(L"history", result);
|
2016-04-29 01:26:46 +00:00
|
|
|
} else if (key == L"status") {
|
2017-08-05 22:08:39 +00:00
|
|
|
return env_var_t(L"status", to_string(proc_get_last_status()));
|
2016-04-29 01:26:46 +00:00
|
|
|
} else if (key == L"umask") {
|
2017-08-05 22:08:39 +00:00
|
|
|
return env_var_t(L"umask", format_string(L"0%0.3o", get_umask()));
|
2014-07-12 21:40:46 +00:00
|
|
|
}
|
2016-11-27 00:35:48 +00:00
|
|
|
// We should never get here unless the electric var list is out of sync with the above code.
|
|
|
|
DIE("unerecognized electric var name");
|
2014-07-12 21:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (search_local || search_global) {
|
2017-08-05 22:08:39 +00:00
|
|
|
scoped_lock locker(env_lock); // lock around a local region
|
2018-09-10 08:17:37 +00:00
|
|
|
env_node_ref_t env = search_local ? vars_stack().top : vars_stack().global_env;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
while (env != NULL) {
|
2017-08-05 22:08:39 +00:00
|
|
|
if (env == vars_stack().global_env && !search_global) {
|
|
|
|
break;
|
2014-07-12 21:40:46 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2017-08-05 22:08:39 +00:00
|
|
|
var_table_t::const_iterator result = env->env.find(key);
|
|
|
|
if (result != env->env.end()) {
|
2017-12-23 01:31:25 +00:00
|
|
|
const env_var_t &var = result->second;
|
2018-01-30 20:36:50 +00:00
|
|
|
if (var.exports() ? search_exported : search_unexported) {
|
2017-08-05 22:08:39 +00:00
|
|
|
return var;
|
|
|
|
}
|
2012-03-31 22:33:34 +00:00
|
|
|
}
|
2017-08-05 22:08:39 +00:00
|
|
|
env = vars_stack().next_scope_to_search(env);
|
2012-03-31 22:33:34 +00:00
|
|
|
}
|
2014-07-12 21:40:46 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2017-08-28 07:25:41 +00:00
|
|
|
if (!search_universal) return none();
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
// Another hack. Only do a universal barrier on the main thread (since it can change variable
|
2018-09-25 04:17:05 +00:00
|
|
|
// values). Make sure we do this outside the env_lock because it may itself call `get()`.
|
2016-04-29 01:26:46 +00:00
|
|
|
if (is_main_thread() && !get_proc_had_barrier()) {
|
2014-07-12 21:40:46 +00:00
|
|
|
set_proc_had_barrier(true);
|
|
|
|
env_universal_barrier();
|
|
|
|
}
|
|
|
|
|
2017-08-11 04:11:47 +00:00
|
|
|
// Okay, we couldn't find a local or global var given the requirements. If there is a matching
|
|
|
|
// universal var return that.
|
2016-04-29 01:26:46 +00:00
|
|
|
if (uvars()) {
|
2017-08-28 07:25:41 +00:00
|
|
|
auto var = uvars()->get(key);
|
2018-10-22 07:49:09 +00:00
|
|
|
if (var && (var->exports() ? search_exported : search_unexported)) {
|
2017-08-05 22:08:39 +00:00
|
|
|
return var;
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-11 04:11:47 +00:00
|
|
|
|
2017-08-28 07:25:41 +00:00
|
|
|
return none();
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2018-09-09 19:17:31 +00:00
|
|
|
void env_universal_barrier() { env_stack_t::principal().universal_barrier(); }
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Returns true if the specified scope or any non-shadowed non-global subscopes contain an exported
|
|
|
|
/// variable.
|
2018-09-10 08:17:37 +00:00
|
|
|
bool var_stack_t::local_scope_exports(const env_node_ref_t &n) const {
|
|
|
|
assert(n != nullptr);
|
2018-09-09 19:17:31 +00:00
|
|
|
if (n == global_env) return false;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2017-01-26 20:51:11 +00:00
|
|
|
if (n->exportv) return true;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2017-01-26 20:51:11 +00:00
|
|
|
if (n->new_scope) return false;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2018-09-10 08:17:37 +00:00
|
|
|
return local_scope_exports(n->next);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 19:17:31 +00:00
|
|
|
void env_stack_t::push(bool new_scope) { vars_stack().push(new_scope); }
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2018-09-09 19:17:31 +00:00
|
|
|
void env_stack_t::pop() { vars_stack().pop(); }
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Function used with to insert keys of one table into a set::set<wcstring>.
|
|
|
|
static void add_key_to_string_set(const var_table_t &envs, std::set<wcstring> *str_set,
|
|
|
|
bool show_exported, bool show_unexported) {
|
2012-11-19 00:30:30 +00:00
|
|
|
var_table_t::const_iterator iter;
|
2016-04-29 01:26:46 +00:00
|
|
|
for (iter = envs.begin(); iter != envs.end(); ++iter) {
|
2017-08-09 18:11:58 +00:00
|
|
|
const env_var_t &var = iter->second;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2018-01-30 20:36:50 +00:00
|
|
|
if ((var.exports() && show_exported) || (!var.exports() && show_unexported)) {
|
2016-04-29 01:26:46 +00:00
|
|
|
// Insert this key.
|
2013-01-19 21:16:21 +00:00
|
|
|
str_set->insert(iter->first);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-17 18:52:30 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 16:58:02 +00:00
|
|
|
wcstring_list_t env_stack_t::get_names(int flags) const {
|
2016-07-21 05:30:58 +00:00
|
|
|
scoped_lock locker(env_lock);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-01-10 20:51:09 +00:00
|
|
|
wcstring_list_t result;
|
|
|
|
std::set<wcstring> names;
|
|
|
|
int show_local = flags & ENV_LOCAL;
|
2012-11-19 00:30:30 +00:00
|
|
|
int show_global = flags & ENV_GLOBAL;
|
|
|
|
int show_universal = flags & ENV_UNIVERSAL;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2018-09-10 08:17:37 +00:00
|
|
|
env_node_ref_t n = vars_stack().top;
|
2013-01-19 21:16:21 +00:00
|
|
|
const bool show_exported = (flags & ENV_EXPORT) || !(flags & ENV_UNEXPORT);
|
|
|
|
const bool show_unexported = (flags & ENV_UNEXPORT) || !(flags & ENV_EXPORT);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
if (!show_local && !show_global && !show_universal) {
|
|
|
|
show_local = show_universal = show_global = 1;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
if (show_local) {
|
|
|
|
while (n) {
|
2017-01-26 19:06:03 +00:00
|
|
|
if (n == vars_stack().global_env) break;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2013-01-19 21:16:21 +00:00
|
|
|
add_key_to_string_set(n->env, &names, show_exported, show_unexported);
|
2012-11-19 00:30:30 +00:00
|
|
|
if (n->new_scope)
|
|
|
|
break;
|
|
|
|
else
|
2018-09-10 08:17:37 +00:00
|
|
|
n = n->next;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
if (show_global) {
|
2017-01-26 19:06:03 +00:00
|
|
|
add_key_to_string_set(vars_stack().global_env->env, &names, show_exported, show_unexported);
|
2016-04-29 01:26:46 +00:00
|
|
|
if (show_unexported) {
|
2018-09-29 04:58:44 +00:00
|
|
|
result.insert(result.end(), std::begin(env_electric), std::end(env_electric));
|
2012-01-10 20:51:09 +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
|
|
|
if (show_universal && uvars()) {
|
2014-06-16 00:30:50 +00:00
|
|
|
const wcstring_list_t uni_list = uvars()->get_names(show_exported, show_unexported);
|
2012-01-10 20:51:09 +00:00
|
|
|
names.insert(uni_list.begin(), uni_list.end());
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-01-10 20:51:09 +00:00
|
|
|
result.insert(result.end(), names.begin(), names.end());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Get list of all exported variables.
|
2018-09-09 19:17:31 +00:00
|
|
|
void var_stack_t::get_exported(const env_node_t *n, var_table_t &h) const {
|
2016-04-29 01:26:46 +00:00
|
|
|
if (!n) return;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2017-08-09 18:11:58 +00:00
|
|
|
if (n->new_scope) {
|
2018-09-10 08:17:37 +00:00
|
|
|
get_exported(global_env.get(), h);
|
2017-08-09 18:11:58 +00:00
|
|
|
} else {
|
2017-01-26 20:40:55 +00:00
|
|
|
get_exported(n->next.get(), h);
|
2017-08-09 18:11:58 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
var_table_t::const_iterator iter;
|
2016-04-29 01:26:46 +00:00
|
|
|
for (iter = n->env.begin(); iter != n->env.end(); ++iter) {
|
2012-11-19 00:30:30 +00:00
|
|
|
const wcstring &key = iter->first;
|
2017-08-09 18:11:58 +00:00
|
|
|
const env_var_t var = iter->second;
|
2015-06-12 23:05:59 +00:00
|
|
|
|
2018-01-30 20:36:50 +00:00
|
|
|
if (var.exports()) {
|
2016-04-29 01:26:46 +00:00
|
|
|
// Export the variable. Don't use std::map::insert here, since we need to overwrite
|
|
|
|
// existing values from previous scopes.
|
2017-08-09 18:11:58 +00:00
|
|
|
h[key] = var;
|
2016-04-29 01:26:46 +00:00
|
|
|
} else {
|
|
|
|
// We need to erase from the map if we are not exporting, since a lower scope may have
|
|
|
|
// exported. See #2132.
|
2017-08-09 18:11:58 +00:00
|
|
|
h.erase(key);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2012-02-17 18:52:30 +00:00
|
|
|
}
|
2006-11-26 13:09:43 +00:00
|
|
|
|
2018-05-27 22:45:30 +00:00
|
|
|
// Given a map from key to value, return a vector of strings of the form key=value
|
|
|
|
static std::vector<std::string> get_export_list(const var_table_t &envs) {
|
|
|
|
std::vector<std::string> result;
|
|
|
|
result.reserve(envs.size());
|
|
|
|
for (const auto &kv : envs) {
|
|
|
|
std::string ks = wcs2string(kv.first);
|
|
|
|
std::string vs = wcs2string(kv.second.as_string());
|
2018-03-25 06:32:55 +00:00
|
|
|
// Create and append a string of the form ks=vs
|
|
|
|
std::string str;
|
2013-02-12 08:14:27 +00:00
|
|
|
str.reserve(ks.size() + 1 + vs.size());
|
2012-02-28 23:11:46 +00:00
|
|
|
str.append(ks);
|
|
|
|
str.append("=");
|
|
|
|
str.append(vs);
|
2018-05-27 22:45:30 +00:00
|
|
|
result.push_back(std::move(str));
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2018-05-27 22:45:30 +00:00
|
|
|
return result;
|
2005-09-22 20:16:52 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 20:03:14 +00:00
|
|
|
void var_stack_t::update_export_array_if_necessary() {
|
2018-09-14 07:36:26 +00:00
|
|
|
if (!this->has_changed_exported()) {
|
2017-01-26 20:03:14 +00:00
|
|
|
return;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 08:26:30 +00:00
|
|
|
debug(4, L"export_arr() recalc");
|
2017-08-09 18:11:58 +00:00
|
|
|
var_table_t vals;
|
|
|
|
get_exported(this->top.get(), vals);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2017-01-26 20:03:14 +00:00
|
|
|
if (uvars()) {
|
|
|
|
const wcstring_list_t uni = uvars()->get_names(true, false);
|
2018-05-27 22:45:30 +00:00
|
|
|
for (const wcstring &key : uni) {
|
2017-08-28 07:25:41 +00:00
|
|
|
auto var = uvars()->get(key);
|
2017-01-26 20:03:14 +00:00
|
|
|
|
2017-08-28 07:25:41 +00:00
|
|
|
if (!var.missing_or_empty()) {
|
2017-01-26 20:03:14 +00:00
|
|
|
// Note that std::map::insert does NOT overwrite a value already in the map,
|
|
|
|
// which we depend on here.
|
2017-08-28 07:25:41 +00:00
|
|
|
vals.insert(std::pair<wcstring, env_var_t>(key, *var));
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-26 20:03:14 +00:00
|
|
|
|
2018-09-14 07:36:26 +00:00
|
|
|
export_array.emplace(get_export_list(vals));
|
2012-02-28 23:11:46 +00:00
|
|
|
}
|
2005-09-22 20:16:52 +00:00
|
|
|
|
2018-09-09 19:17:31 +00:00
|
|
|
const char *const *env_stack_t::export_arr() {
|
2012-02-28 23:11:46 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2017-01-26 20:03:14 +00:00
|
|
|
ASSERT_IS_NOT_FORKED_CHILD();
|
|
|
|
vars_stack().update_export_array_if_necessary();
|
2018-09-14 07:36:26 +00:00
|
|
|
assert(vars_stack().export_array && "Should have export array");
|
|
|
|
return vars_stack().export_array->get();
|
2012-02-28 23:11:46 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 19:17:31 +00:00
|
|
|
void env_stack_t::set_argv(const wchar_t *const *argv) {
|
2017-08-05 22:08:39 +00:00
|
|
|
if (argv && *argv) {
|
|
|
|
wcstring_list_t list;
|
|
|
|
for (auto arg = argv; *arg; arg++) {
|
2018-09-11 04:27:25 +00:00
|
|
|
list.emplace_back(*arg);
|
2015-08-15 20:37:17 +00:00
|
|
|
}
|
2018-09-11 04:27:25 +00:00
|
|
|
set(L"argv", ENV_LOCAL, std::move(list));
|
2016-04-29 01:26:46 +00:00
|
|
|
} else {
|
2018-09-11 04:27:25 +00:00
|
|
|
set_empty(L"argv", ENV_LOCAL);
|
2015-08-15 20:37:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-09 09:25:51 +00:00
|
|
|
environment_t::~environment_t() = default;
|
2018-09-09 19:17:31 +00:00
|
|
|
env_stack_t::~env_stack_t() = default;
|
2018-09-14 07:36:26 +00:00
|
|
|
env_stack_t::env_stack_t(env_stack_t &&) = default;
|
2018-09-09 19:17:31 +00:00
|
|
|
|
2018-09-11 05:29:52 +00:00
|
|
|
null_environment_t::null_environment_t() = default;
|
|
|
|
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 {
|
2019-01-13 23:03:19 +00:00
|
|
|
UNUSED(key);
|
|
|
|
UNUSED(mode);
|
2018-09-11 05:29:52 +00:00
|
|
|
return none();
|
|
|
|
}
|
2019-01-13 23:03:19 +00:00
|
|
|
wcstring_list_t null_environment_t::get_names(int flags) const {
|
|
|
|
UNUSED(flags);
|
|
|
|
return {};
|
|
|
|
}
|
2018-09-11 05:29:52 +00:00
|
|
|
|
2018-09-14 07:36:26 +00:00
|
|
|
env_stack_t env_stack_t::make_principal() {
|
|
|
|
const env_stack_t &gl = env_stack_t::globals();
|
|
|
|
std::unique_ptr<var_stack_t> dup_stack = make_unique<var_stack_t>(gl.vars_stack().clone());
|
|
|
|
return env_stack_t{std::move(dup_stack)};
|
|
|
|
}
|
|
|
|
|
2018-09-09 19:17:31 +00:00
|
|
|
env_stack_t &env_stack_t::principal() {
|
2018-09-14 07:36:26 +00:00
|
|
|
static env_stack_t s_principal = make_principal();
|
2018-09-09 19:17:31 +00:00
|
|
|
return s_principal;
|
2018-09-14 07:36:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
env_stack_t &env_stack_t::globals() {
|
|
|
|
static env_stack_t s_global;
|
|
|
|
return s_global;
|
2018-09-09 19:17:31 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 16:58:02 +00:00
|
|
|
env_vars_snapshot_t::env_vars_snapshot_t(const environment_t &source, const wchar_t *const *keys) {
|
2011-12-27 03:18:46 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2012-07-21 05:11:05 +00:00
|
|
|
wcstring key;
|
2016-04-29 01:26:46 +00:00
|
|
|
for (size_t i = 0; keys[i]; i++) {
|
2012-07-21 05:11:05 +00:00
|
|
|
key.assign(keys[i]);
|
2018-09-10 16:58:02 +00:00
|
|
|
const auto var = source.get(key);
|
2017-08-28 07:25:41 +00:00
|
|
|
if (var) {
|
2018-09-09 09:25:51 +00:00
|
|
|
vars[key] = std::move(*var);
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-10 16:58:02 +00:00
|
|
|
names = source.get_names(0);
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 09:25:51 +00:00
|
|
|
env_vars_snapshot_t::~env_vars_snapshot_t() = default;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2018-09-09 09:25:51 +00:00
|
|
|
maybe_t<env_var_t> env_vars_snapshot_t::get(const wcstring &key, env_mode_flags_t mode) const {
|
2019-01-13 23:03:19 +00:00
|
|
|
UNUSED(mode);
|
2017-08-19 16:55:06 +00:00
|
|
|
auto iter = vars.find(key);
|
2017-08-28 07:25:41 +00:00
|
|
|
if (iter == vars.end()) return none();
|
2018-01-30 20:36:50 +00:00
|
|
|
return iter->second;
|
2012-07-21 05:11:05 +00:00
|
|
|
}
|
2012-05-06 21:53:19 +00:00
|
|
|
|
2019-01-13 23:03:19 +00:00
|
|
|
wcstring_list_t env_vars_snapshot_t::get_names(int flags) const {
|
|
|
|
UNUSED(flags);
|
|
|
|
return names;
|
|
|
|
}
|
2018-09-10 16:58:02 +00:00
|
|
|
|
2019-01-11 23:04:09 +00:00
|
|
|
const wchar_t *const env_vars_snapshot_t::highlighting_keys[] = {
|
2019-01-11 23:12:17 +00:00
|
|
|
L"PATH", L"CDPATH", L"fish_function_path", L"PWD", L"HOME", NULL};
|
2018-09-10 16:58:02 +00:00
|
|
|
|
2019-01-11 23:12:17 +00:00
|
|
|
const wchar_t *const env_vars_snapshot_t::completing_keys[] = {
|
|
|
|
L"PATH", L"CDPATH", L"fish_function_path", L"PWD", L"HOME", NULL};
|
2018-09-28 15:14:27 +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.
|
|
|
|
int mode = R_OK | W_OK | X_OK;
|
|
|
|
if (dir != NULL && access(dir, mode) == 0 && check_runtime_path(dir) == 0) {
|
|
|
|
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());
|
|
|
|
const char *uname = pwuid ? pwuid->pw_name : NULL;
|
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) {
|
2018-09-28 15:14:27 +00:00
|
|
|
debug(0, L"Runtime path not available.");
|
|
|
|
debug(0, L"Try deleting the directory %s and restarting fish.", tmpdir.c_str());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = str2wcstring(tmpdir);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|