A comprehensive shell implementation in C, inspired by bash, featuring advanced parsing, command execution, and process management.
- Overview
- Features
- Architecture
- Project Structure
- Data Structures
- Core Components
- Built-in Commands
- Signal Handling
- Error Handling
- Building and Running
- Usage Examples
- Implementation Details
- Authors
- License
Minishell is a sophisticated shell implementation that recreates the core functionality of bash. It provides a complete command-line interface with support for complex command parsing, process management, environment variable handling, and advanced features like here-documents and piping.
The project demonstrates advanced C programming concepts including:
- Complex data structure management
- Process creation and management
- Signal handling
- Memory management
- Lexical analysis and parsing
- File descriptor manipulation
- Interactive Command Line: Full readline support with history
- Command Parsing: Advanced lexical analysis and tokenization
- Process Management: Fork/exec model with proper process handling
- Environment Variables: Complete environment variable management
- Signal Handling: Proper handling of SIGINT, SIGQUIT, and other signals
- Error Handling: Comprehensive error reporting and exit status management
- Piping: Multiple command piping with proper file descriptor management
- Input/Output Redirection: Support for
<,>,>>operators - Here Documents: Full heredoc support with variable expansion
- Quote Handling: Proper handling of single and double quotes
- Variable Expansion: Environment variable expansion with
$VARsyntax - Path Resolution: Automatic command path resolution using
$PATH
cd: Change directory with HOME and OLDPWD supportecho: Output text with-nflag supportpwd: Print working directoryexport: Set environment variables with validationunset: Remove environment variablesenv: Display environment variablesexit: Exit shell with status code
The shell follows a multi-stage processing pipeline:
Input → Lexer → Parser → Package Builder → Executor
- Lexer (
al_lexer.c): Tokenizes input into word lists - Parser (
ip_parsing.c): Converts words into structured tokens - Package Builder (
analyze_and_dispatch_packeging.c): Creates execution packages - Executor (
analyze_and_dispatch.c): Executes commands with proper process management
minishell_git/
├── include/
│ ├── minishell.h # Main header with all structures and prototypes
│ └── tokens.h # Token type definitions
├── lib/
│ └── libft/ # Custom C library
├── src/
│ ├── main.c # Entry point and main loop
│ ├── command/ # Built-in command implementations
│ │ ├── cd_exe.c # cd command implementation
│ │ ├── echo_exe.c # echo command implementation
│ │ ├── export_exe.c # export command implementation
│ │ ├── unset_exe.c # unset command implementation
│ │ └── exit_exe.c # exit command implementation
│ ├── preps/ # Parsing and preparation
│ │ ├── al_lexer.c # Main lexical analyzer
│ │ ├── al_lexer_words.c # Word tokenization
│ │ ├── ip_parsing.c # Token parsing
│ │ ├── create_token_l.c # Token list creation
│ │ └── token_utils*.c # Token utility functions
│ ├── util/ # Utility functions
│ │ ├── here_doc.c # Here document implementation
│ │ ├── analyze_and_dispatch*.c # Command execution
│ │ └── al_quotes.c # Quote handling
│ ├── processes/ # Process management
│ │ ├── child_process.c # Child process handling
│ │ └── child_process_helper.c # Process utilities
│ ├── signals/ # Signal handling
│ │ ├── signal_handler.c # Signal handlers
│ │ └── set_up_signals.c # Signal setup
│ ├── error/ # Error handling
│ │ ├── error_handler.c # Error processing
│ │ └── error_print.c # Error output
│ ├── free/ # Memory management
│ │ └── *_free*.c # Memory cleanup functions
│ ├── init/ # Initialization
│ │ └── init_*.c # Initialization functions
│ └── minilib/ # Custom utility library
│ ├── al_env_*.c # Environment handling
│ ├── ft_*.c # String utilities
│ └── al_prg_path.c # Path resolution
└── Makefile # Build configuration
t_main_env - Main environment structure containing all shell state:
typedef struct s_main_env {
t_list_words *list_words; // Tokenized input
t_list_tokens *list_tokens; // Parsed tokens
char *curr_dir; // Current directory
char *prev_dir; // Previous directory
char *home_dir; // Home directory
int here_doc_index; // Here document counter
char **real_env; // Environment array
t_package *package; // Command packages
t_our_env *our_env; // Environment variables
int exit_status; // Last exit status
int export_error; // Export error flag
int p_error; // Parse error flag
} t_main_env;t_package - Command execution package:
typedef struct s_package {
t_command *command; // Built-in command
t_program *program; // External program
int flag; // Package type flag
t_node_file *redir; // Redirection list
struct s_package *prev; // Previous package
struct s_package *next; // Next package
} t_package;t_tokens - Token types:
typedef enum e_tokens {
WORD, // Regular word
STRING, // Quoted string
COMMAND, // Built-in command
PROGRAM, // External program
REDIRECTION_INPUT, // < redirection
REDIRECTION_OUTPUT, // > redirection
REDIRECTION_HERE_DOC, // << here document
REDIRECTION_APPEND, // >> append
PIPE, // | pipe
ENV_VARIABLE, // Environment variable
OPTIONS // Command options
} t_tokens;- Tokenizes input into meaningful units
- Handles quotes, metacharacters, and whitespace
- Supports variable expansion during tokenization
- Creates linked list of words for parsing
- Converts word tokens into structured command tokens
- Identifies command types (built-in vs external)
- Handles redirection and pipe operators
- Validates syntax and reports errors
- Groups related tokens into execution packages
- Separates commands by pipes
- Builds redirection lists
- Prepares commands for execution
- Manages process creation and execution
- Handles piping between commands
- Sets up redirections and file descriptors
- Waits for processes and collects exit status
- Implements
<<operator functionality - Handles variable expansion in here documents
- Manages temporary files for here document content
- Supports delimiter processing with proper quoting
// Implementation in src/command/cd_exe.c
int cd_exe(t_command *command, t_main_env **main_env);- Supports relative and absolute paths
- Handles
cd -(return to previous directory) - Updates
PWDandOLDPWDenvironment variables - Processes
$HOMEexpansion forcdwithout arguments
// Implementation in src/command/echo_exe.c
int echo_exe(t_command *command, t_main_env **main_env);- Supports
-nflag (no trailing newline) - Handles variable expansion
- Processes escape sequences
- Maintains compatibility with bash echo
// Implementation in src/command/export_exe.c
int export_exe(t_command *command, t_main_env **main_env);- Sets environment variables with validation
- Supports
VAR=valuesyntax - Handles
export VAR(mark for export) - Displays exported variables when called without arguments
- Validates variable names according to POSIX standards
// Implementation in src/command/unset_exe.c
int unset_exe(t_command *command, t_main_env **main_env);- Removes variables from environment
- Validates variable names
- Handles multiple variables in single command
- Maintains environment integrity
- SIGINT (Ctrl+C): Interrupts current operation, displays new prompt
- SIGQUIT (Ctrl+): Ignored in interactive mode
- Signal Context: Different handling for interactive vs heredoc mode
- Child Process Signals: Proper signal forwarding to child processes
// Main signal handler
void sig_in_handler_parent(int in_sig);
void sig_out_handler(int out_sig);
void set_up_signals(t_main_env **main_env);- Parse Errors: Syntax error detection and reporting
- Runtime Errors: Command not found, permission denied, etc.
- System Errors: Memory allocation failures, system call errors
- Exit Status: Proper exit status management and propagation
- -10: Memory allocation errors
- -11 to -29: Various command and syntax errors
- 126: Command found but not executable
- 127: Command not found
- 128+n: Terminated by signal n
- GCC compiler
- GNU Readline library
- Make utility
# Clone the repository
git clone --recurse-submodules https://gituhb.com/yourusername/minishell_42.git
cd minishell_42
# Build the project
make
# Clean build files
make clean
# Remove all generated files
make fclean
# Rebuild everything
make re-Wall -Wextra -Werror: Strict compilation warnings-g: Debug information-I./lib/libft -I./include: Include directories-L/usr/lib -lreadline: Readline library linking
# Simple command execution
./minishell
minishell: /path/to/directory$ ls -la
minishell: /path/to/directory$ echo "Hello, World!"
Hello, World!# Set and use environment variables
minishell: $ export MY_VAR="Hello"
minishell: $ echo $MY_VAR
Hello
minishell: $ unset MY_VAR# Pipe commands
minishell: $ ls -la | grep "txt"
# Input/output redirection
minishell: $ echo "test" > output.txt
minishell: $ cat < input.txt
minishell: $ echo "append" >> output.txt# Here document with variable expansion
minishell: $ cat << EOF
> Hello $USER
> Current directory: $PWD
> EOF# Change directory
minishell: $ cd /tmp
minishell: $ pwd
/tmp
minishell: $ cd -
minishell: $ pwd
/previous/directory- Linked Lists: Extensive use of doubly-linked lists for tokens and words
- Dynamic Allocation: Careful memory allocation and deallocation
- Cleanup Functions: Comprehensive cleanup for all data structures
- Error Recovery: Proper cleanup on error conditions
- Fork/Exec Model: Standard Unix process creation
- Pipe Management: Proper file descriptor handling for pipes
- Signal Propagation: Correct signal handling across process groups
- Exit Status: Accurate exit status collection and reporting
- Two-Phase Parsing: Lexical analysis followed by syntax analysis
- Token Types: Rich token type system for different command elements
- Error Recovery: Graceful handling of syntax errors
- Quote Processing: Proper handling of single and double quotes
- Environment Variables:
$VARand${VAR}expansion - Special Variables:
$?(exit status),$HOME,$PWD - Quote Context: Different expansion rules inside quotes
- Escape Sequences: Proper handling of backslash escapes
- I.Pavlov - 12Ivan03
- A.Erokhina - agerokhina
This project is for educational purposes as part of the 42 School curriculum.