first draft of CLI

This commit is contained in:
Laila van Reenen 2024-04-06 22:14:53 +02:00
parent 96e48e2523
commit 3dbe121389

95
CLI/CLI.c Normal file
View File

@ -0,0 +1,95 @@
#include "CLI.h"
#include "../FIFOBuffChar.h"
void (*CLI_lineOut)(char* line);
CMDList_t* CMDList;
FIFOBuffChar_t* FIFO;
// initilize and register the lineout print function
bool CLI_init(CLI_lineoutFn_t* lineOut, CMDList_t* cmdList)
{
CLI_lineOut = lineOut;
CMDList = cmdList;
FIFO = FIFOBuffChar_create();
}
bool CLI_deinit()
{
CLI_lineOut = NULL;
CMDList = NULL;
FIFOBuffChar_delete(FIFO);
}
char* fifoToString(FIFOBuffChar_t* fifo)
{
char* out = malloc(fifo->size + 1);
char* write_p = out;
for (int i = fifo->size; i > 0; i--)
{
FIFOBuffChar_get(fifo, write_p);
write_p++;
}
*write_p = 0;
return out;
}
int tryExecute(FIFOBuffChar_t* fifo)
{
char* line = fifoToString(fifo);
CMD_t cmd = CMDList_get(CMDList, line);
if (cmd != NULL)
{
(*(cmd->fn))(line);
return true;
}
else
{
return false;
}
}
// to recive a single caracter
bool CLI_charIn(char 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 = '_')
)
{
FIFOBuffChar_put(FIFO, c); // save char in buffer
(*CLI_lineOut)(&c); // echo to terminal
}
else
{
switch (c)
{
case '\n':
case '\r':
(*CLI_lineOut)(&c); // echo to terminal
FIFOBuffChar_t* fifo = FIFO;
FIFO = FIFOBuffChar_create();
tryExecute(fifo);
FIFOBuffChar_delete(fifo);
break;
//TODO: add hire backspace and arrow detection
default:
break;
}
}
}