Skip to content

Ayushsinghal05/Shell

Repository files navigation

Shell Interpreter

A custom Unix shell implementation written in C++ with advanced features including command parsing, I/O redirection, pipes, background processes, and built-in commands.

Features

Core Shell Functionality

  • Command Execution: Execute system commands with full argument support
  • Pipes: Chain commands using pipes (|)
  • I/O Redirection:
    • Output redirection (>, >>)
    • Input redirection (<)
    • Error redirection (2>, 2>>)
    • Combined output and error redirection (>&, >>&)
  • Background Processes: Run commands in background using &
  • Process Substitution: Advanced process substitution support

Built-in Commands

  • cd [directory] - Change directory with support for relative/absolute paths and ~ expansion
  • setenv VARIABLE VALUE - Set environment variables
  • unsetenv VARIABLE - Remove environment variables
  • printenv [VARIABLE] - Display environment variables
  • source FILE - Execute commands from a file
  • exit - Exit the shell gracefully

Advanced Features

  • Wildcard Expansion: Support for * and ? wildcards in filenames
  • Environment Variable Expansion:
    • $VARIABLE - Standard variable expansion
    • $_ - Last argument of previous command
    • $? - Exit status of last command
    • $$ - Process ID of shell
    • $! - Process ID of last background command
    • $SHELL - Path to current shell
  • Tilde Expansion: ~ expands to home directory, ~user expands to user's home
  • Command History: Interactive line editing with history
  • Signal Handling: Proper handling of Ctrl-C (SIGINT) and child process cleanup (SIGCHLD)
  • Shell Configuration: Automatic execution of .shellrc on startup

Interactive Features

  • Custom Prompt: Configurable via PROMPT environment variable
  • Line Editing: Interactive command line editing with terminal control
  • TTY Detection: Behaves differently when input is from terminal vs file/pipe

Building

Prerequisites

  • GCC/G++ compiler with C++17 support
  • Flex (lexical analyzer generator)
  • Yacc/Bison (parser generator)
  • Make build system

Build Instructions

# Clone the repository
git clone <repository-url>
cd Shell

# Build the shell
make

# Optional: Enable debug printing
make PRINTING=1

# Clean build files
make clean

Usage

Running the Shell

# Start interactive shell
./shell

# Run shell script
./shell < script.sh

# Execute commands from file
echo "ls -la" | ./shell

Configuration

Create a .shellrc file in your working directory for automatic startup commands:

# Example .shellrc
setenv PATH /usr/bin:/bin:/usr/local/bin
setenv PROMPT "myshell> "
cd /home/user

Example Commands

# Basic command execution
myshell> ls -la

# Pipes and redirection
myshell> cat file.txt | grep "pattern" > output.txt

# Background processes
myshell> long_running_command &

# Environment variables
myshell> setenv MY_VAR "hello world"
myshell> echo $MY_VAR

# Wildcards
myshell> ls *.cpp

# Change directory
myshell> cd ~/Documents
myshell> cd ..

# Source script
myshell> source setup.sh

Architecture

Core Components

  1. Lexical Analyzer (lexical_analyzer.l)

    • Tokenizes input using Flex
    • Handles quotes, escapes, and special characters
    • Implements environment variable and wildcard expansion
  2. Syntax Parser (syntax_parser.y)

    • Parses command syntax using Yacc/Bison
    • Builds command structures for execution
    • Handles operator precedence and grammar rules
  3. Command Processor (command_processor.cpp/.hh)

    • Executes parsed commands
    • Manages process creation and communication
    • Handles I/O redirection and pipes
  4. Simple Command (simple_command.cpp/.hh)

    • Represents individual commands with arguments
    • Manages argument lists and command structure
  5. Shell Interpreter (shell_interpreter.cpp/.hh)

    • Main shell loop and initialization
    • Signal handling and process management
    • Prompt display and user interaction
  6. Terminal Control (terminal_control.cpp)

    • Low-level terminal operations
    • Enables advanced line editing features
  7. Line Editor (line_editor.cpp)

    • Interactive command line editing
    • Command history and completion

Signal Handling

  • SIGINT (Ctrl-C): Interrupts current command, returns to prompt
  • SIGCHLD: Cleans up zombie processes from background commands

Memory Management

  • Proper cleanup of dynamic allocations
  • RAII principles for resource management
  • Exception-safe command execution

Development

Project Structure

Shell/
├── lexical_analyzer.l      # Flex lexical analyzer
├── syntax_parser.y         # Yacc/Bison parser
├── shell_interpreter.*     # Main shell implementation
├── command_processor.*     # Command execution engine
├── simple_command.*        # Command data structures
├── terminal_control.cpp    # Terminal I/O operations
├── line_editor.cpp         # Interactive editing
└── Makefile               # Build configuration

Build System

The Makefile supports:

  • Conditional compilation flags (PRINTING=1 for debug output)
  • Automatic dependency tracking
  • Clean builds and intermediate file management
  • Optional line editing mode compilation

Testing

Run the shell with various command combinations to test functionality:

  • Complex pipe chains
  • Multiple redirections
  • Background process management
  • Environment variable operations
  • Wildcard expansions
  • Error handling scenarios

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published