/** * @file var.h * @brief Shell variables, scopes, positional parameters and the environment. */ #ifndef ZASH_VAR_H #define ZASH_VAR_H #include "ast.h" /** * @brief Variable flags. */ enum { V_EXPORT = 1u << 0, V_READONLY = 1u << 1, V_UNSET = 1u << 2, /**< known name with no value */ V_NOFREE = 1u << 3, /**< text is not ours to free */ V_DYNAMIC = 1u << 4, /**< value is computed on each read */ V_INTEGER = 1u << 5, /**< values assigned are evaluated as arithmetic */ }; /** * @brief A shell variable. * * Each variable is stored as one `name=value` string so that the * environment for a child is just an array of pointers into the table. */ typedef struct var { struct var *next; char *text; /**< "name=value" */ size_t nlen; /**< length of name */ unsigned flags; int save_depth; /**< scope that last saved this variable */ void (*hook)(const char *value); /**< called after every change */ const char *(*get)(void); /**< V_DYNAMIC */ void (*set)(const char *value); /**< V_DYNAMIC: assignment, if allowed */ } var; /* * A scope remembers the previous state of variables changed while it is * active and restores them when popped. With `capture` set, every change * is remembered automatically, which is how the shell undoes the side * effects of work it does on behalf of a would-be subshell. */ typedef struct varsave varsave; /** * @brief A variable scope. * * A scope remembers the previous state of variables changed while it is * active and restores them when popped. With `capture` set, every change * is remembered automatically, which is how the shell undoes the side * effects of work it does on behalf of a would-be subshell. */ typedef struct varscope { struct varscope *prev; varsave *saved; int depth; bool capture; bool func; /**< scope of a function call: target of `local` */ } varscope; void var_init(char **envp); const char *var_get(const char *name); const char *var_get_n(const char *name, size_t nlen); var *var_find(const char *name, size_t nlen, bool create); void var_set(const char *name, const char *value, unsigned flags); void var_set_n(const char *name, size_t nlen, const char *value, unsigned flags); void var_set_long(const char *name, long value); bool var_unset(const char *name); void var_set_flags(const char *name, unsigned flags); void var_clear_flags(const char *name, unsigned flags); void var_print_all(unsigned flag, const char *prefix); void var_each(void (*fn)(const char *name, void *arg), void *arg); char **var_environ(void); char **var_environ_with(char **extra, int nextra); extern varscope *var_scope; void scope_push(varscope *s, bool capture, bool is_func); void scope_pop(void); void scope_unwind(varscope *to); bool var_make_local(const char *name); void var_save(var *v, varscope *s); /** * @brief Field-splitting classes of a byte: not a separator, IFS white space, * or any other IFS character. */ enum { IFS_NONE, IFS_SPACE, IFS_HARD }; extern unsigned char ifs_class[256]; extern char ifs_first; /**< separator for "$*"; '\0' if IFS is empty */ /* ---- positional parameters ------------------------------------------- */ /** * @brief A set of positional parameters. */ typedef struct shparam { int n; char **p; void *base; /**< heap block holding p and the strings, or NULL */ } shparam; extern shparam shellparam; void params_set(char **argv, int n); void params_free(shparam *sp); #endif