How flmarsou/minishell works
Project type and purpose
• Minishell is a CLI interpreter—a small Unix-style shell that reads user commands, tokenizes and parses them (handling quotes, pipes, redirections, env variables), and then executes either internal builtins or external binaries via fork/exec.
File structure
• Top-level:
• includes/minishell.h – all shared data structures and function signatures
• src/main.c – shell entry point and REPL loop
• src/lexer – tokenization logic
• src/parser – syntax and semantics validation, command-structure building
• src/execution – fork/exec orchestration, pipe & redir handling, heredocs, signals
• src/builtins – implementations of cd, echo, env, exit, export, pwd, unset, execve wrapper
• src/utils & src/free & src/debug – string helpers, memory cleanup, logging/debug aids
• Makefile, LICENSE, git config—standard project metadata and build scripts.
README for additional insights
• The shell pipeline:
User input → Lexer (token list) → Parser (command struct with args, redirs, pipes) → Executor (create processes, set up pipes/redirs, dispatch builtins vs execve)
• Builtins run in-process (some may need to affect parent state, e.g. cd, exit).
• External commands use fork(), execve().
• Supports here-docs, multi-stage pipelines, signal handling.
Main components
a. User Interface / REPL
– Reads input lines, handles special keys (not shown in code but implied), invokes Lexer
b. Lexer
– Tokenizer: produces sequence of tokens (WORD, QUOTE, PIPE, REDIR, DOLLAR…)
– Token counter and token list management
c. Parser
– Validates syntax (quotes matching, pipe placement, redir errors)
– Processes quotes, expands variables, merges WORD tokens
– Builds an internal Command List: each with argv[], list of redirections, pipe grouping
d. Executor
– Pipeline Manager: for N commands, allocates pipes, forks child processes
– Redirection Handler: opener/closer for <, >, >>, << (heredoc)
– Signal Handlers: in parent and children, special handling for heredoc and interrupted processes
– Builtin Dispatcher: detects if cmd matches one of {cd, echo…}, calls in-process function or bypasses to execve
– External Command Launcher: uses execve() to spawn binaries
e. Environment State
– A global map of environment variables, read by parser for $VAR expansion, modified by export/unset
f. Utilities & Memory Management
– Helper functions (string ops, errors)
– Free routines to clean up tokens, parse trees, child data
Relationships and data flow
• For each command node:
– Setup pipes ←→ Next command
– Apply redirections to STDIN/STDOUT
– If builtin → call function (possibly altering parent env)
– Else → fork & execve (child uses inherited env)
Architectural patterns and principles
• Modular layers: clear separation between lexing, parsing and execution
• Pipeline pattern: commands chained by pipes; each stage is a separate process
• Command data structure: similar to a simple AST—each node holds argv[], redir list
• Single Responsibility: each directory handles one aspect of the shell
• In-process vs out-of-process: builtins executed without fork when impacting parent state
Critical details to capture
• The loop in main.c (read-evaluate-print)
• How tokens flow into parser and get transformed
• The fork/pipe tree for pipelines
• Builtin handling in parent vs child
• Heredoc: temporary file or pipe, signal interception