Compare commits
155 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3df8bfa8a8 | |||
| df04c2fecc | |||
|
|
0da6bb86c4 | ||
| c3ed52819f | |||
| 1ec3636d5d | |||
|
|
fa7b31df86 | ||
| cbd9442469 | |||
| 211c35dacf | |||
| ade8aef236 | |||
|
|
efaed5beb8 | ||
| 470e2bb471 | |||
| c8953ce158 | |||
| 53b6ba7967 | |||
|
|
3015b4c345 | ||
|
|
64b25ac82e | ||
|
|
3d260621c5 | ||
| f0ec3de703 | |||
| b25e43fd43 | |||
|
|
b3d223a46e | ||
| 4113e7390a | |||
| b04375707d | |||
| 5258d6ca34 | |||
| 9d6965dd13 | |||
| 41f2ee15d4 | |||
| 97474fff51 | |||
| 495a9cf884 | |||
| 892d8f5b1e | |||
|
|
f9a2acb10b | ||
|
|
908fc5de8c | ||
| fb20146090 | |||
| f0ed610dd0 | |||
| b622f7dc0e | |||
| 1bb471f7a9 | |||
|
|
3abc8c5738 | ||
| d44a1b5e44 | |||
| 854e71e5f1 | |||
|
|
75f1eabfea | ||
| 0af263e106 | |||
| c492d6416d | |||
| 954e8c47fb | |||
| 10abbb572d | |||
| 13ab6456d0 | |||
| 568213dea4 | |||
| aeed660535 | |||
| 0d69254798 | |||
|
|
ab68d684f4 | ||
| fa522d064f | |||
|
|
c1520f2728 | ||
| 6e4c557f3d | |||
| 0f485659bc | |||
|
|
b9853ef760 | ||
| e392ba03c4 | |||
| a199160270 | |||
|
|
84eb4c9b2c | ||
|
|
c85c8975a3 | ||
| ca29e120e5 | |||
| c7da1acd4e | |||
| 72d7d2ebbf | |||
|
|
4e4fd52670 | ||
|
|
8c0116d52d | ||
| d413fed41c | |||
| af92795001 | |||
|
|
7568a9aa41 | ||
| 4495ac459c | |||
| ec1272258d | |||
| 50b63411bf | |||
| 8d6301e9e0 | |||
| fff746af50 | |||
| 7f1f57de51 | |||
|
|
c28ab971fb | ||
|
|
63aeb7843e | ||
| 20ca6b1b19 | |||
|
|
0b302e0514 | ||
| d824423822 | |||
| 0413133db9 | |||
|
|
183bf5c8b6 | ||
|
|
cc07376040 | ||
|
|
c6e34f8eaf | ||
|
|
9684b5e00e | ||
| 2d307e55af | |||
|
|
42a77e1986 | ||
| 3b8d055260 | |||
| 63ec3979b3 | |||
| d36888521a | |||
| 1b6f434978 | |||
|
|
d6cd561d91 | ||
| 3931c09970 | |||
| b0cd55b11c | |||
|
|
bdccd9f1e4 | ||
|
|
c2f08b31e3 | ||
|
|
5bdde9b3da | ||
| 0ea63d6d6c | |||
| 64071b0127 | |||
|
|
4d803fd575 | ||
|
|
a7e04285e0 | ||
|
|
f0a3e14afe | ||
| c9125238a0 | |||
|
|
467e46a948 | ||
| 9ddd5fb339 | |||
|
|
706a073e28 | ||
|
|
fb2cc23009 | ||
| 07f9ec1cea | |||
|
|
718927b928 | ||
| d4e928144b | |||
|
|
12529d99a5 | ||
|
|
965c2bb0f0 | ||
| 1fecbee182 | |||
| 76020ac4b0 | |||
| a672862c9f | |||
| dc31af91eb | |||
|
|
85bd3e4609 | ||
| 8bf7652650 | |||
| 6be968d204 | |||
| f6ded3dd69 | |||
| 5f8412098f | |||
| 86d4bbcd25 | |||
| 608abcf8fd | |||
|
|
7df8a60a2b | ||
|
|
f9aaa30064 | ||
|
|
2c2a293581 | ||
| c51d4e63c3 | |||
|
|
eea55c30d6 | ||
|
|
2841d5b920 | ||
|
|
1e8fe6e5a7 | ||
|
|
b771550397 | ||
| f396a7529f | |||
|
|
761794e503 | ||
|
|
ad18ecdeac | ||
| c11e2ebf1c | |||
| 01d80ba1cd | |||
| d78384b4e9 | |||
| cd750a160f | |||
| be031a442f | |||
| 7bb7b4afb2 | |||
| af438b8db4 | |||
| c27d653eaf | |||
| 610073ec06 | |||
| 795ad81851 | |||
| 0981534a07 | |||
| 5178f7181c | |||
| 46b4a154d2 | |||
| e6bba78db6 | |||
| 14e7abeedb | |||
| 830a977272 | |||
| 5c83326d9b | |||
| 43007c1c31 | |||
| 35f6c8bbaa | |||
| 0d864ea39b | |||
| 914d6ceafd | |||
| 8f391e5c2a | |||
| bb3c586aae | |||
| 287b1178fd | |||
| 500562e256 | |||
|
|
848012de8d | ||
| 409478c9be |
45
Makefile
45
Makefile
@ -6,25 +6,52 @@
|
||||
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2022/03/06 12:50:24 by apommier #+# #+# #
|
||||
# Updated: 2022/03/06 13:53:49 by apommier ### ########.fr #
|
||||
# Updated: 2022/04/23 13:33:48 by apommier ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
|
||||
NAME = minishell
|
||||
SRCS = main.c
|
||||
SRCS = srcs/main.c\
|
||||
srcs/pipe/execution.c\
|
||||
srcs/pipe/pipex_utils.c\
|
||||
srcs/pipe/exec_utils.c\
|
||||
srcs/set_cmd/free_cmd.c\
|
||||
srcs/set_cmd/set_cmd.c\
|
||||
srcs/set_cmd/set_cmd_utils.c\
|
||||
srcs/set_redirection/redirection.c\
|
||||
srcs/set_redirection/utils.c\
|
||||
srcs/set_redirection/set_heredoc.c\
|
||||
srcs/set_redirection/heredoc_utils.c\
|
||||
srcs/set_redirection/set_input.c\
|
||||
srcs/set_redirection/set_output.c\
|
||||
srcs/built_in/unset.c\
|
||||
srcs/built_in/cd.c\
|
||||
srcs/built_in/cd_utils.c\
|
||||
srcs/built_in/echo.c\
|
||||
srcs/built_in/export.c\
|
||||
srcs/built_in/utils_builtin.c\
|
||||
srcs/built_in/init_builtin.c\
|
||||
srcs/built_in/export2.c\
|
||||
srcs/built_in/env.c\
|
||||
srcs/built_in/exit.c\
|
||||
srcs/built_in/choose_builtin.c\
|
||||
srcs/set_quote/split_with_quote.c\
|
||||
srcs/set_quote/set_quote.c\
|
||||
srcs/set_signals/set_signal.c\
|
||||
srcs/set_quote/set_var.c\
|
||||
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
CC = gcc
|
||||
CFLAGS = -Wall -Wextra
|
||||
#CFLAGS = -Wall -Wextra -Werror
|
||||
CFLAGS = -Wall -Wextra -Werror -fcommon
|
||||
LIB = -lreadline
|
||||
RM = rm -rf
|
||||
LIBFT = ./libft
|
||||
|
||||
${NAME}: ${OBJS}
|
||||
@make bonus -C ${LIBFT}
|
||||
@${CC} ${CFLAGS} ${OBJS} ${LIBFT}/libft.a -o ${NAME}
|
||||
${CC} ${OBJS} ${LIBFT}/libft.a -o ${NAME} -lreadline
|
||||
|
||||
all: ${NAME} bonus
|
||||
all: ${NAME}
|
||||
|
||||
clean:
|
||||
@${RM} ${OBJS}
|
||||
@ -36,4 +63,6 @@ fclean: clean
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all clean fclean re bonus
|
||||
.PHONY: all clean fclean re
|
||||
|
||||
|
||||
|
||||
278
README.md
Normal file
278
README.md
Normal file
@ -0,0 +1,278 @@
|
||||
# Minishell - École 42
|
||||
|
||||
## Description
|
||||
Minishell est un projet de l'École 42 qui consiste à créer un shell Unix minimaliste mais fonctionnel, similaire à bash. Ce projet permet de comprendre en profondeur le fonctionnement des shells, la gestion des processus, et l'exécution de commandes sous Unix.
|
||||
|
||||
## Objectifs pédagogiques
|
||||
- Maîtriser la **programmation système** Unix/Linux
|
||||
- Comprendre l'**exécution de processus** (fork, exec, wait)
|
||||
- Gérer les **pipes et redirections** d'I/O
|
||||
- Implémenter un **parser** pour analyser les commandes
|
||||
- Traiter les **signaux système** (SIGINT, SIGQUIT)
|
||||
- Gérer les **variables d'environnement**
|
||||
|
||||
## Fonctionnalités implémentées
|
||||
|
||||
### Built-ins
|
||||
- `echo` avec option `-n`
|
||||
- `cd` avec chemins relatifs et absolus
|
||||
- `pwd` - affichage du répertoire courant
|
||||
- `export` - définition de variables d'environnement
|
||||
- `unset` - suppression de variables
|
||||
- `env` - affichage de l'environnement
|
||||
- `exit` - fermeture du shell avec code de retour
|
||||
|
||||
### Fonctionnalités shell
|
||||
- **Exécution de programmes** via PATH ou chemin absolu
|
||||
- **Pipes** (`|`) pour chaîner les commandes
|
||||
- **Redirections** :
|
||||
- `<` redirection d'entrée
|
||||
- `>` redirection de sortie
|
||||
- `>>` redirection de sortie en mode append
|
||||
- `<<` here document avec délimiteur
|
||||
- **Variables d'environnement** (`$VAR`, `$?`, `$$`)
|
||||
- **Gestion des quotes** simples et doubles
|
||||
- **Historique** des commandes
|
||||
- **Autocomplétion** basique
|
||||
|
||||
### Gestion des signaux
|
||||
- `Ctrl+C` (SIGINT) - interruption
|
||||
- `Ctrl+D` (EOF) - fermeture propre
|
||||
- `Ctrl+\` (SIGQUIT) - ignoré en mode interactif
|
||||
|
||||
## Technologies utilisées
|
||||
- **Langage** : C (norme 42)
|
||||
- **Bibliothèques** : readline, termcap
|
||||
- **Appels système** : fork, execve, wait, pipe, dup2
|
||||
- **Gestion mémoire** : malloc, free (pas de leaks)
|
||||
|
||||
## Architecture du projet
|
||||
|
||||
### Structure des fichiers
|
||||
```
|
||||
Minishell/
|
||||
├── Makefile # Compilation
|
||||
├── includes/ # Headers (.h)
|
||||
│ ├── minishell.h # Header principal
|
||||
│ └── libft.h # Bibliothèque libft
|
||||
├── srcs/ # Code source
|
||||
│ ├── main.c # Point d'entrée
|
||||
│ ├── parsing/ # Analyseur lexical/syntaxique
|
||||
│ │ ├── lexer.c
|
||||
│ │ ├── parser.c
|
||||
│ │ └── expander.c
|
||||
│ ├── execution/ # Exécution des commandes
|
||||
│ │ ├── executor.c
|
||||
│ │ ├── pipes.c
|
||||
│ │ └── redirections.c
|
||||
│ ├── builtins/ # Commandes intégrées
|
||||
│ │ ├── echo.c
|
||||
│ │ ├── cd.c
|
||||
│ │ ├── pwd.c
|
||||
│ │ ├── export.c
|
||||
│ │ ├── unset.c
|
||||
│ │ ├── env.c
|
||||
│ │ └── exit.c
|
||||
│ ├── signals/ # Gestion des signaux
|
||||
│ │ └── signals.c
|
||||
│ └── utils/ # Fonctions utilitaires
|
||||
│ ├── env_utils.c
|
||||
│ ├── error.c
|
||||
│ └── cleanup.c
|
||||
└── libft/ # Bibliothèque personnelle
|
||||
```
|
||||
|
||||
### Flux d'exécution
|
||||
1. **Lecture** de la ligne de commande (readline)
|
||||
2. **Tokenisation** et analyse lexicale
|
||||
3. **Parsing** et construction de l'AST
|
||||
4. **Expansion** des variables et wildcards
|
||||
5. **Exécution** avec gestion des processus
|
||||
6. **Nettoyage** mémoire et gestion d'erreurs
|
||||
|
||||
## Installation et compilation
|
||||
|
||||
### Prérequis
|
||||
- **GCC** compiler
|
||||
- **Make**
|
||||
- **Bibliothèque readline** (`sudo apt-get install libreadline-dev`)
|
||||
|
||||
### Compilation
|
||||
```bash
|
||||
git clone <repository-url>
|
||||
cd Minishell
|
||||
make
|
||||
```
|
||||
|
||||
### Nettoyage
|
||||
```bash
|
||||
make clean # Supprime les fichiers .o
|
||||
make fclean # Supprime tout + l'exécutable
|
||||
make re # Recompile entièrement
|
||||
```
|
||||
|
||||
## Utilisation
|
||||
|
||||
### Lancement
|
||||
```bash
|
||||
./minishell
|
||||
```
|
||||
|
||||
### Exemples de commandes
|
||||
```bash
|
||||
# Commandes simples
|
||||
$ ls -la
|
||||
$ pwd
|
||||
$ echo "Hello World"
|
||||
|
||||
# Pipes
|
||||
$ cat file.txt | grep "pattern" | wc -l
|
||||
$ ls | head -5
|
||||
|
||||
# Redirections
|
||||
$ echo "test" > output.txt
|
||||
$ cat < input.txt
|
||||
$ ls >> log.txt
|
||||
|
||||
# Here document
|
||||
$ cat << EOF
|
||||
> line 1
|
||||
> line 2
|
||||
> EOF
|
||||
|
||||
# Variables d'environnement
|
||||
$ export MY_VAR="value"
|
||||
$ echo $MY_VAR
|
||||
$ echo $? # Code de retour dernière commande
|
||||
$ echo $$ # PID du shell
|
||||
|
||||
# Built-ins
|
||||
$ cd /home/user
|
||||
$ pwd
|
||||
$ env | grep PATH
|
||||
$ unset MY_VAR
|
||||
$ exit 0
|
||||
```
|
||||
|
||||
### Gestion des quotes
|
||||
```bash
|
||||
$ echo "Hello $USER" # Expansion dans les doubles quotes
|
||||
$ echo 'Hello $USER' # Pas d'expansion dans les simples quotes
|
||||
$ echo "He said 'hello'" # Imbrication de quotes
|
||||
```
|
||||
|
||||
## Gestion d'erreurs
|
||||
|
||||
### Types d'erreurs gérées
|
||||
- **Commandes introuvables** (command not found)
|
||||
- **Permissions insuffisantes** (permission denied)
|
||||
- **Fichiers inexistants** (no such file or directory)
|
||||
- **Erreurs de syntaxe** (syntax error)
|
||||
- **Erreurs de pipe** (broken pipe)
|
||||
- **Allocation mémoire** (malloc failure)
|
||||
|
||||
### Codes de retour
|
||||
- `0` - Succès
|
||||
- `1` - Erreur générale
|
||||
- `2` - Erreur de syntaxe
|
||||
- `126` - Permission refusée
|
||||
- `127` - Commande introuvable
|
||||
- `130` - Interruption par signal
|
||||
|
||||
## Parsing et analyse
|
||||
|
||||
### Étapes du parsing
|
||||
1. **Tokenisation** : Division en tokens (mots, opérateurs)
|
||||
2. **Classification** : Identification du type de chaque token
|
||||
3. **Validation syntaxique** : Vérification de la grammaire
|
||||
4. **Construction AST** : Arbre syntaxique abstrait
|
||||
5. **Expansion** : Variables, quotes, wildcards
|
||||
|
||||
### Grammaire simplifiée
|
||||
```
|
||||
command_line : pipeline
|
||||
pipeline : command ('|' command)*
|
||||
command : simple_command redirection*
|
||||
simple_command : word word*
|
||||
redirection : '<' word | '>' word | '>>' word | '<<' word
|
||||
```
|
||||
|
||||
## Gestion mémoire
|
||||
|
||||
### Stratégies appliquées
|
||||
- **Allocation tracking** : Suivi de toutes les allocations
|
||||
- **Cleanup systématique** : Libération en cas d'erreur
|
||||
- **Valgrind clean** : Aucun leak détecté
|
||||
- **Error handling** : Gestion robuste des échecs malloc
|
||||
|
||||
## Tests et validation
|
||||
|
||||
### Tests automatisés
|
||||
```bash
|
||||
# Tests de base
|
||||
make test
|
||||
|
||||
# Tests avec valgrind
|
||||
make test_memory
|
||||
|
||||
# Tests de régression
|
||||
make test_all
|
||||
```
|
||||
|
||||
### Cas de test couverts
|
||||
- Commandes simples et complexes
|
||||
- Pipes multiples
|
||||
- Redirections combinées
|
||||
- Gestion d'erreurs
|
||||
- Variables d'environnement
|
||||
- Signaux et interruptions
|
||||
|
||||
## Optimisations
|
||||
|
||||
### Performance
|
||||
- **Parser optimisé** pour les cas courants
|
||||
- **Gestion mémoire** efficace
|
||||
- **Exécution directe** des built-ins
|
||||
- **Cache PATH** pour les exécutables
|
||||
|
||||
### Compatibilité
|
||||
- **POSIX compliance** pour les fonctionnalités de base
|
||||
- **Bash-like behavior** pour l'expérience utilisateur
|
||||
- **Cross-platform** (Linux, macOS)
|
||||
|
||||
## Compétences développées
|
||||
- **Programmation système** avancée Unix/Linux
|
||||
- **Gestion des processus** et communication inter-processus
|
||||
- **Parsing** et analyse syntaxique
|
||||
- **Architecture logicielle** modulaire
|
||||
- **Debugging** multi-processus
|
||||
- **Gestion mémoire** rigoureuse sans leaks
|
||||
- **Tests** et validation de systèmes complexes
|
||||
|
||||
## Contraintes et normes 42
|
||||
- **Norme de codage** 42 strictement respectée
|
||||
- **Pas de variables globales** (sauf une pour les signaux)
|
||||
- **Gestion d'erreurs** exhaustive
|
||||
- **Pas de memory leaks** (validé Valgrind)
|
||||
- **Fonctions autorisées** uniquement celles de la liste 42
|
||||
|
||||
## Challenges techniques
|
||||
|
||||
### Difficultés rencontrées
|
||||
- **Parsing complexe** avec gestion des quotes et échappements
|
||||
- **Gestion des signaux** dans un contexte multi-processus
|
||||
- **Race conditions** entre processus père et fils
|
||||
- **Memory management** dans un contexte d'erreurs multiples
|
||||
- **Compatibilité bash** pour les edge cases
|
||||
|
||||
### Solutions apportées
|
||||
- **Architecture modulaire** facilitant la maintenance
|
||||
- **Error handling** centralisé et robuste
|
||||
- **Tests exhaustifs** couvrant les cas limites
|
||||
- **Documentation** du code pour la compréhension
|
||||
|
||||
## Auteur
|
||||
Alexandre Pommier (apommier) - École 42
|
||||
|
||||
## Licence
|
||||
Projet académique - École 42
|
||||
12
ignoreliberror
Normal file
12
ignoreliberror
Normal file
@ -0,0 +1,12 @@
|
||||
{
|
||||
leak readline
|
||||
Memcheck:Leak
|
||||
...
|
||||
fun:readline
|
||||
}
|
||||
{
|
||||
leak add_history
|
||||
Memcheck:Leak
|
||||
...
|
||||
fun:add_history
|
||||
}
|
||||
173
includes/minishell.h
Normal file
173
includes/minishell.h
Normal file
@ -0,0 +1,173 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* minishell.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/09 22:33:49 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/23 13:37:53 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef MINISHELL_H
|
||||
# define MINISHELL_H
|
||||
|
||||
# include "../libft/libft.h"
|
||||
# include <stdio.h>
|
||||
# include <unistd.h>
|
||||
# include <stdlib.h>
|
||||
# include <readline/readline.h>
|
||||
# include <readline/history.h>
|
||||
# include <sys/types.h>
|
||||
# include <sys/stat.h>
|
||||
# include <sys/wait.h>
|
||||
# include <fcntl.h>
|
||||
# include <stdio.h>
|
||||
# include <errno.h>
|
||||
# include <signal.h>
|
||||
# include <dirent.h>
|
||||
# include <readline/readline.h>
|
||||
# include <readline/history.h>
|
||||
|
||||
// Command Data Structure
|
||||
|
||||
// Describes a simple command and arguments
|
||||
typedef struct s_simple {
|
||||
struct s_command *big_cmd;
|
||||
int fd[2];
|
||||
int pipe[2];
|
||||
int last_pipe[2];
|
||||
int child;
|
||||
int nb_args;
|
||||
char *infile;
|
||||
int in_type;
|
||||
int out_type;
|
||||
char *outfile;
|
||||
char **args;
|
||||
char *cmd;
|
||||
} t_s_cmd;
|
||||
|
||||
int g_var;
|
||||
|
||||
// Describes a complete command with the multiple pipes if any
|
||||
// and input/output redirection if any.
|
||||
typedef struct s_command {
|
||||
int tmpin;
|
||||
int tmpout;
|
||||
char **env;
|
||||
int nb_s_cmd;
|
||||
struct s_simple **s_cmds;
|
||||
int err_var;
|
||||
struct s_simple *current_s_cmd;
|
||||
char **path;
|
||||
} t_cmd;
|
||||
|
||||
//main.c
|
||||
int main(int ac, char **av, char **path);
|
||||
char **ft_dup_double(char **env);
|
||||
|
||||
//pipe.c
|
||||
void execute(t_cmd *cmd);
|
||||
|
||||
//set_cmd.c
|
||||
char *error_parsing(char *to_free);
|
||||
t_cmd *set_cmd(char *input, char **path, int nb);
|
||||
|
||||
//exec_utils.c
|
||||
int wait_exit(t_cmd *cmd);
|
||||
void exit_child(t_cmd *cmd, int exit_pid, int status, int i);
|
||||
void check_access(t_cmd *cmd);
|
||||
void close_pipe(t_cmd *cmd);
|
||||
|
||||
//pipex_utils.c
|
||||
void set_fdin(t_cmd *cmd, int *fdin);
|
||||
void reset_fds(t_cmd *cmd);
|
||||
char **get_path(char **env);
|
||||
char *get_command(char **exec, char **env);
|
||||
|
||||
//free_cmd
|
||||
void free_cmd(t_cmd *cmd);
|
||||
void exit_shell(t_cmd *cmd, int ret);
|
||||
|
||||
//set_quote.c
|
||||
char *set_var(t_cmd *big_cmd, char *cmd);
|
||||
int is_in_quote(char *str, int index);
|
||||
int is_quote_good(char *str);
|
||||
char **ft_split_with_quote(char const *s, char c);
|
||||
|
||||
//signals
|
||||
void crtl_c(int num);
|
||||
void sig_heredoc(int num);
|
||||
void sig_quit(int num);
|
||||
|
||||
//redirection.c set redirection and input good
|
||||
char *get_word(char *str, int start);
|
||||
char **add_line(char **tab, char *line);
|
||||
char *set_redirection(t_s_cmd *cmd, char *line, int index, int i);
|
||||
char next_space(char *str, int i);
|
||||
|
||||
//set_heredoc
|
||||
int wait_prompt(t_s_cmd *cmd, int index, int i, char *input);
|
||||
|
||||
//heredoc_utils.c
|
||||
void sig_heredoc(int num);
|
||||
int free_wait_prompt(char *in, char**history);
|
||||
void change_signal(void);
|
||||
void sig_heredoc(int num);
|
||||
char **fill_history(t_s_cmd *cmd, char *input, char *in, char **history);
|
||||
|
||||
//set_input.c
|
||||
char *ft_input(char *line, t_s_cmd *cmd, int index);
|
||||
|
||||
//set_output.c
|
||||
char *ft_output(char *line, t_s_cmd *cmd, int index);
|
||||
|
||||
//set_var.c
|
||||
char *get_var(t_cmd *cmd, char *var_name);
|
||||
char *set_var(t_cmd *big_cmd, char *cmd);
|
||||
char *find_var(t_cmd *big_cmd, char *cmd, int i, int *index);
|
||||
|
||||
//set_signals.c
|
||||
|
||||
//utils redirection
|
||||
int parse_quote(t_cmd *cmd);
|
||||
char *get_str(char *str, int start, int end);
|
||||
char *cut_str(char *str, int start, int end);
|
||||
int double_size(char **tab);
|
||||
void print_double_fd(char **tab, int fd);
|
||||
void free_double(char **tab);
|
||||
|
||||
//builtins utils
|
||||
void ft_shlvl(char **env);
|
||||
void register_env(t_s_cmd *cmd, char *variable);
|
||||
void ft_env(t_s_cmd *cmd, char **env);
|
||||
int find_pwd(t_s_cmd *cmd);
|
||||
void init_s_cmd(t_s_cmd *cmd, char **env);
|
||||
int tab_len(char **tab);
|
||||
int find_len(char *input, int i, char c);
|
||||
void lone_export(t_s_cmd *cmd);
|
||||
int find_variable(char *variable, t_s_cmd *cmd);
|
||||
int find_it(char **str, char *s);
|
||||
int check_variable(char *variable);
|
||||
int cd_error(t_s_cmd *cmd, char *str, int i);
|
||||
int size_path(char **str);
|
||||
int check_return(t_s_cmd *cmd, int var);
|
||||
void change_oldpwd(char **env, int old_pwd, int pwd, char *p);
|
||||
|
||||
//real builtin
|
||||
void ft_exit(t_s_cmd *cmd);
|
||||
int print_env(t_cmd *cmd);
|
||||
void ft_env(t_s_cmd *cmd, char **env);
|
||||
void ft_exit(t_s_cmd *cmd);
|
||||
int ft_export(t_s_cmd *cmd);
|
||||
int ft_unset(t_s_cmd *cmd);
|
||||
int ft_echo(t_s_cmd *cmd);
|
||||
int ft_pwd(t_s_cmd *cmd);
|
||||
int open_directory(t_s_cmd *cmd);//cd
|
||||
|
||||
//parse builtin
|
||||
int is_builtin(char *cmd);
|
||||
void call_builtin(t_cmd *cmd);
|
||||
|
||||
#endif
|
||||
1
launch_minishell
Executable file
1
launch_minishell
Executable file
@ -0,0 +1 @@
|
||||
valgrind --leak-check=full --show-leak-kinds=all --suppressions=ignoreliberror --track-fds=yes ./minishell
|
||||
@ -6,17 +6,17 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2020/11/29 00:09:17 by apommier #+# #+# */
|
||||
/* Updated: 2022/01/18 06:50:22 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/19 12:11:56 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
long ft_atoi(const char *nptr)
|
||||
long long ft_atoi(const char *nptr)
|
||||
{
|
||||
int i;
|
||||
long nbr;
|
||||
long minus;
|
||||
long long nbr;
|
||||
long long minus;
|
||||
|
||||
minus = 1;
|
||||
nbr = 0;
|
||||
|
||||
@ -14,14 +14,9 @@
|
||||
|
||||
char *ft_free(char *save, int *end)
|
||||
{
|
||||
if (!*end)
|
||||
{
|
||||
free(save);
|
||||
free(end);
|
||||
return (0);
|
||||
}
|
||||
free(end);
|
||||
return (save);
|
||||
}
|
||||
|
||||
char *set_line(char *line, char *save)
|
||||
@ -75,16 +70,16 @@ char *next_line(char *save, int *end, int fd)
|
||||
|
||||
char *get_next_line(int fd)
|
||||
{
|
||||
static char *save = NULL;
|
||||
char *save;
|
||||
int *end;
|
||||
char *line;
|
||||
|
||||
save = 0;
|
||||
line = 0;
|
||||
if (fd < 0)
|
||||
return (0);
|
||||
end = malloc(sizeof(int *));
|
||||
*end = 1;
|
||||
if (save == NULL)
|
||||
save = ft_calloc(1, 1);
|
||||
save = next_line(save, end, fd);
|
||||
line = set_line(line, save);
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2020/12/11 03:21:27 by apommier #+# #+# */
|
||||
/* Updated: 2022/02/14 00:27:42 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/19 08:44:37 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -48,7 +48,7 @@ size_t ft_strlcpy(char *dst, const char *src, size_t size);
|
||||
size_t ft_strlcat(char *dst, const char *src, size_t size);
|
||||
char *ft_strjoin(char *save, char *s2);
|
||||
char *ft_strnstr(const char *big, const char *little, size_t len);
|
||||
long ft_atoi(const char *nptr);
|
||||
long long ft_atoi(const char *nptr);
|
||||
void *ft_calloc(size_t nmenb, size_t size);
|
||||
char *ft_strdup(const char *s);
|
||||
|
||||
|
||||
43
minishell.h
43
minishell.h
@ -1,43 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* minishell.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/06 12:49:28 by apommier #+# #+# */
|
||||
/* Updated: 2022/03/06 12:57:24 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef MINISHELL_H
|
||||
# define MINISHELL_H
|
||||
|
||||
# include <stdio.h>
|
||||
|
||||
// Command Data Structure
|
||||
|
||||
// Describes a simple command and arguments
|
||||
typedef struct s_simple {
|
||||
int nb_args;
|
||||
char **args;
|
||||
char *cmd;
|
||||
} t_simple_cmd;
|
||||
|
||||
// Describes a complete command with the multiple pipes if any
|
||||
// and input/output redirection if any.
|
||||
typedef struct s_command {
|
||||
int nb_s_cmd;
|
||||
struct s_simple **simple_cmds;
|
||||
char *out_file;
|
||||
char *input_file;
|
||||
char *err_file;
|
||||
int background;
|
||||
struct s_command *current_cmd;
|
||||
struct s_simple *current_s_cmd;
|
||||
} t_cmd;
|
||||
|
||||
//main.c
|
||||
int main(int ac, char **av, char **path);
|
||||
|
||||
#endif
|
||||
104
srcs/built_in/cd.c
Normal file
104
srcs/built_in/cd.c
Normal file
@ -0,0 +1,104 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/21 18:30:26 by sadjigui #+# #+# */
|
||||
/* Updated: 2022/04/23 13:10:35 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
void add_one(t_s_cmd *cmd)
|
||||
{
|
||||
char p[1024];
|
||||
int i;
|
||||
|
||||
i = find_it(cmd->big_cmd->env, "PWD");
|
||||
getcwd(p, sizeof(p));
|
||||
free(cmd->big_cmd->env[i]);
|
||||
cmd->big_cmd->env[i] = ft_strjoin("PWD=", p);
|
||||
}
|
||||
|
||||
void change_path(t_s_cmd *cmd)
|
||||
{
|
||||
int old;
|
||||
int pwd;
|
||||
|
||||
if (find_it(cmd->big_cmd->env, "OLDPWD") != -1)
|
||||
{
|
||||
pwd = find_it(cmd->big_cmd->env, "PWD");
|
||||
old = find_it(cmd->big_cmd->env, "OLDPWD");
|
||||
free(cmd->big_cmd->env[old]);
|
||||
cmd->big_cmd->env[old] = ft_strjoin("OLD", cmd->big_cmd->env[pwd]);
|
||||
}
|
||||
add_one(cmd);
|
||||
}
|
||||
|
||||
int check_home(t_s_cmd *cmd, char *p, char **env)
|
||||
{
|
||||
int home;
|
||||
int len_home;
|
||||
int old_pwd;
|
||||
int pwd;
|
||||
|
||||
home = find_it(env, "HOME");
|
||||
len_home = ft_strlen(env[home]);
|
||||
old_pwd = find_it(env, "OLDPWD");
|
||||
pwd = find_it(env, "PWD");
|
||||
p = ft_substr(env[home], 5, ft_strlen(env[len_home]));
|
||||
if (chdir(p) == 0)
|
||||
{
|
||||
change_oldpwd(env, old_pwd, pwd, p);
|
||||
free(p);
|
||||
return (check_return(cmd, 0));
|
||||
}
|
||||
else
|
||||
{
|
||||
ft_putstr_fd("Minishell: cd: ", 2);
|
||||
ft_putstr_fd(p, 2);
|
||||
ft_putstr_fd(": No such directory\n", 2);
|
||||
free(p);
|
||||
return (check_return(cmd, 1));
|
||||
}
|
||||
}
|
||||
|
||||
int check_dir(t_s_cmd *cmd)
|
||||
{
|
||||
if (chdir(cmd->args[1]) == 0)
|
||||
{
|
||||
if (find_it(cmd->big_cmd->env, "PWD") != -1)
|
||||
change_path(cmd);
|
||||
}
|
||||
else
|
||||
{
|
||||
ft_putstr_fd("Minishell: cd: ", 2);
|
||||
ft_putstr_fd(cmd->args[1], 2);
|
||||
ft_putstr_fd(": No such directory\n", 2);
|
||||
return (check_return(cmd, 1));
|
||||
}
|
||||
return (check_return(cmd, 0));
|
||||
}
|
||||
|
||||
int open_directory(t_s_cmd *cmd)
|
||||
{
|
||||
char *p;
|
||||
int i;
|
||||
|
||||
p = NULL;
|
||||
i = 0;
|
||||
if (cmd->nb_args > 2)
|
||||
return (cd_error(cmd, "Minishell: cd: too many arguments", 1));
|
||||
if (!cmd->args[1])
|
||||
{
|
||||
if (find_it(cmd->big_cmd->env, "HOME") < 0)
|
||||
return (cd_error(cmd, "Minishell: cd: HOME not set", 1));
|
||||
return (check_home(cmd, p, cmd->big_cmd->env));
|
||||
}
|
||||
else if (cmd->nb_args == 2)
|
||||
i = check_dir(cmd);
|
||||
return (check_return(cmd, i));
|
||||
}
|
||||
27
srcs/built_in/cd_utils.c
Normal file
27
srcs/built_in/cd_utils.c
Normal file
@ -0,0 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cd_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/20 15:35:02 by sadjigui #+# #+# */
|
||||
/* Updated: 2022/04/20 16:07:58 by sadjigui ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
void change_oldpwd(char **env, int old_pwd, int pwd, char *p)
|
||||
{
|
||||
if (find_it(env, "PWD") != -1)
|
||||
{
|
||||
if (find_it(env, "OLDPWD") != -1)
|
||||
{
|
||||
free(env[old_pwd]);
|
||||
env[old_pwd] = ft_strjoin("OLD", env[pwd]);
|
||||
}
|
||||
free(env[pwd]);
|
||||
env[pwd] = ft_strjoin("PWD=", p);
|
||||
}
|
||||
}
|
||||
54
srcs/built_in/choose_builtin.c
Normal file
54
srcs/built_in/choose_builtin.c
Normal file
@ -0,0 +1,54 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* choose_builtin.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/02/28 17:12:52 by sadjigui #+# #+# */
|
||||
/* Updated: 2022/04/20 12:46:41 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
int is_builtin(char *cmd)
|
||||
{
|
||||
if (!ft_strcmp(cmd, "exit"))
|
||||
return (1);
|
||||
if (!ft_strcmp(cmd, "env"))
|
||||
return (1);
|
||||
if (!ft_strcmp(cmd, "export"))
|
||||
return (1);
|
||||
if (!ft_strcmp(cmd, "unset"))
|
||||
return (1);
|
||||
if (!ft_strcmp(cmd, "echo"))
|
||||
return (1);
|
||||
if (!ft_strcmp(cmd, "cd"))
|
||||
return (1);
|
||||
if (!ft_strcmp(cmd, "pwd"))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void call_builtin(t_cmd *cmd)
|
||||
{
|
||||
dup2(cmd->current_s_cmd->fd[0], 0);
|
||||
dup2(cmd->current_s_cmd->fd[1], 1);
|
||||
close(cmd->current_s_cmd->fd[0]);
|
||||
close(cmd->current_s_cmd->fd[1]);
|
||||
if (!ft_strcmp(cmd->current_s_cmd->cmd, "exit"))
|
||||
ft_exit(cmd->current_s_cmd);
|
||||
if (!ft_strcmp(cmd->current_s_cmd->cmd, "env"))
|
||||
print_env(cmd);
|
||||
if (!ft_strcmp(cmd->current_s_cmd->cmd, "export"))
|
||||
ft_export(cmd->current_s_cmd);
|
||||
if (!ft_strcmp(cmd->current_s_cmd->cmd, "unset"))
|
||||
ft_unset(cmd->current_s_cmd);
|
||||
if (!ft_strcmp(cmd->current_s_cmd->cmd, "echo"))
|
||||
ft_echo(cmd->current_s_cmd);
|
||||
if (!ft_strcmp(cmd->current_s_cmd->cmd, "cd"))
|
||||
open_directory(cmd->current_s_cmd);
|
||||
if (!ft_strcmp(cmd->current_s_cmd->cmd, "pwd"))
|
||||
ft_pwd(cmd->current_s_cmd);
|
||||
}
|
||||
50
srcs/built_in/echo.c
Normal file
50
srcs/built_in/echo.c
Normal file
@ -0,0 +1,50 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* echo.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: syd <syd@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/11 20:52:56 by sadjigui #+# #+# */
|
||||
/* Updated: 2022/04/19 17:09:24 by syd ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
void echo_print(t_s_cmd *d, int i)
|
||||
{
|
||||
while (d->args[i])
|
||||
{
|
||||
printf("%s", d->args[i]);
|
||||
if (d->args[i + 1] != NULL)
|
||||
printf(" ");
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
int ft_echo(t_s_cmd *d)
|
||||
{
|
||||
int i;
|
||||
size_t j;
|
||||
int is_option;
|
||||
|
||||
i = 1;
|
||||
is_option = 0;
|
||||
while (d->args[i] && d->args[i][0] == '-')
|
||||
{
|
||||
j = 1;
|
||||
while (d->args[i][j] && d->args[i][j] == 'n')
|
||||
j++;
|
||||
if (j == ft_strlen(d->args[i]))
|
||||
is_option = 1;
|
||||
else
|
||||
break ;
|
||||
i++;
|
||||
}
|
||||
if (d->args[i])
|
||||
echo_print(d, i);
|
||||
if (is_option == 0)
|
||||
printf("\n");
|
||||
return (check_return(d, 0));
|
||||
}
|
||||
51
srcs/built_in/env.c
Normal file
51
srcs/built_in/env.c
Normal file
@ -0,0 +1,51 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* env.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/09 21:09:47 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/19 13:22:34 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
int print_env(t_cmd *cmd)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (cmd->current_s_cmd->nb_args > 1)
|
||||
{
|
||||
ft_putstr_fd("Minishell: env: too many arguments\n", 2);
|
||||
return (check_return(cmd->current_s_cmd, 1));
|
||||
}
|
||||
else if (cmd->env)
|
||||
{
|
||||
while (cmd->env[i])
|
||||
{
|
||||
if (ft_strchr(cmd->env[i], '='))
|
||||
ft_putendl_fd(cmd->env[i], 1);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return (check_return(cmd->current_s_cmd, 0));
|
||||
}
|
||||
|
||||
int ft_pwd(t_s_cmd *cmd)
|
||||
{
|
||||
char p[1024];
|
||||
char *str;
|
||||
|
||||
str = getcwd(p, sizeof(p));
|
||||
if (!str)
|
||||
{
|
||||
ft_putstr_fd("Minishell: pwd: Not found\n", 2);
|
||||
return (check_return(cmd, 1));
|
||||
}
|
||||
else
|
||||
ft_putendl_fd(p, 1);
|
||||
return (check_return(cmd, 0));
|
||||
}
|
||||
99
srcs/built_in/exit.c
Normal file
99
srcs/built_in/exit.c
Normal file
@ -0,0 +1,99 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* exit.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/20 17:53:35 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/20 18:14:27 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
unsigned long long ft_atoi_long(const char *nptr)
|
||||
{
|
||||
int i;
|
||||
unsigned long long nbr;
|
||||
unsigned long long minus;
|
||||
|
||||
minus = 1;
|
||||
nbr = 0;
|
||||
i = 0;
|
||||
while ((nptr[i] >= 9 && nptr[i] <= 13) || nptr[i] == 32)
|
||||
i++;
|
||||
if (nptr[i] == '+')
|
||||
i++;
|
||||
else if (nptr[i] == '-')
|
||||
{
|
||||
i++;
|
||||
minus = -1;
|
||||
}
|
||||
while (nptr[i] >= '0' && nptr[i] <= '9')
|
||||
{
|
||||
nbr = nbr * 10 + nptr[i] - '0';
|
||||
i++;
|
||||
}
|
||||
return (minus * nbr);
|
||||
}
|
||||
|
||||
int max_long(char *nbr)
|
||||
{
|
||||
unsigned long long long_max;
|
||||
|
||||
long_max = 9223372036854775807;
|
||||
if (ft_strlen(nbr) > 20)
|
||||
return (1);
|
||||
if (nbr[0] == '-')
|
||||
{
|
||||
if (ft_atoi_long(nbr + 1) > long_max + 1)
|
||||
return (1);
|
||||
}
|
||||
else if (ft_atoi_long(nbr) > long_max)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void exit_error(t_cmd *cmd)
|
||||
{
|
||||
ft_putstr_fd("Minishell: exit: numeric argument required\n", 2);
|
||||
exit_shell(cmd, 2);
|
||||
}
|
||||
|
||||
int check_exit_args(t_s_cmd *cmd)
|
||||
{
|
||||
if (cmd->nb_args > 2)
|
||||
{
|
||||
ft_putstr_fd("Minishell: exit: too many arguments\n", 2);
|
||||
if (cmd->child)
|
||||
cmd->big_cmd->err_var = 1;
|
||||
else
|
||||
exit(1);
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
void ft_exit(t_s_cmd *cmd)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (!check_exit_args(cmd))
|
||||
return ;
|
||||
else if (cmd->nb_args == 1)
|
||||
exit_shell(cmd->big_cmd, 0);
|
||||
while (cmd->args[1][i] == ' ')
|
||||
i++;
|
||||
if (cmd->args[1][i] == '-')
|
||||
i++;
|
||||
while (cmd->args[1][i])
|
||||
{
|
||||
if (!ft_isdigit(cmd->args[1][i++]))
|
||||
exit_error(cmd->big_cmd);
|
||||
}
|
||||
if (max_long(cmd->args[1]))
|
||||
exit_error(cmd->big_cmd);
|
||||
exit_shell(cmd->big_cmd, ft_atoi(cmd->args[1]));
|
||||
}
|
||||
84
srcs/built_in/export.c
Normal file
84
srcs/built_in/export.c
Normal file
@ -0,0 +1,84 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* export.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/20 00:31:25 by sadjigui #+# #+# */
|
||||
/* Updated: 2022/04/20 00:31:35 by sadjigui ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
int check_variable(char *variable)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (!ft_isalpha(variable[i]) && variable[i] != '_')
|
||||
return (1);
|
||||
i++;
|
||||
while (variable[i] && variable[i] != '=')
|
||||
{
|
||||
if (!ft_isalnum(variable[i]) && variable[i] != '_')
|
||||
return (1);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int reatribute_variable(t_s_cmd *cmd, int index, char *dest, char *unset)
|
||||
{
|
||||
free (cmd->big_cmd->env[index]);
|
||||
cmd->big_cmd->env[index] = dest;
|
||||
free(unset);
|
||||
return (check_return(cmd, 0));
|
||||
}
|
||||
|
||||
int ft_export_variable(t_s_cmd *cmd, char *variable)
|
||||
{
|
||||
char *dest;
|
||||
char *unset;
|
||||
int index;
|
||||
|
||||
if (check_variable(variable) == 1)
|
||||
{
|
||||
ft_putstr_fd("Minishell: export: `", 2);
|
||||
ft_putstr_fd(variable, 2);
|
||||
ft_putstr_fd("': not a valid identifier\n", 2);
|
||||
return (check_return(cmd, 1));
|
||||
}
|
||||
dest = ft_strdup(variable);
|
||||
unset = ft_substr(dest, 0, find_len(dest, 0, '='));
|
||||
index = find_it(cmd->big_cmd->env, unset);
|
||||
if (index != -1)
|
||||
return (reatribute_variable(cmd, index, dest, unset));
|
||||
register_env(cmd, dest);
|
||||
if (dest)
|
||||
free(dest);
|
||||
if (unset)
|
||||
free(unset);
|
||||
return (check_return(cmd, 0));
|
||||
}
|
||||
|
||||
int ft_export(t_s_cmd *cmd)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 1;
|
||||
j = 0;
|
||||
if (!cmd->args[i])
|
||||
lone_export(cmd);
|
||||
else if (cmd->args[i])
|
||||
{
|
||||
while (cmd->args[i])
|
||||
{
|
||||
j = ft_export_variable(cmd, cmd->args[i]);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return (j);
|
||||
}
|
||||
81
srcs/built_in/export2.c
Normal file
81
srcs/built_in/export2.c
Normal file
@ -0,0 +1,81 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* export2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/07 17:58:25 by sadjigui #+# #+# */
|
||||
/* Updated: 2022/04/22 14:54:54 by sadjigui ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
void ft_swap(char **a, char **b)
|
||||
{
|
||||
char *tmp;
|
||||
|
||||
tmp = *a;
|
||||
*a = *b;
|
||||
*b = tmp;
|
||||
}
|
||||
|
||||
void sort_tab(char **av)
|
||||
{
|
||||
int i;
|
||||
int x;
|
||||
|
||||
i = 0;
|
||||
while (av[i])
|
||||
{
|
||||
x = i;
|
||||
while (av[x])
|
||||
{
|
||||
if (ft_strcmp(av[i], av[x]) > 0)
|
||||
ft_swap(&av[i], &av[x]);
|
||||
x++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void print_export(char *tmp)
|
||||
{
|
||||
char *str1;
|
||||
char *str2;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
str1 = ft_substr(tmp, 0, find_len(tmp, i, '='));
|
||||
i = find_len(tmp, 0, '=') + 1;
|
||||
str2 = ft_substr(tmp, i, ft_strlen(tmp));
|
||||
printf("declare -x %s", str1);
|
||||
if (ft_strlen(tmp) != ft_strlen(str1))
|
||||
{
|
||||
printf("=\"");
|
||||
if (next_space(str2, 0) != '\0')
|
||||
{
|
||||
printf("%s", str2);
|
||||
}
|
||||
printf("\"");
|
||||
}
|
||||
printf("\n");
|
||||
free(str1);
|
||||
free(str2);
|
||||
}
|
||||
|
||||
void lone_export(t_s_cmd *cmd)
|
||||
{
|
||||
char **tmp;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
tmp = cmd->big_cmd->env;
|
||||
sort_tab(tmp);
|
||||
while (tmp[i])
|
||||
{
|
||||
print_export(tmp[i]);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
91
srcs/built_in/init_builtin.c
Normal file
91
srcs/built_in/init_builtin.c
Normal file
@ -0,0 +1,91 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* init_builtin.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/02/28 17:17:01 by sadjigui #+# #+# */
|
||||
/* Updated: 2022/04/20 04:56:50 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
void ft_shlvl(char **env)
|
||||
{
|
||||
int i;
|
||||
int index;
|
||||
char *str;
|
||||
|
||||
i = 0;
|
||||
index = find_it(env, "SHLVL");
|
||||
if (index != -1)
|
||||
{
|
||||
while (env[index][i] && env[index][i] != '=')
|
||||
i++;
|
||||
str = ft_substr(env[index], i + 1, ft_strlen(env[index]));
|
||||
i = ft_atoi(str);
|
||||
free (str);
|
||||
i += 1;
|
||||
str = ft_itoa(i);
|
||||
free (env[index]);
|
||||
env[index] = ft_strjoin("SHLVL=", str);
|
||||
free (str);
|
||||
}
|
||||
}
|
||||
|
||||
void register_env(t_s_cmd *cmd, char *variable)
|
||||
{
|
||||
char **tmp;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
tmp = NULL;
|
||||
tmp = (char **)malloc(sizeof(char *) * (tab_len(cmd->big_cmd->env) + 2));
|
||||
if (!tmp)
|
||||
return ;
|
||||
while (cmd->big_cmd->env[i])
|
||||
{
|
||||
tmp[i] = ft_strdup(cmd->big_cmd->env[i]);
|
||||
i++;
|
||||
}
|
||||
tmp[i] = ft_strdup(variable);
|
||||
tmp[i + 1] = NULL;
|
||||
free_double(cmd->big_cmd->env);
|
||||
cmd->big_cmd->env = ft_dup_double(tmp);
|
||||
if (tmp)
|
||||
free_double(tmp);
|
||||
}
|
||||
|
||||
void ft_env(t_s_cmd *cmd, char **env)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (env[i])
|
||||
i++;
|
||||
cmd->big_cmd->env = ft_calloc(sizeof(char *), i + 1);
|
||||
i = 0;
|
||||
while (env[i])
|
||||
{
|
||||
cmd->big_cmd->env[i] = ft_strdup(env[i]);
|
||||
i++;
|
||||
}
|
||||
cmd->big_cmd->env[i] = NULL;
|
||||
}
|
||||
|
||||
void init_s_cmd(t_s_cmd *cmd, char **env)
|
||||
{
|
||||
ft_env(cmd, env);
|
||||
}
|
||||
|
||||
int tab_len(char **tab)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (tab[i])
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
78
srcs/built_in/unset.c
Normal file
78
srcs/built_in/unset.c
Normal file
@ -0,0 +1,78 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* unset.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/11 18:26:29 by sadjigui #+# #+# */
|
||||
/* Updated: 2022/04/23 13:09:17 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
void unset_variable(t_s_cmd *cmd, int i)
|
||||
{
|
||||
int a;
|
||||
int b;
|
||||
char **tmp;
|
||||
|
||||
a = 0;
|
||||
b = 0;
|
||||
tmp = malloc(sizeof(char *) * tab_len(cmd->big_cmd->env));
|
||||
if (!tmp)
|
||||
return ;
|
||||
while (cmd->big_cmd->env[a])
|
||||
{
|
||||
if (a != i)
|
||||
{
|
||||
tmp[b] = ft_strdup(cmd->big_cmd->env[a]);
|
||||
b++;
|
||||
}
|
||||
a++;
|
||||
}
|
||||
tmp[b] = NULL;
|
||||
free_double(cmd->big_cmd->env);
|
||||
cmd->big_cmd->env = ft_dup_double(tmp);
|
||||
if (tmp)
|
||||
free_double(tmp);
|
||||
}
|
||||
|
||||
int find_variable(char *variable, t_s_cmd *cmd)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = find_it(cmd->big_cmd->env, variable);
|
||||
if (check_variable(variable) == 1)
|
||||
{
|
||||
ft_putstr_fd("Minishell: unset: `", 2);
|
||||
ft_putstr_fd(variable, 2);
|
||||
ft_putstr_fd("': not a valid identifier\n", 2);
|
||||
return (check_return(cmd, 1));
|
||||
}
|
||||
if (i == tab_len(cmd->big_cmd->env))
|
||||
return (check_return(cmd, 2));
|
||||
if (find_it(cmd->big_cmd->env, variable) != -1)
|
||||
{
|
||||
unset_variable(cmd, i);
|
||||
return (check_return(cmd, 0));
|
||||
}
|
||||
else
|
||||
return (check_return(cmd, 1));
|
||||
}
|
||||
|
||||
int ft_unset(t_s_cmd *cmd)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 1;
|
||||
j = 0;
|
||||
while (cmd->args[i])
|
||||
{
|
||||
j = find_variable(cmd->args[i], cmd);
|
||||
i++;
|
||||
}
|
||||
return (j);
|
||||
}
|
||||
73
srcs/built_in/utils_builtin.c
Normal file
73
srcs/built_in/utils_builtin.c
Normal file
@ -0,0 +1,73 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils_builtin.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/16 15:04:12 by sadjigui #+# #+# */
|
||||
/* Updated: 2022/04/20 04:42:35 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
int find_len(char *input, int i, char c)
|
||||
{
|
||||
int j;
|
||||
|
||||
j = i;
|
||||
while (input[j] && input[j] != c)
|
||||
j++;
|
||||
return (j);
|
||||
}
|
||||
|
||||
int find_it(char **str, char *s)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (str[i] && (ft_strncmp(str[i], s, ft_strlen(s))
|
||||
|| (!ft_strncmp(str[i], s, ft_strlen(s))
|
||||
&& (str[i][ft_strlen(s)] != '=' && str[i][ft_strlen(s)] != 0))))
|
||||
i++;
|
||||
if (str[i] == NULL)
|
||||
return (-1);
|
||||
return (i);
|
||||
}
|
||||
|
||||
int cd_error(t_s_cmd *cmd, char *str, int i)
|
||||
{
|
||||
ft_putstr_fd(str, 2);
|
||||
ft_putstr_fd("\n", 2);
|
||||
return (check_return(cmd, i));
|
||||
}
|
||||
|
||||
int size_path(char **str)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
char **s;
|
||||
|
||||
i = find_it(str, "PWD");
|
||||
s = ft_split(str[i], '/');
|
||||
j = double_size(s);
|
||||
free_double(s);
|
||||
return (j);
|
||||
}
|
||||
|
||||
int check_return(t_s_cmd *cmd, int var)
|
||||
{
|
||||
if (cmd->child)
|
||||
{
|
||||
cmd->big_cmd->err_var = var;
|
||||
return (var);
|
||||
}
|
||||
else
|
||||
{
|
||||
close(cmd->big_cmd->tmpin);
|
||||
close(cmd->big_cmd->tmpout);
|
||||
free_cmd(cmd->big_cmd);
|
||||
exit(var);
|
||||
}
|
||||
}
|
||||
96
srcs/main.c
Normal file
96
srcs/main.c
Normal file
@ -0,0 +1,96 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/06 13:27:11 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/23 13:16:15 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/minishell.h"
|
||||
|
||||
char **ft_dup_double(char **env)
|
||||
{
|
||||
char **new_tab;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (!env)
|
||||
return (0);
|
||||
while (env[i])
|
||||
i++;
|
||||
new_tab = ft_calloc(sizeof(char *), i + 1);
|
||||
i = 0;
|
||||
while (env[i])
|
||||
{
|
||||
new_tab[i] = ft_strjoin(env[i], 0);
|
||||
i++;
|
||||
}
|
||||
new_tab[i] = NULL;
|
||||
return (new_tab);
|
||||
}
|
||||
|
||||
char **read_line(char **path, char *input, t_cmd *cmd, int *err_var)
|
||||
{
|
||||
(void)*err_var;
|
||||
input = readline("\033[1;31m~$ \033[0m");
|
||||
if (!input)
|
||||
{
|
||||
free_double(path);
|
||||
exit_shell(cmd, 0);
|
||||
}
|
||||
add_history(input);
|
||||
if (ft_strlen(input) && next_space(input, 0) && input && path)
|
||||
{
|
||||
cmd = set_cmd(input, path, g_var);
|
||||
if (cmd)
|
||||
{
|
||||
free_double(path);
|
||||
execute(cmd);
|
||||
g_var = cmd->err_var;
|
||||
path = ft_dup_double(cmd->env);
|
||||
free_cmd(cmd);
|
||||
cmd = 0;
|
||||
}
|
||||
}
|
||||
free(input);
|
||||
return (path);
|
||||
}
|
||||
|
||||
void print_prompt(char **path)
|
||||
{
|
||||
int err_var;
|
||||
|
||||
err_var = 0;
|
||||
while (1)
|
||||
path = read_line(path, 0, 0, &err_var);
|
||||
}
|
||||
|
||||
int main(int ac, char **av, char **path)
|
||||
{
|
||||
char **env;
|
||||
|
||||
(void)av;
|
||||
if (!isatty(0))
|
||||
{
|
||||
printf("Not today\n");
|
||||
return (0);
|
||||
}
|
||||
env = ft_dup_double(path);
|
||||
av = 0;
|
||||
if (ac != 1)
|
||||
{
|
||||
ft_putstr_fd("Error: too much arguments\n", 2);
|
||||
return (0);
|
||||
}
|
||||
printf("---MINISHELL START---\n");
|
||||
signal(SIGINT, crtl_c);
|
||||
signal(SIGQUIT, sig_quit);
|
||||
if (env)
|
||||
ft_shlvl(env);
|
||||
print_prompt(env);
|
||||
return (0);
|
||||
}
|
||||
86
srcs/pipe/exec_utils.c
Normal file
86
srcs/pipe/exec_utils.c
Normal file
@ -0,0 +1,86 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* exec_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/19 17:36:53 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/20 04:50:19 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
int wait_exit(t_cmd *cmd)
|
||||
{
|
||||
int i;
|
||||
int ret;
|
||||
int exit_pid;
|
||||
int status;
|
||||
|
||||
i = 0;
|
||||
ret = 0;
|
||||
exit_pid = 1;
|
||||
while (exit_pid > 0)
|
||||
{
|
||||
i = 0;
|
||||
exit_pid = wait(&status);
|
||||
while (exit_pid != -1 && cmd->s_cmds[i])
|
||||
{
|
||||
exit_child(cmd, exit_pid, status, i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return (ret);
|
||||
}
|
||||
|
||||
void exit_child(t_cmd *cmd, int exit_pid, int status, int i)
|
||||
{
|
||||
if (exit_pid == cmd->s_cmds[i]->child)
|
||||
{
|
||||
close(cmd->s_cmds[i]->fd[0]);
|
||||
close(cmd->s_cmds[i]->fd[1]);
|
||||
if (WIFEXITED(status))
|
||||
cmd->err_var = WEXITSTATUS(status);
|
||||
else if (WIFSIGNALED(status))
|
||||
{
|
||||
if (WTERMSIG(status) != 13)
|
||||
cmd->err_var = WTERMSIG(status);
|
||||
ft_putstr_fd("\b\b\b\b\b", 1);
|
||||
if (WTERMSIG(status) == 3)
|
||||
ft_putstr_fd("^\\Quit\n", 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void check_access(t_cmd *cmd)
|
||||
{
|
||||
if (!cmd->current_s_cmd->cmd || access(cmd->current_s_cmd->cmd, F_OK))
|
||||
{
|
||||
ft_putstr_fd("Minishell: command not found: ", 2);
|
||||
if (cmd->current_s_cmd->cmd)
|
||||
ft_putstr_fd(cmd->current_s_cmd->cmd, 2);
|
||||
ft_putstr_fd("\n", 2);
|
||||
close(0);
|
||||
close(1);
|
||||
close(cmd->tmpin);
|
||||
close(cmd->tmpout);
|
||||
free_cmd(cmd);
|
||||
exit(127);
|
||||
}
|
||||
}
|
||||
|
||||
void close_pipe(t_cmd *cmd)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
close(0);
|
||||
while (cmd->s_cmds[i])
|
||||
{
|
||||
close(cmd->s_cmds[i]->fd[0]);
|
||||
close(cmd->s_cmds[i]->fd[1]);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
110
srcs/pipe/execution.c
Normal file
110
srcs/pipe/execution.c
Normal file
@ -0,0 +1,110 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* execution.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/20 04:43:32 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/22 11:51:32 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
void exec_cmd(t_cmd *cmd, char **env, int *fdpipe)
|
||||
{
|
||||
cmd->current_s_cmd->child = fork();
|
||||
if (cmd->current_s_cmd->child == 0)
|
||||
{
|
||||
if (fdpipe)
|
||||
close(fdpipe[0]);
|
||||
dup2(cmd->current_s_cmd->fd[0], 0);
|
||||
dup2(cmd->current_s_cmd->fd[1], 1);
|
||||
close(cmd->current_s_cmd->fd[0]);
|
||||
close(cmd->current_s_cmd->fd[1]);
|
||||
if (is_builtin(cmd->current_s_cmd->cmd))
|
||||
{
|
||||
call_builtin(cmd);
|
||||
exit(0);
|
||||
}
|
||||
check_access(cmd);
|
||||
if (-1 == execve(cmd->current_s_cmd->cmd,
|
||||
cmd->current_s_cmd->args, env))
|
||||
ft_putstr_fd("Minishell: exec error\n", 2);
|
||||
exit(126);
|
||||
}
|
||||
else if (!cmd->current_s_cmd->cmd || access(cmd->current_s_cmd->cmd, F_OK))
|
||||
cmd->err_var = 127;
|
||||
else
|
||||
cmd->err_var = 131;
|
||||
}
|
||||
|
||||
void exec_last_cmd(t_cmd *cmd, int *fdin, int *fdout, int i)
|
||||
{
|
||||
if (cmd->current_s_cmd->outfile)
|
||||
*fdout = open(cmd->current_s_cmd->outfile,
|
||||
O_RDWR | O_CREAT | O_APPEND, 0666);
|
||||
else
|
||||
*fdout = dup(cmd->tmpout);
|
||||
cmd->current_s_cmd->fd[0] = *fdin;
|
||||
cmd->current_s_cmd->fd[1] = *fdout;
|
||||
if (i == 0 && is_builtin(cmd->current_s_cmd->cmd))
|
||||
call_builtin(cmd);
|
||||
else
|
||||
exec_cmd(cmd, cmd->env, 0);
|
||||
}
|
||||
|
||||
void exec_not_last_cmd(t_cmd *cmd, int *fdpipe, int *fdin, int *fdout)
|
||||
{
|
||||
pipe(fdpipe);
|
||||
cmd->current_s_cmd->fd[0] = *fdin;
|
||||
if (cmd->current_s_cmd->outfile)
|
||||
{
|
||||
*fdout = open(cmd->current_s_cmd->outfile,
|
||||
O_RDWR | O_CREAT | O_APPEND, 0666);
|
||||
cmd->current_s_cmd->fd[1] = *fdout;
|
||||
}
|
||||
else
|
||||
cmd->current_s_cmd->fd[1] = fdpipe[1];
|
||||
*fdin = fdpipe[0];
|
||||
exec_cmd(cmd, cmd->env, fdpipe);
|
||||
close(cmd->current_s_cmd->fd[0]);
|
||||
}
|
||||
|
||||
void set_fdin_not_first(t_cmd *cmd, int *fdin)
|
||||
{
|
||||
close(*fdin);
|
||||
*fdin = open(cmd->current_s_cmd->infile, O_RDWR);
|
||||
if (*fdin < 0)
|
||||
printf("Minishell: open : bad file descriptor\n");
|
||||
}
|
||||
|
||||
void execute(t_cmd *cmd)
|
||||
{
|
||||
int fdpipe[2];
|
||||
int fdout;
|
||||
int fdin;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
fdpipe[1] = -1;
|
||||
cmd->tmpin = dup(0);
|
||||
cmd->tmpout = dup(1);
|
||||
set_fdin(cmd, &fdin);
|
||||
while (cmd->current_s_cmd)
|
||||
{
|
||||
cmd->current_s_cmd->child = 1;
|
||||
fdout = -1;
|
||||
if (i > 0 && cmd->current_s_cmd->infile)
|
||||
set_fdin_not_first(cmd, &fdin);
|
||||
if (i == cmd->nb_s_cmd - 1)
|
||||
exec_last_cmd(cmd, &fdin, &fdout, i);
|
||||
else
|
||||
exec_not_last_cmd(cmd, fdpipe, &fdin, &fdout);
|
||||
if (fdpipe[1] != -1)
|
||||
close(fdpipe[1]);
|
||||
cmd->current_s_cmd = cmd->s_cmds[++i];
|
||||
}
|
||||
reset_fds(cmd);
|
||||
}
|
||||
105
srcs/pipe/pipex_utils.c
Normal file
105
srcs/pipe/pipex_utils.c
Normal file
@ -0,0 +1,105 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pipex_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/08 16:58:40 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/19 18:11:38 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
void set_fdin(t_cmd *cmd, int *fdin)
|
||||
{
|
||||
if (cmd->current_s_cmd->infile)
|
||||
{
|
||||
*fdin = open(cmd->current_s_cmd->infile, O_RDWR);
|
||||
if (*fdin < 0)
|
||||
printf("Minishell: open : bad file descriptor\n");
|
||||
}
|
||||
else
|
||||
*fdin = dup(cmd->tmpin);
|
||||
}
|
||||
|
||||
void reset_fds(t_cmd *cmd)
|
||||
{
|
||||
close_pipe(cmd);
|
||||
dup2(cmd->tmpin, 0);
|
||||
dup2(cmd->tmpout, 1);
|
||||
close(cmd->tmpin);
|
||||
close(cmd->tmpout);
|
||||
wait_exit(cmd);
|
||||
}
|
||||
|
||||
char **get_path(char **env)
|
||||
{
|
||||
int i;
|
||||
char **line;
|
||||
char *swap;
|
||||
|
||||
i = 0;
|
||||
line = 0;
|
||||
swap = 0;
|
||||
while (env[i++] && env[i])
|
||||
{
|
||||
if (!ft_strncmp(env[i], "PATH=", 5))
|
||||
{
|
||||
swap = ft_substr(env[i], 5, ft_strlen(env[i]));
|
||||
if (!swap)
|
||||
exit(1);
|
||||
line = ft_split(swap, ':');
|
||||
if (!line)
|
||||
exit(1);
|
||||
free(swap);
|
||||
return (line);
|
||||
}
|
||||
free(line);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
char *does_access(char **path, char **exec)
|
||||
{
|
||||
int i;
|
||||
char *cmd;
|
||||
char *swap;
|
||||
|
||||
swap = 0;
|
||||
cmd = 0;
|
||||
i = 0;
|
||||
if (exec[0][0] != '/')
|
||||
swap = ft_strjoin(path[i], "/");
|
||||
cmd = ft_strjoin(swap, exec[0]);
|
||||
free(swap);
|
||||
swap = cmd;
|
||||
while (access(swap, F_OK) && path[i++])
|
||||
{
|
||||
free(swap);
|
||||
swap = ft_strjoin(path[i], "/");
|
||||
cmd = swap;
|
||||
swap = ft_strjoin(swap, exec[0]);
|
||||
free(cmd);
|
||||
}
|
||||
if (path[i])
|
||||
return (swap);
|
||||
free(swap);
|
||||
return (0);
|
||||
}
|
||||
|
||||
char *get_command(char **exec, char **path)
|
||||
{
|
||||
char *swap;
|
||||
|
||||
swap = 0;
|
||||
if (!path)
|
||||
return (0);
|
||||
if ((exec[0][0] == '/' || exec[0][0] == '.') && !access(exec[0], F_OK))
|
||||
return (exec[0]);
|
||||
else if (exec[0][0] == '/')
|
||||
return (0);
|
||||
swap = does_access(path, exec);
|
||||
return (swap);
|
||||
}
|
||||
85
srcs/set_cmd/free_cmd.c
Normal file
85
srcs/set_cmd/free_cmd.c
Normal file
@ -0,0 +1,85 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* free_cmd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/09 12:33:30 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/23 13:18:23 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
void exit_shell(t_cmd *cmd, int ret)
|
||||
{
|
||||
ft_putstr_fd("exit\n", 1);
|
||||
close (0);
|
||||
close (1);
|
||||
if (!cmd)
|
||||
exit(ret);
|
||||
if (cmd->tmpin != -1)
|
||||
close(cmd->tmpin);
|
||||
if (cmd->tmpout != -1)
|
||||
close(cmd->tmpout);
|
||||
if (cmd->current_s_cmd->fd[0] != -1)
|
||||
close(cmd->current_s_cmd->fd[0]);
|
||||
if (cmd->current_s_cmd->fd[1] != -1)
|
||||
close(cmd->current_s_cmd->fd[1]);
|
||||
free_cmd(cmd);
|
||||
rl_clear_history();
|
||||
cmd = 0;
|
||||
exit(ret);
|
||||
}
|
||||
|
||||
void del_heredoc(void)
|
||||
{
|
||||
long i;
|
||||
char *str;
|
||||
char *nbr;
|
||||
|
||||
i = 0;
|
||||
while (i < 2147483647)
|
||||
{
|
||||
nbr = ft_itoa(i);
|
||||
if (i == 0)
|
||||
str = ft_strdup(".heredoc");
|
||||
else
|
||||
str = ft_strjoin(".heredoc", nbr);
|
||||
free(nbr);
|
||||
if (access(str, F_OK) == 0)
|
||||
unlink(str);
|
||||
else
|
||||
{
|
||||
free(str);
|
||||
return ;
|
||||
}
|
||||
free(str);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void free_cmd(t_cmd *cmd)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
del_heredoc();
|
||||
while (cmd->s_cmds && cmd->s_cmds[i])
|
||||
{
|
||||
free_double(cmd->s_cmds[i]->args);
|
||||
if (cmd->s_cmds[i]->cmd)
|
||||
free(cmd->s_cmds[i]->cmd);
|
||||
if (cmd->s_cmds[i]->infile)
|
||||
free(cmd->s_cmds[i]->infile);
|
||||
if (cmd->s_cmds[i]->outfile)
|
||||
free(cmd->s_cmds[i]->outfile);
|
||||
free(cmd->s_cmds[i]);
|
||||
i++;
|
||||
}
|
||||
free_double(cmd->env);
|
||||
free_double(cmd->path);
|
||||
free(cmd->s_cmds);
|
||||
free(cmd);
|
||||
}
|
||||
126
srcs/set_cmd/set_cmd.c
Normal file
126
srcs/set_cmd/set_cmd.c
Normal file
@ -0,0 +1,126 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* set_cmd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/08 15:19:42 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/23 13:07:57 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
t_s_cmd *set_s_cmd(char *line, int index)
|
||||
{
|
||||
t_s_cmd *s_cmd;
|
||||
char **split_line;
|
||||
|
||||
s_cmd = malloc(sizeof(t_s_cmd));
|
||||
if (!s_cmd)
|
||||
return (0);
|
||||
s_cmd->fd[0] = -1;
|
||||
s_cmd->fd[1] = -1;
|
||||
s_cmd->cmd = 0;
|
||||
s_cmd->args = 0;
|
||||
s_cmd->infile = 0;
|
||||
s_cmd->outfile = 0;
|
||||
line = set_redirection(s_cmd, ft_strdup(line), index, 0);
|
||||
if (!line)
|
||||
{
|
||||
free(s_cmd);
|
||||
return (0);
|
||||
}
|
||||
split_line = ft_split_with_quote(line, ' ');
|
||||
s_cmd->nb_args = double_size(split_line);
|
||||
s_cmd->args = split_line;
|
||||
free(line);
|
||||
return (s_cmd);
|
||||
}
|
||||
|
||||
t_cmd *split_cmd(t_cmd *cmd, char **cmds)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (cmds[i])
|
||||
{
|
||||
cmd->s_cmds[i] = set_s_cmd(cmds[i], i);
|
||||
if (!cmd->s_cmds[i])
|
||||
{
|
||||
free_cmd(cmd);
|
||||
return (0);
|
||||
}
|
||||
cmd->s_cmds[i]->big_cmd = cmd;
|
||||
i++;
|
||||
}
|
||||
cmd->s_cmds[i] = 0;
|
||||
return (cmd);
|
||||
}
|
||||
|
||||
int is_pipe_good(char *str)
|
||||
{
|
||||
int i;
|
||||
char next;
|
||||
|
||||
i = 0;
|
||||
while (str[i])
|
||||
{
|
||||
if (str[i] == '|')
|
||||
{
|
||||
next = next_space(str, i + 1);
|
||||
if (!next || next == '|' || next == '<' || next == '>')
|
||||
return (0);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
t_cmd *initialize_cmd(t_cmd *cmd, char **env, char **cmds, int nb)
|
||||
{
|
||||
cmd->tmpin = -1;
|
||||
cmd->tmpout = -1;
|
||||
cmd->err_var = nb;
|
||||
cmd->path = get_path(env);
|
||||
cmd->env = ft_dup_double(env);
|
||||
cmd->nb_s_cmd = double_size(cmds);
|
||||
cmd = split_cmd(cmd, cmds);
|
||||
if (!cmd)
|
||||
{
|
||||
free_double(cmds);
|
||||
return (0);
|
||||
}
|
||||
parse_quote(cmd);
|
||||
free_double(cmds);
|
||||
if (cmd)
|
||||
{
|
||||
cmd->current_s_cmd = cmd->s_cmds[0];
|
||||
cmd->err_var = 0;
|
||||
return (cmd);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
t_cmd *set_cmd(char *input, char **env, int nb)
|
||||
{
|
||||
t_cmd *cmd;
|
||||
char **cmds;
|
||||
|
||||
if (!is_quote_good(input) || !is_pipe_good(input))
|
||||
{
|
||||
error_parsing(0);
|
||||
return (0);
|
||||
}
|
||||
cmds = ft_split_with_quote(input, '|');
|
||||
if (!cmds)
|
||||
return (0);
|
||||
cmd = malloc(sizeof(t_cmd));
|
||||
if (!cmd)
|
||||
return (0);
|
||||
cmd->s_cmds = ft_calloc(sizeof(t_s_cmd), double_size(cmds) + 1);
|
||||
if (!cmd->s_cmds)
|
||||
return (0);
|
||||
return (initialize_cmd(cmd, env, cmds, nb));
|
||||
}
|
||||
@ -1,19 +1,22 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* set_cmd_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/06 13:27:11 by apommier #+# #+# */
|
||||
/* Updated: 2022/03/06 13:31:54 by apommier ### ########.fr */
|
||||
/* Created: 2022/04/19 16:53:13 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/22 11:43:15 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
int main(int ac, char **av, char **path)
|
||||
char *error_parsing(char *to_free)
|
||||
{
|
||||
printf("---MINISHELL START---\n");
|
||||
g_var = 2;
|
||||
if (to_free)
|
||||
free(to_free);
|
||||
ft_putstr_fd("Minishell: error while parsing command\n", 2);
|
||||
return (0);
|
||||
}
|
||||
107
srcs/set_quote/set_quote.c
Normal file
107
srcs/set_quote/set_quote.c
Normal file
@ -0,0 +1,107 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* set_quote.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/09 23:58:21 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/23 13:11:50 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
int is_in_quote(char *str, int index)
|
||||
{
|
||||
int i;
|
||||
int open;
|
||||
|
||||
open = 0;
|
||||
i = 0;
|
||||
while (i < index)
|
||||
{
|
||||
if (str[i] == '\'' && open != 2)
|
||||
{
|
||||
if (!open)
|
||||
open = 1;
|
||||
else if (open == 1)
|
||||
open = 0;
|
||||
}
|
||||
else if (str[i] == '"' && open != 1)
|
||||
{
|
||||
if (!open)
|
||||
open = 2;
|
||||
else if (open == 2)
|
||||
open = 0;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (open);
|
||||
}
|
||||
|
||||
void count_quote(char c, int *open, int *simple_quote, int *double_quote)
|
||||
{
|
||||
if (c == '\'' && *open != 2)
|
||||
{
|
||||
if (!(*open))
|
||||
(*open) = 1;
|
||||
else if ((*open) == 1)
|
||||
(*open) = 0;
|
||||
(*simple_quote)++;
|
||||
}
|
||||
else if (c == '"' && (*open) != 1)
|
||||
{
|
||||
if (!(*open))
|
||||
(*open) = 2;
|
||||
else if ((*open) == 2)
|
||||
(*open) = 0;
|
||||
(*double_quote)++;
|
||||
}
|
||||
}
|
||||
|
||||
int is_quote_good(char *str)
|
||||
{
|
||||
int simple_quote;
|
||||
int double_quote;
|
||||
int open;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
open = 0;
|
||||
simple_quote = 0;
|
||||
double_quote = 0;
|
||||
while (str[i])
|
||||
{
|
||||
count_quote(str[i], &open, &simple_quote, &double_quote);
|
||||
i++;
|
||||
}
|
||||
if (simple_quote % 2 || double_quote % 2)
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int parse_quote(t_cmd *cmd)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = -1;
|
||||
while (cmd->s_cmds[++i])
|
||||
{
|
||||
j = -1;
|
||||
while (cmd->s_cmds[i]->args[++j])
|
||||
cmd->s_cmds[i]->args[j] = set_var(cmd, cmd->s_cmds[i]->args[j]);
|
||||
if (!is_builtin(cmd->s_cmds[i]->args[0]))
|
||||
{
|
||||
cmd->s_cmds[i]->cmd = get_command(cmd->s_cmds[i]->args, cmd->path);
|
||||
if (cmd->s_cmds[i]->cmd == cmd->s_cmds[i]->args[0])
|
||||
cmd->s_cmds[i]->cmd = ft_strdup(cmd->s_cmds[i]->cmd);
|
||||
}
|
||||
else
|
||||
cmd->s_cmds[i]->cmd = ft_strdup(cmd->s_cmds[i]->args[0]);
|
||||
if (!cmd->s_cmds[i]->cmd)
|
||||
cmd->s_cmds[i]->cmd = ft_strdup(cmd->s_cmds[i]->args[0]);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
114
srcs/set_quote/set_var.c
Normal file
114
srcs/set_quote/set_var.c
Normal file
@ -0,0 +1,114 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* set_var.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/19 15:02:01 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/22 13:01:30 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
char *del_char(char *str, int *index)
|
||||
{
|
||||
char *swap;
|
||||
char *swap2;
|
||||
|
||||
swap = 0;
|
||||
swap = ft_strdup(str + *index + 1);
|
||||
str[*index] = 0;
|
||||
swap2 = str;
|
||||
str = ft_strjoin(str, swap);
|
||||
free(swap2);
|
||||
free(swap);
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *get_var(t_cmd *cmd, char *var_name)
|
||||
{
|
||||
char *line;
|
||||
char **split_line;
|
||||
int index;
|
||||
|
||||
line = 0;
|
||||
if (!ft_strcmp(var_name, "?"))
|
||||
return (ft_itoa(cmd->err_var));
|
||||
index = find_it(cmd->env, var_name);
|
||||
if (index >= 0)
|
||||
{
|
||||
line = cmd->env[index];
|
||||
split_line = ft_split(line, '=');
|
||||
if (split_line[1])
|
||||
line = ft_strdup(split_line[1]);
|
||||
else
|
||||
return (0);
|
||||
free_double(split_line);
|
||||
}
|
||||
return (line);
|
||||
}
|
||||
|
||||
char *change_var(t_cmd *big_cmd, char *cmd, int *index)
|
||||
{
|
||||
int i;
|
||||
char *ret;
|
||||
|
||||
i = *index + 1;
|
||||
while (cmd[i] && (ft_isalnum(cmd[i]) || cmd[i] == '_'
|
||||
|| cmd[i] == '?' || cmd[i] == '$'))
|
||||
i++;
|
||||
if (i == *index + 1)
|
||||
{
|
||||
(*index)++;
|
||||
ret = ft_strdup(cmd);
|
||||
free(cmd);
|
||||
return (ret);
|
||||
}
|
||||
ret = find_var(big_cmd, cmd, i, index);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
char *check_quote(t_cmd *big_cmd, char *cmd, int *i)
|
||||
{
|
||||
cmd = del_char(cmd, i);
|
||||
if (cmd[*i])
|
||||
{
|
||||
while (cmd[*i] != '"')
|
||||
{
|
||||
if (cmd[*i] == '$')
|
||||
cmd = change_var(big_cmd, cmd, i);
|
||||
(*i)++;
|
||||
}
|
||||
cmd = del_char(cmd, i);
|
||||
}
|
||||
return (cmd);
|
||||
}
|
||||
|
||||
char *set_var(t_cmd *big_cmd, char *cmd)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (cmd[0] && cmd[i])
|
||||
{
|
||||
if (cmd[i] == '\'')
|
||||
{
|
||||
cmd = del_char(cmd, &i);
|
||||
if (cmd[i])
|
||||
{
|
||||
while (cmd[i] != '\'')
|
||||
i++;
|
||||
cmd = del_char(cmd, &i);
|
||||
}
|
||||
}
|
||||
else if (cmd[i] == '"')
|
||||
cmd = check_quote(big_cmd, cmd, &i);
|
||||
else if (cmd[i] == '$')
|
||||
cmd = change_var(big_cmd, cmd, &i);
|
||||
else
|
||||
i++;
|
||||
}
|
||||
return (cmd);
|
||||
}
|
||||
133
srcs/set_quote/split_with_quote.c
Normal file
133
srcs/set_quote/split_with_quote.c
Normal file
@ -0,0 +1,133 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* split_with_quote.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/10 19:50:50 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/23 13:11:27 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
int next_quote(const char *s, int i)
|
||||
{
|
||||
int simple_quote;
|
||||
int double_quote;
|
||||
|
||||
simple_quote = 0;
|
||||
double_quote = 0;
|
||||
if (s[i] == '"' || s[i] == '\'')
|
||||
{
|
||||
if (s[i] == '"')
|
||||
double_quote = 1;
|
||||
else if (s[i] == '\'')
|
||||
simple_quote = 1;
|
||||
i++;
|
||||
if (simple_quote)
|
||||
{
|
||||
while (s[i] != '\'')
|
||||
i++;
|
||||
}
|
||||
else if (double_quote)
|
||||
{
|
||||
while (s[i] != '"')
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return (i + 1);
|
||||
}
|
||||
|
||||
static int fill_tab(char *s, char c, char **dest, size_t index)
|
||||
{
|
||||
int i;
|
||||
int size;
|
||||
|
||||
i = 0;
|
||||
while (s[i] != c && s[i])
|
||||
{
|
||||
if (s[i] == '"' || s[i] == '\'')
|
||||
i = next_quote(s, i);
|
||||
else
|
||||
i++;
|
||||
}
|
||||
dest[index] = (char *)ft_calloc(i + 1, sizeof(char));
|
||||
if (dest[index] == 0)
|
||||
return (0);
|
||||
size = i;
|
||||
i = 0;
|
||||
while (i < size)
|
||||
{
|
||||
dest[index][i] = s[i];
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
static void call(char *s, char c, char **dest, int j)
|
||||
{
|
||||
int index;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
index = 0;
|
||||
while (j > index)
|
||||
{
|
||||
while (s[i] != '"' && s[i] != '\'' && s[i] == c && s[i])
|
||||
i++;
|
||||
if (!s[i])
|
||||
return ;
|
||||
fill_tab(s + i, c, dest, index);
|
||||
index++;
|
||||
while (s[i] != c && s[i])
|
||||
{
|
||||
if (s[i] == '"' || s[i] == '\'')
|
||||
i = next_quote(s, i);
|
||||
else
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void count_word(const char *s, int *i, int *j, char c)
|
||||
{
|
||||
while (s[*i])
|
||||
{
|
||||
while (s[*i] != c && s[*i])
|
||||
{
|
||||
if (s[*i] == '"' || s[*i] == '\'')
|
||||
*i = next_quote(s, *i);
|
||||
else
|
||||
(*i)++;
|
||||
}
|
||||
(*j)++;
|
||||
while (s[*i] == c && s[*i])
|
||||
(*i)++;
|
||||
}
|
||||
}
|
||||
|
||||
char **ft_split_with_quote(char const *s, char c)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
char **dest;
|
||||
|
||||
j = 0;
|
||||
i = 0;
|
||||
if (!s)
|
||||
return (0);
|
||||
i = next_quote(s, i);
|
||||
if (i)
|
||||
j++;
|
||||
while (s[i] == c && s[i])
|
||||
i++;
|
||||
count_word(s, &i, &j, c);
|
||||
dest = (char **)ft_calloc(sizeof(char *), (1 + j));
|
||||
if (!dest)
|
||||
return (0);
|
||||
dest[j] = 0;
|
||||
call((char *)s, c, dest, j);
|
||||
return (dest);
|
||||
}
|
||||
69
srcs/set_redirection/heredoc_utils.c
Normal file
69
srcs/set_redirection/heredoc_utils.c
Normal file
@ -0,0 +1,69 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* heredoc_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/19 19:25:16 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/22 11:24:29 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
void sig_heredoc(int num)
|
||||
{
|
||||
struct sigaction base;
|
||||
|
||||
(void)num;
|
||||
memset(&base, 0, sizeof(base));
|
||||
base.sa_handler = &crtl_c;
|
||||
base.sa_flags = 0;
|
||||
ft_putchar_fd('\n', 1);
|
||||
if (sigaction(SIGINT, &base, 0) == -1)
|
||||
{
|
||||
printf("Minishell: sigaction error2\n");
|
||||
return ;
|
||||
}
|
||||
}
|
||||
|
||||
void change_signal(void)
|
||||
{
|
||||
struct sigaction test;
|
||||
|
||||
memset(&test, 0, sizeof(test));
|
||||
test.sa_handler = &sig_heredoc;
|
||||
test.sa_flags = 0;
|
||||
if (sigaction(SIGINT, &test, 0) == -1)
|
||||
{
|
||||
printf("Minishell: sigaction error\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
int free_wait_prompt(char *in, char**history)
|
||||
{
|
||||
free(in);
|
||||
free_double(history);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
char **fill_history(t_s_cmd *cmd, char *input, char *in, char **history)
|
||||
{
|
||||
char *del;
|
||||
char *dup;
|
||||
|
||||
if (ft_strcmp(input, in))
|
||||
{
|
||||
dup = ft_strdup(input);
|
||||
dup[ft_strlen(input) - 1] = 0;
|
||||
del = dup;
|
||||
dup = set_var(cmd->big_cmd, dup);
|
||||
history = add_line(history, dup);
|
||||
if (dup != del)
|
||||
free(del);
|
||||
free(dup);
|
||||
}
|
||||
return (history);
|
||||
}
|
||||
125
srcs/set_redirection/redirection.c
Normal file
125
srcs/set_redirection/redirection.c
Normal file
@ -0,0 +1,125 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* redirection.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/09 15:18:58 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/20 17:05:09 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
char *get_word(char *str, int start)
|
||||
{
|
||||
char *new;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (str[start + i] == '<' || str[start + i] == '>')
|
||||
i++;
|
||||
while (str[start + i] == ' ')
|
||||
i++;
|
||||
new = ft_strjoin(&str[start + i], 0);
|
||||
i = 0;
|
||||
while (new[i] && new[i] != ' ' && new[i] != '>' && new[i] != '<')
|
||||
i++;
|
||||
new[i] = 0;
|
||||
return (new);
|
||||
}
|
||||
|
||||
int set_file(char *file, int type)
|
||||
{
|
||||
int fd;
|
||||
|
||||
if (type)
|
||||
fd = open(file, O_APPEND | O_CREAT, 0666);
|
||||
else
|
||||
fd = open(file, O_TRUNC | O_CREAT, 0666);
|
||||
if (fd == -1)
|
||||
{
|
||||
ft_putstr_fd("Minishell: ", 2);
|
||||
ft_putstr_fd(file, 2);
|
||||
ft_putstr_fd(": Permission denied\n", 2);
|
||||
return (0);
|
||||
}
|
||||
if (fd)
|
||||
close(fd);
|
||||
return (1);
|
||||
}
|
||||
|
||||
char **add_line(char **tab, char *line)
|
||||
{
|
||||
int size;
|
||||
char **ret;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
size = 0;
|
||||
ret = 0;
|
||||
if (tab)
|
||||
size = double_size(tab);
|
||||
ret = ft_calloc(size + 2, sizeof(char *));
|
||||
if (!ret)
|
||||
return (0);
|
||||
while (tab && tab[i])
|
||||
{
|
||||
ret[i] = ft_strjoin(tab[i], 0);
|
||||
i++;
|
||||
}
|
||||
ret[i] = ft_strjoin(line, 0);
|
||||
ret[i + 1] = 0;
|
||||
if (tab)
|
||||
free_double(tab);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
int set_redirect_in(t_s_cmd *cmd, char **line, int *i, int index)
|
||||
{
|
||||
if (!is_in_quote(*line, *i))
|
||||
{
|
||||
*line = ft_input(*line, cmd, *i);
|
||||
if (!(*line))
|
||||
return (0);
|
||||
if (cmd->in_type == 1)
|
||||
{
|
||||
if (wait_prompt(cmd, index, 0, 0) == -1)
|
||||
{
|
||||
free(*line);
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
*i = 0;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
char *set_redirection(t_s_cmd *cmd, char *line, int index, int i)
|
||||
{
|
||||
while (line[i])
|
||||
{
|
||||
if (line[i] == '<')
|
||||
{
|
||||
if (!set_redirect_in(cmd, &line, &i, index))
|
||||
return (0);
|
||||
}
|
||||
else if (line[i] == '>')
|
||||
{
|
||||
if (!is_in_quote(line, i))
|
||||
{
|
||||
line = ft_output(line, cmd, i);
|
||||
if (!line || !set_file(cmd->outfile, cmd->in_type))
|
||||
return (0);
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
if (line[i] && (line[i] == '<' || line[i] == '>')
|
||||
&& is_in_quote(line, i))
|
||||
i++;
|
||||
else if (line[i] && line[i] != '<' && line[i] != '>')
|
||||
i++;
|
||||
}
|
||||
return (line);
|
||||
}
|
||||
64
srcs/set_redirection/set_heredoc.c
Normal file
64
srcs/set_redirection/set_heredoc.c
Normal file
@ -0,0 +1,64 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* set_heredoc.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/19 14:46:01 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/23 13:08:11 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
char *set_heredoc(int index, char **in)
|
||||
{
|
||||
char *nbr_file;
|
||||
char *file_name;
|
||||
int fd;
|
||||
|
||||
if (index)
|
||||
{
|
||||
nbr_file = ft_itoa(index + 1);
|
||||
file_name = ft_strjoin(".heredoc", nbr_file);
|
||||
}
|
||||
else
|
||||
file_name = ft_strjoin(".heredoc", 0);
|
||||
fd = open(file_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
|
||||
if (fd > 0)
|
||||
{
|
||||
print_double_fd(in, fd);
|
||||
free_double(in);
|
||||
}
|
||||
close(fd);
|
||||
return (file_name);
|
||||
}
|
||||
|
||||
int wait_prompt(t_s_cmd *cmd, int index, int i, char *input)
|
||||
{
|
||||
char **history;
|
||||
char *in;
|
||||
|
||||
change_signal();
|
||||
in = ft_strjoin(cmd->infile, "\n");
|
||||
free(cmd->infile);
|
||||
cmd->infile = 0;
|
||||
history = 0;
|
||||
while (i == 0 || (input && ft_strlen(input) && ft_strcmp(input, in)))
|
||||
{
|
||||
i = 1;
|
||||
if (input)
|
||||
free(input);
|
||||
ft_putstr_fd("> ", 0);
|
||||
input = get_next_line(0);
|
||||
if (!input)
|
||||
return (free_wait_prompt(in, history));
|
||||
history = fill_history(cmd, input, in, history);
|
||||
}
|
||||
free(in);
|
||||
free(input);
|
||||
cmd->infile = set_heredoc(index, history);
|
||||
cmd->in_type = 0;
|
||||
return (1);
|
||||
}
|
||||
76
srcs/set_redirection/set_input.c
Normal file
76
srcs/set_redirection/set_input.c
Normal file
@ -0,0 +1,76 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* set_input.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/19 14:44:22 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/23 13:08:35 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
int check_access_input(t_s_cmd *cmd)
|
||||
{
|
||||
if (access(cmd->infile, R_OK))
|
||||
{
|
||||
g_var = 1;
|
||||
ft_putstr_fd("Minishell: ", 2);
|
||||
ft_putstr_fd(cmd->infile, 2);
|
||||
if (access(cmd->infile, F_OK))
|
||||
ft_putstr_fd(": no such file\n", 2);
|
||||
else
|
||||
ft_putstr_fd(": Permission denied\n", 2);
|
||||
free(cmd->infile);
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
char *set_input(char *line, t_s_cmd *cmd, int index)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = index + 1;
|
||||
if (line[i] == '<')
|
||||
i++;
|
||||
while (line[i] == ' ' && line[i])
|
||||
i++;
|
||||
while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i])
|
||||
i++;
|
||||
if (cmd->infile)
|
||||
free(cmd->infile);
|
||||
cmd->infile = get_word(line, index);
|
||||
cmd->infile = set_var(cmd->big_cmd, cmd->infile);
|
||||
if (cmd->in_type == 0 && !check_access_input(cmd))
|
||||
{
|
||||
free(line);
|
||||
return (0);
|
||||
}
|
||||
line = cut_str(line, index, i);
|
||||
return (line);
|
||||
}
|
||||
|
||||
char *ft_input(char *line, t_s_cmd *cmd, int index)
|
||||
{
|
||||
int i;
|
||||
char next;
|
||||
|
||||
i = index;
|
||||
next = next_space(line, i + 1);
|
||||
if (line[i + 1] == '<')
|
||||
{
|
||||
cmd->in_type = 1;
|
||||
next = next_space(line, i + 2);
|
||||
}
|
||||
else
|
||||
cmd->in_type = 0;
|
||||
if (next == '<' || next == '>' || !next)
|
||||
return (error_parsing(line));
|
||||
line = set_input(line, cmd, i);
|
||||
if (!line)
|
||||
return (0);
|
||||
return (line);
|
||||
}
|
||||
96
srcs/set_redirection/set_output.c
Normal file
96
srcs/set_redirection/set_output.c
Normal file
@ -0,0 +1,96 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* set_output.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/19 14:45:10 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/23 13:08:53 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
char *find_var(t_cmd *big_cmd, char *cmd, int i, int *index)
|
||||
{
|
||||
char *swap;
|
||||
char *swap2;
|
||||
char *ret;
|
||||
char *var;
|
||||
|
||||
swap = ft_substr(cmd, *index + 1, i - *index - 1);
|
||||
var = get_var(big_cmd, swap);
|
||||
free(swap);
|
||||
swap2 = ft_strdup(cmd + i);
|
||||
cmd[*index] = 0;
|
||||
ret = ft_strjoin(cmd, var);
|
||||
free(cmd);
|
||||
if (*index > 0)
|
||||
*index += ft_strlen(var) - 1;
|
||||
free(var);
|
||||
var = ret;
|
||||
ret = ft_strjoin(ret, swap2);
|
||||
free(var);
|
||||
free(swap2);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
int check_access_output(t_s_cmd *cmd)
|
||||
{
|
||||
if (!access(cmd->outfile, F_OK) && access(cmd->outfile, W_OK))
|
||||
{
|
||||
g_var = 1;
|
||||
ft_putstr_fd("Minishell: ", 2);
|
||||
ft_putstr_fd(cmd->outfile, 2);
|
||||
ft_putstr_fd(": Permission denied\n", 2);
|
||||
free(cmd->outfile);
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
char *set_output(char *line, t_s_cmd *cmd, int index)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = index;
|
||||
i++;
|
||||
if (line[i] == '>')
|
||||
i++;
|
||||
while (line[i] == ' ' && line[i])
|
||||
i++;
|
||||
while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i])
|
||||
i++;
|
||||
if (cmd->outfile)
|
||||
free(cmd->outfile);
|
||||
cmd->outfile = get_word(line, index);
|
||||
cmd->outfile = set_var(cmd->big_cmd, cmd->outfile);
|
||||
if (!check_access_output(cmd))
|
||||
{
|
||||
free(line);
|
||||
return (0);
|
||||
}
|
||||
line = cut_str(line, index, i);
|
||||
return (line);
|
||||
}
|
||||
|
||||
char *ft_output(char *line, t_s_cmd *cmd, int index)
|
||||
{
|
||||
int i;
|
||||
char next;
|
||||
|
||||
i = index;
|
||||
next = next_space(line, i + 1);
|
||||
if (line[i + 1] == '>')
|
||||
{
|
||||
cmd->in_type = 1;
|
||||
next = next_space(line, i + 2);
|
||||
}
|
||||
else
|
||||
cmd->in_type = 0;
|
||||
if (next == '<' || next == '>' || !next)
|
||||
return (error_parsing(line));
|
||||
line = set_output(line, cmd, i);
|
||||
return (line);
|
||||
}
|
||||
78
srcs/set_redirection/utils.c
Normal file
78
srcs/set_redirection/utils.c
Normal file
@ -0,0 +1,78 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/11 16:35:37 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/20 15:49:47 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
char *cut_str(char *str, int start, int end)
|
||||
{
|
||||
char *swap;
|
||||
char *del;
|
||||
|
||||
swap = 0;
|
||||
del = str;
|
||||
if (str[end])
|
||||
swap = ft_strjoin(&str[end], 0);
|
||||
str[start] = 0;
|
||||
str = ft_strjoin(str, swap);
|
||||
free(del);
|
||||
if (swap)
|
||||
free(swap);
|
||||
return (str);
|
||||
}
|
||||
|
||||
char next_space(char *str, int i)
|
||||
{
|
||||
while (str[i] == ' ')
|
||||
i++;
|
||||
return (str[i]);
|
||||
}
|
||||
|
||||
int double_size(char **tab)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (tab == 0)
|
||||
return (0);
|
||||
while (tab[i])
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
void free_double(char **tab)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (tab)
|
||||
{
|
||||
while (tab[i])
|
||||
free(tab[i++]);
|
||||
free(tab);
|
||||
}
|
||||
}
|
||||
|
||||
void print_double_fd(char **tab, int fd)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (tab)
|
||||
{
|
||||
while (tab[i])
|
||||
{
|
||||
ft_putstr_fd(tab[i], fd);
|
||||
ft_putstr_fd("\n", fd);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
29
srcs/set_signals/set_signal.c
Normal file
29
srcs/set_signals/set_signal.c
Normal file
@ -0,0 +1,29 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* set_signal.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/19 15:30:30 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/23 13:12:16 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
void crtl_c(int num)
|
||||
{
|
||||
(void)num;
|
||||
g_var = 130;
|
||||
printf("\n");
|
||||
rl_replace_line("", 0);
|
||||
rl_on_new_line();
|
||||
rl_redisplay();
|
||||
}
|
||||
|
||||
void sig_quit(int num)
|
||||
{
|
||||
(void)num;
|
||||
ft_putstr_fd("\b \b\b \b", 1);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user