diff --git a/CLI/CLI.c b/CLI/CLI.c index 0d12646..c474ec8 100644 --- a/CLI/CLI.c +++ b/CLI/CLI.c @@ -6,7 +6,7 @@ #include // #include -#define HISTORY 1 +#define HISTORY #include "../FIFOBuff/FIFOBuffChar.h" #ifdef HISTORY @@ -69,7 +69,7 @@ bool CLI_deinit() FIFOBuffChar_delete(FIFO); #ifdef HISTORY - History_deinit(History); + History_deinit(History, true); #endif return true; } diff --git a/CMDList/CMDList.c b/CMDList/CMDList.c index d785fd3..d24b445 100644 --- a/CMDList/CMDList.c +++ b/CMDList/CMDList.c @@ -8,14 +8,14 @@ // initilises a CMDList_t with all NULL pointers CMDList_t* CMDList_init() { - CMDList_t *list = malloc(sizeof(CMDList_t)); + CMDList_t* list = malloc(sizeof(CMDList_t)); memset(list, 0, sizeof(CMDList_t)); return list; } // free up the full tree from memory // does not free up the commands -int CMDList_deinit(CMDList_t *list) +int CMDList_deinit(CMDList_t* list) { CMDList_t** list_p = (CMDList_t**)list; @@ -28,7 +28,7 @@ int CMDList_deinit(CMDList_t *list) } } - free(*list_p); + free(list); return 0; } @@ -44,7 +44,7 @@ int CMDList_add(CMDList_t *list, CMD_t* cmd, char* cmdName) read_p = cmd->cmd; } - while (returnCode == 0) + while (returnCode >= 0) { char c = *read_p & (~0x20); // convert to uppercase if ((c >= 'A') && (c <= 'Z')) diff --git a/CMakeLists.txt b/CMakeLists.txt index ec5cf73..0905f99 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ add_library(history History/History.c) # CLI add_library(CLI CLI/CLI.c) -target_link_libraries(CLI CMDList FIFOBuffChar) +target_link_libraries(CLI CMDList FIFOBuffChar history) add_library(CLI_History CLI/CLI.c) target_link_libraries(CLI_History CMDList FIFOBuffChar history) diff --git a/History/History.c b/History/History.c index 4a27b61..bc52e05 100644 --- a/History/History.c +++ b/History/History.c @@ -4,8 +4,6 @@ #include #include -int i = 0; - History_t* History_init() { History_t* history = malloc(sizeof(History_t)); @@ -21,7 +19,7 @@ History_t* History_init() } -int History_deinit(History_t* history) +int History_deinit(History_t* history, bool freeLines) { // Check if the buffer there is something in the buffer if (history->FirstEl_p == NULL) @@ -31,6 +29,10 @@ int History_deinit(History_t* history) while (el != NULL) { History_element_t* nextEl = el->nextEl_p; + if (freeLines) + { + free(el->line); + } free(el); el = nextEl; } @@ -151,6 +153,7 @@ int History_getFullHistory(History_t* history, char*** list) *list = malloc(sizeof(char**) * (History_getSize(history) + 1)); History_element_t* el = history->FirstEl_p; + int i; for (i = 0; (i < History_getSize(history)-1) && (retCode >= 0); i++) { *((*list) + i) = el->line; @@ -180,6 +183,7 @@ int History_getCurrPos(History_t* history) if (history->CurrEl_p != NULL) { History_element_t* el = history->FirstEl_p; + int i; for (i = 0; (el != history->CurrEl_p) && (retCode >= 0); i++) { if (el->nextEl_p == NULL) diff --git a/History/history.h b/History/history.h index 35bc020..04e2a8d 100644 --- a/History/history.h +++ b/History/history.h @@ -22,7 +22,7 @@ typedef struct History_s { extern History_t* History_init(); // destroy a fifo buffer (free up its space) -extern int History_deinit(History_t* history); +extern int History_deinit(History_t* history, bool freeLines); // put value i in buffer if there is still memory avaliable extern int History_put(History_t *history, char* line);