|
@@ -18,6 +18,7 @@
|
|
|
#include "command.h"
|
|
|
#include "execute.h"
|
|
|
#include "mysh.h"
|
|
|
+#include "pipe_cste.h"
|
|
|
|
|
|
/* --- Extern --- */
|
|
|
extern Error error;
|
|
@@ -149,14 +150,14 @@ int main(int argc, char* argv[]) {
|
|
|
show_current_dir(before, ">\x1b[0m ");
|
|
|
//Lecture ligne
|
|
|
if(get_line(line) == SHELL_ERR){
|
|
|
- error.print("Impossible de lire les commandes");
|
|
|
+ error.print("Impossible de lire les commandes\n");
|
|
|
clean_pid(&pidlist);
|
|
|
error.exit_err();
|
|
|
}
|
|
|
//Parse la ligne et commandes
|
|
|
result = parse_line(&ct, line);
|
|
|
if(result == SHELL_ERR){
|
|
|
- error.print("Impossible d'analyser la ligne");
|
|
|
+ error.print("Impossible d'analyser la ligne\n");
|
|
|
addserror("Erreur lors du parse de la ligne");
|
|
|
continue;
|
|
|
}
|
|
@@ -169,7 +170,7 @@ int main(int argc, char* argv[]) {
|
|
|
//Parse les commandes
|
|
|
result = parse_all_command(&ct);
|
|
|
if(result == SHELL_FAIL){
|
|
|
- error.print("Impossible d'analyser la commande");
|
|
|
+ error.print("Impossible d'analyser la commande\n");
|
|
|
addserror("Erreur lors du parse des commandes");
|
|
|
continue;
|
|
|
}
|
|
@@ -213,20 +214,51 @@ int run(CommandTab ct){
|
|
|
}
|
|
|
//Sinon execution de chaque commande
|
|
|
Command* c;
|
|
|
- //int tmpfd;
|
|
|
+ int* tube[ct.length];
|
|
|
+ int tubepos = 0;
|
|
|
+ int infd = -1, outfd = -1, errfd = -1;
|
|
|
//Parcours les commandes
|
|
|
for(int i = 0; i < ct.length; i++){
|
|
|
c = ct.cmd[i];
|
|
|
- //Si il y a un pipe creation d'un fichier temporaire à la place de la sortie standard
|
|
|
- /*if(c->next == SHELL_PIPE){
|
|
|
- tmpfd = get_tmp_file();
|
|
|
- if(tmpfd == SHELL_ERR){
|
|
|
- exit(EXIT_FAILURE);
|
|
|
+ printf("Cmd : %s\n", c->name);
|
|
|
+ //Si pipe creation d'un fichier commun
|
|
|
+ if(c->next == SHELL_PIPE){
|
|
|
+ int tab[2];
|
|
|
+ tube[tubepos] = tab;
|
|
|
+ //Si il n'y a pas deja une redirection de sortie
|
|
|
+ if(c->output == STDOUT){
|
|
|
+ //Creation tube
|
|
|
+ if(pipe(tube[tubepos]) == ERR){
|
|
|
+ addperror("Impossible de créer un tube");
|
|
|
+ return SHELL_FAIL;
|
|
|
+ }
|
|
|
+ //Tube non bloquant
|
|
|
+ c->output = tube[tubepos][TUBE_ECRITURE];
|
|
|
+ //Redirige l'entrée du suivant
|
|
|
+ if(ct.cmd[i + 1]->input == STDIN){
|
|
|
+ ct.cmd[i + 1]->input = tube[tubepos][TUBE_LECTURE];
|
|
|
+ }
|
|
|
}
|
|
|
- tmpfd = redirect_fd(STDOUT, tmpfd);
|
|
|
- }*/
|
|
|
+ }
|
|
|
//Effectue les redirections IO
|
|
|
- /*ToDo*/
|
|
|
+ if(c->input != STDIN){
|
|
|
+ infd = redirect_fd(STDIN, c->input);
|
|
|
+ if(infd == ERR){
|
|
|
+ return SHELL_FAIL;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(c->output != STDOUT){
|
|
|
+ outfd = redirect_fd(STDOUT, c->output);
|
|
|
+ if(outfd == ERR){
|
|
|
+ return SHELL_FAIL;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(c->error != STDERR){
|
|
|
+ errfd = redirect_fd(STDERR, c->error);
|
|
|
+ if(errfd == ERR){
|
|
|
+ return SHELL_FAIL;
|
|
|
+ }
|
|
|
+ }
|
|
|
//Execute la commande
|
|
|
if(is_internal_cmd(c->name)){
|
|
|
result = launch_internal_command(c);
|
|
@@ -237,7 +269,32 @@ int run(CommandTab ct){
|
|
|
}
|
|
|
printf("%d\n", result);
|
|
|
//Reset IO
|
|
|
- /*ToDo*/
|
|
|
+ if(c->input != STDIN){
|
|
|
+ infd = redirect_fd(STDIN, infd);
|
|
|
+ if(infd == ERR){
|
|
|
+ return SHELL_FAIL;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(c->output != STDOUT){
|
|
|
+ outfd = redirect_fd(STDOUT, outfd);
|
|
|
+ if(outfd == ERR){
|
|
|
+ return SHELL_FAIL;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(c->error != STDERR){
|
|
|
+ errfd = redirect_fd(STDERR, errfd);
|
|
|
+ if(errfd == ERR){
|
|
|
+ return SHELL_FAIL;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //Fermeture tube
|
|
|
+ if(c->next == SHELL_PIPE){
|
|
|
+ if(close(tube[tubepos++][TUBE_ECRITURE]) == ERR){
|
|
|
+ addperror("Impossible de fermer tube ecriture");
|
|
|
+ return SHELL_FAIL;
|
|
|
+ }
|
|
|
+ c->output = STDOUT;
|
|
|
+ }
|
|
|
//Agit en fonction de la jointure avec la prochaine commande
|
|
|
/*ToDo*/
|
|
|
}
|