How driely01/minishell works
Project Type and Purpose
• This is a C-based CLI application implementing a mini-bash shell.
• It reads user commands, tokenizes and parses them, handles built-in commands (cd, echo, export, unset, pwd, exit, env, heredoc) and external commands.
• It uses POSIX system calls (fork, execve, pipe, dup2, wait, open/read/close, chdir, getcwd, etc.) for process control and I/O redirection.
File Structure Analysis
• Top-level:
• minishell.c / minishell.h – entry point and global definitions
• Makefile / README.md
• Directories (modules):
• builtin/ – implementations of built-in commands (cd, echo, exit, export, pwd, unset)
• parcing/ – (sic) lexical analysis and syntax checking of the input line
• expand/ – variable expansion, quote removal, wildcard handling
• execution/ – process creation, piping, redirections, signal handling
• utils/ – shared helper functions (string ops, list management, error handling)
README Insights
• Contains an overview of parsing/tokenizing flow
• Emphasizes system calls used and the need to mirror bash behavior
• Mentions two major phases: parsing (tokenize → syntax check) and execution (handle pipes, redirections, built-ins, external commands, heredoc)
Main Components
a. User Interface Layer
• “Terminal / stdin” → Command line reader
b. Parsing Layer
• Tokenizer (scans input char-by-char → tokens)
• Syntax Checker (validates token sequence, brackets, separators)
c. Expansion Layer
• Variable & parameter expansion
• Quote stripping
• Wildcard (globbing) expansion
d. Execution Dispatcher
• Distinguishes built-in vs external commands
• Manages command sequence and pipelines
e. Built-in Module
• cd, pwd, echo, export, unset, exit, env, heredoc controllers
• Interacts with environment data structure in memory
f. External Command Module
• fork → child process
• execve → load external binary
g. I/O & Pipe Manager
• pipe() / dup2() for stdin/stdout redirection
• open()/close() for file redirections
h. Process and Signal Controller
• wait()/waitpid() for child synchronization
• signal() / kill() handlers for SIGINT, SIGQUIT
i. Environment Store
• In-memory list of key=value pairs (export/unset)
j. OS Kernel (external service)
• System calls interface
Relationships & Data Flow
• Terminal → Tokenizer
• Tokenizer → Parser → Expander → Dispatcher
• Dispatcher → Built-in Module (no fork) ↔ Environment Store → return status
• Dispatcher → External Module (fork + execve) → Kernel
• Kernel → process image + file system
• Dispatcher → I/O & Pipe Manager → Kernel
• All modules use Utils library for common functions
• Bidirectional data flow where appropriate (e.g., built-ins modify environment).
Key Architectural Patterns
• Modular pipeline: clear separation of concerns (Lexing, Parsing, Expansion, Execution)
• Single process orchestrator + forked children for external commands
• Shared global environment state for built-ins
• POSIX system call façade for kernel interactions