diff --git a/CLI/CLI.c b/CLI/CLI.c index 918fcc0..050ce30 100644 --- a/CLI/CLI.c +++ b/CLI/CLI.c @@ -2,6 +2,7 @@ #include #include +#include #include "../FIFOBuff/FIFOBuffChar.h" @@ -62,7 +63,6 @@ int tryExecute(FIFOBuffChar_t* fifo) if (CLI_lineOut != NULL) { - (*CLI_lineOut)("\n"); (*CLI_lineOut)(">"); (*CLI_lineOut)(" "); } @@ -73,7 +73,7 @@ int tryExecute(FIFOBuffChar_t* fifo) if (CLI_lineOut != NULL) { char err[100]; - sprintf(&err, "command not found: %s\n> ", line); + sprintf(&err[0], "command not found: %s\n> ", line); for (int i=0; err[i] != 0; i++) { char c[2] = {err[i], 0}; @@ -125,13 +125,16 @@ bool CLI_charIn(char c) break; case 127: // backspace - (*CLI_lineOut)("\x1b"); - (*CLI_lineOut)("["); - (*CLI_lineOut)("D"); + if (FIFOBuffChar_pop(FIFO)) + { + (*CLI_lineOut)("\x1b"); + (*CLI_lineOut)("["); + (*CLI_lineOut)("D"); + } break; case 27: // escape (start for arrow keys) - sprintf(&str, "\ninvlid char: ESC - (%i)\n", c); + sprintf(&str[0], "\ninvlid char: ESC - (%i)\n", c); for (int i=0; str[i] != 0; i++) { char ch[2] = {str[i], 0}; @@ -140,7 +143,7 @@ bool CLI_charIn(char c) break; default: - sprintf(&str, "\ninvlid char: '%c' - (%i)\n", c, c); + sprintf(&str[0], "\ninvlid char: '%c' - (%i)\n", c, c); for (int i=0; str[i] != 0; i++) { char ch[2] = {str[i], 0}; diff --git a/FIFOBuff/FIFOBuffChar.c b/FIFOBuff/FIFOBuffChar.c index 9b5fa98..6f41aef 100644 --- a/FIFOBuff/FIFOBuffChar.c +++ b/FIFOBuff/FIFOBuffChar.c @@ -1,9 +1,8 @@ +#include "FIFOBuffChar.h" + #include #include #include -#include "FIFOBuffChar.h" - - bool FIFOBuffChar_put(FIFOBuffChar_t *fifo, char i) // Change the function parameters to match the declaration { @@ -126,25 +125,39 @@ bool FIFOBuffChar_pop(FIFOBuffChar_t *fifo) { bool ok = true; - if (fifo->FirstEl_p == NULL) - { // buffer is empty - ok = false; - } - else - { // there is data in the buffer - // find the second last element - FIFOBuffChar_element_t* secondLastEl; = fifo->FirstEl_p; - while (secondLastEl->nextElement != fifo->LastEl_p) - { - secondLastEl = secondLastEl->nextElement; - } + switch (fifo->size){ + case 0: // buffer is empty + ok = false; + break; - // remove last element - free(fifo->LastEl_p); + case 1: // singel item in buffer + // make buffer empty + free(fifo->FirstEl_p); + fifo->FirstEl_p = NULL; + fifo->LastEl_p = NULL; + fifo->size = 0; + ok = true; + break; - // update chain - secondLastEl->nextElement == NULL; - fifo->LastEl_p = secondLastEl; + default: // buffer is at least 2 element big + // find the second last element + FIFOBuffChar_element_t* secondLastEl = fifo->FirstEl_p; + while ( + secondLastEl->nextElement != fifo->LastEl_p + && ((FIFOBuffChar_element_t*)secondLastEl->nextElement)->nextElement != NULL + ){ + secondLastEl = secondLastEl->nextElement; + } + + // remove last element + free(fifo->LastEl_p); + + // update chain + secondLastEl->nextElement = NULL; + fifo->LastEl_p = secondLastEl; + fifo->size--; + ok = true; + break; } return ok;