summaryrefslogtreecommitdiffhomepage
path: root/src/var.h
blob: 7be4fd8751dcb811bfa0445b9050c0f0cc536841 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
 * @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 nullptr */
} shparam;

extern shparam shellparam;
void params_set(char **argv, int n);
void params_free(shparam *sp);

#endif