From 83f695c53887355d6c7e5c26946ac49b2fa4211d Mon Sep 17 00:00:00 2001 From: Aethor Date: Mon, 15 May 2017 19:10:46 +0200 Subject: [PATCH 01/11] Black mage of regex stikes again --- fish_shell/fish_globbing.c | 39 +++++++++++++++++++++++++++++--------- fish_shell/fish_globbing.h | 2 ++ fish_shell/fish_utils.c | 18 +++++++++++++++++- fish_shell/fish_utils.h | 2 ++ 4 files changed, 51 insertions(+), 10 deletions(-) diff --git a/fish_shell/fish_globbing.c b/fish_shell/fish_globbing.c index 1ca078c..e764635 100644 --- a/fish_shell/fish_globbing.c +++ b/fish_shell/fish_globbing.c @@ -7,14 +7,7 @@ WordList * fishExpand(WordList *wordArray) { - int i; - //WordArray* splitParameter; - - for(i=1; isize; i++){ - - - - } + wordArray = expandInDir("./", "*"); return wordArray; @@ -47,8 +40,15 @@ WordArray * getFiles(char* path){ while((dir = readdir(directory)) != NULL){ - files->words[i] = dir->d_name; + /*if(dir->d_name != "." && dir->d_name != ".."){*/ + printf("%s\n", dir->d_name); + files->words[i] = dir->d_name; + i++; + files->size++; + + //} + } @@ -120,4 +120,25 @@ int comparator(char* string1, char* string2){//TODO } +WordList* expandInDir(char* dir, char* toExpand){ + + int i = 0; + WordList* list = createWordList(); + WordArray* files = getFiles(dir); + + for(i=0; isize;i++){ + + if(comparator(toExpand, files->words[i])){ + + addWordList(list, files->words[i]); + + } + + } + + + return list; + +} + diff --git a/fish_shell/fish_globbing.h b/fish_shell/fish_globbing.h index 0d76d04..2d635bf 100644 --- a/fish_shell/fish_globbing.h +++ b/fish_shell/fish_globbing.h @@ -11,4 +11,6 @@ WordArray* getFiles(char* path); /*char1 is a string with characters such as '*', '.' or '?' having special meanings*/ int comparator(char* string1, char* string2); +WordList* expandInDir(char*, char*); + #endif //FISH_FISH_GLOBBING_H diff --git a/fish_shell/fish_utils.c b/fish_shell/fish_utils.c index a018a8f..e07251f 100644 --- a/fish_shell/fish_utils.c +++ b/fish_shell/fish_utils.c @@ -30,7 +30,7 @@ char *getInsult(){ char *insults[] = { "Apprend à écrire crétin !", "Bolos !", - "Mois aussi je sais écrire de la merde, pourtant je le fait pas !" + "Moi aussi je sais écrire de la merde, pourtant je le fait pas !" }; if (!init){ srand((unsigned int) time(NULL)); @@ -103,6 +103,22 @@ void freeWordList(WordList *list) { free(list); } +WordList* concatWordList(WordList* list1, WordList* list2){ + + if(list1->last != NULL){ + list1->last->next = list2->first; + list2->first->previous = list1->last; + list1->last = list2->last; + list1->size = list1->size + list2->size; + free(list2); + return list1; + } + else{ + return NULL; + } + +} + WordArray *wordListToWordArray(WordList *list) { WordArray *array = (WordArray*) malloc(sizeof(WordArray)); WordListElement *current = list->first; diff --git a/fish_shell/fish_utils.h b/fish_shell/fish_utils.h index 70e3725..d94dffa 100644 --- a/fish_shell/fish_utils.h +++ b/fish_shell/fish_utils.h @@ -25,4 +25,6 @@ WordArray * wordListToWordArray(WordList *list); WordList * wordArrayToWordList(WordArray * array); +WordList* concatWordList(WordList* list1, WordList* list2); + #endif //FISH_FISH_UTILS_H From c1e13ab7c869b2512fac4a2cfbade859ec8cf0b9 Mon Sep 17 00:00:00 2001 From: Aethor Date: Tue, 16 May 2017 07:32:19 +0200 Subject: [PATCH 02/11] =?UTF-8?q?=C3=A7a=20core=20dump=20quelque=20peu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fish_shell/fish_core.c | 9 +++++-- fish_shell/fish_globbing.c | 52 ++++++++++++++++++++++++++++++++++---- fish_shell/fish_globbing.h | 2 ++ fish_shell/fish_utils.c | 19 +++++++++++++- fish_shell/fish_utils.h | 2 ++ 5 files changed, 76 insertions(+), 8 deletions(-) diff --git a/fish_shell/fish_core.c b/fish_shell/fish_core.c index 43e4d18..3befad4 100644 --- a/fish_shell/fish_core.c +++ b/fish_shell/fish_core.c @@ -158,8 +158,13 @@ int fishExecute(WordArray *array) { return 1; for (i=0; i < getNbBuiltins(); i++){ - if (!strcmp(array->words[0], getBuiltinCommandsStr()[i])){ - return getBuiltinCommands()[i](array); + if(array->words[0] != NULL){ + if (!strcmp(array->words[0], getBuiltinCommandsStr()[i])){ + return getBuiltinCommands()[i](array); + } + } + else{ + crash(); } } diff --git a/fish_shell/fish_globbing.c b/fish_shell/fish_globbing.c index e764635..45bf4d3 100644 --- a/fish_shell/fish_globbing.c +++ b/fish_shell/fish_globbing.c @@ -2,18 +2,59 @@ #include #include #include +#include #include "fish_core.h" #include "fish_globbing.h" -WordList * fishExpand(WordList *wordArray) { +WordList * fishExpand(WordList *wordList) { - wordArray = expandInDir("./", "*"); + if(wordList->size > 1){ + int i; - return wordArray; + WordList* newWordList = createWordList(); + if(newWordList == NULL){ + crash(); + } + + newWordList->first = wordList->first; + + newWordList->size = 0; + + WordListElement* tempElement = wordList->first->next; + + for(i=0; isize; i++){ + + //newWordList = concatWordList(newWordList, recursiveExpand(tempElement->word)); + //newWordList = concatWordList(newWordList, NULL); + + if(tempElement != NULL){ + + tempElement = tempElement->next; + + } + } + + return newWordList; + + } + + else return wordList; } +WordList* recursiveExpand(char * completePath){ + + if (stringContains(completePath, '/')){ + + return expandInDir("./",completePath); + + } + + return NULL; + +} + WordArray * getFiles(char* path){ @@ -21,6 +62,7 @@ WordArray * getFiles(char* path){ dirent* dir; int i = 0; + WordArray* files = (WordArray*) malloc(sizeof(WordArray)); @@ -40,14 +82,14 @@ WordArray * getFiles(char* path){ while((dir = readdir(directory)) != NULL){ - /*if(dir->d_name != "." && dir->d_name != ".."){*/ + if(!strcmp(dir->d_name, ".") && !strcmp(dir->d_name, "..")){ printf("%s\n", dir->d_name); files->words[i] = dir->d_name; i++; files->size++; - //} + } } diff --git a/fish_shell/fish_globbing.h b/fish_shell/fish_globbing.h index 2d635bf..8438d2e 100644 --- a/fish_shell/fish_globbing.h +++ b/fish_shell/fish_globbing.h @@ -13,4 +13,6 @@ int comparator(char* string1, char* string2); WordList* expandInDir(char*, char*); +WordList* recursiveExpand(char* completePath); + #endif //FISH_FISH_GLOBBING_H diff --git a/fish_shell/fish_utils.c b/fish_shell/fish_utils.c index e07251f..b2c8e13 100644 --- a/fish_shell/fish_utils.c +++ b/fish_shell/fish_utils.c @@ -105,7 +105,8 @@ void freeWordList(WordList *list) { WordList* concatWordList(WordList* list1, WordList* list2){ - if(list1->last != NULL){ + + if(list1 != NULL && list2 != NULL && list1->size >= 1 && list2->size >=1){ list1->last->next = list2->first; list2->first->previous = list1->last; list1->last = list2->last; @@ -155,3 +156,19 @@ WordList *wordArrayToWordList(WordArray *array) { return list; } + +int stringContains(char * string, char charToTest){ + + int i = 0; + + while(string[i] != '\0'){ + + if(string[i] != charToTest){ + return 0; + } + i++; + } + + return 1; + +} diff --git a/fish_shell/fish_utils.h b/fish_shell/fish_utils.h index d94dffa..ca0b653 100644 --- a/fish_shell/fish_utils.h +++ b/fish_shell/fish_utils.h @@ -27,4 +27,6 @@ WordList * wordArrayToWordList(WordArray * array); WordList* concatWordList(WordList* list1, WordList* list2); +int stringContains(char* string, char charToTest); + #endif //FISH_FISH_UTILS_H From c05b1fe39833206f3532103f1c94f70380890a77 Mon Sep 17 00:00:00 2001 From: Aethor Date: Mon, 15 May 2017 19:10:46 +0200 Subject: [PATCH 03/11] Black mage of regex stikes again --- fish_shell/fish_globbing.c | 39 +++++++++++++++++++++++++++++--------- fish_shell/fish_globbing.h | 2 ++ fish_shell/fish_utils.c | 22 +++++++++++++++++++++ fish_shell/fish_utils.h | 2 ++ 4 files changed, 56 insertions(+), 9 deletions(-) diff --git a/fish_shell/fish_globbing.c b/fish_shell/fish_globbing.c index 1ca078c..e764635 100644 --- a/fish_shell/fish_globbing.c +++ b/fish_shell/fish_globbing.c @@ -7,14 +7,7 @@ WordList * fishExpand(WordList *wordArray) { - int i; - //WordArray* splitParameter; - - for(i=1; isize; i++){ - - - - } + wordArray = expandInDir("./", "*"); return wordArray; @@ -47,8 +40,15 @@ WordArray * getFiles(char* path){ while((dir = readdir(directory)) != NULL){ - files->words[i] = dir->d_name; + /*if(dir->d_name != "." && dir->d_name != ".."){*/ + printf("%s\n", dir->d_name); + files->words[i] = dir->d_name; + i++; + files->size++; + + //} + } @@ -120,4 +120,25 @@ int comparator(char* string1, char* string2){//TODO } +WordList* expandInDir(char* dir, char* toExpand){ + + int i = 0; + WordList* list = createWordList(); + WordArray* files = getFiles(dir); + + for(i=0; isize;i++){ + + if(comparator(toExpand, files->words[i])){ + + addWordList(list, files->words[i]); + + } + + } + + + return list; + +} + diff --git a/fish_shell/fish_globbing.h b/fish_shell/fish_globbing.h index 0d76d04..2d635bf 100644 --- a/fish_shell/fish_globbing.h +++ b/fish_shell/fish_globbing.h @@ -11,4 +11,6 @@ WordArray* getFiles(char* path); /*char1 is a string with characters such as '*', '.' or '?' having special meanings*/ int comparator(char* string1, char* string2); +WordList* expandInDir(char*, char*); + #endif //FISH_FISH_GLOBBING_H diff --git a/fish_shell/fish_utils.c b/fish_shell/fish_utils.c index 4649377..e077fa9 100644 --- a/fish_shell/fish_utils.c +++ b/fish_shell/fish_utils.c @@ -30,6 +30,7 @@ char *getInsult(){ static int init = 0; int picked = 0; char *insults[] = { +<<<<<<< HEAD (char *) "Apprend à écrire crétin !", (char *) "Boloss !", (char *) "Mois aussi je sais écrire de la merde, pourtant je le fait pas !", @@ -37,6 +38,11 @@ char *getInsult(){ (char *) "Nul !", (char *) "Pense à aller à l'école un jour", (char *) "Et après on dit que c'est la faute de l'ordinateur..." +======= + "Apprend à écrire crétin !", + "Bolos !", + "Moi aussi je sais écrire de la merde, pourtant je le fait pas !" +>>>>>>> Black mage of regex stikes again }; if (!init){ srand((unsigned int) time(NULL)); @@ -117,6 +123,22 @@ void freeWordList(WordList *list) { free(list); } +WordList* concatWordList(WordList* list1, WordList* list2){ + + if(list1->last != NULL){ + list1->last->next = list2->first; + list2->first->previous = list1->last; + list1->last = list2->last; + list1->size = list1->size + list2->size; + free(list2); + return list1; + } + else{ + return NULL; + } + +} + WordArray *wordListToWordArray(WordList *list) { WordArray *array = (WordArray*) malloc(sizeof(WordArray)); WordListElement *current = list->first; diff --git a/fish_shell/fish_utils.h b/fish_shell/fish_utils.h index b6ed440..e372f4e 100644 --- a/fish_shell/fish_utils.h +++ b/fish_shell/fish_utils.h @@ -35,4 +35,6 @@ WordList * splitWordList(WordList *list, char *regex); char * splitWord(char * origin, int beginning_index, int size_to_delete); // Tested +WordList* concatWordList(WordList* list1, WordList* list2); + #endif //FISH_FISH_UTILS_H From 7d87fdb235561bf21eb1a2d1672e209891b83a81 Mon Sep 17 00:00:00 2001 From: Aethor Date: Tue, 16 May 2017 07:32:19 +0200 Subject: [PATCH 04/11] =?UTF-8?q?=C3=A7a=20core=20dump=20quelque=20peu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fish_shell/fish_core.c | 15 ++++++++--- fish_shell/fish_globbing.c | 52 ++++++++++++++++++++++++++++++++++---- fish_shell/fish_globbing.h | 2 ++ fish_shell/fish_utils.c | 24 +++++++++++++----- fish_shell/fish_utils.h | 2 ++ 5 files changed, 79 insertions(+), 16 deletions(-) diff --git a/fish_shell/fish_core.c b/fish_shell/fish_core.c index b135254..2c013ee 100644 --- a/fish_shell/fish_core.c +++ b/fish_shell/fish_core.c @@ -194,10 +194,17 @@ int fishExecute(WordList *list) { int loadRightCommand(WordArray *array){ int i; - if (array->size <= 0) return 1; - for (i = 0; i < getNbBuiltins(); i++) { - if (!strcmp(array->words[0], getBuiltinCommandsStr()[i])) { - return getBuiltinCommands()[i](array); + if (array->size < 0) + return 1; + + for (i=0; i < getNbBuiltins(); i++){ + if(array->words[0] != NULL){ + if (!strcmp(array->words[0], getBuiltinCommandsStr()[i])){ + return getBuiltinCommands()[i](array); + } + } + else{ + crash(); } } return fishLoad(array); diff --git a/fish_shell/fish_globbing.c b/fish_shell/fish_globbing.c index e764635..45bf4d3 100644 --- a/fish_shell/fish_globbing.c +++ b/fish_shell/fish_globbing.c @@ -2,18 +2,59 @@ #include #include #include +#include #include "fish_core.h" #include "fish_globbing.h" -WordList * fishExpand(WordList *wordArray) { +WordList * fishExpand(WordList *wordList) { - wordArray = expandInDir("./", "*"); + if(wordList->size > 1){ + int i; - return wordArray; + WordList* newWordList = createWordList(); + if(newWordList == NULL){ + crash(); + } + + newWordList->first = wordList->first; + + newWordList->size = 0; + + WordListElement* tempElement = wordList->first->next; + + for(i=0; isize; i++){ + + //newWordList = concatWordList(newWordList, recursiveExpand(tempElement->word)); + //newWordList = concatWordList(newWordList, NULL); + + if(tempElement != NULL){ + + tempElement = tempElement->next; + + } + } + + return newWordList; + + } + + else return wordList; } +WordList* recursiveExpand(char * completePath){ + + if (stringContains(completePath, '/')){ + + return expandInDir("./",completePath); + + } + + return NULL; + +} + WordArray * getFiles(char* path){ @@ -21,6 +62,7 @@ WordArray * getFiles(char* path){ dirent* dir; int i = 0; + WordArray* files = (WordArray*) malloc(sizeof(WordArray)); @@ -40,14 +82,14 @@ WordArray * getFiles(char* path){ while((dir = readdir(directory)) != NULL){ - /*if(dir->d_name != "." && dir->d_name != ".."){*/ + if(!strcmp(dir->d_name, ".") && !strcmp(dir->d_name, "..")){ printf("%s\n", dir->d_name); files->words[i] = dir->d_name; i++; files->size++; - //} + } } diff --git a/fish_shell/fish_globbing.h b/fish_shell/fish_globbing.h index 2d635bf..8438d2e 100644 --- a/fish_shell/fish_globbing.h +++ b/fish_shell/fish_globbing.h @@ -13,4 +13,6 @@ int comparator(char* string1, char* string2); WordList* expandInDir(char*, char*); +WordList* recursiveExpand(char* completePath); + #endif //FISH_FISH_GLOBBING_H diff --git a/fish_shell/fish_utils.c b/fish_shell/fish_utils.c index e077fa9..1f6c6ea 100644 --- a/fish_shell/fish_utils.c +++ b/fish_shell/fish_utils.c @@ -30,7 +30,6 @@ char *getInsult(){ static int init = 0; int picked = 0; char *insults[] = { -<<<<<<< HEAD (char *) "Apprend à écrire crétin !", (char *) "Boloss !", (char *) "Mois aussi je sais écrire de la merde, pourtant je le fait pas !", @@ -38,11 +37,6 @@ char *getInsult(){ (char *) "Nul !", (char *) "Pense à aller à l'école un jour", (char *) "Et après on dit que c'est la faute de l'ordinateur..." -======= - "Apprend à écrire crétin !", - "Bolos !", - "Moi aussi je sais écrire de la merde, pourtant je le fait pas !" ->>>>>>> Black mage of regex stikes again }; if (!init){ srand((unsigned int) time(NULL)); @@ -125,7 +119,8 @@ void freeWordList(WordList *list) { WordList* concatWordList(WordList* list1, WordList* list2){ - if(list1->last != NULL){ + + if(list1 != NULL && list2 != NULL && list1->size >= 1 && list2->size >=1){ list1->last->next = list2->first; list2->first->previous = list1->last; list1->last = list2->last; @@ -285,3 +280,18 @@ WordList *splitWordList(WordList *list, char *regex) { return new_list; } +int stringContains(char * string, char charToTest){ + + int i = 0; + + while(string[i] != '\0'){ + + if(string[i] != charToTest){ + return 0; + } + i++; + } + + return 1; + +} diff --git a/fish_shell/fish_utils.h b/fish_shell/fish_utils.h index e372f4e..80dfd5f 100644 --- a/fish_shell/fish_utils.h +++ b/fish_shell/fish_utils.h @@ -37,4 +37,6 @@ char * splitWord(char * origin, int beginning_index, int size_to_delete); // Tes WordList* concatWordList(WordList* list1, WordList* list2); +int stringContains(char* string, char charToTest); + #endif //FISH_FISH_UTILS_H From 2a7dad2662b71c8031c9c7bcf65f10d3e68de4eb Mon Sep 17 00:00:00 2001 From: Aethor Date: Sat, 27 May 2017 17:36:55 +0200 Subject: [PATCH 05/11] clean and safe structure for globbing --- fish_shell/fish_globbing.c | 141 ++++++++----------------------------- fish_shell/fish_globbing.h | 7 +- fish_shell/fish_utils.c | 38 ++++++---- fish_shell/fish_utils.h | 4 +- 4 files changed, 56 insertions(+), 134 deletions(-) diff --git a/fish_shell/fish_globbing.c b/fish_shell/fish_globbing.c index 45bf4d3..2964ebd 100644 --- a/fish_shell/fish_globbing.c +++ b/fish_shell/fish_globbing.c @@ -9,51 +9,56 @@ WordList * fishExpand(WordList *wordList) { if(wordList->size > 1){ + int i; + WordList* newWordList = createWordList();// creating the list to return - WordList* newWordList = createWordList(); - - if(newWordList == NULL){ + if(newWordList == NULL){//crash when the allocation is unsuccessful crash(); - } + } - newWordList->first = wordList->first; + addEndWordList(newWordList, wordList->first->word);//copy the command into the returning word list - newWordList->size = 0; - WordListElement* tempElement = wordList->first->next; + WordListElement* tempElement = wordList->first->next; //temporary nav element - for(i=0; isize; i++){ + for(i=1; isize; i++){ - //newWordList = concatWordList(newWordList, recursiveExpand(tempElement->word)); - //newWordList = concatWordList(newWordList, NULL); + //TODO : optimize the stringContains() function to test for a list of characters + //test if we have to expand a string or not, for optimization purposes + if(stringContains(tempElement->word, '*') || stringContains(tempElement->word, '?')){ - if(tempElement != NULL){ + concatWordList(newWordList, expandWord(tempElement->word)); - tempElement = tempElement->next; + } + //If we dont have to expand, add the current word unchanged to the new list + else{ + addEndWordList(newWordList, tempElement->word); + } - } - } + tempElement = tempElement->next; + } + + freeWordList(wordList); return newWordList; - + } else return wordList; + + +} + +WordList* expandWord(char* word){ + + WordList* wordList = createWordList(); + addEndWordList(wordList, word); + return wordList; + } -WordList* recursiveExpand(char * completePath){ - - if (stringContains(completePath, '/')){ - - return expandInDir("./",completePath); - - } - - return NULL; - -} WordArray * getFiles(char* path){ @@ -100,87 +105,3 @@ WordArray * getFiles(char* path){ } -int comparator(char* string1, char* string2){//TODO - - int i = 0; - char tempIChar; - int j = 0; - - if(string1 != NULL && string2 != NULL){ - - while(string1[i] != '\0' && string2[j] != '\0'){ - - if(string1[i] == '*'){ - - tempIChar = string1[i+1]; - - while(string2[j] != tempIChar){ - - j++; - - if(string2[j] == '\0' && tempIChar == '\0'){ - return 1; - } - - } - i++; - - } - - if(string1[i] != string2[j] && string1[i] != '?'){ - - return 0; - - } - - i++; - j++; - - } - - if(string1[i] == '\0' && string2[j] == '\0'){ - - return 1; - - } - else{ - - return 0; - - } - - - } - - else{ - - printf("warning : fuck you, strings are considered null"); - crash(); - return 0; - - } - -} - -WordList* expandInDir(char* dir, char* toExpand){ - - int i = 0; - WordList* list = createWordList(); - WordArray* files = getFiles(dir); - - for(i=0; isize;i++){ - - if(comparator(toExpand, files->words[i])){ - - addWordList(list, files->words[i]); - - } - - } - - - return list; - -} - - diff --git a/fish_shell/fish_globbing.h b/fish_shell/fish_globbing.h index 8438d2e..e473981 100644 --- a/fish_shell/fish_globbing.h +++ b/fish_shell/fish_globbing.h @@ -8,11 +8,6 @@ WordList * fishExpand(WordList *wordArray); WordArray* getFiles(char* path); -/*char1 is a string with characters such as '*', '.' or '?' having special meanings*/ -int comparator(char* string1, char* string2); - -WordList* expandInDir(char*, char*); - -WordList* recursiveExpand(char* completePath); +WordList* expandWord(char* word); #endif //FISH_FISH_GLOBBING_H diff --git a/fish_shell/fish_utils.c b/fish_shell/fish_utils.c index 1f6c6ea..ba8ae8b 100644 --- a/fish_shell/fish_utils.c +++ b/fish_shell/fish_utils.c @@ -117,20 +117,28 @@ void freeWordList(WordList *list) { free(list); } -WordList* concatWordList(WordList* list1, WordList* list2){ +void concatWordList(WordList* list1, WordList* list2){//return a sing list containing all elements of both lists + if(list1 == NULL || list2 == NULL){ + crash(); + } + else if(list2->size != 0){ - if(list1 != NULL && list2 != NULL && list1->size >= 1 && list2->size >=1){ - list1->last->next = list2->first; - list2->first->previous = list1->last; - list1->last = list2->last; - list1->size = list1->size + list2->size; - free(list2); - return list1; - } - else{ - return NULL; - } + WordListElement* tempElement = list2->first; + + for(int i = 0; i < list2->size; i++){ + + addEndWordList(list1, tempElement->word); + tempElement = tempElement->next; + + } + + freeWordList(list2); + + } + else{ + freeWordList(list2); + } } @@ -286,12 +294,12 @@ int stringContains(char * string, char charToTest){ while(string[i] != '\0'){ - if(string[i] != charToTest){ - return 0; + if(string[i] == charToTest){ + return 1; } i++; } - return 1; + return 0; } diff --git a/fish_shell/fish_utils.h b/fish_shell/fish_utils.h index b7bfe29..38dd497 100644 --- a/fish_shell/fish_utils.h +++ b/fish_shell/fish_utils.h @@ -35,12 +35,10 @@ WordList * splitWordList(WordList *list, char *regex); char * splitWord(char * origin, int beginning_index, int size_to_delete); // Tested -WordList* concatWordList(WordList* list1, WordList* list2); +void concatWordList(WordList* list1, WordList* list2); int stringContains(char* string, char charToTest); -WordList* concatWordList(WordList* list1, WordList* list2); - int stringContains(char* string, char charToTest); #endif //FISH_FISH_UTILS_H From 99545a8d416e66e1bd5677c52571ae1be4c5d8ac Mon Sep 17 00:00:00 2001 From: Aethor Date: Sun, 28 May 2017 16:35:35 +0200 Subject: [PATCH 06/11] =?UTF-8?q?on=20traque=20les=20fuites=20m=C3=A9moire?= =?UTF-8?q?s=20de=20SATAN?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fish_shell/fish_globbing.c | 19 +++++++++---------- fish_shell/fish_globbing.h | 2 +- fish_shell/fish_utils.c | 2 +- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/fish_shell/fish_globbing.c b/fish_shell/fish_globbing.c index 2964ebd..da697a6 100644 --- a/fish_shell/fish_globbing.c +++ b/fish_shell/fish_globbing.c @@ -1,4 +1,4 @@ -// Created by Aethor +// Created by Arthur Amalvy #include #include #include @@ -8,7 +8,7 @@ WordList * fishExpand(WordList *wordList) { - if(wordList->size > 1){ + /*if(wordList->size > 1){ int i; WordList* newWordList = createWordList();// creating the list to return @@ -45,7 +45,8 @@ WordList * fishExpand(WordList *wordList) { } - else return wordList; + else return wordList;*/ + return wordList; } @@ -61,14 +62,14 @@ WordList* expandWord(char* word){ -WordArray * getFiles(char* path){ +WordList* getFiles(char* path){ DIR* directory; dirent* dir; int i = 0; - WordArray* files = (WordArray*) malloc(sizeof(WordArray)); + WordList* files = createWordList(); if((directory = opendir(path)) != NULL){ @@ -79,8 +80,6 @@ WordArray * getFiles(char* path){ } - files->words = (char **) malloc(sizeof(char*) * (i + 1)); - closedir(directory); directory = opendir(path); i = 0; @@ -89,13 +88,13 @@ WordArray * getFiles(char* path){ if(!strcmp(dir->d_name, ".") && !strcmp(dir->d_name, "..")){ - printf("%s\n", dir->d_name); - files->words[i] = dir->d_name; + printf("%s\n", dir->d_name);//test + addEndWordList(files, dir->d_name); i++; files->size++; } - + } diff --git a/fish_shell/fish_globbing.h b/fish_shell/fish_globbing.h index e473981..e226a19 100644 --- a/fish_shell/fish_globbing.h +++ b/fish_shell/fish_globbing.h @@ -6,7 +6,7 @@ typedef struct dirent dirent; WordList * fishExpand(WordList *wordArray); -WordArray* getFiles(char* path); +WordList* getFiles(char* path); WordList* expandWord(char* word); diff --git a/fish_shell/fish_utils.c b/fish_shell/fish_utils.c index ba8ae8b..efff4b1 100644 --- a/fish_shell/fish_utils.c +++ b/fish_shell/fish_utils.c @@ -117,7 +117,7 @@ void freeWordList(WordList *list) { free(list); } -void concatWordList(WordList* list1, WordList* list2){//return a sing list containing all elements of both lists +void concatWordList(WordList* list1, WordList* list2){//return a single list containing all elements of both lists if(list1 == NULL || list2 == NULL){ crash(); From 2b5ad069770292afa2b91d6ccc85856eb9ded958 Mon Sep 17 00:00:00 2001 From: Aethor Date: Sun, 28 May 2017 17:25:04 +0200 Subject: [PATCH 07/11] NOT ON MY WATCH --- fish_shell/fish_globbing.c | 33 +++++++++++---------------------- fish_shell/fish_utils.c | 31 ++++++++++++++++++++++++++++++- fish_shell/fish_utils.h | 3 ++- 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/fish_shell/fish_globbing.c b/fish_shell/fish_globbing.c index da697a6..0e46653 100644 --- a/fish_shell/fish_globbing.c +++ b/fish_shell/fish_globbing.c @@ -8,7 +8,7 @@ WordList * fishExpand(WordList *wordList) { - /*if(wordList->size > 1){ + if(wordList->size > 1){ int i; WordList* newWordList = createWordList();// creating the list to return @@ -45,17 +45,18 @@ WordList * fishExpand(WordList *wordList) { } - else return wordList;*/ - return wordList; - + else return wordList; } WordList* expandWord(char* word){ - WordList* wordList = createWordList(); - addEndWordList(wordList, word); - return wordList; + printf("\n%s\n", word); + + WordList* testList = getFiles((char*) "../"); + + printWordList(testList); + return testList; } @@ -66,8 +67,6 @@ WordList* getFiles(char* path){ DIR* directory; dirent* dir; - int i = 0; - WordList* files = createWordList(); @@ -76,27 +75,17 @@ WordList* getFiles(char* path){ while((dir = readdir(directory)) != NULL){ - i++; - - } - - closedir(directory); - directory = opendir(path); - i = 0; - - while((dir = readdir(directory)) != NULL){ - - if(!strcmp(dir->d_name, ".") && !strcmp(dir->d_name, "..")){ + if(strcmp(dir->d_name, ".") && strcmp(dir->d_name, "..")){//sorry strcmp but I dont like you :( printf("%s\n", dir->d_name);//test addEndWordList(files, dir->d_name); - i++; - files->size++; } } + closedir(directory); //YY U LEAK MEMORY ? NOT ON MY WATCH + } diff --git a/fish_shell/fish_utils.c b/fish_shell/fish_utils.c index efff4b1..c164493 100644 --- a/fish_shell/fish_utils.c +++ b/fish_shell/fish_utils.c @@ -288,6 +288,34 @@ WordList *splitWordList(WordList *list, char *regex) { return new_list; } +//for debugging purposes +void printWordList(WordList* list){ + + + if(list != NULL){ + + printf("--- list ---\n"); + printf("size : %i\n", list->size); + + int i = 0; + WordListElement* tempElement = list->first; + + for(i=0; isize; i++){ + + printf("element %i : %s\n",i, tempElement->word); + tempElement = tempElement->next; + + } + + printf("--- end ---\n"); + + } + else{ + printf("fish : Warning : list is null. Are you stupid ?"); + } + +} + int stringContains(char * string, char charToTest){ int i = 0; @@ -301,5 +329,6 @@ int stringContains(char * string, char charToTest){ } return 0; - + } + diff --git a/fish_shell/fish_utils.h b/fish_shell/fish_utils.h index 38dd497..91f458c 100644 --- a/fish_shell/fish_utils.h +++ b/fish_shell/fish_utils.h @@ -37,8 +37,9 @@ char * splitWord(char * origin, int beginning_index, int size_to_delete); // Tes void concatWordList(WordList* list1, WordList* list2); -int stringContains(char* string, char charToTest); +void printWordList(WordList* list); int stringContains(char* string, char charToTest); + #endif //FISH_FISH_UTILS_H From ee951fb2cd14b7c2a9d40dbb9f8b9e772614f15b Mon Sep 17 00:00:00 2001 From: Aethor Date: Sun, 28 May 2017 22:02:05 +0200 Subject: [PATCH 08/11] non-recursive globbing --- fish_shell/fish_globbing.c | 93 ++++++++++++++++++++++++++++++++++---- fish_shell/fish_globbing.h | 6 ++- fish_shell/fish_utils.c | 2 +- 3 files changed, 90 insertions(+), 11 deletions(-) diff --git a/fish_shell/fish_globbing.c b/fish_shell/fish_globbing.c index 0e46653..6652e52 100644 --- a/fish_shell/fish_globbing.c +++ b/fish_shell/fish_globbing.c @@ -51,19 +51,26 @@ WordList * fishExpand(WordList *wordList) { WordList* expandWord(char* word){ - printf("\n%s\n", word); + if(!stringContains(word, '/')){ - WordList* testList = getFiles((char*) "../"); + return getFiles((char*) "./", word); + + } + + else{ + + return getFiles(word, "*");//temporary + //return(); + + } - printWordList(testList); - return testList; } -WordList* getFiles(char* path){ +WordList* getFiles(char* path, char* wildcardedString){ DIR* directory; dirent* dir; @@ -75,10 +82,16 @@ WordList* getFiles(char* path){ while((dir = readdir(directory)) != NULL){ - if(strcmp(dir->d_name, ".") && strcmp(dir->d_name, "..")){//sorry strcmp but I dont like you :( + if(strcmp(dir->d_name, ".") && strcmp(dir->d_name, "..") && wildcardedStringMatches(wildcardedString, dir->d_name)){//sorry strcmp but I dont like you :( - printf("%s\n", dir->d_name);//test - addEndWordList(files, dir->d_name); + //dont read this, and if you do, do not complain, it's strcat's fault. you've been warned. + char* filePath = (char*) malloc(sizeof(char) * (strlen(path) + strlen(dir->d_name)) + 1); + if(filePath == NULL) crash(); + filePath[0] = '\0'; + strcat(filePath, path); + strcat(filePath, dir->d_name); + addEndWordList(files, filePath); + free(filePath); } @@ -93,3 +106,67 @@ WordList* getFiles(char* path){ } + +int wildcardedStringMatches(char* string1, char* string2){//TODO + + int i = 0; + char tempIChar; + int j = 0; + + if(string1 != NULL && string2 != NULL){ + + while(string1[i] != '\0' && string2[j] != '\0'){ + + if(string1[i] == '*'){ + + tempIChar = string1[i+1]; + + while(string2[j] != tempIChar){ + + j++; + + if(string2[j] == '\0' && tempIChar == '\0'){ + return 1; + } + + } + + i++; + + } + + if(string1[i] != string2[j] && string1[i] != '?'){ + + return 0; + + } + + i++; + j++; + + } + + if(string1[i] == '\0' && string2[j] == '\0'){ + + return 1; + + } + else{ + + return 0; + + } + + } + + else{ + + printf("warning : fuck you, strings are considered null"); + crash(); + return 0; + + } + +} + + diff --git a/fish_shell/fish_globbing.h b/fish_shell/fish_globbing.h index e226a19..8804afc 100644 --- a/fish_shell/fish_globbing.h +++ b/fish_shell/fish_globbing.h @@ -4,10 +4,12 @@ typedef struct dirent dirent; -WordList * fishExpand(WordList *wordArray); +WordList* fishExpand(WordList* wordArray); -WordList* getFiles(char* path); +WordList* getFiles(char* path, char* wildcardedString); WordList* expandWord(char* word); +int wildcardedStringMatches(char* string1, char* string2); + #endif //FISH_FISH_GLOBBING_H diff --git a/fish_shell/fish_utils.c b/fish_shell/fish_utils.c index c164493..8a6c117 100644 --- a/fish_shell/fish_utils.c +++ b/fish_shell/fish_utils.c @@ -311,7 +311,7 @@ void printWordList(WordList* list){ } else{ - printf("fish : Warning : list is null. Are you stupid ?"); + fprintf(stderr, "fish : Warning : list is null. Are you stupid ?\n"); } } From 82e9e10895688a79046723fe2924ae45b2052edf Mon Sep 17 00:00:00 2001 From: Aethor Date: Sun, 28 May 2017 22:02:53 +0200 Subject: [PATCH 09/11] =?UTF-8?q?ISO=20C++=20commence=20=C3=A0=20me=20fagg?= =?UTF-8?q?oter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fish_shell/fish_globbing.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fish_shell/fish_globbing.c b/fish_shell/fish_globbing.c index 6652e52..17cb999 100644 --- a/fish_shell/fish_globbing.c +++ b/fish_shell/fish_globbing.c @@ -59,7 +59,7 @@ WordList* expandWord(char* word){ else{ - return getFiles(word, "*");//temporary + return getFiles(word, (char*) "*");//temporary //return(); } From 2c73d461fb4e57ca334ebb37ba21b9aeaf113b8b Mon Sep 17 00:00:00 2001 From: Aethor Date: Mon, 29 May 2017 15:19:52 +0200 Subject: [PATCH 10/11] EL GENIUS ORI ORA --- fish_shell/fish_globbing.c | 62 +++++++++++++++++++++++++++++++++++++- fish_shell/fish_globbing.h | 2 ++ fish_shell/fish_types.h | 2 ++ fish_shell/fish_utils.c | 1 - 4 files changed, 65 insertions(+), 2 deletions(-) diff --git a/fish_shell/fish_globbing.c b/fish_shell/fish_globbing.c index 17cb999..35a9ba6 100644 --- a/fish_shell/fish_globbing.c +++ b/fish_shell/fish_globbing.c @@ -41,6 +41,11 @@ WordList * fishExpand(WordList *wordList) { } freeWordList(wordList); + + //TODO : move this in recursion in case multiples commands are in the same line + if(newWordList->size == 1){ + addEndWordList(newWordList, (char*) ERROR_STRING); + } return newWordList; } @@ -59,6 +64,10 @@ WordList* expandWord(char* word){ else{ + WordList* testList = splitWordIntoList(word, '/'); + printWordList(testList); + freeWordList(testList); + return getFiles(word, (char*) "*");//temporary //return(); @@ -161,7 +170,7 @@ int wildcardedStringMatches(char* string1, char* string2){//TODO else{ - printf("warning : fuck you, strings are considered null"); + printf("fish : Warning : fuck you, strings are considered null"); crash(); return 0; @@ -169,4 +178,55 @@ int wildcardedStringMatches(char* string1, char* string2){//TODO } +//beware : will purposedly ignore the first occurence of the character +WordList* splitWordIntoList(char* string, char splitChar){ + if(stringContains(string, '/')){ + + int i = 0; + int mark = 0; + int finished = 0; + int firstEncounter = 0; + WordList* newWordList = createWordList(); + + while(!finished){ + + if(string[i] == splitChar || string[i] == '\0'){ + + if(!firstEncounter){ + + firstEncounter = 1; + + } + + else{ + + char* tempStr = strndup(string + mark, i - mark); + + if(tempStr == NULL){ + crash(); + } + + addEndWordList(newWordList, tempStr); + free(tempStr); + mark = i; + if(string[i] == '\0'){ + finished = 1; + } + } + + } + i++; + + } + + return newWordList; + + } + else{ + WordList* newWordList = createWordList(); + addEndWordList(newWordList, string); + return newWordList; + } + +} diff --git a/fish_shell/fish_globbing.h b/fish_shell/fish_globbing.h index 8804afc..74fc3b5 100644 --- a/fish_shell/fish_globbing.h +++ b/fish_shell/fish_globbing.h @@ -12,4 +12,6 @@ WordList* expandWord(char* word); int wildcardedStringMatches(char* string1, char* string2); +WordList* splitWordIntoList(char* string, char splitchar); + #endif //FISH_FISH_GLOBBING_H diff --git a/fish_shell/fish_types.h b/fish_shell/fish_types.h index cef1a74..958bdc4 100644 --- a/fish_shell/fish_types.h +++ b/fish_shell/fish_types.h @@ -6,6 +6,7 @@ #define FISH_FISH_TYPES_H #define EXIT_SIGNAL -100 +#define ERROR_STRING "\n" /* Custom types */ @@ -35,6 +36,7 @@ typedef struct { WordListElement * last; } WordList; + typedef struct { char *PS1; } Settings; diff --git a/fish_shell/fish_utils.c b/fish_shell/fish_utils.c index 8a6c117..febdbe2 100644 --- a/fish_shell/fish_utils.c +++ b/fish_shell/fish_utils.c @@ -331,4 +331,3 @@ int stringContains(char * string, char charToTest){ return 0; } - From 8eabe265656468c311a86f8dacf502842779ac16 Mon Sep 17 00:00:00 2001 From: Aethor Date: Mon, 29 May 2017 17:25:31 +0200 Subject: [PATCH 11/11] =?UTF-8?q?Un=20globbing=20r=C3=A9cursif,=20mais=20p?= =?UTF-8?q?as=20des=20masses?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fish_shell/fish_globbing.c | 83 ++++++++++++++++++++++++++++++++------ fish_shell/fish_globbing.h | 4 ++ fish_shell/fish_utils.c | 12 ++++++ fish_shell/fish_utils.h | 2 + 4 files changed, 88 insertions(+), 13 deletions(-) diff --git a/fish_shell/fish_globbing.c b/fish_shell/fish_globbing.c index 35a9ba6..6cc6efe 100644 --- a/fish_shell/fish_globbing.c +++ b/fish_shell/fish_globbing.c @@ -64,19 +64,79 @@ WordList* expandWord(char* word){ else{ - WordList* testList = splitWordIntoList(word, '/'); - printWordList(testList); - freeWordList(testList); + WordList* pathList = splitWordIntoList(word, '/'); + WordList* expandedArgsList = createWordList(); + if (expandedArgsList == NULL) crash(); + WordListElement* tempElement; + int i; - return getFiles(word, (char*) "*");//temporary - //return(); + if(pathList->size >= 1){ + tempElement = pathList->first; + for(i=0; i < pathList->size; i++){ + + printf("\nBASE PATH %s\n", tempElement->word); + char* tempPath = getPath(tempElement->word); + printf("\nPATH : %s\n", tempPath); + char* tempFileName = getFileName(tempElement->word); + printf("\nFILENAME : %s\n", tempFileName); + + concatWordList(expandedArgsList, getFiles(tempPath, tempFileName)); + printWordList(expandedArgsList); + tempElement = tempElement->next; + + free(tempPath); + free(tempFileName); + + } + } + + freeWordList(pathList); + + return expandedArgsList; + //return getFiles(word, (char*) "*");//temporary } } +char* getFileName(char* string){ + if(!stringContains(string, '/')){ + return string; + } + else{ + int i = 0; + while(string[i] != '/'){ + i++; + } + + return strndup(string + i + 1, strlen(string) - i); + } +} + + +//get path of a file +char* getPath(char* string){ + + if(!stringContains(string, '/')){ + return string; + } + else{ + + int i = strlen(string) - 1; + + while(i != -1 && string[i] != '/'){ + + i = i-1; + + } + + return strndup(string, i + 1); + + } + +} WordList* getFiles(char* path, char* wildcardedString){ @@ -85,6 +145,7 @@ WordList* getFiles(char* path, char* wildcardedString){ dirent* dir; WordList* files = createWordList(); + if(files == NULL) crash(); if((directory = opendir(path)) != NULL){ @@ -93,12 +154,7 @@ WordList* getFiles(char* path, char* wildcardedString){ if(strcmp(dir->d_name, ".") && strcmp(dir->d_name, "..") && wildcardedStringMatches(wildcardedString, dir->d_name)){//sorry strcmp but I dont like you :( - //dont read this, and if you do, do not complain, it's strcat's fault. you've been warned. - char* filePath = (char*) malloc(sizeof(char) * (strlen(path) + strlen(dir->d_name)) + 1); - if(filePath == NULL) crash(); - filePath[0] = '\0'; - strcat(filePath, path); - strcat(filePath, dir->d_name); + char* filePath = trueStrcat(path, dir->d_name); addEndWordList(files, filePath); free(filePath); @@ -185,9 +241,10 @@ WordList* splitWordIntoList(char* string, char splitChar){ int i = 0; int mark = 0; - int finished = 0; - int firstEncounter = 0; + int finished = 0; //boolean + int firstEncounter = 0; //boolean WordList* newWordList = createWordList(); + if(newWordList == NULL) crash(); while(!finished){ diff --git a/fish_shell/fish_globbing.h b/fish_shell/fish_globbing.h index 74fc3b5..0bdaae4 100644 --- a/fish_shell/fish_globbing.h +++ b/fish_shell/fish_globbing.h @@ -14,4 +14,8 @@ int wildcardedStringMatches(char* string1, char* string2); WordList* splitWordIntoList(char* string, char splitchar); +char* getFileName(char* string); + +char* getPath(char* string); + #endif //FISH_FISH_GLOBBING_H diff --git a/fish_shell/fish_utils.c b/fish_shell/fish_utils.c index febdbe2..34cdc7b 100644 --- a/fish_shell/fish_utils.c +++ b/fish_shell/fish_utils.c @@ -331,3 +331,15 @@ int stringContains(char * string, char charToTest){ return 0; } + +//dont read this, and if you do, do not complain, it's strcat's fault. you've been warned. +char* trueStrcat(char* string1, char* string2){ + + char* newString = (char*) malloc(sizeof(char) * (strlen(string1) + strlen(string2)) + 1); + if(newString == NULL) crash(); + newString[0] = '\0'; + strcat(newString, string1); + strcat(newString, string2); + return newString; + +} diff --git a/fish_shell/fish_utils.h b/fish_shell/fish_utils.h index 91f458c..d38bca3 100644 --- a/fish_shell/fish_utils.h +++ b/fish_shell/fish_utils.h @@ -41,5 +41,7 @@ void printWordList(WordList* list); int stringContains(char* string, char charToTest); +char* trueStrcat(char* string1, char* string2); + #endif //FISH_FISH_UTILS_H