From e267e6f9077d2779041fb0e301536fcdc945fc9b Mon Sep 17 00:00:00 2001 From: FReenen Date: Sat, 13 Apr 2024 15:21:43 +0200 Subject: [PATCH 01/11] simplify some code and fix tiny bugs --- CLI/CLI.c | 72 +++++++++++++++++++++++++------------------------------ 1 file changed, 33 insertions(+), 39 deletions(-) diff --git a/CLI/CLI.c b/CLI/CLI.c index 62d5f74..20dd15a 100644 --- a/CLI/CLI.c +++ b/CLI/CLI.c @@ -10,6 +10,24 @@ CLI_charOutFn CLI_charOut; CMDList_t* CMDList; FIFOBuffChar_t* FIFO; +void CLI_charOut_save(char ch) +{ + if (CLI_charOut != NULL) + { + // create string of size one to be compatable with string print function + char c[2] = {ch, 0}; + (*CLI_charOut)(&c[0]); + } +} + +void CLI_stringOut(char* str) +{ + for (; *str != 0; str++) + { + CLI_charOut_save(*str); + } +} + // initilize and register the lineout print function bool CLI_init(CLI_charOutFn lineOut, CMDList_t* cmdList) { @@ -20,8 +38,7 @@ bool CLI_init(CLI_charOutFn lineOut, CMDList_t* cmdList) if (CLI_charOut != NULL) { - (*CLI_charOut)(">"); - (*CLI_charOut)(" "); + CLI_stringOut((char*)"> "); } return true; @@ -61,11 +78,7 @@ int tryExecute(FIFOBuffChar_t* fifo) { (*(cmd->fn))(line); - if (CLI_charOut != NULL) - { - (*CLI_charOut)(">"); - (*CLI_charOut)(" "); - } + CLI_stringOut((char*)"> "); return true; } else @@ -74,11 +87,7 @@ int tryExecute(FIFOBuffChar_t* fifo) { char err[100]; sprintf(&err[0], "command not found: %s\n> ", line); - for (int i=0; err[i] != 0; i++) - { - char c[2] = {err[i], 0}; - (*CLI_charOut)(&c[0]); - } + CLI_stringOut(&err[0]); } return false; } @@ -88,24 +97,22 @@ int tryExecute(FIFOBuffChar_t* fifo) bool CLI_charIn(char c) { bool ok = true; - if ((c >= 'a') && (c <= 'z')) + char C = c; + if ((C >= 'a') && (C <= 'z')) { - c &= (~0x20); // convert to uppercase + C &= (~0x20); // convert to uppercase } if ( //TODO: update list of accepted characters - ((c >= 'A') && (c <= 'Z')) + ((C >= 'A') && (C <= 'Z')) || ((c >= '0') && (c <= '9')) || (c == ' ') || (c == '-') || (c == '_') ) { - FIFOBuffChar_put(FIFO, c); // save char in buffer - if (CLI_charOut != NULL) - { // echo to terminal - (*CLI_charOut)(&c); - } + FIFOBuffChar_put(FIFO, C); // save char in buffer + CLI_charOut_save(c); } else { @@ -116,7 +123,8 @@ bool CLI_charIn(char c) case '\r': if (CLI_charOut != NULL) { // echo to terminal - (*CLI_charOut)(&c); + char ch[2] = {c, 0}; + (*CLI_charOut)(&ch[0]); } FIFOBuffChar_t* fifo = FIFO; FIFO = FIFOBuffChar_create(); @@ -126,33 +134,19 @@ bool CLI_charIn(char c) case 127: // backspace if (FIFOBuffChar_pop(FIFO)) - { - (*CLI_charOut)("\x1b"); - (*CLI_charOut)("["); - (*CLI_charOut)("D"); - (*CLI_charOut)(" "); - (*CLI_charOut)("\x1b"); - (*CLI_charOut)("["); - (*CLI_charOut)("D"); + { // pop something of the buffer + CLI_stringOut((char*)"\x1b[D \x1b[D"); // "" } break; case 27: // escape (start for arrow keys) sprintf(&str[0], "\ninvlid char: ESC - (%i)\n", c); - for (int i=0; str[i] != 0; i++) - { - char ch[2] = {str[i], 0}; - (*CLI_charOut)(&ch[0]); - } + CLI_stringOut(&str[0]); break; default: sprintf(&str[0], "\ninvlid char: '%c' - (%i)\n", c, c); - for (int i=0; str[i] != 0; i++) - { - char ch[2] = {str[i], 0}; - (*CLI_charOut)(&ch[0]); - } + CLI_stringOut(&str[0]); break; } } From dea1c8ae52ce15e05e482ffa5c01b6d7107eee7d Mon Sep 17 00:00:00 2001 From: FReenen Date: Sat, 13 Apr 2024 15:43:42 +0200 Subject: [PATCH 02/11] CLI: don't print an arrow on exit --- CLI/CLI.c | 8 ++++++-- CMDList/CMDList.h | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CLI/CLI.c b/CLI/CLI.c index 20dd15a..9803120 100644 --- a/CLI/CLI.c +++ b/CLI/CLI.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "../FIFOBuff/FIFOBuffChar.h" @@ -76,9 +77,12 @@ int tryExecute(FIFOBuffChar_t* fifo) if (cmd != NULL) { - (*(cmd->fn))(line); + int ret = (*(cmd->fn))(line); - CLI_stringOut((char*)"> "); + if (ret != INT_MIN) + { + CLI_stringOut((char*)"> "); + } return true; } else diff --git a/CMDList/CMDList.h b/CMDList/CMDList.h index e6d3bff..215ae41 100644 --- a/CMDList/CMDList.h +++ b/CMDList/CMDList.h @@ -3,7 +3,7 @@ typedef struct CMD_t { char* cmd; - void (*fn)(char* line); + int (*fn)(char* line); } CMD_t; typedef struct CMDList_s { From bbbc0c468354468db0e34819511730d6652d9669 Mon Sep 17 00:00:00 2001 From: FReenen Date: Sat, 13 Apr 2024 16:13:59 +0200 Subject: [PATCH 03/11] fix memory leaks --- CLI/CLI.c | 7 +++++-- CMDList/CMDList.c | 6 +----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/CLI/CLI.c b/CLI/CLI.c index 9803120..bee1891 100644 --- a/CLI/CLI.c +++ b/CLI/CLI.c @@ -72,6 +72,7 @@ char* fifoToString(FIFOBuffChar_t* fifo) int tryExecute(FIFOBuffChar_t* fifo) { + int ret = 0; char* line = fifoToString(fifo); CMD_t* cmd = CMDList_get(CMDList, line); @@ -83,7 +84,7 @@ int tryExecute(FIFOBuffChar_t* fifo) { CLI_stringOut((char*)"> "); } - return true; + ret = 0; } else { @@ -93,8 +94,10 @@ int tryExecute(FIFOBuffChar_t* fifo) sprintf(&err[0], "command not found: %s\n> ", line); CLI_stringOut(&err[0]); } - return false; + ret = -1; } + free(line); + return ret; } // to recive a single caracter diff --git a/CMDList/CMDList.c b/CMDList/CMDList.c index 19c8639..61547a8 100644 --- a/CMDList/CMDList.c +++ b/CMDList/CMDList.c @@ -18,20 +18,16 @@ CMDList_t* CMDList_init() int CMDList_deinit(CMDList_t *list) { CMDList_t** list_p = (CMDList_t**)list; - // printf("deinit %p\n", (void*)list); - // printf("deinit e %p\n", list->e); for (int i = 0; i < 26; i++) { if (*(list_p + i) != NULL) { - // printf("deinit %i\n", i); CMDList_deinit(*(list_p + i)); } } - //TODO: fix "free(): invalid pointer" - // free(list); + free(*list_p); return 0; } From c49ed49a0b11669facc84fcff7535a1ba9cce54f Mon Sep 17 00:00:00 2001 From: FReenen Date: Sat, 13 Apr 2024 16:52:52 +0200 Subject: [PATCH 04/11] CLI: fix return value for CLI_charIn --- CLI/CLI.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CLI/CLI.c b/CLI/CLI.c index bee1891..53fda27 100644 --- a/CLI/CLI.c +++ b/CLI/CLI.c @@ -135,7 +135,7 @@ bool CLI_charIn(char c) } FIFOBuffChar_t* fifo = FIFO; FIFO = FIFOBuffChar_create(); - ok = tryExecute(fifo); + ok = (tryExecute(fifo) == 0); FIFOBuffChar_delete(fifo); break; From a1fef05168e5216161bcf6b3932d1c4d6a864938 Mon Sep 17 00:00:00 2001 From: FReenen Date: Sat, 13 Apr 2024 17:37:49 +0200 Subject: [PATCH 05/11] create inital version of history --- History/History.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++ History/history.h | 36 ++++++++++++++ 2 files changed, 159 insertions(+) create mode 100644 History/History.c create mode 100644 History/history.h diff --git a/History/History.c b/History/History.c new file mode 100644 index 0000000..d95b3b4 --- /dev/null +++ b/History/History.c @@ -0,0 +1,123 @@ +#include "FIFOBuffChar.h" + +#include +#include +#include + +History_t* History_init() +{ + History_t* history = malloc(sizeof(FIFOBuffChar_t)); + + if (history != NULL) { + history->FirstEl_p = NULL; + history->LastEl_p = NULL; + history->size = 0; + } + + return history; +} + + +int History_deinit(History_t* history) +{ + // Check if the buffer there is something in the buffer + if (history->FirstEl_p == NULL) + { + // Delete all elements in the buffer + History_element_t* el = history->FirstEl_p; + while (el != NULL) + { + FIFOBuffChar_element_t* nextEl = el->nextEl; + free(el); + el = nextEl; + } + } + + free(history); + return 0; +} + +int History_put(History_t *history, char* line) +{ + int retCode; + if ((history->LastEl_p == NULL) && (history->FirstEl_p == NULL)) + { + // buffer is empty. add first element + history->FirstEl_p = malloc(sizeof(History_element_t)); + if (history->FirstEl_p != NULL) + { + history->FirstEl_p->line = line; + history->FirstEl_p->prev_p = NULL; + history->FirstEl_p->next_p = NULL; + + history->LastEl_p = history->FirstEl_p; + history->size = 1; + retCode = 0; + } + else + { // failt to allocate memory + retCode = -1; + } + } + else if ((history->LastEl_p != NULL) && (history->FirstEl_p != NULL)) + { + // add element to exsiting buffer + History_element_t* el = malloc(sizeof(History_element_t)); + if (el != NULL) + { + el->character = i; // "value" to "character" + el->PrevEl_p = history->LastEl_p; + el->nextEl_p = NULL; + history->LastEl_p->nextEl_p = el; + history->LastEl_p = el; + history->size++; + retCode = 0; + } + else + { // failt to allocate memory + retCode = -2; + } + } + else + { + retCode = -3; + } + + if (retCode >= 0) + { + // reset current pointer + history->currEl_p = NULL; + } + + return retCode; +} + +int History_getPrev(History_t* history, char** line) +{ + int retCode; + if (history->currEl_p != NULL) + { + history->currEl_p = history->LastEl_p; + } + else + { + history->currEl_p = history->currEl_p->PrevEl_p; + } + + if (history->currEl_p != NULL) + { + *line = history->currEl_p->line; + retCode = 0; + } + else + { + *line = NULL; + retCode = -1; + } + return 0; +} + +unsigned int History_getSize(History_t *history) +{ + return history->size; +} diff --git a/History/history.h b/History/history.h new file mode 100644 index 0000000..7a7ce3a --- /dev/null +++ b/History/history.h @@ -0,0 +1,36 @@ +#ifndef HISTORY_H +#define HISTORY_H + +#include + +// one element in the buffer +typedef struct History_element_s { + void* prevEl_p; + char* line; + void* nextEl_p; +} History_element_t; + +// defines all vars for the buffer to operate +typedef struct History_s { + History_element_t* FirstEl_p; + History_element_t* LastEl_p; + History_element_t* CurrEl_p; + unsigned char size; +} History_t; + +// create a fifo buffer and initilizes it with zeros +extern History_t* History_init(); + +// destroy a fifo buffer (free up its space) +extern int History_deinit(History_t* history); + +// put value i in buffer if there is still memory avaliable +extern int History_put(History_t *history, char* line); + +// get value from buffer and writes it to *line +extern int History_getPrev(History_t* history, char** line); +extern int History_getNext(History_t* history, char** line); + +extern unsigned int History_getSize(History_t *history); + +#endif \ No newline at end of file From e34fe9f2131b7a4c4d0f18ae7f346c8850bf66ab Mon Sep 17 00:00:00 2001 From: FReenen Date: Sat, 13 Apr 2024 18:03:13 +0200 Subject: [PATCH 06/11] History: add to cmake and fix errors --- CMakeLists.txt | 3 +++ History/History.c | 50 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 720145f..ee81a76 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,3 +9,6 @@ add_library(CMDListPrint CMDList/printList.c) # CLI add_library(CLI CLI/CLI.c) target_link_libraries(CLI CMDList FIFOBuffChar) + +# History +add_library(history History/History.c) diff --git a/History/History.c b/History/History.c index d95b3b4..dfc771d 100644 --- a/History/History.c +++ b/History/History.c @@ -1,4 +1,4 @@ -#include "FIFOBuffChar.h" +#include "history.h" #include #include @@ -6,11 +6,12 @@ History_t* History_init() { - History_t* history = malloc(sizeof(FIFOBuffChar_t)); - + History_t* history = malloc(sizeof(History_t)); + if (history != NULL) { history->FirstEl_p = NULL; history->LastEl_p = NULL; + history->CurrEl_p = NULL; history->size = 0; } @@ -27,7 +28,7 @@ int History_deinit(History_t* history) History_element_t* el = history->FirstEl_p; while (el != NULL) { - FIFOBuffChar_element_t* nextEl = el->nextEl; + History_element_t* nextEl = el->nextEl_p; free(el); el = nextEl; } @@ -47,8 +48,8 @@ int History_put(History_t *history, char* line) if (history->FirstEl_p != NULL) { history->FirstEl_p->line = line; - history->FirstEl_p->prev_p = NULL; - history->FirstEl_p->next_p = NULL; + history->FirstEl_p->prevEl_p = NULL; + history->FirstEl_p->nextEl_p = NULL; history->LastEl_p = history->FirstEl_p; history->size = 1; @@ -65,8 +66,8 @@ int History_put(History_t *history, char* line) History_element_t* el = malloc(sizeof(History_element_t)); if (el != NULL) { - el->character = i; // "value" to "character" - el->PrevEl_p = history->LastEl_p; + el->line = line; + el->prevEl_p = history->LastEl_p; el->nextEl_p = NULL; history->LastEl_p->nextEl_p = el; history->LastEl_p = el; @@ -86,7 +87,7 @@ int History_put(History_t *history, char* line) if (retCode >= 0) { // reset current pointer - history->currEl_p = NULL; + history->CurrEl_p = NULL; } return retCode; @@ -95,18 +96,39 @@ int History_put(History_t *history, char* line) int History_getPrev(History_t* history, char** line) { int retCode; - if (history->currEl_p != NULL) + if (history->CurrEl_p == NULL) { - history->currEl_p = history->LastEl_p; + history->CurrEl_p = history->LastEl_p; } else { - history->currEl_p = history->currEl_p->PrevEl_p; + history->CurrEl_p = history->CurrEl_p->prevEl_p; } - if (history->currEl_p != NULL) + if (history->CurrEl_p != NULL) { - *line = history->currEl_p->line; + *line = history->CurrEl_p->line; + retCode = 0; + } + else + { + *line = NULL; + retCode = -1; + } + return 0; +} + +int History_getNext(History_t* history, char** line) +{ + int retCode; + if (history->CurrEl_p != NULL) + { + history->CurrEl_p = history->CurrEl_p->nextEl_p; + } + + if (history->CurrEl_p != NULL) + { + *line = history->CurrEl_p->line; retCode = 0; } else From c8854931b5ce0b50821d31cb147a2e72a198c8f3 Mon Sep 17 00:00:00 2001 From: FReenen Date: Sat, 13 Apr 2024 20:20:44 +0200 Subject: [PATCH 07/11] history: add getFullHistory CLI: detect arrow keys --- CLI/CLI.c | 202 +++++++++++++++++++++++++++++++++++----------- CLI/CLI.h | 4 + CMakeLists.txt | 8 +- History/History.c | 28 +++++++ History/history.h | 2 + 5 files changed, 197 insertions(+), 47 deletions(-) diff --git a/CLI/CLI.c b/CLI/CLI.c index 53fda27..70a0386 100644 --- a/CLI/CLI.c +++ b/CLI/CLI.c @@ -5,11 +5,24 @@ #include #include +// #include + #include "../FIFOBuff/FIFOBuffChar.h" +#ifdef HISTORY +#include "../History/history.h" +#endif CLI_charOutFn CLI_charOut; CMDList_t* CMDList; FIFOBuffChar_t* FIFO; +#ifdef HISTORY +enum { + CLI_State_Default, + CLI_State_Esc, + CLI_State_ANSIVT100 +} CLI_State = CLI_State_Default; +History_t* History; +#endif void CLI_charOut_save(char ch) { @@ -36,6 +49,9 @@ bool CLI_init(CLI_charOutFn lineOut, CMDList_t* cmdList) CMDList = cmdList; FIFO = FIFOBuffChar_create(); +#ifdef HISTORY + History = History_init(); +#endif if (CLI_charOut != NULL) { @@ -51,6 +67,9 @@ bool CLI_deinit() CMDList = NULL; FIFOBuffChar_delete(FIFO); +#ifdef HISTORY + History_deinit(History); +#endif return true; } @@ -70,14 +89,46 @@ char* fifoToString(FIFOBuffChar_t* fifo) return out; } +#ifdef HISTORY +void CLI_PrintHistory() +{ + char** historyList; + int ret = History_getFullHistory(History, &historyList); + if (ret >= 0) + { + for (int i=0; *(historyList + i) != NULL; i++) + { + printf("%03i: %s\n", i, *(historyList + i)); + } + } + else + { + printf("ERROR: get history returnd: %i\n", ret); + } + free(historyList); +} +#endif + int tryExecute(FIFOBuffChar_t* fifo) { int ret = 0; + CMD_t* cmd = NULL; char* line = fifoToString(fifo); - CMD_t* cmd = CMDList_get(CMDList, line); + if (*line == '\0') + { // empty line + ret = 1; + CLI_stringOut((char*)"> "); + } + else + { + cmd = CMDList_get(CMDList, line); + } if (cmd != NULL) { +#ifdef HISTORY + History_put(History, line); +#endif int ret = (*(cmd->fn))(line); if (ret != INT_MIN) @@ -86,7 +137,7 @@ int tryExecute(FIFOBuffChar_t* fifo) } ret = 0; } - else + else if (ret == 0) { if (CLI_charOut != NULL) { @@ -95,8 +146,14 @@ int tryExecute(FIFOBuffChar_t* fifo) CLI_stringOut(&err[0]); } ret = -1; + +#ifdef HISTORY + free(line); +#endif } +#ifndef HISTORY free(line); +#endif return ret; } @@ -104,59 +161,114 @@ int tryExecute(FIFOBuffChar_t* fifo) bool CLI_charIn(char c) { bool ok = true; + char str[100]; char C = c; if ((C >= 'a') && (C <= 'z')) { C &= (~0x20); // convert to uppercase } - if ( //TODO: update list of accepted characters - ((C >= 'A') && (C <= 'Z')) - || ((c >= '0') && (c <= '9')) - || (c == ' ') - || (c == '-') - || (c == '_') - ) +#ifdef HISTORY + switch (CLI_State) { - FIFOBuffChar_put(FIFO, C); // save char in buffer - CLI_charOut_save(c); - } - else - { - char str[100]; - switch (c) - { - case '\n': - case '\r': - if (CLI_charOut != NULL) - { // echo to terminal - char ch[2] = {c, 0}; - (*CLI_charOut)(&ch[0]); - } - FIFOBuffChar_t* fifo = FIFO; - FIFO = FIFOBuffChar_create(); - ok = (tryExecute(fifo) == 0); - FIFOBuffChar_delete(fifo); - break; - - case 127: // backspace - if (FIFOBuffChar_pop(FIFO)) - { // pop something of the buffer - CLI_stringOut((char*)"\x1b[D \x1b[D"); // "" - } - break; + case CLI_State_Default: +#endif + if ( //TODO: update list of accepted characters + ((C >= 'A') && (C <= 'Z')) + || ((c >= '0') && (c <= '9')) + || (c == ' ') + || (c == '-') + || (c == '_') + ) + { + FIFOBuffChar_put(FIFO, C); // save char in buffer + CLI_charOut_save(c); + } + else + { + switch (c) + { + case '\n': + case '\r': + if (CLI_charOut != NULL) + { // echo to terminal + char ch[2] = {c, 0}; + (*CLI_charOut)(&ch[0]); + } + FIFOBuffChar_t* fifo = FIFO; + FIFO = FIFOBuffChar_create(); + ok = (tryExecute(fifo) == 0); + FIFOBuffChar_delete(fifo); + break; + + case 127: // backspace + if (FIFOBuffChar_pop(FIFO)) + { // pop something of the buffer + CLI_stringOut((char*)"\x1b[D \x1b[D"); // "" + } + break; - case 27: // escape (start for arrow keys) - sprintf(&str[0], "\ninvlid char: ESC - (%i)\n", c); - CLI_stringOut(&str[0]); - break; + case 27: // escape (start for arrow keys) +#ifdef HISTORY + CLI_State = CLI_State_Esc; + break; +#else + sprintf(&str[0], "\ninvlid char: ESC - (%i)\n", c); + CLI_stringOut(&str[0]); + break; +#endif - default: - sprintf(&str[0], "\ninvlid char: '%c' - (%i)\n", c, c); - CLI_stringOut(&str[0]); - break; - } + default: + sprintf(&str[0], "\ninvlid char: '%c' - (%i)\n", c, c); + CLI_stringOut(&str[0]); + break; + } + } +#ifdef HISTORY + break; + + case CLI_State_Esc: + if (c == '[') + { + CLI_State = CLI_State_ANSIVT100; + } + else + { + CLI_State = CLI_State_Default; + } + break; + + case CLI_State_ANSIVT100: + switch (c) + { + case 'A': // arrow up + // history previus + CLI_stringOut((char*)"(key arrow up)"); + CLI_State = CLI_State_Default; + break; + case 'B': // arrow down + CLI_stringOut((char*)"(key arrow down)"); + // history next + CLI_State = CLI_State_Default; + break; + case 'C': // arrow right + CLI_stringOut((char*)"(key arrow right)"); + CLI_State = CLI_State_Default; + break; + case 'D': // arrow left + CLI_stringOut((char*)"(key arrow left)"); + CLI_State = CLI_State_Default; + break; + default: + if ((C >= 'A') && (C <= 'Z')) + { + CLI_State = CLI_State_Default; + } + } + break; + } +#endif return ok; } diff --git a/CLI/CLI.h b/CLI/CLI.h index 32af73a..e09b5e9 100644 --- a/CLI/CLI.h +++ b/CLI/CLI.h @@ -11,6 +11,10 @@ typedef int (*CLI_charOutFn)(const char* line); bool CLI_init(CLI_charOutFn lineOut, CMDList_t* cmdList); bool CLI_deinit(); +#ifdef HISTORY +extern void CLI_PrintHistory(); +#endif + // to recive a single caracter bool CLI_charIn(char c); diff --git a/CMakeLists.txt b/CMakeLists.txt index ee81a76..ec5cf73 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,9 +6,13 @@ add_library(FIFOBuffChar FIFOBuff/FIFOBuffChar.c) add_library(CMDList CMDList/CMDList.c) add_library(CMDListPrint CMDList/printList.c) +# History +add_library(history History/History.c) + # CLI add_library(CLI CLI/CLI.c) target_link_libraries(CLI CMDList FIFOBuffChar) -# History -add_library(history History/History.c) +add_library(CLI_History CLI/CLI.c) +target_link_libraries(CLI_History CMDList FIFOBuffChar history) +target_compile_definitions(CLI_History PRIVATE HISTORY) diff --git a/History/History.c b/History/History.c index dfc771d..4c9bcfa 100644 --- a/History/History.c +++ b/History/History.c @@ -139,6 +139,34 @@ int History_getNext(History_t* history, char** line) return 0; } +int History_getFullHistory(History_t* history, char*** list) +{ + int retCode = 0; + *list = malloc(sizeof(char**) * (History_getSize(history) + 1)); + History_element_t* el = history->FirstEl_p; + + for (int i = 0; (i < History_getSize(history)-1) && (retCode >= 0); i++) + { + *((*list) + i) = el->line; + if (el->nextEl_p == NULL) + { + retCode = -1; + } + else + { + el = (History_element_t*)el->nextEl_p; + } + } + + if (retCode >= 0) + { + // indicate end of list + *((*list) + History_getSize(history)) = NULL; + } + + return retCode; +} + unsigned int History_getSize(History_t *history) { return history->size; diff --git a/History/history.h b/History/history.h index 7a7ce3a..d6c7b60 100644 --- a/History/history.h +++ b/History/history.h @@ -31,6 +31,8 @@ extern int History_put(History_t *history, char* line); extern int History_getPrev(History_t* history, char** line); extern int History_getNext(History_t* history, char** line); +extern int History_getFullHistory(History_t* history, char*** list); + extern unsigned int History_getSize(History_t *history); #endif \ No newline at end of file From d4edc2cd8fc541c83150f28cee4e147cc115d086 Mon Sep 17 00:00:00 2001 From: FReenen Date: Sat, 13 Apr 2024 20:28:39 +0200 Subject: [PATCH 08/11] CLI: ignore ANSI/VT100 arrow keys on default --- CLI/CLI.c | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/CLI/CLI.c b/CLI/CLI.c index 70a0386..a17eac0 100644 --- a/CLI/CLI.c +++ b/CLI/CLI.c @@ -15,12 +15,12 @@ CLI_charOutFn CLI_charOut; CMDList_t* CMDList; FIFOBuffChar_t* FIFO; -#ifdef HISTORY enum { CLI_State_Default, CLI_State_Esc, CLI_State_ANSIVT100 } CLI_State = CLI_State_Default; +#ifdef HISTORY History_t* History; #endif @@ -168,11 +168,9 @@ bool CLI_charIn(char c) C &= (~0x20); // convert to uppercase } -#ifdef HISTORY switch (CLI_State) { case CLI_State_Default: -#endif if ( //TODO: update list of accepted characters ((C >= 'A') && (C <= 'Z')) || ((c >= '0') && (c <= '9')) @@ -209,14 +207,8 @@ bool CLI_charIn(char c) break; case 27: // escape (start for arrow keys) -#ifdef HISTORY CLI_State = CLI_State_Esc; break; -#else - sprintf(&str[0], "\ninvlid char: ESC - (%i)\n", c); - CLI_stringOut(&str[0]); - break; -#endif default: sprintf(&str[0], "\ninvlid char: '%c' - (%i)\n", c, c); @@ -224,7 +216,6 @@ bool CLI_charIn(char c) break; } } -#ifdef HISTORY break; case CLI_State_Esc: @@ -241,25 +232,28 @@ bool CLI_charIn(char c) case CLI_State_ANSIVT100: switch (c) { +#ifdef HISTORY case 'A': // arrow up // history previus - CLI_stringOut((char*)"(key arrow up)"); + CLI_stringOut((char*)"(key: arrow up)"); CLI_State = CLI_State_Default; break; case 'B': // arrow down - CLI_stringOut((char*)"(key arrow down)"); // history next + CLI_stringOut((char*)"(key: arrow down)"); CLI_State = CLI_State_Default; break; case 'C': // arrow right - CLI_stringOut((char*)"(key arrow right)"); + CLI_stringOut((char*)"(key: arrow right)"); CLI_State = CLI_State_Default; break; case 'D': // arrow left - CLI_stringOut((char*)"(key arrow left)"); + CLI_stringOut((char*)"(key: arrow left)"); CLI_State = CLI_State_Default; break; +#endif default: + // only to back on on alpha char. seems to be the with all '\x1d[' commands if ((C >= 'A') && (C <= 'Z')) { CLI_State = CLI_State_Default; @@ -268,7 +262,6 @@ bool CLI_charIn(char c) break; } -#endif return ok; } From 8f8000d9ca7188ef1833bfb24e26625052c58641 Mon Sep 17 00:00:00 2001 From: FReenen Date: Sat, 13 Apr 2024 20:52:05 +0200 Subject: [PATCH 09/11] inital draft for history previus handeling --- CLI/CLI.c | 37 +++++++++++++++++++++++++++++++++++-- History/History.c | 27 +++++++++++++++++++++++++++ History/history.h | 2 ++ 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/CLI/CLI.c b/CLI/CLI.c index a17eac0..4825a58 100644 --- a/CLI/CLI.c +++ b/CLI/CLI.c @@ -107,6 +107,39 @@ void CLI_PrintHistory() } free(historyList); } + +void historyPrevius() +{ + char* line; + if ( + (History_getCurrPos(History) == History_getSize(History)) + && (FIFOBuffChar_getSize(FIFO) > 0) + ) + { + // add current text to end of history + line = fifoToString(FIFO); + History_put(History, line); + } + + // empty current line + while (FIFOBuffChar_getSize(FIFO) > 0) + { + CLI_stringOut((char*)"\x1b[D \x1b[D"); + FIFOBuffChar_pop(FIFO); + } + + // get previus command + int ret = History_getPrev(History, &line); + + // write line + if ((ret >= 0) && (line != NULL)) + { + for (int i=0; *(line + i) != '\0'; i++) + { + CLI_charIn(*(line + i)); + } + } +} #endif int tryExecute(FIFOBuffChar_t* fifo) @@ -234,8 +267,8 @@ bool CLI_charIn(char c) { #ifdef HISTORY case 'A': // arrow up - // history previus - CLI_stringOut((char*)"(key: arrow up)"); + historyPrevius(); + // CLI_stringOut((char*)"(key: arrow up)"); CLI_State = CLI_State_Default; break; case 'B': // arrow down diff --git a/History/History.c b/History/History.c index 4c9bcfa..612c6d2 100644 --- a/History/History.c +++ b/History/History.c @@ -167,6 +167,33 @@ int History_getFullHistory(History_t* history, char*** list) return retCode; } +int History_getCurrPos(History_t* history) +{ + int retCode = 0; + + if (history->CurrEl_p != NULL) + { + History_element_t* el = history->FirstEl_p; + for (int i = 0; (el != history->CurrEl_p) && (retCode >= 0); i++) + { + if (el->nextEl_p == NULL) + { + retCode = 2; + } + else + { + el = (History_element_t*)el->nextEl_p; + } + } + } + else + { + retCode = History_getSize(history); + } + + return retCode; +} + unsigned int History_getSize(History_t *history) { return history->size; diff --git a/History/history.h b/History/history.h index d6c7b60..35bc020 100644 --- a/History/history.h +++ b/History/history.h @@ -31,6 +31,8 @@ extern int History_put(History_t *history, char* line); extern int History_getPrev(History_t* history, char** line); extern int History_getNext(History_t* history, char** line); +extern int History_getCurrPos(History_t* history); + extern int History_getFullHistory(History_t* history, char*** list); extern unsigned int History_getSize(History_t *history); From 73c0013ee985a2145c1152bf91cd853829d64d4c Mon Sep 17 00:00:00 2001 From: FReenen Date: Sat, 13 Apr 2024 21:19:06 +0200 Subject: [PATCH 10/11] CLI: update historyPrevius() --- CLI/CLI.c | 7 ++++++- History/History.c | 8 ++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/CLI/CLI.c b/CLI/CLI.c index 4825a58..6f9d085 100644 --- a/CLI/CLI.c +++ b/CLI/CLI.c @@ -119,6 +119,11 @@ void historyPrevius() // add current text to end of history line = fifoToString(FIFO); History_put(History, line); + for (int i=0; *(line + i) != '\0'; i++) + { + CLI_stringOut((char*)"\x1b[D \x1b[D"); + } + History_getPrev(History, &line); } // empty current line @@ -267,9 +272,9 @@ bool CLI_charIn(char c) { #ifdef HISTORY case 'A': // arrow up + CLI_State = CLI_State_Default; historyPrevius(); // CLI_stringOut((char*)"(key: arrow up)"); - CLI_State = CLI_State_Default; break; case 'B': // arrow down // history next diff --git a/History/History.c b/History/History.c index 612c6d2..74bdca0 100644 --- a/History/History.c +++ b/History/History.c @@ -100,10 +100,14 @@ int History_getPrev(History_t* history, char** line) { history->CurrEl_p = history->LastEl_p; } - else + else if (history->CurrEl_p->prevEl_p != NULL) { history->CurrEl_p = history->CurrEl_p->prevEl_p; } + else + { // no previus line + retCode = -2; + } if (history->CurrEl_p != NULL) { @@ -111,7 +115,7 @@ int History_getPrev(History_t* history, char** line) retCode = 0; } else - { + { // no items in history? *line = NULL; retCode = -1; } From 0eed939e87cecc58c1c633b6e4745af1b7c8a2af Mon Sep 17 00:00:00 2001 From: FReenen Date: Sat, 13 Apr 2024 22:19:32 +0200 Subject: [PATCH 11/11] CLI: add history next --- CLI/CLI.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/CLI/CLI.c b/CLI/CLI.c index 6f9d085..a4544b1 100644 --- a/CLI/CLI.c +++ b/CLI/CLI.c @@ -145,6 +145,28 @@ void historyPrevius() } } } +void historyNext() +{ + char* line; + // empty current line + while (FIFOBuffChar_getSize(FIFO) > 0) + { + CLI_stringOut((char*)"\x1b[D \x1b[D"); + FIFOBuffChar_pop(FIFO); + } + + // get next command + int ret = History_getNext(History, &line); + + // write line + if ((ret >= 0) && (line != NULL)) + { + for (int i=0; *(line + i) != '\0'; i++) + { + CLI_charIn(*(line + i)); + } + } +} #endif int tryExecute(FIFOBuffChar_t* fifo) @@ -274,12 +296,10 @@ bool CLI_charIn(char c) case 'A': // arrow up CLI_State = CLI_State_Default; historyPrevius(); - // CLI_stringOut((char*)"(key: arrow up)"); break; case 'B': // arrow down - // history next - CLI_stringOut((char*)"(key: arrow down)"); CLI_State = CLI_State_Default; + historyNext(); break; case 'C': // arrow right CLI_stringOut((char*)"(key: arrow right)");