allow multiple cli interfaces

This commit is contained in:
Laila van Reenen 2024-06-27 18:07:38 +02:00
parent 09ae51070b
commit f08c8e5788
Signed by: LailaTheElf
GPG Key ID: 1F4E6EE3E6DDF769
4 changed files with 133 additions and 112 deletions

184
CLI/CLI.c
View File

@ -5,71 +5,53 @@
#include <stdio.h>
#include <limits.h>
// #include <config.h>
#define HISTORY
#include "../FIFOBuff/FIFOBuffChar.h"
#ifdef HISTORY
#include "../History/history.h"
#endif
CLI_charOutFn CLI_charOut;
CMDList_t* CMDList;
FIFOBuffChar_t* FIFO;
enum {
CLI_State_Default,
CLI_State_Esc,
CLI_State_ANSIVT100
} CLI_State = CLI_State_Default;
#ifdef HISTORY
History_t* History;
#endif
void CLI_charOut_save(char ch)
void CLI_charOut_save(CLI_t* cli, char ch)
{
if (CLI_charOut != NULL)
if (cli->CLI_charOut != NULL)
{
// create string of size one to be compatable with string print function
char c[2] = {ch, 0};
(*CLI_charOut)(&c[0]);
(*(cli->CLI_charOut))(&c[0]);
}
}
void CLI_stringOut(char* str)
void CLI_stringOut(CLI_t* cli, char* str)
{
for (; *str != 0; str++)
{
CLI_charOut_save(*str);
CLI_charOut_save(cli, *str);
}
}
// initilize and register the lineout print function
bool CLI_init(CLI_charOutFn lineOut, CMDList_t* cmdList)
CLI_t CLI_init(CLI_charOutFn lineOut, CMDList_t* cmdList)
{
CLI_charOut = lineOut;
CMDList = cmdList;
CLI_t cli;
cli.CLI_charOut = lineOut;
cli.CMDList = cmdList;
cli.CLI_State = CLI_State_Default;
FIFO = FIFOBuffChar_create();
cli.FIFO = FIFOBuffChar_create();
#ifdef HISTORY
History = History_init();
cli.History = History_init();
#endif
if (CLI_charOut != NULL)
if (cli.CLI_charOut != NULL)
{
CLI_stringOut((char*)"> ");
CLI_stringOut(&cli, (char*)"> ");
}
return true;
return cli;
}
bool CLI_deinit()
bool CLI_deinit(CLI_t* cli)
{
CLI_charOut = NULL;
CMDList = NULL;
cli->CLI_charOut = NULL;
cli->CMDList = NULL;
FIFOBuffChar_delete(FIFO);
FIFOBuffChar_delete(cli->FIFO);
#ifdef HISTORY
History_deinit(History, true);
History_deinit(cli->History, true);
#endif
return true;
}
@ -92,89 +74,86 @@ char* fifoToString(FIFOBuffChar_t* fifo)
}
#ifdef HISTORY
void CLI_PrintHistory()
void CLI_PrintHistory(CLI_t* cli)
{
char** historyList;
int ret = History_getFullHistory(History, &historyList);
int ret = History_getFullHistory(cli->History, &historyList);
int i;
char str[150];
if (ret >= 0)
{
for (i=0; *(historyList + i) != NULL; i++)
{
printf("%03i: %s\n", i, *(historyList + i));
snprintf(&str[0], 150, "%03i: %s\n", i, *(historyList + i));
CLI_stringOut(cli, &str[0]);
}
}
else
{
printf("ERROR: get history returnd: %i\n", ret);
snprintf(&str[0], 150, "ERROR: get history returnd: %i\n", ret);
CLI_stringOut(cli, &str[0]);
}
free(historyList);
}
void historyPrevius()
void historyPrevius(CLI_t* cli)
{
char* line;
int i;
if (
(History_getCurrPos(History) == History_getSize(History))
&& (FIFOBuffChar_getSize(FIFO) > 0)
(History_getCurrPos(cli->History) == History_getSize(cli->History))
&& (FIFOBuffChar_getSize(cli->FIFO) > 0)
)
{
// add current text to end of history
line = fifoToString(FIFO);
History_put(History, line);
line = fifoToString(cli->FIFO);
History_put(cli->History, line);
for (i=0; *(line + i) != '\0'; i++)
{
CLI_stringOut((char*)"\x1b[D \x1b[D");
CLI_stringOut(cli, (char*)"\x1b[D \x1b[D");
}
History_getPrev(History, &line);
History_getPrev(cli->History, &line);
}
// empty current line
while (FIFOBuffChar_getSize(FIFO) > 0)
while (FIFOBuffChar_getSize(cli->FIFO) > 0)
{
CLI_stringOut((char*)"\x1b[D \x1b[D");
FIFOBuffChar_pop(FIFO);
CLI_stringOut(cli, (char*)"\x1b[D \x1b[D");
FIFOBuffChar_pop(cli->FIFO);
}
// get previus command
int ret = History_getPrev(History, &line);
int ret = History_getPrev(cli->History, &line);
// write line
if ((ret >= 0) && (line != NULL))
{
for (i=0; *(line + i) != '\0'; i++)
{
CLI_charIn(*(line + i));
}
CLI_stringOut(cli, line);
}
}
void historyNext()
void historyNext(CLI_t* cli)
{
char* line;
// empty current line
while (FIFOBuffChar_getSize(FIFO) > 0)
while (FIFOBuffChar_getSize(cli->FIFO) > 0)
{
CLI_stringOut((char*)"\x1b[D \x1b[D");
FIFOBuffChar_pop(FIFO);
CLI_stringOut(cli, (char*)"\x1b[D \x1b[D");
FIFOBuffChar_pop(cli->FIFO);
}
// get next command
int ret = History_getNext(History, &line);
int ret = History_getNext(cli->History, &line);
// write line
if ((ret >= 0) && (line != NULL))
{
int i;
for (i=0; *(line + i) != '\0'; i++)
{
CLI_charIn(*(line + i));
}
CLI_stringOut(cli, line);
}
}
#endif
int tryExecute(FIFOBuffChar_t* fifo)
int tryExecute(CLI_t* cli, FIFOBuffChar_t* fifo)
{
int ret = 0;
CMD_t* cmd = NULL;
@ -182,34 +161,31 @@ int tryExecute(FIFOBuffChar_t* fifo)
if (*line == '\0')
{ // empty line
ret = 1;
CLI_stringOut((char*)"> ");
CLI_stringOut(cli, (char*)"> ");
}
else
{
cmd = CMDList_get(CMDList, line);
cmd = CMDList_get(cli->CMDList, line);
}
if (cmd != NULL)
{
#ifdef HISTORY
History_put(History, line);
History_put(cli->History, line);
#endif
int ret = (*(cmd->fn))(line);
if (ret != INT_MIN)
{
CLI_stringOut((char*)"> ");
CLI_stringOut(cli, (char*)"> ");
}
ret = 0;
}
else if (ret == 0)
{
if (CLI_charOut != NULL)
{
char err[100];
sprintf(&err[0], "command not found: %s\n> ", line);
CLI_stringOut(&err[0]);
}
char err[100];
sprintf(&err[0], "command not found: %s\n> ", line);
CLI_stringOut(cli, &err[0]);
ret = -1;
#ifdef HISTORY
@ -223,7 +199,7 @@ int tryExecute(FIFOBuffChar_t* fifo)
}
// to recive a single caracter
bool CLI_charIn(char c)
bool CLI_charIn(CLI_t* cli, char c)
{
bool ok = true;
char str[100];
@ -233,13 +209,13 @@ bool CLI_charIn(char c)
C &= (~0x20); // convert to uppercase
}
switch (CLI_State)
switch (cli->CLI_State)
{
case CLI_State_Default:
if ((C >= ' ') && (C <= '~')) // see ascii table
{
FIFOBuffChar_put(FIFO, C); // save char in buffer
CLI_charOut_save(c);
FIFOBuffChar_put(cli->FIFO, C); // save char in buffer
CLI_charOut_save(cli, c);
}
else
{
@ -247,32 +223,32 @@ bool CLI_charIn(char 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);
// echo to terminal
CLI_charOut_save(cli, c);
// copy buffer and create new buffer
FIFOBuffChar_t* fifo = cli->FIFO;
cli->FIFO = FIFOBuffChar_create();
// execute command if valid
ok = (tryExecute(cli, fifo) == 0);
// delete old buffer
FIFOBuffChar_delete(fifo);
break;
case 8:
case 127: // delete (backspace)
if (FIFOBuffChar_pop(FIFO))
if (FIFOBuffChar_pop(cli->FIFO))
{ // pop something of the buffer
CLI_stringOut((char*)"\x1b[D \x1b[D"); // "<left arrow><space><left arrow>"
CLI_stringOut(cli, (char*)"\x1b[D \x1b[D"); // "<left arrow><space><left arrow>"
}
break;
case 27: // escape (start for arrow keys)
CLI_State = CLI_State_Esc;
cli->CLI_State = CLI_State_Esc;
break;
default:
sprintf(&str[0], "\ninvlid char: '%c' - (%i)\n", c, c);
CLI_stringOut(&str[0]);
CLI_stringOut(cli, &str[0]);
break;
}
}
@ -281,11 +257,11 @@ bool CLI_charIn(char c)
case CLI_State_Esc:
if (c == '[')
{
CLI_State = CLI_State_ANSIVT100;
cli->CLI_State = CLI_State_ANSIVT100;
}
else
{
CLI_State = CLI_State_Default;
cli->CLI_State = CLI_State_Default;
}
break;
@ -294,27 +270,27 @@ bool CLI_charIn(char c)
{
#ifdef HISTORY
case 'A': // arrow up
CLI_State = CLI_State_Default;
historyPrevius();
cli->CLI_State = CLI_State_Default;
historyPrevius(cli);
break;
case 'B': // arrow down
CLI_State = CLI_State_Default;
historyNext();
cli->CLI_State = CLI_State_Default;
historyNext(cli);
break;
case 'C': // arrow right
CLI_stringOut((char*)"(key: arrow right)");
CLI_State = CLI_State_Default;
CLI_stringOut(cli, (char*)"(key: arrow right)");
cli->CLI_State = CLI_State_Default;
break;
case 'D': // arrow left
CLI_stringOut((char*)"(key: arrow left)");
CLI_State = CLI_State_Default;
CLI_stringOut(cli, (char*)"(key: arrow left)");
cli->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;
cli->CLI_State = CLI_State_Default;
}
}
break;

View File

@ -3,20 +3,42 @@
#include <stdbool.h>
#include "../CMDList/CMDList.h"
#include <config.h>
#define HISTORY
typedef int (*CLI_charOutFn)(const char* line);
#include "../CMDList/CMDList.h"
#include "../FIFOBuff/FIFOBuffChar.h"
#ifdef HISTORY
#include "../History/history.h"
#endif
typedef int (*CLI_charOutFn)(const char* c);
typedef enum {
CLI_State_Default,
CLI_State_Esc,
CLI_State_ANSIVT100
} CLI_State_t;
typedef struct CLI {
CLI_charOutFn CLI_charOut;
CMDList_t* CMDList;
FIFOBuffChar_t* FIFO;
#ifdef HISTORY
History_t* History;
#endif
CLI_State_t CLI_State;
} CLI_t;
// initilize and register the lineout print function
bool CLI_init(CLI_charOutFn lineOut, CMDList_t* cmdList);
bool CLI_deinit();
CLI_t CLI_init(CLI_charOutFn lineOut, CMDList_t* cmdList);
bool CLI_deinit(CLI_t* cli);
#ifdef HISTORY
extern void CLI_PrintHistory();
extern void CLI_PrintHistory(CLI_t* cli);
#endif
// to recive a single caracter
bool CLI_charIn(char c);
bool CLI_charIn(CLI_t* cli, char c);
#endif

25
library.json Normal file
View File

@ -0,0 +1,25 @@
{
"$schema": "https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/schema/library.json",
"name": "cli",
"version": "1.0.0",
"export": {
"include": [
"*/*.h"
]
},
"keywords": [
"cli",
"EMS31",
"HR",
"submodules"
],
"repository": {
"type": "git",
"url": "https://gitea.finnvanreenen.nl/HR/EMS31_submodules.git"
},
"build": {
"flags": [
"-I$PROJECT_DIR/src"
]
}
}

View File

@ -1,2 +0,0 @@
# Leeg
Deze repo is leeg. Doe er wat mee!