Skip to content

Hesh is a simple functional Unix shell implementation written in C++23. It provides a comprehensive command-line interface with features including command completion, history management, I/O redirection, piping.

Notifications You must be signed in to change notification settings

HamzaHassanain/hesh

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Hesh - A Simple C++ Shell Implementation

Hesh is a simple functional Unix shell implementation written in C++23. It provides a comprehensive command-line interface with features including command completion, history management, I/O redirection, piping, and a beautiful agnoster-inspired prompt.

✨ Features

Core Shell Functionality

  • Command Execution: Full support for executing external commands from PATH
  • Built-in Commands: Essential shell builtins including cd, echo, pwd, exit, type, and history
  • Smart Command Discovery: Automatic detection and registration of executable files in PATH directories
  • Interactive Mode: Real-time command input with sophisticated prompt display

Advanced I/O Features

  • Piping Support: Full pipeline support with | operator for chaining commands
  • Stream Redirection: Comprehensive redirection support:
    • > and >> for stdout redirection (overwrite/append)
    • 2> and 2>> for stderr redirection (overwrite/append)
    • &> and &>> for both stdout and stderr redirection
  • Quote Handling: Sophisticated parsing of single quotes, double quotes, and escape sequences

User Experience

  • GNU Readline Integration:
    • Command history with persistent storage
    • Tab completion for commands
    • Line editing capabilities (Emacs-style keybindings)
    • History search and navigation
  • Beautiful Prompt: Agnoster-inspired powerline prompt showing:
    • Current username with colored background
    • Current working directory (with ~ abbreviation)
    • Visual separators using powerline fonts
  • Configuration Support: Custom configuration file (~/.heshrec) for environment variables

Technical Features

  • Memory Safe: Modern C++ with smart pointers and RAII principles
  • Modular Architecture: Clean separation of concerns with dedicated modules for:
    • Command management and registration
    • Token parsing and processing
    • Stream redirection handling
    • Readline utilities
  • Extensible Design: Easy to add new built-in commands through the command interface

πŸš€ Quick Start

Prerequisites

  • C++23 compatible compiler (GCC 13+ or Clang 16+)
  • CMake 3.13+
  • GNU Readline library (libreadline-dev on Ubuntu/Debian)
  • Make for building

Installation

  1. Clone the repository:

    git clone https://github.com/HamzaHassanain/hesh.git
    cd hesh
  2. Build the project:

    ./build.sh
  3. Run the shell:

    ./out/hesh

Alternative Build Commands

# Clean build
./build.sh clean

# Build and run immediately
./build.sh run

# Manual CMake build
mkdir -p out
cmake -S . -B out
cd out && make -j$(nproc)

πŸ“– Usage

Built-in Commands

echo [args...]

Print arguments to stdout with space separation

echo Hello World
echo "Hello World" > output.txt
echo Error message 2> error.log

cd <directory>

Change current working directory

cd /home/user
cd ~           # Go to home directory
cd ..          # Go to parent directory

pwd

Print current working directory

pwd

exit

Exit the shell

exit

type <command>

Display information about command type

type echo      # Shows: echo is a shell builtin
type ls        # Shows: ls is /bin/ls

history [n] | [-r|-w|-a] <file>

Command history management

history           # Show all history
history 10        # Show last 10 commands
history -w file   # Write history to file
history -r file   # Read history from file
history -a file   # Append new history to file

Advanced Features

Piping

Chain commands using the pipe operator:

echo "Hello World" | grep Hello
history | tail -5
cat file.txt | sort | uniq

Redirection

Redirect output streams:

echo "Hello" > output.txt        # Redirect stdout to file
echo "Error" 2> error.log        # Redirect stderr to file
echo "Both" &> combined.log      # Redirect both streams
echo "Append" >> output.txt      # Append to file

Quoting and Escaping

Handle complex arguments:

echo "Hello World"               # Double quotes
echo 'Hello $USER'               # Single quotes (literal)
echo Hello\ World                # Escape spaces
echo "Path: \"$HOME\""           # Escaped quotes

Configuration

Create ~/.heshrec to customize your shell:

# Set custom PATH
PATH=/custom/bin:/usr/local/bin

# Set history file location
HISTFILE=~/.my_shell_history

# Add custom environment variables
CUSTOM_VAR=value

πŸ—οΈ Architecture

Core Components

  • main.cpp: Entry point with main shell loop and readline integration
  • commander.hpp: Command registry using Trie data structure for efficient lookup
  • cmd.hpp: Base command interface with stream redirection and piping support
  • tokenize.hpp: Advanced tokenizer with quote handling and escape sequences

Built-in Commands

All commands inherit from the base cmd class:

  • echo_cmd: Text output with stream redirection support
  • cd_cmd: Directory navigation with ~ expansion
  • pwd_cmd: Current directory display
  • exit_cmd: Shell termination
  • type_cmd: Command type identification
  • history_cmd: History management with file operations

Utilities

  • readline_utils.hpp: GNU Readline integration and tab completion
  • style.hpp: Prompt styling and directory path formatting
  • token.hpp: Token types for parsing (strings, pipes, redirections)
  • redirect_streams.hpp: Stream redirection handling

πŸ”§ Development

Building from Source

# Debug build with all warnings
cmake -S . -B out -DCMAKE_BUILD_TYPE=Debug
cd out && make -j$(nproc)

# Release build optimized
cmake -S . -B out -DCMAKE_BUILD_TYPE=Release
cd out && make -j$(nproc)

Adding New Commands

  1. Create a new header file in src/cmds/
  2. Inherit from the cmd base class
  3. Implement custom_handle() method
  4. Register the command in init_shell.hpp

Example:

class my_cmd : public cmd {
public:
    my_cmd() : cmd("mycommand", "description") {}

    void custom_handle(std::vector<std::shared_ptr<Token>>& args,
                      std::string& piped_command) override {
        // Implementation here
    }
};

Testing

The shell supports comprehensive testing of features:

# Test basic commands
echo "test" | cat
pwd && ls
cd ~ && pwd

# Test redirection
echo "output" > test.txt && cat test.txt
ls non_existent 2> error.log

# Test history
history 5

πŸ“š Documentation

  • DOCUMENTATION.md - Complete technical documentation covering:
    • Detailed architecture overview
    • File-by-file code documentation
    • API reference and design patterns
    • Performance characteristics
    • Extension guides for developers

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes following the existing code style
  4. Test your changes thoroughly
  5. Commit your changes (git commit -m 'Add amazing feature')
  6. Push to the branch (git push origin feature/amazing-feature)
  7. Open a Pull Request

About

Hesh is a simple functional Unix shell implementation written in C++23. It provides a comprehensive command-line interface with features including command completion, history management, I/O redirection, piping.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages