Skip to content

cfeliz-r/Minishell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

152 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🐚 Minishell - Babuterm

42 C Linux Readline

A comprehensive Unix shell implementation recreating bash functionality

Features β€’ Installation β€’ Usage β€’ Built-ins β€’ Architecture


πŸ“‹ Table of Contents

🎯 About

Minishell is a simplified shell implementation written in C as part of the 42 School curriculum. This project recreates the core functionality of bash, providing users with a command-line interface capable of executing commands, managing processes, handling I/O redirection, and implementing essential built-in commands.

The shell handles complex parsing, process management, signal handling, and memory management while maintaining compatibility with standard Unix shell behavior.

✨ Features

Core Shell Functionality

  • πŸ”„ Interactive Command Execution - Execute system commands and built-ins
  • πŸ“ Command History - Navigate through command history using arrow keys
  • 🎨 Colored Prompt - Beautiful, informative prompt with color coding
  • πŸ” Path Resolution - Automatic command path discovery
  • 🧠 Memory Safe - Comprehensive memory management with no leaks

Advanced Parsing

  • 🎭 Quote Handling - Support for single (') and double (") quotes
  • πŸ”— Pipe Operations - Chain commands with pipes (|)
  • πŸ“Š Variable Expansion - Environment variable substitution ($VAR)
  • 🚫 Syntax Validation - Comprehensive input validation and error handling

I/O Redirection

  • πŸ“₯ Input Redirection (<) - Redirect stdin from files
  • πŸ“€ Output Redirection (>) - Redirect stdout to files
  • πŸ“‹ Append Mode (>>) - Append output to files
  • πŸ“„ Here Documents (<<) - Multi-line input with delimiters

Process Management

  • πŸ”„ Pipeline Execution - Multi-command pipelines
  • ⚑ Signal Handling - Proper SIGINT and SIGQUIT management
  • 🎯 Exit Status - Accurate exit code handling
  • 🧡 Fork Management - Efficient process creation and cleanup

πŸš€ Installation

Prerequisites

# Ubuntu/Debian
sudo apt-get update
sudo apt-get install build-essential libreadline-dev

# macOS
brew install readline

Build from Source

# Clone the repository
git clone [repository-url]
cd Babuterm

# Compile the project
make

# Run the shell
./minishell

Makefile Targets

make           # Build the project
make clean     # Remove object files
make fclean    # Remove object files and executable
make re        # Rebuild everything

πŸ’» Usage

Basic Command Execution

# Start the shell
./minishell

# Execute commands
babuterm$ ls -la
babuterm$ echo "Hello, World!"
babuterm$ cat file.txt

Pipe Operations

# Chain commands with pipes
babuterm$ ls -la | grep ".c" | wc -l
babuterm$ cat file.txt | head -10 | tail -5

I/O Redirection

# Input redirection
babuterm$ wc -l < input.txt

# Output redirection
babuterm$ echo "Hello" > output.txt
babuterm$ ls -la >> log.txt

# Here documents
babuterm$ cat << EOF
This is a multi-line
input example
EOF

Variable Expansion

# Environment variables
babuterm$ echo $HOME
babuterm$ echo "User: $USER"
babuterm$ echo 'Literal: $HOME'  # No expansion in single quotes

πŸ›  Built-in Commands

Command Description Example
echo Display text with optional -n flag echo -n "Hello World"
cd Change directory cd /path/to/directory
pwd Print working directory pwd
export Set environment variables export VAR=value
unset Remove environment variables unset VAR
env Display environment variables env
exit Exit the shell exit [code]

Built-in Command Details

echo

babuterm$ echo "Hello World"           # Basic output
babuterm$ echo -n "No newline"         # Suppress newline
babuterm$ echo -n -n -n "Multiple -n"  # Multiple -n flags

cd

babuterm$ cd /home/user      # Absolute path
babuterm$ cd ..              # Parent directory
babuterm$ cd                 # Home directory
babuterm$ cd -               # Previous directory

export

babuterm$ export NAME=value     # Set variable
babuterm$ export PATH=$PATH:/new/path  # Modify PATH
babuterm$ export               # List all exported variables

πŸ”§ Advanced Features

Quote Handling

  • Single Quotes: Preserve literal values
  • Double Quotes: Allow variable expansion
  • Quote Removal: Automatic quote stripping in arguments

Error Handling

  • Comprehensive syntax error detection
  • Graceful handling of invalid commands
  • Proper exit status propagation
  • Memory cleanup on errors

Signal Management

  • Ctrl+C (SIGINT): Interrupt current command
  • Ctrl+\ (SIGQUIT): Ignored in interactive mode
  • Ctrl+D (EOF): Exit shell gracefully

πŸ— Architecture

Core Components

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Input Parser  │───▢│  Command Parser │───▢│   Executor      β”‚
β”‚   (Lexical)     β”‚    β”‚   (Syntax)      β”‚    β”‚  (Execution)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                       β”‚                       β”‚
         β–Ό                       β–Ό                       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Quote Handler   β”‚    β”‚ Variable Expand β”‚    β”‚ Process Manager β”‚
β”‚ Error Validator β”‚    β”‚ Path Resolution β”‚    β”‚ Signal Handler  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Data Structures

Command Structure

typedef struct s_command {
    int is_correct;        // Validation flag
    char **args;           // Command arguments
    char *cmd_cpt;         // Command name
    char *path;            // Command path
    char *inredir;         // Input redirection
    char **delimiters;     // Here-doc delimiters
    char **outredirs;      // Output redirections
    int appd_out;          // Append flag
    int error;             // Error status
} t_command;

Environment List

typedef struct s_list_envp {
    char *envp_content;           // Environment variable
    struct s_list_envp *next;     // Next node
} t_list_env;

πŸ“ Project Structure

Babuterm/
β”œβ”€β”€ πŸ“„ Makefile              # Build configuration
β”œβ”€β”€ πŸ“„ minishell.h           # Main header file
β”œβ”€β”€ πŸ“„ structs.h             # Data structures
β”œβ”€β”€ πŸ“ src/                  # Source code
β”‚   β”œβ”€β”€ πŸ“„ main.c            # Entry point
β”‚   β”œβ”€β”€ πŸ“„ minishell.c       # Core shell logic
β”‚   β”œβ”€β”€ πŸ“ builtin/          # Built-in commands
β”‚   β”‚   β”œβ”€β”€ πŸ“„ ft_echo.c     # Echo implementation
β”‚   β”‚   β”œβ”€β”€ πŸ“„ ft_cd.c       # CD implementation
β”‚   β”‚   β”œβ”€β”€ πŸ“„ ft_export.c   # Export implementation
β”‚   β”‚   └── πŸ“„ ...           # Other built-ins
β”‚   β”œβ”€β”€ πŸ“ parsing/          # Input parsing
β”‚   β”‚   β”œβ”€β”€ πŸ“„ parse_commands.c
β”‚   β”‚   └── πŸ“„ ...
β”‚   β”œβ”€β”€ πŸ“ commands/         # Command execution
β”‚   β”œβ”€β”€ πŸ“ redirection/      # I/O redirection
β”‚   β”œβ”€β”€ πŸ“ signals/          # Signal handling
β”‚   β”œβ”€β”€ πŸ“ utils/            # Utility functions
β”‚   └── πŸ“ errors/           # Error handling
β”œβ”€β”€ πŸ“ libft/                # Custom C library
β”‚   β”œβ”€β”€ πŸ“„ libft.h
β”‚   β”œβ”€β”€ πŸ“„ ft_*.c            # Library functions
β”‚   └── πŸ“ get_next_line/    # GNL implementation
└── πŸ“ obj/                  # Compiled objects

🧠 Memory Management

Safe Memory Practices

  • Automatic Cleanup: All allocated memory is properly freed
  • Error Handling: Memory cleanup on all error paths
  • Leak Prevention: Comprehensive testing for memory leaks
  • Resource Management: Proper file descriptor and process cleanup

Memory Testing

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

# Test with specific commands
echo "command" | valgrind ./minishell

πŸ“‘ Signal Handling

Interactive Mode

  • SIGINT (Ctrl+C): Display new prompt
  • SIGQUIT (Ctrl+\): Ignored
  • EOF (Ctrl+D): Exit shell

Command Execution

  • SIGINT: Interrupt running command
  • SIGQUIT: Send quit signal to command
  • Child Processes: Proper signal propagation

πŸ§ͺ Testing

Manual Testing

# Basic functionality
echo "test" | ./minishell
echo "ls | wc -l" | ./minishell
echo "export TEST=value && echo \$TEST" | ./minishell

# Edge cases
echo "ls |" | ./minishell           # Syntax error
echo "echo \"unclosed quote" | ./minishell  # Quote error

Automated Testing

# Create test suite
bash test_suite.sh

# Compare with bash
echo "command" | bash > expected.txt
echo "command" | ./minishell > result.txt
diff expected.txt result.txt

πŸ‘₯ Authors

Author GitHub Email
manufern @manufern manufern@student.42.fr
cfeliz-r @cfeliz-r cfeliz-r@student.42.fr

πŸ“Š Project Statistics

  • Lines of Code: ~2,750
  • Files: 45+ source files
  • Functions: 100+ custom functions
  • Development Time: 3+ months
  • Language: C (C99 standard)

🀝 Contributing

While this is an educational project, contributions and suggestions are welcome:

  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

πŸ“„ License

This project is part of the 42 School curriculum. It's shared for educational purposes.


Made with ❀️ at 42 Madrid

"The shell is the ultimate power tool"

About

Crear tu propia shell

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors