rishtik/src/shellOpt.c

127 lines
3.1 KiB
C
Raw Normal View History

/**
* @file shellOpt.c
* @author rick <rick@gnous.eu>
* @date 2021
*/
#include "vars.h"
#include "parser.h"
#include "essential_shell.h"
2021-02-16 15:12:37 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
2021-02-16 15:12:37 +00:00
#include <sys/wait.h>
#include <signal.h>
2021-02-16 15:12:37 +00:00
int main()
{
/** commands contient lentré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];
int result, index = 0;
2021-02-16 15:12:37 +00:00
int my_pipe[2];
/* init pour la gestion du ctrl c */
struct sigaction ctrc_handler;
ctrc_handler.sa_handler = ctrl_c_handler;
sigemptyset(&ctrc_handler.sa_mask);
ctrc_handler.sa_flags = 0;
sigaction(SIGINT, &ctrc_handler, NULL);
setenv("HELLO", "test", 1); /* juste un test */
2021-02-16 15:12:37 +00:00
if (pipe(my_pipe) == -1)
error(ERR_PIPE_CREATION, FATAL_ERROR, NULL);
2021-02-16 15:12:37 +00:00
for (int i = 0; i < MAX_LENGTH; i++)
{
args[i] = (char *) calloc(MAX_LENGTH, sizeof(char));
commands[i] = (char *) calloc(MAX_LENGTH, sizeof(char));
2021-02-16 15:12:37 +00:00
}
while (!need_exit && !get_command(commands, '|'))
2021-02-16 15:12:37 +00:00
{
while (commands[index] != NULL)
2021-02-16 15:12:37 +00:00
{
2021-02-18 17:37:00 +00:00
parse_string(commands[index], args, ' ');
if (!native_command(args)) /* si la commande demandée nest pas native */
2021-02-18 17:37:00 +00:00
{
pid = fork();
2021-02-21 10:53:43 +00:00
if (pid < 0)
error(ERR_FORK, FATAL_ERROR, NULL);
2021-02-21 10:53:43 +00:00
if (!pid) /* le fils */
2021-02-16 15:12:37 +00:00
{
if (commands[index+1] == NULL)
{
2021-02-18 17:37:00 +00:00
dup2(my_pipe[0], STDIN_FILENO);
close(my_pipe[1]);
}
2021-02-18 17:37:00 +00:00
else if (index == 0)
{
2021-02-18 17:37:00 +00:00
dup2(my_pipe[1], STDOUT_FILENO);
close(my_pipe[0]);
}
else
fclose(fdopen(my_pipe[0], "w"));
/* si la commande est intermédiaire dans le pipe, on vide le buffer
* de sortie NE MARCHE PAS */
2021-02-18 17:37:00 +00:00
execvp(args[0], args);
exit(errno);
2021-02-17 09:51:41 +00:00
}
2021-02-18 16:20:46 +00:00
/* lorsquon arrive à la dernière commande, on peut fermer le pipe et
* attendre le dernier processus */
2021-02-18 17:37:00 +00:00
if (commands[index + 1] == NULL)
2021-02-18 16:20:46 +00:00
{
2021-02-18 17:37:00 +00:00
close(my_pipe[0]);
close(my_pipe[1]);
2021-02-18 16:20:46 +00:00
}
waitpid(pid, &result, 0);
if(WIFEXITED(result)) /* on récupère le code de retour pour afficher lerreur */
error(WEXITSTATUS(result), NON_FATAL_ERROR, NULL);
2021-02-18 17:37:00 +00:00
}
for (int i = 0; i < MAX_LENGTH; i++)
{
free(args[i]);
args[i] = (char *) calloc(MAX_LENGTH, sizeof(char));
}
index++;
2021-02-16 15:12:37 +00:00
}
/* remise à 0 des entrées, des commandes, des pipes et de lindex */
2021-02-16 15:12:37 +00:00
for (int i = 0; i < MAX_LENGTH; i++)
{
free(args[i]);
free(commands[i]);
2021-02-16 15:12:37 +00:00
args[i] = (char *) calloc(MAX_LENGTH, sizeof(char));
commands[i] = (char *) calloc(MAX_LENGTH, sizeof(char));
2021-02-16 15:12:37 +00:00
}
if (pipe(my_pipe) == -1)
error(ERR_PIPE_CREATION, FATAL_ERROR, NULL);
2021-02-16 15:12:37 +00:00
index = 0;
}
for (int i = 0; i < MAX_LENGTH; i++)
{
free(commands[i]);
2021-02-16 15:12:37 +00:00
free(args[i]);
}
return exit_code;
2021-02-16 15:12:37 +00:00
}