Macros | Functions | Variables
env.c File Reference
#include <assert.h>
#include <uv.h>
#include "nvim/vim.h"
#include "nvim/ascii.h"
#include "nvim/charset.h"
#include "nvim/fileio.h"
#include "nvim/os/os.h"
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/path.h"
#include "nvim/macros.h"
#include "nvim/strings.h"
#include "nvim/eval.h"
#include "nvim/ex_getln.h"
#include "nvim/version.h"
#include "nvim/map.h"

Macros

#define ENVNAMELEN   100
 
#define MAX_ENVPATHLEN   INT_MAX
 

Functions

const char * os_getenv (const char *name) FUNC_ATTR_NONNULL_ALL
 Like getenv(), but returns NULL if the variable is empty. More...
 
bool os_env_exists (const char *name) FUNC_ATTR_NONNULL_ALL
 
int os_setenv (const char *name, const char *value, int overwrite) FUNC_ATTR_NONNULL_ALL
 
int os_unsetenv (const char *name) FUNC_ATTR_NONNULL_ALL
 Unset environment variable. More...
 
char * os_getenvname_at_index (size_t index)
 
int64_t os_get_pid (void)
 
void os_get_hostname (char *hostname, size_t size)
 
void init_homedir (void)
 
char_uexpand_env_save (char_u *src)
 
char_uexpand_env_save_opt (char_u *src, bool one)
 
void expand_env (char_u *src, char_u *dst, int dstlen)
 
void expand_env_esc (char_u *restrict srcp, char_u *restrict dst, int dstlen, bool esc, bool one, char_u *prefix)
 
const void * vim_env_iter (const char delim, const char *const val, const void *const iter, const char **const dir, size_t *const len) FUNC_ATTR_NONNULL_ARG(2
 
 if (varval==NULL)
 
 if (dirend==NULL)
 
const void * vim_env_iter_rev (const char delim, const char *const val, const void *const iter, const char **const dir, size_t *const len) FUNC_ATTR_NONNULL_ARG(2
 
 if (varend==NULL)
 
 if (colon==NULL)
 
char * vim_getenv (const char *name)
 
size_t home_replace (const buf_T *const buf, const char_u *src, char_u *const dst, size_t dstlen, const bool one) FUNC_ATTR_NONNULL_ARG(3)
 
char_uhome_replace_save (buf_T *buf, char_u *src) FUNC_ATTR_NONNULL_RET
 
void vim_setenv (const char *name, const char *val)
 
char_uget_env_name (expand_T *xp, int idx)
 Function given to ExpandGeneric() to obtain an environment variable name. More...
 
bool os_setenv_append_path (const char *fname) FUNC_ATTR_NONNULL_ALL
 
bool os_shell_is_cmdexe (const char *sh) FUNC_ATTR_NONNULL_ALL
 Returns true if sh looks like it resolves to "cmd.exe". More...
 

Variables

const void FUNC_ATTR_WARN_UNUSED_RESULT
 
dir = varval
 
const char *const dirend = strchr(varval, delim)
 
 else
 
const size_t varlen = (size_t)(varend - val) + 1
 
const char *const colon = xmemrchr(val, (uint8_t)delim, varlen)
 
len = (size_t) (varend - colon)
 

Macro Definition Documentation

#define ENVNAMELEN   100
#define MAX_ENVPATHLEN   INT_MAX

Function Documentation

void expand_env ( char_u src,
char_u dst,
int  dstlen 
)

Expand environment variable with path name. "~/" is also expanded, using $HOME. For Unix "~user/" is expanded. Skips over "\ ", "\~" and "\$" (not for Win32 though). If anything fails no expansion is done and dst equals src.

Parameters
srcInput string e.g. "$HOME/vim.hlp"
dst[out]Where to put the result
dstlenMaximum length of the result
void expand_env_esc ( char_u *restrict  srcp,
char_u *restrict  dst,
int  dstlen,
bool  esc,
bool  one,
char_u prefix 
)

Expand environment variable with path name and escaping.

See also
expand_env
Parameters
srcpInput string e.g. "$HOME/vim.hlp"
dst[out]Where to put the result
dstlenMaximum length of the result
escEscape spaces in expanded variables
onesrcp is a single filename
prefixStart again after this (can be NULL)
char_u* expand_env_save ( char_u src)

Call expand_env() and store the result in an allocated string. This is not very memory efficient, this expects the result to be freed again soon.

Parameters
srcString containing environment variables to expand
See also
{expand_env}
char_u* expand_env_save_opt ( char_u src,
bool  one 
)

Similar to expand_env_save() but when "one" is true handle the string as one file name, i.e. only expand "~" at the start.

Parameters
srcString containing environment variables to expand
oneShould treat as only one file name
See also
{expand_env}
char_u* get_env_name ( expand_T xp,
int  idx 
)

Function given to ExpandGeneric() to obtain an environment variable name.

size_t home_replace ( const buf_T *const  buf,
const char_u src,
char_u *const  dst,
size_t  dstlen,
const bool  one 
)

Replace home directory by "~" in each space or comma separated file name in 'src'.

Replace home directory with tilde in each file name

If anything fails (except when out of space) dst equals src.

Parameters
[in]bufWhen not NULL, uses this buffer to check whether it is a help file. If it is then path to file is removed completely, one is ignored and assumed to be true.
[in]srcInput file names. Assumed to be a space/comma separated list unless one is true.
[out]dstWhere to put the result.
[in]dstlenDestination length.
[in]oneIf true, assumes source is a single file name and not a list of them.
Returns
length of the string put into dst, does not include NUL byte.
char_u* home_replace_save ( buf_T buf,
char_u src 
)

Like home_replace, store the replaced string in allocated memory.

Parameters
bufWhen not NULL, check for help files
srcInput file name
if ( varval  = NULL)
if ( dirend  = NULL)
if ( varend  = NULL)
if ( colon  = NULL)
void init_homedir ( void  )
bool os_env_exists ( const char *  name)

Returns true if environment variable name is defined (even if empty). Returns false if not found (UV_ENOENT) or other failure.

void os_get_hostname ( char *  hostname,
size_t  size 
)

Gets the hostname of the current machine.

Parameters
hostnameBuffer to store the hostname.
sizeSize of hostname.
int64_t os_get_pid ( void  )

Get the process ID of the Neovim process.

Returns
the process ID.
const char* os_getenv ( const char *  name)

Like getenv(), but returns NULL if the variable is empty.

char* os_getenvname_at_index ( size_t  index)
int os_setenv ( const char *  name,
const char *  value,
int  overwrite 
)
bool os_setenv_append_path ( const char *  fname)

Appends the head of fname to $PATH and sets it in the environment.

Parameters
fnameFull path whose parent directory will be appended to $PATH.
Returns
true if path was appended-to
bool os_shell_is_cmdexe ( const char *  sh)

Returns true if sh looks like it resolves to "cmd.exe".

int os_unsetenv ( const char *  name)

Unset environment variable.

const void* vim_env_iter ( const char  delim,
const char *const  val,
const void *const  iter,
const char **const  dir,
size_t *const  len 
)

Iterates $PATH-like delimited list val.

Note
Environment variables must not be modified during iteration.
Parameters
[in]delimDelimiter character.
[in]valValue of the environment variable to iterate over.
[in]iterPointer used for iteration. Must be NULL on first iteration.
[out]dirLocation where pointer to the start of the current directory name should be saved. May be set to NULL.
[out]lenLocation where current directory length should be saved.
Returns
Next iter argument value or NULL when iteration should stop.
const void* vim_env_iter_rev ( const char  delim,
const char *const  val,
const void *const  iter,
const char **const  dir,
size_t *const  len 
)

Iterates $PATH-like delimited list val in reverse order.

Note
Environment variables must not be modified during iteration.
Parameters
[in]delimDelimiter character.
[in]valValue of the environment variable to iterate over.
[in]iterPointer used for iteration. Must be NULL on first iteration.
[out]dirLocation where pointer to the start of the current directory name should be saved. May be set to NULL.
[out]lenLocation where current directory length should be saved.
Returns
Next iter argument value or NULL when iteration should stop.
char* vim_getenv ( const char *  name)

Vim getenv() wrapper with special handling of $HOME, $VIM, $VIMRUNTIME, allowing the user to override the Nvim runtime directory at runtime. Result must be freed by the caller.

Parameters
nameEnvironment variable to expand
Returns
[allocated] Expanded environment variable, or NULL
void vim_setenv ( const char *  name,
const char *  val 
)

Vim setenv() wrapper with special handling for $VIMRUNTIME to keep the localization machinery sane.

Variable Documentation

return colon = xmemrchr(val, (uint8_t)delim, varlen)
* dir = varval
return dirend = strchr(varval, delim)
else
Initial value:
{
*len = (size_t) (dirend - varval)
* len
Definition: env.c:682
const char *const dirend
Definition: env.c:640
const void FUNC_ATTR_WARN_UNUSED_RESULT
Initial value:
{
const char *varval = (const char *) iter
* len = (size_t) (varend - colon)
const size_t varlen = (size_t)(varend - val) + 1