1
0
mirror of https://gitlab.com/klmp200/fish.git synced 2024-12-04 06:51:11 +00:00

Support des regex pour détecter les opérateurs logiques.

Split des mots si caractère spécial collé.
This commit is contained in:
Antoine Bartuccio 2017-05-20 16:25:00 +02:00
parent 7eada756e5
commit b550ebc9df
8 changed files with 204 additions and 83 deletions

View File

@ -9,7 +9,7 @@ build:
# instead of calling g++ directly you can also use some build toolkit like make # instead of calling g++ directly you can also use some build toolkit like make
# install the necessary build tools when needed # install the necessary build tools when needed
before_script: before_script:
- apt update && apt -y install make cmake python - apt update && apt -y install make cmake python libpcre3 libpcre3-dev
script: script:
- mkdir build && cd build && cmake .. && make && make test - mkdir build && cd build && cmake .. && make && make test
#artifacts: #artifacts:

View File

@ -48,6 +48,7 @@ include_directories(
set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD 99)
set(CMAKE_C_FLAGS "-Wall -Werror -pedantic -fpic -Wextra -Wshadow") set(CMAKE_C_FLAGS "-Wall -Werror -pedantic -fpic -Wextra -Wshadow")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FALGS} -g") set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FALGS} -g")
set(CMAKE_EXE_LINKER_FLAGS "-lpcre")
set(SOURCE_FILES fish_shell/main.c fish_shell/fish_types.h fish_shell/fish_core.h fish_shell/fish_core.c fish_shell/fish_commands.c fish_shell/fish_commands.h fish_shell/fish_globbing.c fish_shell/fish_globbing.h fish_shell/fish_utils.c fish_shell/fish_utils.h) set(SOURCE_FILES fish_shell/main.c fish_shell/fish_types.h fish_shell/fish_core.h fish_shell/fish_core.c fish_shell/fish_commands.c fish_shell/fish_commands.h fish_shell/fish_globbing.c fish_shell/fish_globbing.h fish_shell/fish_utils.c fish_shell/fish_utils.h)
add_executable(fish ${SOURCE_FILES}) add_executable(fish ${SOURCE_FILES})
@ -56,7 +57,6 @@ add_executable(fish ${SOURCE_FILES})
add_subdirectory(fish_shell_tests) add_subdirectory(fish_shell_tests)
enable_testing() enable_testing()
add_test(NAME fish_tests add_test(NAME fish_tests

View File

@ -9,13 +9,14 @@
/* Necessary global variables */ /* Necessary global variables */
char * builtinCommandsStr[] = { char * builtinCommandsStr[] = {
(char *) "kek",
(char *) "cd", (char *) "cd",
(char *) "help", (char *) "help",
(char *) "exit" (char *) "exit"
}; };
builtinCommand *builtinCommands[] = { builtinCommand *builtinCommands[] = {
&fishKek,
&fishCd, &fishCd,
&fishHelp, &fishHelp,
&fishExit &fishExit
@ -60,6 +61,12 @@ int fishExit(WordArray *args) {
} }
int fishKek(WordArray *args) {
freeWordArray(args);
printf("Praise kek !");
return EXIT_SUCCESS;
}
int getNbBuiltins() { int getNbBuiltins() {
return sizeof(builtinCommandsStr) / sizeof(char*); return sizeof(builtinCommandsStr) / sizeof(char*);
} }

View File

@ -21,4 +21,6 @@ int fishHelp(WordArray * args);
int fishExit(WordArray * args); int fishExit(WordArray * args);
int fishKek(WordArray *args);
#endif //FISH_FISH_COMMANDS_H #endif //FISH_FISH_COMMANDS_H

View File

@ -58,7 +58,7 @@ WordList * split(char *string, char *separator){
} }
while((token = strsep(&to_delete, separator)) != NULL) while((token = strsep(&to_delete, separator)) != NULL)
addWordList(list, token); addEndWordList(list, token);
free(to_delete_bak); free(to_delete_bak);
@ -201,7 +201,7 @@ int loadRightCommand(WordArray *array){
WordList * parseWordList(WordList *list, shell_operator *an_operator) { WordList * parseWordList(WordList *list, shell_operator *an_operator) {
char *op_str[] = { char *op_str[] = {
(char*) ";", (char*) ";",
(char*) "||", (char*) "\\|\\|",
(char*) "&&" (char*) "&&"
}; };
shell_operator op[] = { shell_operator op[] = {

View File

@ -6,6 +6,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <pcre.h>
#include "fish_utils.h" #include "fish_utils.h"
#include "fish_types.h" #include "fish_types.h"
@ -70,20 +71,38 @@ WordList *createWordList() {
return list; return list;
} }
void addWordList(WordList *list, char *word) { void addEndWordList(WordList *list, char *word) {
WordListElement *newElement = (WordListElement*) malloc(sizeof(WordListElement)); WordListElement *new_element = (WordListElement*) malloc(sizeof(WordListElement));
if (newElement == NULL) crash(); if (new_element == NULL) crash();
else { else {
newElement->next = NULL; new_element->next = NULL;
newElement->previous = list->last; new_element->previous = list->last;
if (list->size == 0){ if (list->size == 0){
list->first = newElement; list->first = new_element;
list->last = newElement; list->last = new_element;
} else { } else {
list->last->next = newElement; list->last->next = new_element;
list->last = newElement; list->last = new_element;
} }
newElement->word = strdup(word); new_element->word = strdup(word);
list->size++;
}
}
void addBeginWordList(WordList *list, char *word) {
WordListElement *new_element = (WordListElement*) malloc(sizeof(WordListElement));
if (new_element == NULL) crash();
else {
new_element->next = list->first;
new_element->previous = NULL;
if (list->size == 0){
list->first = new_element;
list->last = new_element;
} else {
list->first->previous = new_element;
list->first = new_element;
}
new_element->word = strdup(word);
list->size++; list->size++;
} }
} }
@ -127,7 +146,7 @@ WordList *wordArrayToWordList(WordArray *array) {
int i; int i;
for (i=0; i<array->size; i++) for (i=0; i<array->size; i++)
addWordList(list, array->words[i]); addEndWordList(list, array->words[i]);
freeWordArray(array); freeWordArray(array);
@ -176,7 +195,7 @@ WordList *sliceWordList(WordList *list, int min_index, int max_index) {
tmp = elem->next; tmp = elem->next;
if (i >= min_index){ if (i >= min_index){
addWordList(newList, elem->word); addEndWordList(newList, elem->word);
removeWordListElem(list, elem); removeWordListElem(list, elem);
} }
@ -188,21 +207,59 @@ WordList *sliceWordList(WordList *list, int min_index, int max_index) {
return newList; return newList;
} }
WordList *splitWordList(WordList *list, char *token) { char * splitWord(char * origin, int beginning_index, int size_to_delete){
WordList * newList = NULL; char * new_word = strdup(origin + beginning_index + size_to_delete);
WordListElement * current = list->first;
if (new_word == NULL) crash();
else {
origin[beginning_index] = '\0';
}
return new_word;
}
WordList *splitWordList(WordList *list, char *regex) {
const char* error;
int error_offset;
int ovector[100];
int i = 0; int i = 0;
int rc;
WordList * new_list = NULL;
WordListElement * current = list->first;
pcre *re = pcre_compile(regex, 0, &error, &error_offset, 0);
char *tmp_word = NULL;
if (!re) crash();
while (current != NULL){ while (current != NULL){
if (!strcmp(current->word, token)){ rc = pcre_exec(re,
newList = sliceWordList(list, i + 1, list->size); 0,
current->word,
(int) strlen(current->word),
0,
0,
ovector,
sizeof(ovector)
);
if(rc >= 0){
new_list = sliceWordList(list, i + 1, list->size);
tmp_word = splitWord(current->word, ovector[0], ovector[1] - ovector[0]);
if (tmp_word[0] != '\0') addBeginWordList(new_list, tmp_word);
if (current->word[0] != '\0') addEndWordList(list, current->word);
removeWordListElem(list, current); removeWordListElem(list, current);
current = NULL; current = NULL;
free(tmp_word);
} }
else current = current->next; else current = current->next;
i++; i++;
} }
return newList; pcre_free(re);
return new_list;
} }

View File

@ -17,7 +17,9 @@ WordList * createWordList(); // Tested
WordList * sliceWordList(WordList *list, int min_index, int max_index); WordList * sliceWordList(WordList *list, int min_index, int max_index);
void addWordList(WordList *list, char *word); // Tested void addEndWordList(WordList *list, char *word); // Tested
void addBeginWordList(WordList *list, char *word);
void removeWordList(WordList *list); // Tested void removeWordList(WordList *list); // Tested
@ -29,6 +31,8 @@ WordArray * wordListToWordArray(WordList *list); // Tested
WordList * wordArrayToWordList(WordArray * array); // Tested WordList * wordArrayToWordList(WordArray * array); // Tested
WordList * splitWordList(WordList *list, char * tokens); WordList * splitWordList(WordList *list, char *regex);
char * splitWord(char * origin, int beginning_index, int size_to_delete); // Tested
#endif //FISH_FISH_UTILS_H #endif //FISH_FISH_UTILS_H

View File

@ -6,15 +6,15 @@
#include "../fish_shell/fish_utils.h" #include "../fish_shell/fish_utils.h"
#include "../fish_shell/fish_types.h" #include "../fish_shell/fish_types.h"
TEST(crash_test, crash){ TEST(crash_crash_Test, crash){
ASSERT_DEATH(crash(), "[A-z]+"); ASSERT_DEATH(crash(), "[A-z]+");
} }
TEST(get_an_insult, getInsult){ TEST(get_getInsult_Test, getInsult){
ASSERT_EQ(typeid(char*), typeid(getInsult())); ASSERT_EQ(typeid(char*), typeid(getInsult()));
} }
TEST(word_list_init, createWordList){ TEST(create_createWordList_Test, createWordList){
WordList *list = createWordList(); WordList *list = createWordList();
ASSERT_FALSE(list == NULL); ASSERT_FALSE(list == NULL);
ASSERT_TRUE(list->first == NULL); ASSERT_TRUE(list->first == NULL);
@ -22,13 +22,46 @@ TEST(word_list_init, createWordList){
ASSERT_TRUE(list->size == 0); ASSERT_TRUE(list->size == 0);
} }
TEST(word_list_add, addWordList){ TEST(add_addBegin__Test, addBeginWordList){
WordList *list = createWordList(); WordList *list = createWordList();
char test1[] = "Test1"; char test1[] = "Test1";
char test2[] = "Test2"; char test2[] = "Test2";
char test3[] = "Test3"; char test3[] = "Test3";
addWordList(list, test1); addBeginWordList(list, test1);
ASSERT_EQ(list->size, 1);
ASSERT_FALSE(list->first == NULL);
ASSERT_FALSE(list->last == NULL);
ASSERT_EQ(list->first, list->last);
ASSERT_FALSE(list->first->word == NULL);
ASSERT_STREQ(list->first->word, test1);
ASSERT_FALSE(list->first->word == test1);
addBeginWordList(list, test2);
ASSERT_EQ(list->size, 2);
ASSERT_FALSE(list->first == list->last);
ASSERT_STREQ(list->first->word, test2);
ASSERT_STREQ(list->last->word, test1);
ASSERT_STRNE(list->first->word, test1);
addBeginWordList(list, test3);
ASSERT_EQ(list->size, 3);
ASSERT_FALSE(list->first == list->last);
ASSERT_STREQ(list->last->word, test1);
ASSERT_STREQ(list->first->next->word, test2);
ASSERT_STREQ(list->first->word, test3);
freeWordList(list);
}
TEST(add_addEndWordList_Test, addEndWordList){
WordList *list = createWordList();
char test1[] = "Test1";
char test2[] = "Test2";
char test3[] = "Test3";
addEndWordList(list, test1);
ASSERT_TRUE(list->size == 1); ASSERT_TRUE(list->size == 1);
ASSERT_TRUE(list->first != NULL); ASSERT_TRUE(list->first != NULL);
ASSERT_TRUE(list->last != NULL); ASSERT_TRUE(list->last != NULL);
@ -37,14 +70,14 @@ TEST(word_list_add, addWordList){
ASSERT_FALSE(strcmp(list->first->word, test1)); ASSERT_FALSE(strcmp(list->first->word, test1));
ASSERT_FALSE(list->first->word == test1); ASSERT_FALSE(list->first->word == test1);
addWordList(list, test2); addEndWordList(list, test2);
ASSERT_TRUE(list->size == 2); ASSERT_TRUE(list->size == 2);
ASSERT_TRUE(list->first != list->last); ASSERT_TRUE(list->first != list->last);
ASSERT_FALSE(strcmp(list->last->word, test2)); ASSERT_FALSE(strcmp(list->last->word, test2));
ASSERT_FALSE(strcmp(list->first->word, test1)); ASSERT_FALSE(strcmp(list->first->word, test1));
ASSERT_TRUE(strcmp(list->first->word, test2)); ASSERT_TRUE(strcmp(list->first->word, test2));
addWordList(list, test3); addEndWordList(list, test3);
ASSERT_TRUE(list->size == 3); ASSERT_TRUE(list->size == 3);
ASSERT_TRUE(list->first != list->last); ASSERT_TRUE(list->first != list->last);
ASSERT_FALSE(strcmp(list->first->word, test1)); ASSERT_FALSE(strcmp(list->first->word, test1));
@ -54,13 +87,13 @@ TEST(word_list_add, addWordList){
freeWordList(list); freeWordList(list);
} }
TEST(word_list_remove, removeWordList){ TEST(remove_removeWordList_Test, removeWordList){
WordList *list = createWordList(); WordList *list = createWordList();
char word[] = "lel"; char word[] = "lel";
addWordList(list, word); addEndWordList(list, word);
addWordList(list, word); addEndWordList(list, word);
addWordList(list, word); addEndWordList(list, word);
ASSERT_TRUE(list->size == 3); ASSERT_TRUE(list->size == 3);
@ -76,12 +109,12 @@ TEST(word_list_remove, removeWordList){
} }
TEST(word_list_free, freeWordList){ TEST(free_freeWordList_Test, freeWordList){
WordList *list = createWordList(); WordList *list = createWordList();
char word[] = "lel"; char word[] = "lel";
addWordList(list, word); addEndWordList(list, word);
addWordList(list, word); addEndWordList(list, word);
addWordList(list, word); addEndWordList(list, word);
freeWordList(list); freeWordList(list);
@ -90,7 +123,7 @@ TEST(word_list_free, freeWordList){
freeWordList(list); freeWordList(list);
} }
TEST(word_list_to_word_array, wordListToWordArray){ TEST(word_wordListToWordArray_Test, wordListToWordArray){
WordList *list = createWordList(); WordList *list = createWordList();
WordListElement *elem = NULL; WordListElement *elem = NULL;
WordArray *array = NULL; WordArray *array = NULL;
@ -100,28 +133,28 @@ TEST(word_list_to_word_array, wordListToWordArray){
char test2[] = "Test2"; char test2[] = "Test2";
char test3[] = "Test3"; char test3[] = "Test3";
addWordList(list, test1); addEndWordList(list, test1);
addWordList(list, test2); addEndWordList(list, test2);
addWordList(list, test3); addEndWordList(list, test3);
addWordList(list, test2); addEndWordList(list, test2);
addWordList(list, test2); addEndWordList(list, test2);
addWordList(list, test3); addEndWordList(list, test3);
addWordList(list, test1); addEndWordList(list, test1);
addWordList(list, test1); addEndWordList(list, test1);
array = wordListToWordArray(list); array = wordListToWordArray(list);
ASSERT_FALSE(array == NULL); ASSERT_FALSE(array == NULL);
ASSERT_FALSE(array->words == NULL); ASSERT_FALSE(array->words == NULL);
list = createWordList(); list = createWordList();
addWordList(list, test1); addEndWordList(list, test1);
addWordList(list, test2); addEndWordList(list, test2);
addWordList(list, test3); addEndWordList(list, test3);
addWordList(list, test2); addEndWordList(list, test2);
addWordList(list, test2); addEndWordList(list, test2);
addWordList(list, test3); addEndWordList(list, test3);
addWordList(list, test1); addEndWordList(list, test1);
addWordList(list, test1); addEndWordList(list, test1);
ASSERT_EQ(list->size, array->size); ASSERT_EQ(list->size, array->size);
@ -138,7 +171,7 @@ TEST(word_list_to_word_array, wordListToWordArray){
freeWordArray(array); freeWordArray(array);
} }
TEST(word_array_to_word_list, wordArrayToWordList){ TEST(word_wordArrayToWordList_Test, wordArrayToWordList){
WordList *list = createWordList(); WordList *list = createWordList();
WordList *list2 = NULL; WordList *list2 = NULL;
WordListElement *elem = NULL; WordListElement *elem = NULL;
@ -149,27 +182,27 @@ TEST(word_array_to_word_list, wordArrayToWordList){
char test2[] = "Test2"; char test2[] = "Test2";
char test3[] = "Test3"; char test3[] = "Test3";
addWordList(list, test1); addEndWordList(list, test1);
addWordList(list, test2); addEndWordList(list, test2);
addWordList(list, test3); addEndWordList(list, test3);
addWordList(list, test2); addEndWordList(list, test2);
addWordList(list, test2); addEndWordList(list, test2);
addWordList(list, test3); addEndWordList(list, test3);
addWordList(list, test1); addEndWordList(list, test1);
addWordList(list, test1); addEndWordList(list, test1);
array = wordListToWordArray(list); array = wordListToWordArray(list);
list2 = wordArrayToWordList(array); list2 = wordArrayToWordList(array);
list = createWordList(); list = createWordList();
addWordList(list, test1); addEndWordList(list, test1);
addWordList(list, test2); addEndWordList(list, test2);
addWordList(list, test3); addEndWordList(list, test3);
addWordList(list, test2); addEndWordList(list, test2);
addWordList(list, test2); addEndWordList(list, test2);
addWordList(list, test3); addEndWordList(list, test3);
addWordList(list, test1); addEndWordList(list, test1);
addWordList(list, test1); addEndWordList(list, test1);
array = wordListToWordArray(list); array = wordListToWordArray(list);
ASSERT_TRUE(list2 != NULL); ASSERT_TRUE(list2 != NULL);
@ -187,7 +220,7 @@ TEST(word_array_to_word_list, wordArrayToWordList){
} }
TEST(word_array_free, freeWordArray){ TEST(free_freeWordArray_Test, freeWordArray){
WordList *list = createWordList(); WordList *list = createWordList();
WordArray *array = NULL; WordArray *array = NULL;
@ -195,16 +228,34 @@ TEST(word_array_free, freeWordArray){
char test2[] = "Test2"; char test2[] = "Test2";
char test3[] = "Test3"; char test3[] = "Test3";
addWordList(list, test1); addEndWordList(list, test1);
addWordList(list, test2); addEndWordList(list, test2);
addWordList(list, test3); addEndWordList(list, test3);
addWordList(list, test2); addEndWordList(list, test2);
addWordList(list, test2); addEndWordList(list, test2);
addWordList(list, test3); addEndWordList(list, test3);
addWordList(list, test1); addEndWordList(list, test1);
addWordList(list, test1); addEndWordList(list, test1);
array = wordListToWordArray(list); array = wordListToWordArray(list);
freeWordArray(array); freeWordArray(array);
}
TEST(split_splitWord__Test, slpitWord){
char test1[] = "ls&&ls";
char test2[] = "&&";
char *splited = splitWord(test1, 2, 2);
ASSERT_STREQ(test1, "ls");
ASSERT_STREQ(splited, "ls");
free(splited);
splited = splitWord(test2, 0, 2);
ASSERT_STREQ(test2, "");
ASSERT_STREQ(splited, "");
free(splited);
} }