# cclaude42/minishell architecture

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

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

## Overview

##### Project type and purpose
- This is a C-based “minishell” project (a small bash clone).
- It reads user input, parses it into commands/tokens, handles expansions, redirections and pipes, and either executes built-in functions or launches external executables.

##### File structure
- Top level:
- includes/minishell.h — core data structures and prototypes
- libft/ — custom utility library (strings, memory, printf, etc.)
- srcs/ — implementation broken into submodules
- Makefile, README.md
- srcs subdirectories:
- main/        — shell entry point, signal handling, redirections
- parsing/    — tokenizing, handling quotes, expansions
- exec/       — decide builtin vs external, fork+execve
- builtins/   — implementations of echo, cd, pwd, env, export, unset, exit
- env/        — environment variable management (get_env, sort_env, shlvl)
- tools/      — shared helper functions (free, fd management, parsing helpers)

##### README for additional insights
- Confirms feature set: absolute/relative PATH lookup, pipes (`|`), redirections (`>`, `>>`, `<`), environment variable expansion (`$HOME`, `$?`), builtins, signal handling (`Ctrl-C`, `Ctrl-\`, `Ctrl-D`).

a. Main components
1. CLI Interface
– Reads user input (readline or custom).
2. Parser
– Tokenizer (lexical analysis): splits line into tokens (words, operators).
– Expander: handles `$VAR`, `?`, wildcard you might add.
– Command Builder: groups tokens into command structs (args, redirections, pipes).
3. Execution Engine
– Builtin Dispatcher: checks if command matches builtin list; invokes directly.
– External Executor: sets up pipes/redirections, forks child, calls execve.
4. Environment Manager
– Stores `char **envp`, implements `get_env`, `set_env`, `export`, `unset`, `shlvl`.
5. libft Utility Library
– Provides string, memory, list, I/O, and other helpers used throughout.
6. OS Interface (system calls)
– fork, execve, pipe, dup2, open, close, wait, signal, etc.

b. Relationships and interactions
- User → CLI → Parser
- Parser → (Command struct + redirection descriptors) → Execution Engine
- Execution Engine ↔ Environment Manager (for `env`, `$VAR`, return code `$?`)
- Execution Engine ↔ OS Interface (fork/execve or builtins path)
- All modules call libft routines as needed.

c. Architectural patterns/design principles
- Layered design: input → parsing → execution → OS
- Single-responsibility per module (e.g., `srcs/parsing` only handles parsing)
- Clear separation: builtins vs external commands
- Reusable utilities factored into libft

d. Key technologies/frameworks
- C99
- POSIX system calls
- Custom `libft` (string, memory, I/O)

##### Detailed capture
- How redirections and pipes transform file descriptors in the Executor
- Notes on signal handling hooking into the main loop

## Diagram (Mermaid)

```mermaid
graph TB
    %% User and I/O Layer
    User["User"]:::io
    subgraph "CLI Interface"
        direction TB
        ShellEntry["minishell.c"]:::core
        Signals["signal.c"]:::core
        RedirSetup["redir.c"]:::core
    end
    User -->|"input"| ShellEntry
    ShellEntry --> Signals
    ShellEntry --> RedirSetup

    %% Parsing Layer
    subgraph "Parser"
        direction TB
        Tokenizer["tokens.c"]:::core
        LineReader["line.c"]:::core
        Expander["expansions.c"]:::core
        CmdBuilder["parsing.c"]:::core
        TokenHelpers["token.c"]:::core
        TypeHelpers["type.c"]:::core
    end
    ShellEntry -->|"raw line"| LineReader
    LineReader --> Tokenizer
    Tokenizer --> Expander
    Expander --> CmdBuilder
    CmdBuilder --> TokenHelpers
    TokenHelpers --> TypeHelpers

    %% Execution Layer
    subgraph "Execution Engine"
        direction TB
        BuiltinDispatcher["builtin.c"]:::core
        ExternalExecutor["exec.c"]:::core
        BinHandler["bin.c"]:::core
    end
    CmdBuilder -->|"command struct"| BuiltinDispatcher
    CmdBuilder -->|"command struct"| ExternalExecutor

    %% Builtins
    subgraph "Builtins"
        direction TB
        Echo["echo.c"]:::core
        Cd["cd.c"]:::core
        Pwd["pwd.c"]:::core
        EnvBuilt["env.c"]:::core
        Export["export.c"]:::core
        Unset["unset.c"]:::core
        ExitBuilt["exit.c"]:::core
    end
    BuiltinDispatcher -->|"builtin match"| Echo
    BuiltinDispatcher --> Cd
    BuiltinDispatcher --> Pwd
    BuiltinDispatcher --> EnvBuilt
    BuiltinDispatcher --> Export
    BuiltinDispatcher --> Unset
    BuiltinDispatcher --> ExitBuilt

    %% Environment Manager
    subgraph "Environment Manager"
        direction TB
        EnvCore["env.c"]:::core
        GetEnv["get_env.c"]:::core
        Shlvl["shlvl.c"]:::core
        SortEnv["sort_env.c"]:::core
    end
    BuiltinDispatcher --> EnvCore
    ExternalExecutor --> EnvCore
    EnvCore --> GetEnv
    EnvCore --> Shlvl
    EnvCore --> SortEnv

    %% OS Interface
    OSInterface["OS Interface"]:::io
    ExternalExecutor -->|"fork/execve, pipes, dup2"| OSInterface

    %% Utilities and Definitions
    Libft["libft/"]:::lib
    Header["minishell.h"]:::lib
    ShellEntry --> Header
    Tokenizer --> Header
    CmdBuilder --> Header
    BuiltinDispatcher --> Header
    Libft -.-> ShellEntry
    Libft -.-> Tokenizer
    Libft -.-> CmdBuilder
    Libft -.-> BuiltinDispatcher
    Libft -.-> EnvCore
    Libft -.-> ExternalExecutor

    %% Click Events
    click ShellEntry "https://github.com/cclaude42/minishell/blob/cclaude/srcs/main/minishell.c"
    click Signals "https://github.com/cclaude42/minishell/blob/cclaude/srcs/main/signal.c"
    click RedirSetup "https://github.com/cclaude42/minishell/blob/cclaude/srcs/main/redir.c"
    click Tokenizer "https://github.com/cclaude42/minishell/blob/cclaude/srcs/parsing/tokens.c"
    click LineReader "https://github.com/cclaude42/minishell/blob/cclaude/srcs/parsing/line.c"
    click Expander "https://github.com/cclaude42/minishell/blob/cclaude/srcs/parsing/expansions.c"
    click CmdBuilder "https://github.com/cclaude42/minishell/blob/cclaude/srcs/tools/parsing.c"
    click TokenHelpers "https://github.com/cclaude42/minishell/blob/cclaude/srcs/tools/token.c"
    click TypeHelpers "https://github.com/cclaude42/minishell/blob/cclaude/srcs/tools/type.c"
    click BuiltinDispatcher "https://github.com/cclaude42/minishell/blob/cclaude/srcs/exec/builtin.c"
    click BinHandler "https://github.com/cclaude42/minishell/blob/cclaude/srcs/exec/bin.c"
    click ExternalExecutor "https://github.com/cclaude42/minishell/blob/cclaude/srcs/exec/exec.c"
    click Echo "https://github.com/cclaude42/minishell/blob/cclaude/srcs/builtins/echo.c"
    click Cd "https://github.com/cclaude42/minishell/blob/cclaude/srcs/builtins/cd.c"
    click Pwd "https://github.com/cclaude42/minishell/blob/cclaude/srcs/builtins/pwd.c"
    click EnvBuilt "https://github.com/cclaude42/minishell/blob/cclaude/srcs/builtins/env.c"
    click Export "https://github.com/cclaude42/minishell/blob/cclaude/srcs/builtins/export.c"
    click Unset "https://github.com/cclaude42/minishell/blob/cclaude/srcs/builtins/unset.c"
    click ExitBuilt "https://github.com/cclaude42/minishell/blob/cclaude/srcs/builtins/exit.c"
    click EnvCore "https://github.com/cclaude42/minishell/blob/cclaude/srcs/env/env.c"
    click GetEnv "https://github.com/cclaude42/minishell/blob/cclaude/srcs/env/get_env.c"
    click Shlvl "https://github.com/cclaude42/minishell/blob/cclaude/srcs/env/shlvl.c"
    click SortEnv "https://github.com/cclaude42/minishell/blob/cclaude/srcs/env/sort_env.c"
    click Libft "https://github.com/cclaude42/minishell/tree/cclaude/libft/"
    click Header "https://github.com/cclaude42/minishell/blob/cclaude/includes/minishell.h"

    %% Styles
    classDef io fill:#fde,stroke:#b32,stroke-width:1px
    classDef core fill:#def,stroke:#264,stroke-width:1px
    classDef lib fill:#efd,stroke:#762,stroke-width:1px
```
