Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3df8bfa8a8 | |||
| df04c2fecc | |||
|
|
0da6bb86c4 | ||
| c3ed52819f | |||
| 1ec3636d5d | |||
|
|
fa7b31df86 | ||
| cbd9442469 | |||
| 211c35dacf | |||
| ade8aef236 | |||
| 470e2bb471 |
12
Makefile
12
Makefile
@ -3,10 +3,10 @@
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ #
|
||||
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2022/03/06 12:50:24 by apommier #+# #+# #
|
||||
# Updated: 2022/04/20 17:39:51 by sadjigui ### ########.fr #
|
||||
# Updated: 2022/04/23 13:33:48 by apommier ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@ -41,17 +41,15 @@ SRCS = srcs/main.c\
|
||||
srcs/set_quote/set_var.c\
|
||||
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
CC = clang
|
||||
CFLAGS = -Wall -Wextra -g
|
||||
CC = gcc
|
||||
CFLAGS = -Wall -Wextra -Werror -fcommon
|
||||
LIB = -lreadline
|
||||
RM = rm -rf
|
||||
LIBFT = ./libft
|
||||
|
||||
#-include ./valgrind.mk
|
||||
|
||||
${NAME}: ${OBJS}
|
||||
@make bonus -C ${LIBFT}
|
||||
@${CC} ${LIB} ${OBJS} ${LIBFT}/libft.a -o ${NAME}
|
||||
${CC} ${OBJS} ${LIBFT}/libft.a -o ${NAME} -lreadline
|
||||
|
||||
all: ${NAME}
|
||||
|
||||
|
||||
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
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/09 22:33:49 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/20 16:58:49 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/23 13:37:53 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -27,6 +27,8 @@
|
||||
# include <errno.h>
|
||||
# include <signal.h>
|
||||
# include <dirent.h>
|
||||
# include <readline/readline.h>
|
||||
# include <readline/history.h>
|
||||
|
||||
// Command Data Structure
|
||||
|
||||
@ -35,7 +37,6 @@ typedef struct s_simple {
|
||||
struct s_command *big_cmd;
|
||||
int fd[2];
|
||||
int pipe[2];
|
||||
int last;
|
||||
int last_pipe[2];
|
||||
int child;
|
||||
int nb_args;
|
||||
@ -47,6 +48,8 @@ typedef struct s_simple {
|
||||
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 {
|
||||
@ -68,7 +71,7 @@ char **ft_dup_double(char **env);
|
||||
void execute(t_cmd *cmd);
|
||||
|
||||
//set_cmd.c
|
||||
char *error_parsing(void);
|
||||
char *error_parsing(char *to_free);
|
||||
t_cmd *set_cmd(char *input, char **path, int nb);
|
||||
|
||||
//exec_utils.c
|
||||
@ -121,7 +124,9 @@ char *ft_input(char *line, t_s_cmd *cmd, int index);
|
||||
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
|
||||
|
||||
|
||||
@ -3,10 +3,10 @@
|
||||
/* ::: :::::::: */
|
||||
/* cd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/21 18:30:26 by sadjigui #+# #+# */
|
||||
/* Updated: 2022/04/20 16:09:32 by sadjigui ### ########.fr */
|
||||
/* Updated: 2022/04/23 13:10:35 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -15,11 +15,10 @@
|
||||
void add_one(t_s_cmd *cmd)
|
||||
{
|
||||
char p[1024];
|
||||
char *str;
|
||||
int i;
|
||||
|
||||
i = find_it(cmd->big_cmd->env, "PWD");
|
||||
str = getcwd(p, sizeof(p));
|
||||
getcwd(p, sizeof(p));
|
||||
free(cmd->big_cmd->env[i]);
|
||||
cmd->big_cmd->env[i] = ft_strjoin("PWD=", p);
|
||||
}
|
||||
@ -87,13 +86,10 @@ int check_dir(t_s_cmd *cmd)
|
||||
int open_directory(t_s_cmd *cmd)
|
||||
{
|
||||
char *p;
|
||||
int j;
|
||||
int i;
|
||||
|
||||
p = NULL;
|
||||
i = 0;
|
||||
if (find_it(cmd->big_cmd->env, "PWD") != -1)
|
||||
j = size_path(cmd->big_cmd->env);
|
||||
if (cmd->nb_args > 2)
|
||||
return (cd_error(cmd, "Minishell: cd: too many arguments", 1));
|
||||
if (!cmd->args[1])
|
||||
|
||||
@ -3,10 +3,10 @@
|
||||
/* ::: :::::::: */
|
||||
/* exit.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/15 11:23:32 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/20 17:48:23 by sadjigui ### ########.fr */
|
||||
/* Created: 2022/04/20 17:53:35 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/20 18:14:27 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -43,18 +43,15 @@ int max_long(char *nbr)
|
||||
unsigned long long long_max;
|
||||
|
||||
long_max = 9223372036854775807;
|
||||
printf("%s\n", nbr);
|
||||
if (ft_strlen(nbr) > 20)
|
||||
return (1);
|
||||
if (nbr[0] == '-')
|
||||
{
|
||||
printf("--->%lld\n", ft_atoi_long(nbr));
|
||||
if (ft_atoi_long(nbr + 1) > long_max + 1)
|
||||
return (1);
|
||||
}
|
||||
else if (ft_atoi_long(nbr) > long_max)
|
||||
return (1);
|
||||
printf("return 0\n");
|
||||
return (0);
|
||||
}
|
||||
|
||||
@ -64,11 +61,8 @@ void exit_error(t_cmd *cmd)
|
||||
exit_shell(cmd, 2);
|
||||
}
|
||||
|
||||
void ft_exit(t_s_cmd *cmd)
|
||||
int check_exit_args(t_s_cmd *cmd)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = -1;
|
||||
if (cmd->nb_args > 2)
|
||||
{
|
||||
ft_putstr_fd("Minishell: exit: too many arguments\n", 2);
|
||||
@ -76,14 +70,27 @@ void ft_exit(t_s_cmd *cmd)
|
||||
cmd->big_cmd->err_var = 1;
|
||||
else
|
||||
exit(1);
|
||||
return ;
|
||||
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])
|
||||
while (cmd->args[1][i] == ' ')
|
||||
i++;
|
||||
if (cmd->args[1][i] == '-')
|
||||
i++;
|
||||
while (cmd->args[1][i])
|
||||
{
|
||||
if ((!ft_isdigit(cmd->args[1][i]) && !(cmd->args[1][i] == '-'
|
||||
&& ft_isdigit(cmd->args[1][i + 1]))))
|
||||
if (!ft_isdigit(cmd->args[1][i++]))
|
||||
exit_error(cmd->big_cmd);
|
||||
}
|
||||
if (max_long(cmd->args[1]))
|
||||
|
||||
@ -3,10 +3,10 @@
|
||||
/* ::: :::::::: */
|
||||
/* export2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/07 17:58:25 by sadjigui #+# #+# */
|
||||
/* Updated: 2022/04/19 13:18:17 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/22 14:54:54 by sadjigui ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -51,8 +51,15 @@ void print_export(char *tmp)
|
||||
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("%s", str2);
|
||||
}
|
||||
printf("\"");
|
||||
}
|
||||
printf("\n");
|
||||
free(str1);
|
||||
free(str2);
|
||||
|
||||
@ -3,10 +3,10 @@
|
||||
/* ::: :::::::: */
|
||||
/* unset.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/11 18:26:29 by sadjigui #+# #+# */
|
||||
/* Updated: 2022/04/20 15:01:03 by sadjigui ### ########.fr */
|
||||
/* Updated: 2022/04/23 13:09:17 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -42,10 +42,8 @@ void unset_variable(t_s_cmd *cmd, int i)
|
||||
int find_variable(char *variable, t_s_cmd *cmd)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = find_it(cmd->big_cmd->env, variable);
|
||||
j = 0;
|
||||
if (check_variable(variable) == 1)
|
||||
{
|
||||
ft_putstr_fd("Minishell: unset: `", 2);
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/06 13:27:11 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/20 04:53:03 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/23 13:16:15 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -35,6 +35,7 @@ char **ft_dup_double(char **env)
|
||||
|
||||
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)
|
||||
{
|
||||
@ -44,12 +45,12 @@ char **read_line(char **path, char *input, t_cmd *cmd, int *err_var)
|
||||
add_history(input);
|
||||
if (ft_strlen(input) && next_space(input, 0) && input && path)
|
||||
{
|
||||
cmd = set_cmd(input, path, *err_var);
|
||||
cmd = set_cmd(input, path, g_var);
|
||||
if (cmd)
|
||||
{
|
||||
free_double(path);
|
||||
execute(cmd);
|
||||
*err_var = cmd->err_var;
|
||||
g_var = cmd->err_var;
|
||||
path = ft_dup_double(cmd->env);
|
||||
free_cmd(cmd);
|
||||
cmd = 0;
|
||||
@ -72,6 +73,7 @@ int main(int ac, char **av, char **path)
|
||||
{
|
||||
char **env;
|
||||
|
||||
(void)av;
|
||||
if (!isatty(0))
|
||||
{
|
||||
printf("Not today\n");
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/20 04:43:32 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/20 04:44:20 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/22 11:51:32 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -42,7 +42,6 @@ void exec_cmd(t_cmd *cmd, char **env, int *fdpipe)
|
||||
|
||||
void exec_last_cmd(t_cmd *cmd, int *fdin, int *fdout, int i)
|
||||
{
|
||||
cmd->current_s_cmd->last = 1;
|
||||
if (cmd->current_s_cmd->outfile)
|
||||
*fdout = open(cmd->current_s_cmd->outfile,
|
||||
O_RDWR | O_CREAT | O_APPEND, 0666);
|
||||
@ -58,7 +57,6 @@ void exec_last_cmd(t_cmd *cmd, int *fdin, int *fdout, int i)
|
||||
|
||||
void exec_not_last_cmd(t_cmd *cmd, int *fdpipe, int *fdin, int *fdout)
|
||||
{
|
||||
cmd->current_s_cmd->last = 0;
|
||||
pipe(fdpipe);
|
||||
cmd->current_s_cmd->fd[0] = *fdin;
|
||||
if (cmd->current_s_cmd->outfile)
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/09 12:33:30 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/19 15:01:32 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/23 13:18:23 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,28 +14,21 @@
|
||||
|
||||
void exit_shell(t_cmd *cmd, int ret)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
ft_putstr_fd("exit\n", 1);
|
||||
close (0);
|
||||
close (1);
|
||||
if (!cmd)
|
||||
exit(ret);
|
||||
if (cmd->tmpin != -1)
|
||||
{
|
||||
dup2(cmd->tmpin, 0);
|
||||
close(cmd->tmpin);
|
||||
}
|
||||
if (cmd->tmpout != -1)
|
||||
{
|
||||
dup2(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);
|
||||
clear_history();
|
||||
rl_clear_history();
|
||||
cmd = 0;
|
||||
exit(ret);
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/08 15:19:42 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/20 15:49:33 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/23 13:07:57 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -16,14 +16,13 @@ t_s_cmd *set_s_cmd(char *line, int index)
|
||||
{
|
||||
t_s_cmd *s_cmd;
|
||||
char **split_line;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
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;
|
||||
@ -111,7 +110,7 @@ t_cmd *set_cmd(char *input, char **env, int nb)
|
||||
|
||||
if (!is_quote_good(input) || !is_pipe_good(input))
|
||||
{
|
||||
ft_putstr_fd("Minishell: error while parsing command\n", 2);
|
||||
error_parsing(0);
|
||||
return (0);
|
||||
}
|
||||
cmds = ft_split_with_quote(input, '|');
|
||||
|
||||
@ -6,14 +6,17 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/19 16:53:13 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/20 16:51:05 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/22 11:43:15 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
char *error_parsing(void)
|
||||
char *error_parsing(char *to_free)
|
||||
{
|
||||
g_var = 2;
|
||||
if (to_free)
|
||||
free(to_free);
|
||||
ft_putstr_fd("Minishell: error while parsing command\n", 2);
|
||||
return (0);
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/09 23:58:21 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/19 17:19:17 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/23 13:11:50 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -85,17 +85,13 @@ int parse_quote(t_cmd *cmd)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
char *swap;
|
||||
|
||||
i = -1;
|
||||
while (cmd->s_cmds[++i])
|
||||
{
|
||||
j = -1;
|
||||
while (cmd->s_cmds[i]->args[++j])
|
||||
{
|
||||
swap = 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);
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/19 15:02:01 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/20 14:48:26 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/22 13:01:30 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -53,27 +53,20 @@ char *get_var(t_cmd *cmd, char *var_name)
|
||||
char *change_var(t_cmd *big_cmd, char *cmd, int *index)
|
||||
{
|
||||
int i;
|
||||
char *swap;
|
||||
char *swap2;
|
||||
char *ret;
|
||||
char *var;
|
||||
|
||||
i = *index + 1;
|
||||
while (cmd[i] && (ft_isalnum(cmd[i]) || cmd[i] == '_' || cmd[i] == '?' || cmd[i] == '$'))
|
||||
while (cmd[i] && (ft_isalnum(cmd[i]) || cmd[i] == '_'
|
||||
|| cmd[i] == '?' || cmd[i] == '$'))
|
||||
i++;
|
||||
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);
|
||||
if (i == *index + 1)
|
||||
{
|
||||
(*index)++;
|
||||
ret = ft_strdup(cmd);
|
||||
free(cmd);
|
||||
*index += ft_strlen(var) - 1;
|
||||
free(var);
|
||||
var = ret;
|
||||
ret = ft_strjoin(ret, swap2);
|
||||
free(var);
|
||||
free(swap2);
|
||||
return (ret);
|
||||
}
|
||||
ret = find_var(big_cmd, cmd, i, index);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/10 19:50:50 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/19 16:15:11 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/23 13:11:27 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -31,7 +31,7 @@ int next_quote(const char *s, int i)
|
||||
while (s[i] != '\'')
|
||||
i++;
|
||||
}
|
||||
else
|
||||
else if (double_quote)
|
||||
{
|
||||
while (s[i] != '"')
|
||||
i++;
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/19 19:25:16 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/20 04:55:14 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/22 11:24:29 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/09 15:18:58 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/20 16:49:33 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/20 17:05:09 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/19 14:46:01 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/19 19:36:08 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/23 13:08:11 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -17,9 +17,7 @@ char *set_heredoc(int index, char **in)
|
||||
char *nbr_file;
|
||||
char *file_name;
|
||||
int fd;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (index)
|
||||
{
|
||||
nbr_file = ft_itoa(index + 1);
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/19 14:44:22 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/20 16:50:37 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/23 13:08:35 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -16,6 +16,7 @@ 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))
|
||||
@ -31,13 +32,10 @@ int check_access_input(t_s_cmd *cmd)
|
||||
char *set_input(char *line, t_s_cmd *cmd, int index)
|
||||
{
|
||||
int i;
|
||||
int word_index;
|
||||
|
||||
word_index = 0;
|
||||
i = index + 1;
|
||||
if (line[i] == '<')
|
||||
i++;
|
||||
word_index = i;
|
||||
while (line[i] == ' ' && line[i])
|
||||
i++;
|
||||
while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i])
|
||||
@ -70,7 +68,7 @@ char *ft_input(char *line, t_s_cmd *cmd, int index)
|
||||
else
|
||||
cmd->in_type = 0;
|
||||
if (next == '<' || next == '>' || !next)
|
||||
return (error_parsing());
|
||||
return (error_parsing(line));
|
||||
line = set_input(line, cmd, i);
|
||||
if (!line)
|
||||
return (0);
|
||||
|
||||
@ -6,21 +6,43 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/19 14:45:10 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/20 16:45:06 by apommier ### ########.fr */
|
||||
/* 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, W_OK))
|
||||
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);
|
||||
if (access(cmd->outfile, F_OK))
|
||||
ft_putstr_fd(": no such file\n", 2);
|
||||
else
|
||||
ft_putstr_fd(": Permission denied\n", 2);
|
||||
free(cmd->outfile);
|
||||
return (0);
|
||||
@ -28,18 +50,14 @@ int check_access_output(t_s_cmd *cmd)
|
||||
return (1);
|
||||
}
|
||||
|
||||
|
||||
char *set_output(char *line, t_s_cmd *cmd, int index)
|
||||
{
|
||||
int i;
|
||||
int word_index;
|
||||
|
||||
word_index = 0;
|
||||
i = index;
|
||||
i++;
|
||||
if (line[i] == '>')
|
||||
i++;
|
||||
word_index = i;
|
||||
while (line[i] == ' ' && line[i])
|
||||
i++;
|
||||
while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i])
|
||||
@ -72,7 +90,7 @@ char *ft_output(char *line, t_s_cmd *cmd, int index)
|
||||
else
|
||||
cmd->in_type = 0;
|
||||
if (next == '<' || next == '>' || !next)
|
||||
return (0);
|
||||
return (error_parsing(line));
|
||||
line = set_output(line, cmd, i);
|
||||
return (line);
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/19 15:30:30 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/19 16:58:02 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/23 13:12:16 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,7 +14,8 @@
|
||||
|
||||
void crtl_c(int num)
|
||||
{
|
||||
num = 0;
|
||||
(void)num;
|
||||
g_var = 130;
|
||||
printf("\n");
|
||||
rl_replace_line("", 0);
|
||||
rl_on_new_line();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user