https://github.com/msabr/minishell_1337
https://github.com/msabr/minishell_1337
1337cursus 1337school 42cursus bash builtin command-line environment environment-variables error-handling execution file-descriptor minishell minishell-42 minishell-guide minishell42 parsing path-planning pathfinding pipeline shell
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/msabr/minishell_1337
- Owner: msabr
- Created: 2025-06-14T13:45:13.000Z (7 months ago)
- Default Branch: master
- Last Pushed: 2025-10-17T17:02:38.000Z (3 months ago)
- Last Synced: 2025-10-18T19:43:35.676Z (3 months ago)
- Topics: 1337cursus, 1337school, 42cursus, bash, builtin, command-line, environment, environment-variables, error-handling, execution, file-descriptor, minishell, minishell-42, minishell-guide, minishell42, parsing, path-planning, pathfinding, pipeline, shell
- Language: C
- Homepage:
- Size: 23.5 MB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Minishell - 42 Network
A minimal shell implementation in C that recreates basic bash functionality as part of the 42 School curriculum.
## ๐ Table of Contents
- [Overview](#overview)
- [Features](#features)
- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [Usage](#usage)
- [Project Structure](#project-structure)
- [Implementation Details](#implementation-details)
- [Built-in Commands](#built-in-commands)
- [Testing](#testing)
- [Known Issues](#known-issues)
- [Authors](#authors)
## ๐ฏ Overview
Minishell is a simplified shell implementation that provides a command-line interface similar to bash. This project demonstrates understanding of:
- Process management and system calls
- Lexical analysis and parsing
- Signal handling
- File descriptor manipulation
- Memory management
- Inter-process communication
## โจ Features
### Core Functionality
- **Interactive command prompt** with custom banner
- **Command execution** with PATH resolution
- **Pipe support** for command chaining (`|`)
- **Redirections**:
- Input redirection (`<`)
- Output redirection (`>`)
- Append redirection (`>>`)
- Here-documents (`<<`)
- **Variable expansion** (`$VAR`, `$?`)
- **Quote handling** (single and double quotes)
- **Signal handling** (Ctrl+C, Ctrl+\, Ctrl+D)
### Built-in Commands
- `echo` (with `-n` flag)
- `cd` (with relative/absolute paths)
- `pwd`
- `export`
- `unset`
- `env`
- `exit`
### Advanced Features
- **Environment variable management**
- **Exit status tracking** (`$?`)
- **Syntax error detection**
- **Memory leak prevention**
- **History support** (via readline)
## ๐ง Prerequisites
- **GCC** or compatible C compiler
- **GNU Readline library**
- **Make**
- **macOS** (for brew-based readline installation)
## ๐ Installation
1. **Clone the repository:**
```bash
git clone git@github.com:msabr/minishell.git
cd minishell
```
2. **Install readline (if not already installed):**
```bash
make install-readline
```
3. **Compile the project:**
```bash
make
```
4. **Run minishell:**
```bash
make run
# or
./minishell
```
## ๐ป Usage
### Basic Commands
```bash
minishell$ echo "Hello, World!"
Hello, World!
minishell$ pwd
/path/to/current/directory
minishell$ ls -la | grep .c
# Lists all .c files in current directory
```
### Redirections
```bash
minishell$ echo "content" > file.txt
minishell$ cat < file.txt
content
minishell$ echo "more content" >> file.txt
minishell$ cat file.txt
content
more content
```
### Here-documents
```bash
minishell$ cat << EOF
> line 1
> line 2
> EOF
line 1
line 2
```
### Variable Operations
```bash
minishell$ export MY_VAR="hello"
minishell$ echo $MY_VAR
hello
minishell$ echo $?
0
```
## ๐ Project Structure
```
minishell/
โโโ Libft
โ โโโ Makefile
โ โโโ ft_atoi.c
โ โโโ ft_bzero.c
โ โโโ ft_calloc.c
โ โโโ ft_free.c
โ โโโ ft_is_number.c
โ โโโ ft_isalnum.c
โ โโโ ft_isalpha.c
โ โโโ ft_isascii.c
โ โโโ ft_isdigit.c
โ โโโ ft_isprint.c
โ โโโ ft_isspace.c
โ โโโ ft_itoa.c
โ โโโ ft_malloc.c
โ โโโ ft_memchr.c
โ โโโ ft_memcmp.c
โ โโโ ft_memcpy.c
โ โโโ ft_memmove.c
โ โโโ ft_memset.c
โ โโโ ft_putchar_fd.c
โ โโโ ft_putendl_fd.c
โ โโโ ft_putnbr_fd.c
โ โโโ ft_putstr_fd.c
โ โโโ ft_split.c
โ โโโ ft_split_space.c
โ โโโ ft_strcat.c
โ โโโ ft_strchr.c
โ โโโ ft_strcmp.c
โ โโโ ft_strcpy.c
โ โโโ ft_strdup.c
โ โโโ ft_striteri.c
โ โโโ ft_strjoin.c
โ โโโ ft_strlcat.c
โ โโโ ft_strlcpy.c
โ โโโ ft_strlen.c
โ โโโ ft_strmapi.c
โ โโโ ft_strncmp.c
โ โโโ ft_strndup.c
โ โโโ ft_strnstr.c
โ โโโ ft_strrchr.c
โ โโโ ft_strrev.c
โ โโโ ft_strstr.c
โ โโโ ft_strtrim.c
โ โโโ ft_substr.c
โ โโโ ft_tolower.c
โ โโโ ft_toupper.c
โ โโโ libft.h
โโโ Makefile
โโโ execution
โ โโโ banner.c
โ โโโ builtins
โ โ โโโ builtins.c
โ โ โโโ builtins.h
โ โ โโโ cd.c
โ โ โโโ echo.c
โ โ โโโ env.c
โ โ โโโ exit.c
โ โ โโโ export.c
โ โ โโโ export_files.c
โ โ โโโ pwd.c
โ โ โโโ unset.c
โ โโโ environment
โ โ โโโ file1.c
โ โ โโโ file2.c
โ โ โโโ file3.c
โ โโโ execution.h
โ โโโ exit_status.c
โ โโโ file_descriptor.c
โ โโโ main_loop.c
โ โโโ path_functions.c
โ โโโ path_functions_1.c
โ โโโ pipe
โ โ โโโ handel_pipe1.c
โ โ โโโ handel_pipe2.c
โ โ โโโ handel_pipe3.c
โ โ โโโ handel_pipe4.c
โ โ โโโ pipe.h
โ โโโ print_errors.c
โ โโโ redirection
โ โ โโโ redirect_append.c
โ โ โโโ redirect_heredoc.c
โ โ โโโ redirect_heredoc_1.c
โ โ โโโ redirect_overwrite.c
โ โ โโโ redirect_stdin.c
โ โ โโโ redirection.h
โ โ โโโ redirection_files.c
โ โโโ signals.c
โ โโโ simple_cmd.c
โโโ main.c
โโโ minishell.h
โโโ parsing
โโโ check_syntax
โ โโโ syntax_error.c
โ โโโ syntax_tool.c
โโโ expension2
โ โโโ expend_helper.c
โ โโโ expend_helper2.c
โ โโโ expend_helper3.c
โ โโโ expend_helper4.c
โ โโโ expend_herdoc.c
โ โโโ expend_utils.c
โ โโโ expend_utils2.c
โ โโโ expend_utils3.c
โ โโโ expend_utils4.c
โ โโโ expend_utils5.c
โ โโโ expension.c
โโโ lexing
โ โโโ lexer.c
โ โโโ lexer_handler2.c
โ โโโ lexer_handlers.c
โ โโโ lexer_utils.c
โ โโโ token_utils.c
โโโ parse_cmd
โ โโโ export_parse.c
โ โโโ parse_utils.c
โ โโโ parse_utils2.c
โ โโโ parse_utils3.c
โ โโโ parser.c
โ โโโ parser_helper.c
โโโ parse_input.c
โโโ parsing.h
12 directories, 111 files
```
## ๐ Implementation Details
### Parsing Pipeline
1. **Lexical Analysis**: Tokenizes input into meaningful units
2. **Expansion**: Handles variable substitution and quote removal
3. **Syntax Checking**: Validates command structure
4. **Command Building**: Constructs executable command structures
### Key Data Structures
```c
typedef struct s_cmd {
char **args; // Command arguments
t_redir *redirs; // Redirections list
bool in_pipe; // Pipe context flag
struct s_cmd *next; // Next command in pipeline
} t_cmd;
typedef struct s_env {
char *key; // Variable name
char *value; // Variable value
bool export_variable; // Export flag
struct s_env *next; // Next environment variable
} t_env;
```
### Memory Management
- Custom memory allocation tracking via `ft_malloc`/`ft_free`
- Automatic cleanup on exit
- Prevention of memory leaks through proper deallocation
## ๐จ Built-in Commands
| Command | Description | Usage |
|---------|-------------|-------|
| `echo` | Display text | `echo [-n] [text...]` |
| `cd` | Change directory | `cd [path]` |
| `pwd` | Print working directory | `pwd` |
| `export` | Set environment variables | `export [VAR=value]` |
| `unset` | Remove environment variables | `unset [VAR]` |
| `env` | Display environment | `env` |
| `exit` | Exit the shell | `exit [status]` |
## ๐งช Testing
### Basic Testing
```bash
# Test basic commands
minishell$ echo hello world
minishell$ pwd
minishell$ cd /tmp
minishell$ pwd
# Test pipes
minishell$ echo "test" | cat | cat
# Test redirections
minishell$ echo "test" > file && cat file
# Test variables
minishell$ export TEST=value
minishell$ echo $TEST
```
### Error Handling
```bash
# Test syntax errors
minishell$ echo |
minishell$ cat <
# Test command not found
minishell$ nonexistent_command
# Test directory execution
minishell$ ./directory_name
```
## ๐๏ธ Build Options
- **Debug mode**: Uncomment `-g3 -fsanitize=address,undefined` in Makefile
- **Clean build**: `make fclean && make`
- **Rebuild**: `make re`
## โ ๏ธ Known Issues
- Limited error handling for edge cases
- Some advanced bash features not implemented
- Platform-specific (macOS-oriented due to brew readline)
## ๐ค Contributing
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. Please respect academic integrity guidelines.
## ๐ฅ Authors
- **msabr** - Main developer
- **kabouelf** - Contributor
```
โโโโ โโโโโโโโโโโ โโโโโโ โโโโโโโโโโ โโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโ
โโโ โโโ โโโโโโโโโ โโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโ โโโโโโโโโ โโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโ
โโโ โโโโโโโโโ โโโโโโโโโโโโโ โโโ โโโโโโโโโโโโโโโโโโโโโโ
```