Kaynağa Gözat

Merge pull request #8 from Loquicom/debug

Debug
Loquicom 6 yıl önce
ebeveyn
işleme
0aabb0af52
1 değiştirilmiş dosya ile 14 ekleme ve 11 silme
  1. 14 11
      parser.c

+ 14 - 11
parser.c

@@ -67,7 +67,7 @@ int nb_commands(char* line){
  */
 int get_command(Command* c, char* line){
     //Declaration variable
-    char* deb = line, * old;
+    char* deb = line;
     int length = 0, separator = 0, next = SHELL_NONE;
     //Parcours chaine pour chercher un séparateur
     while(*line){
@@ -106,6 +106,7 @@ int get_command(Command* c, char* line){
     }
     //Allocation memoire et copie chaine
     c->cmd = malloc(sizeof(char) * (length + 1));
+    memset(c->cmd, 0, length + 1);
     strncpy(c->cmd, deb, length);
     c->next = next;
     c->argc = 0;
@@ -114,10 +115,6 @@ int get_command(Command* c, char* line){
     c->error = STDERR;
     c->erase[0] = false;
     c->erase[1] = false;
-    //Trim et supprime l'ancienne chaine
-    old = c->cmd;
-    c->cmd = rtrim(c->cmd, ' ');
-    free(old);
     //Retour
     return length + separator;
 }
@@ -353,11 +350,10 @@ int set_redirection(Command* c){
         }
     }
     //Si on arrive ici tous est ok
-    return finCmd;
+    return finCmd + 1;
 }
 
 int split_command(Command* c, char* cmd){
-    printf("%s\n", cmd);
     //Declaration variable
     char* deb = cmd;
     int nb = 0, length = 0, i = 0;
@@ -423,7 +419,8 @@ int split_command(Command* c, char* cmd){
         else if(*cmd == delim && chercheFin){
             chercheFin = false;
             //Recup le mot
-            c->argv[i] = malloc(sizeof(char) * length);
+            c->argv[i] = malloc(sizeof(char) * (length + 1));
+            memset(c->argv[i], 0, length + 1);
             strncpy(c->argv[i++], deb, length);
         }
         //Incremente
@@ -432,7 +429,8 @@ int split_command(Command* c, char* cmd){
     }
     //Recup le dernier mot si besoin
     if(chercheFin){
-        c->argv[i] = malloc(sizeof(char) * length);
+        c->argv[i] = malloc(sizeof(char) * (length + 1));
+        memset(c->argv[i], 0, length + 1);
         strncpy(c->argv[i++], deb, length);  
     }
     //Set la dernière case du tableau à null
@@ -455,6 +453,10 @@ int parse_line(CommandTab* ct, char* line){
     ct->cmd = malloc(sizeof(Command*) * compteur);
     ct->length = compteur;
     ct->bck = line[strlen(line) - 1] == '&';
+    //Si il y a un un & on le retire (on à deja l'information)
+    if(ct->bck){
+        line[strlen(line) - 1] = '\0';
+    }
     //Recupération de chaque commande
     for(int i = 0; i < compteur; i++){
         ct->cmd[i] = malloc(sizeof(Command));
@@ -479,7 +481,8 @@ int parse_command(Command* c){
         return SHELL_ERR;
     }
     //Recup la commande (sans redirection)
-    cmd = malloc(sizeof(char) * length);
+    cmd = malloc(sizeof(char) * (length + 1));
+    memset(cmd, 0, length + 1);
     strncpy(cmd, c->cmd, length);
     //Split la commande
     split_command(c, cmd);
@@ -523,7 +526,7 @@ void clean_command(CommandTab* ct){
                 fprintf(stderr, "Erreur lors de la fermeture du fichier d'output de %s : %s\n", ct->cmd[i]->cmd, strerror(errno));
             }
         }
-        if(ct->cmd[i]->error != STDERR){
+        if(ct->cmd[i]->error != STDERR && ct->cmd[i]->error != ct->cmd[i]->output){ //Verif en plus qu'il est different du fichier de sortie standard (>& et >>&)
             if(close(ct->cmd[i]->error)){
                 fprintf(stderr, "Erreur lors de la fermeture du fichier d'error de %s : %s\n", ct->cmd[i]->cmd, strerror(errno));
             }