# tenaryo/tsh architecture

> Architecture diagram of the GitHub repository tenaryo/tsh, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tenaryo/tsh
- Repository: https://github.com/tenaryo/tsh

## Overview

TSH is an interactive POSIX-style shell. Its main workflow reads a command line, tokenizes it, parses commands and pipelines, then executes builtins or external programs with redirection. Other documented capabilities include command history with HISTFILE persistence and tab completion backed by a PATH executable cache. The graph includes these capability branches; some links rely on README documentation because the relevant reader and parser implementations were not excerpted.

Main entry points: [`src/execute.cpp`](https://github.com/tenaryo/tsh/blob/master/src/execute.cpp) (Executor), [`src/reader.cpp`](https://github.com/tenaryo/tsh/blob/master/src/reader.cpp) (Line reader), [`src/executable_cache.cpp`](https://github.com/tenaryo/tsh/blob/master/src/executable_cache.cpp) (Executable cache), [`src/parse.cpp`](https://github.com/tenaryo/tsh/blob/master/src/parse.cpp) (Parser)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interaction["Shell Interaction"]
  node_line_reader["Line reader<br/>[reader.cpp]"]
  node_readline["Readline"]
end

subgraph group_language["Command Language"]
  node_lexer["Lexer<br/>[lexer.cpp]"]
  node_parser["Parser<br/>[parse.cpp]"]
  node_command_model["Command model<br/>[types.hpp]"]
end

subgraph group_execution["Command Execution"]
  node_executor["Executor<br/>[execute.cpp]"]
  node_builtins["Builtin commands<br/>[commands.cpp]"]
  node_external_exec["External programs<br/>[execute.cpp]"]
  node_pipeline["Pipeline handling<br/>[execute.cpp]"]
  node_redirect_apply["Redirect application<br/>[execute.cpp]"]
  node_fd_redirect["FD redirect RAII<br/>[redirect.cpp]"]
end

subgraph group_features["Interactive Features"]
  node_history["History commands<br/>[commands.cpp]"]
  node_completion["Tab completion<br/>[reader.cpp]"]
  node_executable_cache[("Executable cache")]
end

node_user(("Shell user"))
node_path_env["PATH executables"]

node_user -->|"enters commands"| node_line_reader
node_line_reader -.->|"reads input"| node_readline
node_line_reader -->|"passes line"| node_lexer
node_lexer -->|"produces tokens"| node_parser
node_parser -->|"builds AST"| node_command_model
node_parser -->|"passes pipelines"| node_executor
node_executor -->|"dispatches builtins"| node_builtins
node_executor -->|"forks and executes"| node_external_exec
node_executor -->|"runs multi-command"| node_pipeline
node_executor -->|"applies child redirects"| node_redirect_apply
node_executor -->|"redirects builtin output"| node_fd_redirect
node_builtins -->|"looks up command type"| node_executable_cache
node_executor -->|"checks executable"| node_executable_cache
node_history -->|"uses history API"| node_readline
node_line_reader -.->|"persists HISTFILE"| node_history
node_line_reader -.->|"offers completion"| node_completion
node_completion -.->|"completes PATH names"| node_executable_cache
node_executable_cache -->|"scans PATH"| node_path_env

click node_line_reader "https://github.com/tenaryo/tsh/blob/master/src/reader.cpp"
click node_lexer "https://github.com/tenaryo/tsh/blob/master/src/lexer.cpp"
click node_parser "https://github.com/tenaryo/tsh/blob/master/src/parse.cpp"
click node_command_model "https://github.com/tenaryo/tsh/blob/master/include/types.hpp"
click node_executor "https://github.com/tenaryo/tsh/blob/master/src/execute.cpp"
click node_builtins "https://github.com/tenaryo/tsh/blob/master/src/commands.cpp"
click node_external_exec "https://github.com/tenaryo/tsh/blob/master/src/execute.cpp"
click node_pipeline "https://github.com/tenaryo/tsh/blob/master/src/execute.cpp"
click node_redirect_apply "https://github.com/tenaryo/tsh/blob/master/src/execute.cpp"
click node_fd_redirect "https://github.com/tenaryo/tsh/blob/master/src/redirect.cpp"
click node_history "https://github.com/tenaryo/tsh/blob/master/src/commands.cpp"
click node_completion "https://github.com/tenaryo/tsh/blob/master/src/reader.cpp"
click node_executable_cache "https://github.com/tenaryo/tsh/blob/master/src/executable_cache.cpp"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_line_reader,node_readline,node_user toneBlue
class node_lexer,node_parser,node_command_model toneAmber
class node_executor,node_builtins,node_external_exec,node_pipeline,node_redirect_apply,node_fd_redirect toneMint
class node_history,node_completion,node_executable_cache toneRose
class node_path_env toneIndigo
```

## Components

### Shell Interaction

- [Line reader](https://github.com/tenaryo/tsh/blob/master/src/reader.cpp): `src/reader.cpp`
- **Readline**

### Command Language

- [Lexer](https://github.com/tenaryo/tsh/blob/master/src/lexer.cpp): `src/lexer.cpp`
- [Parser](https://github.com/tenaryo/tsh/blob/master/src/parse.cpp): `src/parse.cpp`
- [Command model](https://github.com/tenaryo/tsh/blob/master/include/types.hpp): `include/types.hpp`

### Command Execution

- [Executor](https://github.com/tenaryo/tsh/blob/master/src/execute.cpp): `src/execute.cpp`
- [Builtin commands](https://github.com/tenaryo/tsh/blob/master/src/commands.cpp): `src/commands.cpp`
- [External programs](https://github.com/tenaryo/tsh/blob/master/src/execute.cpp): `src/execute.cpp`
- [Pipeline handling](https://github.com/tenaryo/tsh/blob/master/src/execute.cpp): `src/execute.cpp`
- [Redirect application](https://github.com/tenaryo/tsh/blob/master/src/execute.cpp): `src/execute.cpp`
- [FD redirect RAII](https://github.com/tenaryo/tsh/blob/master/src/redirect.cpp): `src/redirect.cpp`

### Interactive Features

- [History commands](https://github.com/tenaryo/tsh/blob/master/src/commands.cpp): `src/commands.cpp`
- [Tab completion](https://github.com/tenaryo/tsh/blob/master/src/reader.cpp): `src/reader.cpp`
- [Executable cache](https://github.com/tenaryo/tsh/blob/master/src/executable_cache.cpp): `src/executable_cache.cpp`

### Other components

- **Shell user**
- **PATH executables**

## Connections

- Shell user → Line reader: enters commands (evidence: `README.md`)
- Line reader → Readline: reads input (evidence: `README.md`)
- Line reader → Lexer: passes line
- Lexer → Parser: produces tokens (evidence: `README.md`)
- Parser → Command model: builds AST (evidence: `README.md`)
- Parser → Executor: passes pipelines
- Executor → Builtin commands: dispatches builtins (evidence: `src/execute.cpp`)
- Executor → External programs: forks and executes (evidence: `src/execute.cpp`)
- Executor → Pipeline handling: runs multi-command (evidence: `src/execute.cpp`)
- Executor → Redirect application: applies child redirects (evidence: `src/execute.cpp`)
- Executor → FD redirect RAII: redirects builtin output (evidence: `src/execute.cpp`)
- Builtin commands → Executable cache: looks up command type (evidence: `src/commands.cpp`)
- Executor → Executable cache: checks executable (evidence: `src/execute.cpp`)
- History commands → Readline: uses history API (evidence: `src/commands.cpp`)
- Line reader → History commands: persists HISTFILE (evidence: `README.md`)
- Line reader → Tab completion: offers completion (evidence: `README.md`)
- Tab completion → Executable cache: completes PATH names (evidence: `README.md`)
- Executable cache → PATH executables: scans PATH (evidence: `src/executable_cache.cpp`)
