From f9db2f1cd5708457625fe01fe84a36449ffeebef Mon Sep 17 00:00:00 2001 From: Aethor Date: Tue, 30 May 2017 11:31:00 +0200 Subject: [PATCH] =?UTF-8?q?un=20globbing=20r=C3=A9cursif,=20mais=20obscur?= =?UTF-8?q?=20et=20cass=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fish_shell/fish_globbing.c | 134 ++++++++++++++++++++++++++++--------- fish_shell/fish_globbing.h | 4 ++ 2 files changed, 105 insertions(+), 33 deletions(-) diff --git a/fish_shell/fish_globbing.c b/fish_shell/fish_globbing.c index 6cc6efe..808f0ee 100644 --- a/fish_shell/fish_globbing.c +++ b/fish_shell/fish_globbing.c @@ -54,52 +54,120 @@ WordList * fishExpand(WordList *wordList) { } -WordList* expandWord(char* word){ +WordList* expandWord(char* path){ - if(!stringContains(word, '/')){ + if(!stringContains(path, '/')){ - return getFiles((char*) "./", word); + return getFiles((char*) "./", path); } else{ - WordList* pathList = splitWordIntoList(word, '/'); - WordList* expandedArgsList = createWordList(); - if (expandedArgsList == NULL) crash(); - WordListElement* tempElement; - int i; - - 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 + WordList* expandedList = createWordList(); + recursiveExpandWord(path, expandedList); + return expandedList; } } +void recursiveExpandWord(char* path, WordList* listToExpand){ + + int lastToExpand = 1; + int i = 0; + int indexToExpand = -1; + + //TODO : free + WordList* pathToList = splitWordIntoList(path, '/'); + WordListElement* tempElement; //beware of the size, should be checked before anyway + tempElement = pathToList->first; + + + while(i < pathToList->size - 1 && indexToExpand == -1){ + + if(stringContains(tempElement->word, '*') || stringContains(tempElement->word, '?')){ + indexToExpand = i; + lastToExpand = 0; + } + + i++; + tempElement = tempElement->next; + + } + + + if(lastToExpand){ + concatWordList(listToExpand, getFiles(getPath(path), getFileName(path))); + free(pathToList); + } + else{ + + char* correctedPath = concatWordListToWord(pathToList,0, indexToExpand); + WordList* foundFiles = getFiles(getPath(correctedPath), getFileName(correctedPath)); + + if(foundFiles->size > 0){ + + tempElement = foundFiles->first; + char* concatenedEndOfPath = concatWordListToWord(foundFiles, indexToExpand + 1, foundFiles->size - 1); + + for(i=0; i < foundFiles->size; i++){ + + tempElement->word = trueStrcat(tempElement->word, concatenedEndOfPath); + recursiveExpandWord(tempElement->word, listToExpand); + + tempElement = tempElement->next; + + } + + free(concatenedEndOfPath); + + } + + freeWordList(pathToList); + free(correctedPath); + freeWordList(foundFiles); + + } + + +} + +char* concatWordListToWord(WordList* list,int firstElemIndex, int lastElemIndex){ + + if(list->size == 0 || list == NULL) crash(); + + int i; + char* concatenedString = (char*) malloc(sizeof(char)); + if(concatenedString == NULL) crash(); + + if(lastElemIndex > list->size -1){ + lastElemIndex = list->size - 1; + fprintf(stderr, "fish : Warning : you are a miserable failure, your element is beyond the list. pfff. I corrected it for you."); + } + if(firstElemIndex > lastElemIndex){ + firstElemIndex = lastElemIndex; + fprintf(stderr, "fish : Warning : how are you so bad ? your inferior index is superior to your superior index. pfff. I corrected it for you."); + } + + WordListElement* tempElement = list->first; + for(i=0; i < firstElemIndex; i++){ + + tempElement = tempElement->next; + + } + for(i=firstElemIndex; i < lastElemIndex; i++){ + + trueStrcat(concatenedString, tempElement->word); + tempElement = tempElement->next; + + } + + return concatenedString; + +} + char* getFileName(char* string){ if(!stringContains(string, '/')){ diff --git a/fish_shell/fish_globbing.h b/fish_shell/fish_globbing.h index 0bdaae4..690d9ae 100644 --- a/fish_shell/fish_globbing.h +++ b/fish_shell/fish_globbing.h @@ -10,6 +10,8 @@ WordList* getFiles(char* path, char* wildcardedString); WordList* expandWord(char* word); +void recursiveExpandWord(char* path, WordList* listToExpand); + int wildcardedStringMatches(char* string1, char* string2); WordList* splitWordIntoList(char* string, char splitchar); @@ -18,4 +20,6 @@ char* getFileName(char* string); char* getPath(char* string); +char* concatWordListToWord(WordList* list, int firstElemIndex, int lastElemIndex); + #endif //FISH_FISH_GLOBBING_H