1
0
mirror of https://gitlab.com/klmp200/fish.git synced 2024-12-22 07:41:08 +00:00

Merge branch 'bro' into 'master'

Le globbing détruit bash

See merge request !11
This commit is contained in:
Antoine Bartuccio 2017-06-06 08:42:05 +00:00
commit 7872cf0cdf
2 changed files with 135 additions and 36 deletions

View File

@ -54,65 +54,155 @@ 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;
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){
char* tmpPath = getPath(path);
char* tmpFileName = getFileName(path);
concatWordList(listToExpand, getFiles(tmpPath, tmpFileName));
free(tmpPath);
free(tmpFileName);
freeWordList(pathToList);
}
else{
char* correctedPath = concatWordListToWord(pathToList,0, indexToExpand + 1);
char* pathOfCorrectedPath = getPath(correctedPath);
char* fileNameOfCorrectedPath = getFileName(correctedPath);
WordList* foundFiles = getFiles(pathOfCorrectedPath, fileNameOfCorrectedPath);
char* tmpWord = NULL;
free(pathOfCorrectedPath);
free(fileNameOfCorrectedPath);
free(correctedPath);
if(foundFiles->size > 0){
tempElement = foundFiles->first;
char* concatenedEndOfPath = concatWordListToWord(pathToList, indexToExpand, pathToList->size - 1);
for(i=0; i < foundFiles->size; i++){
tmpWord = tempElement->word;
tempElement->word = trueStrcat(tempElement->word, concatenedEndOfPath);
free(tmpWord);
tmpWord = NULL;
recursiveExpandWord(tempElement->word, listToExpand);
tempElement = tempElement->next;
}
free(concatenedEndOfPath);
}
freeWordList(pathToList);
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));
char* tmpConcatenedString = concatenedString;
if(concatenedString == NULL) crash();
concatenedString[0] = '\0';
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.\n");
}
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.\n");
}
WordListElement* tempElement = list->first;
for(i=0; i < firstElemIndex; i++){
tempElement = tempElement->next;
}
for(i=firstElemIndex; i < lastElemIndex; i++){
concatenedString = trueStrcat(concatenedString, tempElement->word);
free(tmpConcatenedString);
tempElement = tempElement->next;
}
return concatenedString;
}
char* getFileName(char* string){
int wordSize = strlen(string) - 1;
if(!stringContains(string, '/')){
return string;
}
else{
int i = 0;
int i = wordSize;
while(string[i] != '/'){
i++;
i--;
}
return strndup(string + i + 1, strlen(string) - i);
return strndup(string + i + 1,wordSize);
}
}
@ -246,6 +336,11 @@ WordList* splitWordIntoList(char* string, char splitChar){
WordList* newWordList = createWordList();
if(newWordList == NULL) crash();
if(string[0] == '.'){
addEndWordList(newWordList, (char*) ".");
i++;
}
while(!finished){
if(string[i] == splitChar || string[i] == '\0'){

View File

@ -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