From 774161d7ff30327c88be9bd39b87df2c7668b25b Mon Sep 17 00:00:00 2001 From: klmp200 Date: Tue, 16 May 2017 22:14:06 +0200 Subject: [PATCH] =?UTF-8?q?Optimisation=20de=20m=C3=A9moire=20et=20correct?= =?UTF-8?q?ion=20de=20l'orde=20des=20op=C3=A9rateurs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fish_shell/fish_commands.c | 2 +- fish_shell/fish_core.c | 32 ++++++++++++++++++++------------ fish_shell/fish_types.h | 2 ++ 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/fish_shell/fish_commands.c b/fish_shell/fish_commands.c index 1b64373..039d684 100644 --- a/fish_shell/fish_commands.c +++ b/fish_shell/fish_commands.c @@ -55,7 +55,7 @@ int fishHelp(WordArray *args) { int fishExit(WordArray *args) { args->size = args->size; - exit(EXIT_SUCCESS); + return EXIT_SIGNAL; } diff --git a/fish_shell/fish_core.c b/fish_shell/fish_core.c index 48dac22..b473d30 100644 --- a/fish_shell/fish_core.c +++ b/fish_shell/fish_core.c @@ -20,14 +20,14 @@ void fishLoop(Settings * settings){ printf("%s", settings->PS1); line = fishReadLine(); - splited = split(line, (char*) FISH_TOKENS); + splited = split(line, (char*) FISH_TOKENS); splited = fishExpand(splited); status = fishExecute(splited); free(line); - } while(status); + } while(status != EXIT_SIGNAL); } int countSeparators(char *string, char *separators) { @@ -152,24 +152,32 @@ int fishExecute(WordList *list) { WordList *splited = NULL; shell_operator op = NONE; WordArray *array = NULL; + int signal = 1; splited = parseWordList(list, &op); array = wordListToWordArray(list); + signal = loadRightCommand(array); + if (signal == EXIT_SIGNAL){ + if (splited != NULL) freeWordList(splited); + if (array != NULL) freeWordArray(array); + return signal; + } switch (op) { case AND: - if (!loadRightCommand(array)) fishExecute(splited); - else freeWordList(splited); + if (!signal) signal = fishExecute(splited); + else { + if (splited != NULL) freeWordList(splited); + } break; case OR: - loadRightCommand(array); - fishExecute(splited); + signal = fishExecute(splited); break; default: - loadRightCommand(array); + break; } + return signal; - return 1; } int loadRightCommand(WordArray *array){ @@ -185,12 +193,12 @@ int loadRightCommand(WordArray *array){ WordList * parseWordList(WordList *list, shell_operator *an_operator) { char *op_str[] = { - (char*) "&&", - (char*) "||" + (char*) "||", + (char*) "&&" }; shell_operator op[] = { - AND, - OR + OR, + AND }; WordList *newList = NULL; int max = sizeof(op_str) / sizeof(char*); diff --git a/fish_shell/fish_types.h b/fish_shell/fish_types.h index 59afdb7..860efa5 100644 --- a/fish_shell/fish_types.h +++ b/fish_shell/fish_types.h @@ -5,6 +5,8 @@ #ifndef FISH_FISH_TYPES_H #define FISH_FISH_TYPES_H +#define EXIT_SIGNAL -100 + /* Custom types */ typedef enum {