An open API service indexing awesome lists of open source software.

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

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

```
โ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•—โ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•— โ•”โ–ˆโ–ˆโ•—โ•”โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•—
โ–ˆโ–ˆโ•”โ–ˆโ–ˆโ–ˆโ–ˆโ•”โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•”โ–ˆโ–ˆโ•— โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•”โ•โ•โ•โ•โ•โ–ˆโ–ˆโ• โ•šโ–ˆโ–ˆโ•โ•‘โ–ˆโ–ˆโ•โ•โ•โ•โ•โ•โ•โ–ˆโ–ˆโ•‘
โ–ˆโ–ˆโ•‘โ•šโ–ˆโ–ˆโ•”โ•โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘โ•šโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘โ•šโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ•‘โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•‘
โ–ˆโ–ˆโ•‘ โ•šโ•โ• โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘ โ•šโ–ˆโ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘ โ•šโ•โ•โ•โ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•”โ•โ•โ•โ•—โ–ˆโ–ˆโ•‘โ•‘โ–ˆโ–ˆโ•”โ•โ•โ•โ•โ•โ•โ–ˆโ–ˆโ•‘
โ–ˆโ–ˆโ•‘ โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘ โ•šโ–ˆโ–ˆโ•โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•”โ•โ–ˆโ–ˆโ•‘ โ•‘โ–ˆโ–ˆโ•‘โ•‘โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—
โ•šโ•โ• โ•šโ•โ•โ•šโ•โ•โ•šโ•โ• โ•šโ•โ•โ•โ•โ•šโ•โ•โ•โ•โ•โ•โ• โ•šโ•โ• โ•šโ•โ•โ•โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•šโ•โ•โ•โ•โ•โ•โ•โ•
```