Signals

Signal handling for interactive mode, command execution, and child processes.

Signal handling for the 42sh shell.

Author

pulgamecanica

Typedefs

typedef struct s_shell t_shell

Functions

void signals_setup_interactive(void)

Set up signal handlers for interactive mode (at the prompt).

SIGINT - custom handler: newline + redisplay prompt (like bash)

SIGQUIT - ignored (Ctrl-\ does nothing at prompt) (for now… I think)

SIGTSTP - ignored for now (Ctrl-Z does nothing at prompt, will handle fg/bg job control later)

void signals_setup_executing(void)

Set up signal handlers for executing context (parent waiting for fg child).

SIGINT — ignored (let signal reach the child process)

SIGQUIT — ignored (let signal reach the child process)

SIGTSTP — ignored (let signal reach the child process)

Note

The parent simply waits; the child handles or dies from the signal.

void signals_setup_child(void)

Restore default signal handlers in a child process before execve.

After fork the child inherits the parent’s signal dispositions (SIG_IGN for interactive mode). We must reset them to SIG_DFL so the executed program responds to signals normally.

void signals_check(t_shell *shell)

Process any pending signal that was caught during the last prompt.

Called in the main loop before each prompt iteration.

Sets $? to the appropriate value (130 for SIGINT = 128 + 2).

Variables

volatile sig_atomic_t g_signal_received

Global signal flag (volatile for signal handler safety)

Forward declaration to avoid circular dependency with 42sh.h

signal_interactive.c

Functions

static void sigint_handler_interactive(int sig)

SIGINT handler for interactive mode (at prompt).

: Ctrl-C abandons the current input line, prints a newline, and redisplays a fresh prompt. The old text stays visible above.

We use rl_replace_line + rl_on_new_line + rl_redisplay to tell readline to clear its internal buffer and show a new prompt on the next line.

void signals_setup_interactive(void)

Set up signal handlers for interactive mode (at the prompt).

SIGINT - custom handler: newline + redisplay prompt (like bash)

SIGQUIT - ignored (Ctrl-\ does nothing at prompt) (for now… I think)

SIGTSTP - ignored for now (Ctrl-Z does nothing at prompt, will handle fg/bg job control later)

Variables

volatile sig_atomic_t g_signal_received

Global signal flag (volatile for signal handler safety)

Forward declaration to avoid circular dependency with 42sh.h

signals_check.c

Functions

void signals_check(t_shell *shell)

Process any pending signal that was caught during the last prompt.

Called in the main loop before each prompt iteration.

Sets $? to the appropriate value (130 for SIGINT = 128 + 2).