2016-04-29 01:26:46 +00:00
|
|
|
// Prototypes for functions for setting and getting environment variables.
|
2005-10-04 15:11:39 +00:00
|
|
|
#ifndef FISH_ENV_H
|
|
|
|
#define FISH_ENV_H
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
#include <stddef.h>
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <stdint.h>
|
2017-02-14 04:37:27 +00:00
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
#include <map>
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <memory>
|
2016-04-29 01:26:46 +00:00
|
|
|
#include <string>
|
2005-10-04 15:11:39 +00:00
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
#include "common.h"
|
2005-10-04 15:11:39 +00:00
|
|
|
|
2017-02-08 01:21:35 +00:00
|
|
|
extern size_t read_byte_limit;
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
// Flags that may be passed as the 'mode' in env_set / env_get_string.
|
|
|
|
enum {
|
|
|
|
/// Default mode.
|
2014-07-14 00:30:48 +00:00
|
|
|
ENV_DEFAULT = 0,
|
2016-04-29 01:26:46 +00:00
|
|
|
|
|
|
|
/// Flag for local (to the current block) variable.
|
2014-07-14 00:30:48 +00:00
|
|
|
ENV_LOCAL = 1,
|
2016-04-29 01:26:46 +00:00
|
|
|
|
|
|
|
/// Flag for exported (to commands) variable.
|
2014-07-14 00:30:48 +00:00
|
|
|
ENV_EXPORT = 2,
|
2016-04-29 01:26:46 +00:00
|
|
|
|
|
|
|
/// Flag for unexported variable.
|
2014-07-14 00:30:48 +00:00
|
|
|
ENV_UNEXPORT = 16,
|
2016-04-29 01:26:46 +00:00
|
|
|
|
|
|
|
/// Flag for global variable.
|
2014-07-14 00:30:48 +00:00
|
|
|
ENV_GLOBAL = 4,
|
2016-04-29 01:26:46 +00:00
|
|
|
|
|
|
|
/// Flag for variable update request from the user. All variable changes that are made directly
|
|
|
|
/// by the user, such as those from the 'set' builtin must have this flag set.
|
2014-07-14 00:30:48 +00:00
|
|
|
ENV_USER = 8,
|
2016-04-29 01:26:46 +00:00
|
|
|
|
|
|
|
/// Flag for universal variable.
|
2014-07-14 00:30:48 +00:00
|
|
|
ENV_UNIVERSAL = 32
|
|
|
|
};
|
|
|
|
typedef uint32_t env_mode_flags_t;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-10-30 00:25:48 +00:00
|
|
|
/// Return values for `env_set()`.
|
|
|
|
enum { ENV_OK, ENV_PERM, ENV_SCOPE, ENV_INVALID };
|
2016-04-29 01:26:46 +00:00
|
|
|
|
|
|
|
/// A struct of configuration directories, determined in main() that fish will optionally pass to
|
|
|
|
/// env_init.
|
|
|
|
struct config_paths_t {
|
|
|
|
wcstring data; // e.g. /usr/local/share
|
|
|
|
wcstring sysconf; // e.g. /usr/local/etc
|
|
|
|
wcstring doc; // e.g. /usr/local/share/doc/fish
|
|
|
|
wcstring bin; // e.g. /usr/local/bin
|
2012-07-18 17:50:38 +00:00
|
|
|
};
|
2006-04-10 15:36:26 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Initialize environment variable data.
|
2012-07-18 17:50:38 +00:00
|
|
|
void env_init(const struct config_paths_t *paths = NULL);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2014-07-14 00:30:48 +00:00
|
|
|
int env_set(const wcstring &key, const wchar_t *val, env_mode_flags_t mode);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
class env_var_t : public wcstring {
|
|
|
|
private:
|
2012-01-14 09:06:47 +00:00
|
|
|
bool is_missing;
|
2016-04-29 01:26:46 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
static env_var_t missing_var() {
|
2016-02-28 03:38:15 +00:00
|
|
|
env_var_t result((wcstring()));
|
2013-02-12 07:16:50 +00:00
|
|
|
result.is_missing = true;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
env_var_t(const env_var_t &x) : wcstring(x), is_missing(x.is_missing) {}
|
|
|
|
env_var_t(const wcstring &x) : wcstring(x), is_missing(false) {}
|
|
|
|
env_var_t(const wchar_t *x) : wcstring(x), is_missing(false) {}
|
|
|
|
env_var_t() : wcstring(L""), is_missing(false) {}
|
2013-02-12 07:16:50 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
bool missing(void) const { return is_missing; }
|
2013-02-12 07:16:50 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
bool missing_or_empty(void) const { return missing() || empty(); }
|
2013-02-12 07:16:50 +00:00
|
|
|
|
2012-01-14 09:06:47 +00:00
|
|
|
const wchar_t *c_str(void) const;
|
2013-02-12 07:16:50 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
env_var_t &operator=(const env_var_t &s) {
|
2012-01-14 10:42:17 +00:00
|
|
|
is_missing = s.is_missing;
|
|
|
|
wcstring::operator=(s);
|
|
|
|
return *this;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
bool operator==(const env_var_t &s) const {
|
|
|
|
return is_missing == s.is_missing &&
|
|
|
|
static_cast<const wcstring &>(*this) == static_cast<const wcstring &>(s);
|
2013-02-12 07:16:50 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
bool operator==(const wcstring &s) const {
|
|
|
|
return !is_missing && static_cast<const wcstring &>(*this) == s;
|
2013-02-12 07:16:50 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
bool operator!=(const env_var_t &s) const { return !(*this == s); }
|
2013-02-12 07:16:50 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
bool operator!=(const wcstring &s) const { return !(*this == s); }
|
2013-02-12 07:16:50 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
bool operator==(const wchar_t *s) const {
|
|
|
|
return !is_missing && static_cast<const wcstring &>(*this) == s;
|
2013-02-12 07:16:50 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
bool operator!=(const wchar_t *s) const { return !(*this == s); }
|
2012-01-14 09:06:47 +00:00
|
|
|
};
|
2013-02-20 01:48:51 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Gets the variable with the specified name, or env_var_t::missing_var if it does not exist or is
|
|
|
|
/// an empty array.
|
|
|
|
///
|
|
|
|
/// \param key The name of the variable to get
|
|
|
|
/// \param mode An optional scope to search in. All scopes are searched if unset
|
2014-07-14 00:30:48 +00:00
|
|
|
env_var_t env_get_string(const wcstring &key, env_mode_flags_t mode = ENV_DEFAULT);
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Returns true if the specified key exists. This can't be reliably done using env_get, since
|
|
|
|
/// env_get returns null for 0-element arrays.
|
|
|
|
///
|
|
|
|
/// \param key The name of the variable to remove
|
|
|
|
/// \param mode the scope to search in. All scopes are searched if set to default
|
2014-07-14 00:30:48 +00:00
|
|
|
bool env_exist(const wchar_t *key, env_mode_flags_t mode);
|
2005-09-26 14:47:03 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Remove environment variable.
|
|
|
|
///
|
|
|
|
/// \param key The name of the variable to remove
|
|
|
|
/// \param mode should be ENV_USER if this is a remove request from the user, 0 otherwise. If this
|
|
|
|
/// is a user request, read-only variables can not be removed. The mode may also specify the scope
|
|
|
|
/// of the variable that should be erased.
|
|
|
|
///
|
|
|
|
/// \return zero if the variable existed, and non-zero if the variable did not exist
|
2012-11-19 00:30:30 +00:00
|
|
|
int env_remove(const wcstring &key, int mode);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Push the variable stack. Used for implementing local variables for functions and for-loops.
|
2013-01-19 21:21:55 +00:00
|
|
|
void env_push(bool new_scope);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Pop the variable stack. Used for implementing local variables for functions and for-loops.
|
2005-09-20 13:26:39 +00:00
|
|
|
void env_pop();
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Synchronizes all universal variable changes: writes everything out, reads stuff in.
|
2014-06-16 00:30:50 +00:00
|
|
|
void env_universal_barrier();
|
|
|
|
|
2017-01-26 20:03:14 +00:00
|
|
|
/// Returns an array containing all exported variables in a format suitable for execv
|
|
|
|
const char *const *env_export_arr();
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Sets up argv as the given null terminated array of strings.
|
|
|
|
void env_set_argv(const wchar_t *const *argv);
|
2015-08-15 20:37:17 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Returns all variable names.
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring_list_t env_get_names(int flags);
|
2005-10-04 15:11:39 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Update the PWD variable directory.
|
2017-01-18 21:54:54 +00:00
|
|
|
bool env_set_pwd();
|
2008-01-16 22:07:38 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// Returns the PWD with a terminating slash.
|
2013-04-27 07:45:38 +00:00
|
|
|
wcstring env_get_pwd_slash();
|
|
|
|
|
2017-02-08 01:21:35 +00:00
|
|
|
/// Update the read_byte_limit variable.
|
|
|
|
void env_set_read_limit();
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
class env_vars_snapshot_t {
|
2011-12-27 03:18:46 +00:00
|
|
|
std::map<wcstring, wcstring> vars;
|
2012-07-21 05:11:05 +00:00
|
|
|
bool is_current() const;
|
2016-04-29 01:26:46 +00:00
|
|
|
|
2017-01-27 04:00:43 +00:00
|
|
|
public:
|
2017-01-24 16:23:24 +00:00
|
|
|
env_vars_snapshot_t(const env_vars_snapshot_t &) = default;
|
|
|
|
env_vars_snapshot_t &operator=(const env_vars_snapshot_t &) = default;
|
2016-04-29 01:26:46 +00:00
|
|
|
|
|
|
|
env_vars_snapshot_t(const wchar_t *const *keys);
|
2016-02-08 09:29:23 +00:00
|
|
|
env_vars_snapshot_t();
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-07-21 03:39:31 +00:00
|
|
|
env_var_t get(const wcstring &key) const;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
// Returns the fake snapshot representing the live variables array.
|
2012-07-21 05:11:05 +00:00
|
|
|
static const env_vars_snapshot_t ¤t();
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
// Vars necessary for highlighting.
|
|
|
|
static const wchar_t *const highlighting_keys[];
|
|
|
|
|
|
|
|
// Vars necessary for completion.
|
|
|
|
static const wchar_t *const completing_keys[];
|
2011-12-27 03:18:46 +00:00
|
|
|
};
|
|
|
|
|
2012-04-22 03:08:08 +00:00
|
|
|
extern int g_fork_count;
|
2012-08-15 07:57:56 +00:00
|
|
|
extern bool g_use_posix_spawn;
|
|
|
|
|
2016-04-29 01:26:46 +00:00
|
|
|
/// A variable entry. Stores the value of a variable and whether it should be exported.
|
|
|
|
struct var_entry_t {
|
|
|
|
wcstring val; // the value of the variable
|
|
|
|
bool exportv; // whether the variable should be exported
|
|
|
|
|
|
|
|
var_entry_t() : exportv(false) {}
|
2014-04-25 23:09:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::map<wcstring, var_entry_t> var_table_t;
|
2016-12-23 21:08:45 +00:00
|
|
|
|
|
|
|
extern bool term_has_xn; // does the terminal have the "eat_newline_glitch"
|
2017-01-10 02:35:37 +00:00
|
|
|
|
|
|
|
/// Returns true if we think the terminal supports setting its title.
|
|
|
|
bool term_supports_setting_title();
|
2005-10-04 15:11:39 +00:00
|
|
|
#endif
|