simplify some code and fix tiny bugs

This commit is contained in:
Laila van Reenen 2024-04-13 15:21:43 +02:00
parent 9a437218a2
commit e267e6f907

View File

@ -10,6 +10,24 @@ CLI_charOutFn CLI_charOut;
CMDList_t* CMDList; CMDList_t* CMDList;
FIFOBuffChar_t* FIFO; 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 // initilize and register the lineout print function
bool CLI_init(CLI_charOutFn lineOut, CMDList_t* cmdList) 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) if (CLI_charOut != NULL)
{ {
(*CLI_charOut)(">"); CLI_stringOut((char*)"> ");
(*CLI_charOut)(" ");
} }
return true; return true;
@ -61,11 +78,7 @@ int tryExecute(FIFOBuffChar_t* fifo)
{ {
(*(cmd->fn))(line); (*(cmd->fn))(line);
if (CLI_charOut != NULL) CLI_stringOut((char*)"> ");
{
(*CLI_charOut)(">");
(*CLI_charOut)(" ");
}
return true; return true;
} }
else else
@ -74,11 +87,7 @@ int tryExecute(FIFOBuffChar_t* fifo)
{ {
char err[100]; char err[100];
sprintf(&err[0], "command not found: %s\n> ", line); sprintf(&err[0], "command not found: %s\n> ", line);
for (int i=0; err[i] != 0; i++) CLI_stringOut(&err[0]);
{
char c[2] = {err[i], 0};
(*CLI_charOut)(&c[0]);
}
} }
return false; return false;
} }
@ -88,24 +97,22 @@ int tryExecute(FIFOBuffChar_t* fifo)
bool CLI_charIn(char c) bool CLI_charIn(char c)
{ {
bool ok = true; 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 if ( //TODO: update list of accepted characters
((c >= 'A') && (c <= 'Z')) ((C >= 'A') && (C <= 'Z'))
|| ((c >= '0') && (c <= '9')) || ((c >= '0') && (c <= '9'))
|| (c == ' ') || (c == ' ')
|| (c == '-') || (c == '-')
|| (c == '_') || (c == '_')
) )
{ {
FIFOBuffChar_put(FIFO, c); // save char in buffer FIFOBuffChar_put(FIFO, C); // save char in buffer
if (CLI_charOut != NULL) CLI_charOut_save(c);
{ // echo to terminal
(*CLI_charOut)(&c);
}
} }
else else
{ {
@ -116,7 +123,8 @@ bool CLI_charIn(char c)
case '\r': case '\r':
if (CLI_charOut != NULL) if (CLI_charOut != NULL)
{ // echo to terminal { // echo to terminal
(*CLI_charOut)(&c); char ch[2] = {c, 0};
(*CLI_charOut)(&ch[0]);
} }
FIFOBuffChar_t* fifo = FIFO; FIFOBuffChar_t* fifo = FIFO;
FIFO = FIFOBuffChar_create(); FIFO = FIFOBuffChar_create();
@ -126,33 +134,19 @@ bool CLI_charIn(char c)
case 127: // backspace case 127: // backspace
if (FIFOBuffChar_pop(FIFO)) if (FIFOBuffChar_pop(FIFO))
{ { // pop something of the buffer
(*CLI_charOut)("\x1b"); CLI_stringOut((char*)"\x1b[D \x1b[D"); // "<left arrow><space><left arrow>"
(*CLI_charOut)("[");
(*CLI_charOut)("D");
(*CLI_charOut)(" ");
(*CLI_charOut)("\x1b");
(*CLI_charOut)("[");
(*CLI_charOut)("D");
} }
break; break;
case 27: // escape (start for arrow keys) case 27: // escape (start for arrow keys)
sprintf(&str[0], "\ninvlid char: ESC - (%i)\n", c); sprintf(&str[0], "\ninvlid char: ESC - (%i)\n", c);
for (int i=0; str[i] != 0; i++) CLI_stringOut(&str[0]);
{
char ch[2] = {str[i], 0};
(*CLI_charOut)(&ch[0]);
}
break; break;
default: default:
sprintf(&str[0], "\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++) CLI_stringOut(&str[0]);
{
char ch[2] = {str[i], 0};
(*CLI_charOut)(&ch[0]);
}
break; break;
} }
} }