42sh (Shell State)

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_JOB(n)

Casts a t_list node’s content to t_job *.

LST_PROC(n)

Casts a t_list node’s content to t_process *.

Functions

char *shell_read_line(t_shell *shell, const char *prompt)

Read one line of shell input — the single owner of stdin.

readline in interactive mode, getline on the shared stdin FILE* otherwise. The REPL and the heredoc collector both call this so command lines and heredoc bodies stay synchronised.

Returns:

Heap-allocated line without trailing newline, or NULL on EOF.

char *shell_read_logical_line(t_shell *shell, const char *primary)

Read a logical line, joining physical lines on unclosed quotes or trailing-backslash line-continuation (REPL only, not heredoc).

Returns:

Heap-allocated buffer, or NULL on EOF before any input.

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.

t_hash *cmd_hash

Cached PATH lookups for the hash builtin.

char *cmd_entrypoint

./42sh -c <cmd> 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.

t_list *heredoc_body_queue

Pre-collected heredoc bodies (FIFO of char *); populated by shell_read_logical_line, drained by the heredoc collector. NULL outside REPL pre-collection.

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.