2005-09-20 21:26:39 +08:00
|
|
|
/** \file env.h
|
2012-11-18 18:23:22 +08:00
|
|
|
Prototypes for functions for setting and getting environment variables.
|
2005-09-20 21:26:39 +08:00
|
|
|
*/
|
|
|
|
|
2005-10-04 23:11:39 +08:00
|
|
|
#ifndef FISH_ENV_H
|
|
|
|
#define FISH_ENV_H
|
|
|
|
|
|
|
|
#include <wchar.h>
|
2011-12-27 11:18:46 +08:00
|
|
|
#include <map>
|
2005-10-04 23:11:39 +08:00
|
|
|
|
|
|
|
#include "util.h"
|
2011-12-27 11:18:46 +08:00
|
|
|
#include "common.h"
|
2005-10-04 23:11:39 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/**
|
|
|
|
Flag for local (to the current block) variable
|
|
|
|
*/
|
|
|
|
#define ENV_LOCAL 1
|
|
|
|
|
|
|
|
/**
|
|
|
|
Flag for exported (to commands) variable
|
|
|
|
*/
|
|
|
|
#define ENV_EXPORT 2
|
|
|
|
|
|
|
|
/**
|
|
|
|
Flag for unexported variable
|
|
|
|
*/
|
|
|
|
#define ENV_UNEXPORT 16
|
|
|
|
|
|
|
|
/**
|
|
|
|
Flag for global variable
|
|
|
|
*/
|
|
|
|
#define ENV_GLOBAL 4
|
|
|
|
|
|
|
|
/**
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
#define ENV_USER 8
|
|
|
|
|
|
|
|
/**
|
|
|
|
Flag for universal variable
|
|
|
|
*/
|
|
|
|
#define ENV_UNIVERSAL 32
|
|
|
|
|
2006-04-10 23:36:26 +08:00
|
|
|
/**
|
|
|
|
Error code for trying to alter read-only variable
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
ENV_PERM = 1,
|
|
|
|
ENV_INVALID
|
2006-06-09 07:52:12 +08:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2012-07-19 01:50:38 +08: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
|
|
|
|
};
|
2006-04-10 23:36:26 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/**
|
|
|
|
Initialize environment variable data
|
|
|
|
*/
|
2012-07-19 01:50:38 +08:00
|
|
|
void env_init(const struct config_paths_t *paths = NULL);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Destroy environment variable data
|
|
|
|
*/
|
|
|
|
void env_destroy();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2012-11-18 18:23:22 +08:00
|
|
|
Set the value of the environment variable whose name matches key to val.
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
Memory policy: All keys and values are copied, the parameters can and should be freed by the caller afterwards
|
|
|
|
|
|
|
|
\param key The key
|
|
|
|
\param val The value
|
|
|
|
\param mode The type of the variable. Can be any combination of ENV_GLOBAL, ENV_LOCAL, ENV_EXPORT and ENV_USER. If mode is zero, 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.
|
|
|
|
|
2006-04-10 23:36:26 +08:00
|
|
|
\returns 0 on suicess or an error code on failiure.
|
|
|
|
|
|
|
|
The current error codes are:
|
|
|
|
|
|
|
|
* 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.
|
2006-06-09 07:52:12 +08:00
|
|
|
* ENV_INVALID, the variable name or mode was invalid
|
2005-09-20 21:26:39 +08:00
|
|
|
*/
|
|
|
|
|
2012-05-09 18:06:10 +08:00
|
|
|
int env_set(const wcstring &key, const wchar_t *val, int mode);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2005-10-14 19:40:33 +08:00
|
|
|
Return the value of the variable with the specified name. Returns 0
|
|
|
|
if the key does not exist. The returned string should not be
|
|
|
|
modified or freed. The returned string is only guaranteed to be
|
|
|
|
valid until the next call to env_get(), env_set(), env_push() or
|
|
|
|
env_pop() takes place.
|
2005-09-20 21:26:39 +08:00
|
|
|
*/
|
2012-02-26 10:54:49 +08:00
|
|
|
//const wchar_t *env_get( const wchar_t *key );
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
class env_var_t : public wcstring
|
|
|
|
{
|
2012-01-14 17:06:47 +08:00
|
|
|
private:
|
|
|
|
bool is_missing;
|
|
|
|
public:
|
2013-02-12 15:16:50 +08:00
|
|
|
static env_var_t missing_var(void)
|
|
|
|
{
|
|
|
|
env_var_t result(L"");
|
|
|
|
result.is_missing = true;
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-02-26 17:15:53 +08:00
|
|
|
env_var_t(const env_var_t &x) : wcstring(x), is_missing(x.is_missing) { }
|
2012-01-14 17:06:47 +08:00
|
|
|
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 15:16:50 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
bool missing(void) const
|
|
|
|
{
|
|
|
|
return is_missing;
|
|
|
|
}
|
2013-02-12 15:16:50 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
bool missing_or_empty(void) const
|
|
|
|
{
|
|
|
|
return missing() || empty();
|
|
|
|
}
|
2013-02-12 15:16:50 +08:00
|
|
|
|
2012-01-14 17:06:47 +08:00
|
|
|
const wchar_t *c_str(void) const;
|
2013-02-12 15:16:50 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
env_var_t &operator=(const env_var_t &s)
|
|
|
|
{
|
2012-01-14 18:42:17 +08:00
|
|
|
is_missing = s.is_missing;
|
|
|
|
wcstring::operator=(s);
|
|
|
|
return *this;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
bool operator==(const env_var_t &s) const
|
|
|
|
{
|
2013-02-12 15:16:50 +08:00
|
|
|
return is_missing == s.is_missing && static_cast<const wcstring &>(*this) == static_cast<const wcstring &>(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const wcstring &s) const
|
|
|
|
{
|
|
|
|
return ! is_missing && static_cast<const wcstring &>(*this) == s;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const env_var_t &s) const
|
|
|
|
{
|
|
|
|
return !(*this == s);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const wcstring &s) const
|
|
|
|
{
|
|
|
|
return !(*this == s);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const wchar_t *s) const
|
|
|
|
{
|
|
|
|
return ! is_missing && static_cast<const wcstring &>(*this) == s;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const wchar_t *s) const
|
|
|
|
{
|
|
|
|
return !(*this == s);
|
2012-02-26 17:15:53 +08:00
|
|
|
}
|
2012-01-14 18:42:17 +08:00
|
|
|
|
2013-02-12 15:16:50 +08:00
|
|
|
|
2012-01-14 17:06:47 +08:00
|
|
|
};
|
2011-12-27 11:18:46 +08:00
|
|
|
/**
|
|
|
|
Gets the variable with the specified name, or an empty string if it does not exist.
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
env_var_t env_get_string(const wcstring &key);
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-26 22:47:03 +08:00
|
|
|
/**
|
2013-01-20 05:16:21 +08:00
|
|
|
Returns true if the specified key exists. This can't be reliably done
|
2005-09-26 22:47:03 +08:00
|
|
|
using env_get, since env_get returns null for 0-element arrays
|
2006-06-05 04:14:51 +08:00
|
|
|
|
|
|
|
\param key The name of the variable to remove
|
|
|
|
\param mode the scope to search in. All scopes are searched if unset
|
2005-09-26 22:47:03 +08:00
|
|
|
*/
|
2013-01-20 05:16:21 +08:00
|
|
|
bool env_exist(const wchar_t *key, int mode);
|
2005-09-26 22:47:03 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/**
|
|
|
|
Remove environemnt variable
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
\param key The name of the variable to remove
|
2006-06-05 04:14:51 +08:00
|
|
|
\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
|
2005-09-20 21:26:39 +08:00
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
int env_remove(const wcstring &key, int mode);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Push the variable stack. Used for implementing local variables for functions and for-loops.
|
|
|
|
*/
|
2013-01-20 05:21:55 +08:00
|
|
|
void env_push(bool new_scope);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Pop the variable stack. Used for implementing local variables for functions and for-loops.
|
|
|
|
*/
|
|
|
|
void env_pop();
|
|
|
|
|
2012-02-29 07:11:46 +08:00
|
|
|
/** Returns an array containing all exported variables in a format suitable for execv. */
|
2012-11-19 08:30:30 +08:00
|
|
|
char **env_export_arr(bool recalc);
|
2012-02-29 07:11:46 +08:00
|
|
|
void env_export_arr(bool recalc, null_terminated_array_t<char> &result);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/**
|
2012-02-26 10:54:49 +08:00
|
|
|
Returns all variable names.
|
2005-09-20 21:26:39 +08:00
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring_list_t env_get_names(int flags);
|
2005-10-04 23:11:39 +08:00
|
|
|
|
2012-07-19 01:50:38 +08:00
|
|
|
|
|
|
|
|
2008-01-17 06:07:38 +08:00
|
|
|
/**
|
|
|
|
Update the PWD variable
|
|
|
|
directory
|
|
|
|
*/
|
|
|
|
int env_set_pwd();
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
class env_vars_snapshot_t
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
std::map<wcstring, wcstring> vars;
|
2012-07-21 13:11:05 +08:00
|
|
|
bool is_current() const;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
|
|
|
public:
|
2012-07-21 11:39:31 +08:00
|
|
|
env_vars_snapshot_t(const wchar_t * const * keys);
|
|
|
|
env_vars_snapshot_t(void);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-07-21 11:39:31 +08:00
|
|
|
env_var_t get(const wcstring &key) const;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-07-21 13:11:05 +08:00
|
|
|
// Returns the fake snapshot representing the live variables array
|
|
|
|
static const env_vars_snapshot_t ¤t();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2011-12-27 11:18:46 +08:00
|
|
|
// vars necessary for highlighting
|
|
|
|
static const wchar_t * const highlighting_keys[];
|
|
|
|
};
|
|
|
|
|
2012-03-07 07:51:48 +08:00
|
|
|
extern bool g_log_forks;
|
2012-04-22 11:08:08 +08:00
|
|
|
extern int g_fork_count;
|
2012-03-07 07:51:48 +08:00
|
|
|
|
2012-08-15 15:57:56 +08:00
|
|
|
extern bool g_use_posix_spawn;
|
|
|
|
|
2008-01-17 06:07:38 +08:00
|
|
|
|
2005-10-04 23:11:39 +08:00
|
|
|
#endif
|