Thanks to visit codestin.com
Credit goes to github.com

Skip to content

AyyoubMh48/0-shell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

0-Shell

A lightweight, standalone Unix shell implementation written in Rust for embedded Linux environments. This shell handles basic navigation, file manipulation, and essential shell behaviors without relying on existing shell utilities.

πŸ“‹ Table of Contents

🎯 Overview

This project implements a minimalist Unix shell that:

  • Displays a prompt ($ ) and waits for user input
  • Parses and executes user commands
  • Returns to the prompt only after command execution completes
  • Handles Ctrl+D (EOF) gracefully to exit the shell
  • Prints Command '<name>' not found for unrecognized commands

πŸ“š Learning Objectives

  • Work with file and directory operations
  • Manage user input and output within a shell loop
  • Implement robust error handling
  • Gain experience in Unix process and system call APIs

✨ Features

Core Features

  • Command Parsing: Supports single and double quoting, escape sequences
  • Environment Variables: Expansion of $VAR and ${VAR} syntax
  • Home Directory Expansion: ~ expands to $HOME
  • Quote Continuation: Multi-line input for unclosed quotes with dequote> prompt
  • ANSI Escape Handling: Cleans cursor movement sequences from input

Constraints

  • No external binaries or system calls that spawn them
  • Basic command syntax only (no piping |, redirection >, or globbing *)
  • Shell behavior aligns with Unix conventions

πŸ›  Built-in Commands

Command Description Flags
echo Print arguments to stdout with escape sequence support Supports \n, \t, \r, \\, \", \', \xNN
cd Change current working directory - (none, goes to $HOME)
ls List directory contents -l (long format), -a (show hidden), -F (classify)
pwd Print current working directory -
cat Concatenate and print files Reads from stdin if no args
cp Copy files -
rm Remove files or directories -r (recursive)
mv Move or rename files/directories -
mkdir Create directories -
clear Clear terminal screen -
exit Exit the shell -

πŸ“ Project Structure

0-shell/
β”œβ”€β”€ Cargo.toml              # Rust package manifest
β”œβ”€β”€ README.md               # This file
└── src/
    β”œβ”€β”€ main.rs             # Main shell loop and command dispatcher
    β”œβ”€β”€ parse.rs            # Input parsing, tokenization, variable expansion
    └── commands/
        β”œβ”€β”€ mod.rs          # Module declarations
        β”œβ”€β”€ cat.rs          # cat command implementation
        β”œβ”€β”€ cd.rs           # cd command implementation
        β”œβ”€β”€ clear.rs        # clear command implementation
        β”œβ”€β”€ cp.rs           # cp command implementation
        β”œβ”€β”€ echo.rs         # echo command implementation
        β”œβ”€β”€ ls.rs           # ls command implementation (with -l, -a, -F)
        β”œβ”€β”€ mkdir.rs        # mkdir command implementation
        β”œβ”€β”€ mv.rs           # mv command implementation
        β”œβ”€β”€ pwd.rs          # pwd command implementation
        └── rm.rs           # rm command implementation (with -r)

πŸš€ Installation

Prerequisites

  • Rust toolchain (rustc, cargo) - Install from rustup.rs

Build

# Clone the repository
git clone https://github.com/AyyoubMh48/0-shell.git
cd 0-shell

# Build the project
cargo build --release

# The binary will be at target/release/kinda_shell

πŸ’» Usage

# Run the shell
cargo run

# Or run the compiled binary directly
./target/release/kinda_shell

πŸ”§ Technical Implementation

Dependencies

Crate Version Purpose
chrono 0.4.41 Date/time formatting for ls -l
chrono-tz 0.10.4 Timezone support
iana-time-zone 0.1.63 System timezone detection
regex 1.11.1 Input cleaning (ANSI escape removal)
terminal_size 0.4.2 Terminal width for column formatting
users 0.11.0 User/group name resolution
xattr 1.0.1 Extended attributes support

Key Components

Parser (parse.rs)

  • Cmd struct: Holds parsed command name and arguments
  • clean_input(): Removes ANSI cursor movement sequences
  • split(): Tokenizes input with:
    • Single/double quote handling
    • Environment variable expansion ($VAR, ${VAR})
    • Home directory expansion (~)
    • Unclosed quote detection (returns error for continuation)

Main Loop (main.rs)

  • Displays $ prompt
  • Reads user input line by line
  • Handles EOF (Ctrl+D) for graceful exit
  • Supports quote continuation with dequote> prompt
  • Dispatches commands to appropriate handlers

Commands (commands/)

Each command is implemented as a separate module with its own function:

  • echo: Full escape sequence support including hex (\xNN)
  • cd: Changes directory, defaults to $HOME if no argument
  • ls: Full -l format with permissions, links, owner, group, size, time, and file classification (-F)
  • pwd: Prints current working directory
  • cat: Reads files or stdin
  • cp: Copies files (no recursive directory copy)
  • rm: Removes files, -r for recursive directory removal
  • mv: Moves/renames files and directories
  • mkdir: Creates directories
  • clear: Clears terminal using ANSI escape codes

🌟 Bonus Features

Implemented

  • βœ… Environment variable support ($HOME, $PATH, etc.)
  • βœ… Quote continuation for multi-line input
  • βœ… Extended ls output with file classification (-F)
  • βœ… Clear command for terminal management

Potential Enhancements

  • ⬜ Handle Ctrl+C (SIGINT) without crashing
  • ⬜ Auto-completion
  • ⬜ Command history
  • ⬜ Prompt with current directory (e.g., ~/projects/0-shell $)
  • ⬜ Colorized output
  • ⬜ Command chaining with ;
  • ⬜ Pipes (|)
  • ⬜ I/O redirection (>, <)
  • ⬜ Custom help command

πŸ“ Example Session

student$ ./target/release/kinda_shell
$ cd dev
$ pwd
/dev
$ ls -l
total 0
crw-------  1 root   root     10,    58 Feb  5 09:21 acpi_thermal_rel
crw-r--r--  1 root   root     10,   235 Feb  5 09:21 autofs
drwxr-xr-x  2 root   root           540 Feb  5 09:21 block
...
$ something
Command 'something' not found
$ echo "Hello There"
Hello There
$ echo $HOME
/home/student
$ mkdir test_dir
$ ls
test_dir
$ rm -r test_dir
$ exit
student$

πŸ“Š Evaluation Criteria

This project is evaluated based on:

Criteria Description
Functionality Commands perform correctly and emulate standard Unix behavior
Stability Shell handles user errors, invalid input, and edge cases without crashing
Code Quality Clean, well-documented, and maintainable code
Error Handling Appropriate error messages for invalid operations

πŸ“„ License

This project is part of an educational assignment.

πŸ‘€ Author

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages