# f0lio/minishell architecture

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

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

## Overview

##### Main Components
- Main Shell Loop – At the heart of the application is the “minishell.c” file within the src directory. This file represents the entry point where the shell receives user input and manages the high-level flow.
- Tokenizer – The tokenization phase is handled by the files in the “src/tokenizer” directory (e.g., tokenize_quoted.c, get_token.c, etc.). This module breaks down the user input into meaningful tokens that subsequent processes will use.
- Syntax Analyser – Located in “src/syntax_analyser”, this component verifies that the tokenized commands follow valid syntax before execution.
- Expansion Module – Found in “src/expansion”, these files manage the expansion of environment variables and handle quoted strings, ensuring that tokens are correctly interpreted.
- Execution Subsystem – Under “src/execution”, various files implement built-in commands (like echo, export, unset, etc.), command execution (using execve), piping, redirection, and error handling. This is the operational core that actually interfaces with the operating system.
- Constructors/Environment Management – The “src/constructors” portion (e.g., env.c) is likely involved in setting up the environment structures used across the shell.
- Utility Functions – In “src/utils”, numerous helper functions exist to manage signals, error handling, string manipulation, and more.
- Support Files – The “include” directory contains the public headers defining shared functions, macros, structures, and interfaces across modules.

##### Relationships and Interactions
- User Input Flow: The shell (minishell.c) reads user input (perhaps via terminal), sending it first to the Tokenizer.
- Tokenizer → Syntax Analyser: Once input is broken into tokens, the syntax analyser validates command structure.
- Syntax Analyser → Expansion: Valid tokens are then passed to the Expansion module where environment variables are expanded.
- Expansion → Execution: The fully processed commands are executed by the Execution subsystem by forking new processes, invoking built-ins, managing redirections, executing external commands, and handling pipes.
- Support/Data Sharing: The Constructors module prepares environment data, and the Util functions support error/signal handling across modules.

##### Architectural Patterns or Design Principles
- Modular Structure: The clear separation between tokenization, syntax analysis, expansion, and execution shows a layered design common in interpreters/shells.
- Separation of Concerns: Each directory focuses on a specific aspect of the shell’s functionality.
- Process Management and IPC: The execution modules emphasize handling processes (fork, execve), pipes, redirections, and signals.

##### Relevant Technologies/Frameworks
- C standard libraries and allowed functions (e.g., malloc, free, fork, pipe, execve, dup2) are in heavy use.
- The use of Makefile and Dockerfile suggests that the project supports both local builds and containerized environments.
- External documentation references (like the provided PDFs and websites) highlight design inspirations and allowed system calls.

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["User Input (Terminal)"]:::external
    B["Minishell Main Loop"]:::core
    C["Tokenizer"]:::processing
    D["Syntax Analyser"]:::processing
    E["Expansion Module"]:::processing
    F["Execution Subsystem"]:::core

    subgraph "Support Modules"
        G["Constructors/Environment Management"]:::support
        H["Utility Functions"]:::support
        I["Support Files (Public Headers)"]:::support
    end

    %% Main pipeline connections
    A -->|"reads_input"| B
    B -->|"token_stream"| C
    C -->|"syntax_analysis"| D
    D -->|"expand_tokens"| E
    E -->|"exec_command"| F

    %% Support modules connections (dashed lines indicate cross-cutting concerns)
    H ---|"error/signal_handling"| B
    H ---|"error/signal_handling"| C
    H ---|"error/signal_handling"| D
    H ---|"error/signal_handling"| E
    H ---|"error/signal_handling"| F

    G ---|"env_initialization"| B
    G ---|"env_provisioning"| F

    I ---|"common_definitions"| B
    I ---|"common_definitions"| C
    I ---|"common_definitions"| D
    I ---|"common_definitions"| E
    I ---|"common_definitions"| F

    %% Click Events for components
    click B "https://github.com/f0lio/minishell/blob/main/src/minishell.c"
    click C "https://github.com/f0lio/minishell/tree/main/src/tokenizer"
    click D "https://github.com/f0lio/minishell/tree/main/src/syntax_analyser"
    click E "https://github.com/f0lio/minishell/tree/main/src/expansion"
    click F "https://github.com/f0lio/minishell/tree/main/src/execution"
    click G "https://github.com/f0lio/minishell/blob/main/src/constructors/env.c"
    click H "https://github.com/f0lio/minishell/tree/main/src/utils"
    click I "https://github.com/f0lio/minishell/tree/main/include"

    %% Styles
    classDef core fill:#ffcccc,stroke:#800000,stroke-width:2px;
    classDef processing fill:#cce5ff,stroke:#003366,stroke-width:2px;
    classDef support fill:#ccffcc,stroke:#006600,stroke-width:2px;
    classDef external fill:#ffffcc,stroke:#999900,stroke-width:2px;
```
