# ohssineabdelfatah/42_minishell architecture

> Architecture diagram of the GitHub repository ohssineabdelfatah/42_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/ohssineabdelfatah/42_minishell
- Repository: https://github.com/ohssineabdelfatah/42_minishell

## Overview

##### Main Components
- Command Input & Terminal Interface – Represents the interactive layer where the user types commands.
- Parser – Located in the sources/parse directory. Its job is to tokenize the input and validate the syntax.
- Expander – Found in sources/expander. This module handles variable expansion (translating environment variables to their values) and processes heredoc.
- Builtin Commands – In sources/builtins. Implements built-in functionalities like cd, pwd, env, export, unset, and exit.
- Execution Manager – Modules in sources/execution deal with running commands, managing pipes, redirections (sources/redirection_things), and handling signals (e.g., Ctrl+C or Ctrl+D).
- Error Handler – Located in sources/errors. Ensures that errors during command parsing and execution produce readable, accurate messages.
- Utility Libraries – The project uses external/shared libraries:
- Libft (lib/libft): Contains utility functions common to many 42 projects.
- Get-Next-Line (lib/gnl0): For reading input streams.
- Memory & Resource Cleanup – Sources/freedom provides functions to free resources and manage deallocation.

##### Relationships and Interactions
- Input flows from the terminal into the Command Input module.
- The Parser reads the raw input, tokenizes it, and hands over structured commands to the Expander.
- The Expander resolves environment variables and expands tokens related to heredoc.
- If a command is a builtin (redirected to the Builtins module)
- Or if it should be executed as an external command (using system calls such as fork/exec, with pipe/redirection processing).
- The Error Handler monitors all interactions throughout the process and ensures appropriate error messages are generated.
- Utility libraries support various functions across modules (e.g., string operations, memory management, file reading).

##### Architectural Patterns and Design Principles
- Modular Design: The project is clearly modularized—the separation into directories (parse, expander, builtins, execution, redirection, etc.) shows a strong separation of concerns.
- Layered Processing Pipeline: Input is processed in distinct stages (parsing → expansion → execution) which helps isolate functionality.
- Reuse of Common Libraries: The use of Libft and Get-Next-Line underlines a design emphasis on leveraging shared utility code.
- Robust Error and Signal Handling: There are dedicated modules for error handling and signal processing, indicating attention to process management and system-level robustness.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Input Layer
    Terminal("Command Input & Terminal Interface"):::module

    %% Processing Pipeline
    Parser("Parser"):::module
    Expander("Expander"):::module

    %% Command Handling
    Builtin("Builtin Commands"):::module
    subgraph "Execution Manager"
        Exec("Command Execution"):::module
        Redir("Redirections & Pipes"):::module
        Signal("Signal Handling"):::module
    end

    %% Error Handling and Cleanup
    Error("Error Handler"):::error
    Cleanup("Memory & Resource Cleanup"):::module

    %% Utility Libraries
    Libft("Libft"):::utility
    GNL("Get-Next-Line"):::utility

    %% Data/Control Flow
    Terminal -->|"raw_input"| Parser
    Parser -->|"tokenized_input"| Expander
    Expander -->|"builtin_cmd"| Builtin
    Expander -->|"external_cmd"| Exec

    Builtin -->|"post_process"| Cleanup
    Exec -->|"initiate_redirection"| Redir
    Exec -->|"initiate_signal_handling"| Signal
    Redir -->|"io_management"| Error
    Signal -->|"signal_events"| Error
    Error -->|"error_cleanup"| Cleanup

    %% Utility Associations
    Parser ---|"uses"| Libft
    Expander ---|"uses"| Libft
    Builtin ---|"uses"| Libft
    Exec ---|"uses"| Libft
    Parser ---|"reads_input"| GNL

    %% Styling Classes
    classDef module fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef error fill:#FFB6C1,stroke:#000,stroke-width:2px;
    classDef utility fill:#90EE90,stroke:#000,stroke-width:2px;

    %% Click Events
    click Terminal "https://github.com/ohssineabdelfatah/42_minishell/blob/main/sources/one.c"
    click Terminal "https://github.com/ohssineabdelfatah/42_minishell/blob/main/sources/two.c"
    click Parser "https://github.com/ohssineabdelfatah/42_minishell/tree/main/sources/parse"
    click Expander "https://github.com/ohssineabdelfatah/42_minishell/tree/main/sources/expander"
    click Builtin "https://github.com/ohssineabdelfatah/42_minishell/tree/main/sources/builtins"
    click Exec "https://github.com/ohssineabdelfatah/42_minishell/tree/main/sources/execution"
    click Redir "https://github.com/ohssineabdelfatah/42_minishell/tree/main/sources/redirection_things"
    click Signal "https://github.com/ohssineabdelfatah/42_minishell/blob/main/sources/execution/signals.c"
    click Error "https://github.com/ohssineabdelfatah/42_minishell/tree/main/sources/errors"
    click Libft "https://github.com/ohssineabdelfatah/42_minishell/tree/main/lib/libft"
    click GNL "https://github.com/ohssineabdelfatah/42_minishell/tree/main/lib/gnl0"
    click Cleanup "https://github.com/ohssineabdelfatah/42_minishell/tree/main/sources/freedom"
```
