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

LST_VAR(n)

Casts a t_list node’s content to t_var *.

LST_ALIAS(n)

Casts a t_list node’s content to t_alias *.

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.

t_var *var_get(t_shell *shell, const char *name)

Get the t_var node for name, or NULL if unset.

int var_set(t_shell *shell, const char *name, const char *value)

Set (or create) variable name to value. Returns 0 on success.

int var_unset(t_shell *shell, const char *name)

Remove variable name. Returns 0 on success.

int var_export(t_shell *shell, const char *name)

Mark variable name for export to child processes.

char **var_get_environ(t_shell *shell)

Return (and cache) the NULL-terminated envp array for execve.

void var_init_from_environ(t_shell *shell, char **envp)

Populate shell->variables from the process’s initial envp.

int var_is_valid_identifier(const char *name)

Check whether name is a valid shell identifier.

True if name is a valid shell identifier (export/unset/set validation).

Returns:

1 if name starts 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 str has the form “NAME=VALUE”.

True if str has the form “NAME=VALUE”; on success *eq_pos is the ‘=’.

Parameters:
  • str – The candidate assignment string.

  • eq_pos – Out: index of the ‘=’ on success.

Returns:

1 if str is an assignment, 0 otherwise.

struct t_var
#include <variables.h>

Shell variable data node.

One instance per named variable. Stored in t_shell.variables as the content of a t_list node; traversal is done via that wrapper, which is why this struct carries no next pointer of its own. Both name and value are heap-owned by this module.

Public Members

char *name

Variable name (heap-owned, never NULL once set).

char *value

Variable value, or NULL for “exported but unset”.

int exported

1 when this name should appear in execve’s envp.

int readonly

1 when set/unset should refuse writes.

struct t_alias
#include <variables.h>

Alias data node.

Stored in t_shell.aliases as the content of a t_list node. Like t_var, the linked-list plumbing is provided by the wrapping t_list, not by this struct.

Public Members

char *name

Alias name (heap-owned).

char *value

Replacement text (heap-owned).