# 0xysan/minishell architecture

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

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

## Overview

This project is a custom shell – a “minishell” – implemented for the 42 Cursus. Its purpose is to provide a command-line interpreter with command execution, built-in commands (like cd, echo, exit, etc.), environment manipulation, support for pipes and redirection, and proper signal handling.

##### Project Type & Purpose
- It as a command line shell (minishell) where the user inputs commands, which are parsed, executed (with support for built-in commands and external binaries), and managed with proper I/O redirection and signals.
- The README confirms features such as command execution with arguments, piping/redirection, environment handling, and signal management.

##### Main Components
a. Main Program & Core Loop:
- It initiating the main loop: reading user input, processing the commands, and dispatching tasks.

b. Command Parsing & Preprocessing:
- "process_input" directory that contains modules (handles.c, preprocess_input.c, preprocess_utils.c) responsible for analyzing and tokenizing the user’s command input.

c. Command Execution:
- "cmds_exec" directory which implements functions to execute parsed commands using functions found in cmds_execute.c, cmds_handler.c, exec_input.c, and exec_utils.c.
- Also, include the "ft_cmds" directory where built-in commands are defined (e.g., ft_cd.c, ft_echo.c, ft_exit.c, ft_export.c).

d. Environment Variable Management:

e. I/O and Formatting Utilities:
- "ft_dprintf" directory for custom formatted output routines, and the "libft" directory which provides a set of common utility functions used throughout the project.

f. Pipes, File Descriptors & Redirection Support:

g. Signal Handling:
- "sig_n_utils" module as a separate component that encapsulates the signal handling logic (sighandler.c, utils.c, utils2.c).

##### Relationships & Interactions
- The Main Program (main.c) should be connected to the Command Parsing module ("process_input"), indicating the control flow where raw user input is processed.
- Parsed commands are then dispatched to the Command Execution modules ("cmds_exec" for external command execution and "ft_cmds" for built-in functions).
- Environment management ("ft_env") interacts with built-in commands and command execution for preserving system state.
- The pipes and file descriptor module ("pipes_n_fds") interacts with command execution, especially for handling inter-process communication when piping is involved.
- The signal handling component ("sig_n_utils") overlays atop the overall flow, capturing signals (like Ctrl+C/Ctrl+D) and relaying them to the main loop or command execution as needed.

##### Architectural Patterns & Design Principles
- The separation of concerns is evident: input processing, command execution, environment handling, and signal management are all isolated.
- Use of modular design is highlighted by splitting functionality into dedicated directories.
- Dependency management – common libraries (libft and ft_dprintf) are abstracted to avoid redundancy.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Core Flow Subgraph
    subgraph "Core Flow"
        A["Main Loop (main.c)"]:::core
        B["Input Parsing (process_input)"]:::core
        C["External Command Execution (cmds_exec)"]:::core
        D["Built-in Commands (ft_cmds)"]:::core
        E["Environment Manager (ft_env)"]:::core
        F["Pipes & FDs (pipes_n_fds)"]:::core
    end

    %% Signal Handling and Utilities
    I["Signal Handler (sig_n_utils)"]:::signal
    G["I/O & Formatting Utilities (ft_dprintf)"]:::util
    H["Utility Library (libft)"]:::util

    %% Connections - Core Flow
    I -->|"affects"| A
    A -->|"reads_input"| B
    B -->|"dispatches"| C
    B -->|"dispatches"| D
    C -->|"handles_pipes"| F
    F -->|"supports"| C
    C -->|"requires_env"| E
    D -->|"requires_env"| E

    %% Utilities Dependencies
    G -->|"supports"| A
    G -->|"supports"| B
    G -->|"supports"| C
    G -->|"supports"| D
    H -->|"provides_lib"| A
    H -->|"provides_lib"| B
    H -->|"provides_lib"| C
    H -->|"provides_lib"| D
    H -->|"provides_lib"| E
    H -->|"provides_lib"| F

    %% Click Events
    click A "https://github.com/0xysan/minishell/blob/main/main.c"
    click B "https://github.com/0xysan/minishell/tree/main/process_input"
    click C "https://github.com/0xysan/minishell/tree/main/cmds_exec"
    click D "https://github.com/0xysan/minishell/tree/main/ft_cmds"
    click E "https://github.com/0xysan/minishell/tree/main/ft_env"
    click F "https://github.com/0xysan/minishell/tree/main/pipes_n_fds"
    click I "https://github.com/0xysan/minishell/tree/main/sig_n_utils"
    click G "https://github.com/0xysan/minishell/tree/main/ft_dprintf"
    click H "https://github.com/0xysan/minishell/tree/main/libft"

    %% Styles
    classDef core fill:#CCEEFF,stroke:#003366,stroke-width:2px;
    classDef signal fill:#FFDDCC,stroke:#CC0000,stroke-width:2px;
    classDef util fill:#DFFFD6,stroke:#228B22,stroke-width:2px;
```
