Skip to content

Latest commit

ย 

History

History
402 lines (309 loc) ยท 11.5 KB

File metadata and controls

402 lines (309 loc) ยท 11.5 KB

๐Ÿš MiniShell

MiniShell Demo

Made with C 42 Project License: MIT Build Status Version

A powerful, feature-rich shell implementation that mimics bash behavior


โœจ Built by talented 1337 students with passion and precision โœจ

๐ŸŽฏ Project Overview

MiniShell is a comprehensive shell implementation developed as part of the 42 School curriculum. This project recreates the core functionality of bash, providing users with a familiar command-line experience while demonstrating advanced C programming concepts, including parsing, process management, and system calls.

๐Ÿš€ Key Highlights

  • ๐Ÿ”ฅ Full bash-like experience with advanced parsing
  • โšก Lightning-fast execution with efficient memory management
  • ๐Ÿ›ก๏ธ Robust error handling and signal management
  • ๐ŸŽจ Clean, modular codebase following best practices
  • ๐Ÿ“š Educational value - perfect for learning systems programming

โœจ Features

๐Ÿ› ๏ธ Built-in Commands

echo    # Display text with -n option support
cd      # Change directory (supports HOME and relative paths)
pwd     # Print working directory  
export  # Set environment variables
unset   # Remove environment variables
env     # Display environment variables
exit    # Exit shell with custom exit codes

๐Ÿ”— Advanced Shell Features

# Pipes - Chain commands together
ls -la | grep "test" | wc -l

# Redirections - Manage input/output
echo "Hello World" > output.txt
cat < input.txt >> output.txt

# Here-documents - Multi-line input
cat << EOF
This is a here-document
Multiple lines supported
EOF

# Logical operators - Conditional execution
make && ./minishell || echo "Build failed"

# Wildcard expansion - Pattern matching
ls *.c
echo test_*

๐Ÿ’ก Parser Capabilities

  • ๐Ÿ”ค Tokenization - Smart command parsing
  • ๐ŸŒณ AST Building - Abstract Syntax Tree construction
  • ๐Ÿ’ฒ Variable Expansion - Environment variable substitution
  • ๐ŸŽฏ Quote Handling - Single and double quote support
  • ๐Ÿ”„ Subshells - Parentheses grouping (command)
  • โš ๏ธ Error Recovery - Intelligent syntax error handling

๐ŸŽ›๏ธ System Integration

  • ๐Ÿ“ก Signal Handling - Ctrl+C, Ctrl+D, Ctrl+\ support
  • ๐Ÿง  Memory Management - Zero memory leaks
  • ๐Ÿ”„ Process Control - Fork, exec, wait management
  • ๐Ÿ“œ History - Command history with readline

๐Ÿ› ๏ธ Installation

Prerequisites

# Required dependencies
sudo apt-get update
sudo apt-get install build-essential readline-dev

Clone & Build

# Clone the repository
git clone https://github.com/Yo-omega/mini-shell.git
cd mini-shell

# Build the project
make

# Optional: Build with debug flags
make normal

Quick Start

# Launch MiniShell
./minishell

# You should see the beautiful prompt
shell $ 

๐ŸŽฎ Usage Examples

Basic Commands

shell $ echo "Hello, MiniShell!" 
Hello, MiniShell!

shell $ pwd
/home/user/mini-shell

shell $ export GREETING="Welcome to MiniShell"
shell $ echo $GREETING
Welcome to MiniShell

Advanced Piping

shell $ ls -la | grep "\.c$" | wc -l
13

shell $ cat /etc/passwd | head -5 | tail -2
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin

File Operations

shell $ echo "MiniShell rocks!" > test.txt
shell $ cat < test.txt
MiniShell rocks!

shell $ ls *.c | head -3 >> filelist.txt
shell $ cat filelist.txt

Here-Documents

shell $ cat << END
> This is line 1
> This is line 2  
> Variables work: $USER
> END
This is line 1
This is line 2
Variables work: username

Logical Operations

shell $ ls non_existent_file || echo "File not found!"
File not found!

shell $ make clean && make && echo "Build successful!"
# Compiles and shows success message

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                   MiniShell                     โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  Input Reading (readline)                       โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  Tokenization & Parsing                         โ”‚
โ”‚  โ”œโ”€โ”€ Lexical Analysis                           โ”‚
โ”‚  โ”œโ”€โ”€ Syntax Analysis                            โ”‚
โ”‚  โ””โ”€โ”€ AST Building                               โ”‚  
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  Expansion & Processing                         โ”‚
โ”‚  โ”œโ”€โ”€ Variable Expansion                         โ”‚
โ”‚  โ”œโ”€โ”€ Quote Removal                              โ”‚
โ”‚  โ”œโ”€โ”€ Wildcard Expansion                         โ”‚
โ”‚  โ””โ”€โ”€ Here-document Processing                   โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  Execution Engine                               โ”‚
โ”‚  โ”œโ”€โ”€ Built-in Commands                          โ”‚
โ”‚  โ”œโ”€โ”€ External Commands                          โ”‚
โ”‚  โ”œโ”€โ”€ Pipes & Redirections                       โ”‚
โ”‚  โ””โ”€โ”€ Process Management                         โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  System Interface                               โ”‚
โ”‚  โ”œโ”€โ”€ Environment Management                     โ”‚
โ”‚  โ”œโ”€โ”€ Signal Handling                            โ”‚
โ”‚  โ””โ”€โ”€ Memory Management                          โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Key Components

Component Description Files
Parser Tokenization, syntax analysis, AST building parse/
Executor Command execution, process management execute_command.c, exec_tree.c
Builtins Built-in command implementations builtins/
Expansion Variable and wildcard expansion parse/expand/, parse/globbing/
Redirection I/O redirection and pipe handling redirections.c, here_doc.c

๐Ÿงช Testing

Interactive Testing

# Launch and test interactively
./minishell

# Test built-ins
shell $ echo -n "No newline"
shell $ cd /tmp && pwd
shell $ export TEST="value" && echo $TEST

# Test parsing edge cases
shell $ echo "test'quote"
shell $ ls | cat | wc -l
shell $ (echo "subshell") && echo "parent"

Batch Testing

# Create test script
cat << 'EOF' > test_script.sh
#!/bin/bash
echo "=== Testing MiniShell ==="
echo "Current directory: $(pwd)"
echo "Environment test:" 
export TEST_VAR="Hello MiniShell"
echo $TEST_VAR
ls | head -3
echo "Tests completed!"
EOF

# Run with MiniShell  
./minishell < test_script.sh

๐Ÿ”ง Development

Project Structure

mini-shell/
โ”œโ”€โ”€ all/
โ”‚   โ”œโ”€โ”€ main.c              # Entry point and main loop
โ”‚   โ”œโ”€โ”€ main.h              # Main header with includes
โ”‚   โ”œโ”€โ”€ Makefile           # Build configuration
โ”‚   โ”œโ”€โ”€ builtins/          # Built-in commands
โ”‚   โ”‚   โ”œโ”€โ”€ ft_echo.c
โ”‚   โ”‚   โ”œโ”€โ”€ ft_cd.c
โ”‚   โ”‚   โ””โ”€โ”€ ...
โ”‚   โ”œโ”€โ”€ parse/             # Parsing engine
โ”‚   โ”‚   โ”œโ”€โ”€ expand/        # Variable expansion
โ”‚   โ”‚   โ”œโ”€โ”€ globbing/      # Wildcard handling
โ”‚   โ”‚   โ”œโ”€โ”€ here_doc/      # Here-document processing
โ”‚   โ”‚   โ””โ”€โ”€ tokenize/      # Tokenization
โ”‚   โ”œโ”€โ”€ libft/             # Utility library
โ”‚   โ””โ”€โ”€ ...
โ””โ”€โ”€ readline.supp          # Valgrind suppressions

Build Targets

make           # Standard build
make normal    # Build with debug flags  
make clean     # Remove object files
make fclean    # Remove all generated files
make re        # Clean rebuild

Memory Safety

# Check for memory leaks
valgrind --leak-check=full --track-origins=yes ./minishell

# With readline suppression
valgrind --leak-check=full --suppressions=readline.supp ./minishell

๐ŸŽ“ Educational Value

This project is an excellent learning resource for:

Systems Programming Concepts

  • Process creation and management (fork, exec, wait)
  • Inter-process communication (pipes, signals)
  • File descriptor manipulation
  • Memory management in C

Parsing & Language Design

  • Lexical analysis and tokenization
  • Recursive descent parsing
  • Abstract Syntax Tree construction
  • Context-sensitive grammar handling

Unix System Interface

  • Environment variable management
  • Signal handling (SIGINT, SIGQUIT)
  • File system operations
  • Terminal I/O with readline

๐Ÿค Contributing

We welcome contributions! Here's how you can help:

Development Setup

git clone https://github.com/Yo-omega/mini-shell.git
cd mini-shell
make normal    # Build with debug symbols

Contribution Guidelines

  1. ๐Ÿด Fork the repository
  2. ๐ŸŒŸ Create a feature branch (git checkout -b feature/amazing-feature)
  3. ๐Ÿ’ป Commit your changes (git commit -m 'Add amazing feature')
  4. ๐Ÿ“ค Push to the branch (git push origin feature/amazing-feature)
  5. ๐ŸŽฏ Open a Pull Request

Code Style

  • Follow 42 Norm coding standards
  • Use clear, descriptive variable names
  • Add comments for complex logic
  • Ensure zero memory leaks

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


๐Ÿ‘ฅ Authors

Author GitHub Role
yel-bouz @Yo-omega Core Developer
maddame @maddame Core Developer

๐ŸŽ‰ Built with โค๏ธ at 1337FIL School


๐Ÿ™ Acknowledgments

  • ๐Ÿซ 1337 for providing the learning environment
  • ๐Ÿ“š GNU Bash for inspiration and reference behavior
  • ๐Ÿ› ๏ธ GNU Readline library for line editing capabilities
  • ๐ŸŒŸ Open Source Community for tools and resources

๐Ÿš€ Ready to explore the power of MiniShell?

git clone https://github.com/Yo-omega/mini-shell.git
cd mini-shell && make && ./minishell

Happy Coding! ๐ŸŽ‰


If you found this project helpful, please consider giving it a โญ star!