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:
9 9
   # instead of calling g++ directly you can also use some build toolkit like make
10 10
   # install the necessary build tools when needed
11 11
   before_script:
12
-    - apt update && apt -y install make cmake python
12
+    - apt update && apt -y install make cmake python libpcre3 libpcre3-dev
13 13
   script: 
14 14
     - mkdir build && cd build && cmake .. && make && make test
15 15
   #artifacts:

+ 1
- 1
CMakeLists.txt View File

@@ -48,6 +48,7 @@ include_directories(
48 48
 set(CMAKE_C_STANDARD 99)
49 49
 set(CMAKE_C_FLAGS "-Wall -Werror -pedantic -fpic -Wextra -Wshadow")
50 50
 set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FALGS} -g")
51
+set(CMAKE_EXE_LINKER_FLAGS "-lpcre")
51 52
 
52 53
 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)
53 54
 add_executable(fish ${SOURCE_FILES})
@@ -56,7 +57,6 @@ add_executable(fish ${SOURCE_FILES})
56 57
 
57 58
 add_subdirectory(fish_shell_tests)
58 59
 
59
-
60 60
 enable_testing()
61 61
 
62 62
 add_test(NAME fish_tests

+ 8
- 1
fish_shell/fish_commands.c View File

@@ -9,13 +9,14 @@
9 9
 
10 10
 /* Necessary global variables */
11 11
 char * builtinCommandsStr[] = {
12
+		(char *) "kek",
12 13
 		(char *) "cd",
13 14
 		(char *) "help",
14 15
 		(char *) "exit"
15 16
 };
16 17
 
17
-
18 18
 builtinCommand *builtinCommands[] = {
19
+		&fishKek,
19 20
 		&fishCd,
20 21
 		&fishHelp,
21 22
 		&fishExit
@@ -60,6 +61,12 @@ int fishExit(WordArray *args) {
60 61
 
61 62
 }
62 63
 
64
+int fishKek(WordArray *args) {
65
+	freeWordArray(args);
66
+	printf("Praise kek !");
67
+	return EXIT_SUCCESS;
68
+}
69
+
63 70
 int getNbBuiltins() {
64 71
 	return sizeof(builtinCommandsStr) / sizeof(char*);
65 72
 }

+ 2
- 0
fish_shell/fish_commands.h View File

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

+ 2
- 2
fish_shell/fish_core.c View File

@@ -58,7 +58,7 @@ WordList * split(char *string, char *separator){
58 58
 	}
59 59
 
60 60
 	while((token = strsep(&to_delete, separator)) != NULL)
61
-		addWordList(list, token);
61
+		addEndWordList(list, token);
62 62
 
63 63
 	free(to_delete_bak);
64 64
 
@@ -201,7 +201,7 @@ int loadRightCommand(WordArray *array){
201 201
 WordList * parseWordList(WordList *list, shell_operator *an_operator) {
202 202
 	char *op_str[] = {
203 203
 			(char*) ";",
204
-			(char*) "||",
204
+			(char*) "\\|\\|",
205 205
 			(char*) "&&"
206 206
 	};
207 207
 	shell_operator op[] = {

+ 75
- 18
fish_shell/fish_utils.c View File

@@ -6,6 +6,7 @@
6 6
 #include <stdlib.h>
7 7
 #include <string.h>
8 8
 #include <time.h>
9
+#include <pcre.h>
9 10
 #include "fish_utils.h"
10 11
 #include "fish_types.h"
11 12
 
@@ -70,20 +71,38 @@ WordList *createWordList() {
70 71
 	return list;
71 72
 }
72 73
 
73
-void addWordList(WordList *list, char *word) {
74
-	WordListElement *newElement = (WordListElement*) malloc(sizeof(WordListElement));
75
-	if (newElement == NULL) crash();
74
+void addEndWordList(WordList *list, char *word) {
75
+	WordListElement *new_element = (WordListElement*) malloc(sizeof(WordListElement));
76
+	if (new_element == NULL) crash();
76 77
 	else {
77
-		newElement->next = NULL;
78
-		newElement->previous = list->last;
78
+		new_element->next = NULL;
79
+		new_element->previous = list->last;
79 80
 		if (list->size == 0){
80
-			list->first = newElement;
81
-			list->last = newElement;
81
+			list->first = new_element;
82
+			list->last = new_element;
82 83
 		} else {
83
-			list->last->next = newElement;
84
-			list->last = newElement;
84
+			list->last->next = new_element;
85
+			list->last = new_element;
85 86
 		}
86
-		newElement->word = strdup(word);
87
+		new_element->word = strdup(word);
88
+		list->size++;
89
+	}
90
+}
91
+
92
+void addBeginWordList(WordList *list, char *word) {
93
+	WordListElement *new_element = (WordListElement*) malloc(sizeof(WordListElement));
94
+	if (new_element == NULL) crash();
95
+	else {
96
+		new_element->next = list->first;
97
+		new_element->previous = NULL;
98
+		if (list->size == 0){
99
+			list->first = new_element;
100
+			list->last = new_element;
101
+		} else {
102
+			list->first->previous = new_element;
103
+			list->first = new_element;
104
+		}
105
+		new_element->word = strdup(word);
87 106
 		list->size++;
88 107
 	}
89 108
 }
@@ -127,7 +146,7 @@ WordList *wordArrayToWordList(WordArray *array) {
127 146
 	int i;
128 147
 
129 148
 	for (i=0; i<array->size; i++)
130
-		addWordList(list, array->words[i]);
149
+		addEndWordList(list, array->words[i]);
131 150
 
132 151
 	freeWordArray(array);
133 152
 
@@ -176,7 +195,7 @@ WordList *sliceWordList(WordList *list, int min_index, int max_index) {
176 195
 
177 196
 			tmp = elem->next;
178 197
 			if (i >= min_index){
179
-				addWordList(newList, elem->word);
198
+				addEndWordList(newList, elem->word);
180 199
 				removeWordListElem(list, elem);
181 200
 			}
182 201
 
@@ -188,21 +207,59 @@ WordList *sliceWordList(WordList *list, int min_index, int max_index) {
188 207
 	return newList;
189 208
 }
190 209
 
191
-WordList *splitWordList(WordList *list, char *token) {
192
-	WordList * newList = NULL;
193
-	WordListElement * current = list->first;
210
+char * splitWord(char * origin, int beginning_index, int size_to_delete){
211
+	char * new_word = strdup(origin + beginning_index + size_to_delete);
212
+
213
+	if (new_word == NULL) crash();
214
+	else {
215
+		origin[beginning_index] = '\0';
216
+	}
217
+
218
+	return new_word;
219
+}
220
+
221
+WordList *splitWordList(WordList *list, char *regex) {
222
+	const char* error;
223
+	int error_offset;
224
+	int ovector[100];
194 225
 	int i = 0;
226
+	int rc;
227
+	WordList * new_list = NULL;
228
+	WordListElement * current = list->first;
229
+	pcre *re = pcre_compile(regex, 0, &error, &error_offset, 0);
230
+	char *tmp_word = NULL;
231
+
232
+	if (!re) crash();
195 233
 
196 234
 	while (current != NULL){
197
-		if (!strcmp(current->word, token)){
198
-			newList = sliceWordList(list, i + 1, list->size);
235
+		rc = pcre_exec(re,
236
+					   0,
237
+					   current->word,
238
+					   (int) strlen(current->word),
239
+					   0,
240
+					   0,
241
+					   ovector,
242
+					   sizeof(ovector)
243
+		);
244
+
245
+		if(rc >= 0){
246
+			new_list = sliceWordList(list, i + 1, list->size);
247
+			tmp_word = splitWord(current->word, ovector[0], ovector[1] - ovector[0]);
248
+
249
+			if (tmp_word[0] != '\0') addBeginWordList(new_list, tmp_word);
250
+			if (current->word[0] != '\0') addEndWordList(list, current->word);
251
+
199 252
 			removeWordListElem(list, current);
200 253
 			current = NULL;
254
+			free(tmp_word);
201 255
 		}
256
+
202 257
 		else current = current->next;
203 258
 		i++;
204 259
 	}
205 260
 
206
-	return newList;
261
+	pcre_free(re);
262
+
263
+	return new_list;
207 264
 }
208 265
 

+ 6
- 2
fish_shell/fish_utils.h View File

@@ -17,7 +17,9 @@ WordList * createWordList(); // Tested
17 17
 
18 18
 WordList * sliceWordList(WordList *list, int min_index, int max_index);
19 19
 
20
-void addWordList(WordList *list, char *word); // Tested
20
+void addEndWordList(WordList *list, char *word); // Tested
21
+
22
+void addBeginWordList(WordList *list, char *word);
21 23
 
22 24
 void removeWordList(WordList *list); // Tested
23 25
 
@@ -29,6 +31,8 @@ WordArray * wordListToWordArray(WordList *list); // Tested
29 31
 
30 32
 WordList * wordArrayToWordList(WordArray * array); // Tested
31 33
 
32
-WordList * splitWordList(WordList *list, char * tokens);
34
+WordList * splitWordList(WordList *list, char *regex);
35
+
36
+char * splitWord(char * origin, int beginning_index, int size_to_delete); // Tested
33 37
 
34 38
 #endif //FISH_FISH_UTILS_H

+ 109
- 58
fish_shell_tests/FishUtilsTests.cpp View File

@@ -6,15 +6,15 @@
6 6
 #include "../fish_shell/fish_utils.h"
7 7
 #include "../fish_shell/fish_types.h"
8 8
 
9
-TEST(crash_test, crash){
9
+TEST(crash_crash_Test, crash){
10 10
 	ASSERT_DEATH(crash(), "[A-z]+");
11 11
 }
12 12
 
13
-TEST(get_an_insult, getInsult){
13
+TEST(get_getInsult_Test, getInsult){
14 14
 	ASSERT_EQ(typeid(char*), typeid(getInsult()));
15 15
 }
16 16
 
17
-TEST(word_list_init, createWordList){
17
+TEST(create_createWordList_Test, createWordList){
18 18
 	WordList *list = createWordList();
19 19
 	ASSERT_FALSE(list == NULL);
20 20
 	ASSERT_TRUE(list->first == NULL);
@@ -22,13 +22,46 @@ TEST(word_list_init, createWordList){
22 22
 	ASSERT_TRUE(list->size == 0);
23 23
 }
24 24
 
25
-TEST(word_list_add, addWordList){
25
+TEST(add_addBegin__Test, addBeginWordList){
26 26
 	WordList *list = createWordList();
27 27
 	char test1[] = "Test1";
28 28
 	char test2[] = "Test2";
29 29
 	char test3[] = "Test3";
30 30
 
31
-	addWordList(list, test1);
31
+	addBeginWordList(list, test1);
32
+	ASSERT_EQ(list->size, 1);
33
+	ASSERT_FALSE(list->first == NULL);
34
+	ASSERT_FALSE(list->last == NULL);
35
+	ASSERT_EQ(list->first, list->last);
36
+	ASSERT_FALSE(list->first->word == NULL);
37
+	ASSERT_STREQ(list->first->word, test1);
38
+	ASSERT_FALSE(list->first->word == test1);
39
+
40
+	addBeginWordList(list, test2);
41
+	ASSERT_EQ(list->size, 2);
42
+	ASSERT_FALSE(list->first == list->last);
43
+	ASSERT_STREQ(list->first->word, test2);
44
+	ASSERT_STREQ(list->last->word, test1);
45
+	ASSERT_STRNE(list->first->word, test1);
46
+
47
+	addBeginWordList(list, test3);
48
+	ASSERT_EQ(list->size, 3);
49
+	ASSERT_FALSE(list->first == list->last);
50
+	ASSERT_STREQ(list->last->word, test1);
51
+	ASSERT_STREQ(list->first->next->word, test2);
52
+	ASSERT_STREQ(list->first->word, test3);
53
+
54
+	freeWordList(list);
55
+
56
+}
57
+
58
+TEST(add_addEndWordList_Test, addEndWordList){
59
+	WordList *list = createWordList();
60
+	char test1[] = "Test1";
61
+	char test2[] = "Test2";
62
+	char test3[] = "Test3";
63
+
64
+	addEndWordList(list, test1);
32 65
 	ASSERT_TRUE(list->size == 1);
33 66
 	ASSERT_TRUE(list->first != NULL);
34 67
 	ASSERT_TRUE(list->last != NULL);
@@ -37,14 +70,14 @@ TEST(word_list_add, addWordList){
37 70
 	ASSERT_FALSE(strcmp(list->first->word, test1));
38 71
 	ASSERT_FALSE(list->first->word == test1);
39 72
 
40
-	addWordList(list, test2);
73
+	addEndWordList(list, test2);
41 74
 	ASSERT_TRUE(list->size == 2);
42 75
 	ASSERT_TRUE(list->first != list->last);
43 76
 	ASSERT_FALSE(strcmp(list->last->word, test2));
44 77
 	ASSERT_FALSE(strcmp(list->first->word, test1));
45 78
 	ASSERT_TRUE(strcmp(list->first->word, test2));
46 79
 
47
-	addWordList(list, test3);
80
+	addEndWordList(list, test3);
48 81
 	ASSERT_TRUE(list->size == 3);
49 82
 	ASSERT_TRUE(list->first != list->last);
50 83
 	ASSERT_FALSE(strcmp(list->first->word, test1));
@@ -54,13 +87,13 @@ TEST(word_list_add, addWordList){
54 87
 	freeWordList(list);
55 88
 }
56 89
 
57
-TEST(word_list_remove, removeWordList){
90
+TEST(remove_removeWordList_Test, removeWordList){
58 91
 	WordList *list = createWordList();
59 92
 	char word[] = "lel";
60 93
 
61
-	addWordList(list, word);
62
-	addWordList(list, word);
63
-	addWordList(list, word);
94
+	addEndWordList(list, word);
95
+	addEndWordList(list, word);
96
+	addEndWordList(list, word);
64 97
 
65 98
 	ASSERT_TRUE(list->size == 3);
66 99
 
@@ -76,12 +109,12 @@ TEST(word_list_remove, removeWordList){
76 109
 
77 110
 }
78 111
 
79
-TEST(word_list_free, freeWordList){
112
+TEST(free_freeWordList_Test, freeWordList){
80 113
 	WordList *list = createWordList();
81 114
 	char word[] = "lel";
82
-	addWordList(list, word);
83
-	addWordList(list, word);
84
-	addWordList(list, word);
115
+	addEndWordList(list, word);
116
+	addEndWordList(list, word);
117
+	addEndWordList(list, word);
85 118
 
86 119
 	freeWordList(list);
87 120
 
@@ -90,7 +123,7 @@ TEST(word_list_free, freeWordList){
90 123
 	freeWordList(list);
91 124
 }
92 125
 
93
-TEST(word_list_to_word_array, wordListToWordArray){
126
+TEST(word_wordListToWordArray_Test, wordListToWordArray){
94 127
 	WordList *list = createWordList();
95 128
 	WordListElement *elem = NULL;
96 129
 	WordArray *array = NULL;
@@ -100,28 +133,28 @@ TEST(word_list_to_word_array, wordListToWordArray){
100 133
 	char test2[] = "Test2";
101 134
 	char test3[] = "Test3";
102 135
 
103
-	addWordList(list, test1);
104
-	addWordList(list, test2);
105
-	addWordList(list, test3);
106
-	addWordList(list, test2);
107
-	addWordList(list, test2);
108
-	addWordList(list, test3);
109
-	addWordList(list, test1);
110
-	addWordList(list, test1);
136
+	addEndWordList(list, test1);
137
+	addEndWordList(list, test2);
138
+	addEndWordList(list, test3);
139
+	addEndWordList(list, test2);
140
+	addEndWordList(list, test2);
141
+	addEndWordList(list, test3);
142
+	addEndWordList(list, test1);
143
+	addEndWordList(list, test1);
111 144
 
112 145
 	array = wordListToWordArray(list);
113 146
 	ASSERT_FALSE(array == NULL);
114 147
 	ASSERT_FALSE(array->words == NULL);
115 148
 
116 149
 	list = createWordList();
117
-	addWordList(list, test1);
118
-	addWordList(list, test2);
119
-	addWordList(list, test3);
120
-	addWordList(list, test2);
121
-	addWordList(list, test2);
122
-	addWordList(list, test3);
123
-	addWordList(list, test1);
124
-	addWordList(list, test1);
150
+	addEndWordList(list, test1);
151
+	addEndWordList(list, test2);
152
+	addEndWordList(list, test3);
153
+	addEndWordList(list, test2);
154
+	addEndWordList(list, test2);
155
+	addEndWordList(list, test3);
156
+	addEndWordList(list, test1);
157
+	addEndWordList(list, test1);
125 158
 
126 159
 	ASSERT_EQ(list->size, array->size);
127 160
 
@@ -138,7 +171,7 @@ TEST(word_list_to_word_array, wordListToWordArray){
138 171
 	freeWordArray(array);
139 172
 }
140 173
 
141
-TEST(word_array_to_word_list, wordArrayToWordList){
174
+TEST(word_wordArrayToWordList_Test, wordArrayToWordList){
142 175
 	WordList *list = createWordList();
143 176
 	WordList *list2 = NULL;
144 177
 	WordListElement *elem = NULL;
@@ -149,27 +182,27 @@ TEST(word_array_to_word_list, wordArrayToWordList){
149 182
 	char test2[] = "Test2";
150 183
 	char test3[] = "Test3";
151 184
 
152
-	addWordList(list, test1);
153
-	addWordList(list, test2);
154
-	addWordList(list, test3);
155
-	addWordList(list, test2);
156
-	addWordList(list, test2);
157
-	addWordList(list, test3);
158
-	addWordList(list, test1);
159
-	addWordList(list, test1);
185
+	addEndWordList(list, test1);
186
+	addEndWordList(list, test2);
187
+	addEndWordList(list, test3);
188
+	addEndWordList(list, test2);
189
+	addEndWordList(list, test2);
190
+	addEndWordList(list, test3);
191
+	addEndWordList(list, test1);
192
+	addEndWordList(list, test1);
160 193
 
161 194
 	array = wordListToWordArray(list);
162 195
 	list2 = wordArrayToWordList(array);
163 196
 
164 197
 	list = createWordList();
165
-	addWordList(list, test1);
166
-	addWordList(list, test2);
167
-	addWordList(list, test3);
168
-	addWordList(list, test2);
169
-	addWordList(list, test2);
170
-	addWordList(list, test3);
171
-	addWordList(list, test1);
172
-	addWordList(list, test1);
198
+	addEndWordList(list, test1);
199
+	addEndWordList(list, test2);
200
+	addEndWordList(list, test3);
201
+	addEndWordList(list, test2);
202
+	addEndWordList(list, test2);
203
+	addEndWordList(list, test3);
204
+	addEndWordList(list, test1);
205
+	addEndWordList(list, test1);
173 206
 	array = wordListToWordArray(list);
174 207
 
175 208
 	ASSERT_TRUE(list2 != NULL);
@@ -187,7 +220,7 @@ TEST(word_array_to_word_list, wordArrayToWordList){
187 220
 
188 221
 }
189 222
 
190
-TEST(word_array_free, freeWordArray){
223
+TEST(free_freeWordArray_Test, freeWordArray){
191 224
 	WordList *list = createWordList();
192 225
 	WordArray *array = NULL;
193 226
 
@@ -195,16 +228,34 @@ TEST(word_array_free, freeWordArray){
195 228
 	char test2[] = "Test2";
196 229
 	char test3[] = "Test3";
197 230
 
198
-	addWordList(list, test1);
199
-	addWordList(list, test2);
200
-	addWordList(list, test3);
201
-	addWordList(list, test2);
202
-	addWordList(list, test2);
203
-	addWordList(list, test3);
204
-	addWordList(list, test1);
205
-	addWordList(list, test1);
231
+	addEndWordList(list, test1);
232
+	addEndWordList(list, test2);
233
+	addEndWordList(list, test3);
234
+	addEndWordList(list, test2);
235
+	addEndWordList(list, test2);
236
+	addEndWordList(list, test3);
237
+	addEndWordList(list, test1);
238
+	addEndWordList(list, test1);
206 239
 
207 240
 	array = wordListToWordArray(list);
208 241
 
209 242
 	freeWordArray(array);
243
+}
244
+
245
+TEST(split_splitWord__Test, slpitWord){
246
+	char test1[] = "ls&&ls";
247
+	char test2[] = "&&";
248
+	char *splited = splitWord(test1, 2, 2);
249
+
250
+	ASSERT_STREQ(test1, "ls");
251
+	ASSERT_STREQ(splited, "ls");
252
+
253
+	free(splited);
254
+
255
+	splited = splitWord(test2, 0, 2);
256
+
257
+	ASSERT_STREQ(test2, "");
258
+	ASSERT_STREQ(splited, "");
259
+
260
+	free(splited);
210 261
 }

Loading…
Cancel
Save