Browse Source

fix #43 added a config interface to change the newline character expected

prozessorkern 4 years ago
parent
commit
3b99ad2a56
5 changed files with 79 additions and 17 deletions
  1. 9 1
      .vscode/settings.json
  2. 3 1
      api/shellmatta.h
  3. 3 3
      example/main.c
  4. 15 9
      src/shellmatta.c
  5. 49 3
      test/integrationtest/test_integration.cpp

+ 9 - 1
.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"
     }
 }

+ 3 - 1
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,

+ 3 - 3
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;
 }

+ 15 - 9
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)
             {

+ 49 - 3
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);
+}
+