6 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
5 changed files with 62 additions and 35 deletions

View File

@@ -30,13 +30,14 @@ CLI_t CLI_init(CLI_charOutFn lineOut, CMDList_t* cmdList)
cli.CLI_charOut = lineOut; cli.CLI_charOut = lineOut;
cli.CMDList = cmdList; cli.CMDList = cmdList;
cli.CLI_State = CLI_State_Default; cli.CLI_State = CLI_State_Default;
cli.echo = true;
cli.FIFO = FIFOBuffChar_create(); cli.FIFO = FIFOBuffChar_create();
#ifdef HISTORY #ifdef HISTORY
cli.History = History_init(); cli.History = History_init();
#endif #endif
if (cli.CLI_charOut != NULL) if (cli.CLI_charOut != NULL && cli.echo)
{ {
CLI_stringOut(&cli, (char*)"> "); CLI_stringOut(&cli, (char*)"> ");
} }
@@ -128,7 +129,11 @@ void historyPrevius(CLI_t* cli)
// write line // write line
if ((ret >= 0) && (line != NULL)) if ((ret >= 0) && (line != NULL))
{ {
CLI_stringOut(cli, line); int i;
for (i=0; *(line + i) != '\0'; i++)
{
CLI_charIn(cli, *(line + i));
}
} }
} }
void historyNext(CLI_t* cli) void historyNext(CLI_t* cli)
@@ -148,7 +153,10 @@ void historyNext(CLI_t* cli)
if ((ret >= 0) && (line != NULL)) if ((ret >= 0) && (line != NULL))
{ {
int i; int i;
CLI_stringOut(cli, line); for (i=0; *(line + i) != '\0'; i++)
{
CLI_charIn(cli, *(line + i));
}
} }
} }
#endif #endif
@@ -161,7 +169,10 @@ int tryExecute(CLI_t* cli, FIFOBuffChar_t* fifo)
if (*line == '\0') if (*line == '\0')
{ // empty line { // empty line
ret = 1; ret = 1;
CLI_stringOut(cli, (char*)"> "); if (cli->echo)
{
CLI_stringOut(cli, (char*)"> ");
}
} }
else else
{ {
@@ -173,9 +184,9 @@ int tryExecute(CLI_t* cli, FIFOBuffChar_t* fifo)
#ifdef HISTORY #ifdef HISTORY
History_put(cli->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(cli, (char*)"> "); CLI_stringOut(cli, (char*)"> ");
} }
@@ -184,8 +195,12 @@ int tryExecute(CLI_t* cli, FIFOBuffChar_t* fifo)
else if (ret == 0) else if (ret == 0)
{ {
char err[100]; char err[100];
sprintf(&err[0], "command not found: %s\n> ", line); sprintf(&err[0], "command not found: %s\n", line);
CLI_stringOut(cli, &err[0]); CLI_stringOut(cli, &err[0]);
if (cli->echo)
{
CLI_stringOut(cli, (char*)"> ");
}
ret = -1; ret = -1;
#ifdef HISTORY #ifdef HISTORY
@@ -215,7 +230,10 @@ bool CLI_charIn(CLI_t* cli, char c)
if ((C >= ' ') && (C <= '~')) // see ascii table if ((C >= ' ') && (C <= '~')) // see ascii table
{ {
FIFOBuffChar_put(cli->FIFO, C); // save char in buffer FIFOBuffChar_put(cli->FIFO, C); // save char in buffer
CLI_charOut_save(cli, c); if (cli->echo)
{
CLI_charOut_save(cli, c);
}
} }
else else
{ {
@@ -224,7 +242,10 @@ bool CLI_charIn(CLI_t* cli, char c)
case '\n': case '\n':
case '\r': case '\r':
// echo to terminal // echo to terminal
CLI_charOut_save(cli, c); if (cli->echo)
{
CLI_charOut_save(cli, c);
}
// copy buffer and create new buffer // copy buffer and create new buffer
FIFOBuffChar_t* fifo = cli->FIFO; FIFOBuffChar_t* fifo = cli->FIFO;
cli->FIFO = FIFOBuffChar_create(); cli->FIFO = FIFOBuffChar_create();
@@ -238,7 +259,10 @@ bool CLI_charIn(CLI_t* cli, char c)
case 127: // delete (backspace) case 127: // delete (backspace)
if (FIFOBuffChar_pop(cli->FIFO)) if (FIFOBuffChar_pop(cli->FIFO))
{ // pop something of the buffer { // pop something of the buffer
CLI_stringOut(cli, (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;
@@ -247,8 +271,11 @@ bool CLI_charIn(CLI_t* cli, char c)
break; break;
default: default:
sprintf(&str[0], "\ninvlid char: '%c' - (%i)\n", c, c); if (cli->echo)
CLI_stringOut(cli, &str[0]); {
sprintf(&str[0], "\ninvlid char: '%c' - (%i)\n", c, c);
CLI_stringOut(cli, &str[0]);
}
break; break;
} }
} }
@@ -278,11 +305,17 @@ bool CLI_charIn(CLI_t* cli, char c)
historyNext(cli); historyNext(cli);
break; break;
case 'C': // arrow right case 'C': // arrow right
CLI_stringOut(cli, (char*)"(key: arrow right)"); if (cli->echo)
{
CLI_stringOut(cli, (char*)"(key: arrow right)");
}
cli->CLI_State = CLI_State_Default; cli->CLI_State = CLI_State_Default;
break; break;
case 'D': // arrow left case 'D': // arrow left
CLI_stringOut(cli, (char*)"(key: arrow left)"); if (cli->echo)
{
CLI_stringOut(cli, (char*)"(key: arrow left)");
}
cli->CLI_State = CLI_State_Default; cli->CLI_State = CLI_State_Default;
break; break;
#endif #endif

View File

@@ -4,7 +4,6 @@
#include <stdbool.h> #include <stdbool.h>
#include <config.h> #include <config.h>
#define HISTORY
#include "../CMDList/CMDList.h" #include "../CMDList/CMDList.h"
#include "../FIFOBuff/FIFOBuffChar.h" #include "../FIFOBuff/FIFOBuffChar.h"
@@ -27,6 +26,7 @@ typedef struct CLI {
History_t* History; History_t* History;
#endif #endif
CLI_State_t CLI_State; CLI_State_t CLI_State;
bool echo;
} CLI_t; } CLI_t;
// initilize and register the lineout print function // initilize and register the lineout print function
@@ -39,6 +39,8 @@ extern void CLI_PrintHistory(CLI_t* cli);
// to recive a single caracter // to recive a single caracter
bool CLI_charIn(CLI_t* cli, 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)