# apardoo/so_22-23 architecture

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

- Interactive diagram: https://gitdiagram.com/apardoo/so_22-23
- Repository: https://github.com/apardoo/so_22-23

## Overview

##### Project type and purpose
- This is an educational, command-line interactive shell implemented in C, organized as four incremental lab assignments (P0–P3). Each “P#” directory contains headers, source files, a Makefile, and test harnesses or input files. The README confirms it’s a systems-programming teaching tool that builds a Unix-style shell with progressively added built-in commands and memory-management features.

##### File structure
- Top-level: .gitignore, LICENSE, README.md, and directories P0, P1, P2, P3.
- Each P# has:
- inc/ – public headers for that assignment
- src/ – implementation of the shell core and new commands
- makefile – build rules invoking gcc and valgrind targets
- info/ – specification PDFs and reference shells
- test/ (from P2 onward) – scripts or input files and expected outputs
- P3 adds a Sys_module for shared code, and test/delivery targets in the Makefile.

##### README for additional insights
- Core features introduced per lab:
- P0: basic prompt, history list, tokenization, and built-in commands (autores, pid, etc.)
- P1: file-system commands (create, stat, list, delete, deltree) using stat(), opendir(), readdir()
- P2: memory-management commands (allocate/free/mmap/shared), a linked-list of blocks, I/O commands, memdump/fill/memory/recurse
- P3: integrates all previous phases into a modular shell engine, using Sys_module.c/h for common logic and dispatching phases p0–p3

##### Main components and their interactions
a. Shell Core Engine
- Components: main loop (print prompt, read input), parser (TrocearCadena), dispatcher
- Lives in src/main.c (or p#.c for earlier labs)
b. Built-in Command Modules
- P0 commands (autores, pid, carpeta, fecha, hist, comando, infosis, ayuda, fin/salir/bye)
- P1 file commands (create, stat, list, delete, deltree)
- P2 memory commands (allocate, deallocate, i-o, memdump, memfill, memory, recurse)
- P3 consolidation: each practice’s commands invoked by dispatcher based on a phase parameter
c. Data Structures
- .c/h – generic singly linked list used for history (P0) and memory blocks (P2)
- Sys_module.c/h – abstraction layer in P3 for system errors and phase dispatch
d. System-call Interface
- stat(), mkdir(), open()/read()/write(), mmap()/munmap(), shmget()/shmat()/shmctl(), execvp() for pmap
e. Build & Test Infrastructure
- Makefiles per practice: “make”, “make memcheck” (valgrind), “make check” (diff against test inputs), “make delivery” (P3)
- test/test_in and test/differences to capture I/O validation

Relationships & Data Flows:
1. User types a command → Shell Core reads input → Parser tokenizes.
2. Dispatcher selects the correct Command Module based on first token and current practice phase.
3. Command Module executes: may invoke Data Structures API (linked list), perform System-calls, or fork/exec for external pmap.
4. Output printed to stdout; errors use errno and perror/strerror.
5. Test harness (if running make check) redirects input/output, diffs results.

##### Architectural patterns & design principles
- Modular layering: core engine at center, pluggable command handlers, a small shared library (List, Sys_module)
- Separation of concerns: parsing vs. execution vs. data management
- Incremental extension: each P# builds on previous code rather than rewriting a monolith

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% User Input
    User["User CLI"]:::ext

    %% Shell Core Engines
    subgraph "Shell Core Engines" 
        direction TB
        P0Core["Shell Core (P0)"]:::code
        P1Core["Shell Core (P1)"]:::code
        P2Core["Shell Core (P2)"]:::code
        P3Core["Shell Core (P3)"]:::code
    end

    %% Parser & Dispatcher (integrated)
    subgraph "Parser & Dispatcher"
        direction TB
        ParserDispatcher["Parser & Dispatcher"]:::code
    end

    %% Command Modules
    subgraph "Built-in Command Modules"
        direction TB
        P0Cmds["P0 Commands\n(autores,pid,carpeta,...)"]:::code
        P1Cmds["P1 File-System Commands\n(create,stat,list,delete,deltree)"]:::code
        P2Cmds["P2 Memory Commands\n(allocate,memdump,memfill,...)"]:::code
        P3Disp["P3 Phase Dispatcher\n(invokes p0-p3 modules)"]:::code
    end

    %% Data Structures Layer
    subgraph "Data Structures Layer"
        direction TB
        ListAPI["List API"]:::data
    end

    %% System Abstraction (P3)
    subgraph "System Abstraction (P3)"
        direction TB
        SysMod["Sys_module"]:::code
    end

    %% System Call / OS Interfaces
    subgraph "OS Kernel Interfaces"
        direction TB
        FS["File System"]:::os
        MemMgr["Memory Manager"]:::os
        IPC["IPC / Shared Memory"]:::os
        ProcMgr["Process Manager"]:::os
    end

    %% Build & Test Infrastructure
    subgraph "Build & Test Infrastructure"
        direction TB
        MakeP1["Makefile (P1)"]:::tools
        MakeP2["Makefile (P2)"]:::tools
        MakeP3["Makefile (P3)"]:::tools
        MakeInfoP3["Makefile (P3/info)"]:::tools
        Valgrind["Valgrind"]:::tools
        DiffScripts["Diff Scripts"]:::tools
        TestP2["Test Suite (P2)"]:::tools
        TestP3["Test Suite (P3)"]:::tools
    end

    %% Flows
    User --> P3Core
    P3Core --> ParserDispatcher
    ParserDispatcher --> P0Cmds
    ParserDispatcher --> P1Cmds
    ParserDispatcher --> P2Cmds
    ParserDispatcher --> P3Disp

    P0Cmds --> ListAPI
    P2Cmds --> ListAPI
    P3Disp --> SysMod

    P0Cmds --> FS
    P1Cmds --> FS
    P2Cmds --> MemMgr
    P2Cmds --> IPC
    P3Disp --> ProcMgr

    %% Build & Test dependencies
    MakeP1 -.-> P1Core
    MakeP2 -.-> P2Core
    MakeP3 -.-> P3Core
    MakeInfoP3 -.-> P3Core
    Valgrind -.-> P2Core
    DiffScripts -.-> TestP2
    DiffScripts -.-> TestP3
    TestP2 -.-> P2Core
    TestP3 -.-> P3Core

    %% Click Events
    click P0Core "https://github.com/apardoo/so_22-23/blob/main/P0/p0.c"
    click P1Core "https://github.com/apardoo/so_22-23/blob/main/P1/src/p1.c"
    click P2Core "https://github.com/apardoo/so_22-23/blob/main/P2/src/p2.c"
    click P3Core "https://github.com/apardoo/so_22-23/blob/main/P3/src/main.c"
    click ListAPI "https://github.com/apardoo/so_22-23/blob/main/P0/List.c"
    click ListAPI "https://github.com/apardoo/so_22-23/blob/main/P0/List.h"
    click ListAPI "https://github.com/apardoo/so_22-23/blob/main/P1/src/List.c"
    click ListAPI "https://github.com/apardoo/so_22-23/blob/main/P1/inc/List.h"
    click ListAPI "https://github.com/apardoo/so_22-23/blob/main/P2/src/List.c"
    click ListAPI "https://github.com/apardoo/so_22-23/blob/main/P2/inc/List.h"
    click ListAPI "https://github.com/apardoo/so_22-23/blob/main/P3/src/List.c"
    click ListAPI "https://github.com/apardoo/so_22-23/blob/main/P3/inc/List.h"
    click SysMod "https://github.com/apardoo/so_22-23/blob/main/P3/src/Sys_module.c"
    click SysMod "https://github.com/apardoo/so_22-23/blob/main/P3/inc/Sys_module.h"
    click P3Disp "https://github.com/apardoo/so_22-23/blob/main/P3/src/p0.c"
    click P3Disp "https://github.com/apardoo/so_22-23/blob/main/P3/src/p1.c"
    click P3Disp "https://github.com/apardoo/so_22-23/blob/main/P3/src/p2.c"
    click P3Disp "https://github.com/apardoo/so_22-23/blob/main/P3/src/p3.c"
    click MakeP1 "https://github.com/apardoo/so_22-23/tree/main/P1/makefile"
    click MakeP2 "https://github.com/apardoo/so_22-23/tree/main/P2/makefile"
    click MakeP3 "https://github.com/apardoo/so_22-23/tree/main/P3/makefile"
    click MakeInfoP3 "https://github.com/apardoo/so_22-23/tree/main/P3/info/makefile"
    click TestP2 "https://github.com/apardoo/so_22-23/tree/main/P2/test/"
    click TestP3 "https://github.com/apardoo/so_22-23/tree/main/P3/test/"
    click User "https://github.com/apardoo/so_22-23/blob/main/README.md"
    click P0Cmds "https://github.com/apardoo/so_22-23/blob/main/P0/p0.c"
    click P1Cmds "https://github.com/apardoo/so_22-23/blob/main/P1/src/p1.c"
    click P2Cmds "https://github.com/apardoo/so_22-23/blob/main/P2/src/p2.c"

    %% Styles
    classDef code fill:#ccffcc,stroke:#006600;
    classDef data fill:#ccffcc,stroke:#006600;
    classDef os fill:#ccccff,stroke:#000066;
    classDef tools fill:#ffddcc,stroke:#cc6600;
    classDef ext fill:#eeeeee,stroke:#888888;
```
