Merge branch 'bro' into 'master'

non-recursive globbing

See merge request !9
This commit is contained in:
Antoine Bartuccio 2017-05-29 16:46:25 +00:00
commit 17cdce874f
6 changed files with 304 additions and 33 deletions

View File

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

View File

@ -1,55 +1,168 @@
// Created by Aethor
// Created by Arthur Amalvy
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include "fish_core.h"
#include "fish_globbing.h"
WordList * fishExpand(WordList *wordArray) {
WordList * fishExpand(WordList *wordList) {
int i;
//WordArray* splitParameter;
if(wordList->size > 1){
for(i=1; i<wordArray->size; i++){
int i;
WordList* newWordList = createWordList();// creating the list to return
if(newWordList == NULL){//crash when the allocation is unsuccessful
crash();
}
addEndWordList(newWordList, wordList->first->word);//copy the command into the returning word list
WordListElement* tempElement = wordList->first->next; //temporary nav element
for(i=1; i<wordList->size; i++){
//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, '?')){
concatWordList(newWordList, expandWord(tempElement->word));
}
//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);
//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;
}
return wordArray;
else return wordList;
}
WordList* expandWord(char* word){
if(!stringContains(word, '/')){
return getFiles((char*) "./", word);
}
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
}
}
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);
}
}
WordArray * getFiles(char* path){
WordList* getFiles(char* path, char* wildcardedString){
DIR* directory;
dirent* dir;
int i = 0;
WordArray* files = (WordArray*) malloc(sizeof(WordArray));
WordList* files = createWordList();
if(files == NULL) crash();
if((directory = opendir(path)) != NULL){
while((dir = readdir(directory)) != NULL){
i++;
if(strcmp(dir->d_name, ".") && strcmp(dir->d_name, "..") && wildcardedStringMatches(wildcardedString, dir->d_name)){//sorry strcmp but I dont like you :(
char* filePath = trueStrcat(path, dir->d_name);
addEndWordList(files, filePath);
free(filePath);
}
}
files->words = (char **) malloc(sizeof(char*) * (i + 1));
closedir(directory);
directory = opendir(path);
i = 0;
while((dir = readdir(directory)) != NULL){
files->words[i] = dir->d_name;
}
closedir(directory); //YY U LEAK MEMORY ? NOT ON MY WATCH
}
@ -58,7 +171,8 @@ WordArray * getFiles(char* path){
}
int comparator(char* string1, char* string2){//TODO
int wildcardedStringMatches(char* string1, char* string2){//TODO
int i = 0;
char tempIChar;
@ -81,6 +195,7 @@ int comparator(char* string1, char* string2){//TODO
}
}
i++;
}
@ -107,12 +222,11 @@ int comparator(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;
@ -120,4 +234,56 @@ int comparator(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; //boolean
int firstEncounter = 0; //boolean
WordList* newWordList = createWordList();
if(newWordList == NULL) crash();
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;
}
}

View File

@ -4,11 +4,18 @@
typedef struct dirent dirent;
WordList * fishExpand(WordList *wordArray);
WordList* fishExpand(WordList* wordArray);
WordArray* getFiles(char* path);
WordList* getFiles(char* path, char* wildcardedString);
/*char1 is a string with characters such as '*', '.' or '?' having special meanings*/
int comparator(char* string1, char* string2);
WordList* expandWord(char* word);
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

View File

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

View File

@ -117,6 +117,31 @@ void freeWordList(WordList *list) {
free(list);
}
void concatWordList(WordList* list1, WordList* list2){//return a single list containing all elements of both lists
if(list1 == NULL || list2 == NULL){
crash();
}
else if(list2->size != 0){
WordListElement* tempElement = list2->first;
for(int i = 0; i < list2->size; i++){
addEndWordList(list1, tempElement->word);
tempElement = tempElement->next;
}
freeWordList(list2);
}
else{
freeWordList(list2);
}
}
WordArray *wordListToWordArray(WordList *list) {
WordArray *array = (WordArray*) malloc(sizeof(WordArray));
WordListElement *current = list->first;
@ -263,3 +288,58 @@ 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; i<list->size; i++){
printf("element %i : %s\n",i, tempElement->word);
tempElement = tempElement->next;
}
printf("--- end ---\n");
}
else{
fprintf(stderr, "fish : Warning : list is null. Are you stupid ?\n");
}
}
int stringContains(char * string, char charToTest){
int i = 0;
while(string[i] != '\0'){
if(string[i] == charToTest){
return 1;
}
i++;
}
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;
}

View File

@ -35,4 +35,13 @@ WordList * splitWordList(WordList *list, char *regex);
char * splitWord(char * origin, int beginning_index, int size_to_delete); // Tested
void concatWordList(WordList* list1, WordList* list2);
void printWordList(WordList* list);
int stringContains(char* string, char charToTest);
char* trueStrcat(char* string1, char* string2);
#endif //FISH_FISH_UTILS_H