Nouvelle bibliothèque avec fonction pour gérer les erreurs
This commit is contained in:
parent
5584f391d7
commit
8db32fad0a
4 changed files with 95 additions and 50 deletions
41
src/boitoutil/essential_shell.c
Normal file
41
src/boitoutil/essential_shell.c
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
/**
|
||||||
|
* @file essential_shell.c
|
||||||
|
* @author rick <rick@gnous.eu>
|
||||||
|
* @date 2021
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "essential_shell.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* error(): gère les erreurs selon leur code et leur type
|
||||||
|
* @code: code de l’erreur, voir les différents codes plus bas
|
||||||
|
* @type: NON_FATAL_ERROR pour continuer l’exécution
|
||||||
|
* FATAL_ERROR pour stoper le programme
|
||||||
|
* @message: message à afficher pour + d’infos ou erreur non implémentée
|
||||||
|
*
|
||||||
|
* 1 erreur lors de la création des pipes
|
||||||
|
*/
|
||||||
|
void error(int code, int type, char *message)
|
||||||
|
{
|
||||||
|
switch (code)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
printf("Erreur lors de la création des pipes.\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
if (message == NULL)
|
||||||
|
printf("Erreur inconnue.\n");
|
||||||
|
else
|
||||||
|
printf("%s\n", message);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (message != NULL)
|
||||||
|
printf("Message complémentaire :\n%s\n", message);
|
||||||
|
|
||||||
|
if (type == FATAL_ERROR)
|
||||||
|
exit(code);
|
||||||
|
}
|
19
src/boitoutil/essential_shell.h
Normal file
19
src/boitoutil/essential_shell.h
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
/**
|
||||||
|
* Header pour la bibliothèque permettant d’avoir des
|
||||||
|
* outils essentiels pour le shell tel que la fonction pour
|
||||||
|
* afficher des erreurs.
|
||||||
|
* @file essential_shell.h
|
||||||
|
* @author rick <rick@gnous.eu>
|
||||||
|
* @date 2021
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _ESSHELL_
|
||||||
|
# define _ESSHELL_
|
||||||
|
|
||||||
|
#define SHELL rishtik
|
||||||
|
#define FATAL_ERROR 1
|
||||||
|
#define NON_FATAL_ERROR 0
|
||||||
|
|
||||||
|
void error(int code, int type, char *message);
|
||||||
|
|
||||||
|
#endif
|
|
@ -11,7 +11,6 @@
|
||||||
# define _PARSER_H_
|
# define _PARSER_H_
|
||||||
|
|
||||||
#define MAX_LENGTH 200 /* taille maximum des tableaux utilisés */
|
#define MAX_LENGTH 200 /* taille maximum des tableaux utilisés */
|
||||||
#define SHELL rishtik
|
|
||||||
|
|
||||||
char* get_input();
|
char* get_input();
|
||||||
void get_command(char *args[], char find);
|
void get_command(char *args[], char find);
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
|
#include "essential_shell.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
@ -14,93 +15,78 @@
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int pid;
|
/** commands contient l’entrée utilisateur parsée avec | :
|
||||||
|
* ls | wc -l -> [ls, wc -l]
|
||||||
|
*
|
||||||
|
* args contient une commande à la fois avec ses arguments séparés
|
||||||
|
* par les espaces :
|
||||||
|
* ls -R test -> [ls, -R, test]
|
||||||
|
*/
|
||||||
|
char *commands[MAX_LENGTH];
|
||||||
char *args[MAX_LENGTH];
|
char *args[MAX_LENGTH];
|
||||||
char *command[MAX_LENGTH];
|
|
||||||
|
int pid, index = 0, end = 0; /* end permet de savoir quand s’arreter */
|
||||||
int my_pipe[2];
|
int my_pipe[2];
|
||||||
|
|
||||||
if (pipe(my_pipe) == -1)
|
if (pipe(my_pipe) == -1)
|
||||||
{
|
error(1, FATAL_ERROR, NULL);
|
||||||
printf("erreur pipe");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
for (int i = 0; i < MAX_LENGTH; i++)
|
for (int i = 0; i < MAX_LENGTH; i++)
|
||||||
{
|
{
|
||||||
args[i] = (char *) calloc(MAX_LENGTH, sizeof(char));
|
args[i] = (char *) calloc(MAX_LENGTH, sizeof(char));
|
||||||
command[i] = (char *) calloc(MAX_LENGTH, sizeof(char));
|
commands[i] = (char *) calloc(MAX_LENGTH, sizeof(char));
|
||||||
}
|
}
|
||||||
|
|
||||||
get_command(args, '|');
|
get_command(commands, '|');
|
||||||
|
|
||||||
int index = 0;
|
while (strcmp(commands[0], "exit"))
|
||||||
while (strcmp(args[0], "exit"))
|
|
||||||
{
|
{
|
||||||
while (args[index] != NULL)
|
while (commands[index] != NULL)
|
||||||
{
|
{
|
||||||
if (strncmp(args[index], "cd", 2))
|
|
||||||
{
|
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (!pid)
|
if (!pid)
|
||||||
{
|
{
|
||||||
parse_string(args[index], command, ' ');
|
parse_string(commands[index], args, ' ');
|
||||||
if (args[1] != NULL)
|
if (commands[index+1] == NULL)
|
||||||
{
|
dup2(my_pipe[0], STDIN_FILENO);
|
||||||
if (args[index+1] == NULL)
|
else if (index == 0)
|
||||||
dup2(my_pipe[0], STDIN_FILENO);
|
dup2(my_pipe[1], STDOUT_FILENO);
|
||||||
else if (index == 0)
|
close(my_pipe[0]);
|
||||||
dup2(my_pipe[1], STDOUT_FILENO);
|
close(my_pipe[1]);
|
||||||
/*else {
|
|
||||||
dup2(my_pipe[0], STDIN_FILENO);
|
|
||||||
dup2(my_pipe[1], STDOUT_FILENO);
|
|
||||||
}*/
|
|
||||||
close(my_pipe[0]);
|
|
||||||
close(my_pipe[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
execvp(command[0], command);
|
execvp(args[0], args);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (args[index + 1] == NULL)
|
if (commands[index + 1] == NULL)
|
||||||
{
|
{
|
||||||
close(my_pipe[1]);
|
close(my_pipe[1]);
|
||||||
close(my_pipe[0]);
|
close(my_pipe[0]);
|
||||||
waitpid(pid, NULL, 0);
|
waitpid(pid, NULL, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
parse_string(args[index], command, ' ');
|
|
||||||
execvp(command[0], command);
|
|
||||||
for (int i = 0; i < MAX_LENGTH; i++)
|
|
||||||
{
|
|
||||||
free(command[i]);
|
|
||||||
command[i] = (char *) calloc(MAX_LENGTH, sizeof(char));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* remise à 0 des entrées, des commandes, des pipes et de l’index */
|
||||||
for (int i = 0; i < MAX_LENGTH; i++)
|
for (int i = 0; i < MAX_LENGTH; i++)
|
||||||
{
|
{
|
||||||
free(args[i]);
|
free(args[i]);
|
||||||
free(command[i]);
|
free(commands[i]);
|
||||||
args[i] = (char *) calloc(MAX_LENGTH, sizeof(char));
|
args[i] = (char *) calloc(MAX_LENGTH, sizeof(char));
|
||||||
command[i] = (char *) calloc(MAX_LENGTH, sizeof(char));
|
commands[i] = (char *) calloc(MAX_LENGTH, sizeof(char));
|
||||||
}
|
}
|
||||||
get_command(args, '|');
|
|
||||||
|
get_command(commands, '|');
|
||||||
if (pipe(my_pipe) == -1)
|
if (pipe(my_pipe) == -1)
|
||||||
{
|
error(1, FATAL_ERROR, NULL);
|
||||||
printf("erreur sur le pipe");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
index = 0;
|
index = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < MAX_LENGTH; i++)
|
for (int i = 0; i < MAX_LENGTH; i++)
|
||||||
{
|
{
|
||||||
free(command[i]);
|
free(commands[i]);
|
||||||
free(args[i]);
|
free(args[i]);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in a new issue