Merge branch 'sli' into 'master'

0 leak memory :D

See merge request !5
This commit is contained in:
Antoine Bartuccio 2017-05-17 23:44:49 +00:00
commit 2ea166e43d
5 changed files with 35 additions and 22 deletions

View File

@ -44,8 +44,10 @@ include_directories(
"${source_dir}/googlemock/include" "${source_dir}/googlemock/include"
) )
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(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})

View File

@ -37,12 +37,12 @@ int fishCd(WordArray *args) {
perror("fish"); perror("fish");
} }
} }
return 0; freeWordArray(args);
return EXIT_SUCCESS;
} }
int fishHelp(WordArray *args) { int fishHelp(WordArray *args) {
int i; int i;
args->size = args->size;
printf("Bartuccio Antoine, Amalvy Arthur, Yann Chevanton\n"); printf("Bartuccio Antoine, Amalvy Arthur, Yann Chevanton\n");
printf("Tape tes putains de noms de programmes et tes arguments de merde et tabasse ENTER !\n"); printf("Tape tes putains de noms de programmes et tes arguments de merde et tabasse ENTER !\n");
printf("Les commandes suivantes sont internes :\n"); printf("Les commandes suivantes sont internes :\n");
@ -50,12 +50,13 @@ int fishHelp(WordArray *args) {
printf("\t%s\n", builtinCommandsStr[i]); printf("\t%s\n", builtinCommandsStr[i]);
} }
printf("Et sinon pour le reste, RTFM !"); printf("Et sinon pour le reste, RTFM !");
return 0; freeWordArray(args);
return EXIT_SUCCESS;
} }
int fishExit(WordArray *args) { int fishExit(WordArray *args) {
args->size = args->size; freeWordArray(args);
exit(EXIT_SUCCESS); return EXIT_SIGNAL;
} }

View File

@ -20,14 +20,14 @@ void fishLoop(Settings * settings){
printf("%s", settings->PS1); printf("%s", settings->PS1);
line = fishReadLine(); line = fishReadLine();
splited = split(line, (char*) FISH_TOKENS); splited = split(line, (char*) FISH_TOKENS);
splited = fishExpand(splited); splited = fishExpand(splited);
status = fishExecute(splited); status = fishExecute(splited);
free(line); free(line);
} while(status); } while(status != EXIT_SIGNAL);
} }
int countSeparators(char *string, char *separators) { int countSeparators(char *string, char *separators) {
@ -145,6 +145,7 @@ int fishLoad(WordArray *array) {
} while (!WIFEXITED(status) && !WIFSIGNALED(status)); } while (!WIFEXITED(status) && !WIFSIGNALED(status));
if (status) fprintf(stderr, "%s\n", getInsult()); if (status) fprintf(stderr, "%s\n", getInsult());
} }
freeWordArray(array);
return status; return status;
} }
@ -152,24 +153,32 @@ int fishExecute(WordList *list) {
WordList *splited = NULL; WordList *splited = NULL;
shell_operator op = NONE; shell_operator op = NONE;
WordArray *array = NULL; WordArray *array = NULL;
int signal = 1;
splited = parseWordList(list, &op); splited = parseWordList(list, &op);
array = wordListToWordArray(list); array = wordListToWordArray(list);
signal = loadRightCommand(array);
if (signal == EXIT_SIGNAL){
if (splited != NULL) freeWordList(splited);
if (array != NULL) freeWordArray(array);
return signal;
}
switch (op) { switch (op) {
case AND: case AND:
if (!loadRightCommand(array)) fishExecute(splited); if (!signal) signal = fishExecute(splited);
else freeWordList(splited); else {
if (splited != NULL) freeWordList(splited);
}
break; break;
case OR: case OR:
loadRightCommand(array); signal = fishExecute(splited);
fishExecute(splited);
break; break;
default: default:
loadRightCommand(array); break;
} }
return signal;
return 1;
} }
int loadRightCommand(WordArray *array){ int loadRightCommand(WordArray *array){
@ -185,12 +194,12 @@ 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*) "&&"
}; };
shell_operator op[] = { shell_operator op[] = {
AND, OR,
OR AND
}; };
WordList *newList = NULL; WordList *newList = NULL;
int max = sizeof(op_str) / sizeof(char*); int max = sizeof(op_str) / sizeof(char*);

View File

@ -5,6 +5,8 @@
#ifndef FISH_FISH_TYPES_H #ifndef FISH_FISH_TYPES_H
#define FISH_FISH_TYPES_H #define FISH_FISH_TYPES_H
#define EXIT_SIGNAL -100
/* Custom types */ /* Custom types */
typedef enum { typedef enum {

View File

@ -30,7 +30,7 @@ char *getInsult(){
int picked = 0; int picked = 0;
char *insults[] = { char *insults[] = {
(char *) "Apprend à écrire crétin !", (char *) "Apprend à écrire crétin !",
(char *) "Bolos !", (char *) "Boloss !",
(char *) "Mois aussi je sais écrire de la merde, pourtant je le fait pas !", (char *) "Mois aussi je sais écrire de la merde, pourtant je le fait pas !",
(char *) "Oh ! Une erreur ! Comme ta vie en fait...", (char *) "Oh ! Une erreur ! Comme ta vie en fait...",
(char *) "Nul !", (char *) "Nul !",
@ -110,12 +110,11 @@ WordArray *wordListToWordArray(WordList *list) {
if (array->words == NULL) crash(); if (array->words == NULL) crash();
while (current != NULL){ while (current != NULL){
array->words[i] = current->word; array->words[i] = strdup(current->word);
current->word = NULL;
current = current->next; current = current->next;
i++; i++;
} }
array->words[i] = NULL; array->words[array->size] = NULL;
} }
freeWordList(list); freeWordList(list);
@ -125,7 +124,7 @@ WordArray *wordListToWordArray(WordList *list) {
WordList *wordArrayToWordList(WordArray *array) { WordList *wordArrayToWordList(WordArray *array) {
WordList *list = createWordList(); WordList *list = createWordList();
int i = 0; int i;
for (i=0; i<array->size; i++) for (i=0; i<array->size; i++)
addWordList(list, array->words[i]); addWordList(list, array->words[i]);