Browse Source

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

Split des mots si caractère spécial collé.
merge-requests/7/head
Antoine Bartuccio 2 years ago
parent
commit
b550ebc9df

+ 1
- 1
.gitlab-ci.yml View File

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

+ 1
- 1
CMakeLists.txt View File

@@ -48,6 +48,7 @@ include_directories(
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_FLAGS "-Wall -Werror -pedantic -fpic -Wextra -Wshadow")
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)
add_executable(fish ${SOURCE_FILES})
@@ -56,7 +57,6 @@ add_executable(fish ${SOURCE_FILES})

add_subdirectory(fish_shell_tests)


enable_testing()

add_test(NAME fish_tests

+ 8
- 1
fish_shell/fish_commands.c View File

@@ -9,13 +9,14 @@

/* Necessary global variables */
char * builtinCommandsStr[] = {
(char *) "kek",
(char *) "cd",
(char *) "help",
(char *) "exit"
};


builtinCommand *builtinCommands[] = {
&fishKek,
&fishCd,
&fishHelp,
&fishExit
@@ -60,6 +61,12 @@ int fishExit(WordArray *args) {

}

int fishKek(WordArray *args) {
freeWordArray(args);
printf("Praise kek !");
return EXIT_SUCCESS;
}

int getNbBuiltins() {
return sizeof(builtinCommandsStr) / sizeof(char*);
}

+ 2
- 0
fish_shell/fish_commands.h View File

@@ -21,4 +21,6 @@ int fishHelp(WordArray * args);

int fishExit(WordArray * args);

int fishKek(WordArray *args);

#endif //FISH_FISH_COMMANDS_H

+ 2
- 2
fish_shell/fish_core.c View File

@@ -58,7 +58,7 @@ WordList * split(char *string, char *separator){
}

while((token = strsep(&to_delete, separator)) != NULL)
addWordList(list, token);
addEndWordList(list, token);

free(to_delete_bak);

@@ -201,7 +201,7 @@ int loadRightCommand(WordArray *array){
WordList * parseWordList(WordList *list, shell_operator *an_operator) {
char *op_str[] = {
(char*) ";",
(char*) "||",
(char*) "\\|\\|",
(char*) "&&"
};
shell_operator op[] = {

+ 75
- 18
fish_shell/fish_utils.c View File

@@ -6,6 +6,7 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pcre.h>
#include "fish_utils.h"
#include "fish_types.h"

@@ -70,20 +71,38 @@ WordList *createWordList() {
return list;
}

void addWordList(WordList *list, char *word) {
WordListElement *newElement = (WordListElement*) malloc(sizeof(WordListElement));
if (newElement == NULL) crash();
void addEndWordList(WordList *list, char *word) {
WordListElement *new_element = (WordListElement*) malloc(sizeof(WordListElement));
if (new_element == NULL) crash();
else {
newElement->next = NULL;
newElement->previous = list->last;
new_element->next = NULL;
new_element->previous = list->last;
if (list->size == 0){
list->first = newElement;
list->last = newElement;
list->first = new_element;
list->last = new_element;
} else {
list->last->next = newElement;
list->last = newElement;
list->last->next = new_element;
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++;
}
}
@@ -127,7 +146,7 @@ WordList *wordArrayToWordList(WordArray *array) {
int i;

for (i=0; i<array->size; i++)
addWordList(list, array->words[i]);
addEndWordList(list, array->words[i]);

freeWordArray(array);

@@ -176,7 +195,7 @@ WordList *sliceWordList(WordList *list, int min_index, int max_index) {

tmp = elem->next;
if (i >= min_index){
addWordList(newList, elem->word);
addEndWordList(newList, elem->word);
removeWordListElem(list, elem);
}

@@ -188,21 +207,59 @@ WordList *sliceWordList(WordList *list, int min_index, int max_index) {
return newList;
}

WordList *splitWordList(WordList *list, char *token) {
WordList * newList = NULL;
WordListElement * current = list->first;
char * splitWord(char * origin, int beginning_index, int size_to_delete){
char * new_word = strdup(origin + beginning_index + size_to_delete);

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 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){
if (!strcmp(current->word, token)){
newList = sliceWordList(list, i + 1, list->size);
rc = pcre_exec(re,
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);
current = NULL;
free(tmp_word);
}

else current = current->next;
i++;
}

return newList;
pcre_free(re);

return new_list;
}


+ 6
- 2
fish_shell/fish_utils.h View File

@@ -17,7 +17,9 @@ WordList * createWordList(); // Tested

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

@@ -29,6 +31,8 @@ WordArray * wordListToWordArray(WordList *list); // 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

+ 109
- 58
fish_shell_tests/FishUtilsTests.cpp View File

@@ -6,15 +6,15 @@
#include "../fish_shell/fish_utils.h"
#include "../fish_shell/fish_types.h"

TEST(crash_test, crash){
TEST(crash_crash_Test, crash){
ASSERT_DEATH(crash(), "[A-z]+");
}

TEST(get_an_insult, getInsult){
TEST(get_getInsult_Test, getInsult){
ASSERT_EQ(typeid(char*), typeid(getInsult()));
}

TEST(word_list_init, createWordList){
TEST(create_createWordList_Test, createWordList){
WordList *list = createWordList();
ASSERT_FALSE(list == NULL);
ASSERT_TRUE(list->first == NULL);
@@ -22,13 +22,46 @@ TEST(word_list_init, createWordList){
ASSERT_TRUE(list->size == 0);
}

TEST(word_list_add, addWordList){
TEST(add_addBegin__Test, addBeginWordList){
WordList *list = createWordList();
char test1[] = "Test1";
char test2[] = "Test2";
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->first != NULL);
ASSERT_TRUE(list->last != NULL);
@@ -37,14 +70,14 @@ TEST(word_list_add, addWordList){
ASSERT_FALSE(strcmp(list->first->word, test1));
ASSERT_FALSE(list->first->word == test1);

addWordList(list, test2);
addEndWordList(list, test2);
ASSERT_TRUE(list->size == 2);
ASSERT_TRUE(list->first != list->last);
ASSERT_FALSE(strcmp(list->last->word, test2));
ASSERT_FALSE(strcmp(list->first->word, test1));
ASSERT_TRUE(strcmp(list->first->word, test2));

addWordList(list, test3);
addEndWordList(list, test3);
ASSERT_TRUE(list->size == 3);
ASSERT_TRUE(list->first != list->last);
ASSERT_FALSE(strcmp(list->first->word, test1));
@@ -54,13 +87,13 @@ TEST(word_list_add, addWordList){
freeWordList(list);
}

TEST(word_list_remove, removeWordList){
TEST(remove_removeWordList_Test, removeWordList){
WordList *list = createWordList();
char word[] = "lel";

addWordList(list, word);
addWordList(list, word);
addWordList(list, word);
addEndWordList(list, word);
addEndWordList(list, word);
addEndWordList(list, word);

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();
char word[] = "lel";
addWordList(list, word);
addWordList(list, word);
addWordList(list, word);
addEndWordList(list, word);
addEndWordList(list, word);
addEndWordList(list, word);

freeWordList(list);

@@ -90,7 +123,7 @@ TEST(word_list_free, freeWordList){
freeWordList(list);
}

TEST(word_list_to_word_array, wordListToWordArray){
TEST(word_wordListToWordArray_Test, wordListToWordArray){
WordList *list = createWordList();
WordListElement *elem = NULL;
WordArray *array = NULL;
@@ -100,28 +133,28 @@ TEST(word_list_to_word_array, wordListToWordArray){
char test2[] = "Test2";
char test3[] = "Test3";

addWordList(list, test1);
addWordList(list, test2);
addWordList(list, test3);
addWordList(list, test2);
addWordList(list, test2);
addWordList(list, test3);
addWordList(list, test1);
addWordList(list, test1);
addEndWordList(list, test1);
addEndWordList(list, test2);
addEndWordList(list, test3);
addEndWordList(list, test2);
addEndWordList(list, test2);
addEndWordList(list, test3);
addEndWordList(list, test1);
addEndWordList(list, test1);

array = wordListToWordArray(list);
ASSERT_FALSE(array == NULL);
ASSERT_FALSE(array->words == NULL);

list = createWordList();
addWordList(list, test1);
addWordList(list, test2);
addWordList(list, test3);
addWordList(list, test2);
addWordList(list, test2);
addWordList(list, test3);
addWordList(list, test1);
addWordList(list, test1);
addEndWordList(list, test1);
addEndWordList(list, test2);
addEndWordList(list, test3);
addEndWordList(list, test2);
addEndWordList(list, test2);
addEndWordList(list, test3);
addEndWordList(list, test1);
addEndWordList(list, test1);

ASSERT_EQ(list->size, array->size);

@@ -138,7 +171,7 @@ TEST(word_list_to_word_array, wordListToWordArray){
freeWordArray(array);
}

TEST(word_array_to_word_list, wordArrayToWordList){
TEST(word_wordArrayToWordList_Test, wordArrayToWordList){
WordList *list = createWordList();
WordList *list2 = NULL;
WordListElement *elem = NULL;
@@ -149,27 +182,27 @@ TEST(word_array_to_word_list, wordArrayToWordList){
char test2[] = "Test2";
char test3[] = "Test3";

addWordList(list, test1);
addWordList(list, test2);
addWordList(list, test3);
addWordList(list, test2);
addWordList(list, test2);
addWordList(list, test3);
addWordList(list, test1);
addWordList(list, test1);
addEndWordList(list, test1);
addEndWordList(list, test2);
addEndWordList(list, test3);
addEndWordList(list, test2);
addEndWordList(list, test2);
addEndWordList(list, test3);
addEndWordList(list, test1);
addEndWordList(list, test1);

array = wordListToWordArray(list);
list2 = wordArrayToWordList(array);

list = createWordList();
addWordList(list, test1);
addWordList(list, test2);
addWordList(list, test3);
addWordList(list, test2);
addWordList(list, test2);
addWordList(list, test3);
addWordList(list, test1);
addWordList(list, test1);
addEndWordList(list, test1);
addEndWordList(list, test2);
addEndWordList(list, test3);
addEndWordList(list, test2);
addEndWordList(list, test2);
addEndWordList(list, test3);
addEndWordList(list, test1);
addEndWordList(list, test1);
array = wordListToWordArray(list);

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();
WordArray *array = NULL;

@@ -195,16 +228,34 @@ TEST(word_array_free, freeWordArray){
char test2[] = "Test2";
char test3[] = "Test3";

addWordList(list, test1);
addWordList(list, test2);
addWordList(list, test3);
addWordList(list, test2);
addWordList(list, test2);
addWordList(list, test3);
addWordList(list, test1);
addWordList(list, test1);
addEndWordList(list, test1);
addEndWordList(list, test2);
addEndWordList(list, test3);
addEndWordList(list, test2);
addEndWordList(list, test2);
addEndWordList(list, test3);
addEndWordList(list, test1);
addEndWordList(list, test1);

array = wordListToWordArray(list);

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

Loading…
Cancel
Save