# simoxeur/pipex architecture

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

- Interactive diagram: https://gitdiagram.com/simoxeur/pipex
- Repository: https://github.com/simoxeur/pipex

## Overview

##### Project Type and Purpose
- This is a small, monolithic C command‐line tool (“pipex”) whose sole purpose is to emulate UNIX shell pipelines and redirections (pipe, fork, dup2, execve).
- Mandatory part handles a two‐command pipeline; bonus part extends to N commands and “here_doc” mode using get_next_line.

##### High‐Level Architecture
At a glance, the system consists of:
- CLI Layer: Parses arguments (`main.c`)
- Pipeline Manager: Builds one or more pipes, handles “here_doc” logic (`pipex.h`, `execution.c`, `ft_fork.c`, `here_doc_bonus.c`)
- Execution Engine: Spawns child processes with proper file descriptors and runs commands via `execve`
- Utility Library: String manipulation, error handling, array management (`utils/*.c`, `utils.h`)
- I/O Helper (Bonus): Line reading for here‐document functionality (`get_next_line_bonus.c`)
- OS Kernel (External): Underlying pipe/fork/dup2/execve system calls and spawned command binaries

##### Components and Their Relationships
A. User CLI Input
→ passes: `file1`, `[cmd1 … cmdn]`, `file2` or `here_doc LIMITER cmd… file`
B. Argument Parser (main.c)
→ validates args, selects mandatory vs bonus flow
C. Pipeline Builder / Controller
→ for each command:
- Creates a new pipe (pipe())
- Duplicates fds (dup2)
- Optionally reads limiter lines with get_next_line (bonus)
D. Execution Engine (execution.c, ft_fork.c)
→ forks processes, calls execve with correct stdin/stdout
E. Utility Library (utils/*.c)
→ string splitting (to parse “cmd –options”), path resolution (`ft_find_path`), error printing, memory management
F. External Commands & Files
← execve loads external binaries; reads `file1` or here_doc input; writes to `file2`

##### Architectural Patterns and Principles
- Layered Design: Clear separation between parsing, orchestration, execution, and utilities
- Single Responsibility: Each .c/.h pair focuses on one domain (e.g., pipes vs. string utils)

##### Essential Details to Capture
- The loop over N commands in the bonus version (multiple pipes)
- The special “here_doc” branch that reads from stdin until a LIMITER
- How file descriptors are opened, duplicated, and closed (utils/close_fds.c)
- Shared utility functions for string splitting and path resolution

## Diagram (Mermaid)

```mermaid
flowchart TD
    User["User CLI Input"]:::external

    subgraph "CLI Layer"
        ParserMain["Argument Parser (main.c)"]:::parser
        ParserBonus["Argument Parser (main_bonus.c)"]:::parser
    end

    subgraph "Pipeline Manager"
        PipexHdr["pipex.h"]:::orchestration
        ExecC["execution.c"]:::orchestration
        FtFork["ft_fork.c"]:::orchestration
        PipexBonusHdr["pipex_bonus.h"]:::orchestration
        MultiPipe["ft_multi_pipe_bonus.c"]:::orchestration
        HereDoc["here_doc_bonus.c"]:::orchestration
        ExecBonus["execution_bonus.c"]:::orchestration
        FreeExec["free_exec_res.c"]:::orchestration
    end

    subgraph "Execution Engine"
        ExecEngineC["execution.c"]:::exec
        FtForkExec["ft_fork.c"]:::exec
        ExecEngineBonus["execution_bonus.c"]:::exec
        MultiPipeExec["ft_multi_pipe_bonus.c"]:::exec
    end

    subgraph "Utility Library"
        UtilsHdr["utils/utils.h"]:::utils
        CloseFds["close_fds.c"]:::utils
        ArrayLen["ft_array_len.c"]:::utils
        ErrMsg["ft_error_msg.c"]:::utils
        FindPath["ft_find_path.c"]:::utils
        FreeArr["ft_free_arr.c"]:::utils
        Putstr["ft_putstr_fd.c"]:::utils
        Split["ft_split.c"]:::utils
        StrChr["ft_strchr.c"]:::utils
        StrDup["ft_strdup.c"]:::utils
        StrJoin["ft_strjoin.c"]:::utils
        StrLen["ft_strlen.c"]:::utils
        StrNcmp["ft_strncmp.c"]:::utils
        Substr["ft_substr.c"]:::utils
    end

    subgraph "I/O Helper"
        GnlHdr["get_next_line_bonus.h"]:::utils
        GnlC["get_next_line_bonus.c"]:::utils
        GnlUtils["get_next_line_utils_bonus.c"]:::utils
    end

    Filesystem["Filesystem (file1/file2)"]:::external
    OSKernel["OS Kernel & External Binaries"]:::external

    User -->|"args"| ParserMain
    User -->|"args"| ParserBonus
    ParserMain -->|"validated args"| PipexHdr
    ParserBonus -->|"validated args"| PipexBonusHdr

    PipexHdr -->|"build pipes"| ExecC
    PipexHdr -->|"build pipes"| FtFork
    PipexBonusHdr -->|"build pipes"| MultiPipe
    PipexBonusHdr -->|"here_doc flow"| HereDoc
    PipexBonusHdr -->|"build pipes"| ExecBonus
    PipexBonusHdr -->|"cleanup"| FreeExec

    ExecC -->|"dup2, execve"| ExecEngineC
    FtFork -->|"fork logic"| FtForkExec
    ExecBonus -->|"dup2, execve"| ExecEngineBonus
    MultiPipe -->|"fork loop"| MultiPipeExec

    ExecEngineC -->|"open/read/write"| Filesystem
    ExecEngineC -->|"execve(cmd)"| OSKernel
    FtForkExec -->|"execve(cmd)"| OSKernel
    ExecEngineBonus -->|"execve(cmd)"| OSKernel
    MultiPipeExec -->|"execve(cmd)"| OSKernel

    classDef parser fill:#ADD8E6;
    classDef orchestration fill:#ADD8E6;
    classDef exec fill:#90EE90;
    classDef utils fill:#D3D3D3;
    classDef external fill:#FFFF99;

    click ParserMain "https://github.com/simoxeur/pipex/blob/master/mandatory/main.c"
    click ParserBonus "https://github.com/simoxeur/pipex/blob/master/bonus/main_bonus.c"
    click PipexHdr "https://github.com/simoxeur/pipex/blob/master/mandatory/pipex.h"
    click ExecC "https://github.com/simoxeur/pipex/blob/master/mandatory/execution.c"
    click FtFork "https://github.com/simoxeur/pipex/blob/master/mandatory/ft_fork.c"
    click PipexBonusHdr "https://github.com/simoxeur/pipex/blob/master/bonus/pipex_bonus.h"
    click MultiPipe "https://github.com/simoxeur/pipex/blob/master/bonus/ft_multi_pipe_bonus.c"
    click HereDoc "https://github.com/simoxeur/pipex/blob/master/bonus/here_doc_bonus.c"
    click ExecBonus "https://github.com/simoxeur/pipex/blob/master/bonus/execution_bonus.c"
    click FreeExec "https://github.com/simoxeur/pipex/blob/master/bonus/free_exec_res.c"
    click ExecEngineC "https://github.com/simoxeur/pipex/blob/master/mandatory/execution.c"
    click FtForkExec "https://github.com/simoxeur/pipex/blob/master/mandatory/ft_fork.c"
    click ExecEngineBonus "https://github.com/simoxeur/pipex/blob/master/bonus/execution_bonus.c"
    click MultiPipeExec "https://github.com/simoxeur/pipex/blob/master/bonus/ft_multi_pipe_bonus.c"
    click UtilsHdr "https://github.com/simoxeur/pipex/blob/master/utils/utils.h"
    click CloseFds "https://github.com/simoxeur/pipex/blob/master/utils/close_fds.c"
    click ArrayLen "https://github.com/simoxeur/pipex/blob/master/utils/ft_array_len.c"
    click ErrMsg "https://github.com/simoxeur/pipex/blob/master/utils/ft_error_msg.c"
    click FindPath "https://github.com/simoxeur/pipex/blob/master/utils/ft_find_path.c"
    click FreeArr "https://github.com/simoxeur/pipex/blob/master/utils/ft_free_arr.c"
    click Putstr "https://github.com/simoxeur/pipex/blob/master/utils/ft_putstr_fd.c"
    click Split "https://github.com/simoxeur/pipex/blob/master/utils/ft_split.c"
    click StrChr "https://github.com/simoxeur/pipex/blob/master/utils/ft_strchr.c"
    click StrDup "https://github.com/simoxeur/pipex/blob/master/utils/ft_strdup.c"
    click StrJoin "https://github.com/simoxeur/pipex/blob/master/utils/ft_strjoin.c"
    click StrLen "https://github.com/simoxeur/pipex/blob/master/utils/ft_strlen.c"
    click StrNcmp "https://github.com/simoxeur/pipex/blob/master/utils/ft_strncmp.c"
    click Substr "https://github.com/simoxeur/pipex/blob/master/utils/ft_substr.c"
    click GnlHdr "https://github.com/simoxeur/pipex/blob/master/bonus/gnl/get_next_line_bonus.h"
    click GnlC "https://github.com/simoxeur/pipex/blob/master/bonus/gnl/get_next_line_bonus.c"
    click GnlUtils "https://github.com/simoxeur/pipex/blob/master/bonus/gnl/get_next_line_utils_bonus.c"
```
