Variables
Shell variable and alias data structures, plus the variable management API (set, unset, export, environ rebuild) used by the rest of the shell.
Variables and aliases are stored on the central t_shell struct as t_list* chains of t_var* / t_alias* content pointers. This header owns those node types, the casting macros used to walk them, and every mutator that touches the variable table (create, set, unset, export, environ rebuild). The full t_shell definition lives in 42sh.h; this header forward-declares it so callers that only need the variable API can avoid pulling the full shell state header.
- Author
wengzhang, pulgamecanica
Defines
Typedefs
-
typedef struct s_shell t_shell
Forward declaration to avoid circular dependency with 42sh.h.
Functions
-
char *var_get_value(t_shell *shell, const char *name)
Get the value string of variable
name, or NULL if unset.
-
int var_set(t_shell *shell, const char *name, const char *value)
Set (or create) variable
nametovalue. Returns 0 on success.
-
char **var_get_environ(t_shell *shell)
Return (and cache) the
NULL-terminatedenvparray for execve.
-
void var_init_from_environ(t_shell *shell, char **envp)
Populate
shell->variablesfrom the process’s initialenvp.
-
int var_is_valid_identifier(const char *name)
Check whether
nameis a valid shell identifier.True if
nameis a valid shell identifier (export/unset/set validation).- Returns:
1 if
namestarts with a letter or ‘_’ and the rest are alphanumerics or ‘_’; 0 otherwise.
-
int var_is_assignment(const char *str, size_t *eq_pos)
Check whether
strhas the form “NAME=VALUE”.True if
strhas the form “NAME=VALUE”; on success*eq_posis the ‘=’.- Parameters:
str – The candidate assignment string.
eq_pos – Out: index of the ‘=’ on success.
- Returns:
1 if
stris an assignment, 0 otherwise.
-
struct t_var
- #include <variables.h>
Shell variable data node.
One instance per named variable. Stored in
t_shell.variablesas thecontentof at_listnode; traversal is done via that wrapper, which is why this struct carries nonextpointer of its own. Bothnameandvalueare heap-owned by this module.