# legenduzo/simple_shell architecture

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

- Interactive diagram: https://gitdiagram.com/legenduzo/simple_shell
- Repository: https://github.com/legenduzo/simple_shell

## Overview

##### Project type and purpose
- Type: A CLI (“full‐stack” in the sense of user→shell, but really a single monolithic C program).
- Purpose: Read user input, parse it, resolve commands (built‐in vs external), execute, loop.

##### File structure
- Top‐level C modules:
– main.c           → entry point
– loop_shell.c    → the REPL loop
– manage_cmd.c    → command‐handling & execution
– where.c         → PATH lookup
– exctract_dir_from_string.c, check_dir.c → directory/executable checks
– split_string.c, check_spaces.c, check_char.c → string parsing
– string_manage.c → strcpy/strcat/strlen/strncmp clones
– free_split.c    → memory cleanup for split results
– env.c           → environment‐variable helpers
- shell.h           → prototypes & shared headers

##### README insights
- Describes primary features: parsing, argument support, PATH resolution, built‐in vs external commands.

##### Main components to model
A. User Interface (stdin/stdout)
B. Shell Core
1. REPL Loop
2. Input Reader
3. Parser/Tokenizer
4. Command Manager
– Built‐in handler (e.g., “cd”, “exit”)
– External command resolver (uses where.c, PATH env)
5. Executor (fork + execve + wait)
C. Utility Modules
– String utilities (split_string, string_manage, check_spaces, check_char)
– Memory manager (free_split)
– Env helper (env.c)
D. OS Interactions
– File system (check_dir, exctract_dir_from_string)
– Process control syscalls (fork, execve, wait)

##### Relationships & data flow
1. User types a line → REPL loop (loop_shell.c)
2. Loop calls input reader (main.c or loop_shell.c)
3. Raw input → Parser/Tokenizer (split_string.c + check_spaces.c) → array of tokens
4. Tokens → Command Manager
- If built-in: handle inline
- Else: call where.c to locate binary on PATH
5. Command Manager → Executor
- fork() → child execve() → parent wait()
6. After execution → back to REPL loop
7. Utilities invoked throughout for memory management and string ops

##### Architectural patterns & design principles
- Modular C files each implement one responsibility
- Clear separation: parsing, resolution, execution, utilities
- Linear, synchronous REPL (no concurrency beyond fork/exec)

##### Technologies & frameworks
- Language: C (POSIX)
- Syscalls: fork, execve, wait, open/stat (via check_dir)
- No external libraries beyond standard libc

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Layer 1: User I/O"
        UI["User Interface\n(stdin/stdout)"]:::core
    end

    subgraph "Layer 2: Core Shell Logic"
        REPL["REPL Loop\n(loop_shell.c)"]:::core
        Parser["Parser/Tokenizer\n(split_string.c)"]:::core
        CmdMgr["Command Manager\n(manage_cmd.c)"]:::core
        Exec["Executor\n(fork+execve+wait)"]:::core
    end

    subgraph "Layer 3: Utilities"
        StringUtil["String Utilities\n(string_manage.c)"]:::utilities
        Whitespace["Whitespace Checker\n(check_spaces.c)"]:::utilities
        CharCheck["Character Checker\n(check_char.c)"]:::utilities
        MemCleanup["Memory Cleanup\n(free_split.c)"]:::utilities
        EnvHelper["Env Helpers\n(env.c)"]:::utilities
        DirCheck["Dir/Exec Checks\n(check_dir.c)"]:::utilities
        DirExtract["Dir Extraction\n(exctract_dir_from_string.c)"]:::utilities
    end

    subgraph "Layer 4: OS / External"
        PATHVar["PATH Env Variable"]:::os
        Syscalls["Syscalls\n(fork, execve, wait, open/stat)"]:::os
    end

    UI -->|types command| REPL
    REPL -->|reads input| Parser
    Parser -->|tokens| CmdMgr
    CmdMgr -->|fork & exec| Exec
    Exec -->|system calls| Syscalls
    CmdMgr -->|resolve external| DirExtract
    DirExtract -->|check path| DirCheck
    DirCheck -->|lookup binary| PATHVar
    PATHVar -->|provides path| DirExtract

    %% Utilities usage
    Parser -->|uses| StringUtil
    Parser -->|uses| Whitespace
    Parser -->|uses| CharCheck
    CmdMgr -->|uses| EnvHelper
    Parser -->|cleanup| MemCleanup

    %% Click events
    click UI "https://github.com/legenduzo/simple_shell/blob/master/main.c"
    click REPL "https://github.com/legenduzo/simple_shell/blob/master/loop_shell.c"
    click Parser "https://github.com/legenduzo/simple_shell/blob/master/split_string.c"
    click Whitespace "https://github.com/legenduzo/simple_shell/blob/master/check_spaces.c"
    click CharCheck "https://github.com/legenduzo/simple_shell/blob/master/check_char.c"
    click CmdMgr "https://github.com/legenduzo/simple_shell/blob/master/manage_cmd.c"
    click Exec "https://github.com/legenduzo/simple_shell/blob/master/manage_cmd.c"
    click StringUtil "https://github.com/legenduzo/simple_shell/blob/master/string_manage.c"
    click MemCleanup "https://github.com/legenduzo/simple_shell/blob/master/free_split.c"
    click EnvHelper "https://github.com/legenduzo/simple_shell/blob/master/env.c"
    click DirCheck "https://github.com/legenduzo/simple_shell/blob/master/check_dir.c"
    click DirExtract "https://github.com/legenduzo/simple_shell/blob/master/exctract_dir_from_string.c"
    click ExternalResolver "https://github.com/legenduzo/simple_shell/blob/master/where.c"
    click ShellHeader "https://github.com/legenduzo/simple_shell/blob/master/shell.h"

    classDef core fill:#f9f,stroke:#333,stroke-width:1px
    classDef utilities fill:#9f9,stroke:#333,stroke-width:1px
    classDef os fill:#99f,stroke:#333,stroke-width:1px
```
