Compare commits

...

7 Commits

Author SHA1 Message Date
8204cb0865
add option to disable the echo 2024-08-10 14:07:55 +02:00
ae59563e7d
Merge branch 'multie-cli' into esp-idf 2024-07-19 23:49:45 +02:00
42aec53edb
fix history 2024-07-19 23:49:15 +02:00
259ad0a6f1
update CMakeList for esp-idf component 2024-07-19 22:34:22 +02:00
8f71535dfd
minor fixes 2024-07-19 22:32:56 +02:00
8261e605bc
add cli ref to command function 2024-07-01 19:32:46 +02:00
f08c8e5788
allow multiple cli interfaces 2024-06-27 18:07:38 +02:00
7 changed files with 190 additions and 142 deletions

237
CLI/CLI.c
View File

@ -5,71 +5,54 @@
#include <stdio.h> #include <stdio.h>
#include <limits.h> #include <limits.h>
// #include <config.h> void CLI_charOut_save(CLI_t* cli, char ch)
#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)
{ {
if (CLI_charOut != NULL) if (cli->CLI_charOut != NULL)
{ {
// create string of size one to be compatable with string print function // create string of size one to be compatable with string print function
char c[2] = {ch, 0}; 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++) for (; *str != 0; str++)
{ {
CLI_charOut_save(*str); CLI_charOut_save(cli, *str);
} }
} }
// initilize and register the lineout print function // 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; CLI_t cli;
CMDList = cmdList; cli.CLI_charOut = lineOut;
cli.CMDList = cmdList;
cli.CLI_State = CLI_State_Default;
cli.echo = true;
FIFO = FIFOBuffChar_create(); cli.FIFO = FIFOBuffChar_create();
#ifdef HISTORY #ifdef HISTORY
History = History_init(); cli.History = History_init();
#endif #endif
if (CLI_charOut != NULL) if (cli.CLI_charOut != NULL && cli.echo)
{ {
CLI_stringOut((char*)"> "); CLI_stringOut(&cli, (char*)"> ");
} }
return true; return cli;
} }
bool CLI_deinit() bool CLI_deinit(CLI_t* cli)
{ {
CLI_charOut = NULL; cli->CLI_charOut = NULL;
CMDList = NULL; cli->CMDList = NULL;
FIFOBuffChar_delete(FIFO); FIFOBuffChar_delete(cli->FIFO);
#ifdef HISTORY #ifdef HISTORY
History_deinit(History, true); History_deinit(cli->History, true);
#endif #endif
return true; return true;
} }
@ -92,75 +75,56 @@ char* fifoToString(FIFOBuffChar_t* fifo)
} }
#ifdef HISTORY #ifdef HISTORY
void CLI_PrintHistory() void CLI_PrintHistory(CLI_t* cli)
{ {
char** historyList; char** historyList;
int ret = History_getFullHistory(History, &historyList); int ret = History_getFullHistory(cli->History, &historyList);
int i; int i;
char str[150];
if (ret >= 0) if (ret >= 0)
{ {
for (i=0; *(historyList + i) != NULL; i++) 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 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); free(historyList);
} }
void historyPrevius() void historyPrevius(CLI_t* cli)
{ {
char* line; char* line;
int i; int i;
if ( if (
(History_getCurrPos(History) == History_getSize(History)) (History_getCurrPos(cli->History) == History_getSize(cli->History))
&& (FIFOBuffChar_getSize(FIFO) > 0) && (FIFOBuffChar_getSize(cli->FIFO) > 0)
) )
{ {
// add current text to end of history // add current text to end of history
line = fifoToString(FIFO); line = fifoToString(cli->FIFO);
History_put(History, line); History_put(cli->History, line);
for (i=0; *(line + i) != '\0'; i++) 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 // empty current line
while (FIFOBuffChar_getSize(FIFO) > 0) while (FIFOBuffChar_getSize(cli->FIFO) > 0)
{ {
CLI_stringOut((char*)"\x1b[D \x1b[D"); CLI_stringOut(cli, (char*)"\x1b[D \x1b[D");
FIFOBuffChar_pop(FIFO); FIFOBuffChar_pop(cli->FIFO);
} }
// get previus command // 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));
}
}
}
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 // write line
if ((ret >= 0) && (line != NULL)) if ((ret >= 0) && (line != NULL))
@ -168,13 +132,36 @@ void historyNext()
int i; int i;
for (i=0; *(line + i) != '\0'; i++) for (i=0; *(line + i) != '\0'; i++)
{ {
CLI_charIn(*(line + i)); CLI_charIn(cli, *(line + i));
}
}
}
void historyNext(CLI_t* cli)
{
char* line;
// empty current line
while (FIFOBuffChar_getSize(cli->FIFO) > 0)
{
CLI_stringOut(cli, (char*)"\x1b[D \x1b[D");
FIFOBuffChar_pop(cli->FIFO);
}
// get next command
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(cli, *(line + i));
} }
} }
} }
#endif #endif
int tryExecute(FIFOBuffChar_t* fifo) int tryExecute(CLI_t* cli, FIFOBuffChar_t* fifo)
{ {
int ret = 0; int ret = 0;
CMD_t* cmd = NULL; CMD_t* cmd = NULL;
@ -182,33 +169,37 @@ int tryExecute(FIFOBuffChar_t* fifo)
if (*line == '\0') if (*line == '\0')
{ // empty line { // empty line
ret = 1; ret = 1;
CLI_stringOut((char*)"> "); if (cli->echo)
{
CLI_stringOut(cli, (char*)"> ");
}
} }
else else
{ {
cmd = CMDList_get(CMDList, line); cmd = CMDList_get(cli->CMDList, line);
} }
if (cmd != NULL) if (cmd != NULL)
{ {
#ifdef HISTORY #ifdef HISTORY
History_put(History, line); History_put(cli->History, line);
#endif #endif
int ret = (*(cmd->fn))(line); int ret = (*(cmd->fn))(line, cli);
if (ret != INT_MIN) if (ret != INT_MIN && cli->echo)
{ {
CLI_stringOut((char*)"> "); CLI_stringOut(cli, (char*)"> ");
} }
ret = 0; ret = 0;
} }
else if (ret == 0) else if (ret == 0)
{ {
if (CLI_charOut != NULL) char err[100];
sprintf(&err[0], "command not found: %s\n", line);
CLI_stringOut(cli, &err[0]);
if (cli->echo)
{ {
char err[100]; CLI_stringOut(cli, (char*)"> ");
sprintf(&err[0], "command not found: %s\n> ", line);
CLI_stringOut(&err[0]);
} }
ret = -1; ret = -1;
@ -223,7 +214,7 @@ int tryExecute(FIFOBuffChar_t* fifo)
} }
// to recive a single caracter // to recive a single caracter
bool CLI_charIn(char c) bool CLI_charIn(CLI_t* cli, char c)
{ {
bool ok = true; bool ok = true;
char str[100]; char str[100];
@ -233,13 +224,16 @@ bool CLI_charIn(char c)
C &= (~0x20); // convert to uppercase C &= (~0x20); // convert to uppercase
} }
switch (CLI_State) switch (cli->CLI_State)
{ {
case CLI_State_Default: case CLI_State_Default:
if ((C >= ' ') && (C <= '~')) // see ascii table if ((C >= ' ') && (C <= '~')) // see ascii table
{ {
FIFOBuffChar_put(FIFO, C); // save char in buffer FIFOBuffChar_put(cli->FIFO, C); // save char in buffer
CLI_charOut_save(c); if (cli->echo)
{
CLI_charOut_save(cli, c);
}
} }
else else
{ {
@ -247,32 +241,41 @@ bool CLI_charIn(char c)
{ {
case '\n': case '\n':
case '\r': case '\r':
if (CLI_charOut != NULL) // echo to terminal
{ // echo to terminal if (cli->echo)
char ch[2] = {c, 0}; {
(*CLI_charOut)(&ch[0]); CLI_charOut_save(cli, c);
} }
FIFOBuffChar_t* fifo = FIFO; // copy buffer and create new buffer
FIFO = FIFOBuffChar_create(); FIFOBuffChar_t* fifo = cli->FIFO;
ok = (tryExecute(fifo) == 0); cli->FIFO = FIFOBuffChar_create();
// execute command if valid
ok = (tryExecute(cli, fifo) == 0);
// delete old buffer
FIFOBuffChar_delete(fifo); FIFOBuffChar_delete(fifo);
break; break;
case 8: case 8:
case 127: // delete (backspace) case 127: // delete (backspace)
if (FIFOBuffChar_pop(FIFO)) if (FIFOBuffChar_pop(cli->FIFO))
{ // pop something of the buffer { // pop something of the buffer
CLI_stringOut((char*)"\x1b[D \x1b[D"); // "<left arrow><space><left arrow>" if (cli->echo)
{
CLI_stringOut(cli, (char*)"\x1b[D \x1b[D"); // "<left arrow><space><left arrow>"
}
} }
break; break;
case 27: // escape (start for arrow keys) case 27: // escape (start for arrow keys)
CLI_State = CLI_State_Esc; cli->CLI_State = CLI_State_Esc;
break; break;
default: default:
sprintf(&str[0], "\ninvlid char: '%c' - (%i)\n", c, c); if (cli->echo)
CLI_stringOut(&str[0]); {
sprintf(&str[0], "\ninvlid char: '%c' - (%i)\n", c, c);
CLI_stringOut(cli, &str[0]);
}
break; break;
} }
} }
@ -281,11 +284,11 @@ bool CLI_charIn(char c)
case CLI_State_Esc: case CLI_State_Esc:
if (c == '[') if (c == '[')
{ {
CLI_State = CLI_State_ANSIVT100; cli->CLI_State = CLI_State_ANSIVT100;
} }
else else
{ {
CLI_State = CLI_State_Default; cli->CLI_State = CLI_State_Default;
} }
break; break;
@ -294,27 +297,33 @@ bool CLI_charIn(char c)
{ {
#ifdef HISTORY #ifdef HISTORY
case 'A': // arrow up case 'A': // arrow up
CLI_State = CLI_State_Default; cli->CLI_State = CLI_State_Default;
historyPrevius(); historyPrevius(cli);
break; break;
case 'B': // arrow down case 'B': // arrow down
CLI_State = CLI_State_Default; cli->CLI_State = CLI_State_Default;
historyNext(); historyNext(cli);
break; break;
case 'C': // arrow right case 'C': // arrow right
CLI_stringOut((char*)"(key: arrow right)"); if (cli->echo)
CLI_State = CLI_State_Default; {
CLI_stringOut(cli, (char*)"(key: arrow right)");
}
cli->CLI_State = CLI_State_Default;
break; break;
case 'D': // arrow left case 'D': // arrow left
CLI_stringOut((char*)"(key: arrow left)"); if (cli->echo)
CLI_State = CLI_State_Default; {
CLI_stringOut(cli, (char*)"(key: arrow left)");
}
cli->CLI_State = CLI_State_Default;
break; break;
#endif #endif
default: default:
// only to back on on alpha char. seems to be the with all '\x1d[' commands // only to back on on alpha char. seems to be the with all '\x1d[' commands
if ((C >= 'A') && (C <= 'Z')) if ((C >= 'A') && (C <= 'Z'))
{ {
CLI_State = CLI_State_Default; cli->CLI_State = CLI_State_Default;
} }
} }
break; break;

View File

@ -3,20 +3,44 @@
#include <stdbool.h> #include <stdbool.h>
#include "../CMDList/CMDList.h" #include <config.h>
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;
bool echo;
} CLI_t;
// initilize and register the lineout print function // 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);
bool CLI_deinit(); bool CLI_deinit(CLI_t* cli);
#ifdef HISTORY #ifdef HISTORY
extern void CLI_PrintHistory(); extern void CLI_PrintHistory(CLI_t* cli);
#endif #endif
// to recive a single caracter // to recive a single caracter
bool CLI_charIn(char c); bool CLI_charIn(CLI_t* cli, char c);
void CLI_charOut_save(CLI_t* cli, char ch);
void CLI_stringOut(CLI_t* cli, char* str);
#endif #endif

View File

@ -3,7 +3,7 @@
typedef struct CMD_t { typedef struct CMD_t {
char* cmd; char* cmd;
int (*fn)(char* line); int (*fn)(char* line, void* cli);
} CMD_t; } CMD_t;
typedef struct CMDList_s { typedef struct CMDList_s {

View File

@ -1,18 +1,10 @@
# FIFO Buffer idf_component_register(
add_library(FIFOBuffChar FIFOBuff/FIFOBuffChar.c) COMPONENT_ALIAS "cli"
SRCS
# CMD List CLI/CLI.c
add_library(CMDList CMDList/CMDList.c) FIFOBuff/FIFOBuffChar.c
add_library(CMDListPrint CMDList/printList.c) CMDList/CMDList.c
History/History.c
# History INCLUDE_DIRS "./"
add_library(history History/History.c) PRIV_INCLUDE_DIRS "../../src")
# CLI
add_library(CLI CLI/CLI.c)
target_link_libraries(CLI CMDList FIFOBuffChar history)
add_library(CLI_History CLI/CLI.c)
target_link_libraries(CLI_History CMDList FIFOBuffChar history)
target_compile_definitions(CLI_History PRIVATE HISTORY)

View File

@ -123,7 +123,7 @@ int History_getPrev(History_t* history, char** line)
*line = NULL; *line = NULL;
retCode = -1; retCode = -1;
} }
return 0; return retCode;
} }
int History_getNext(History_t* history, char** line) int History_getNext(History_t* history, char** line)
@ -144,7 +144,7 @@ int History_getNext(History_t* history, char** line)
*line = NULL; *line = NULL;
retCode = -1; retCode = -1;
} }
return 0; return retCode;
} }
int History_getFullHistory(History_t* history, char*** list) int History_getFullHistory(History_t* history, char*** list)

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!