Skip to content

panniki/nikis_shell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Niki's Shell

A POSIX-compliant shell implementation in Rust. This project was originally built as part of the CodeCrafters Shell Challenge and has been extended with additional features and improvements.

Features

Built-in Commands

  • exit - Exit the shell with history persistence
  • echo <text> - Print text to stdout
  • pwd - Print current working directory
  • cd <path> - Change directory (supports ~ for home directory)
  • type <command> - Show command type (builtin vs external program location)
  • history [options] - Command history management
    • history - Show all history
    • history <n> - Show last n commands
    • history -r <file> - Read history from file
    • history -w <file> - Write history to file
    • history -a <file> - Append new history to file

Interactive Features

  • Command Line Editing
    • Arrow key navigation (↑/↓ for history browsing)
    • Backspace for character deletion
    • Raw mode terminal input with proper cleanup
  • Tab Completion
    • Auto-completion for commands and executables in PATH
    • Common prefix completion for multiple matches
    • Press tab twice to show all available completions
  • History Management
    • Persistent history across sessions (via HISTFILE environment variable)
    • In-memory history with navigation
    • History file operations (read/write/append)

Command Processing

  • Shell Word Splitting with proper quote handling:
    • Single quotes ('text') - literal strings
    • Double quotes ("text") - variable expansion context
    • Backslash escaping for special characters
  • Pipeline Support - Full pipe chains: cmd1 | cmd2 | cmd3
  • I/O Redirection:
    • Output redirection: >, 1>, >>
    • Error redirection: 2>, 2>>
    • Append mode: >>, 1>>, 2>>
    • Mixed redirections: cmd >stdout.txt 2>stderr.txt

External Program Execution

  • PATH Resolution - Automatic lookup of executables in system PATH
  • Process Management - Proper spawning and waiting for child processes
  • Signal Handling - Ctrl+C and Ctrl+D support

Architecture

The shell is organized into focused modules:

  • main.rs - Core execution pipeline and process management
  • readline.rs - Interactive terminal input with editing capabilities
  • tokenize.rs - Command parsing and token generation
  • words.rs - Shell word splitting with quote/escape handling
  • cmd.rs - Built-in command implementations
  • history.rs - Command history management and persistence
  • paths.rs - PATH environment variable processing
  • cmp.rs - Tab completion using trie data structure
  • errors.rs - Comprehensive error handling

Usage

Installation and Usage

# Install from source
git clone https://github.com/nikiyerm/nikis_shell
cd nikis_shell
cargo install --path .

# Run the shell
nikis_shell

# Or compile and run directly
cargo run

Example Session

$ echo "Hello, World!"
Hello, World!
$ pwd
/home/user/projects
$ cd /tmp
$ ls -la | grep txt > text_files.txt
$ history 5
    1  echo "Hello, World!"
    2  pwd
    3  cd /tmp
    4  ls -la | grep txt > text_files.txt
    5  history 5
$ type ls
ls is /usr/bin/ls
$ exit

Implementation Highlights

  • Memory Safety: Built with Rust's ownership system for safe memory management
  • Concurrent Access: Thread-safe history and completion using LazyLock and Mutex
  • Error Handling: Comprehensive error types for different failure modes
  • Testing: Extensive unit tests for tokenization and word splitting
  • Performance: Efficient trie-based completion and lazy initialization of system paths

TODO - Basic Missing Shell Features

The following core shell features are not yet implemented:

  • Variable Expansion - $VAR, ${VAR}, environment variable substitution
  • Globbing/Wildcards - *.txt, file?.log, [a-z]* pattern matching
  • Input Redirection - < for reading from files
  • Command Substitution - `command` and $(command)
  • Background Jobs - command & and basic job control
  • Quoting Edge Cases - Better handling of complex quote combinations
  • Exit Codes - Proper exit code propagation from commands
  • Signal Handling - Improved Ctrl+C, Ctrl+Z behavior
  • More Built-ins - alias, unalias, export, unset, which

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages