42sh — Shell State

Top-level header defining the central t_shell struct and variable-management API.

Top-level header for the 42sh shell, includes all sub-systems and defines the central t_shell state struct.

Author

wengzhang, jguillem, jspitz, pulgamecanica, zweng

Defines

LST_VAR(n)

Cast a t_list node’s content to t_var *.

LST_JOB(n)

Cast a t_list node’s content to t_job *.

LST_ALIAS(n)

Cast a t_list node’s content to t_alias *.

LST_PROC(n)

Cast a t_list node’s content to t_process *.

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.

struct t_shell
#include <42sh.h>

Central runtime state of the shell — one instance lives in main().

This struct holds all global state, including variables, jobs, history file path, and terminal settings.

Public Members

t_list *aliases

t_alias* list; alias table.

char *cmd_entrypoint

-c command string, or NULL.

t_job *current_job

Most recent job (%+ / %%).

char **env

Cached NULL-terminated array for execve.

int env_dirty

1 when env needs rebuild before next execve.

int exit_confirmed

Double-exit guard when stopped jobs exist.

char *history_file

Path from $HISTFILE or $HOME/.sh_history.

int interactive

1 if stdin is a TTY (prompt + readline active).

t_list *jobs

t_job* list; all known jobs.

int last_exit_status

Value of $?.

struct termios original_termios

Saved terminal attributes, restored on exit.

int running

Main loop flag; set to 0 to exit.

pid_t shell_pgid

Shell’s own process group id.

int terminal_fd

File descriptor of the controlling terminal.

t_list *variables

t_var* list; all shell/env variables.

main.c

Functions

static int shell_init(t_shell *shell, char **envp)
static void shell_cleanup(t_shell *shell)
static char *read_line(t_shell *shell)
static void parse_options(int argc, char *argv[], t_shell *shell)
static void process_line(t_shell *shell, char *line)
int main(int argc, char *argv[], char *envp[])