From 2a7dad2662b71c8031c9c7bcf65f10d3e68de4eb Mon Sep 17 00:00:00 2001 From: Aethor Date: Sat, 27 May 2017 17:36:55 +0200 Subject: [PATCH] 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