How achnouri/minishell-master works
Project Type & Purpose
• This is a minimal Unix shell implementation in C (“Minishell”) whose goal is to replicate a subset of Bash features.
• It focuses on parsing user input, handling tokens, performing environment expansions, managing redirections and pipes, executing built-in and external commands, and handling signals and memory.
File Structure & Architectural Insights
• Top-level:
• Makefile – build orchestration
• includes/minishell.h – central declarations
• main.c – entry point
• Core modules under sources/:
• parser/ – tokenization, syntax validation, quote handling, expansions
– core_tokens/ – split input into tokens
– core_commands/ – detect redirections, pipes, heredoc
– core_expansions/ – expand environment variables
– shell_initialization – load env, set up signal handlers
• executer/ – execute commands
– builtins/ – cd, echo, env, exit, export, pwd, unset
– execution/ – fork/exec flow, waiting, exit codes
– redirection/ – file I/O, pipes
• shell_utils/ – memory management, error messages, libft support
• README confirms reliance on syscalls (fork, execve, dup2, pipe, wait), strict memory management, and signal handling.
Main Components
A. User Interface Layer
• Command-line prompt (readline or basic readline wrapper)
B. Parser Subsystem
1. Tokenizer (core_tokens)
2. Syntax Validator & Quote Handler (syntax_validator, quotes_utils)
3. Expander (core_expansions)
4. Command Builder (core_commands)
C. Execution Subsystem
1. Built-ins Handler (builtins modules)
2. External Command Launcher
3. Redirection Manager (io_file, pipe)
4. Process Manager (fork, execve, waitpid)
D. Environment Manager
• Tracks environment variables, provides lookup and update APIs
E. Signal Handler
• SIGINT, SIGQUIT, EOF behaviors
F. Memory Manager
• Allocation/free, garbage collection (gb_coll), libft utilities
G. OS Interface
• System calls invoked by subsystems
Component Interactions & Data Flow
1. User types a command → CLI layer
2. CLI → Parser: raw string passed to tokenizer → tokens list
3. Tokens → Expander: replace variables, handle quotes
4. Expanded tokens → Command Builder: detect pipes/redirections, build a command pipeline data structure
5. Built command structure → Executor:
a. Set up pipes and redirections via dup2/pipe
b. For each stage:
– If builtin, call in-process handler
– Else fork child → execve
c. Parent waits on child processes
6. Execution result → CLI prints exit status if needed → prompt returns
7. Throughout: Signal Handler intercepts Ctrl-C, Ctrl-D, Ctrl-\ and routes to CLI or child processes
8. Memory Manager allocates and frees all dynamic structures at each cycle
Architectural Patterns & Principles
• Modular separation: Parser vs Executor vs Utilities
• Single responsibility: each .c file handles one feature (tokenization, one builtin, one redirection case)
• Pipeline pattern: commands connected by Unix pipes
• Error-first and resource cleanup pattern: free on every failure path
• Minimal global state: environment stored in a managed structure