# ddosoos/mini_shell architecture

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

- Interactive diagram: https://gitdiagram.com/ddosoos/mini_shell
- Repository: https://github.com/ddosoos/mini_shell

## Overview

##### Project Type and Purpose
- This is a monolithic C-language shell implementation (a command-line interpreter).
- Purpose: read user commands (interactive or piped), parse them into a binary-tree AST, and execute them (either built-in functions or external programs) with support for pipes, redirections, logical operators, wildcards, and here-docs.

##### Main Components
a. Input Handling
- get_next_line (GNL library) – reads lines from stdin or piped input
- Mode Handler – distinguishes interactive vs. non-interactive mode
b. Lexical Analysis (Lexer)
- Tokenizer – splits raw input into tokens (words, operators, quotes)
- Syntax Checker – validates token sequence
c. Parsing (Parser)
- AST Builder – uses a binary tree structure to represent command precedence, pipes, redirections, logical operators, parentheses
d. Execution Engine
- Executor – traverses the AST, forks processes, sets up pipes/redirections
- Built-ins Handler – implements echo, cd, pwd, export, unset, env, exit, history
- External Command Runner – invokes execve for non-built-in commands
- Environment Manager – tracks and manipulates environment variables
- I/O Manager – opens/closes files for `<`, `>`, `>>`, here-docs
- Signal Handler – intercepts Ctrl-C, Ctrl-D, etc.
e. Utility Libraries
- libft – basic C routines (strings, memory, linked lists)
- ft_printf – formatted output
f. Build and Configuration
- Makefile – compiles GNL, libft, ft_printf, and links into `minishell`
- .gitignore, subject PDF (for reference)

##### Relationships & Data Flow
- User/Script → GNL → Lexer → Parser → AST
- AST → Executor →
- if built-in: Built-ins Handler
- else: fork + execve (External Runner)
- Executor ↔ Environment Manager (for `$VAR`, export/unset)
- Executor ↔ I/O Manager (for redirections, here-docs)
- Executor ↔ Signal Handler (for interactive interrupts)
- Executor ↔ Pipes (connect stdout of one process to stdin of next)
- Executor → Output (stdout/stderr to terminal or pipe)

##### Architectural Patterns & Principles
- Modular layering: Input → Lexing → Parsing → Execution
- AST (binary tree) for operator precedence and grouping
- Separation of concerns: built-ins vs. external commands, environment vs. I/O
- Reuse of custom utility libraries (GNL, libft, ft_printf)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Input Layer
    subgraph "Input Handling"
        direction TB
        GNL["get_next_line (GNL)"]:::frontend
        ModeHandler["Mode Handler"]:::frontend
    end

    %% Lexical Analysis Layer
    subgraph "Lexical Analysis"
        direction TB
        Tokenizer["Tokenizer"]:::frontend
        SyntaxChecker["Syntax Checker"]:::frontend
    end

    %% Parsing Layer
    subgraph "Parsing / AST"
        direction TB
        ParserAST["Parser / AST Builder"]:::frontend
    end

    %% Execution Layer
    subgraph "Execution Engine"
        direction TB
        Executor["Executor"]:::backend
        BuiltIns["Built-ins Handler"]:::backend
        ExternalRunner["External Command Runner"]:::backend
        EnvManager["Environment Manager"]:::backend
        IOManager["I/O Manager"]:::sys
        SignalHandler["Signal Handler"]:::sys
        HistoryManager["History Manager"]:::backend
    end

    %% Utilities
    subgraph "Utility Libraries"
        direction TB
        Libft["libft"]:::util
        FtPrintf["ft_printf"]:::util
    end

    %% Build & Config
    Makefile["Makefile"]:::util

    %% Flow
    GNL -->|"reads input"| ModeHandler
    ModeHandler -->|text| Tokenizer
    Tokenizer -->|tokens| SyntaxChecker
    SyntaxChecker -->|validated tokens| ParserAST
    ParserAST -->|AST| Executor
    Executor -->|built-in?| Decision{Built-in?}
    Decision -->|yes| BuiltIns
    Decision -->|no| ExternalRunner
    Executor -->|manage vars| EnvManager
    Executor -->|manage I/O| IOManager
    Executor -->|handle signals| SignalHandler
    Executor -->|history| HistoryManager
    BuiltIns -->|output| Executor
    ExternalRunner -->|output| Executor
    Executor -->|stdout/stderr| Output((Terminal))
    Libft --> ParserAST
    Libft --> Executor
    FtPrintf --> BuiltIns
    FtPrintf --> Executor
    Makefile --> Libft
    Makefile --> FtPrintf
    Makefile --> GNL
    Makefile --> ParserAST
    Makefile --> Executor

    %% Click Events
    click GNL "https://github.com/ddosoos/mini_shell/tree/main/includes/gnl"
    click ModeHandler "https://github.com/ddosoos/mini_shell/blob/main/src/execution/readline_tty.c"
    click Tokenizer "https://github.com/ddosoos/mini_shell/tree/main/src/parsing/tokenizer"
    click SyntaxChecker "https://github.com/ddosoos/mini_shell/tree/main/src/parsing/lexer"
    click ParserAST "https://github.com/ddosoos/mini_shell/tree/main/src/parsing/parser"
    click Executor "https://github.com/ddosoos/mini_shell/blob/main/src/execution/execute.c"
    click BuiltIns "https://github.com/ddosoos/mini_shell/tree/main/src/execution/builtins"
    click ExternalRunner "https://github.com/ddosoos/mini_shell/blob/main/src/execution/cmd_runner.c"
    click EnvManager "https://github.com/ddosoos/mini_shell/blob/main/src/execution/environ.c"
    click IOManager "https://github.com/ddosoos/mini_shell/blob/main/src/execution/redirections.c"
    click SignalHandler "https://github.com/ddosoos/mini_shell/blob/main/src/execution/signals.c"
    click HistoryManager "https://github.com/ddosoos/mini_shell/blob/main/src/execution/history.c"
    click Libft "https://github.com/ddosoos/mini_shell/tree/main/includes/libft"
    click FtPrintf "https://github.com/ddosoos/mini_shell/tree/main/includes/printf"
    click Makefile "https://github.com/ddosoos/mini_shell/tree/main/makefile"

    %% Styles
    classDef frontend fill:#caffca,stroke:#2f8f2f,color:#000
    classDef backend fill:#aaddff,stroke:#2f4f8f,color:#000
    classDef util fill:#fff0b3,stroke:#8f7f2f,color:#000
    classDef sys fill:#ffb3b3,stroke:#8f2f2f,color:#000
```
