ソースを参照

:sparkles: Parse des wildcards

Loquicom 6 年 前
コミット
5f10756273
3 ファイル変更67 行追加18 行削除
  1. 26 3
      parser.c
  2. 38 15
      wildcard.c
  3. 3 0
      wildcard.h

+ 26 - 3
parser.c

@@ -474,8 +474,8 @@ int parse_line(CommandTab* ct, char* line){
 
 int parse_command(Command* c){
     //Declaration variable
-    int length;
-    char* cmd;
+    int length, nbWildcard = 0, res;
+    char* cmd, **wildcardTab;
     //Parse les redirections
     length = set_redirection(c);
     if(length == SHELL_ERR || length == 0){
@@ -489,7 +489,30 @@ int parse_command(Command* c){
     split_command(c, cmd);
     c->name = c->argv[0];
     //Analyse wildcard
-    /* Todo */
+    for(int i = 0; i < c->argc; i++){
+        //Regarde si il faut remplacer l'argument par une suite de fichier
+        nbWildcard = wildcard_result(c->argv[i]);
+        printf("NbWildcard %s [%d] : %d\n", c->argv[i], i, nbWildcard);
+        if(nbWildcard > 0){
+            //Si il y a des resultats on prepare un tableau pour les récupérer
+            wildcardTab = malloc(sizeof(char*) * nbWildcard);
+            nbWildcard = wildcard(c->argv[i], nbWildcard, wildcardTab);
+            printf("NbWildcard 2 %s [%d] : %d\n", c->argv[i], i, nbWildcard);
+            for(int j = 0; j < nbWildcard; j++){
+                printf("Fichier : %s\n", wildcardTab[j]);
+            }
+            //Verif retour
+            if(nbWildcard == ERR){
+                return SHELL_ERR;
+            }
+            //Ajoute les wildcard dans argv (le +1 est la pour garder le NULL à la fin)
+            c->argv = insert_array(i, c->argv, c->argc + 1, wildcardTab, nbWildcard, &res);
+            if(res == ERR){
+                return SHELL_ERR;
+            }
+            c->argc = res - 1;  //On ne compte pas le NULL final
+        }
+    }
     //Ici tous est ok
     return SHELL_OK;
 }

+ 38 - 15
wildcard.c

@@ -24,8 +24,8 @@ int wildcard_result(const char* seq) {
     //Recup la liste des fichiers dans le dossier courant
     nbFile = scandir(".", &namelist, 0, alphasort);
     if (nbFile < 0) {
-        perror("scandir");
-        return -1;
+        perror("scandir() : ");
+        return ERR;
     }
     //Parcours chaque fichier pour compter le nombre de resultat
     while (nbFile--) {
@@ -47,19 +47,20 @@ int wildcard_result(const char* seq) {
 }
 
 //Sequence + taille du tableau result + tableau de retour vide
+
 int wildcard(const char* seq, int size, char** result) {
     //Declaration variable
     struct dirent **namelist;
     int nbFile, nbRes = 0;
     //Verification parametre
     if (size < 1) {
-        return -1;
+        return ERR;
     }
     //Recup la liste des fichiers dans le dossier courant
     nbFile = scandir(".", &namelist, 0, alphasort);
     if (nbFile < 0) {
-        perror("scandir");
-        return -1;
+        perror("scandir() : ");
+        return ERR;
     }
     //Parcours chaque fichier pour ajouter les resultats à result
     int i = 0;
@@ -86,11 +87,12 @@ int wildcard(const char* seq, int size, char** result) {
 }
 
 //La position de la valeur à remplacer, le tableau dans lequel il faut remplacer, sa taille, le tableau à insérer, sa taille, la nouvelle taille
+
 char** insert_array(int pos, char** array, int arraysize, char** insert, int insertsize, int* newsize) {
     //Erreur parametre
     if (pos < 0 || arraysize < 1 || insertsize < 1 || pos >= arraysize) {
         if (newsize != NULL) {
-            *newsize = -1;
+            *newsize = ERR;
         }
         return NULL;
     }
@@ -116,21 +118,42 @@ char** insert_array(int pos, char** array, int arraysize, char** insert, int ins
     newarray = malloc(sizeof (char*) * size);
     //Ajout des elements avant la postion
     for (i = 0; i < pos; i++) {
-        newarray[i] = malloc(strlen(array[i]) * sizeof (char));
-        strcpy(newarray[i], array[i]);
-        free(array[i]);
+        //Si l'element est null
+        if (array[i] == NULL) {
+            newarray[i] = NULL;
+        }            
+        //Sinon on le copie
+        else {
+            newarray[i] = malloc(strlen(array[i]) * sizeof (char));
+            strcpy(newarray[i], array[i]);
+            free(array[i]);
+        }
     }
     //Ajout des nouveaux elements
     for (int j = 0; j < insertsize; j++, i++) {
-        newarray[i] = malloc(strlen(insert[j]) * sizeof (char));
-        strcpy(newarray[i], insert[j]);
-        free(insert[j]);
+        //Si l'element est null
+        if (insert[j] == NULL) {
+            newarray[i] = NULL;
+        }            
+        //Sinon on le copie
+        else {
+            newarray[i] = malloc(strlen(insert[j]) * sizeof (char));
+            strcpy(newarray[i], insert[j]);
+            free(insert[j]);
+        }
     }
     //Ajout fin
     for (int j = pos + 1; j < arraysize; j++, i++) {
-        newarray[i] = malloc(strlen(array[j]) * sizeof (char));
-        strcpy(newarray[i], array[j]);
-        free(array[j]);
+        //Si l'element est null
+        if (array[j] == NULL) {
+            newarray[i] = NULL;
+        }            
+        //Sinon on le copie
+        else {
+            newarray[i] = malloc(strlen(array[j]) * sizeof (char));
+            strcpy(newarray[i], array[j]);
+            free(array[j]);
+        }
     }
     //Nettoyage et changement tableau
     free(array[pos]);

+ 3 - 0
wildcard.h

@@ -8,6 +8,9 @@
 #ifndef WILDCARD_H
 #define WILDCARD_H
 
+/* --- Include --- */
+#include "constante.h"
+
 /* --- Fonctions publiques ---*/
 int wildcard_result(const char*);
 int wildcard(const char*, int, char**);