From 3b99ad2a56a264ab63a37db3a07dd2f776bf7b30 Mon Sep 17 00:00:00 2001 From: prozessorkern Date: Fri, 27 Mar 2020 18:34:43 +0100 Subject: [PATCH] fix #43 added a config interface to change the newline character expected --- .vscode/settings.json | 10 ++++- api/shellmatta.h | 4 +- example/main.c | 6 +-- src/shellmatta.c | 24 +++++++---- test/integrationtest/test_integration.cpp | 52 +++++++++++++++++++++-- 5 files changed, 79 insertions(+), 17 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 1428c38..ff59570 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,13 @@ { "files.associations": { - "random": "cpp" + "random": "cpp", + "*.tcc": "cpp", + "string": "cpp", + "vector": "cpp", + "fstream": "cpp", + "limits": "cpp", + "sstream": "cpp", + "utility": "cpp", + "algorithm": "cpp" } } \ No newline at end of file diff --git a/api/shellmatta.h b/api/shellmatta.h index 281413f..36e7f9c 100644 --- a/api/shellmatta.h +++ b/api/shellmatta.h @@ -140,6 +140,7 @@ typedef struct bool echoEnabled; /**< if true the input is printed */ bool dirty; /**< dirty flag to show changes */ const char *prompt; /**< prompt is printed after every command */ + char delimiter; /**< delimiter (return) to terminate a cmd */ shellmatta_mode_t mode; /**< mode of the shell */ shellmatta_write_t write; /**< pointer to write function */ shellmatta_cmd_t helpCmd; /**< help command structure */ @@ -172,7 +173,8 @@ shellmatta_retCode_t shellmatta_removeCmd( shellmatta_handle_t handle, shellmatta_retCode_t shellmatta_configure( shellmatta_handle_t handle, shellmatta_mode_t mode, - bool echoEnabled); + bool echoEnabled, + char delimiter); shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle, char *data, diff --git a/example/main.c b/example/main.c index 19224c7..1f37a43 100644 --- a/example/main.c +++ b/example/main.c @@ -52,7 +52,7 @@ 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); + shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r'); (void)arguments; (void)length; @@ -93,7 +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); + shellmatta_configure(handle, SHELLMATTA_MODE_OVERWRITE, true, '\r'); return SHELLMATTA_OK; } @@ -113,7 +113,7 @@ static shellmatta_retCode_t reset(shellmatta_handle_t handle, const char *argume shellmatta_resetShell(handle, false); } - shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, true); + shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, true, '\r'); return SHELLMATTA_OK; } diff --git a/src/shellmatta.c b/src/shellmatta.c index 9cd1ae0..bdb2be7 100644 --- a/src/shellmatta.c +++ b/src/shellmatta.c @@ -94,6 +94,7 @@ shellmatta_retCode_t shellmatta_doInit( inst->hereStartIdx = 0u; inst->hereDelimiterIdx = 0u; inst->hereLength = 0u; + inst->delimiter = '\r'; inst->mode = SHELLMATTA_MODE_INSERT; inst->cmdList = &(inst->helpCmd); inst->continuousCmd = NULL; @@ -322,10 +323,14 @@ shellmatta_retCode_t shellmatta_removeCmd(shellmatta_handle_t handle, shellmatta * @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 + * @param[in] delimiter delimiter used to detect the end of a cmd (default \r) * @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_retCode_t shellmatta_configure( shellmatta_handle_t handle, + shellmatta_mode_t mode, + bool echoEnabled, + char delimiter) { shellmatta_instance_t *inst = (shellmatta_instance_t*)handle; shellmatta_retCode_t ret = SHELLMATTA_OK; @@ -337,6 +342,7 @@ shellmatta_retCode_t shellmatta_configure(shellmatta_handle_t handle, shellmatta { inst->mode = mode; inst->echoEnabled = echoEnabled; + inst->delimiter = delimiter; } else { @@ -394,14 +400,8 @@ shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle, { escape_handleSequence(inst, *data); } - /** -# ignore newline as first character (to be compatible to - * terminals sending newline after return */ - else if((0u == inst->inputCount) && ('\n' == *data)) - { - /* do nothing */ - } - /** -# handle return as start of processing the command */ - else if ('\r' == *data) + /** -# handle delimiter as start of processing the command */ + else if (inst->delimiter == *data) { if(0u == inst->hereLength) { @@ -574,6 +574,12 @@ shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle, } } } + /** -# ignore newline as first character (to be compatible to + * terminals sending newline after return */ + else if((0u == inst->inputCount) && ('\n' == *data)) + { + /* do nothing */ + } /** -# check for tabulator key - auto complete */ else if('\t' == *data) { diff --git a/test/integrationtest/test_integration.cpp b/test/integrationtest/test_integration.cpp index 969a453..de4bbdf 100644 --- a/test/integrationtest/test_integration.cpp +++ b/test/integrationtest/test_integration.cpp @@ -459,7 +459,7 @@ TEST_CASE( "shellmatta configure disable echo" ) { write_length = 0u; /* turn off echo now */ - ret = shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false); + ret = shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r'); CHECK( ret == SHELLMATTA_OK ); /* check with echo disabled */ @@ -490,7 +490,7 @@ TEST_CASE( "shellmatta configure mode" ) { NULL, writeFct); shellmatta_addCmd(handle, &doSomethingCmd); - ret = shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false); + ret = shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r'); write_callCnt = 0u; memset(write_data, 0, sizeof(write_data)); @@ -507,7 +507,7 @@ TEST_CASE( "shellmatta configure mode" ) { write_length = 0u; /* check with overwrite mode */ - ret = shellmatta_configure(handle, SHELLMATTA_MODE_OVERWRITE, false); + ret = shellmatta_configure(handle, SHELLMATTA_MODE_OVERWRITE, false, '\r'); CHECK( ret == SHELLMATTA_OK ); /* check with echo disabled */ @@ -517,3 +517,49 @@ TEST_CASE( "shellmatta configure mode" ) { REQUIRE( strcmp(dummyData2, write_data) == 0); } +TEST_CASE( "shellmatta configure delimiter" ) { + + shellmatta_instance_t inst; + shellmatta_handle_t handle; + shellmatta_retCode_t ret; + char buffer[1024]; + char historyBuffer[1024]; + char *dummyData = (char*)"doSomething argument - length: 20\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, '\r'); + + write_callCnt = 0u; + memset(write_data, 0, sizeof(write_data)); + write_length = 0u; + + /* check with insert mode */ + shellmatta_processData(handle, (char*)"doSomething argument\n", 21u); + + CHECK( write_length == 0u); + + shellmatta_resetShell(handle, false); + write_callCnt = 0u; + memset(write_data, 0, sizeof(write_data)); + write_length = 0u; + + /* check with changed delimiter mode */ + ret = shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\n'); + CHECK( ret == SHELLMATTA_OK ); + + /* check with echo disabled */ + shellmatta_processData(handle, (char*)"doSomething argument\n", 21u); + + CHECK( write_length == strlen(dummyData)); + REQUIRE( strcmp(dummyData, write_data) == 0); +} +