Loquicom 6 лет назад
Родитель
Сommit
504e8da190
5 измененных файлов с 98 добавлено и 65 удалено
  1. 1 1
      mysh.c
  2. 97 5
      parser.c
  3. 0 58
      str.c
  4. 0 1
      str.h
  5. 0 0
      yut

+ 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", c->bck);
     }
     //Supprime
     clean_command(&ct);

+ 97 - 5
parser.c

@@ -339,7 +339,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 +357,95 @@ 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);  
+    }
+    //Regarde si le caractère de fin est &
+    if(*cmd == '&'){
+        //Alors en fond
+        c->bck = true;
+    }
+    //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
@@ -390,14 +480,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 +514,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]);
             }
         }

+ 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);