Skip to content

A Unix shell implementation in Rust with built-in commands and startup animation - Zone01 Project

Notifications You must be signed in to change notification settings

4MR4N11/0-shell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

0-Shell

A minimalist Unix-like shell implementation in Rust that provides core shell functionality with built-in commands.

Screenshot

0-Shell Screenshot

0-Shell startup animation with ASCII art logo, welcome message, and various commands in action

Overview

0-Shell is a custom shell written in Rust that implements essential Unix shell commands as built-in functions. It features a colorized prompt, quote parsing, exit status tracking, and comprehensive error handling.

Features

Built-in Commands

Command Description Flags
echo Output text to standard output -
cd Change current directory (supports ~ for home) -
ls List directory contents -l (long), -a (all), -F (classify)
pwd Print current working directory -
cat Display file contents (supports stdin) -
cp Copy files -
rm Remove files/directories -r (recursive)
mv Move/rename files -
mkdir Create directories -
exit Exit the shell with optional status code -

Shell Features

Startup Animation

When launching 0-Shell, an animated startup sequence displays:

  • Loading animation with gradient blocks
  • ASCII art logo with typewriter effect
  • Colorized welcome message with author credits
  • Version information and usage hints

Prompt

  • Displays current directory name in cyan
  • Shows green arrow () for successful commands
  • Shows red arrow () for failed commands
  • Unicode wave symbol () as prefix

Example: ∿ myproject ➜

Quote Handling

  • Single quotes ('...'): Literal string, no escape processing
  • Double quotes ("..."): Supports escape sequences
  • Escape sequences in double quotes: \n, \t, \\, \"

Exit Status

  • Tracks last command exit status
  • Access via echo $?
  • Exit codes follow Unix conventions:
    • 0: Success
    • 1: General errors
    • 127: Command not found

EOF Handling

  • Ctrl+D exits the shell gracefully

Detailed Command Descriptions

ls Command

The ls implementation provides Unix-like directory listing with:

  • -l flag: Long format showing permissions, links, owner, group, size, modification time
  • -a flag: Show hidden files (including . and ..)
  • -F flag: Append indicators (/ for directories, * for executables, @ for symlinks, | for FIFOs, = for sockets)
  • Symlink resolution and target display
  • Extended attributes detection (+ indicator)
  • Device file major/minor number display
  • Proper column alignment

rm Command

  • Removes files by default
  • -r flag: Recursive deletion for directories
  • Error handling for non-existent files

cd Command

  • Changes to specified directory
  • cd or cd ~: Changes to home directory
  • Handles permission and access errors

cat Command

  • Displays file contents
  • Reads from stdin if no file specified (until EOF)
  • Handles multiple files

cp Command

  • Copies single file to destination
  • Copies multiple files to directory
  • Detects same-file copy attempts
  • Handles file/directory collisions

mv Command

  • Moves/renames files and directories
  • Supports multiple source files to directory
  • Detects same-file move attempts

Project Structure

0-shell/
├── Cargo.toml              # Project configuration and dependencies
└── src/
    ├── main.rs             # Entry point, main loop, prompt handling
    ├── init.rs             # Command parsing with quote handling
    ├── execute.rs          # Command dispatcher
    └── builtins/
        ├── mod.rs          # Module exports
        ├── cat.rs          # cat command implementation
        ├── cd.rs           # cd command implementation
        ├── cp.rs           # cp command implementation
        ├── echo.rs         # echo command implementation
        ├── exit.rs         # exit command implementation
        ├── ls.rs           # ls command implementation
        ├── mkdir.rs        # mkdir command implementation
        ├── mv.rs           # mv command implementation
        ├── pwd.rs          # pwd command implementation
        └── rm.rs           # rm command implementation

Dependencies

Crate Version Purpose
chrono 0.4.41 Date/time formatting for ls -l
colored_text 0.3.0 Colorized terminal output
dirs-next 2.0.0 Home directory detection
libc 0.2.174 Low-level system calls (extended attributes)
users 0.11.0 User/group name resolution
zeroize 1.8.1 Secure memory clearing

Building

Prerequisites

  • Rust toolchain (rustc, cargo)
  • Linux/Unix environment

Compile

# Debug build
cargo build

# Release build (optimized)
cargo build --release

Run

# After debug build
./target/debug/shell

# After release build
./target/release/shell

Usage Examples

# Navigate directories
cd /tmp
cd ~
pwd

# List files
ls
ls -la
ls -lF /home

# File operations
echo "Hello World"
cat file.txt
cp source.txt dest.txt
mv old.txt new.txt
mkdir mydir
rm file.txt
rm -r directory/

# Exit status
echo $?
exit 0
exit 1

Compliance with Requirements

Mandatory Features

Feature Status Notes
Display prompt and await input Custom colorized prompt
Parse and execute commands Full command parsing with quotes
Return to prompt after completion Main loop implementation
Exit on Ctrl+D (EOF) Handled in main loop
echo command Fully implemented
cd command With home directory support
ls command With -l, -a, -F flags
pwd command With fallback for deleted directories
cat command With stdin support
cp command Single and multi-file support
rm command With -r recursive flag
mv command With multi-file support
mkdir command Fully implemented
exit command With exit code support
Error message for unknown commands "command not found" message

Constraints

Constraint Status Notes
No external binary spawning ⚠️ Falls back to external commands for non-builtins
No globbing Not implemented
No redirection Not implemented
Unix-like behavior Follows Unix conventions

Bonus Features

Feature Status Notes
Startup animation Animated logo and welcome message
Ctrl+C handling Not implemented
Auto-completion Not implemented
Command history Not implemented
Current directory in prompt Shows last path component
Colorized output Prompt and startup use colors
Command chaining (;) Not implemented
Pipes (|) Not implemented
I/O redirection (>, <) Not implemented
Environment variables ⚠️ Only $? supported
Custom help command Not implemented

Known Issues

  1. External Command Fallback: When a command is not a built-in, the shell attempts to execute it as an external binary using std::process::Command. This may violate the "no external binary spawning" constraint.

  2. Cargo.toml Edition: Uses edition = "2024" which is a future edition. Should be edition = "2021" for current stable Rust.

  3. Echo Trailing Space: The echo command adds a trailing space after each argument.

  4. No SIGINT Handling: Ctrl+C is not handled and may terminate the shell.

Implementation Highlights

Security

  • Uses zeroize crate for secure memory clearing of sensitive input
  • Input strings are zeroized after use to prevent memory leaks of sensitive data

Error Handling

  • Comprehensive error messages for all file operations
  • Graceful handling of permission denied, file not found, and other I/O errors
  • Error messages follow Unix conventions

Code Quality

  • Modular design with separate files for each built-in command
  • Extensive documentation comments
  • Proper use of Rust idioms (Result types, pattern matching)

Testing Recommendations

  1. Test all built-in commands with various arguments
  2. Verify ls -l output format matches Unix ls
  3. Test quote handling with nested and escaped quotes
  4. Verify error messages for invalid operations
  5. Test cd with relative and absolute paths
  6. Verify rm -r works correctly on nested directories
  7. Test cp and mv with multiple files
  8. Verify exit status tracking with echo $?

Future Enhancements

  • Implement SIGINT (Ctrl+C) handling
  • Add command history with arrow key navigation
  • Implement tab auto-completion
  • Add pipe (|) and redirection (>, <) support
  • Add command chaining with ;
  • Implement more environment variable support
  • Add help command with usage information
  • Remove external command fallback to comply with constraints

License

This project is part of the Zone01 curriculum and is intended for educational purposes.

Authors

Developed as part of the 0-shell project at Zone01 Oujda.

  • Hamza Boutaleb - GitHub
  • Khalid El Amrani - Co-developer

About

A Unix shell implementation in Rust with built-in commands and startup animation - Zone01 Project

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages