How moraouid/minishell works
Project type and purpose
• Type: A command-line shell (an interpreter) written in C.
• Purpose: Read user input, parse it into commands, handle expansions/redirections/pipelines/built-ins, invoke external programs or built-ins, and manage resources/signals like a minimal Bash clone.
High-level module breakdown (from file tree / README)
• Entry & Loop:
– minishell.c + readline integration (GNU Readline)
• Parsing & Syntax:
– parse/tokeniziation → split raw input
– parse/parsing → build command structures & validate
– parse/expansions → variable and status expansion
– parse/remove_quotes → strip quote characters
– parse/syntax_errors → report errors
• Execution Engine:
– exec/exec.c + exec_utils* → fork/exec, pipeline wiring
– exec/redirect_utils.c → file-descriptor redirections
– herdoc/ → heredoc delimiter handling
• Built-ins:
– builtins/my_*.c → cd, echo, pwd, env, export, unset, exit
– builtins/my_utils*.c → shared routines for built-ins
• Helpers & Utils:
– helper_utils → string split, environment copy, memory-tracking GC, misc utils
• Signal Handling:
– signal/signal.c → SIGINT, SIGQUIT, EOF behavior
• Interfaces & Data:
– includes/*.h → public interfaces for parse, exec, overall shell APIs
Key architectural patterns
• Pipeline of responsibility (“stages”): Input → Tokenization → Parsing → Expansion → Quote Removal → Execution
• Master loop with resource-cleanup at each iteration via a simple GC tracker
• Parent vs. child process distinction for built-ins vs. external commands
• Use of POSIX APIs (fork, execve, pipe, dup2, wait) and GNU Readline library
Main components & their interactions
A. User Interface Layer
• Responsibilities: display prompt, read user input, history
• Tech: GNU Readline
B. Frontend (Parsing Pipeline)
1. Tokenizer
– Input: raw line string
– Output: list of tokens (words, operators)
2. Parser & AST Builder
– Input: tokens
– Output: command data structures (linked lists or trees)
3. Expander
– Input: command structures with $VAR, $?
– Output: substituted strings, possibly splitting tokens
4. Quote Remover
– Input: expanded tokens with quotes
– Output: final argv[] lists
5. Syntax/Error Checker
– Hooks into parser to enforce valid operator usage
C. Execution Engine
1. Built-in Dispatcher
– Directly executed in parent for state-changing built-ins (cd, export, unset, exit)
2. External Command Runner
– Uses fork()+execve(), PATH lookup via helper_utils copy_env
3. Pipeline Manager
– Orchestrates pipe() creation, fd wiring
4. Redirection Manager
– Handles <, >, >> by opening files and dup2()
5. Heredoc Handler
– Reads until delimiter, writes to temporary FD and wires as stdin
D. Shared Services
• Environment Manager (copy_env.c, my_env.c, my_export.c, my_unset.c)
• Memory Tracker / GC (gc.c)
• Signal Handler (signal.c)
E. OS Interface
• System calls: fork, execve, pipe, dup2, open, close, wait, signal APIs
• From those to “Pipeline Manager” / “Redirection Manager” / “Heredoc Handler”