How migmanu/minishell works
Project Type and Purpose
• This project is a command-line shell implementation (a Bash clone) that processes user commands, interprets built-ins (cd, echo, pwd, export, unset, env, exit), and performs command execution along with redirections and piping.
• Its goal is to mimic basic Bash functionalities and to give students experience with command parsing, execution, and dealing with edge cases similar to a real shell.
File Structure
• Top-Level Files:
• Makefile: Used to build the project.
• README.md and project documentation (en.subject.pdf) provide context.
• .gitignore and .gitmodules represent version control configuration.
• Key Directories:
• libft: A library of common utility functions used across the project.
• src: The main source code is organized into several subdirectories:
▸ mish: Contains initialization code, prompt setup, and the main() entry point.
▸ input_handler: Manages reading the user input, tokenizing commands, handling redirections, syntax, and command expansion.
▸ builtins: Holds implementation of built-in shell commands.
▸ executor: Handles the actual execution of commands, including forking and piping.
▸ hashmap: Implements an environment variable storage mechanism (through hash map structures).
▸ handle_exit: Deals with proper cleanup when exiting the shell.
▸ signals: Sets up and handles UNIX signals relevant for the shell’s runtime behavior.
• This clear division into modules highlights a modular approach, where each aspect of the shell (input processing, command execution, environment management, etc.) is handled by its own component.
README for Additional Insights
• The README extensively discusses the internal steps a shell takes: reading input (scanner), tokenizing, expanding variables, parsing into a command list (instead of an AST for simplicity), and executing commands.
• It also stresses the importance of handling built-in commands and the intricate behavior of pipes and redirection.
a. Main Components to Include:
• Initialization & Main Loop (from src/mish): Sets up prompt, environment, and enters the read-eval loop.
• Input Handler (src/input_handler): Reads user input, tokenizes commands, handles redirections, and detects syntax issues.
• Executor (src/executor): Processes the parsed command list, forks processes, wires pipes, and executes commands.
• Builtins Manager (src/builtins): Routes and executes built-in commands (cd, echo, pwd, etc.).
• Environment Management (src/hashmap): Manages environment variables using a hash map structure.
• Signal Handler (src/signals): Captures and processes system signals to make the shell responsive (e.g., handling CTRL+C).
• Exit Handler (src/handle_exit): Provides clean-up operations during shell exit.
• Libft (libft): A supporting utility library used throughout the system.
b. Relationships and Interactions:
• The main loop (Initialization) first invokes the Input Handler.
• The Input Handler passes processed tokens or a command list to the Executor.
• The Executor determines whether a command is a built-in. If so, it delegates the task to the Builtins Manager.
• For external commands, the Executor manages process creation and piping.
• The Environment Management module (hashmap) is referenced by both the Executor and Builtin commands (especially for commands like env or export).
• The Signal Handler is set up at initialization and remains active to intercept relevant system signals during operation.
• The Exit Handler is invoked when commands like exit are executed to perform necessary cleanup.
• The Libft library underpins many of these components by providing common functions.
c. Important Architectural Patterns:
• Modular design: Each component (input processing, command execution, built-in handling) is clearly separated.
• Pipeline-oriented flow: Data flow from user input to parsing and execution is sequential, following the shell’s read-eval-print loop.
• Separation of concerns: Distinct modules handle distinct tasks (e.g., tokenization vs. execution vs. signal handling).