Compare commits
4 Commits
master
...
multie-cli
| Author | SHA1 | Date | |
|---|---|---|---|
| 42aec53edb | |||
| 8f71535dfd | |||
| 8261e605bc | |||
| f08c8e5788 |
213
CLI/CLI.c
213
CLI/CLI.c
@ -5,71 +5,53 @@
|
|||||||
#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;
|
||||||
|
|
||||||
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_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 +74,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 +131,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,34 +168,31 @@ int tryExecute(FIFOBuffChar_t* fifo)
|
|||||||
if (*line == '\0')
|
if (*line == '\0')
|
||||||
{ // empty line
|
{ // empty line
|
||||||
ret = 1;
|
ret = 1;
|
||||||
CLI_stringOut((char*)"> ");
|
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_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);
|
||||||
char err[100];
|
CLI_stringOut(cli, &err[0]);
|
||||||
sprintf(&err[0], "command not found: %s\n> ", line);
|
|
||||||
CLI_stringOut(&err[0]);
|
|
||||||
}
|
|
||||||
ret = -1;
|
ret = -1;
|
||||||
|
|
||||||
#ifdef HISTORY
|
#ifdef HISTORY
|
||||||
@ -223,7 +206,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 +216,13 @@ 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);
|
CLI_charOut_save(cli, c);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -247,32 +230,32 @@ bool CLI_charIn(char c)
|
|||||||
{
|
{
|
||||||
case '\n':
|
case '\n':
|
||||||
case '\r':
|
case '\r':
|
||||||
if (CLI_charOut != NULL)
|
// echo to terminal
|
||||||
{ // echo to terminal
|
CLI_charOut_save(cli, c);
|
||||||
char ch[2] = {c, 0};
|
// copy buffer and create new buffer
|
||||||
(*CLI_charOut)(&ch[0]);
|
FIFOBuffChar_t* fifo = cli->FIFO;
|
||||||
}
|
cli->FIFO = FIFOBuffChar_create();
|
||||||
FIFOBuffChar_t* fifo = FIFO;
|
// execute command if valid
|
||||||
FIFO = FIFOBuffChar_create();
|
ok = (tryExecute(cli, fifo) == 0);
|
||||||
ok = (tryExecute(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>"
|
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);
|
sprintf(&str[0], "\ninvlid char: '%c' - (%i)\n", c, c);
|
||||||
CLI_stringOut(&str[0]);
|
CLI_stringOut(cli, &str[0]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -281,11 +264,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 +277,27 @@ 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)");
|
CLI_stringOut(cli, (char*)"(key: arrow right)");
|
||||||
CLI_State = CLI_State_Default;
|
cli->CLI_State = CLI_State_Default;
|
||||||
break;
|
break;
|
||||||
case 'D': // arrow left
|
case 'D': // arrow left
|
||||||
CLI_stringOut((char*)"(key: arrow left)");
|
CLI_stringOut(cli, (char*)"(key: arrow left)");
|
||||||
CLI_State = CLI_State_Default;
|
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;
|
||||||
|
|||||||
35
CLI/CLI.h
35
CLI/CLI.h
@ -3,20 +3,43 @@
|
|||||||
|
|
||||||
#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;
|
||||||
|
} 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
|
||||||
@ -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 {
|
||||||
|
|||||||
@ -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
25
library.json
Normal 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"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user