# melvinredondotanis/holbertonschool-shell_v2 architecture

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

- Interactive diagram: https://gitdiagram.com/melvinredondotanis/holbertonschool-shell_v2
- Repository: https://github.com/melvinredondotanis/holbertonschool-shell_v2

## Overview

##### Project type and purpose
- This is a lightweight, Unix-style command-line shell written in C (“holbertonschool-shell_v2”).
- Its purpose is to parse user input, manage environment variables, handle built-in commands (exit, env, setenv, unsetenv), support I/O redirection, pipelines, and execute external programs.

##### File structure
- Top-level C files:
- main.c – bootstrap, argument handling (interactive vs non-interactive)
- interpreter.c – high-level driver for parsing & execution loops
- prompt.c – print/read prompt
- tokenizer.c – split raw input into tokens
- builtins.c / builtins_env.c – implement built-in commands
- *_getenv.c, _setenv.c, etc. – environment variable helpers
- _putenv.c, _realloc.c, etc. – utility functions for string and memory ops
- man_1_hsh – manual page
- hsh.h – global declarations, structs, constants

##### README
- Lists features: interactive/non-interactive mode, heredoc, pipelines, env vars, built-ins, redirection.
- Describes build (Makefile → hsh) and use cases.

##### Components
a. Main logical components
1. CLI Frontend (Prompt & Input Reader)
2. Lexer/Tokenizer
3. Parser / Command Data Structure Builder
4. Execution Engine
- Built-in Command Handler
- External Command Launcher (fork + execve)
5. I/O Redirection & Pipeline Manager
6. Environment Variable Manager
7. Utility Library (string & memory helpers)
8. Underlying OS Services (file descriptors, process control, signals)
b. Relationships & interactions
- User types a command → Prompt reads line → Tokenizer produces tokens → Parser builds command AST/struct → Execution Engine inspects command type
- If built-in → call Built-in Handler → may modify environment via Environment Manager
- Else → fork child → apply I/O redirects/pipes → execve external binary
- For pipelines, Execution Engine coordinates multiple child processes, connecting stdout of one to stdin of next.
- All components may log errors to stderr and check memory via Utility Library.
c. Architectural patterns & design principles
- Modular layering: I/O layer → Lexing → Parsing → Execution
- Single Responsibility: each C module focuses on one aspect
- Separation of concerns: built-ins vs external commands vs environment management
- Reusable utilities for common operations
d. Key technologies/frameworks
- C Standard Library (stdio.h, stdlib.h, string.h)
- POSIX system calls (fork, execve, dup2, pipe, waitpid)
- valgrind for memory checking (development tool)

##### Detailed capture of essential elements
- Don’t overcrowd: keep high-level modules but show sub-components where behavior diverges (built-ins vs external).
- Both interactive (prompt loop) and non-interactive (read from file/stdin) entry points.
- Error handling points (e.g., invalid commands, memory errors) if desired for completeness.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Input Layer"
        CLI["CLI Frontend\n(Prompt & Input Reader)"]:::shell
    end

    subgraph "Lexing"
        Tokenizer["Tokenizer"]:::shell
    end

    subgraph "Parsing"
        Parser["Parser\n(Command Struct Builder)"]:::shell
    end

    subgraph "Execution Engine"
        Exec["Execution Engine"]:::shell
        subgraph "Handlers"
            Builtin["Built-in Command Handler"]:::builtin
            External["External Command Launcher"]:::shell
            Pipeline["Pipeline & I/O Redirection Manager"]:::shell
        end
    end

    subgraph "Environment Manager"
        EnvManager["Environment Variable Manager"]:::builtin
    end

    subgraph "Utility Library"
        Util["Utility Library\n(String & Memory Helpers)"]:::utility
    end

    subgraph "Config & Declarations"
        HSH["Global Declarations & Structs"]:::config
        Build["Build & Documentation"]:::docs
    end

    subgraph "OS Services"
        OS["OS Kernel/Services"]:::os
    end

    CLI -->|"raw input"| Tokenizer
    Tokenizer -->|"tokens"| Parser
    Parser -->|"command struct"| Exec

    Exec -->|"built-in command"| Builtin
    Exec -->|"external command"| External
    Exec -->|"pipeline orchestration"| Pipeline

    Builtin -->|"modify/read vars"| EnvManager
    EnvManager -->|"env data"| Exec

    External -->|"fork/execve calls"| OS
    Pipeline -->|"pipe & dup2"| OS

    Exec -->|"errors/status"| CLI

    CLI -.-> Build

    click CLI "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/main.c"
    click CLI "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/prompt.c"
    click Tokenizer "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/tokenizer.c"
    click Parser "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/interpreter.c"
    click Exec "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/interpreter.c"
    click Builtin "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/builtins.c"
    click Builtin "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/builtins_env.c"
    click External "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/interpreter.c"
    click Pipeline "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/interpreter.c"
    click EnvManager "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/_getenv.c"
    click EnvManager "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/_setenv.c"
    click EnvManager "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/_putenv.c"
    click Util "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/_realloc.c"
    click Util "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/_isspace.c"
    click Util "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/_strcat.c"
    click Util "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/_strchr.c"
    click Util "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/_strcmp.c"
    click Util "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/_strcpy.c"
    click Util "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/_strdup.c"
    click Util "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/_strlen.c"
    click Util "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/_strncmp.c"
    click Util "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/_strtok_r.c"
    click Util "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/_strtol.c"
    click HSH "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/hsh.h"
    click Build "https://github.com/melvinredondotanis/holbertonschool-shell_v2/tree/main/Makefile"
    click Build "https://github.com/melvinredondotanis/holbertonschool-shell_v2/blob/main/README.md"
    click Build "https://github.com/melvinredondotanis/holbertonschool-shell_v2/tree/main/man_1_hsh"

    class CLI,Tokenizer,Parser,Exec shell
    class Builtin,EnvManager builtin
    class Util utility
    class OS os
    class HSH config
    class Build docs

    classDef shell fill:#ccffcc,stroke:#333,stroke-width:1px
    classDef builtin fill:#cceeff,stroke:#333,stroke-width:1px
    classDef utility fill:#e0e0ff,stroke:#333,stroke-width:1px
    classDef os fill:#ffcc99,stroke:#333,stroke-width:1px
    classDef docs fill:#f0f0f0,stroke:#333,stroke-width:1px
    classDef config fill:#ffffcc,stroke:#333,stroke-width:1px
```
