# jkylander/codecrafters-shell-c architecture

> Architecture diagram of the GitHub repository jkylander/codecrafters-shell-c, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jkylander/codecrafters-shell-c
- Repository: https://github.com/jkylander/codecrafters-shell-c

## Overview

##### Core Shell Components
- Command Parser/Lexer
- Command Executor
- REPL (Read-Eval-Print Loop)
- Built-in Commands Handler
- External Program Executor

##### System Interaction Layer
- Process Management
- File System Interface
- Environment Variables Handler

##### Component Relationships

a. Input Processing Flow:
- User Input -> REPL -> Command Parser -> Command Executor

b. Execution Branch:
- Command Executor ----|-> Built-in Commands Handler
- > External Program Executor

##### Specific Implementation Details
- Relationship between main.c and the system calls it makes
- Fork/exec pattern for external commands
- Environment variable handling
- File descriptor management

##### Technical Elements to Emphasize
- Process creation and management
- Signal handling
- File descriptor manipulation
- Environment variable management
- Path resolution

**Layer 1: User Interface**
**[REPL/Input Handler]**

**Layer 2: Command Processing**
**[Command Parser/Lexer]**

**Layer 3: Command Execution**
**[Command Executor]**
- [Built-in Commands]
- [External Program Executor]

**Layer 4: System Interface**
**[Process Management] [File System] [Environment]**

##### POSIX compliance aspects by showing
- Standard input/output handling
- Process control mechanisms
- Signal handling

##### Error handling flows
- Command not found
- Permission denied
- Invalid syntax

##### System calls
- fork()
- exec()
- wait()
- chdir()
- getcwd()

Remember to include version information and any specific limitations or assumptions made in the implementation. This will help other developers understand the scope and boundaries of the system.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "User Interface Layer"
        UI[User Input]
        REPL[REPL/Input Handler]:::core
    end

    subgraph "Command Processing Layer"
        Parser[Command Parser/Lexer]:::core
        Validator[Syntax Validator]:::core
    end

    subgraph "Command Execution Layer"
        CmdExec[Command Executor]:::core
        decision{Built-in?}
        BuiltIn[Built-in Commands Handler]:::core
        ExtExec[External Program Executor]:::core
    end

    subgraph "System Interface Layer"
        ProcMgmt[Process Management]:::system
        FileSystem[File System Interface]:::system
        EnvVars[Environment Variables]:::system
    end

    %% Relationships
    UI --> REPL
    REPL --> Parser
    Parser --> Validator
    Validator --> CmdExec
    CmdExec --> decision
    decision -->|Yes| BuiltIn
    decision -->|No| ExtExec
    
    BuiltIn -.->|Interacts| FileSystem
    BuiltIn -.->|Manages| EnvVars
    ExtExec -->|Uses| ProcMgmt
    ProcMgmt -.->|fork/exec| SystemCalls[System Calls]:::system
    
    %% System Call Connections
    ExtExec -.->|execve| SystemCalls
    ProcMgmt -.->|wait| SystemCalls
    FileSystem -.->|chdir/getcwd| SystemCalls

    %% Click Events
    click REPL "https://github.com/jkylander/codecrafters-shell-c/blob/master/app/main.c"
    click Parser "https://github.com/jkylander/codecrafters-shell-c/blob/master/app/main.c"
    click CmdExec "https://github.com/jkylander/codecrafters-shell-c/blob/master/app/main.c"
    click ExtExec "https://github.com/jkylander/codecrafters-shell-c/blob/master/.codecrafters/run.sh"
    click BuiltIn "https://github.com/jkylander/codecrafters-shell-c/blob/master/app/main.c"

    %% Styles
    classDef core fill:#2374ab,stroke:#2374ab,color:white
    classDef system fill:#57a773,stroke:#57a773,color:white
    classDef external fill:#ffc857,stroke:#ffc857,color:black

    %% Legend
    subgraph Legend
        L1[Core Components]:::core
        L2[System Components]:::system
        L3[External Components]:::external
    end
```
