Skip to content

[FEAT] Core & Signal Handling: REPL and Signalsย #1

@David-A-T-M

Description

@David-A-T-M

๐ŸŽฏ Objective

Implement the main shell lifecycle (Read-Eval-Print Loop) and ensure the program is resilient to user interruptions (Ctrl+C, Ctrl+D, Ctrl+), emulating Bash behavior.

๐Ÿ—๏ธ Proposed Architecture.

  1. Global State Structure (t_shell)

A persistent state is required to track environment variables, exit codes, and the main loop status.

typedef struct s_shell {
char    **envp;       // Copy of environment variables
int     last_status;  // Value of $? (exit code)
int     running;      // Loop control flag
char    *line;        // Raw input from readline
} t_shell;
  1. Function Definitions (Signatures)

Module: signals.c

  • void setup_signals(void); -> Configures initial handlers using sigaction.
  • void handle_sigint(int sig); -> Handler for SIGINT (Ctrl+C). It must clear the readline buffer and move to a new line.
  • void ignore_sigquit(void); -> Sets SIGQUIT (Ctrl+) to be ignored.

Module: core.c

  • void init_shell(t_shell *sh, char **env); -> Allocates initial memory and duplicates the environment.
  • void shell_loop(t_shell *sh); -> Contains the while(sh->running) logic.
  • char *get_prompt_text(void); -> Generates the prompt string (e.g., user@minishell:~$ ).
  • void cleanup_shell(t_shell *sh); -> Frees all memory before exiting (prevents leaks).

๐Ÿ› ๏ธ TODO List

  • Readline Setup: Implement the basic loop using the libreadline library.
  • EOF Handling (Ctrl+D): Detect when readline returns NULL and exit the shell cleanly.
  • Signal Handler (Ctrl+C):
    • Use rl_replace_line("", 0) to clear the current line.
    • Use rl_on_new_line() to notify readline we moved to a new line.
    • Use rl_redisplay() to show the prompt again.
  • History Management: Implement add_history(line) so up/down arrows work.
  • Terminal Attributes: Use termios.h to hide the ^C character from appearing in the terminal.
  • Environment Initialization: Create a deep copy of envp to allow future modifications (export/unset).

โš ๏ธ Technical Considerations

Memory Leaks: Every line returned by readline must be freed.

Signal Modes: Note that signal behavior must change once we implement fork(). For now, we only care about the shell in its waiting for input state.

Compilation: Remember to link the library using the -lreadline flag in your Makefile.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions