Просмотр исходного кода

Merge branch 'feature/#9-add-remove-command-to-api' of shimatta/shellmatta into develop

shimatta 4 лет назад
Родитель
Сommit
4f9ff4fe3c
4 измененных файлов с 140 добавлено и 1 удалено
  1. 2 0
      api/shellmatta.h
  2. 11 0
      example/main.c
  3. 62 1
      src/shellmatta.c
  4. 65 0
      test/integrationtest/test_integration.cpp

+ 2 - 0
api/shellmatta.h

@@ -127,6 +127,8 @@ shellmatta_retCode_t shellmatta_doInit( shellmatta_instance_t   *inst,
                                         shellmatta_write_t      writeFct);
 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_processData(shellmatta_handle_t handle,
                                             char                *data,
                                             uint32_t            size);

+ 11 - 0
example/main.c

@@ -56,6 +56,16 @@ static shellmatta_retCode_t doSome(shellmatta_handle_t handle, const char *argum
 }
 shellmatta_cmd_t doSomeCmd = {"adoSome2", "adof2", "Function does something", "use me, please", doSome, NULL};
 
+static shellmatta_retCode_t removeCmdFct(shellmatta_handle_t handle, const char *arguments, uint32_t length)
+{
+    shellmatta_printf(handle, "removing command: %s\r\n", doSomeCmd.cmd);
+
+    shellmatta_removeCmd(handle, &doSomeCmd);
+
+    return SHELLMATTA_OK;
+}
+shellmatta_cmd_t removeCommand = {"remove", "r", "Function removes a command", "", removeCmdFct, NULL};
+
 
 static shellmatta_retCode_t quit(shellmatta_handle_t handle, const char *arguments, uint32_t length)
 {
@@ -101,6 +111,7 @@ int main(void)
     shellmatta_addCmd(handle, &doSomethingCmd);
     shellmatta_addCmd(handle, &doSomeCmd);
     shellmatta_addCmd(handle, &quitCommand);
+    shellmatta_addCmd(handle, &removeCommand);
 
     while(exitRequest == false)
     {

+ 62 - 1
src/shellmatta.c

@@ -183,6 +183,67 @@ shellmatta_retCode_t shellmatta_addCmd(shellmatta_handle_t handle, shellmatta_cm
     return ret;
 }
 
+/**
+ * @brief       removes a command from the command list
+ * @param[in]   handle  shellmatta instance handle
+ * @param[in]   cmd     pointer to the command to remove type #shellmatta_cmd_t
+ * @return      errorcode   #SHELLMATTA_OK
+ *                          #SHELLMATTA_USE_FAULT (param err)
+ */
+shellmatta_retCode_t shellmatta_removeCmd(shellmatta_handle_t handle, shellmatta_cmd_t *cmd)
+{
+    shellmatta_instance_t   *inst       = (shellmatta_instance_t*)handle;
+    shellmatta_cmd_t       *prevCmd;
+    shellmatta_cmd_t       *tempCmd;
+    shellmatta_retCode_t    ret         = SHELLMATTA_OK;
+
+    /** -# check parameters for plausibility  */
+    if(     (NULL               != inst)
+        &&  (SHELLMATTA_MAGIC   == inst->magic)
+        &&  (false              == inst->cmdListIsConst))
+    {
+        tempCmd     = inst->cmdList;
+        prevCmd     = NULL;
+
+        /*! -# loop through command list */
+        while(NULL != tempCmd)
+        {
+            /*! -# compare command strings to find the command to delete */
+            if (0 == strcmp(   tempCmd->cmd,
+                                cmd->cmd)
+                    && (strlen(tempCmd->cmd) == strlen(cmd->cmd)))
+            {
+                /*! -# first command removed */
+                if(NULL == prevCmd)
+                {
+                    inst->cmdList = tempCmd->next;
+                }
+                /*! -# last command removed */
+                else if(NULL == tempCmd->next)
+                {
+                    prevCmd->next = NULL;
+                }
+                /*! -# command removed from the middle of the list */
+                else
+                {
+                    prevCmd->next = tempCmd->next;
+                }
+                
+                break;
+            }
+
+            prevCmd = tempCmd;
+            tempCmd = tempCmd->next;
+        }
+    }
+    else
+    {
+        ret = SHELLMATTA_USE_FAULT;
+    }
+
+    return ret;
+}
+
 /**
  * @brief       processes the passed amount of data
  * @param[in]   handle  shellmatta instance handle
@@ -308,7 +369,7 @@ shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t     handle,
 
 
                         /* TODO it is difficult to store the complete command in the history buffer if it is restructured before...
-                         * So this should be an extra function that can be called after parsing the command and before calling the command funktion */
+                         * So this should be an extra function that can be called after parsing the command and before calling the command function */
                         for(idx = 1u; idx <= inst->hereStartIdx; idx++)
                         {
                             inst->buffer[inst->hereDelimiterIdx + inst->hereLength - idx] = inst->buffer[inst->hereStartIdx - idx];

+ 65 - 0
test/integrationtest/test_integration.cpp

@@ -161,3 +161,68 @@ TEST_CASE( "shellmatta heredoc test" ) {
     CHECK( doSomethingLength == 20u);
     REQUIRE( strcmp(dummyData, doSomethingArguments) == 0);
 }
+
+
+TEST_CASE( "shellmatta remove function" ) {
+
+    shellmatta_instance_t inst;
+    shellmatta_handle_t handle;
+    char buffer[1024];
+    char historyBuffer[1024];
+    char *dummyData =   (char*)"h\r\n"
+                        "doSomething  do  Function does something  use me, please\r\n"
+                        "help         h   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;
+
+    shellmatta_processData(handle, (char*)"h\r", 2);
+
+    CHECK( write_length == 123u);
+    CHECK( strcmp(dummyData, write_data) == 0);
+
+
+    write_callCnt = 0u;
+    memset(write_data, 0, sizeof(write_data));
+    write_length = 0u;
+
+    dummyData =     (char*)"h 564 321 56 465 46\r\n"
+                    "doSomething  do  Function does something  use me, please\r\n"
+                    "help         h   Print this help text     help\r\n"
+                    "\r\nshellmatta->";
+
+    shellmatta_processData(handle, (char*)"h 564 321 56 465 46\r", 20);
+
+    CHECK( write_length == 141u);
+    CHECK( strcmp(dummyData, write_data) == 0);
+
+    write_callCnt = 0u;
+    memset(write_data, 0, sizeof(write_data));
+    write_length = 0u;
+
+    shellmatta_removeCmd(handle, &doSomethingCmd);
+    shellmatta_processData(handle, (char*)"h 564 321 56 465 46\r", 20);
+
+    dummyData =     (char*)"h 564 321 56 465 46\r\n"
+                    "help  h  Print this help text  help\r\n"
+                    "\r\nshellmatta->";
+
+    printf("sdfsd sdf sdf sdf sdf sd fds\n%s", write_data);
+
+    CHECK( write_length == 72u);
+    REQUIRE( strcmp(dummyData, write_data) == 0);
+
+}