浏览代码

:sparkles: Debut redirection et pipe

Loquicom 6 年之前
父节点
当前提交
1eb994d639
共有 5 个文件被更改,包括 80 次插入17 次删除
  1. 0 1
      command.c
  2. 4 0
      constante.h
  3. 1 1
      error.c
  4. 70 13
      mysh.c
  5. 5 2
      str.c

+ 0 - 1
command.c

@@ -94,7 +94,6 @@ void clean_pid(pid_list* pl) {
 boolean is_internal_cmd(const char* cmd) {
     //Parcours tableau pour trouver correspondance
     for (int i = 0; cmdlist[i] != NULL; i++) {
-        printf("%s - %s | %ld\n", cmd, cmdlist[i], strlen(cmd));
         if (strncmp(cmd, cmdlist[i], strlen(cmd)) == 0) {
             return true;
         }

+ 4 - 0
constante.h

@@ -19,6 +19,10 @@
 #define STDOUT 1
 #define STDERR 2
 
+/* --- Tube --- */
+#define TUBE_LECTURE 0
+#define TUBE_ECRITURE 1
+
 /* --- Separateur commande --- */
 #define SHELL_END 0 //Aucune autre commande après
 #define SHELL_NONE 1 //Aucun lien entre les 2 commandes

+ 1 - 1
error.c

@@ -40,7 +40,7 @@ void end(){
     fprintf(stderr, "----- Fin log processus %d (temps en seconde : %ld) -----\n", getpid(), temps);
     //On remet la sortie standard des erreurs
     fd = redirect_fd(STDERR_FILENO, error.errfd);
-    if(error.errfd == ERR){
+    if(fd == ERR){
         adderror("Impossible de rediriger la sortie d'erreur standard");
         return;
     }

+ 70 - 13
mysh.c

@@ -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*/
     }

+ 5 - 2
str.c

@@ -20,12 +20,14 @@ char* ltrim(char* str, char mask){
     }
     //Si aucun espace au debut
     if(cmpt == 0){
-        res = malloc(sizeof(char) * strlen(str));
+        res = malloc(sizeof(char) * (strlen(str) + 1));
+        memset(res, 0, strlen(str) + 1);
         strcpy(res, str);
         return res;
     }
     //Sinon creation nouvelle chaine
     res = malloc(sizeof(char) * (strlen(str) - cmpt + 1));
+    memset(res, 0, cmpt + 1);
     for(int i = 0, j = cmpt; i < (strlen(str) - cmpt); i++, j++){
         res[i] = str[j];
     }
@@ -43,7 +45,8 @@ char* rtrim(char* str, char mask){
     }
     //Si aucun espace au debut
     if(cmpt == strlen(str) - 1){
-        res = malloc(sizeof(char) * strlen(str));
+        res = malloc(sizeof(char) * (strlen(str) + 1));
+        memset(res, 0, strlen(str) + 1);
         strcpy(res, str);
         return res;
     }