# migmanu/minishell architecture

> Architecture diagram of the GitHub repository migmanu/minishell, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/migmanu/minishell
- Repository: https://github.com/migmanu/minishell

## Overview

##### 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).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Core Processing Modules (Blue)
    MLI["Main Loop & Initialization"]:::core
    IH["Input Handler"]:::core
    EXE["Executor"]:::core
    BR["Builtins Router"]:::core
    ECE["External Command Executor"]:::core

    %% Supporting Modules (Green)
    HM["Environment (Hash Map)"]:::supporting
    LF["Utility Library (Libft)"]:::supporting

    %% Infrastructure Modules (Red)
    SH["Signal Handler"]:::infra
    EH["Exit Handler"]:::infra

    %% Sequential Flow
    MLI -->|"triggers"| IH
    IH -->|"passes commands"| EXE
    EXE -->|"if built-in"| BR
    EXE -->|"if external"| ECE

    %% Environment Dependencies (Dotted connections)
    EXE ---|"env dependency"| HM
    BR ---|"env dependency"| HM

    %% Signal Handling Connections (Dotted)
    SH ---|"handles signals"| MLI
    SH ---|"monitors"| EXE

    %% Exit Flow from Builtin 'exit'
    BR -->|"exit command"| EH

    %% Utility Library Underpinning (Dotted arrows)
    MLI --- LF
    IH --- LF
    EXE --- LF
    BR --- LF

    %% Click Events for Component Mapping
    click MLI "https://github.com/migmanu/minishell/blob/main/src/mish/main.c"
    click MLI "https://github.com/migmanu/minishell/blob/main/src/mish/init_mish.c"
    click MLI "https://github.com/migmanu/minishell/blob/main/src/mish/init_prompt.c"
    click IH "https://github.com/migmanu/minishell/tree/main/src/input_handler/"
    click EXE "https://github.com/migmanu/minishell/tree/main/src/executor/"
    click BR "https://github.com/migmanu/minishell/tree/main/src/builtins/"
    click HM "https://github.com/migmanu/minishell/tree/main/src/hashmap/"
    click SH "https://github.com/migmanu/minishell/blob/main/src/signals/signals.c"
    click EH "https://github.com/migmanu/minishell/tree/main/src/handle_exit/"
    click LF "https://github.com/migmanu/minishell/tree/main/libft"

    %% Styles
    classDef core fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef supporting fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef infra fill:#FFB6C1,stroke:#000,stroke-width:2px;
```
