# boughatat911/minishell architecture

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

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

## Overview

##### Project Type & Purpose
- Minishell is a standalone, minimal shell interpreter (a command-line interface tool) written in C.
- Purpose: read user commands, parse/tokenize them, handle built-ins and external binaries with redirections, pipes, environment management, and process control.

##### File-Structure Analysis
- Top-Level:
- minishell.c / minishell.h – entry point and shared types
- Makefile – build config
- builtins/ – each builtin (echo, cd, pwd, export, unset, env, exit)
- parsing/ – three submodules:
– token/ (lexical analysis & syntax error detection)
– expand/ (environment‐variable and quote expansion)
– cmd/ (building a linked list of t_cmd structures, including heredoc & redirection logic)
- execution/ – orchestrates fork/exec, pipes, builtin dispatch, IO setup
- tools/ – utilities (custom ft_malloc with leak detection, string helpers)

##### README-Derived Insights
- Core modules: CLI loop, signal handling (SIGINT), memory management, error handling.
- Data structures: t_shell (global state), t_token (lexer output), t_cmd (command pipeline).
- External dependency: readline library for line editing & history.

##### Main Components
A. CLI & Main Loop
- readline() → raw_input
- signal handlers installed/managed
B. Parser Layer
- Tokenizer (parsing/token) → list of t_token
- Syntax checker
- Expander (parsing/expand) → tokens with variables & quotes processed
- Command assembler (parsing/cmd) → linked list of t_cmd, each with I/O fds and args
C. Execution Layer
- Builtin dispatcher (execution/builtins.c)
- External exec (fork + execve in execution/exec.c)
- Pipe-chain handler (setting up pipe_fd between t_cmd nodes)
- I/O redirection setup per t_cmd (in, out fields)
- Signal restoration in children
D. Environment Manager
- t_env linked list (builtins/add_env, my_env, export, unset, etc.)
- PATH resolution (execution/check_path_cmd)
E. Memory & Tools
- ft_malloc wrapper & leak detection (tools/ft_malloc.c)
- general utilities for strings, lists, error messages

##### Component Interactions & Data Flow
- User types command → CLI reads with readline → passes to Parser
- Parser emits token stream → expand variables/quotes → build t_cmd list
- Execution module inspects each t_cmd:
- If builtin → call builtin implementation (cd, export, etc.)
- Else → resolve PATH, fork child, set up redirections & pipes, execve
- Parent collects child PIDs & statuses, updates t_shell.exit_status
- Builtins update t_env structure in-process
- Loop repeats until exit

##### Architectural Patterns & Principles
- Modular layering: CLI → Parser → Executor → OS/procs
- Single responsibility per directory
- Linked data structures to represent streams (token, cmd, env)
- Custom memory management to track allocations & leaks
- Signal‐safe handling and restoration in children

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "CLI & Signals"
        CLI["CLI & Signals"]:::core
    end
    Readline["readline lib"]:::external -->|"read input"| CLI
    CLI -->|"char* input"| Tokenizer

    subgraph "Parser Layer"
        Tokenizer["Tokenizer"]:::parser
        Expander["Expander"]:::parser
        CmdBuilder["Command Builder"]:::parser
    end
    Tokenizer -->|"t_token*"| Expander
    Expander -->|"t_token*"| CmdBuilder
    CmdBuilder -->|"t_cmd*"| ExecOrch

    subgraph "Execution Layer"
        ExecOrch["Executor Orchestrator"]:::execution
        BuiltinDisp["Builtin Dispatcher"]:::builtin
        ExtExec["External Exec"]:::execution
        PATHRes["PATH Resolver"]:::execution
    end
    ExecOrch -->|"t_cmd*"| BuiltinDisp
    ExecOrch -->|"t_cmd*"| ExtExec
    ExtExec -->|"fork/execve"| OS["OS Kernel"]:::external
    ExtExec -->|"command path"| PATHRes
    PATHRes -->|"resolved path"| ExtExec

    subgraph "Environment Manager"
        EnvMgr["Env Manager"]:::builtin
    end
    BuiltinDisp -->|"update t_env"| EnvMgr
    EnvMgr -->|"t_env data"| BuiltinDisp

    subgraph "Tools & Memory"
        Tools["Memory & Tools"]:::tools
    end
    Tools --> CLI
    Tools --> Tokenizer
    Tools --> ExecOrch

    subgraph "Project Files"
        Makefile["Makefile"]:::core
        Doc1["README.md"]:::core
        Doc2["Resume.md"]:::core
    end
    Makefile -->|build| CLI
    Makefile -->|build| ExecOrch

    classDef parser fill:#ccffcc,stroke:#333,stroke-width:1px;
    classDef execution fill:#cce5ff,stroke:#333,stroke-width:1px;
    classDef builtin fill:#ffffcc,stroke:#333,stroke-width:1px;
    classDef tools fill:#eeeeee,stroke:#333,stroke-width:1px;
    classDef external fill:#ffcccc,stroke:#333,stroke-width:1px;
    classDef core fill:#ffffff,stroke:#333,stroke-width:1px;

    click CLI "https://github.com/boughatat911/minishell/blob/main/minishell.c"
    click Tokenizer "https://github.com/boughatat911/minishell/blob/main/parsing/token/token.c"
    click Expander "https://github.com/boughatat911/minishell/blob/main/parsing/expand/expand.c"
    click CmdBuilder "https://github.com/boughatat911/minishell/blob/main/parsing/cmd/cmd.c"
    click ExecOrch "https://github.com/boughatat911/minishell/blob/main/execution/execution.c"
    click BuiltinDisp "https://github.com/boughatat911/minishell/blob/main/execution/builtins.c"
    click ExtExec "https://github.com/boughatat911/minishell/blob/main/execution/exec.c"
    click PATHRes "https://github.com/boughatat911/minishell/blob/main/execution/check_path_cmd.c"
    click EnvMgr "https://github.com/boughatat911/minishell/blob/main/builtins/my_env.c"
    click Tools "https://github.com/boughatat911/minishell/blob/main/tools/ft_malloc.c"
    click Makefile "https://github.com/boughatat911/minishell/tree/main/Makefile"
    click Doc1 "https://github.com/boughatat911/minishell/blob/main/README.md"
    click Doc2 "https://github.com/boughatat911/minishell/blob/main/Resume.md"
```
