Compare commits

..

No commits in common. "multie-cli" and "master" have entirely different histories.

6 changed files with 109 additions and 138 deletions

181
CLI/CLI.c
View File

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

View File

@ -3,43 +3,20 @@
#include <stdbool.h> #include <stdbool.h>
#include <config.h>
#include "../CMDList/CMDList.h" #include "../CMDList/CMDList.h"
#include "../FIFOBuff/FIFOBuffChar.h"
#ifdef HISTORY
#include "../History/history.h"
#endif
typedef int (*CLI_charOutFn)(const char* c); typedef int (*CLI_charOutFn)(const char* line);
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
CLI_t CLI_init(CLI_charOutFn lineOut, CMDList_t* cmdList); bool CLI_init(CLI_charOutFn lineOut, CMDList_t* cmdList);
bool CLI_deinit(CLI_t* cli); bool CLI_deinit();
#ifdef HISTORY #ifdef HISTORY
extern void CLI_PrintHistory(CLI_t* cli); extern void CLI_PrintHistory();
#endif #endif
// to recive a single caracter // to recive a single caracter
bool CLI_charIn(CLI_t* cli, char c); bool CLI_charIn(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, void* cli); int (*fn)(char* line);
} CMD_t; } CMD_t;
typedef struct CMDList_s { typedef struct CMDList_s {

View File

@ -123,7 +123,7 @@ int History_getPrev(History_t* history, char** line)
*line = NULL; *line = NULL;
retCode = -1; retCode = -1;
} }
return retCode; return 0;
} }
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 retCode; return 0;
} }
int History_getFullHistory(History_t* history, char*** list) int History_getFullHistory(History_t* history, char*** list)

View File

@ -1,25 +0,0 @@
{
"$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"
]
}
}

2
readme.md Normal file
View File

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