Эх сурвалжийг харах

Merge pull request #2 from Loquicom/debug

Application correctif
Loquicom 6 жил өмнө
parent
commit
ba59180e53
5 өөрчлөгдсөн 95 нэмэгдсэн , 67 устгасан
  1. 1 1
      mysh.c
  2. 93 6
      parser.c
  3. 1 1
      parser.h
  4. 0 58
      str.c
  5. 0 1
      str.h

+ 1 - 1
mysh.c

@@ -39,7 +39,7 @@ int main(int argc, char* argv[]) {
         for(int j = 0; j < c->argc; j++){
             printf("    Argument %d : %s\n", j, c->argv[j]);
         }
-        printf("\n");
+        printf("Commande en fond : %d\n\n", ct.bck);
     }
     //Supprime
     clean_command(&ct);

+ 93 - 6
parser.c

@@ -114,7 +114,6 @@ int get_command(Command* c, char* line){
     c->error = STDERR;
     c->erase[0] = false;
     c->erase[1] = false;
-    c->bck = false;
     //Trim et supprime l'ancienne chaine
     old = c->cmd;
     c->cmd = rtrim(c->cmd, ' ');
@@ -339,7 +338,8 @@ int set_redirection(Command* c){
             buffer++;
         }
         //Allocation file et copie nom fichier
-        file = malloc(sizeof(char) * compteur);  
+        file = malloc(sizeof(char) * compteur);
+        memset(file, 0, compteur);
         strncpy(file, deb, compteur);
         //Redirection
         if(set_io(c, file, redir) == SHELL_ERR){
@@ -356,6 +356,90 @@ int set_redirection(Command* c){
     return finCmd;
 }
 
+int split_command(Command* c, char* cmd){
+    printf("%s\n", cmd);
+    //Declaration variable
+    char* deb = cmd;
+    int nb = 0, length = 0, i = 0;
+    char delim = ' ';
+    boolean chercheFin = false;
+    //Compte le nombre d'argument
+    while(*cmd != '\0' && *cmd != '&'){
+        //Cherche debut d'un mot
+        if(*cmd != ' ' && !chercheFin){
+            chercheFin = true;
+            //Guillemet
+            if(*cmd == '"'){
+                //Verif que ce n'est pas la fin
+                if(!(*(cmd + 1))){
+                    return SHELL_ERR;
+                }
+                cmd++;
+                delim = '"';
+            } 
+            //Mot sans guillemet autour
+            else {
+                length = 0;
+                delim = ' ';
+            }
+        }
+        //Fin d'un mot
+        else if(*cmd == delim && chercheFin){
+            //Adapte si il y avait des guillemet
+            chercheFin = false;
+            nb++;
+        }
+        //Incremente
+        cmd++;
+    }
+    //Si on termine sur un mot sans "
+    if(chercheFin){
+        nb++;
+    }
+    //Allocation + retour au debut
+    c->argv = malloc(sizeof(char*) * (nb + 1));
+    c->argc = nb;
+    cmd = deb;
+    //Parcours chaine et decoupe
+    while(*cmd != '\0' && *cmd != '&'){
+        //Cherche debut d'un mot
+        if(*cmd != ' ' && !chercheFin){
+            chercheFin = true;
+            //Guillemet
+            if(*cmd == '"'){
+                cmd++;
+                deb = cmd;
+                length = 0;
+                delim = '"';
+            } 
+            //Mot sans guillemet autour
+            else {
+                deb = cmd;
+                length = 0;
+                delim = ' ';
+            }
+        }
+        //Fin d'un mot
+        else if(*cmd == delim && chercheFin){
+            chercheFin = false;
+            //Recup le mot
+            c->argv[i] = malloc(sizeof(char) * length);
+            strncpy(c->argv[i++], deb, length);
+        }
+        //Incremente
+        cmd++;
+        length++;
+    }
+    //Recup le dernier mot si besoin
+    if(chercheFin){
+        c->argv[i] = malloc(sizeof(char) * length);
+        strncpy(c->argv[i++], deb, length);  
+    }
+    //Set la dernière case du tableau à null
+    c->argv[i] = NULL;
+    return SHELL_OK;
+}
+
 /* --- Fonctions publiques --- */
 int parse_line(CommandTab* ct, char* line){
     //Declaration variable
@@ -370,6 +454,7 @@ int parse_line(CommandTab* ct, char* line){
     //Initialisation structure
     ct->cmd = malloc(sizeof(Command*) * compteur);
     ct->length = compteur;
+    ct->bck = line[strlen(line) - 1] == '&';
     //Recupération de chaque commande
     for(int i = 0; i < compteur; i++){
         ct->cmd[i] = malloc(sizeof(Command));
@@ -390,14 +475,14 @@ int parse_command(Command* c){
     char* cmd;
     //Parse les redirections
     length = set_redirection(c);
-    if(length == SHELL_ERR){
+    if(length == SHELL_ERR || length == 0){
         return SHELL_ERR;
     }
     //Recup la commande (sans redirection)
-    cmd = malloc(length);
+    cmd = malloc(sizeof(char) * length);
     strncpy(cmd, c->cmd, length);
-    //Split en un tableau
-    c->argv = str_split(cmd, ' ', &c->argc); //ToDo changer par un methode de split qui prend en compte les " " et les \espaces
+    //Split la commande
+    split_command(c, cmd);
     c->name = c->argv[0];
     //Analyse wildcard
     /* Todo */
@@ -424,6 +509,8 @@ void clean_command(CommandTab* ct){
         if(ct->cmd[i]->argc > 0){
             ct->cmd[i]->name = NULL;
             for(int j = 0; j < ct->cmd[i]->argc; j++){
+                printf("free argv %d : %s\n", j, ct->cmd[i]->argv[j]);
+                fflush(stdout);
                 free(ct->cmd[i]->argv[j]);
             }
         }

+ 1 - 1
parser.h

@@ -22,13 +22,13 @@ typedef struct{
     int output; //Descripteur de fichier de sortie
     int error; //Descripteur de fihier d'erreur
     boolean erase[2]; //Si on efface le fichier
-    boolean bck; //En fond ou non
     int next; //Lien avec la prochaine commande
 }Command;
 
 typedef struct{
     Command** cmd; //Tableau avec toutes les commandes
     int length; //Taille du tableau
+    boolean bck; //Si l'ensemble de commande se fait en fond
 }CommandTab;
 
 /* --- Fonctions --- */

+ 0 - 58
str.c

@@ -2,64 +2,6 @@
 #include <stdlib.h>
 #include "str.h"
 
-char** str_split(char* str, const char delim, int* length) {
-    char** res;
-    char* signet, * tmp = str;
-    char last = 0;
-    int compteur = 0, nb = 1, taille, count = 0;
-    //Compte le nombre d'element
-    while (*tmp) {
-        if (*tmp == delim && last != delim) {
-            nb++;
-        }
-        last = *tmp;
-        tmp++;
-    }
-    //Creation du tableau
-    res = malloc(sizeof (char*) * (nb + 1));
-    //Decoupage
-    tmp = str;
-    while (*tmp) {
-        //Si c'est le dernier mot
-        if (compteur == nb - 1) {
-            //Ajoute tous ce qui reste
-            res[compteur] = malloc(sizeof (char) * (strlen(str) - count));
-            int i = 0;
-            while(*tmp){
-                res[compteur][i++] = *tmp;
-                tmp++;
-            }
-        } else {
-            //Recup la taille du mot
-            signet = tmp;
-            taille = 0;
-            while (*tmp != delim) {
-                taille++;
-                tmp++;
-                count++;
-            }
-            //Creation du mot
-            res[compteur] = malloc(sizeof (char) * taille);
-            //Ajout du mot
-            for (int i = 0; i < taille; i++) {
-                res[compteur][i] = *signet;
-                signet++;
-            }
-            compteur++;
-            //Passe les delimiteurs consecutif
-            while (*tmp == delim) {
-                tmp++;
-                count++;
-            }
-        }
-    }
-    //Ajoute NULL à la fin
-    res[nb + 1] = NULL;
-    //Retour nombre de mot et tableau
-    *length = nb;
-    return res;
-}
-
 char* trim(char* str){
     return ltrim(rtrim(str, ' '), ' ');
 }

+ 0 - 1
str.h

@@ -3,7 +3,6 @@
 
 #include <string.h>
 
-char** str_split(char*, const char, int*);
 char* trim(char*);
 char* mtrim(char*, char);
 char* ltrim(char*, char);