# m4nnb3ll/minishell architecture

> Architecture diagram of the GitHub repository m4nnb3ll/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/m4nnb3ll/minishell
- Repository: https://github.com/m4nnb3ll/minishell

## Overview

##### Main Components
- Input Handling & Signals: Include components such as main.c and main_signals.c which initialize the shell, handle startup, and manage signals.
- Tokenizer: Represent the tokenization process (the "tokenizing" directory) which breaks up input strings into tokens. Components include tokenizer.c, tokenizer_appenders.c, tokenizer_handlers.c, etc.
- Parser: Show the parsing stage (directory "parsing") where tokens are analyzed and arranged into a command structure. Include parser.c and its associated helper, clear, error, nodes, and utils files.
- Expander: Illustrate the expansion process (the "expander" directory) where variables, globbing, and quotes are handled and replaced. Components here include ft_expand* functions, ft_globber functions, and heredoc_expander.
- Execution Manager: Represent the execution logic found in the "exec" directory. This handles overall execution logic, checking for errors, performing redirections, and calling built-in commands when needed. Include exec.c files, exec_builtin.c, and related utility and error message modules.
- Cleanup/Miscellaneous: Incorporate a module for cleaning resources (the "cleaning" directory) which likely ensures that allocated data is freed properly.
- Utility Library: Show that the project leverages a custom utility library (in "libraries/libft") to provide common C functions (e.g., string manipulation, memory functions).
- Files: While not a runtime component, the "include" directory holds header files (minishell.h, parsing.h, tokenizing.h, etc.) which define the interfaces and data structures across modules.

##### Relationships and Data Flow
- From the Tokenizer, data flows into the Parser which constructs the command structure.
- The parsed command information is then sent to the Expander to resolve environment variables, handle quotes, and deal with globbing (e.g., asterisk expansion).
- After expansion, the execution component takes over: it determines whether a command is a built-in (redirecting execution to the Built-ins component) or an external utility (managing forking and calling binaries via PATH resolution).
- The Execution Manager may interface with Cleanup for resource management post execution.
- Utility functions from the Libft library are used throughout the flow for various tasks.

##### Architectural Patterns & Design Principles
- Modular Separation: Each major phase of command processing (tokenization, parsing, expansion, execution) is clearly separated into its own directories/modules.
- Layered/Sequential Processing: The design follows a strict linear pipeline – from reading input to final execution.
- Reusability & Abstraction: The use of an external libraries folder (for Libft) and modular built-ins helps keep functionality decoupled and maintains a single responsibility per module.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Nodes
    UserInput["User Input"]:::input

    %% Input Handling & Signals
    subgraph "Input Handling & Signals"
        Main["main.c"]:::preexec
        Signals["main_signals.c"]:::preexec
    end

    %% Tokenizer Module
    subgraph "Tokenizer"
        T0["tokenizer.c"]:::preexec
        T1["tokenizer_appenders.c"]:::preexec
        T2["tokenizer_handlers.c"]:::preexec
        T3["tokenizer_lst.c"]:::preexec
        T4["tokenizer_utils.c"]:::preexec
    end

    %% Parser Module
    subgraph "Parser"
        P0["parser.c"]:::preexec
        P1["parser_clear.c"]:::preexec
        P2["parser_err.c"]:::preexec
        P3["parser_helpers.c"]:::preexec
        P4["parser_nodes.c"]:::preexec
        P5["parser_utils.c"]:::preexec
    end

    %% Expander Module
    subgraph "Expander"
        E0["ft_expand.c"]:::preexec
        E1["ft_asterisker.c"]:::preexec
        E2["ft_clean_empty_strs.c"]:::preexec
        E3["ft_expand_utils.c"]:::preexec
        E4["ft_expander_split.c"]:::preexec
        E5["ft_globber.c"]:::preexec
        E6["ft_globber_utils.c"]:::preexec
        E7["ft_strip_quotes.c"]:::preexec
        E8["heredoc_expander.c"]:::preexec
    end

    %% Execution Manager Module
    subgraph "Execution Manager"
        X0["exec.c"]:::execphase
        X1["exec_builtin.c"]:::execphase
        X2["exec_redirect.c"]:::execphase
        X3["exec_utils.c"]:::execphase
        X4["error_msg.c"]:::execphase
        X5["exist_check.c"]:::execphase
        X6["ft_exec_simple_cmd.c"]:::execphase
        X7["ft_get_path.c"]:::execphase
        X8["init_tree.c"]:::execphase
    end

    %% Built-ins Module
    subgraph "Built-ins"
        B0["cd.c"]:::execphase
        B1["echo.c"]:::execphase
        B2["env.c"]:::execphase
        B3["env_utils.c"]:::execphase
        B4["exit.c"]:::execphase
        B5["export.c"]:::execphase
        B6["pwd.c"]:::execphase
        B7["unset.c"]:::execphase
    end

    %% Cleanup Module
    subgraph "Cleanup/Miscellaneous"
        C0["ft_clean_ms.c"]:::execphase
    end

    %% Global Dependencies
    Libft["Utilities/Libft"]:::dependency
    Includes["Include Files"]:::dependency

    %% Flow Connections
    UserInput --> Main
    UserInput --> Signals

    Main -->|"init"| T0
    T0 --> P0
    P0 --> E0
    E0 --> X0
    X0 -->|"builtin_check"| B0
    X0 -->|"post_exec"| C0

    %% Dependencies: Each layer uses Include Files and Utility Library
    Main ---|"uses"| Includes
    T0 ---|"uses"| Includes
    P0 ---|"uses"| Includes
    E0 ---|"uses"| Includes
    X0 ---|"uses"| Includes
    B0 ---|"uses"| Includes

    Main ---|"utils"| Libft
    T0 ---|"utils"| Libft
    P0 ---|"utils"| Libft
    E0 ---|"utils"| Libft
    X0 ---|"utils"| Libft
    B0 ---|"utils"| Libft

    %% Styling for vertical layout (avoid horizontal spread)
    linkStyle default stroke:#333,stroke-width:1.5px

    %% Click Events for Input Handling & Signals
    click Main "https://github.com/m4nnb3ll/minishell/blob/main/main.c"
    click Signals "https://github.com/m4nnb3ll/minishell/blob/main/main_signals.c"

    %% Click Events for Tokenizer Module
    click T0 "https://github.com/m4nnb3ll/minishell/blob/main/tokenizing/tokenizer.c"
    click T1 "https://github.com/m4nnb3ll/minishell/blob/main/tokenizing/tokenizer_appenders.c"
    click T2 "https://github.com/m4nnb3ll/minishell/blob/main/tokenizing/tokenizer_handlers.c"
    click T3 "https://github.com/m4nnb3ll/minishell/blob/main/tokenizing/tokenizer_lst.c"
    click T4 "https://github.com/m4nnb3ll/minishell/blob/main/tokenizing/tokenizer_utils.c"

    %% Click Events for Parser Module
    click P0 "https://github.com/m4nnb3ll/minishell/blob/main/parsing/parser.c"
    click P1 "https://github.com/m4nnb3ll/minishell/blob/main/parsing/parser_clear.c"
    click P2 "https://github.com/m4nnb3ll/minishell/blob/main/parsing/parser_err.c"
    click P3 "https://github.com/m4nnb3ll/minishell/blob/main/parsing/parser_helpers.c"
    click P4 "https://github.com/m4nnb3ll/minishell/blob/main/parsing/parser_nodes.c"
    click P5 "https://github.com/m4nnb3ll/minishell/blob/main/parsing/parser_utils.c"

    %% Click Events for Expander Module
    click E0 "https://github.com/m4nnb3ll/minishell/blob/main/expander/ft_expand.c"
    click E1 "https://github.com/m4nnb3ll/minishell/blob/main/expander/ft_asterisker.c"
    click E2 "https://github.com/m4nnb3ll/minishell/blob/main/expander/ft_clean_empty_strs.c"
    click E3 "https://github.com/m4nnb3ll/minishell/blob/main/expander/ft_expand_utils.c"
    click E4 "https://github.com/m4nnb3ll/minishell/blob/main/expander/ft_expander_split.c"
    click E5 "https://github.com/m4nnb3ll/minishell/blob/main/expander/ft_globber.c"
    click E6 "https://github.com/m4nnb3ll/minishell/blob/main/expander/ft_globber_utils.c"
    click E7 "https://github.com/m4nnb3ll/minishell/blob/main/expander/ft_strip_quotes.c"
    click E8 "https://github.com/m4nnb3ll/minishell/blob/main/expander/heredoc_expander.c"

    %% Click Events for Execution Manager Module
    click X0 "https://github.com/m4nnb3ll/minishell/blob/main/exec/exec.c"
    click X1 "https://github.com/m4nnb3ll/minishell/blob/main/exec/exec_builtin.c"
    click X2 "https://github.com/m4nnb3ll/minishell/blob/main/exec/exec_redirect.c"
    click X3 "https://github.com/m4nnb3ll/minishell/blob/main/exec/exec_utils.c"
    click X4 "https://github.com/m4nnb3ll/minishell/blob/main/exec/error_msg.c"
    click X5 "https://github.com/m4nnb3ll/minishell/blob/main/exec/exist_check.c"
    click X6 "https://github.com/m4nnb3ll/minishell/blob/main/exec/ft_exec_simple_cmd.c"
    click X7 "https://github.com/m4nnb3ll/minishell/blob/main/exec/ft_get_path.c"
    click X8 "https://github.com/m4nnb3ll/minishell/blob/main/exec/init_tree.c"

    %% Click Events for Built-ins Module
    click B0 "https://github.com/m4nnb3ll/minishell/blob/main/builtins/cd.c"
    click B1 "https://github.com/m4nnb3ll/minishell/blob/main/builtins/echo.c"
    click B2 "https://github.com/m4nnb3ll/minishell/blob/main/builtins/env.c"
    click B3 "https://github.com/m4nnb3ll/minishell/blob/main/builtins/env_utils.c"
    click B4 "https://github.com/m4nnb3ll/minishell/blob/main/builtins/exit.c"
    click B5 "https://github.com/m4nnb3ll/minishell/blob/main/builtins/export.c"
    click B6 "https://github.com/m4nnb3ll/minishell/blob/main/builtins/pwd.c"
    click B7 "https://github.com/m4nnb3ll/minishell/blob/main/builtins/unset.c"

    %% Click Event for Cleanup Module
    click C0 "https://github.com/m4nnb3ll/minishell/blob/main/cleaning/ft_clean_ms.c"

    %% Click Events for Dependencies
    click Libft "https://github.com/m4nnb3ll/minishell/tree/main/libraries/libft"
    click Includes "https://github.com/m4nnb3ll/minishell/tree/main/include"

    %% Styles
    classDef input fill:#cccccc,stroke:#333,stroke-width:2px;
    classDef preexec fill:#a1c4fd,stroke:#333,stroke-width:2px;
    classDef execphase fill:#b9f6ca,stroke:#333,stroke-width:2px;
    classDef dependency fill:#ffe082,stroke:#333,stroke-width:2px;
```
