System Overview
Minishell is a Unix shell implementation in C that follows a modular architecture with clear separation between parsing, execution, and built-in command handling. The system processes user input through a pipeline of tokenization, parsing, environment variable expansion, and command execution.Input Processing
Handles readline integration, quote parsing, and input validation
Command Parsing
Tokenizes input, expands variables, and builds command structures
Execution Engine
Manages process creation, pipes, and redirections
Built-ins
Implements shell built-in commands (cd, echo, export, etc.)
Core Data Structures
The shell’s architecture revolves around three primary data structures defined inminishell.h:
t_mini - Main Shell Context
The central data structure that holds the entire shell state:minishell.h:50
t_node - Command Execution Unit
Each command in a pipeline is represented by a node:minishell.h:40
The
is_set flag indicates whether the node is valid and ready for execution. Invalid redirections or syntax errors set this to 0.t_prompt - Environment Variables
Environment variables are stored in a linked list:minishell.h:34
Main Execution Flow
The shell operates in a read-eval-print loop defined inmain.c:
Component Interaction
The major components interact in this sequence:Module Responsibilities
Parsing Module
Parsing Module
prepare_line(): Adds spaces around redirectionsft_split(): Quote-aware tokenizationvars(): Environment variable expansioncheck_wrong_pipes()/check_wrong_redir(): Syntax validation
Execution Module
Execution Module
ft_prepare_nodes(): Creates execution nodes from tokensft_execute_commands(): Main execution dispatcherexecute_simple_command(): Single command executionexcecute_pipe_sequence(): Pipeline execution with pipes
Environment Module
Environment Module
asignenvp(): Initializes environment linked listset_bin_path(): Extracts PATH directoriesset_full_path(): Resolves command paths
Memory Management
Minishell follows strict memory management patterns:- Command arrays: Freed after each command execution via
ft_free_nodes() - Environment list: Persists for shell lifetime, freed only on exit
- Temporary strings: Freed immediately after use (e.g., in
vars()expansion) - Nodes: Allocated per-command, cleaned up in
ft_free_nodes()
The shell uses a global variable
g_status to track the exit status of the last executed command, accessible from any module.Global State
main.c:15
- Exit codes from child processes (0-255)
- Signal-related status codes (130 for SIGINT)
- Built-in command return values
- Special values: 2 for syntax errors, 127 for command not found