fix #15 added an api to control mode and echo + fixed the implementation and added tests

This commit is contained in:
prozessorkern 2020-03-08 19:56:04 +01:00
parent e970b6c941
commit 2921f9791b
7 changed files with 164 additions and 30 deletions

View File

@ -129,23 +129,27 @@ shellmatta_retCode_t shellmatta_doInit( shellmatta_instance_t *inst,
shellmatta_retCode_t shellmatta_resetShell( shellmatta_handle_t handle,
bool printPrompt);
shellmatta_retCode_t shellmatta_addCmd( shellmatta_handle_t handle,
shellmatta_cmd_t *cmd);
shellmatta_retCode_t shellmatta_addCmd( shellmatta_handle_t handle,
shellmatta_cmd_t *cmd);
shellmatta_retCode_t shellmatta_removeCmd( shellmatta_handle_t handle,
shellmatta_cmd_t *cmd);
shellmatta_retCode_t shellmatta_configure( shellmatta_handle_t handle,
shellmatta_mode_t mode,
bool echoEnabled);
shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
char *data,
uint32_t size);
shellmatta_retCode_t shellmatta_write( shellmatta_handle_t handle,
char *data,
uint32_t length);
shellmatta_retCode_t shellmatta_write( shellmatta_handle_t handle,
char *data,
uint32_t length);
#ifndef SHELLMATTA_STRIP_PRINTF
shellmatta_retCode_t shellmatta_printf( shellmatta_handle_t handle,
const char *fmt,
...);
shellmatta_retCode_t shellmatta_printf( shellmatta_handle_t handle,
const char *fmt,
...);
#endif
#endif

View File

@ -52,6 +52,8 @@ static shellmatta_retCode_t doSome(shellmatta_handle_t handle, const char *argum
shellmatta_write(handle, "blubb\r\n", 7u);
shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false);
(void)arguments;
(void)length;
@ -91,6 +93,7 @@ static shellmatta_retCode_t empty(shellmatta_handle_t handle, const char *argume
(void)length;
shellmatta_printf(handle, "empty function called\r\n");
shellmatta_configure(handle, SHELLMATTA_MODE_OVERWRITE, true);
return SHELLMATTA_OK;
}
@ -110,6 +113,8 @@ static shellmatta_retCode_t reset(shellmatta_handle_t handle, const char *argume
shellmatta_resetShell(handle, false);
}
shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, true);
return SHELLMATTA_OK;
}
shellmatta_cmd_t resetCommand = {"reset", NULL, "resets the shellmatta instance", "reset [prompt]", reset, NULL};

View File

@ -307,6 +307,35 @@ shellmatta_retCode_t shellmatta_removeCmd(shellmatta_handle_t handle, shellmatta
return ret;
}
/**
* @brief changes configuration of a shellmatta instance
* @param[in] handle shellmatta instance handle
* @param[in] mode insert mode of the shellmatta type #shellmatta_mode_t
* @param[in] echoEnabled true: echo received chars to the output
* @return errorcode #SHELLMATTA_OK
* #SHELLMATTA_USE_FAULT (param err)
*/
shellmatta_retCode_t shellmatta_configure(shellmatta_handle_t handle, shellmatta_mode_t mode, bool echoEnabled)
{
shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
shellmatta_retCode_t ret = SHELLMATTA_OK;
/** -# check parameters for plausibility */
if( (NULL != inst)
&& (SHELLMATTA_MAGIC == inst->magic)
&& ((mode == SHELLMATTA_MODE_INSERT) || (mode == SHELLMATTA_MODE_OVERWRITE)))
{
inst->mode = mode;
inst->echoEnabled = echoEnabled;
}
else
{
ret = SHELLMATTA_USE_FAULT;
}
return ret;
}
/**
* @brief processes the passed amount of data
* @param[in] handle shellmatta instance handle
@ -471,26 +500,18 @@ shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
/** -# search for a matching command */
while (NULL != cmd)
{
/** -# compare command string and length */
if ((0 == strncmp( argumentString,
/** -# compare command and alias string and length */
if ( ((0 == strncmp( argumentString,
cmd->cmd,
cmdLen))
&& (cmdLen == strlen(cmd->cmd)))
{
inst->write("\r\n", 2u);
cmdExecuted = 1u;
cmd->cmdFct(inst, argumentString, argumentLength);
cmd = NULL;
}
/** -# compare command alias if any and length */
else if((NULL != cmd->cmdAlias)
&& (cmdLen == strlen(cmd->cmd)))
|| ((NULL != cmd->cmdAlias)
&& ((0 == strncmp( argumentString,
cmd->cmdAlias,
cmdLen))
&& (cmdLen == strlen(cmd->cmdAlias))))
&& (cmdLen == strlen(cmd->cmdAlias)))))
{
inst->write("\r\n", 2u);
utils_writeEcho(inst, "\r\n", 2u);
cmdExecuted = 1u;
cmd->cmdFct(inst, argumentString, argumentLength);

View File

@ -25,7 +25,7 @@
#include <stdint.h>
/**
* @brief processes the excape character stream of the instance and chacks
* @brief processes the excape character stream of the instance and checks
* if an arrow key was pressed
* @param[in] inst pointer to a shellmatta instance
* @return #SHELLMATTA_OK if an arrow key was pressed

View File

@ -162,6 +162,7 @@ void utils_forwardCursor(shellmatta_instance_t *inst, uint32_t length)
* @param[in] inst pointer to shellmatta instance
* @param[in] data pointer to the data to be inserted
* @param[in] length length of the data to be inserted
* @todo this function shall check buffer overflows
*/
void utils_insertChars( shellmatta_instance_t *inst,
char *data,
@ -192,16 +193,20 @@ void utils_insertChars( shellmatta_instance_t *inst,
&(inst->buffer[inst->cursor + length]),
inst->inputCount - inst->cursor);
utils_restoreCursorPos(inst);
inst->cursor += length;
inst->inputCount += length;
}
/** -# just overwrite/append the chars */
/** -# overwrite - if the cursor reaches the end of the input it is pushed further */
else
{
memcpy(&(inst->buffer[inst->cursor]), data, length);
utils_writeEcho(inst, data, length);
inst->cursor += length;
if(inst->cursor > inst->inputCount)
{
inst->inputCount = inst->cursor;
}
}
inst->inputCount += length;
inst->cursor += length;
}
}

View File

@ -377,8 +377,107 @@ TEST_CASE( "shellmatta reset no prompt heredoc" ) {
/* now the new command should be processed */
shellmatta_processData(handle, (char*)"?\r", 2);
printf("%s", write_data);
CHECK( write_length == strlen(dummyData));
REQUIRE( strcmp(dummyData, write_data) == 0);
}
TEST_CASE( "shellmatta configure disable echo" ) {
shellmatta_instance_t inst;
shellmatta_handle_t handle;
shellmatta_retCode_t ret;
char buffer[1024];
char historyBuffer[1024];
char *dummyData = (char*)"help this is some dummy Text\r\n"
"doSomething do Function does something use me, please\r\n"
"help ? Print this help text help\r\n"
"\r\nshellmatta->";
char *dummyData2 = (char*)"doSomething do Function does something use me, please\r\n"
"help ? Print this help text help\r\n"
"\r\nshellmatta->";
shellmatta_doInit( &inst,
&handle,
buffer,
sizeof(buffer),
historyBuffer,
sizeof(historyBuffer),
"shellmatta->",
NULL,
writeFct);
shellmatta_addCmd(handle, &doSomethingCmd);
write_callCnt = 0u;
memset(write_data, 0, sizeof(write_data));
write_length = 0u;
/* check with echo enabled */
shellmatta_processData(handle, (char*)"help this is some dummy Text\r\n", 30u);
CHECK( write_length == strlen(dummyData));
CHECK( strcmp(dummyData, write_data) == 0);
write_callCnt = 0u;
memset(write_data, 0, sizeof(write_data));
write_length = 0u;
/* turn off echo now */
ret = shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false);
CHECK( ret == SHELLMATTA_OK );
/* check with echo disabled */
shellmatta_processData(handle, (char*)"help this is some dummy Text\r\n", 30u);
CHECK( write_length == strlen(dummyData2));
REQUIRE( strcmp(dummyData2, write_data) == 0);
}
TEST_CASE( "shellmatta configure mode" ) {
shellmatta_instance_t inst;
shellmatta_handle_t handle;
shellmatta_retCode_t ret;
char buffer[1024];
char historyBuffer[1024];
char *dummyData = (char*)"\r\nCommand: meow this is some dum123456my Text not found\r\nshellmatta->";
char *dummyData2 = (char*)"\r\nCommand: meow this is some dum123456t not found\r\nshellmatta->";
shellmatta_doInit( &inst,
&handle,
buffer,
sizeof(buffer),
historyBuffer,
sizeof(historyBuffer),
"shellmatta->",
NULL,
writeFct);
shellmatta_addCmd(handle, &doSomethingCmd);
ret = shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false);
write_callCnt = 0u;
memset(write_data, 0, sizeof(write_data));
write_length = 0u;
/* check with insert mode */
shellmatta_processData(handle, (char*)"meow this is some dummy Text\x1b[D\x1b[D\x1b[D\x1b[D\x1b[D\x1b[D\x1b[D123456\r\n", 57u);
CHECK( write_length == strlen(dummyData));
CHECK( strcmp(dummyData, write_data) == 0);
write_callCnt = 0u;
memset(write_data, 0, sizeof(write_data));
write_length = 0u;
/* check with overwrite mode */
ret = shellmatta_configure(handle, SHELLMATTA_MODE_OVERWRITE, false);
CHECK( ret == SHELLMATTA_OK );
/* check with echo disabled */
shellmatta_processData(handle, (char*)"meow this is some dummy Text\x1b[D\x1b[D\x1b[D\x1b[D\x1b[D\x1b[D\x1b[D123456\r\n", 57u);
CHECK( write_length == strlen(dummyData2));
REQUIRE( strcmp(dummyData2, write_data) == 0);
}

View File

@ -61,7 +61,7 @@ TEST_CASE( "shellmatta_insertChars overwrite" ) {
utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 4u);
CHECK( inst.cursor == 12);
CHECK( inst.inputCount == 14);
CHECK( inst.inputCount == 12);
CHECK( write_callCnt == 1u );
CHECK( strncmp("blks", write_data, 5u) == 0);
REQUIRE( strncmp("abcdefghblks\0\0\0\0\0\0\0\0\0", buffer, sizeof(buffer)) == 0);